File: developer-simulationprinciples.txt

package info (click to toggle)
brian 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, stretch
  • size: 23,436 kB
  • sloc: python: 68,707; cpp: 29,040; ansic: 5,182; sh: 111; makefile: 61
file content (231 lines) | stat: -rw-r--r-- 5,672 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
Simulation principles
---------------------

The following paper outlines the principles of Brian simulation: Goodman, D and
Brette R (2008),
`Brian: a simulator for spiking neural networks in Python <http://www.frontiersin.org/neuroinformatics/paper/10.3389/neuro.11/005.2008/>`__,
Front. Neuroinform. doi:10.3389/neuro.11.005.2008.

This one describes the simulation algorithms, which are based on vectorisation:
Brette R and Goodman, DF,
`Vectorised algorithms for spiking neural network simulation <http://www.briansimulator.org/WordPress/wp-content/uploads/2010/10/algorithms-preprint.pdf>`__,
Neural Computation (in press).

Sample script
~~~~~~~~~~~~~

Below we present a Brian script, and a translation into pure Python to
illustrate the basic principles of Brian simulations.

Original Brian script
.....................

A script in Brian::

	'''
	Very short example program.
	'''
	from brian import *
	from time import time
	
	N=10000        # number of neurons
	Ne=int(N*0.8) # excitatory neurons 
	Ni=N-Ne       # inhibitory neurons
	p=80./N
	duration=1000*ms
	
	eqs='''
	dv/dt = (ge+gi-(v+49*mV))/(20*ms) : volt
	dge/dt = -ge/(5*ms) : volt
	dgi/dt = -gi/(10*ms) : volt
	'''
	
	P=NeuronGroup(N,model=eqs,
	              threshold=-50*mV,reset=-60*mV)
	P.v=-60*mV+10*mV*rand(len(P))
	Pe=P.subgroup(Ne)
	Pi=P.subgroup(Ni)
	
	Ce=Connection(Pe,P,'ge',weight=1.62*mV,sparseness=p)
	Ci=Connection(Pi,P,'gi',weight=-9*mV,sparseness=p)
	
	M=SpikeMonitor(P)
	trace=StateMonitor(P,'v',record=0)
	
	t1=time()
	run(1*second)
	t2=time()
	print "Simulated in",t2-t1,"s"
	print len(M.spikes),"spikes"
	
	subplot(211)
	raster_plot(M)
	subplot(212)
	plot(trace.times/ms,trace[0]/mV)
	show()

Equivalent in pure Python
.........................

The script above translated into pure Python (no Brian)::

	'''
	A pure Python version of the CUBA example, that reproduces basic Brian principles.
	'''
	from pylab import *
	from time import time
	from random import sample
	from scipy import random as scirandom
	
	"""
	Parameters
	"""
	N=10000        # number of neurons
	Ne=int(N*0.8) # excitatory neurons 
	Ni=N-Ne       # inhibitory neurons
	mV=ms=1e-3    # units
	dt=0.1*ms     # timestep
	taum=20*ms    # membrane time constant
	taue=5*ms
	taui=10*ms
	p=80.0/N # connection probability (80 synapses per neuron)
	Vt=-1*mV      # threshold = -50+49
	Vr=-11*mV     # reset = -60+49
	we=60*0.27/10 # excitatory weight
	wi=-20*4.5/10 # inhibitory weight
	duration=1000*ms
	
	"""
	Equations
	---------
	eqs='''
	dv/dt = (ge+gi-(v+49*mV))/(20*ms) : volt
	dge/dt = -ge/(5*ms) : volt
	dgi/dt = -gi/(10*ms) : volt
	'''
	
	This is a linear system, so each update corresponds to
	multiplying the state matrix by a (3,3) 'update matrix'
	"""
	
	# Update matrix
	A=array([[exp(-dt/taum),0,0],
	         [taue/(taum-taue)*(exp(-dt/taum)-exp(-dt/taue)),exp(-dt/taue),0],
	         [taui/(taum-taui)*(exp(-dt/taum)-exp(-dt/taui)),0,exp(-dt/taui)]]).T
	
	"""
	State variables
	---------------
	P=NeuronGroup(4000,model=eqs,
	              threshold=-50*mV,reset=-60*mV)
	"""
	S=zeros((3,N))
	
	"""
	Initialisation
	--------------
	P.v=-60*mV+10*mV*rand(len(P))
	"""
	S[0,:]=rand(N)*(Vt-Vr)+Vr # Potential: uniform between reset and threshold
	
	"""
	Connectivity matrices
	---------------------
	Pe=P.subgroup(3200) # excitatory group
	Pi=P.subgroup(800)  # inhibitory group
	Ce=Connection(Pe,P,'ge',weight=1.62*mV,sparseness=p)
	Ci=Connection(Pi,P,'gi',weight=-9*mV,sparseness=p)
	"""
	We_target=[]
	We_weight=[]
	for _ in range(Ne):
	    k=scirandom.binomial(N,p,1)[0]
	    target=sample(xrange(N),k)
	    target.sort()
	    We_target.append(target)
	    We_weight.append([1.62*mV]*k)
	Wi_target=[]
	Wi_weight=[]
	for _ in range(Ni):
	    k=scirandom.binomial(N,p,1)[0]
	    target=sample(xrange(N),k)
	    target.sort()
	    Wi_target.append(target)
	    Wi_weight.append([-9*mV]*k)
	
	"""
	Spike monitor
	-------------
	M=SpikeMonitor(P)
	
	will contain a list of (i,t), where neuron i spiked at time t.
	"""
	spike_monitor=[] # Empty list of spikes
	
	"""
	State monitor
	-------------
	trace=StateMonitor(P,'v',record=0) # record only neuron 0
	"""
	trace=[] # Will contain v(t) for each t (for neuron 0)
	
	"""
	Simulation
	----------
	run(duration)
	"""
	t1=time()
	t=0*ms
	while t<duration:
	    # STATE UPDATES
	    S[:]=dot(A,S)
	
	    # Threshold
	    all_spikes=(S[0,:]>Vt).nonzero()[0]     # List of neurons that meet threshold condition
	
	    # PROPAGATION OF SPIKES
	    # Excitatory neurons
	    spikes=(S[0,:Ne]>Vt).nonzero()[0]       # In Brian we actually use bisection to speed it up    
	    for i in spikes:
	        S[1,We_target[i]]+=We_weight[i]
	        
	    # Inhibitory neurons
	    spikes=(S[0,Ne:N]>Vt).nonzero()[0]
	    for i in spikes:
	        S[2,Wi_target[i]]+=Wi_weight[i]
	    
	    # Reset neurons after spiking
	    S[0,all_spikes]=Vr                       # Reset membrane potential
	      
	    # Spike monitor
	    spike_monitor+=[(i,t) for i in all_spikes]
	    
	    # State monitor
	    trace.append(S[0,0])
	
	    t+=dt
	
	t2=time()
	print "Simulated in",t2-t1,"s"
	print len(spike_monitor),"spikes"
	
	"""
	Plot
	----
	subplot(211)
	raster_plot(M)
	subplot(212)
	plot(trace.times/ms,trace[0]/mV)
	show()
	
	Here we cheat a little.
	"""
	from brian import raster_plot
	class M:
	    pass
	M.spikes=spike_monitor
	subplot(211)
	raster_plot(M)
	subplot(212)
	plot(arange(len(trace))*dt/ms,array(trace)/mV)
	show()