def RunSimulation(): nest.set_verbosity(M_INFO) logger = Logger() logger.log('{} # virt_mem_0'.format(memory_thisjob())) # ----------------------- Network Construction ----------------------------- BuildNetwork(logger) # ---------------- Initial simulation: rig and calibrate ------------------- tic = time.time() nest.Prepare() nest.Run(params['inisimtime']) InitializationTime = time.time() - tic logger.log('{} # init_time'.format(InitializationTime)) logger.log('{} # virt_mem_after_init'.format(memory_thisjob())) # ----------------------- Cleanup and output ------------------------------- nest.Cleanup() logger.log('{} # num_neurons'.format(logger_params['num_nodes'])) logger.log('{} # num_connections'.format( nest.GetKernelStatus('num_connections'))) logger.log('{} # min_delay'.format(nest.GetKernelStatus('min_delay'))) logger.log('{} # max_delay'.format(nest.GetKernelStatus('max_delay')))
def connect(self): """ Connects the network. Recurrent connections among neurons of the neuronal populations are established, and recording and stimulation devices are connected. The ``self.__connect_*()`` functions use ``nest.Connect()`` calls which set up the postsynaptic connectivity. Since the introduction of the 5g kernel in NEST 2.16.0 the full connection infrastructure including presynaptic connectivity is set up afterwards in the preparation phase of the simulation. The preparation phase is usually induced by the first ``nest.Simulate()`` call. For including this phase in measurements of the connection time, we induce it here explicitly by calling ``nest.Prepare()``. """ self.__connect_neuronal_populations() if len(self.sim_dict['rec_dev']) > 0: self.__connect_recording_devices() if self.net_dict['poisson_input']: self.__connect_poisson_bg_input() if self.stim_dict['thalamic_input']: self.__connect_thalamic_stim_input() if self.stim_dict['dc_input']: self.__connect_dc_stim_input() nest.Prepare() nest.Cleanup()
def testAAAOverwriteFiles(self): nest.ResetKernel() mm_params = {"record_to": "ascii", "record_from": ["V_m"]} mm = nest.Create("multimeter", params=mm_params) try: os.remove(mm.get("filenames")[0]) except FileNotFoundError: pass nest.Connect(mm, nest.Create("iaf_psc_alpha")) nest.Simulate(100) nest.ResetKernel() mm = nest.Create("multimeter", params=mm_params) nest.Connect(mm, nest.Create("iaf_psc_alpha")) with self.assertRaises(nest.kernel.NESTErrors.IOError): nest.Simulate(100) nest.Cleanup() nest.SetKernelStatus({"overwrite_files": True}) nest.Simulate(100)
def simulate(self, dvs_data, reward_conditional, reward_collision, area_left, area_right, test): for synapse in self.conn_l: weight = nest.GetStatus([synapse], keys="weight")[0] nest.SetStatus( [synapse], { "n": (reward_conditional - reward_collision) * weight * p.reward_factor * 0.0001 }) for synapse in self.conn_r: weight = nest.GetStatus([synapse], keys="weight")[0] nest.SetStatus( [synapse], { "n": -(reward_conditional + reward_collision) * weight * p.reward_factor * 0.0001 }) time = nest.GetKernelStatus("time") # # nest.SetStatus(self.spike_generators_left, {"origin": time}) # nest.SetStatus(self.spike_generators_left, {"stop": p.sim_time}) # nest.SetStatus(self.spike_generators_right, {"origin": time}) # nest.SetStatus(self.spike_generators_right, {"stop": p.sim_time}) nest.SetStatus(self.spike_generators, {"origin": time}) nest.SetStatus(self.spike_generators, {"stop": p.sim_time}) dvs_data = dvs_data.reshape(dvs_data.size) for i in range(dvs_data.size): rate = dvs_data[i] / p.max_spikes rate = np.clip(rate, 0, 1) * p.max_poisson_freq nest.SetStatus([self.spike_generators[i]], {"rate": rate}) # if i < 16: # nest.SetStatus([self.spike_generators_left[i]], {"rate": rate}) # else: # nest.SetStatus([self.spike_generators_right[i % 16]], {"rate": rate}) if test: nest.Prepare() nest.Run(p.sim_time) nest.Cleanup() else: nest.Simulate(p.sim_time) n_l = nest.GetStatus(self.spike_detector, keys="n_events")[0] n_r = nest.GetStatus(self.spike_detector, keys="n_events")[1] nest.SetStatus(self.spike_detector, {"n_events": 0}) weights_l = np.array(nest.GetStatus( self.conn_l, keys="weight")).reshape(p.resolution) weights_r = np.array(nest.GetStatus( self.conn_r, keys="weight")).reshape(p.resolution) # weights_l = np.array(nest.GetStatus(self.conn_l, keys="weight")).reshape(4,4) # weights_r = np.array(nest.GetStatus(self.conn_r, keys="weight")).reshape(4,4) return n_l, n_r, weights_l, weights_r
def simulate(self): """ Create the network and execute simulation. Record used memory and wallclock time. """ t0 = time.time() self.base_memory = self.memory() self.prepare() t1 = time.time() self.time_prepare = t1 - t0 print("Prepared simulation in {0:.2f} seconds.".format( self.time_prepare)) self.create_recording_devices() self.create_areas() t2 = time.time() self.time_network_local = t2 - t1 print("Created areas and internal connections in {0:.2f} seconds.". format(self.time_network_local)) self.cortico_cortical_input() t3 = time.time() self.network_memory = self.memory() self.time_network_global = t3 - t2 print( "Created cortico-cortical connections in {0:.2f} seconds.".format( self.time_network_global)) self.save_network_gids() print("Network size:", nest.GetKernelStatus('network_size')) print("Saved network in {0:2f} seconds.".format(time.time() - t3)) t4 = time.time() nest.Prepare() nest.Run(10.) self.time_init = time.time() - t4 self.init_memory = self.memory() print("Init time in {0:.2f} seconds.".format(self.time_init)) t5 = time.time() #nest.Simulate(self.T) nest.Run(self.T) nest.Cleanup() t6 = time.time() self.time_simulate = t6 - t5 self.total_memory = self.memory() print("Simulated network in {0:.2f} seconds.".format( self.time_simulate)) self.logging()
def simulate_mpi_co_simulation(time_synch, end, logger): """ simulation with co-simulation :param time_synch: time of synchronization between all the simulator :param end : time of end simulation :param logger : logger simulation """ # Simulation count = 0.0 logger.info("Nest Prepare") nest.Prepare() while count * time_synch < end: # FAT END POINT logger.info(" Nest run time " + str(nest.GetKernelStatus('time'))) nest.Run(time_synch) logger.info(" Nest end") count += 1 logger.info("cleanup") nest.Cleanup() logger.info("finish") return
def run(self, dvs_data): """Run the SNN (use this for testing as weights are not changed).""" # Set poisson neuron firing time span time = nest.GetKernelStatus("time") nest.SetStatus(self.spike_generators, {"origin": time}) nest.SetStatus(self.spike_generators, {"stop": params.sim_time}) # Set poisson neuron firing frequency dvs_data = dvs_data.reshape(dvs_data.size) for i in range(dvs_data.size): rate = dvs_data[i] / params.max_spikes rate = np.clip(rate, 0, 1) * params.max_poisson_freq nest.SetStatus([self.spike_generators[i]], {"rate": rate}) # Run network in NEST nest.Prepare() nest.Run(params.sim_time) nest.Cleanup() # Get left and right output spikes n_l = nest.GetStatus(self.spike_detector, keys="n_events")[0] n_r = nest.GetStatus(self.spike_detector, keys="n_events")[1] # Reset output spike detector nest.SetStatus(self.spike_detector, {"n_events": 0}) return n_l, n_r
def run(self, stim=0, stim_rate=0.5, stim_tim=5000, n_trials=1): """run the simulation Chunk - to prevent from running unnecessary stim - with extra Poiss inoput""" # print("Simulating") # Simulate in chunk and check a special condition in between # Thus we avoid unwanted condition if self.chunk and stim != 1: nest.Prepare() self.chunk_times = np.arange(0, self.simtime + 1, self.chunk_size)[1::] if len(self.chunk_times) != int(self.simtime / self.chunk_size): raise ChunkError('check the simulation length') for ch in self.chunk_times: nest.Run(self.chunk_size) self.events_ex = nest.GetStatus(self.espikes, "n_events")[0] self.rate_ex = self.events_ex / ch * 1000.0 / self.N_rec if self.rate_ex > 200.0: #if self.verbose: print('rate is ', self.rate_ex) print('Rate is too high. Finishing simulation at %s ms' % (self.chunk_size)) self.simtime = ch #self.chunk_size break nest.Cleanup() elif stim == 1: self.nu_th = self.theta / ( self.J * self.KE * self.tauMem ) ## (theta * CMem) / (J_ex * CE * exp(1) * tauMem * tauSyn) #self.new_nu_ex = stim_eta * self.nu_th self.new_p_rate = stim_rate self.add_noise = nest.Create("poisson_generator") nest.Simulate(self.simtime) nest.SetStatus(self.add_noise, {"rate": self.new_p_rate}) nest.Connect(self.add_noise, self.nodes_ex[:100], syn_spec=self.syn_dict) #nest.Connect(self.add_noise,self.nodes_in[:100], syn_spec=self.syn_dict) for trial in range(n_trials): nest.SetStatus(self.add_noise, {"rate": self.new_p_rate}) nest.Simulate(stim_tim) nest.SetStatus(self.add_noise, {"rate": 0.0}) nest.Simulate(10000) else: #nest.Simulate(self.simtime) nest.Prepare() nest.Run(self.simtime) nest.Cleanup() if self.verbose: self.events_ex = nest.GetStatus(self.espikes, "n_events")[0] #self.events_in = nest.GetStatus(self.ispikes,"n_events")[0] self.rate_ex = self.events_ex / self.simtime * 1000.0 / self.N_rec #self.rate_in = self.events_in/self.simtime*1000.0/self.N_rec self.num_synapses = ( nest.GetDefaults("excitatory")["num_connections"] + nest.GetDefaults("inhibitory")["num_connections"] + nest.GetDefaults("external")["num_connections"]) print("Brunel network simulation (Python)") print("Number of neurons : {0}".format(self.N)) print("Number of synapses: {0}".format(self.num_synapses)) print(" Exitatory : {0}".format( int(self.KE * self.N) + self.N)) print(" Inhibitory : {0}".format(int(self.KI * self.N))) print("Rate : %.2f Hz" % self.rate_ex) #print("Inhibitory rate : %.2f Hz" % self.rate_in) print("Simulation time : %.2f s" % self.simtime)
# Creation of current generator dc = nest.Create("dc_generator", params={"amplitude": 900.0}) dc_2 = nest.Create("dc_generator", params={"amplitude": 1000.0}) print("create nodes") # Creation of connections nest.Connect(s_ex, n, syn_spec={"weight": 1000.0}) nest.Connect(s_in, n_2, syn_spec={"weight": 1000.0}) nest.Connect(n, m) nest.Connect(n_2, m) nest.Connect(s_ex, m_2) nest.Connect(s_in, m_2) nest.Connect(n, m_3) nest.Connect(n_2, m_3) nest.Connect(s_ex, m_4) nest.Connect(s_in, m_4) print("create connect") ''' A network simulation with a duration of 5*100 ms is started. ''' print("Spike generator 1 {} and 2 {}".format(s_in, s_ex)) nest.Prepare() print("Start run") nest.Run(200.) nest.Run(200.) nest.Run(200.) nest.Run(200.) nest.Cleanup() # print result print(nest.GetStatus(m_3)[0]['events']) print(nest.GetStatus(m_4)[0]['events'])
def create_network( path, nb_VP, nb_mpi, nb_run, time_sim, spike_generator=0, parrot=0, iaf=0, nb_mpi_recorder=0, separate=False, nb_mpi_generator_spike=0, nb_mpi_generator_current=0, shared_mpi_input=False, mix_mpi=0, ): """ configure Nest for the testing part :param path: path for saving sim :param nb_VP: number of virtual processing :param nb_mpi: number of MPI rank :param nb_run: number of run :param time_sim: time of 1 run :param spike_generator: number of devices :param parrot: number of parrot neurons :param iaf: number of model of neurons :param nb_mpi_recorder: number of MPI recording :param separate: boolean for the separation of not of the reference :param nb_mpi_generator_spike: number of mpi generator spike :param nb_mpi_generator_current: number of mpi current :param shared_mpi_input: shared of the mpi run ( need to valide the test ) :param mix_mpi: different case #TODO not yet implemented : creation of network where mpi input and output are connected :return: """ print(nb_VP, nb_run, time_sim, spike_generator, parrot, iaf, nb_mpi_recorder, separate, nb_mpi_generator_spike, nb_mpi_generator_current, shared_mpi_input, mix_mpi) sys.stdout.flush() # name of the test name = "nb_VP_"+str(nb_VP)+"nb_mpi"+str(nb_mpi)\ +'_D_'+str(int(spike_generator))+'_P_'+str(int(parrot))+'_N_'+str(int(iaf))\ +'_R_'+str(nb_mpi_recorder)+'_Separate_'+str(int(separate))\ +'_GS_'+str(nb_mpi_generator_spike) +'_GC_'+str(nb_mpi_generator_current)\ +'_SH_'+str(int(shared_mpi_input))+'_SM_'+str(mix_mpi) logger = create_logger( path + '/log/', name='nest_' + str(nest.Rank())) # TODO need to use it for the degging part nest.ResetKernel() nest.SetKernelStatus({ "overwrite_files": True, "data_path": path, "total_num_virtual_procs": nb_VP, }) # compute index and nb element for distinguish the different tests nb_element = int(parrot > 0) + int(spike_generator > 0) + int(iaf > 0) if nb_element == 0: raise Exception('Miss the configuration ' + str(nb_element)) if shared_mpi_input: if nb_mpi_recorder != 0 and nb_mpi_recorder % nb_element != 0: raise Exception('Miss nb recorder') if nb_mpi_generator_spike != 0 and nb_mpi_generator_spike % nb_element != 0: raise Exception('Miss nb spike generator') if nb_mpi_generator_current != 0 and nb_mpi_generator_current % nb_element != 0: raise Exception('Miss nb current generator') index_parrot = -2 index_device = -2 index_aif = -2 else: if parrot > 0: index_parrot = 0 if spike_generator > 0: index_device = 1 if iaf > 0: index_aif = 2 else: index_aif = -1 else: index_device = -1 if iaf > 0: index_aif = 1 else: index_aif = -1 else: index_parrot = -1 if spike_generator > 0: index_device = 0 if iaf > 0: index_aif = 1 else: index_aif = -1 else: index_device = -1 if iaf > 0: index_aif = 0 else: index_aif = -1 #create mpi device recorders = [] generator_spike = [] generator_current = [] for i in range(nb_mpi_recorder): recorders.append( nest.Create("spike_recorder", params={ "record_to": "mpi", "label": "file_record" })) for i in range(nb_mpi_generator_spike): generator_spike.append( nest.Create("spike_generator", params={ "spike_times": np.array([]), 'stimulus_source': 'mpi', "label": "file_gen_spike" })) for i in range(nb_mpi_generator_current): generator_current.append( nest.Create("step_current_generator", params={ 'amplitude_times': np.array([]), 'amplitude_values': np.array([]), 'stimulus_source': 'mpi', "label": "file_gen_current" })) # particular device poisson_generator = None if parrot > 0 and nb_mpi_recorder > 0: poisson_generator = nest.Create("poisson_generator") # recorder recorders_twins = [] recorder_parrot = [] recorder_device = [] recorder_neuron = [] for i in range(nb_mpi_recorder): recorders_twins.append( nest.Create("spike_recorder", params={"record_to": "memory"})) if parrot > 0: if (index_parrot != -1 and i % nb_element == index_parrot) or index_parrot == -2: recorder_parrot.append(nest.Create('parrot_neuron', parrot)) nest.Connect(poisson_generator, recorder_parrot[-1]) nest.Connect(recorder_parrot[-1], recorders[i]) nest.Connect(recorder_parrot[-1], recorders_twins[i]) if spike_generator > 0: if (index_device != -1 and i % nb_element == index_device) or index_device == -2: np.random.seed(recorders[i].tolist()[0]) recorder_device.append( nest.Create("spike_generator", spike_generator)) for j in range(spike_generator): recorder_device[-1][j].set({ "spike_times": np.around(np.sort( np.random.rand(100) * time_sim * nb_run), decimals=1).tolist() }) nest.Connect(recorder_device[-1], recorders[i]) if separate: np.random.seed(recorders[i].tolist()[0]) new_generator = nest.Create("spike_generator", spike_generator) for j in range(spike_generator): new_generator[j].set({ "spike_times": np.around(np.sort( np.random.rand(100) * time_sim * nb_run), decimals=1).tolist() }) nest.Connect(new_generator, recorders_twins[i]) else: nest.Connect(recorder_device[-1], recorders_twins[i]) if iaf > 0: if (index_aif != -1 and i % nb_element == index_aif) or index_aif == -2: recorder_neuron.append(nest.Create('iaf_psc_alpha', iaf)) nest.Connect(poisson_generator, recorder_neuron[-1]) nest.Connect(recorder_neuron[-1], recorders[i]) nest.Connect(recorder_neuron[-1], recorders_twins[i]) # generator spike generator_spike_twin = [] generator_spike_parrot = [] generator_spike_parrot_bis = None if separate: generator_spike_parrot_bis = [] generator_spike_device = [] generator_spike_device_bis = None if separate: generator_spike_device_bis = [] generator_spike_neuron = [] generator_spike_neuron_bis = None if separate: generator_spike_neuron_bis = [] for i in range(nb_mpi_generator_spike): data = generate_spike(generator_spike[i].tolist()[0], nb_run, time_sim)[1] data = np.concatenate(data).tolist() generator_spike_twin.append( nest.Create("spike_generator", params={"spike_times": data})) if parrot > 0: if (index_parrot != -1 and i % nb_element == index_parrot) or index_parrot == -2: new_parrot = nest.Create('parrot_neuron', parrot) generator_spike_parrot.append( nest.Create("spike_recorder", params={"record_to": "memory"})) nest.Connect(generator_spike[i], new_parrot) nest.Connect(new_parrot, generator_spike_parrot[-1]) if separate: new_parrot_2 = nest.Create('parrot_neuron', parrot) generator_spike_parrot_bis.append( nest.Create("spike_recorder", params={"record_to": "memory"})) nest.Connect(generator_spike_twin[i], new_parrot_2) nest.Connect(new_parrot_2, generator_spike_parrot_bis[-1]) else: nest.Connect(generator_spike_twin[i], new_parrot) if spike_generator > 0: if (index_device != -1 and i % nb_element == index_device) or index_device == -2: np.random.seed(generator_spike[-1].tolist()[0]) generator_spike_device.append( nest.Create("spike_recorder", spike_generator)) nest.Connect(generator_spike[i], generator_spike_device[-1]) if separate: generator_spike_device_bis.append( nest.Create("spike_recorder", spike_generator)) nest.Connect(generator_spike_twin[i], generator_spike_device_bis[-1]) else: nest.Connect(generator_spike_twin[i], generator_spike_device[-1]) if iaf > 0: if (index_aif != -1 and i % nb_element == index_aif) or index_aif == -2: neuron_test = nest.Create('iaf_psc_alpha', iaf) generator_spike_neuron.append(nest.Create("spike_recorder", 1)) nest.Connect(generator_spike[i], neuron_test) nest.Connect(neuron_test, generator_spike_neuron[-1]) if separate: generator_spike_neuron_bis.append( nest.Create("spike_recorder", 1)) nest.Connect(generator_spike_twin[i], generator_spike_neuron_bis[-1]) else: nest.Connect(generator_spike_twin[i], generator_spike_neuron[-1]) # generator generator_current_twin = [] generator_current_parrot = [] generator_current_parrot_bis = None if separate: generator_current_parrot_bis = [] generator_current_device = [] generator_current_device_bis = None if separate: generator_current_device_bis = [] generator_current_neuron = [] generator_current_neuron_bis = None if separate: generator_current_neuron_bis = [] for i in range(nb_mpi_generator_current): data = np.concatenate( generate_current(generator_current[i].tolist()[0], nb_run, time_sim)[1]) generator_current_twin.append( nest.Create("step_current_generator", params={ 'amplitude_times': data[:, 0], 'amplitude_values': data[:, 1], })) if parrot > 0: if (index_parrot != -1 and i % nb_element == index_parrot) or index_parrot == -2: new_neuron = nest.Create('iaf_cond_alpha', parrot) generator_current_parrot.append( nest.Create("multimeter", params={ 'record_from': ['V_m'], "record_to": "memory" })) nest.Connect(generator_current[i], new_neuron) nest.Connect(generator_current_parrot[-1], new_neuron) if separate: new_neuron_2 = nest.Create('iaf_cond_alpha', parrot) generator_current_parrot_bis.append( nest.Create("multimeter", params={ 'record_from': ['V_m'], "record_to": "memory" })) nest.Connect(generator_current_twin[i], new_neuron_2) nest.Connect(generator_current_parrot_bis[-1], new_neuron_2) else: nest.Connect(generator_current_twin[i], new_neuron) if spike_generator > 0: if (index_device != -1 and i % nb_element == index_device) or index_device == -2: neuron_test = nest.Create('iaf_cond_alpha', iaf) generator_current_device.append( nest.Create("multimeter", params={ 'record_from': ['V_m'], "record_to": "memory" })) nest.Connect(generator_current[i], neuron_test) nest.Connect(generator_current_device[-1], neuron_test) if separate: neuron_test_2 = nest.Create('iaf_cond_alpha', iaf) generator_current_device_bis.append( nest.Create("multimeter", params={ 'record_from': ['V_m'], "record_to": "memory" })) nest.Connect(generator_current_device_bis[-1], neuron_test) nest.Connect(generator_current_twin[i], neuron_test_2) else: nest.Connect(generator_current_twin[i], neuron_test) if iaf > 0: if (index_aif != -1 and i % nb_element == index_aif) or index_aif == -2: neuron_test = nest.Create('iaf_psc_alpha', iaf) generator_current_neuron.append( nest.Create("multimeter", params={ 'record_from': ['V_m'], "record_to": "memory" })) nest.Connect(generator_current[i], neuron_test) nest.Connect(generator_current_neuron[-1], neuron_test) if separate: neuron_test_2 = nest.Create('iaf_psc_alpha', iaf) generator_current_neuron_bis.append( nest.Create("multimeter", params={ 'record_from': ['V_m'], "record_to": "memory" })) nest.Connect(generator_current_neuron_bis[-1], neuron_test) nest.Connect(generator_current_twin[i], neuron_test_2) else: nest.Connect(generator_current_twin[i], neuron_test) # start simulation nest.Prepare() for i in range(nb_run): nest.Run(time_sim) nest.Cleanup() # write result in file for the validation devices_record_memory = [ ('recorders_memory', recorders_twins), ('GS_parrot', generator_spike_parrot), ('GS_parrot_bis', generator_spike_parrot_bis), ('GS_spike_device', generator_spike_device), ('GS_spike_device_bis', generator_spike_device_bis), ('GS_spike_neuron', generator_spike_neuron), ('GS_spike_neuron_bis', generator_spike_neuron_bis), ('GC_parrot', generator_current_parrot), ('GC_parrot_bis', generator_current_parrot_bis), ('GC_device', generator_current_device), ('GC_device_bis', generator_current_device_bis), ('GC_neuron', generator_current_neuron), ('GC_neuron_bis', generator_current_neuron_bis) ] for label, devices in devices_record_memory: if devices is not None: data_collect = [] for device in devices: for data in nest.GetStatus(device): data_collect.append(data['events']) name_save = path + '/' + label + '_rank_' + str( nest.Rank()) + '.npy' np.save(name_save, data_collect, allow_pickle=True)