Debian Codegraph @

examples/advanced/custom_events.py

raw
1 """
2 Example demonstrating the use of custom events.
3
4 Here we have three neurons, the first is Poisson spiking and connects to neuron G,
5 which in turn connects to neuron H. Neuron G has two variables v and g, and the
6 incoming Poisson spikes cause an instantaneous increase in variable g. g decays
7 rapidly, and in turn causes a slow increase in v. If v crosses a threshold, it
8 causes a standard spike and reset. If g crosses a threshold, it causes a custom
9 event ``gspike``, and if it returns below that threshold it causes a custom
10 event ``end_gspike``. The standard spike event when v crosses a threshold
11 causes an instantaneous increase in variable x in neuron H (which happens
12 through the standard ``pre`` pathway in the synapses), and the gspike
13 event causes an increase in variable y (which happens through the custom
14 pathway ``gpath``).
15 """
16 from brian2 import *
17 # Input Poisson spikes
18 inp = PoissonGroup(1, rates=250*Hz)
19 # First group G
20 eqs_G = '''
21 dv/dt = (g-v)/(50*ms) : 1
22 dg/dt = -g/(10*ms) : 1
23 allow_gspike : boolean
24 '''
25 G = NeuronGroup(1, eqs_G, threshold='v>1',
26 reset='v = 0; g = 0; allow_gspike = True;',
27 events={'gspike': 'g>1 and allow_gspike',
28 'end_gspike': 'g<1 and not allow_gspike'})
29 G.run_on_event('gspike', 'allow_gspike = False')
30 G.run_on_event('end_gspike', 'allow_gspike = True')
31 # Second group H
32 eqs_H = '''
33 dx/dt = -x/(10*ms) : 1
34 dy/dt = -y/(10*ms) : 1
35 '''
36 H = NeuronGroup(1, eqs_H)
37 # Synapses from input Poisson group to G
38 Sin = Synapses(inp, G, on_pre='g += 0.5')
39 Sin.connect()
40 # Synapses from G to H
41 S = Synapses(G, H,
42 on_pre={'pre': 'x += 1',
43 'gpath': 'y += 1'},
44 on_event={'pre': 'spike',
45 'gpath': 'gspike'})
46 S.connect()
47 # Monitors
48 Mstate = StateMonitor(G, ('v', 'g'), record=True)
49 Mgspike = EventMonitor(G, 'gspike', 'g')
50 Mspike = SpikeMonitor(G, 'v')
51 MHstate = StateMonitor(H, ('x', 'y'), record=True)
52 # Initialise and run
53 G.allow_gspike = True
54 run(500*ms)
55 # Plot
56 figure(figsize=(10, 4))
57 subplot(121)
58 plot(Mstate.t/ms, Mstate.g[0], '-g', label='g')
59 plot(Mstate.t/ms, Mstate.v[0], '-b', lw=2, label='V')
60 plot(Mspike.t/ms, Mspike.v, 'ob', label='_nolegend_')
61 plot(Mgspike.t/ms, Mgspike.g, 'og', label='_nolegend_')
62 xlabel('Time (ms)')
63 title('Presynaptic group G')
64 legend(loc='best')
65 subplot(122)
66 plot(MHstate.t/ms, MHstate.y[0], '-r', label='y')
67 plot(MHstate.t/ms, MHstate.x[0], '-k', lw=2, label='x')
68 xlabel('Time (ms)')
69 title('Postsynaptic group H')
70 legend(loc='best')
71 tight_layout()
72 show()