-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateNeuroMLlite.py
1138 lines (935 loc) · 48.8 KB
/
GenerateNeuroMLlite.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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from neuromllite import Network, Cell, InputSource, Population, Synapse, RectangularRegion, RelativeLayout
from neuromllite import Projection, RandomConnectivity, Input, Simulation
import numpy as np
import pickle
# Add the Python folder to the Python path
import sys
sys.path.append("../Python")
'''
Get a color for the area/region, possibly depending on level in functional hierarchy
'''
def get_scaled_color(area):
ranking = {}
for l in open('../Python/interareal/areas_ranking.txt'):
if not 'region' in l:
ws = [w.strip() for w in l.split(',')]
ranking[ws[0]] = int(ws[1])
r = ranking[area]
scale = r/30.
c = '%s %s %s'%(0.95-0.5*(scale),1-scale,0.4*(scale))
print('Color for area %s: %s'%(area,c))
return c
def get_connectivity(conn, areas, ranking, conn_bin):
'''
Generate connectivity matrix
:param conn: Dictionary with the fln and sln information
:param conn_bin: binary matrix describing which region is connected to which. This is only necessary
if the fln and sln are not taken into account.
:param ranking: List of all areas with their rankings
:param areas: List of Areas under analysis (assumes that all areas have been sorted by their ranking)
:return:
conn: The connectivity matrix scalled by fln and sln
'''
# Check if the selected regions are present in the file that describe the ranking
assert(len(areas) == sum(ranking['region'].isin(areas)))
# sort the regions by the areas in the ranking
ranking = ranking[ranking['region'].isin(areas)]
if conn_bin == None:
# find fln and sln information for the 4 regions of interest
region_indexes = ranking.index.tolist()
# Get the tuple of the combination of connectivity and obtain the correspoding fln and sln values
indexes = [i for i in product(region_indexes, repeat=2)]
fln = conn['fln'][tuple(np.array(indexes).T)].reshape(len(areas), len(areas))
# range compression for the connection weights
fln = 1.2 * np.power(fln, .30)
sln = conn['sln'][tuple(np.array(indexes).T)].reshape(len(areas), len(areas))
conn = np.multiply(fln, sln)
else:
conn = conn_bin
return conn
'''
Get name without / and not starting with digit
'''
def get_safe_area_name(area):
return 'a%s'%area.replace('/','_') if area[0].isdigit() else area.replace('/','_')
'''
Main generate method
'''
def generate(wee = 1.5, wei = -3.25, wie = 3.5, wii = -2.5,
i_l5e_l2i=0., i_l2e_l5e=0.,
areas=['V1'],
sigma23=.3, sigma56=.45, noise=True, duration=1000, dt=0.2, Iext=[[0, 0]], count=0,
net_id='MejiasFig2', conn=None):
scale = 1
centres = {}
# From https://scalablebrainatlas.incf.org/macaque/MERetal14
f = open('MERetal14_on_F99.tsv')
for l in f:
w = l.split()
id = w[0].replace('-','_').lower()
centres[id] = (float(w[2])*scale,float(w[3])*scale,float(w[4])*scale)
################################################################################
### Build new network
net = Network(id=net_id)
net.notes = 'Testing...'
# specify the interareal FF and FB connections depending on the number of areas under analysis
if len(areas) == 1:
FF_l2e_l2e = 0.
FB_l5e_l2i = 0.
FB_l5e_l5e = 0.
FB_l5e_l5i = 0.
FB_l5e_l2e = 0.
net.parameters = { 'wee': wee,
'wei': wei,
'wie': wie,
'wii': wii,
'l5e_l2i': i_l5e_l2i,
'l2e_l5e': i_l2e_l5e,
'sigma23': sigma23,
'sigma56': sigma56 }
elif len(areas) == 2:
FF_l2e_l2e = 1.
FB_l5e_l2i = .5
FB_l5e_l5e = .9
FB_l5e_l5i = .5
FB_l5e_l2e = .1
net.parameters = { 'wee': wee,
'wei': wei,
'wie': wie,
'wii': wii,
'l5e_l2i': i_l5e_l2i,
'l2e_l5e': i_l2e_l5e,
'FF_l2e_l2e': FF_l2e_l2e,
'FB_l5e_l2i': FB_l5e_l2i,
'FB_l5e_l5e': FB_l5e_l5e,
'FB_l5e_l5i': FB_l5e_l5i,
'FB_l5e_l2e': FB_l5e_l2e,
'sigma23': sigma23,
'sigma56': sigma56 }
elif len(areas) > 2:
FF_l2e_l2e = 1.0
FB_l5e_l2i = .5
FB_l5e_l5e = .9
FB_l5e_l5i = .5
FB_l5e_l2e = .1
net.parameters = { 'wee': wee,
'wei': wei,
'wie': wie,
'wii': wii,
'l5e_l2i': i_l5e_l2i,
'l2e_l5e': i_l2e_l5e,
'FF_l2e_l2e': FF_l2e_l2e,
'FB_l5e_l2i': FB_l5e_l2i,
'FB_l5e_l5e': FB_l5e_l5e,
'FB_l5e_l5i': FB_l5e_l5i,
'FB_l5e_l2e': FB_l5e_l2e,
'sigma23': sigma23,
'sigma56': sigma56 }
delay_stim = '0ms'
duration_stim = '1e9ms'
net.parameters['delay_stim'] = delay_stim
net.parameters['duration_stim'] = duration_stim
suffix = '' if noise else '_flat'
if dt==0.2:
suffix2 = ''
elif dt==0.02:
suffix2 = '_smalldt'
else:
print('Using a value for dt which is not supported!!')
quit()
l23ecell = Cell(id='L23_E_comp'+suffix+suffix2, lems_source_file='Prototypes.xml')
l23icell = Cell(id='L23_I_comp'+suffix+suffix2, lems_source_file='Prototypes.xml') # hack to include this file too.
l56ecell = Cell(id='L56_E_comp'+suffix+suffix2, lems_source_file='Prototypes.xml') # hack to include this file too.
l56icell = Cell(id='L56_I_comp'+suffix+suffix2, lems_source_file='Prototypes.xml')
net.cells.append(l23ecell)
net.cells.append(l23icell)
net.cells.append(l56ecell)
net.cells.append(l56icell)
color_str = {'l23e':'.8 0 0','l23i':'0 0 .8',
'l56e':'1 .2 0','l56i':'0 .2 1'}
def internal_connections(pops, W, pre_pop, post_pop):
print('Connection %s -> %s:' %(pre_pop.id[:2], post_pop.id[:2]))
weight = str(W[pops.index(pre_pop)][pops.index(post_pop)])
print(' Connection %s -> %s weight %s'%(pre_pop.id, post_pop.id, weight))
zero_weight = False
try:
zero_weight = len(weight)==0 or float(weight)==0
except:
pass
if not zero_weight:
net.projections.append(Projection(id='proj_%s_%s'%(pre_pop.id, post_pop.id),
presynaptic=pre_pop.id,
postsynaptic=post_pop.id,
synapse='rs',
type='continuousProjection',
delay=0,
weight=weight,
random_connectivity=RandomConnectivity(probability=1)))
else:
print(' Ignoring, as weight=0...')
n_areas = len(areas)
if n_areas == 1:
l2e_l2e = 'wee'; l2e_l2i = 'wei'; l2i_l2e = 'wie'; l2i_l2i = 'wii';
l5e_l5e = 'wee'; l5e_l5i = 'wei'; l5i_l5e = 'wie'; l5i_l5i = 'wii';
l2e_l5i = 0; l2e_l5e = 'l2e_l5e'; l2i_l5e = 0; l2i_l5i = 0;
l5e_l2e = 0; l5e_l2i= 'l5e_l2i'; l5i_l2e = 0; l5i_l2i = 0;
W = np.array([[l2e_l2e, l2e_l2i, l2e_l5e, l2e_l5i],
[l2i_l2e, l2i_l2i, l2i_l5e, l2i_l5i],
[l5e_l2e, l5e_l2i, l5e_l5e, l5e_l5i],
[l5i_l2e, l5i_l2i, l5i_l5e, l5i_l5i]], dtype='U14')
elif n_areas == 2:
v1_v1_l2e_l2e = v4_v4_l2e_l2e = 'wee'; v1_v1_l5e_l5e = v4_v4_l5e_l5e = 'wee'
v1_v1_l2e_l2i = v4_v4_l2e_l2i = 'wei'; v1_v1_l5e_l5i = v4_v4_l5e_l5i = 'wei'
v1_v1_l2i_l2e = v4_v4_l2i_l2e = 'wie'; v1_v1_l5i_l5e = v4_v4_l5i_l5e = 'wie'
v1_v1_l2i_l2i = v4_v4_l2i_l2i = 'wii'; v1_v1_l5i_l5i = v4_v4_l5i_l5i = 'wii'
v1_v1_l2e_l5e = v4_v4_l2e_l5e = 'l2e_l5e'; v1_v1_l2e_l5i = v4_v4_l2e_l5i = 0;
v1_v1_l5e_l2i = v4_v4_l5e_l2i = 'l5e_l2i'; v1_v1_l5e_l2e = v4_v4_l5e_l2e = 0;
v1_v1_l2i_l5e = v4_v4_l2i_l5e = 0; v1_v1_l5i_l2i = v4_v4_l5i_l2i = 0;
v1_v1_l5i_l2e = v4_v4_l5i_l2e = 0; v1_v1_l2i_l5i = v4_v4_l2i_l5i = 0;
# interareal
v1_v4_l2e_l2e = 'FF_l2e_l2e'; v4_v1_l2e_l2e = 0; v1_v4_l2e_l2i = v4_v1_l2e_l2i = 0; v1_v4_l2e_l5e = v4_v1_l2e_l5e = 0; v1_v4_l2e_l5i = v4_v1_l2e_l5i= 0;
v1_v4_l2i_l2e = v4_v1_l2i_l2e = 0; v1_v4_l2i_l2i = v4_v1_l2i_l2i = 0; v1_v4_l2i_l5e = v4_v1_l2i_l5e = 0; v1_v4_l2i_l5i = v4_v1_l2i_l5i= 0;
v1_v4_l5e_l2e = 0; v4_v1_l5e_l2e = 'FB_l5e_l2e'; v1_v4_l5e_l2i = 0; v4_v1_l5e_l2i = 'FB_l5e_l2i'; v1_v4_l5e_l5e = 0; v4_v1_l5e_l5e = 'FB_l5e_l5e'; v1_v4_l5e_l5i = 0; v4_v1_l5e_l5i= 'FB_l5e_l2i';
v1_v4_l5i_l2e = v4_v1_l5i_l2e = 0; v1_v4_l5i_l2i = v4_v1_l5i_l2i = 0; v1_v4_l5i_l5e = v4_v1_l5i_l5e = 0; v1_v4_l5i_l5i = v4_v1_l5i_l5i= 0;
W = np.array([ [v1_v1_l2e_l2e, v1_v1_l2e_l2i, v1_v1_l2e_l5e, v1_v1_l2e_l5i, v1_v4_l2e_l2e, v1_v4_l2e_l2i, v1_v4_l2e_l5e, v1_v4_l2e_l5i],
[v1_v1_l2i_l2e, v1_v1_l2i_l2i, v1_v1_l2i_l5e, v1_v1_l2i_l5i, v1_v4_l2i_l2e, v1_v4_l2i_l2i, v1_v4_l2i_l5e, v1_v4_l2i_l5i],
[v1_v1_l5e_l2e, v1_v1_l5e_l2i, v1_v1_l5e_l5e, v1_v1_l5e_l5i, v1_v4_l5e_l2e, v1_v4_l5e_l2i, v1_v4_l5e_l5e, v1_v4_l5e_l5i],
[v1_v1_l5i_l2e, v1_v1_l5i_l2i, v1_v1_l5i_l5e, v1_v1_l5i_l5i, v1_v4_l5i_l2e, v1_v4_l5i_l2i, v1_v4_l5i_l5e, v1_v4_l5i_l5i],
[v4_v1_l2e_l2e, v4_v1_l2e_l2i, v4_v1_l2e_l5e, v4_v1_l2e_l5i, v4_v4_l2e_l2e, v4_v4_l2e_l2i, v4_v4_l2e_l5e, v4_v4_l2e_l5i],
[v4_v1_l2i_l2e, v4_v1_l2i_l2i, v4_v1_l2i_l5e, v4_v1_l2i_l5i, v4_v4_l2i_l2e, v4_v4_l2i_l2i, v4_v4_l2i_l5e, v4_v4_l2i_l5i],
[v4_v1_l5e_l2e, v4_v1_l5e_l2i, v4_v1_l5e_l5e, v4_v1_l5e_l5i, v4_v4_l5e_l2e, v4_v4_l5e_l2i, v4_v4_l5e_l5e, v4_v4_l5e_l5i],
[v4_v1_l5i_l2e, v4_v1_l5i_l2i, v4_v1_l5i_l5e, v4_v1_l5i_l5i, v4_v4_l5i_l2e, v4_v4_l5i_l2i, v4_v4_l5i_l5e, v4_v4_l5i_l5i]],
dtype='U14')
elif n_areas > 2:
nlayers = 4 #(L2/3E, L5/6, L2/3I, L5/6I)
# Define connections to self areas (diagonal entries)
interlayer_conn_diag = np.array([[wee, wei, i_l2e_l5e, 0],
[wie, wii, 0, 0],
[0, i_l5e_l2i, wee, wei],
[0, 0, wie, wii]
])
# Define the main connectivity matrix
W = np.empty((n_areas * nlayers, n_areas * nlayers),dtype='U34')
# Get the upper or the lower diagonal. This information is useful to now if the connection is FF(uper triangular matrix)
# or FB (lower triangular matrix)
up_tri = zip(np.triu_indices(n_areas,1)[0], np.triu_indices(n_areas,1)[1])
lw_tri = zip(np.tril_indices(n_areas,-1)[0], np.tril_indices(n_areas,-1)[1])
for row in range(conn.shape[0]):
for col in range(conn.shape[1]):
# What to do in the self-connection case
if row == col:
W[row * 4:row * 4 + 4, col * 4:col * 4 + 4] = interlayer_conn_diag
continue
# Define connection between different areas
if conn[row, col] != 0:
# Add FF connection
if (row, col) in up_tri:
W[row * 4 + 0, col * 4 + 0] = 'FF_l2e_l2e * %s' % conn[row, col] # FF_L2e_l2e
# Add FB connection
if (row, col) in lw_tri:
W[row *4 + 2, col * 4 + 0] = 'FB_l5e_l2e * %s' % conn[row, col] # FB l5e_l2e
W[row *4 + 2, col * 4 + 3] = 'FB_l5e_l5i * %s' % conn[row, col] # FB l5e_l5i
W[row *4 + 2, col * 4 + 2] = 'FB_l5e_l5e * %s' % conn[row, col] # FB l5e_l5e
W[row *4 + 2, col * 4 + 1] = 'FB_l5e_l2i * %s' % conn[row, col] # FB l5e_l2i
else:
raise ValueError('Connectivity matrix not defined for more than 3 regions')
net.synapses.append(Synapse(id='rs',
lems_source_file='Prototypes.xml'))
net.synapses.append(Synapse(id='silent1',
lems_source_file='RateBased.xml')) # silent synapse not used, just using this to include RateBased.xml...
# Background input
# Iterate over the different possible areas
pops = []
area_edge = 5
area_spacing = 10
layer_thickness = 5
l23e_radius = .5
l23i_radius = .35
l56e_radius = .6
l56i_radius = .35
for area_idx, area in enumerate(areas):
if len(areas)>4:
region_color = get_scaled_color(area)
p = centres[area.replace('/','_').lower()]
x_offset = area_idx*(area_edge + area_spacing)
region = RectangularRegion(id='%s' %(area), x=p[0], y=p[1], z=p[2], width=area_edge, height=30, depth=area_edge)
net.regions.append(region)
l23_region = region
l56_region = region
l23e_color = region_color
l23i_color = region_color
l56e_color = region_color
l56i_color = region_color
separation = 1.5
l23e_offset_x = 0
l23i_offset_x = 0
l56e_offset_x = 0
l56i_offset_x = 0
l23e_offset_y = 0
l23i_offset_y = separation
l56e_offset_y = 0
l56i_offset_y = separation
l23e_offset_z = separation
l23i_offset_z = separation
l56e_offset_z = 0
l56i_offset_z = 0
else:
# Add populations
x_offset = area_idx*(area_edge + area_spacing)
l23_region = RectangularRegion(id='%s_L23' %(area), x=x_offset, y=layer_thickness, z=0, width=area_edge, height=30, depth=area_edge)
net.regions.append(l23_region)
l56_region = RectangularRegion(id='%s_L56' %(area), x=x_offset, y=0, z=0, width=area_edge, height=layer_thickness, depth=area_edge)
net.regions.append(l56_region)
l23e_color = color_str['l23e']
l23i_color = color_str['l23i']
l56e_color = color_str['l56e']
l56i_color = color_str['l56i']
l23e_offset_x = 0
l23i_offset_x = area_edge
l56e_offset_x = 0
l56i_offset_x = area_edge
l23e_offset_y = layer_thickness*1.4/3.
l23i_offset_y = layer_thickness*1/3.
l56e_offset_y = layer_thickness*1.4/3.
l56i_offset_y = layer_thickness*1/3.
l23e_offset_z = 0
l23i_offset_z = 0
l56e_offset_z = 0
l56i_offset_z = 0
safe_area = get_safe_area_name(area)
pl23e = Population(id='%s_L23_E' %(safe_area),
size=1,
component=l23ecell.id,
properties={'color':l23e_color,'radius':l23e_radius},
relative_layout = RelativeLayout(region=l23_region.id,x=l23e_offset_x,y=l23e_offset_y,z=l23e_offset_z))
pops.append(pl23e)
pl23i = Population(id='%s_L23_I' %(safe_area),
size=1,
component=l23icell.id,
properties={'color':l23i_color,'radius':l23i_radius},
relative_layout = RelativeLayout(region=l23_region.id,x=l23i_offset_x,y=l23i_offset_y,z=l23i_offset_z))
pops.append(pl23i)
pl56e = Population(id='%s_L56_E' %(safe_area),
size=1,
component=l56ecell.id,
properties={'color':l56e_color,'radius':l56e_radius},
relative_layout = RelativeLayout(region=l56_region.id,x=l56e_offset_x,y=l56e_offset_y,z=l56e_offset_z))
pops.append(pl56e)
pl56i = Population(id='%s_L56_I' %(safe_area),
size=1,
component=l56icell.id,
properties={'color':l56i_color,'radius':l56i_radius},
relative_layout = RelativeLayout(region=l56_region.id,x=l56i_offset_x,y=l56i_offset_y,z=l56i_offset_z))
pops.append(pl56i)
net.populations.append(pl23e)
net.populations.append(pl23i)
net.populations.append(pl56e)
net.populations.append(pl56i)
# Force input to be only on V1.
if area == 'V1':
safe_area = get_safe_area_name('V1')
# Add inputs
# find index for V1
area_idx = areas.index('V1')
insource_id = 'iclamp_%s_L23'%safe_area
insource_param = '%s_amp'%(insource_id)
net.parameters[insource_param] = '%snA'%Iext[area_idx][0]
input_source_l23 = InputSource(id= insource_id,
neuroml2_input='PulseGenerator',
parameters={'amplitude':insource_param, 'delay':'delay_stim', 'duration':'duration_stim'})
net.input_sources.append(input_source_l23)
# Add modulation
net.inputs.append(Input(id='modulation_%s_L23_E'%safe_area,
input_source=input_source_l23.id,
population=pl23e.id,
percentage=100))
insource_id = 'iclamp_%s_L56'%safe_area
insource_param = '%s_amp'%(insource_id)
net.parameters[insource_param] = '%snA'%Iext[area_idx][1]
input_source_l56 = InputSource(id=insource_id,
neuroml2_input='PulseGenerator',
parameters={'amplitude':insource_param, 'delay':'delay_stim', 'duration':'duration_stim'})
net.input_sources.append(input_source_l56)
# Add modulation
net.inputs.append(Input(id='modulation_%s_L56_E'%safe_area,
input_source=input_source_l56.id,
population=pl56e.id,
percentage=100))
for pre_pop in pops:
for post_pop in pops:
internal_connections(pops, W, pre_pop, post_pop)
print(net)
#print(net.to_json())
new_file = net.to_json_file('%s.json'%net.id)
################################################################################
### Build Simulation object & save as JSON
sim = Simulation(id='Sim%s_%d'%(net.id,count),
network=new_file,
duration=duration,
dt=dt,
seed=count,
record_rates={'all':'*'})
sim.to_json_file()
return sim, net
if __name__ == "__main__":
from neuromllite.NetworkGenerator import check_to_generate_or_run
from pyneuroml import pynml
JEE = 1.5
JIE = 3.5
JEI = -3.25
JII = -2.5
pop_colors = {'L23_E':'#dd7777','L23_I':'#7777dd','L23_E Py':'#990000','L23_I Py':'#000099',
'L56_E':'#77dd77','L56_I':'#dd77dd','L56_E Py':'#009900','L56_I Py':'#990099'}
if '-sweep' in sys.argv:
# To do...
pass
if '-test' in sys.argv or '-dt' in sys.argv:
from neuromllite.sweep.ParameterSweep import *
if '-test' in sys.argv:
arg_options = {'No connections; no noise':[{'wee':0, 'wei':0, 'wie':0, 'wii':0,
'duration':1000, 'dt':0.2, 'noise':False},
'simulation_Iext0_nrun0_noise0.0_dur1.0_noconns_dt0.0002.txt'],
'With connections; no noise':[{'wee':JEE, 'wei':JIE, 'wie':JEI, 'wii':JII,
'duration':1000, 'dt':0.2, 'noise':False},
'simulation_Iext0_nrun0_noise0.0_dur1.0_dt0.0002.txt'],
'No connections; with noise':[{'wee':0, 'wei':0, 'wie':0, 'wii':0,
'duration':50000, 'dt':0.2, 'noise':True},
'simulation_Iext0_nrun0_noiseNone_dur50.0_noconns_dt0.0002.txt'],
'With connections; with noise':[{'wee':JEE, 'wei':JIE, 'wie':JEI, 'wii':JII,
'duration':50000, 'dt':0.2, 'noise':True},
'simulation_Iext0_nrun0_noiseNone_dur50.0_dt0.0002.txt']}
hist_bins = 50
elif '-dt' in sys.argv:
print('Running dt tests...')
arg_options = {'dt normal':[{'wee':0, 'wei':0, 'wie':0, 'wii':0,
'duration':50000, 'dt':0.2, 'noise':True},
'simulation_Iext0_nrun0_noiseNone_dur50.0_noconns_dt0.0002.txt'],
'dt small':[{'wee':0, 'wei':0, 'wie':0, 'wii':0,
'duration':50000, 'dt':0.02, 'noise':True},
'simulation_Iext0_nrun0_noiseNone_dur50.0_noconns_dt2e-05.txt']}
hist_bins = 50
#sim, net = generate(wee = 0, wei = 0, wie = 0, wii = 0, duration=50000, dt=0.2)
#sim, net = generate(duration=50000, dt=0.2)
for a in arg_options:
print("Running sim: %s"%arg_options[a])
sim, net = generate(**arg_options[a][0])
simulator = 'jNeuroML'
nmllr = NeuroMLliteRunner('%s.json'%sim.id,
simulator=simulator)
incl_23 = False
incl_23 = True
incl_56 = False
incl_56 = True
traces, events = nmllr.run_once('/tmp')
xs = []
ys = []
labels = []
colors = []
histxs = []
histys = []
histlabels = []
histcolors = []
for tr in traces:
if tr!='t':
if ('23' in tr and incl_23) or ('56' in tr and incl_56):
xs.append(traces['t'])
ys.append(traces[tr])
pop = tr.split('/')[0]
labels.append(pop)
pop_type = pop[pop.index('_')+1:]
pop_color = pop_colors[pop_type]
colors.append(pop_color)
hist1, edges1 = np.histogram(traces[tr], bins=hist_bins)
mid1 = [e +(edges1[1]-edges1[0])/2 for e in edges1[:-1]]
histxs.append(mid1)
histys.append(hist1)
histcolors.append(pop_color)
histlabels.append(pop)
debug_datafile = '../Python/debug/intralaminar/%s'%arg_options[a][1]
with open(debug_datafile) as f:
l23e = []; l23i = []; l56e = []; l56i = []; ts = []
t=0
dt = 0.0002
count = 0
for line in f:
w = line.split()
l23e.append(float(w[0]))
l23i.append(float(w[1]))
l56e.append(float(w[2]))
l56i.append(float(w[3]))
ts.append(t)
t+=dt
count+=1
print("Read in 4 x %i data points from %s"%(count, debug_datafile))
if incl_23:
xs.append(ts)
ys.append(l23e)
pop = 'L23_E Py'
labels.append(pop)
colors.append(pop_colors[pop])
hist1, edges1 = np.histogram(l23e, bins=hist_bins)
mid1 = [e +(edges1[1]-edges1[0])/2 for e in edges1[:-1]]
histxs.append(mid1)
histys.append(hist1)
histlabels.append(pop)
histcolors.append(pop_colors[pop])
xs.append(ts)
ys.append(l23i)
pop = 'L23_I Py'
labels.append(pop)
colors.append(pop_colors[pop])
hist1, edges1 = np.histogram(l23i, bins=hist_bins)
mid1 = [e +(edges1[1]-edges1[0])/2 for e in edges1[:-1]]
histxs.append(mid1)
histys.append(hist1)
histlabels.append(pop)
histcolors.append(pop_colors[pop])
if incl_56:
xs.append(ts)
ys.append(l56e)
pop = 'L56_E Py'
labels.append(pop)
colors.append(pop_colors[pop])
hist1, edges1 = np.histogram(l56e, bins=hist_bins)
mid1 = [e +(edges1[1]-edges1[0])/2 for e in edges1[:-1]]
histxs.append(mid1)
histys.append(hist1)
histlabels.append(pop)
histcolors.append(pop_colors[pop])
xs.append(ts)
ys.append(l56i)
pop = 'L56_I Py'
labels.append(pop)
colors.append(pop_colors[pop])
hist1, edges1 = np.histogram(l56i, bins=hist_bins)
mid1 = [e +(edges1[1]-edges1[0])/2 for e in edges1[:-1]]
histxs.append(mid1)
histys.append(hist1)
histlabels.append(pop)
histcolors.append(pop_colors[pop])
print(colors)
pynml.generate_plot(xs,
ys,
a,
labels=labels,
linewidths=[(1 if 'Py' in l else 2) for l in labels],
linestyles=[('-' if 'Py' in l else '--') for l in labels],
colors=colors,
show_plot_already=False,
yaxis='Rate (Hz)',
xaxis='Time (s)',
legend_position='right',
title_above_plot=True)
if arg_options[a][0]['noise']:
pynml.generate_plot(histxs,
histys,
'Histograms: %s'%a,
labels=histlabels,
colors=histcolors,
show_plot_already=False,
xaxis='Rate bins (Hz)',
yaxis='Num timesteps rate in bins',
markers=['o' for x in histxs],
markersizes=[2 for x in histxs],
legend_position='right',
title_above_plot=True)
import matplotlib.pyplot as plt
plt.show()
elif '-intralaminar' in sys.argv:
from neuromllite.sweep.ParameterSweep import *
from intralaminar import intralaminar_analysis, intralaminar_plt
wee = JEE; wei = JIE; wie = JEI; wii = JII; l5e_l2i = 0; l2e_l5e = 0
# Input strength of the excitatory population
# When the analysis argument is not passed run the intralaminar simulation for only one case
# total number of simulations to run for each input strength
if '-analysis' in sys.argv:
Iexts = [0, 2, 4, 6]
nruns = 10
else:
Iexts = [2]
nruns = 1
simulation = {}
for Iext in Iexts:
simulation[Iext] = {}
for run in range(nruns):
sim, net = generate(wee=wee, wei=wei, wie=wie, wii=wii, i_l5e_l2i=l5e_l2i, i_l2e_l5e=l2e_l5e, duration=25000,
areas=['V1'], Iext=[[Iext, Iext]], count=run,
net_id='Intralaminar')
################################################################################
### Run in some simulators
if not '-analysis' in sys.argv:
check_to_generate_or_run(sys.argv, sim)
else:
simulator = 'jNeuroML'
nmllr = NeuroMLliteRunner('%s.json'%sim.id,
simulator=simulator)
traces, events = nmllr.run_once('/tmp')
simulation[Iext][run] = {}
# For the purpose of this analysis we will save only the traces related to the excitatory L23 population
simulation[Iext][run]['L23_E/0/L23_E/r'] = np.array(traces['V1_L23_E/0/L23_E_comp/r'])
if '-analysis' in sys.argv:
# analyse the traces using python methods
psd_dic = intralaminar_analysis(simulation, Iexts, nruns, layer='L23', dt=2e-04, transient=5)
# plot the results
intralaminar_plt(psd_dic)
elif '-interlaminar' in sys.argv:
from neuromllite.sweep.ParameterSweep import *
import matplotlib.pylab as plt
from interlaminar import calculate_interlaminar_power_spectrum, plot_interlaminar_power_spectrum, \
plot_power_spectrum_neurodsp
# Load the python results (this script assumes that the python script
# Mejias-2016.py -interlaminar_a has already
# generated the pickle file with the results).
simulation_file = '../Python/debug/interlaminar_a/simulation.pckl'
with open(simulation_file, 'rb') as filename:
pyrate = pickle.load(filename, encoding='latin1')
dt = 2e-01
transient = 10
Nbin = 100
if '-analysis' in sys.argv:
duration = 6e05
else:
duration = 1e03
wee = JEE; wei = JIE; wie = JEI; wii = JII; l5e_l2i = .75; l2e_l5e = 1
sim, net = generate(wee=wee, wei=wei, wie=wie, wii=wii, i_l5e_l2i=l5e_l2i, i_l2e_l5e=l2e_l5e, dt=dt,
areas=['V1'], duration=duration, Iext=[[8, 8]], count=0,
net_id='Interlaminar')
# Run in some simulators
check_to_generate_or_run(sys.argv, sim)
if '-analysis' in sys.argv:
simulator = 'jNeuroML'
nmllr = NeuroMLliteRunner('%s.json' % sim.id,
simulator=simulator)
traces, events = nmllr.run_once('/tmp')
rate_conn = np.stack((np.array(traces['V1_L23_E/0/L23_E_comp/r']),
np.array(traces['V1_L23_I/0/L23_I_comp/r']),
np.array(traces['V1_L56_E/0/L56_E_comp/r']),
np.array(traces['V1_L56_I/0/L56_I_comp/r']),
))
# for compatibility with the Python code, expand the third dimension
rate_conn = np.expand_dims(rate_conn, axis=2)
# TODO: Make this more obvious
# transform the dt from ms to s, for the rest of the analysis
s_dt = dt / 1000
pxx_coupled_l23_bin, fxx_coupled_l23_bin, pxx_coupled_l56_bin, fxx_coupled_l56_bin = \
calculate_interlaminar_power_spectrum(rate_conn, s_dt, transient, Nbin)
xs1 = []
ys1 = []
labels1 = []
xs2 = []
ys2 = []
labels2 = []
histxs = []
histys = []
histlabels = []
colors = []
histcolors = []
hist_bins = 50
pop_colors = {'V1_L23_E': '#dd7777', 'V1_L23_I': '#7777dd', 'L23_E_Py':'#990000','L23_I_Py':'#000099',
'V1_L56_E': '#77dd77', 'V1_L56_I': '#dd77dd', 'L56_E_Py':'#009900','L56_I_Py':'#990099'}
# Append traces generated with NeuroML
for tr in traces:
if tr != 't':
xs1.append(traces['t'])
ys1.append(traces[tr])
pop = tr.split('/')[0]
labels1.append(pop)
colors.append(pop_colors[pop])
hist1, edges1 = np.histogram(traces[tr], bins=hist_bins)
mid1 = [e + (edges1[1] - edges1[0]) / 2 for e in edges1[:-1]]
histxs.append(mid1)
histys.append(hist1)
histcolors.append(pop_colors[pop])
histlabels.append(pop)
# Append Python traces
for key in pyrate:
if key.endswith('/conn'):
xs2.append(pyrate['ts'])
ys2.append(pyrate[key])
pop = key.split('/')[0]
labels2.append(pop)
colors.append(pop_colors[pop])
hist1, edges1 = np.histogram(pyrate[key], bins=hist_bins)
mid1 = [e +(edges1[1]-edges1[0])/2 for e in edges1[:-1]]
histxs.append(mid1)
histys.append(hist1)
histlabels.append(pop)
histcolors.append(pop_colors[pop])
pynml.generate_plot(xs1,
ys1,
'With connections Rates',
show_plot_already=False,
labels=labels1,
linewidths=[(1 if 'Py' in l else 2) for l in labels1],
yaxis='Rate (Hz)',
xaxis='Time (s)',
legend_position='right',
title_above_plot=True)
pynml.generate_plot(xs2,
ys2,
'With connections Rates',
show_plot_already=False,
labels=labels2,
linewidths=[(1 if 'Py' in l else 2) for l in labels2],
yaxis='Rate (Hz)',
xaxis='Time (s)',
legend_position='right',
title_above_plot=True)
pynml.generate_plot(histxs,
histys,
'Histograms: With Connection',
labels=histlabels,
colors=histcolors,
show_plot_already=False,
xaxis='Rate bins (Hz)',
yaxis='Num timesteps rate in bins',
markers=['o' for x in histxs],
markersizes=[2 for x in histxs],
legend_position='right',
title_above_plot=True)
# Repeat the calculations for the case where there is no connection between layers
wee = JEE; wei = JIE; wie = JEI; wii = JII; l5e_l2i = 0; l2e_l5e = 0
sim, net = generate(wee=wee, wei=wei, wie=wie, wii=wii, i_l5e_l2i=l5e_l2i, i_l2e_l5e=l2e_l5e, duration=duration,
areas=['V1'], Iext=[[8, 8]], count=0)
# Run in some simulators
check_to_generate_or_run(sys.argv, sim)
simulator = 'jNeuroML'
nmllr = NeuroMLliteRunner('%s.json' % sim.id,
simulator=simulator)
traces, events = nmllr.run_once('/tmp')
rate_noconn = np.stack((np.array(traces['V1_L23_E/0/L23_E_comp/r']),
np.array(traces['V1_L23_I/0/L23_I_comp/r']),
np.array(traces['V1_L56_E/0/L56_E_comp/r']),
np.array(traces['V1_L56_I/0/L56_I_comp/r']),
))
# for compatibility with the Python code, expand the third dimension
rate_noconn = np.expand_dims(rate_noconn, axis=2)
xs1 = []
ys1 = []
labels1 = []
xs2 = []
ys2 = []
labels2 = []
histxs = []
histys = []
histlabels = []
colors = []
histcolors = []
for tr in traces:
if tr != 't':
xs1.append(traces['t'])
ys1.append(traces[tr])
pop = tr.split('/')[0]
labels1.append(pop)
colors.append(pop_colors[pop])
hist1, edges1 = np.histogram(traces[tr], bins=hist_bins)
mid1 = [e + (edges1[1] - edges1[0]) / 2 for e in edges1[:-1]]
histxs.append(mid1)
histys.append(hist1)
histcolors.append(pop_colors[pop])
histlabels.append(pop)
# Append Python traces
for key in pyrate:
if key.endswith('/unconn'):
xs2.append(pyrate['ts'])
ys2.append(pyrate[key])
pop = key.split('/')[0]
labels2.append(pop)
colors.append(pop_colors[pop])
hist1, edges1 = np.histogram(pyrate[key], bins=hist_bins)
mid1 = [e +(edges1[1]-edges1[0])/2 for e in edges1[:-1]]
histxs.append(mid1)
histys.append(hist1)
histlabels.append(pop)
histcolors.append(pop_colors[pop])
pynml.generate_plot(xs1,
ys1,
'No connections Rates',
show_plot_already=False,
labels=labels1,
linewidths=[(1 if 'Py' in l else 2) for l in labels1],
yaxis='Rate (Hz)',
xaxis='Time (s)',
legend_position='right',
title_above_plot=True)
pynml.generate_plot(xs2,
ys2,
'No connections Rates',
show_plot_already=False,
labels=labels2,
linewidths=[(1 if 'Py' in l else 2) for l in labels2],
yaxis='Rate (Hz)',
xaxis='Time (s)',
legend_position='right',
title_above_plot=True)
pynml.generate_plot(histxs,
histys,
'Histograms: No Connection',
labels=histlabels,
colors=histcolors,
show_plot_already=False,
xaxis='Rate bins (Hz)',
yaxis='Num timesteps rate in bins',
markers=['o' for x in histxs],
markersizes=[2 for x in histxs],
legend_position='right',
title_above_plot=True)
pxx_uncoupled_l23_bin, fxx_uncoupled_l23_bin, pxx_uncoupled_l56_bin, fxx_uncoupled_l56_bin = \
calculate_interlaminar_power_spectrum(rate_noconn, s_dt, transient, Nbin)
# Plot the Power Spectrum Analysis
plot_power_spectrum_neurodsp(s_dt, rate_conn, rate_noconn, 'interlaminar')
# Plot spectrogram
plot_interlaminar_power_spectrum(fxx_uncoupled_l23_bin, fxx_coupled_l23_bin,
pxx_uncoupled_l23_bin, pxx_coupled_l23_bin,
fxx_uncoupled_l56_bin, fxx_coupled_l56_bin,
pxx_uncoupled_l56_bin, pxx_coupled_l56_bin,
'interlaminar')
plt.show()
elif '-interareal' in sys.argv:
from neuromllite.sweep.ParameterSweep import *
from interareal import interareal_analysis, interareal_plt
# Set model settings
wee = JEE; wei = JIE; wie = JEI; wii = JII; l5e_l2i = .75; l2e_l5e = 1
dt = 2e-01
transient = 5
duration = 4e04
minfreq_l23 = 30. # Hz
minfreq_l56 = 3. # Hz
net_id='Interareal'
if '-3rois' not in sys.argv and \
'-4rois' not in sys.argv and \
'-30rois' not in sys.argv:
# Background current simulation.
# Note: For testing porpose, only the rest simulation is performed if the flag '-analysis' is not
# passed
areas = ['V1', 'V4']
nareas = len(areas)
# for testing purpose generate one single simulation
if '-analysis' in sys.argv:
stats = 10
else:
stats = 1
if '-stimulate_V1' in sys.argv:
# Iext0: Excitatory current on E L23,
# Iext1: Excitatory current on layer E l56
# The first 2 values correspond to the first area, the last 2 values for the second area
stimulated_area = 'stimulate_V1'
Iext0 = 2; Iext1 = 4 # background current at excitatory population
Iext_rest = [[Iext0, Iext1],[Iext0, Iext1]]
# Injection is applied at V1
stim = 15
Iext_stim = [[stim + Iext0, stim + Iext1], [Iext0, Iext1]]
elif '-stimulate_V4' in sys.argv:
stimulated_area = 'stimulate_V4'
Iext0 = 1; Iext1= 1 # background current at excitatory population
Iext_rest = [[Iext0, Iext1],[Iext0, Iext1]]
# Injection is applied at V4
stim = 15