-
Notifications
You must be signed in to change notification settings - Fork 0
/
BalanceNetwork.py
439 lines (379 loc) · 9.82 KB
/
BalanceNetwork.py
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#/##################/#
# Parameters
#
#import
import numpy as np
#set
DefaultDict={
#Neurons prop
"UnitsInt":4000, #(N)
"InhRatioFloat":1./4., #(gamma)
"NeuronTypeStrsList":["Exc","Inh"], #(E,I)
"ConstantTimeFloat":20., #(tau_m, ms)
"RefractoryTimeFloat":2., #(tau_rp, ms)
"ThresholdFloat":-50., #(theta, mV)
"RestFloat":-70., #(Em, mV)
"ResetFloat":-60., #(vr, mV)
#Synapses prop
"SparsityFloat":0.1, #(epsilon)
"ExcWeightFloat":0.2, #(J,mV)
"InhScaleFloat":4., #(g)
"DelayTimeFloat":1., #(D,ms)
"LateralIsBool":True,
#Input
"RatioFrequencyFloat":1.,
#Run
"RunStepTimeFloat":0.02, #ms
"RunDurationTimeFloat":200., #ms
"PlotIpythonBool":False
}
#get
GlobalsDict=globals()
#put in globals
map(
lambda __ItemTuple:
GlobalsDict.__setitem__(*__ItemTuple)
if __ItemTuple[0] not in GlobalsDict
else None,
DefaultDict.items()
)
FrequencyThresholdFloat=(ThresholdFloat-RestFloat)/(UnitsInt*SparsityFloat*ExcWeightFloat*ConstantTimeFloat*0.001)
print('FrequencyThresholdFloat is '+str(FrequencyThresholdFloat)+' Hz')
PoissonFrequencyFloat=RatioFrequencyFloat*FrequencyThresholdFloat #(nu_ext,Hz)
#/##################/#
# Architecture
#
#NeuronGroups
NeuronGroupStrsList=map(
lambda __NeuronTypeStr:
__NeuronTypeStr+'NeuronGroup',
NeuronTypeStrsList
)
#Inputs
PoissonInputStrsList=map(
lambda __NeuronTypeStr:
__NeuronTypeStr+'PoissonInput',
NeuronTypeStrsList
)
#Synapses
import itertools
NeuronTypeStrsTuplesList=list(
itertools.product(
NeuronTypeStrsList,
NeuronTypeStrsList
)
)
SynapsesStrsList=map(
lambda __NeuronTypeStrsTuple:
__NeuronTypeStrsTuple[0]+'To'+__NeuronTypeStrsTuple[1]+'Synapses',
NeuronTypeStrsTuplesList
)
#StateMonitor
StateMonitorStrsList=map(
lambda __NeuronTypeStr:
__NeuronTypeStr+'StateMonitor',
NeuronTypeStrsList
)
#SpikeMonitor
SpikeMonitorStrsList=map(
lambda __NeuronTypeStr:
__NeuronTypeStr+'SpikeMonitor',
NeuronTypeStrsList
)
#PopulationRateMonitor
PopulationRateMonitorStrsList=map(
lambda __NeuronTypeStr:
__NeuronTypeStr+'PopulationRateMonitor',
NeuronTypeStrsList
)
#/##################/#
# Brian import
#
#import
import brian2
#derive
class SynapsesClass(brian2.Synapses):
def connect(self,*_LiargVariablesList,**_KwargVariablesDict):
brian2.Synapses.connect(self,*_LiargVariablesList,**_KwargVariablesDict)
return self
def setWeight(self,_WeigthFloatsArray):
self.J[:]=_WeigthFloatsArray.reshape(
self.source.N*self.target.N
)
return self
#/##################/#
# Reset Network and all brian objects
#
#build network
InstanceTrackerSet=brian2.Nameable.__instances__()
map(
lambda __BrianVariable:
InstanceTrackerSet.remove(__BrianVariable),
list(InstanceTrackerSet)
);
#build network
BalanceNetwork=brian2.Network()
#/##################/#
# build neuron groups
#
#map
map(
lambda __NeuronGroupStr:
BalanceNetwork.add(
brian2.NeuronGroup(
N=UnitsInt
if __NeuronGroupStr.startswith('Exc')
else InhRatioFloat*UnitsInt,
model='''
mu : volt
dv/dt= ( -(v-('''+str(RestFloat)+'''*mV)) + mu + xi*sqrt('''+str(ConstantTimeFloat
)+'''*ms)*0.*mV ) /('''+str(ConstantTimeFloat)+'''*ms) : volt (unless refractory)
''',
threshold="v>"+str(ThresholdFloat)+"*mV",
reset="v="+str(ResetFloat)+"*mV",
refractory=str(RefractoryTimeFloat)+"*ms",
name=__NeuronGroupStr
)
)
if __NeuronGroupStr not in BalanceNetwork
else None,
NeuronGroupStrsList
);
#set
BalanceNetwork['ExcNeuronGroup'].clock.dt=RunStepTimeFloat*brian2.ms
#build poisson inputs
map(
lambda __PoissonInputStr,__NeuronGroupStr:
BalanceNetwork.add(
brian2.PoissonInput(
BalanceNetwork[__NeuronGroupStr],
'v',
SparsityFloat*UnitsInt,
PoissonFrequencyFloat*brian2.Hz,
weight=ExcWeightFloat*brian2.mV
)
),
PoissonInputStrsList,
NeuronGroupStrsList
);
#/##################/#
# build synapses
#
#import
import scipy.stats
#Check
if LateralIsBool:
#map
map(
lambda __NeuronTypeStrsTuple,__SynapsesStr:
BalanceNetwork.add(
SynapsesClass(
BalanceNetwork[__NeuronTypeStrsTuple[0]+'NeuronGroup'],
BalanceNetwork[__NeuronTypeStrsTuple[1]+'NeuronGroup'],
model='''
J:1
''',
pre="v+=J*mV",
delay=DelayTimeFloat*brian2.ms,
name=__SynapsesStr
).connect(
LateralIsBool
).setWeight(
ExcWeightFloat*scipy.stats.bernoulli.rvs(
SparsityFloat,
size=(
BalanceNetwork[__NeuronTypeStrsTuple[0]+'NeuronGroup'].N,
BalanceNetwork[__NeuronTypeStrsTuple[1]+'NeuronGroup'].N
)
)
if __NeuronTypeStrsTuple[0]=='Exc'
else
-InhScaleFloat*ExcWeightFloat*scipy.stats.bernoulli.rvs(
SparsityFloat,
size=(
BalanceNetwork[__NeuronTypeStrsTuple[0]+'NeuronGroup'].N,
BalanceNetwork[__NeuronTypeStrsTuple[1]+'NeuronGroup'].N
)
)
)
)
if __SynapsesStr not in BalanceNetwork
else None,
NeuronTypeStrsTuplesList,
SynapsesStrsList
);
#/##################/#
# build Monitors
#
#build state monitor for v
map(
lambda __NeuronGroupStr,__StateMonitorStr:
BalanceNetwork.add(
brian2.StateMonitor(
BalanceNetwork[__NeuronGroupStr],
'v',
[0,1],
name=__StateMonitorStr,
)
)
if __StateMonitorStr not in BalanceNetwork
else None,
NeuronGroupStrsList,
StateMonitorStrsList
);
#build spike monitor
map(
lambda __NeuronGroupStr,__SpikeMonitorStr:
BalanceNetwork.add(
brian2.SpikeMonitor(
BalanceNetwork[__NeuronGroupStr],
name=__SpikeMonitorStr
)
)
if __SpikeMonitorStr not in BalanceNetwork
else None,
NeuronGroupStrsList,
SpikeMonitorStrsList
);
#build rate monitor
map(
lambda __NeuronGroupStr,__PopulationRateMonitorStr:
BalanceNetwork.add(
brian2.PopulationRateMonitor(
BalanceNetwork[__NeuronGroupStr],
name=__PopulationRateMonitorStr
)
)
if __PopulationRateMonitorStr not in BalanceNetwork
else None,
NeuronGroupStrsList,
PopulationRateMonitorStrsList
);
#/##################/#
# Random initial values
#
#map
map(
lambda __NeuronGroupStr:
setattr(
BalanceNetwork[__NeuronGroupStr],
'v',
(
RestFloat+scipy.stats.uniform.rvs(
size=BalanceNetwork[__NeuronGroupStr].N
)
)*brian2.mV
),
NeuronGroupStrsList
)
#/##################/#
# Run
#
#run
BalanceNetwork.run(RunDurationTimeFloat*brian2.ms);
#/##################/#
# Plot
#
#dict
ColorDict={
"Exc":"blue",
"Inh":"red"
}
#import
from matplotlib import pyplot
#init
RunFigure=pyplot.figure(
figsize=(
15,10
)
)
#/##################/#
# Voltage Traces
#
#init
StateAxes=RunFigure.add_subplot(311)
#set
XlimList=[
BalanceNetwork['ExcStateMonitor'].t[0],
BalanceNetwork['ExcStateMonitor'].t[-1]
]
#map
map(
lambda __NeuronTypeStr,__StateMonitorStr:
StateAxes.plot(
BalanceNetwork[__StateMonitorStr].t,
BalanceNetwork[__StateMonitorStr].v.T,
color=ColorDict[__NeuronTypeStr],
linewidth=3
),
NeuronTypeStrsList,
StateMonitorStrsList
)
StateAxes.plot(XlimList,[0.001*ThresholdFloat]*2,'--',color='black')
StateAxes.plot(XlimList,[0.001*RestFloat]*2,'--',color='black')
StateAxes.plot(XlimList,[0.001*ResetFloat]*2,'--',color='black')
#/##################/#
# Spike Traces
#
#init
SpikeAxes=RunFigure.add_subplot(312)
#map
map(
lambda __IndexInt,__NeuronTypeStr,__SpikeMonitorStr:
SpikeAxes.plot(
BalanceNetwork[__SpikeMonitorStr].t,
BalanceNetwork[__SpikeMonitorStr].i+(
BalanceNetwork[
NeuronTypeStrsList[
__IndexInt-1
]+'NeuronGroup'
].N
if __IndexInt>0 else 0.
),
linestyle='',
marker='o',
markersize=2,
color=ColorDict[__NeuronTypeStr],
label='$'+__NeuronTypeStr+'$'
),
xrange(len(NeuronTypeStrsList)),
NeuronTypeStrsList,
SpikeMonitorStrsList
)
SpikeAxes.set_xlim(XlimList)
SpikeAxes.set_ylim([0,UnitsInt*(1.+InhRatioFloat)])
SpikeAxes.legend()
#/##################/#
# Rate Traces
#
#init
RateAxes=RunFigure.add_subplot(313)
#set bins
WindowTimeFloat = 0.4
WindowLengthInt = int(WindowTimeFloat*brian2.ms/brian2.defaultclock.dt)
CumsumArraysList=map(
lambda __PopulationRateMonitorStr:
np.cumsum(np.insert(BalanceNetwork[__PopulationRateMonitorStr].rate,0,0)),
PopulationRateMonitorStrsList
)
BinRateArraysList=map(
lambda __CumsumArray:
(__CumsumArray[WindowLengthInt:] - __CumsumArray[:-WindowLengthInt]) / WindowLengthInt,
CumsumArraysList
)
#map
map(
lambda __NeuronTypeStr,__PopulationRateMonitorStr,__BinRateArray:
RateAxes.plot(
BalanceNetwork[__PopulationRateMonitorStr].t[WindowLengthInt-1:]/brian2.ms,
__BinRateArray,
color=ColorDict[__NeuronTypeStr],
linewidth=3
),
NeuronTypeStrsList,
PopulationRateMonitorStrsList,
BinRateArraysList
)
#Check
if PlotIpythonBool==False:
pyplot.show()