-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiking_model_cleo.py
1278 lines (1080 loc) · 53.3 KB
/
Spiking_model_cleo.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
L2/3: PCs, PVs, SOMs and VIPs receive L4 bottom-up and top-down input
Created on Mon Mar 6 14:12:15 2017
@author: kwilmes
"""
#import msilib
import os
import shutil
from tempfile import mkdtemp
import numpy as np
import pickle
from brian2 import *
from brian2tools import *
from sacred import Experiment
ex = Experiment("L23_network")
from analyse_experiment import *
from cleo import *
from cleo.ioproc import (LatencyIOProcessor,
FiringRateEstimator,
ConstantDelay,
PIController,
)
from cleo.coords import assign_coords_rand_rect_prism
from cleo.opto import *
from cleo.ephys import Probe, SortedSpiking
class Struct:
def __init__(self, **entries):
self.__dict__.update(entries)
class TmpExpDir(object):
"""A context manager that creates and deletes temporary directories.
"""
def __init__(self, base_dir="./"):
self._base_dir = base_dir
self._exp_dir = None
def __enter__(self):
# create a temporary directory into which we will store all our files
# it will be placed into the current directory but you could change that
self._exp_dir = mkdtemp(dir=self._base_dir)
return self._exp_dir
def __exit__(self, *args):
# at the very end of the run delete the temporary directory
# sacred will have taken care of copying all the results files over
# to the run directoy
if self._exp_dir is not None:
shutil.rmtree(self._exp_dir)
def calc_impact(con_REC):
pop1impact = np.mean(con_REC['i<100 and j>100'])
otherimpact = (np.mean(con_REC['i>100 and i<300 and j>300'])+
np.mean(con_REC['i>200 and j>100 and j<200'])+
np.mean(con_REC['i>100 and i<200 and j>200 and j<300'])+
np.mean(con_REC['i>300 and j>200 and j<300']))/4
self_impact = (np.mean(con_REC['i<100 and j<100'])+
np.mean(con_REC['i>100 and i<200 and j>100 and j<200'])+
np.mean(con_REC['i>200 and i<300 and j>200 and j<300'])+
np.mean(con_REC['i>300 and i<400 and j>300 and j<400']))/4
impact_normtoself = (pop1impact - otherimpact)/self_impact
impact_normtomax = (pop1impact - otherimpact)/np.max(con_REC)
print("impact")
return impact_normtoself, impact_normtomax
# function that defines parameters of the model:
@ex.config
def config():
params = {
# simulation parameters
'plot': False, # enables plotting during the run
'seed' : 7472, # random seed
'nonplasticwarmup_simtime' : 1.4*second,# no plasticity, to measure tuning
'warmup_simtime' : 42*second,# 42*second, # plasticity, no reward
'reward_simtime' : 24.5*second,# 24.5*second, # plasticity, with reward
'noreward_simtime': 45*second,# 45*second, # plasticity, without reward
'noSSTPV_simtime': 21*second,# 21*second, # plasticity, without reward
# for Suppl. Figure, we killed SSTPV structure after 45s, therefore the no reward simtime is split up
'after_simtime' : 1.4*second, # no plasticity, to measure tuning
'timestep' : 0.1*ms,
# number of neurons
'NPYR' : 400, # Number of excitatory L23 PYR cells
'NSOM' : 30*4, # Number of inhibitory SOM cells
'NVIP' : 50, # Number of inhibitory VIP cells
'NPV' : 120, # Number of inhibitory PV cells
'NTD' : 100, # Number of top-down units
# time constants of synaptic kernels
'tau_ampa' : 5.0*ms, # Excitatory synaptic time constant
'tau_gaba' : 10.0*ms, # Inhibitory synaptic time constant
# L4 input
'N4' : 4, # Number of L4 units
'L4_rate' : 4/(1*ms), # Firing rate of L4 units
'orientations' : np.array([0.785398163397, 1.57079632679, 2.35619449019, 0.0]), # Four orientations
'input_time' : 70*ms, # stim_time + gap_time, i.e. time between starts of two subsequent stimuli
'stim_time' : 50*ms, # duration of stimulus
# L23 neuron parameters
'gl' : 10.0*nsiemens, # Leak conductance
'el' : -60*mV, # Resting potential
'er' : -80*mV, # Inhibitory reversal potential
'vt' : -50.*mV, # Spiking threshold
'memc' : 200.0*pfarad, # Membrane capacitance
'sigma' : 2.0*mV, # sigma of Ornstein-Uhlenbeck noise
'tau_noise' : 5.0*ms, # tau of Ornstein-Uhlenbeck noise
# Connectivity
#p_pre_post is the probability of connection from a neuron in the presynaptic population to a neuron in the postsynaptic population
#w_pre_post is the synaptic strength of the connection from a neuron in the presynaptic population to a neuron in the postsynaptic population
'p_PYR_PYR' : 1.0,
'recurrent_weights' : 'clip(.01 * randn() + .01, 0, .15)*nS', # initial weights between PCs
'p_SOM_PV' : .857,
'SOM2PV_weights' : 'clip(.1 * randn() + .2, 0, 1.0)*nS', # initial weights between SST and PV
'p_L4_TD' : 1.0,
'p_TD_VIP' : 1.0,
'p_PYR_SOM' : 1.0,
'p_PYR_VIP' : 1.0,
'p_PYR_PV' : .88,
'p_SOM_PYR' : 1.0,
'p_SOM_VIP' : 1.0,
'p_PV_PV' : 1.0,
'p_VIP_SOM' : 1.0,
'p_VIP_PYR' : .125,
'p_VIP_PV' : .125,
'p_PV_SOM' : .125,
'p_PV_PYR' : 1.0,
'p_PV_VIP' : 1.0,
'p_VIP_VIP' : .125,
'p_SOM_SOM' : .125,
'w_PYR_SOM' : 0.07*nS,
'w_PYR_VIP' : 0.07*nS,
'w_PYR_PV' : .12*nS,
'w_SOM_PYR' : 0.3*nS,
'w_SOM_VIP' : 0.42*nS,
'w_PV_PV' : 0.55*nS,
'w_VIP_SOM' : 0.195*nS,
'w_PV_SOM' : 0.08*nS,
'w_PV_PYR' : 0.55*nS,
'w_PV_VIP' : 0.12*nS,
'w_VIP_PYR' : .0675*nS,
'w_VIP_PV' : .0675*nS,
'w_VIP_VIP' : .0*nS,
'w_SOM_SOM' : .0675*nS,
'w_L4PYR' : .28*nS,
'w_FFPYR' : .13*nS,
'w_FFPV' : .01*nS,
'w_FFSOM' : .15*nS,
'w_TDVIP' : .2*nS,
# gap junction parameters
'w_gap' : 0*nS, # sub-threshold coupling
'c_gap' : 13*pA, # spikelet current
'tau_spikelet' : 9.0*ms,# spikelet time constant
# Plasticity parameters
'tau_stdp' : 20*ms, # STDP time constant at excitatory synapses
'tau_istdp' : 20*ms, # STDP time constant at inhibitory synapses
'dApre' : .005, # STDP amplitude
'dApre_i' : 0.015, # Inhibitory STDP amplitude
'gmax' : .25*nS, # maximum synaptic weight for excitatory synapses
'gmax_SSTPV': 1.0*nS, # maximum synaptic weight for SST-to-PV synapses
'relbound' : .1, # maximum synaptic weight bound relative to initial weight
'restplastic' : False, # if True all connections are plastic
}
target_firing_rate = 625
@ex.command
def run_network(params,target_firing_rate,_run):
# get parameters
p = Struct(**params)
use_opto = target_firing_rate != -1
# simulation
total_simtime = p.nonplasticwarmup_simtime + p.warmup_simtime + p.reward_simtime + p.noreward_simtime + p.noSSTPV_simtime + p.after_simtime
total_warmup_simtime = p.nonplasticwarmup_simtime + p.warmup_simtime
stim_time = p.stim_time
input_time = p.input_time
seed(p.seed)
# neurons
N4 = p.N4
L4_rate = p.L4_rate
gl = p.gl
el = p.el
er = p.er
vt = p.vt
memc = p.memc
tau_gaba = p.tau_gaba
tau_ampa = p.tau_ampa
tau = p.tau_noise
sigma = p.sigma
# connections
w_PYR_PV = p.w_PYR_PV
w_PYR_VIP = p.w_PYR_VIP
w_PYR_SOM = p.w_PYR_SOM
w_FFPYR = p.w_FFPYR
w_FFPV = p.w_FFPV
w_FFSOM = p.w_FFSOM
w_TDVIP = p.w_TDVIP
w_L4PYR = p.w_L4PYR
c_gap = p.c_gap
tau_spikelet = p.tau_spikelet
# plasticity
tau_stdp = p.tau_stdp
tau_istdp = p.tau_istdp
relbound = p.relbound
gmax_SSTPV = p.gmax_SSTPV
dApre = p.dApre*nS
dApost = -dApre * tau_stdp / tau_stdp * 1.05
dApre_i = p.dApre_i*nS
dApost_i = -dApre_i * tau_istdp / tau_istdp * 1.05
# untuned Layer 4 neurons:
eqs_FF = '''
rate = L4_rate: Hz
'''
FF = NeuronGroup(1, eqs_FF, threshold='rand() < rate*dt',
method='euler', name='FF')
# tuned Layer 4 neurons:*((t<(stim_end_time+10*ms)))
eqs_layer4 = '''
rate = clip(cos(orientation*2 - selectivity*2), 0, inf)*L4_rate : Hz
stim_rate = rate*(int(t<stim_end_time)): Hz
gap_rate = (L4_rate*2/5)*(int(t>=stim_end_time)) : Hz
selectivity : 1 # preferred orientation
orientation : 1 (shared) # orientation of the current stimulus
stim_start_time : second (shared) # start time of the current stimulus
stim_end_time : second (shared) # end time of the current stimulus
'''
layer4 = NeuronGroup(N4, eqs_layer4, threshold='rand() < stim_rate *dt',
method='euler', name='layer4')
gapfiller = NeuronGroup(N4, '''gap_rate : Hz (linked)''', threshold='rand() < gap_rate *dt',
method='euler', name='gapfiller')
gapfiller.gap_rate = linked_var(layer4, 'gap_rate')
# selectivities for N4 = 4 neurons: 180, 45, 90, and 135 degrees in radians
layer4.selectivity = '(i%N4)/(1.0*N4)*pi' # for each L4 neuron, selectivity between 0 and pi
# Choose one of the four preferred oriented bars every 70ms (discrete stimulus)
# idx = int(floor(rand()*N4)) for N4=4 samples uniformly from [0,1,2,3]
# orientation = (idx%4)/(1.0*4)*pi
runner_code = '''
orientation = ((int(floor(rand()*N4)))%4)/(1.0*4)*pi
stim_start_time = t
stim_end_time = t + stim_time
'''
layer4.run_regularly(runner_code, dt=p.input_time, when='start')
Stimmonitor = SpikeMonitor(layer4, variables=['orientation'])
# L23 neurons
eqs_neurons='''
dv/dt=(-gl*(v-el)+Isyn+Igap+Ispikelet+Iopto)/memc + sigma * (2 / tau)**.5 *xi_model: volt (unless refractory)
Isyn = IsynE + IsynI : amp
IsynE = -g_ampa*v : amp
IsynI = -g_gaba*(v-er) : amp
Igap: amp
Iopto: amp
dIspikelet/dt = -Ispikelet/tau_spikelet : amp
dg_ampa/dt = -g_ampa/tau_ampa : siemens
dg_gaba/dt = -g_gaba/tau_gaba : siemens
x : meter
y : meter
z : meter
'''
# Excitatory synapses
STDP_E = '''w : siemens
gmax : siemens
dApre/dt = -Apre / tau_stdp : siemens (event-driven)
dApost/dt = -Apost / tau_stdp : siemens (event-driven)
plastic : boolean (shared)
'''
# STDP at excitatory synapses
on_pre_STDP_E = '''g_ampa += w
Apre += dApre
w = clip(w + plastic*Apost, 0*siemens, gmax)'''
on_post_STDP_E = '''Apost += dApost
w = clip(w + plastic*Apre, 0*siemens, gmax)'''
# anti-Hebbian STDP at excitatory synapses
on_pre_antiHebb_IE = '''g_ampa += w
Apre += dApre
w = clip(w - plastic*Apost, 0*siemens, gmax)'''
on_post_antiHebb_IE = '''Apost += dApost
w = clip(w - plastic*Apre, 0*siemens, gmax)'''
# define neurons
exc_neurons = NeuronGroup(p.NPYR, model=eqs_neurons, threshold='v > vt',
reset='v=el', refractory=2*ms, method='euler')
inh_neurons = NeuronGroup(p.NSOM+p.NVIP+p.NPV, model=eqs_neurons, threshold='v > vt',
reset='v=el', refractory=2*ms, method='euler')
inh_neurons.x=np.random.uniform(-.2,.2, p.NSOM+p.NVIP+p.NPV)*mmeter
inh_neurons.y=np.random.uniform(-.2,.2, p.NSOM+p.NVIP+p.NPV)*mmeter
inh_neurons.z=np.random.uniform(.4,.6,p.NSOM+p.NVIP+p.NPV)*mmeter
exc_neurons.x=np.random.uniform(-.2,.2, p.NPYR)*mmeter
exc_neurons.y=np.random.uniform(-.2,.2, p.NPYR)*mmeter
exc_neurons.z=np.random.uniform(.4,.6, p.NPYR)*mmeter
PYR = exc_neurons[:p.NPYR]
SOM = inh_neurons[:p.NSOM]
VIP = inh_neurons[p.NSOM:int(p.NSOM+p.NVIP)]
PV = inh_neurons[int(p.NSOM+p.NVIP):]
neuron1 = StateMonitor(PYR, ('v', 'Isyn', 'IsynE', 'IsynI'), record=PYR[0:1])
neuron2 = StateMonitor(PYR, ('v','Isyn','IsynE','IsynI'), record=PYR[100:101])
neuron3 = StateMonitor(PYR, ('v','IsynE', 'IsynI'), record=PYR[200:201])
neuron4 = StateMonitor(PYR, ('v','IsynE','IsynI'), record=PYR[300:301])
currents = StateMonitor(PYR, ('IsynE', 'IsynI'), record=[0,1,2,3,4,100,101,102,103,104,200,201,202,203,204,300,301,302,303,304])
SOMneuron1 = StateMonitor(SOM, ('v','Isyn','IsynE','IsynI'), record=SOM[0:1])
SOMneuron2 = StateMonitor(SOM, ('v','Isyn','IsynE','IsynI'), record=SOM[30:31])
VIPneuron1 = StateMonitor(VIP, ('v','Isyn','IsynE','IsynI'), record=VIP[0:1])
PVneuron1 = StateMonitor(PV, ('v','Isyn','IsynE','IsynI'), record=PV[0:1])
# Feedforward synaptic connections from L4 to L23
feedforward1 = Synapses(layer4[0:1], PYR[0:100],
'''w = w_L4PYR: siemens''',
on_pre='g_ampa += w', name='feedforward1')
feedforward1.connect(p=1)
feedforward2 = Synapses(layer4[1:2], PYR[100:200], on_pre='g_ampa += w_L4PYR', name='feedforward2')
feedforward2.connect(p=1)
feedforward3 = Synapses(layer4[2:3], PYR[200:300], on_pre='g_ampa += w_L4PYR', name='feedforward3')
feedforward3.connect(p=1)
feedforward4 = Synapses(layer4[3:4], PYR[300:400], on_pre='g_ampa += w_L4PYR', name='feedforward4')
feedforward4.connect(p=1)
feedforwardgap1 = Synapses(gapfiller[0:1], PYR[0:100], on_pre='g_ampa += w_L4PYR', name='feedforwardgap1')
feedforwardgap1.connect(p=1)
feedforwardgap2 = Synapses(gapfiller[1:2], PYR[100:200], on_pre='g_ampa += w_L4PYR', name='feedforwardgap2')
feedforwardgap2.connect(p=1)
feedforwardgap3 = Synapses(gapfiller[2:3], PYR[200:300], on_pre='g_ampa += w_L4PYR', name='feedforwardgap3')
feedforwardgap3.connect(p=1)
feedforwardgap4 = Synapses(gapfiller[3:4], PYR[300:400], on_pre='g_ampa += w_L4PYR', name='feedforwardgap4')
feedforwardgap4.connect(p=1)
feedforward_unspec = Synapses(FF, PYR, on_pre='g_ampa += w_FFPYR', name='feedforward_unspec')
feedforward_unspec.connect(p=1)
feedforward_PV = Synapses(FF, PV, on_pre='g_ampa += w_FFPV', name='feedforward_PV')
feedforward_PV.connect(p=1)
feedforward_i1 = Synapses(layer4[0:1], SOM[0:30], on_pre='g_ampa += w_FFSOM', name='feedforward_i1')
feedforward_i1.connect(p=1)
feedforward_i2 = Synapses(layer4[1:2], SOM[30:60], on_pre='g_ampa += w_FFSOM', name='feedforward_i2')
feedforward_i2.connect(p=1)
feedforward_i3 = Synapses(layer4[2:3], SOM[60:90], on_pre='g_ampa += w_FFSOM', name='feedforward_i3')
feedforward_i3.connect(p=1)
feedforward_i4 = Synapses(layer4[3:4], SOM[90:120], on_pre='g_ampa += w_FFSOM', name='feedforward_i4')
feedforward_i4.connect(p=1)
feedforward_gap1 = Synapses(gapfiller[0:1], SOM[0:30], on_pre='g_ampa += w_FFSOM*1.1', name='feedforward_gapi1')
feedforward_gap1.connect(p=1)
feedforward_gap2 = Synapses(gapfiller[1:2], SOM[30:60], on_pre='g_ampa += w_FFSOM*1.1', name='feedforward_gapi2')
feedforward_gap2.connect(p=1)
feedforward_gap3 = Synapses(gapfiller[2:3], SOM[60:90], on_pre='g_ampa += w_FFSOM*1.1', name='feedforward_gapi3')
feedforward_gap3.connect(p=1)
feedforward_gap4 = Synapses(gapfiller[3:4], SOM[90:120], on_pre='g_ampa += w_FFSOM*1.1', name='feedforward_gapi4')
feedforward_gap4.connect(p=1)
# Synaptic connections within L23
# Connections from PCs to SSTs:
on_pre_PCSOM = on_pre_antiHebb_IE
on_post_PCSOM = on_post_antiHebb_IE
PYR_SOM1 = Synapses(PYR[0:100], SOM[0:30], STDP_E, on_pre= on_pre_PCSOM,on_post = on_post_PCSOM,name='PYR_SOM1')
PYR_SOM1.connect(p=p.p_PYR_SOM)
PYR_SOM1.w = w_PYR_SOM
PYR_SOM1.gmax = w_PYR_SOM+relbound*nS
PYR_SOM2 = Synapses(PYR[100:200], SOM[30:60], STDP_E, on_pre= on_pre_PCSOM,on_post = on_post_PCSOM, name='PYR_SOM2')
PYR_SOM2.connect(p=p.p_PYR_SOM)
PYR_SOM2.w = w_PYR_SOM
PYR_SOM2.gmax = w_PYR_SOM+relbound*nS
PYR_SOM3 = Synapses(PYR[200:300], SOM[60:90], STDP_E, on_pre= on_pre_PCSOM,on_post = on_post_PCSOM, name='PYR_SOM3')
PYR_SOM3.connect(p=p.p_PYR_SOM)
PYR_SOM3.w = w_PYR_SOM
PYR_SOM3.gmax = w_PYR_SOM+relbound*nS
PYR_SOM4 = Synapses(PYR[300:400], SOM[90:120], STDP_E, on_pre= on_pre_PCSOM,on_post = on_post_PCSOM, name='PYR_SOM4')
PYR_SOM4.connect(p=p.p_PYR_SOM)
PYR_SOM4.w = w_PYR_SOM
PYR_SOM4.gmax = w_PYR_SOM+relbound*nS
# Inhibitory synapses
Synaptic_model_I = '''w : siemens
gmax_i : siemens
dApre_i/dt = -Apre_i / tau_istdp : siemens (event-driven)
dApost_i/dt = -Apost_i / tau_istdp : siemens (event-driven)
plastic : boolean (shared)'''
# STDP at inhibitory synapses
on_pre_STDP_I = '''g_gaba += w
Apre_i += dApre_i
w = clip(w + plastic*Apost_i, 0*siemens, gmax_i)'''
on_post_STDP_I = '''Apost_i += dApost_i
w = clip(w + plastic*Apre_i, 0*siemens, gmax_i)'''
# anti-Hebbian STDP at inhibitory synapses
on_pre_antiHebb_I = '''g_gaba += w
Apre_i += dApre_i
w = clip(w - plastic*Apost_i, 0*siemens, gmax_i)'''
on_post_antiHebb_I = '''Apost_i += dApost_i
w = clip(w - plastic*Apre_i, 0*siemens, gmax_i)'''
"""excitatory synapses"""
# plastic recurrent synapses
con_REC = Synapses(PYR, PYR,
STDP_E,
on_pre=on_pre_STDP_E,
on_post=on_post_STDP_E,
name='recurrent')
con_REC.connect(p=p.p_PYR_PYR)
con_REC.gmax = p.gmax
con_REC.w = p.recurrent_weights
# SST to PV
con_SOM_PV = Synapses(SOM, PV,
Synaptic_model_I,
on_pre=on_pre_STDP_I,
on_post=on_post_STDP_I,
name='som2pv')
con_SOM_PV.connect(p=p.p_SOM_PV)
con_SOM_PV.w = p.SOM2PV_weights
con_SOM_PV.gmax_i = p.gmax_SSTPV
# PYR to PV
con_PYR_PV = Synapses(PYR, PV, STDP_E,
on_pre= on_pre_STDP_E,
on_post = on_post_STDP_E,
name='PYR0_PV0')
con_PYR_PV.connect(p=p.p_PYR_PV)
con_PYR_PV.w = w_PYR_PV
con_PYR_PV.gmax = p.w_PYR_PV+relbound*nS
# PC to VIP
con_PYR_VIP = Synapses(PYR, VIP, STDP_E,
on_pre= on_pre_STDP_E,
on_post = on_post_STDP_E,
name='PYR0_VIP0')
con_PYR_VIP.connect(p=p.p_PYR_VIP)
con_PYR_VIP.w = w_PYR_VIP
con_PYR_VIP.gmax = p.w_PYR_VIP+relbound*nS
"""inhibitory synapses"""
# SST to PC
con_SOM_PYR = Synapses(SOM, PYR, Synaptic_model_I,
on_pre=on_pre_STDP_I,
on_post=on_post_STDP_I,
name='SOMPYR')
con_SOM_PYR.connect(p=p.p_SOM_PYR)
con_SOM_PYR.w = p.w_SOM_PYR
con_SOM_PYR.gmax_i = p.w_SOM_PYR+relbound*nS
# SST to VIP
con_SOM_VIP = Synapses(SOM, VIP, Synaptic_model_I,
on_pre='''g_gaba += w
Apre_i += dApre_i
w = clip(w + plastic*.1*Apost_i, 0*siemens, gmax_i)''',
on_post='''Apost_i += dApost_i
w = clip(w + plastic*.1*Apre_i, 0*siemens, gmax_i)''',
name='SOMVIP')
con_SOM_VIP.connect(p=p.p_SOM_VIP)
con_SOM_VIP.w = p.w_SOM_VIP
con_SOM_VIP.gmax_i = p.w_SOM_VIP+relbound*nS
#SST to SST
con_SOM_SOM = Synapses(SOM, SOM, Synaptic_model_I,
on_pre=on_pre_STDP_I,
on_post=on_post_STDP_I,
name='SOMSOM')
con_SOM_SOM.connect(p=p.p_SOM_SOM)
con_SOM_SOM.w = p.w_SOM_SOM
con_SOM_SOM.gmax_i = p.w_SOM_SOM+relbound*nS
# PV to PC
con_PV_PYR = Synapses(PV, PYR,Synaptic_model_I,
on_pre=on_pre_antiHebb_I,
on_post=on_post_antiHebb_I,
name='PVPYR')
con_PV_PYR.connect(p=p.p_PV_PYR)
con_PV_PYR.w = p.w_PV_PYR
con_PV_PYR.gmax_i = p.w_PV_PYR+relbound*nS
#PV to SST
con_PV_SOM = Synapses(PV, SOM,Synaptic_model_I,
on_pre=on_pre_STDP_I,
on_post=on_post_STDP_I,
name='PVSOM')
con_PV_SOM.connect(p=p.p_PV_SOM)
con_PV_SOM.w = p.w_PV_SOM
con_PV_SOM.gmax_i = p.w_PV_SOM+relbound*nS
#PV to VIP
con_PV_VIP = Synapses(PV, VIP,Synaptic_model_I,
on_pre=on_pre_STDP_I,
on_post=on_post_STDP_I,
name='PVVIP')
con_PV_VIP.connect(p=p.p_PV_VIP)
con_PV_VIP.w = p.w_PV_VIP
con_PV_VIP.gmax_i = p.w_PV_VIP+relbound*nS
#PV to PV
con_PV_PV = Synapses(PV, PV,Synaptic_model_I,
on_pre=on_pre_STDP_I,
on_post=on_post_STDP_I,
name='PVPV')
con_PV_PV.connect(p=p.p_PV_PV)
con_PV_PV.w = p.w_PV_PV
con_PV_PV.gmax_i = p.w_PV_PV+relbound*nS
# VIP to SST
on_pre_VIPSOM = on_pre_antiHebb_I
on_post_VIPSOM = on_post_antiHebb_I
con_VIP_SOM = Synapses(VIP, SOM, Synaptic_model_I,
on_pre=on_pre_VIPSOM,
on_post=on_post_VIPSOM,
name='VIPSOM')
con_VIP_SOM.connect(p=p.p_VIP_SOM)
con_VIP_SOM.w = p.w_VIP_SOM
con_VIP_SOM.gmax_i = p.w_VIP_SOM+relbound*nS
# VIP to PC
con_VIP_PYR = Synapses(VIP, PYR,Synaptic_model_I,
on_pre=on_pre_STDP_I,
on_post=on_post_STDP_I,
name='VIPPYR')
con_VIP_PYR.connect(p=p.p_VIP_PYR)
con_VIP_PYR.w = p.w_VIP_PYR
con_VIP_PYR.gmax_i = p.w_VIP_PYR+relbound*nS
# VIP to PV
con_VIP_PV = Synapses(VIP, PV,Synaptic_model_I,
on_pre=on_pre_STDP_I,
on_post=on_post_STDP_I,
name='VIPPV')
con_VIP_PV.connect(p=p.p_VIP_PV)
con_VIP_PV.w = p.w_VIP_PV
con_VIP_PV.gmax_i = p.w_VIP_PV+relbound*nS
# VIP to VIP
con_VIP_VIP = Synapses(VIP, VIP,Synaptic_model_I,
on_pre=on_pre_STDP_I,
on_post=on_post_STDP_I,
name='VIPVIP')
con_VIP_VIP.connect(p=p.p_VIP_VIP)
con_VIP_VIP.w = p.w_VIP_VIP
con_VIP_VIP.gmax_i = p.w_VIP_VIP+relbound*nS
# gap junctions between PVs
PVPV_gap = Synapses(PV, PV, '''w : siemens
Igap_post = w * (v_pre - v_post) : amp (summed)
''',
on_pre='Ispikelet+=c_gap',
)
PVPV_gap.connect()
PVPV_gap.w = p.w_gap
# monitor synaptic weights
monPYRPV = StateMonitor(con_PYR_PV, 'w', record=True, dt = 1000*ms)
monVIPSOM = StateMonitor(con_VIP_SOM, 'w', record=True, dt = 1000*ms)
monVIPPV = StateMonitor(con_VIP_PV, 'w', record=True, dt = 1000*ms)
monVIPPYR = StateMonitor(con_VIP_PYR, 'w', record=True, dt = 1000*ms)
monPVPYR = StateMonitor(con_PV_PYR, 'w', record=True, dt = 1000*ms)
monPVSOM = StateMonitor(con_PV_SOM, 'w', record=True, dt = 1000*ms)
monPVPV = StateMonitor(con_PV_PV, 'w', record=True, dt = 1000*ms)
monPVVIP = StateMonitor(con_PV_VIP, 'w', record=True, dt = 1000*ms)
monSOMVIP = StateMonitor(con_SOM_VIP, 'w', record=True, dt = 1000*ms)
monSOMPYR = StateMonitor(con_SOM_PYR, 'w', record=True, dt = 1000*ms)
monSOMSOM = StateMonitor(con_SOM_SOM, 'w', record=True, dt = 1000*ms)
monPYRSOM1 = StateMonitor(PYR_SOM1, 'w', record=True, dt = 1000*ms)
monPYRSOM2 = StateMonitor(PYR_SOM2, 'w', record=True, dt = 1000*ms)
monPYRSOM3 = StateMonitor(PYR_SOM3, 'w', record=True, dt = 1000*ms)
monPYRSOM4 = StateMonitor(PYR_SOM4, 'w', record=True, dt = 1000*ms)
monPYRVIP = StateMonitor(con_PYR_VIP, 'w', record=True, dt = 1000*ms)
monVIPVIP = StateMonitor(con_VIP_VIP, 'w', record=True, dt = 1000*ms)
# monitor excitatory connections
mona = StateMonitor(con_REC, 'w', record=con_REC[0:100,100:400], dt = 100*ms) # pyr 1 to others
monb = StateMonitor(con_REC, 'w', record=con_REC[100:400,0:100], dt = 100*ms) # other to pyr 1
monc = StateMonitor(con_REC, 'w', record=con_REC[100:200,200:400], dt = 100*ms) # pyr2 to others
mond = StateMonitor(con_REC, 'w', record=con_REC[300:400,100:300], dt = 100*ms) # pyr 4 to others
mone = StateMonitor(con_REC, 'w', record=con_REC[0:100,0:100], dt = 100*ms) # pyr 1 to pyr1
monf = StateMonitor(con_REC, 'w', record=con_REC[100:200,100:200], dt = 100*ms) # pyr 1 to pyr1
# monitor population rates
PYR1 = PopulationRateMonitor(PYR[0:100])
PYR2 = PopulationRateMonitor(PYR[100:200])
PYR3 = PopulationRateMonitor(PYR[200:300])
PYR4 = PopulationRateMonitor(PYR[300:400])
SOM1 = PopulationRateMonitor(SOM[0:30])
SOM2 = PopulationRateMonitor(SOM[30:60])
SOM3 = PopulationRateMonitor(SOM[60:90])
SOM4 = PopulationRateMonitor(SOM[90:120])
PVmon = PopulationRateMonitor(PV)
VIPmon = PopulationRateMonitor(VIP)
# monitor SST to PV connections
monSOMPV = StateMonitor(con_SOM_PV, 'w', record=True, dt = 1000*ms)
SOM0PV = StateMonitor(con_SOM_PV, 'w', record=con_SOM_PV[:30:10,::40])
SOMotherPV = StateMonitor(con_SOM_PV, 'w', record=con_SOM_PV[30::10,1::40])
# Top down input: reward for stimulus 0 (horizontal, 180 degrees)
TD = NeuronGroup(p.NTD, model=eqs_neurons, threshold='v > vt',
reset='v=el', refractory=2*ms, method='euler')
con_ff_td = Synapses(layer4[0:1], TD, on_pre='g_ampa += 0.3*nS')
con_ff_td.connect(p=p.p_L4_TD)
# top down input goes onto VIP
con_topdown = Synapses(TD, VIP, on_pre='g_ampa += w_TDVIP')
con_topdown.connect(p=p.p_TD_VIP)
# monitor spikes
sm_PYR = SpikeMonitor(PYR)
sm_VIP = SpikeMonitor(VIP)
sm_SOM = SpikeMonitor(SOM)
sm_PV = SpikeMonitor(PV)
sm_TD = SpikeMonitor(TD)
sm_layer4 = SpikeMonitor(layer4)
sm_FF = SpikeMonitor(FF)
sm_gap = SpikeMonitor(gapfiller)
# run without plasticity
defaultclock.dt = p.timestep
con_ff_td.active = False
TD.active = False
con_REC.plastic = False
con_SOM_PV.plastic = False
con_PYR_PV.plastic = False
PYR_SOM1.plastic = False
PYR_SOM2.plastic = False
PYR_SOM3.plastic = False
PYR_SOM4.plastic = False
con_PYR_VIP.plastic = False
con_VIP_SOM.plastic = False
con_VIP_PV.plastic = False
con_VIP_VIP.plastic = False
con_VIP_PYR.plastic = False
con_SOM_PYR.plastic = False
con_SOM_VIP.plastic = False
con_SOM_SOM.plastic = False
con_PV_SOM.plastic = False
con_PV_PYR.plastic = False
con_PV_VIP.plastic = False
con_PV_PV.plastic = False
conREC_start = np.copy(con_REC.w[:])
#assign_coords_rand_rect_prism(inh_neurons, xlim=(-0.4, 0.4), ylim=(-0.4, 0.4), zlim=(0.4, 0.6))
net=Network(collect())
mua = ephys.MultiUnitSpiking(
r_perfect_detection=25 * umeter,
r_half_detection=50 * umeter,
save_history=True,
)
spikes = ephys.SortedSpiking(name='spikes', r_perfect_detection=25 * umeter, r_half_detection=50 * umeter, save_history=True)
probe = ephys.Probe(coords=[0, 0, .5] * mm)
probe.add_signals(mua, spikes)
sim=CLSimulator(net)
opsin = chr2_4s()
fiber = Light(
coords=(0, 0, .4) * mm,
light_model=fiber473nm(),
name="fiber",
)
if use_opto:
sim.inject(opsin, PV, Iopto_var_name='Iopto')
sim.inject(fiber, PV)
sim.inject(probe, inh_neurons)
#def stimulus(time_ms):
#if time_ms>45000 and time_ms<65000:
#stimulus_return=.75
#initial was 2, half is 1, quarter is .5, off is 0
#else:
# stimulus_return=0
#return stimulus_return
spike_t=[]
spike_vals=[]
opto_history=[]
firing_rate_history=[]
start_time=(p.nonplasticwarmup_simtime+p.warmup_simtime)/second*1000
end_time=(p.nonplasticwarmup_simtime+p.warmup_simtime+p.reward_simtime)/second*1000
def target_Hz(t_ms):
return target_firing_rate
# class ReactiveLoopOpto(LatencyIOProcessor):
# def __init__(self):
# super().__init__(sample_period_ms=1)
# def process(self, state_dict, time_ms):
# i, t, z_t = state_dict['probe']['spikes']
# if np.size(i) <= 0:
# opto_intensity = stimulus(time_ms)
# else:
# opto_intensity = 0
# spike_t.append(time_ms)
# spike_vals.append(np.size(i))
# opto_history.append(opto_intensity)
# return output dict and time
# return ({"opto": opto_intensity}, time_ms)
class FeedbackOpto(LatencyIOProcessor):
delta = 1 # ms
time_constant=1000
Kp=.005 #.016 for firing rates on the order of 200 hz,
Ki=.003 #.008 for firing rates on the order of 200 hz,
def __init__(self):
super().__init__(sample_period_ms=self.delta, processing="parallel")
# using hand-tuned gains that seem reasonable
self.pi_controller = PIController(
target_Hz,
Kp=self.Kp,
Ki=self.Ki,
sample_period_ms=self.delta,
delay=ConstantDelay(2), # latency in ms
save_history=True, # lets us plot later
)
def process(self, state_dict, sample_time_ms):
i, t, z_t = state_dict['Probe']['spikes']
spike_t.append(sample_time_ms)
spike_vals.append(np.size(i))
# feed output and out_time through each block
sh_len=len(spike_vals)
firing_rate=0
alpha=1-2.72**(-self.delta/self.time_constant)
if sh_len<int(self.time_constant/self.delta):
firing_rate=spike_vals[sh_len-1]*1000
else:
for i in range(int(self.time_constant/self.delta)):
firing_rate=firing_rate+1000*alpha*(1-alpha)**i*spike_vals[sh_len-1-i]
firing_rate_history.append(firing_rate)
if not use_opto or sample_time_ms<start_time or sample_time_ms>end_time:
opto_intensity = 0
else:
opto_intensity, time_ms = self.pi_controller.process(
firing_rate, sample_time_ms, sample_time_ms=sample_time_ms
)
if opto_intensity < 0 : # limit to positive current
opto_intensity = 0
# time_ms at the end reflects the delays added by each block
opto_history.append(opto_intensity)
return ({fiber.name:opto_intensity}, sample_time_ms)
sim.set_io_processor(FeedbackOpto())
sim.run(p.nonplasticwarmup_simtime, report = 'text')
store('nonplasticwarmup')
print('non-plastic warmup done')
# plastic warmup
restore('nonplasticwarmup')
con_ff_td.active = False
TD.active = False
con_REC.plastic = True
con_SOM_PV.plastic = True
if p.restplastic == True:
con_VIP_SOM.plastic = True
con_PYR_PV.plastic = True
con_PV_PYR.plastic = True
con_PYR_VIP.plastic = True
PYR_SOM1.plastic = True
PYR_SOM2.plastic = True
PYR_SOM3.plastic = True
PYR_SOM4.plastic = True
con_VIP_PV.plastic = True
con_VIP_VIP.plastic = True
con_VIP_PYR.plastic = True
con_SOM_PYR.plastic = True
con_SOM_VIP.plastic = True
con_SOM_SOM.plastic = True
con_PV_SOM.plastic = True
con_PV_VIP.plastic = True
con_PV_PV.plastic = True
else:
con_PYR_PV.plastic = False
PYR_SOM1.plastic = False
PYR_SOM2.plastic = False
PYR_SOM3.plastic = False
PYR_SOM4.plastic = False
con_PYR_VIP.plastic = False
con_VIP_SOM.plastic = False
con_VIP_PV.plastic = False
con_VIP_VIP.plastic = False
con_VIP_PYR.plastic = False
con_SOM_PYR.plastic = False
con_SOM_VIP.plastic = False
con_SOM_SOM.plastic = False
con_PV_SOM.plastic = False
con_PV_PYR.plastic = False
con_PV_VIP.plastic = False
con_PV_PV.plastic = False
print('starting warmup')
sim.run(p.warmup_simtime, report = 'text')
conREC_afterwarmup = np.copy(con_REC.w[:])
sstpv_w_afterwarmup = np.copy(con_SOM_PV.w[:])
store('afterwarmup')
print('warmup done')
# rewarded phase
restore('afterwarmup')
con_ff_td.active = True
TD.active = True
con_REC.plastic = True
con_SOM_PV.plastic = True
print('starting reward period')
sim.run(p.reward_simtime, report = 'text')
impact_afterreward, impactmax_afterreward = calc_impact(con_REC.w)
print('calculated impacts')
conREC_afterreward = np.copy(con_REC.w[:])
print('copied con_Rec')
sstpv_w_afterreward = np.copy(con_SOM_PV.w[:])
print('copied sstpv')
store('afterreward')
print('rewarded phase done')
# refinement phase
restore('afterreward')
con_SOM_PV.plastic = True
con_ff_td.active = False
con_topdown.active = False
TD.active = False
print('starting refinement phase')
sim.run(p.noreward_simtime, report = 'text')
store('afternoreward')
print('45s of refinement phase done')
# refinement phase, option to kill SST-PV structure
restore('afternoreward')
# For Suppl. Fig. kill inhibitory weight structure:
# con_SOM_PV.w_i = p.SOM2PV_weights
con_ff_td.active = False
TD.active = False
con_REC.plastic = True
con_SOM_PV.plastic = True
sim.run(p.noSSTPV_simtime, report = 'text')
store('afternoSSTPV')
print('refinement phase done')
# final non-plastic phase to measure tuning
restore('afternoSSTPV')
con_ff_td.active = False
TD.active = False
con_REC.plastic = False
con_SOM_PV.plastic = False
con_PYR_PV.plastic = False
PYR_SOM1.plastic = False
PYR_SOM2.plastic = False
PYR_SOM3.plastic = False
PYR_SOM4.plastic = False
con_PYR_VIP.plastic = False
con_VIP_SOM.plastic = False
con_VIP_PV.plastic = False
con_VIP_VIP.plastic = False
con_VIP_PYR.plastic = False
con_SOM_PYR.plastic = False
con_SOM_VIP.plastic = False
con_SOM_SOM.plastic = False
con_PV_SOM.plastic = False
con_PV_PYR.plastic = False
con_PV_VIP.plastic = False
con_PV_PV.plastic = False
sim.run(p.after_simtime)
# get spiking information
PYR_spiketrains = sm_PYR.spike_trains()
SOM_spiketrains = sm_SOM.spike_trains()
VIP_spiketrains = sm_VIP.spike_trains()
PV_spiketrains = sm_PV.spike_trains()
stimuli_t = Stimmonitor.t
PYRi, PYRt = sm_PYR.it
SSTi, SSTt = sm_SOM.it
PVi, PVt = sm_PV.it
VIPi, VIPt = sm_VIP.it
gapi, gapt = sm_gap.it
results = {
'SOM0PV' : SOM0PV.w,
'SOMotherPV' : SOMotherPV.w,
'weights_rec' : con_REC.w[:],
'weights_rec_afterwarmup' : conREC_afterwarmup,
'weights_rec_afterreward' : conREC_afterreward,
'weights_rec_start' : conREC_start,
'weights_rec_i' : con_REC.i[:],
'weights_rec_j' : con_REC.j[:],
'weights_sst_pv': con_SOM_PV.w[:],
'weights_sst_pv_afterreward' : sstpv_w_afterreward,
'weights_sst_pv_afterwarmup' : sstpv_w_afterwarmup,
't' : PYR1.t[:],
'SOMPV_t' : monSOMPV.t[:],
'SOMPV_w' : monSOMPV.w[:],
'SOMPV_t' : monSOMPV.t[:],
'SOMPV_w' : monSOMPV.w[:],
'PYRPV_w' : monPYRPV.w[:],
'PYRVIP_w' : monPYRVIP.w[:],
'PVPYR_w' : monPVPYR.w[:],
'PVPV_w' : monPVPV.w[:],
'PVSOM_w' : monPVSOM.w[:],
'PVVIP_w' : monPVVIP.w[:],
'VIPSOM_w' : monVIPSOM.w[:],
'VIPPYR_w' : monVIPPYR.w[:],
'VIPPV_w' : monVIPPV.w[:],
'VIPVIP_w' : monVIPVIP.w[:],
'SOMVIP_w' : monSOMVIP.w[:],
'SOMPYR_w' : monSOMPYR.w[:],
'SOMSOM_w' : monSOMSOM.w[:],
'PYRSOM1_w' : monPYRSOM1.w[:],
'PYRSOM2_w' : monPYRSOM2.w[:],
'PYRSOM3_w' : monPYRSOM3.w[:],
'PYRSOM4_w' : monPYRSOM4.w[:],
'PYR0toothers': mona.w,
'otherstoPYR0': monb.w,
'PYR1toothers': monc.w,
'PYR2toothers': mond.w,
'PYRi' : PYRi[:],
'PYRt' : PYRt[:],