def test_store_restore_magic(): source = NeuronGroup(10, '''dv/dt = rates : 1 rates : Hz''', threshold='v>1', reset='v=0') source.rates = 'i*100*Hz' target = NeuronGroup(10, 'v:1') synapses = Synapses(source, target, model='w:1', pre='v+=w', connect='i==j') synapses.w = 'i*1.0' synapses.delay = 'i*ms' state_mon = StateMonitor(target, 'v', record=True) spike_mon = SpikeMonitor(source) store() # default time slot run(10*ms) store('second') run(10*ms) v_values = state_mon.v[:, :] spike_indices, spike_times = spike_mon.it_ restore() # Go back to beginning assert magic_network.t == 0*ms run(20*ms) assert defaultclock.t == 20*ms assert_equal(v_values, state_mon.v[:, :]) assert_equal(spike_indices, spike_mon.i[:]) assert_equal(spike_times, spike_mon.t_[:]) # Go back to middle restore('second') assert magic_network.t == 10*ms run(10*ms) assert defaultclock.t == 20*ms assert_equal(v_values, state_mon.v[:, :]) assert_equal(spike_indices, spike_mon.i[:]) assert_equal(spike_times, spike_mon.t_[:])
def test_store_restore_magic_to_file(): filename = tempfile.mktemp(suffix='state', prefix='brian_test') source = NeuronGroup(10, '''dv/dt = rates : 1 rates : Hz''', threshold='v>1', reset='v=0') source.rates = 'i*100*Hz' target = NeuronGroup(10, 'v:1') synapses = Synapses(source, target, model='w:1', pre='v+=w', connect='i==j') synapses.w = 'i*1.0' synapses.delay = 'i*ms' state_mon = StateMonitor(target, 'v', record=True) spike_mon = SpikeMonitor(source) store(filename=filename) # default time slot run(10 * ms) store('second', filename=filename) run(10 * ms) v_values = state_mon.v[:, :] spike_indices, spike_times = spike_mon.it_ restore(filename=filename) # Go back to beginning assert magic_network.t == 0 * ms run(20 * ms) assert defaultclock.t == 20 * ms assert_equal(v_values, state_mon.v[:, :]) assert_equal(spike_indices, spike_mon.i[:]) assert_equal(spike_times, spike_mon.t_[:]) # Go back to middle restore('second', filename=filename) assert magic_network.t == 10 * ms run(10 * ms) assert defaultclock.t == 20 * ms assert_equal(v_values, state_mon.v[:, :]) assert_equal(spike_indices, spike_mon.i[:]) assert_equal(spike_times, spike_mon.t_[:]) try: os.remove(filename) except OSError: pass
def test_filterbankgroup_restart(): sound1 = tone(1 * kHz, .1 * second) sound2 = whitenoise(.1 * second) sound = sound1 + sound2 sound = sound.ramp() cf = erbspace(20 * Hz, 20 * kHz, 3000) cochlea = Gammatone(sound, cf) # Half-wave rectification and compression [x]^(1/3) ihc = FunctionFilterbank(cochlea, lambda x: 3 * np.clip(x, 0, np.inf) ** (1.0 / 3.0)) # Leaky integrate-and-fire model with noise and refractoriness eqs = ''' dv/dt = (I-v)/(1*ms)+0.2*xi*(2/(1*ms))**.5 : 1 (unless refractory) I : 1 ''' anf = FilterbankGroup(ihc, 'I', eqs, reset='v=0', threshold='v>1', refractory=5*ms, method='euler') M = SpikeMonitor(anf) store() run(sound.duration) restore() run(sound.duration)
def test_store_restore_magic_to_file(): filename = tempfile.mktemp(suffix='state', prefix='brian_test') source = NeuronGroup(10, '''dv/dt = rates : 1 rates : Hz''', threshold='v>1', reset='v=0') source.rates = 'i*100*Hz' target = NeuronGroup(10, 'v:1') synapses = Synapses(source, target, model='w:1', on_pre='v+=w') synapses.connect(j='i') synapses.w = 'i*1.0' synapses.delay = 'i*ms' state_mon = StateMonitor(target, 'v', record=True) spike_mon = SpikeMonitor(source) store(filename=filename) # default time slot run(10*ms) store('second', filename=filename) run(10*ms) v_values = state_mon.v[:, :] spike_indices, spike_times = spike_mon.it_ restore(filename=filename) # Go back to beginning assert magic_network.t == 0*ms run(20*ms) assert defaultclock.t == 20*ms assert_equal(v_values, state_mon.v[:, :]) assert_equal(spike_indices, spike_mon.i[:]) assert_equal(spike_times, spike_mon.t_[:]) # Go back to middle restore('second', filename=filename) assert magic_network.t == 10*ms run(10*ms) assert defaultclock.t == 20*ms assert_equal(v_values, state_mon.v[:, :]) assert_equal(spike_indices, spike_mon.i[:]) assert_equal(spike_times, spike_mon.t_[:]) try: os.remove(filename) except OSError: pass
def main2(plot=True): # Optimizing the code from main1 by only making the networks just once before the loop # running multiple separate simulations bs.start_scope() num_inputs = 100 input_rate = 10 * bs.Hz weight = 0.1 # range of time constants tau_range = bs.linspace(start=1, stop=10, num=30) * bs.ms # storing output rates output_rates = [] P = bs.PoissonGroup(num_inputs, rates=input_rate) eqs = """ dv/dt = -v/tau : 1 """ G = bs.NeuronGroup(1, eqs, threshold='v>1', reset='v=0', method='exact') # connect the external spike source with the neurons # instead of connecting the same neurons like in previous examples S = bs.Synapses(P, G, on_pre='v += weight') S.connect() # visualise_connectivity(S) spike_monitor = bs.SpikeMonitor(source=G) bs.store() # stores the current state of the network for tau in tau_range: # calling this allows for resetting of the network to its original state bs.restore() bs.run(1 * bs.second) n_spikes = spike_monitor.num_spikes output_rates.append(n_spikes / bs.second) if plot: plt.clf() plt.plot(tau_range / bs.ms, output_rates) plt.xlabel(r'$\tau$ (ms)') plt.ylabel('Firing Rate (spikes/s)') plt.show()
firingratesgpi = [] firingratesgpta = [] firingratesgpti = [] firingratesstn = [] b2.store() c_var = np.arange(0., 0.11, 0.01) # c_var = [0.] # file_path = os.path.join(os.environ['USERPROFILE'], 'Desktop', 'frandff.txt') file_path = '/home/f_mastellone/frandff.txt' text_file = open(file_path, "w") for i, c_i in enumerate(c_var): b2.restore() c = c_i for spikemonitor in spikemonitors: spikemonitor.active = False b2.run(300 * b2.ms) for spikemonitor in spikemonitors: spikemonitor.active = True b2.run(duration) text_file.write(f"Correlation parameter: {c}\n") print(f"Correlation parameter: {c}\n") text_file.write(
time_per_neuron = 400 * b.ms n_times = 100 n_neurons = 2 freq = 50 * b.Hz P = b.PoissonGroup(1, freq) M = b.SpikeMonitor(P) b.store() print("Simulating-1...", end='', flush=True) spike_times = [] spike_indices = [] start_time = 0 for k in range(n_times): for n in range(n_neurons): b.restore() b.run(time_per_neuron) percentage = 100.0 * (k * n_neurons + n) / (n_times * n_neurons + 0.0) print("\rSimulating-1... t={}%".format(round(percentage, 2)), end='', flush=True) spike_times += [a + start_time for a in list(M.t)] spike_indices += [n] * M.num_spikes start_time += time_per_neuron print("\rSimulating-1... done") print("Simulating-2... ", end='', flush=True) b.restore() b.run(time_per_neuron * n_neurons * n_times) spike_times += list(M.t) spike_indices += [n_neurons] * M.num_spikes