Debian Codegraph @

examples/network_operation_stop.py

raw
1 """
2 Example: Using `network_operation` to stop a simulation.
3 This script demonstrates how to use `network_operation` to stop a simulation
4 when a group of neurons reaches a target number of spikes.
5 """
6
7 from brian2 import *
8
9 N = 10
10 tau = 10*ms
11 I = 1.5
12 target_spikes = 20
13
14 G = NeuronGroup(N, 'dv/dt = (I - v)/tau : 1', threshold='v>1', reset='v=0', method='exact')
15 G.v = 'rand()'
16
17 spike_mon = SpikeMonitor(G)
18
19 @network_operation(dt=1*ms)
20 def check_spikes():
21 if spike_mon.num_spikes >= target_spikes:
22 print(f"Stopped at t={defaultclock.t} with {spike_mon.num_spikes} spikes")
23 stop()
24
25 run(100*ms)
26
27 plot(spike_mon.t/ms, spike_mon.i, '.k')
28 xlabel('Time (ms)')
29 ylabel('Neuron index')
30 title('Spike raster plot')
31 show()