-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.py
536 lines (487 loc) · 17.2 KB
/
sim.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import sys
import numpy as np
import nest
from params import *
"""
select network type and stimuli onset
"""
network_mode = sys.argv[1] # input should be either "noise", "random" or "topo"
sim_time = float(eval(sys.argv[2])) # simulate time
if int(sim_time) % int(t_asterisk) != 0:
print(
"time of simulation should be integer multiples of each stimulus presentation time."
)
exit()
runindex = eval(sys.argv[3]) # n-th trial
print("network network_mode: ", sys.argv[1])
print("simulate for {} ms with {} ms extra onset time".format(sim_time, t_onset))
"""
set the network and simulation environment
"""
nest.SetKernelStatus(
params={
"total_num_virtual_procs": num_process,
"local_num_threads": num_threads,
"overwrite_files": True,
"print_time": timeprint,
}
)
nest.SetDefaults(
"iaf_cond_exp",
{
"E_L": E_L,
"C_m": C_m,
"t_ref": tau_ref,
"V_th": V_th,
"V_reset": V_reset,
"E_ex": V_revr_E,
"E_in": V_revr_I,
"g_L": g_L,
"tau_syn_ex": tau_E,
"tau_syn_in": tau_I,
},
)
nest.CopyModel("static_synapse", "syn_exci", params={"delay": d, "weight": gbar_E})
nest.CopyModel("static_synapse", "syn_inhi", params={"delay": d, "weight": gbar_I})
"""
setting delays using nest internal functions
"""
delay_mode = []
delay_param = []
delay_dict = []
profile_num = 2 # repeat for recurrent connections and feed-forward connections
delay_mode.append(sys.argv[4])
delay_mode.append(sys.argv[5])
delay_param.append(eval(sys.argv[6]))
delay_param.append(eval(sys.argv[7]))
# params for skipping connections
if len(sys.argv) > 8:
skip_double = bool(eval(sys.argv[8])) # True if double connection is activated
delay_mode.append(
sys.argv[5]
) # skipping connections follow the distribution of feed-forward connection
delay_param.append(eval(sys.argv[9])) # increasing delays
skip_weights = eval(
sys.argv[10]
) # might want to use decreasing weights, as a factor
profile_num = 3 # now also repeat for skipping connections
print(
"skipping connnection with {} double connections and weights={}".format(
skip_double, skip_weights
)
)
# recurrent connections, feed-forward connections and then skipping connections
for _ in range(profile_num):
print("delays:")
if delay_mode[_] == "null": # use a integer delay
if delay_param[_] == "null":
print("no specification")
delay_dict.append(d)
else:
d_prime = delay_param[_]
print("set delay to ", d_prime)
delay_dict.append(d_prime)
elif delay_mode[_] == "unimodal": # use a Gaussian dist. of delay
sigma = delay_param[_]
print("unimodal dist. with sigma={}".format(sigma))
delay_dict.append(
{"distribution": "normal_clipped", "low": 0.1, "mu": d, "sigma": sigma}
)
else:
print("fucked up")
exit()
"""
initialise neurons and recording devices and connect recording devices
"""
pop_exci = []
pop_inhi = []
pop = []
spike_device = []
for mod_i in range(module_depth): # iterate over different modules
current_mod_exci = nest.Create(
neuron_model, N_E
) # excitatory neurons for each module
pop_exci.append(current_mod_exci)
current_mod_inhi = nest.Create(neuron_model, N_I) # inhibitory
pop_inhi.append(current_mod_inhi)
current_mod_pop = current_mod_exci + current_mod_inhi
pop.append(current_mod_pop) # whole population for each module
nest.SetStatus(
current_mod_pop, "V_m", np.random.uniform(E_L, V_th, len(current_mod_pop))
) # random init. membrane potential
# I run short simulations for recording spikes and long simulations for recording voltages
if sim_time < short_threshold:
for mod_i in range(
module_depth
): # don't use the same for loops to avoid gid getting mixed
# spike detector for each module. Record only for 10 seconds
spike_device.append(
nest.Create(
"spike_detector",
params={
"withgid": True,
"withtime": True,
"start": t_onset,
"stop": endspiketime + t_onset,
"binary": True,
"to_memory": True,
"to_file": False,
},
)
)
nest.Connect(
pop[mod_i], spike_device[mod_i], conn_spec={"rule": "all_to_all"}
) # connect det. with neurons
"""
create background noise input Poisson generator and connect them to neurons
"""
poisson_gen = nest.Create(
"poisson_generator", params={"rate": v_x * K_x_0}
) # K_x different generator summed-up
nest.Connect(poisson_gen, pop[0], conn_spec={"rule": "all_to_all"})
poisson_gen_rest = nest.Create("poisson_generator", params={"rate": v_x * K_x_rest})
for mod_i in range(1, module_depth):
nest.Connect(poisson_gen_rest, pop[mod_i], conn_spec={"rule": "all_to_all"})
"""
intra-module recurrent connections. Every possible connection is connected with probability @epsilon(=0.1)
"""
for mod_i in range(module_depth):
nest.Connect(
pop_exci[mod_i],
pop[mod_i],
conn_spec={"rule": "pairwise_bernoulli", "p": epsilon},
syn_spec={"model": "syn_exci", "delay": delay_dict[0]},
)
nest.Connect(
pop_inhi[mod_i],
pop[mod_i],
conn_spec={"rule": "pairwise_bernoulli", "p": epsilon},
syn_spec={"model": "syn_inhi", "delay": delay_dict[0]},
)
"""
if stimuli is on, create input stimuli and corresponding poisson gen. and connect them to the input module
"""
if (network_mode == "random") or (network_mode == "topo"):
# record membrane potential for long simulations
if sim_time > short_threshold:
# voltmeter for the whole network, record when the stimuli changes
voltage_device = nest.Create(
"voltmeter",
params={
"withgid": True,
"interval": t_asterisk,
"start": t_onset,
"to_file": False,
"to_memory": True,
},
)
# connect voltmeter to exci. neurons
[
nest.Connect(voltage_device, epop_module, conn_spec={"rule": "all_to_all"})
for epop_module in pop_exci
]
# init. stimuli pattern
sym_seq_len = int(
sim_time // t_asterisk
) # how many presentation during the simulation occurs
sym_seq = np.zeros((num_stimulus, sym_seq_len), dtype=bool)
# for each presentation time select just one stimulus to be activated
for time_index in range(sym_seq_len):
sym_seq[:, time_index][np.random.randint(low=0, high=num_stimulus)] = True
gen_stim = [] # list of poisson generators functioning as stimuli
for stim_sym in sym_seq: # for each symbolic repr. of stimuli
stim_time = np.arange(
t_onset, t_onset + t_asterisk * sym_seq_len, t_asterisk
) # time points to turn on/off
stim_rate = np.zeros(stim_time.shape[0])
stim_rate[stim_sym] = v_stim * input_spike_len # TODO: v_stim or delta
# #delta * v_x * input_spike_len # assign right rate if it's on, 3*5*800 (linear sum-up)
# create generator for each stimulus
inho_gen = nest.Create("inhomogeneous_poisson_generator")
nest.SetStatus(
inho_gen, {"rate_times": list(stim_time), "rate_values": list(stim_rate)}
)
gen_stim.append(inho_gen)
"""
switch for different network configurations
"""
####
if (network_mode == "noise") or (network_mode == "random"):
####
# inter-module feed-forward connections. Every exci. neuron projects to the next module with prob. @p_ff(=0.075)
for mod_i in range(module_depth - 1):
nest.Connect(
pop_exci[mod_i],
pop[mod_i + 1],
conn_spec={"rule": "pairwise_bernoulli", "p": p_ff},
syn_spec={"model": "syn_exci", "delay": delay_dict[1]},
)
# connect stimuli generators to random sub-populations
if network_mode == "random":
for stim_i in range(num_stimulus):
nest.Connect(
gen_stim[stim_i],
tuple(np.random.choice(pop_exci[0], N_E_speci, replace=False)),
conn_spec={"rule": "all_to_all"},
)
nest.Connect(
gen_stim[stim_i],
tuple(np.random.choice(pop_inhi[0], N_I_speci, replace=False)),
conn_spec={"rule": "all_to_all"},
)
# TODO: skipping connections
if len(sys.argv) > 8:
nest.Connect(
pop_exci[0],
pop[2],
conn_spec={"rule": "pairwise_bernoulli", "p": p_ff},
syn_spec={
"model": "syn_exci",
"delay": delay_dict[2],
"weight": gbar_E * skip_weights,
},
)
if skip_double:
nest.Connect(
pop_exci[1],
pop[3],
conn_spec={"rule": "pairwise_bernoulli", "p": p_ff},
syn_spec={
"model": "syn_exci",
"delay": delay_dict[2],
"weight": gbar_E * skip_weights,
},
)
####
elif network_mode == "topo":
####
# printconnec = lambda source, target: print(len(nest.GetStatus(
# nest.GetConnections(source=source, target=target), "target")))
# select stimuli specific neurons and save them in lists of lists. specific_exci[module][stimuli]
specific_exci = [
[
tuple(np.random.choice(pop_exci[mod_i], N_E_speci, replace=False))
for stim_i in range(num_stimulus)
]
for mod_i in range(module_depth)
]
specific_inhi = [
[
tuple(np.random.choice(pop_inhi[mod_i], N_I_speci, replace=False))
for stim_i in range(num_stimulus)
]
for mod_i in range(module_depth)
]
# stimuli specific feed-forward inter-module connection. Stimulus specific neurons connect to the next module
# specific neurons with probability @p_ff(=0.075)
[
[
nest.Connect(
specific_exci[mod_i][stim_i],
specific_exci[mod_i + 1][stim_i] + specific_inhi[mod_i + 1][stim_i],
conn_spec={"rule": "pairwise_bernoulli", "p": p_ff},
syn_spec={"model": "syn_exci", "delay": delay_dict[1]},
)
for stim_i in range(num_stimulus)
]
for mod_i in range(module_depth - 1)
]
# random feed-forward inter-module connection. Only stimuli non-specific exci. neurons projects randomly.
for mod_i in range(module_depth - 1):
all_specific_exci = []
for subpop in specific_exci[mod_i]:
all_specific_exci.extend(
list(subpop)
) # now a list with gids of all neurons that are stimuli-specific
nonspeci_exci = tuple(
set(pop_exci[mod_i]).difference(set(all_specific_exci))
) # all neurons that aren't speci.
nest.Connect(
nonspeci_exci,
pop[mod_i + 1], # only non-specific ones project randomly to the next layer
conn_spec={"rule": "pairwise_bernoulli", "p": p_ff},
syn_spec={"model": "syn_exci", "delay": delay_dict[1]},
)
# connect gen. to the input module
for stim_i in range(num_stimulus):
nest.Connect(
gen_stim[stim_i], specific_exci[0][stim_i], conn_spec={"rule": "all_to_all"}
)
nest.Connect(
gen_stim[stim_i], specific_inhi[0][stim_i], conn_spec={"rule": "all_to_all"}
)
# TODO: skipping connections
if len(sys.argv) > 8:
[
nest.Connect(
specific_exci[0][stim_i],
specific_exci[2][stim_i] + specific_inhi[2][stim_i],
conn_spec={"rule": "pairwise_bernoulli", "p": p_ff},
syn_spec={
"model": "syn_exci",
"delay": delay_dict[2],
"weight": gbar_E * skip_weights,
},
)
for stim_i in range(num_stimulus)
]
if skip_double:
[
nest.Connect(
specific_exci[1][stim_i],
specific_exci[3][stim_i] + specific_inhi[3][stim_i],
conn_spec={"rule": "pairwise_bernoulli", "p": p_ff},
syn_spec={
"model": "syn_exci",
"delay": delay_dict[2],
"weight": gbar_E * skip_weights,
},
)
for stim_i in range(num_stimulus)
]
####
else:
####
print("you typed in the wrong network name")
exit()
"""
simulate with extra time for stimuli to be set
"""
nest.Simulate(sim_time + t_onset)
"""
save the data
"""
# only when the simulation time is short
if sim_time < short_threshold:
spike_times = []
spike_senders = []
for mod_i in range(module_depth):
data_spike = nest.GetStatus(spike_device[mod_i], "events")[0]
spike_times.append(
data_spike["times"]
) # list of spike times with each component repr. layer
spike_senders.append(data_spike["senders"])
if len(sys.argv) > 8:
np.save(
PATH
+ "spiketimes_run={}_{}_intra={}{}_inter={}{}_skip_double={}_d={}_w={}.npy".format(
runindex,
network_mode,
delay_mode[0],
delay_param[0],
delay_mode[1],
delay_param[1],
skip_double,
delay_param[2],
skip_weights,
),
np.array(spike_times),
)
np.save(
PATH
+ "spikesenders_run={}_{}_intra={}{}_inter={}{}_skip_double={}_d={}_w={}.npy".format(
runindex,
network_mode,
delay_mode[0],
delay_param[0],
delay_mode[1],
delay_param[1],
skip_double,
delay_param[2],
skip_weights,
),
np.array(spike_senders),
)
else:
np.save(
PATH
+ "spiketimes_run={}_{}_intra={}{}_inter={}{}.npy".format(
runindex,
network_mode,
delay_mode[0],
delay_param[0],
delay_mode[1],
delay_param[1],
),
np.array(spike_times),
)
np.save(
PATH
+ "spikesenders_run={}_{}_intra={}{}_inter={}{}.npy".format(
runindex,
network_mode,
delay_mode[0],
delay_param[0],
delay_mode[1],
delay_param[1],
),
np.array(spike_senders),
)
# only if the network has input and the simulation was long enough
if ((network_mode == "random") or (network_mode == "topo")) and (
sim_time > short_threshold
):
data_volt = nest.GetStatus(voltage_device, "events")[0]
gids = data_volt["senders"]
vms = data_volt["V_m"]
# reshape the voltage values
volt_values = np.zeros((module_depth, N_E, int(sim_time / t_asterisk)))
for module_i in range(4):
for nindex, nid in enumerate(range(1 + N * module_i, 1 + N * module_i + N_E)):
volt_values[module_i, nindex] = vms[np.where(gids == nid)[0]]
if len(sys.argv) > 8:
np.save(
PATH
+ "stimuli_run={}_{}_intra={}{}_inter={}{}_skip_double={}_d={}_w={}.npy".format(
runindex,
network_mode,
delay_mode[0],
delay_param[0],
delay_mode[1],
delay_param[1],
skip_double,
delay_param[2],
skip_weights,
),
sym_seq,
)
np.save(
PATH
+ "voltvalues_run={}_{}_intra={}{}_inter={}{}_skip_double={}_d={}_w={}.npy".format(
runindex,
network_mode,
delay_mode[0],
delay_param[0],
delay_mode[1],
delay_param[1],
skip_double,
delay_param[2],
skip_weights,
),
volt_values,
)
else:
np.save(
PATH
+ "stimuli_run={}_{}_intra={}{}_inter={}{}.npy".format(
runindex,
network_mode,
delay_mode[0],
delay_param[0],
delay_mode[1],
delay_param[1],
),
sym_seq,
)
np.save(
PATH
+ "voltvalues_run={}_{}_intra={}{}_inter={}{}.npy".format(
runindex,
network_mode,
delay_mode[0],
delay_param[0],
delay_mode[1],
delay_param[1],
),
volt_values,
)