Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

https://github.com/jacopobono/learning_cognitive_maps_code
24 February 2026, 16:03:07 UTC
  • Code
  • Branches (2)
  • Releases (0)
  • Visits
    • Branches
    • Releases
    • HEAD
    • refs/heads/first_branch
    • refs/heads/main
    No releases to show
  • 5701fc7
  • /
  • main_MC_random_walk.py
Raw File Download Save again
Take a new snapshot of a software origin

If the archived software origin currently browsed is not synchronized with its upstream version (for instance when new commits have been issued), you can explicitly request Software Heritage to take a new snapshot of it.

Use the form below to proceed. Once a request has been submitted and accepted, it will be processed as soon as possible. You can then check its processing state by visiting this dedicated page.
swh spinner

Processing "take a new snapshot" request ...

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
  • revision
  • snapshot
origin badgecontent badge
swh:1:cnt:07707437fffe6bbdeae56ede2ba716c9d881f614
origin badgedirectory badge
swh:1:dir:5701fc7fe0b96ca0f3351dbf9c6f6ed4627884f1
origin badgerevision badge
swh:1:rev:8aed486bb756b5aed6085f9b2a60e49d65445904
origin badgesnapshot badge
swh:1:snp:3e11e29283bbaf71786af24d2d77c12596b6e584

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
  • revision
  • snapshot
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
Tip revision: 8aed486bb756b5aed6085f9b2a60e49d65445904 authored by jacopobono on 29 August 2021, 13:07:26 UTC
first commit
Tip revision: 8aed486
main_MC_random_walk.py
import numpy as np
import pylab as plt 
from utils_learning_rules import fetch_parameters_new, theor_model2
from utils_environment import T_random_walk_linear_track
# from utils_plot import plot_weights_stdp_multiple_runs
from utils_nn import run_MC
import pickle 
from multiprocessing import Pool 
from main_compareMLmodel12_random_walk_parall import root_mse
import datetime, os, string


def parall_fun(traj):
    
    traj,nr,ini,path = traj
    print(nr)
    
    params, gamma_var, eta, _ = fetch_parameters_new()
    
    temporal_same = 2#.1#params['tau_plus'] * np.log(params['A_plus'])
    temporal_between = -np.log(gamma_var)*params['tau_plus']
    params['temporal_same'] = temporal_same
    params['temporal_between'] = temporal_between#15#temporal_between
    
    # print('temporal_same: ' + str(temporal_same))
    # print('temporal_between: ' + str(temporal_between))
    
    params['eta_stdp'] = eta/(params['A_plus']*np.exp(-temporal_same/params['tau_plus']))

    eta_replay = params['eta_stdp'] *params['A_plus']*np.exp(-temporal_same/params['tau_plus'])
    gamma_replay = np.exp(-temporal_between/params['tau_plus'])

    print('Eta replay: '+str(eta_replay))
    print('Gamma replay: '+str(gamma_replay))


    params['trajectories'] = traj 
    M_stdp = run_MC(**params,spike_noise_prob=0.25)#,ini='random')
    
    with open(path+f'/MC_traj_{ini+nr}.pkl','wb') as f:
        pickle.dump(M_stdp,f)
    return M_stdp#, M_evo]

if __name__ == '__main__':

    ### close all plots
    plt.close('all')
    
    ### create custom path
    newpath = str(datetime.datetime.now()) 
    newpath= newpath.translate(str.maketrans(string.punctuation, '_'*len(string.punctuation)))
    newpath=newpath.replace(" ", "_")
    newpath = 'MC_runs/'+newpath
    
    ### make directory
    os.makedirs(newpath)
    
    ### store copy of the script 
    # filename = __file__
    # filename = filename.split("/")
    # filename = filename[-1]
    # shutil.copy(filename, newpath)
      
    ### load all trajectories
    traj = pickle.load(open('1000trajectories.pkl', 'rb'))
    
    ### parameters
    ini_run = 200
    fin_run = 1000
    assert fin_run>ini_run, 'should have more than 1 run!'
    tot_runs = fin_run-ini_run
    tot_epochs = 75
    
    ### select trajectories
   
    traj = [[t[:] for t in x[:tot_epochs]] for x in traj[ini_run:fin_run]]
    #traj = [t[:tot_epochs] for t in traj[:1]]*tot_runs 
    #traj = [np.repeat(np.arange(3).reshape(-1,1).T, tot_epochs, axis=0)]
    
    ### parallel processing
    pool = Pool(4) 
    output = pool.map(parall_fun, zip(traj,range(len(traj)),[ini_run]*len(traj),[newpath]*len(traj) ) )
    # print(np.shape(output))
    output=np.array(output)
    output = output[:,1,:,:,:]
    
    ### store results
    # pickle.dump(output, open(newpath + '/output_stdp.pkl','wb'))
    
    #    
    w_stdps2 = np.array(output)#[:, 1, :, :]
    # pickle.dump(w_stdps2, open(newpath + '/w_stdps2.pkl','wb'))
    # 
    init = np.tile(np.expand_dims(np.expand_dims(np.eye(4),0),0),[tot_runs,1,1,1])
    w_stdps2 = np.concatenate([init,w_stdps2],axis=1)
    
    
    params, gamma_var, eta, _ = fetch_parameters_new()
    T_mat = T_random_walk_linear_track(params['no_states']-1)
    M_theor = np.linalg.inv(np.eye(params['no_states'])-gamma_var*T_mat)
    #
    fig = plt.figure()
    for w1 in range(params['no_states']):
        for w2 in range(params['no_states']):
            if 6 not in [w1,w2]:
                plt.plot(w_stdps2[:, :, w1, w2].mean(axis=0))
                plt.plot([0,len(w_stdps2[0,:,0,0])],[M_theor[w1, w2],M_theor[w1, w2]])
    # fig.savefig(newpath + '/weights.png')
    
    fig2 = plt.figure()
    root_mse_TD_spiking,std = root_mse(w_stdps2[:,:, :-1, :-1], M_theor[:-1, :-1], 0)
    plt.plot(root_mse_TD_spiking)   
    plt.fill_between(range(len(std)), root_mse_TD_spiking-std, root_mse_TD_spiking+std,alpha=.3)
    # fig2.savefig(newpath + '/MSE_TD_spiking.png')
    pickle.dump(root_mse_TD_spiking, open('rmse_MC_rnd_0.1.pkl','wb'))
    
    M_theor_evo = [None]*tot_runs
    for i in range(len(traj)):
        M_theor_evo[i] = theor_model2(traj[i], gamma_var, params['no_states'], eta)
    # pickle.dump(M_theor_evo, open(newpath + '/M_evo.pkl','wb'))
       
    # for w1 in range(params['no_states']):
    #     for w2 in range(params['no_states']):
    #         fig3 = plt.figure()
    #         # print(w1)
    #         # print(w2)
    #         plt.plot(w_stdps2[:, :, w1, w2].mean(axis=0), label = 'stdp')
    #         plt.plot(np.array(M_theor_evo)[:, :, w1, w2].mean(axis=0), label = 'MC')
    #         plt.plot([0,len(w_stdps2[0,:,0,0])],[M_theor[w1, w2],M_theor[w1, w2]])
    #         plt.title(f'{w1} {w2}')
    #         # fig3.savefig(newpath + '/evolution.png')
   
    

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API