This repository has been archived by the owner on Feb 20, 2020. It is now read-only.
forked from danielmk/ouropy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genconnection.py
874 lines (753 loc) · 32.2 KB
/
genconnection.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
# -*- coding: utf-8 -*-
"""
This module implements the GenNetwork class, which implements generic network
logic. Also implements Population and GenConnection classes
@author: DanielM
"""
from neuron import h
import random
import numpy as np
import matplotlib.pyplot as plt
import math
import time
import os
import shelve
import scipy.stats as stats
class GenConnection(object):
def __init__(self):
pass
def get_description(self):
"""Return a descriptive string for the connection"""
name = self.pre_pop.name + ' to ' + self.post_pop.name + '\n'
pre_cell_targets = '\n'.join([str(x) for x in self.pre_cell_targets])
return name + pre_cell_targets
def get_name(self):
if type(self.pre_pop) == str:
return self.pre_pop + ' to ' + str(self.post_pop)
else:
return str(self.pre_pop) + ' to ' + str(self.post_pop)
def get_properties(self):
"""Get the and make them suitable for pickling"""
properties = {'name': self.get_name(),
'init_parameters': self.init_parameters,
'pre_cell_targets': self.pre_cell_targets}
properties['init_parameters']['post_pop'] = str(properties['init_parameters']['post_pop'])
properties['init_parameters']['self'] = str(properties['init_parameters']['self'])
try:
properties['init_parameters']['pre_pop'] = str(properties['init_parameters']['pre_pop'])
except:
pass
return {self.get_name(): properties}
class tmgsynConnection(GenConnection):
def __init__(self, pre_pop, post_pop,
target_pool, target_segs, divergence,
tau_1, tau_facil, U, tau_rec, e, thr, delay, weight):
"""Create a connection with tmgsyn as published by Tsodyks, Pawelzik &
Markram, 1998.
The tmgsyn is a dynamic three state implicit resource synapse model.
The response onset is instantaneous and the decay is exponential.
It combines a frequency dependent depression and a facilitation
mechanism that both depend on independent time constants.
The synaptic targets are chosen by proximity, that is the target pool
are the cells in closest proximity.
Parameters
----------
pre_pop - gennetwork.Population
The presynaptic population
post_pop - gennetwork.Population
the postsynaptic population
target_pool - int
the number of cells in the target pool
target_segs - str
the name of the segments that are possible synaptic targets at the
postsynaptic population
divergence - int
divergence in absolute terms, that is the number of synapses each
presynaptic cell forms
tau_1 - numeric
the time constant of synaptic decay. conforms to the transition
from the active to the inactive resource state. units of time as in
neuron standard units
tau_facil - numeric
the time constant of facilitation decay. this essentially creates
the frequency dependence. set to 0 for no facilitation.
U - numeric
maximum of postsynaptic response. has to be considered together
with the weight from the netcon.
tau_rec - numeric
time constant of recovery from inactive for recovered state.
gives depression since inactive resources do not contribute to
postsynaptic signal. set to 0 for no depression.
e - numeric
equilibrium potential of the postsynaptic conductance
thr - numeric
threshold for synaptic event at the presynaptic source
delay - numeric
delay between presynaptic signal and onset of postsynaptic signal
weight - numeric
weight for the netcon object connecting source and target
Returns
-------
None
Use Cases
---------
>>> tmgsynConnection(nw.population[0], nw.population[1],
3, 'prox', 1, 6.0, 0, 0.04, 0, 0, 10, 3, 0)
A non-facilitating, non-depressing excitatory connection.
"""
self.init_parameters = locals()
self.pre_pop = pre_pop
self.post_pop = post_pop
pre_pop.add_connection(self)
post_pop.add_connection(self)
pre_pop_rad = (np.arange(pre_pop.get_cell_number(), dtype=float) /
pre_pop.get_cell_number()) * (2*np.pi)
post_pop_rad = (np.arange(post_pop.get_cell_number(), dtype=float) /
post_pop.get_cell_number()) * (2*np.pi)
pre_pop_pos = pos(pre_pop_rad)
post_pop_pos = pos(post_pop_rad)
pre_cell_target = []
synapses = []
netcons = []
conductances = []
for idx, curr_cell_pos in enumerate(pre_pop_pos):
curr_dist = []
for post_cell_pos in post_pop_pos:
curr_dist.append(euclidian_dist(curr_cell_pos, post_cell_pos))
sort_idc = np.argsort(curr_dist)
closest_cells = sort_idc[0:target_pool]
picked_cells = np.random.choice(closest_cells,
divergence,
replace=False)
pre_cell_target.append(picked_cells)
for tar_c in picked_cells:
curr_syns = []
curr_netcons = []
curr_conductances = []
curr_seg_pool = post_pop[tar_c].get_segs_by_name(target_segs)
chosen_seg = np.random.choice(curr_seg_pool)
for seg in chosen_seg:
curr_syn = h.tmgsyn(chosen_seg(0.5))
curr_syn.tau_1 = tau_1
curr_syn.tau_facil = tau_facil
curr_syn.U = U
curr_syn.e = e
curr_syn.tau_rec = tau_rec
curr_syns.append(curr_syn)
curr_netcon = h.NetCon(pre_pop[idx].soma(0.5)._ref_v,
curr_syn, thr, delay,
weight, sec=pre_pop[idx].soma)
curr_gvec = h.Vector()
curr_gvec.record(curr_syn._ref_g)
curr_conductances.append(curr_gvec)
curr_netcons.append(curr_netcon)
netcons.append(curr_netcons)
synapses.append(curr_syns)
conductances.append(curr_conductances)
self.conductances = conductances
self.netcons = netcons
self.pre_cell_targets = np.array(pre_cell_target)
self.synapses = synapses
class tmgsynConnectionExponentialProb(GenConnection):
def __init__(self, pre_pop, post_pop,
scale, target_segs, divergence,
tau_1, tau_facil, U, tau_rec, e, thr, delay, weight):
"""Create a connection with tmgsyn as published by Tsodyks, Pawelzik &
Markram, 1998.
The tmgsyn is a dynamic three state implicit resource synapse model.
The response onset is instantaneous and the decay is exponential.
It combines a frequency dependent depression and a facilitation
mechanism that both depend on independent time constants.
The synaptic targets are chosen by proximity, that is the target pool
are the cells in closest proximity.
Parameters
----------
pre_pop - gennetwork.Population
The presynaptic population
post_pop - gennetwork.Population
the postsynaptic population
target_pool - int
the number of cells in the target pool
target_segs - str
the name of the segments that are possible synaptic targets at the
postsynaptic population
divergence - int
divergence in absolute terms, that is the number of synapses each
presynaptic cell forms
tau_1 - numeric
the time constant of synaptic decay. conforms to the transition
from the active to the inactive resource state. units of time as in
neuron standard units
tau_facil - numeric
the time constant of facilitation decay. this essentially creates
the frequency dependence. set to 0 for no facilitation.
U - numeric
maximum of postsynaptic response. has to be considered together
with the weight from the netcon.
tau_rec - numeric
time constant of recovery from inactive for recovered state.
gives depression since inactive resources do not contribute to
postsynaptic signal. set to 0 for no depression.
e - numeric
equilibrium potential of the postsynaptic conductance
thr - numeric
threshold for synaptic event at the presynaptic source
delay - numeric
delay between presynaptic signal and onset of postsynaptic signal
weight - numeric
weight for the netcon object connecting source and target
Returns
-------
None
Use Cases
---------
>>> tmgsynConnection(nw.population[0], nw.population[1],
3, 'prox', 1, 6.0, 0, 0.04, 0, 0, 10, 3, 0)
A non-facilitating, non-depressing excitatory connection.
"""
self.init_parameters = locals()
self.pre_pop = pre_pop
self.post_pop = post_pop
pre_pop.add_connection(self)
post_pop.add_connection(self)
pre_pop_rad = (np.arange(pre_pop.get_cell_number(), dtype=float) /
pre_pop.get_cell_number()) * (2*np.pi)
post_pop_rad = (np.arange(post_pop.get_cell_number(), dtype=float) /
post_pop.get_cell_number()) * (2*np.pi)
pre_pop_pos = pos(pre_pop_rad)
post_pop_pos = pos(post_pop_rad)
pre_cell_target = []
synapses = []
netcons = []
# Setup the Gaussian distribution
loc = post_pop.get_cell_number() / 2
gauss = stats.expon(loc=0, scale=scale)
pdf = gauss.pdf(np.arange(post_pop.get_cell_number()))
pdf = pdf/pdf.sum()
for idx, curr_cell_pos in enumerate(pre_pop_pos):
curr_dist = []
for post_cell_pos in post_pop_pos:
curr_dist.append(euclidian_dist(curr_cell_pos, post_cell_pos))
sort_idc = np.argsort(curr_dist)
picked_cells = np.random.choice(sort_idc, divergence,
replace=True, p=pdf)
pre_cell_target.append(picked_cells)
for target_cell in picked_cells:
curr_syns = []
curr_netcons = []
curr_seg_pool = post_pop[target_cell].get_segs_by_name(target_segs)
chosen_seg = np.random.choice(curr_seg_pool)
for seg in chosen_seg:
curr_syn = h.tmgsyn(chosen_seg(0.5))
curr_syn.tau_1 = tau_1
curr_syn.tau_facil = tau_facil
curr_syn.U = U
curr_syn.e = e
curr_syn.tau_rec = tau_rec
curr_syns.append(curr_syn)
curr_netcon = h.NetCon(pre_pop[idx].soma(0.5)._ref_v,
curr_syn, thr, delay, weight,
sec=pre_pop[idx].soma)
curr_netcons.append(curr_netcon)
netcons.append(curr_netcons)
synapses.append(curr_syns)
self.netcons = netcons
self.pre_cell_targets = np.array(pre_cell_target)
self.synapses = synapses
class tmgsynConnection_old(GenConnection):
def __init__(self, pre_pop, post_pop,
target_pool, target_segs, divergence,
tau_1, tau_facil, U, tau_rec, e, thr, delay, weight):
"""Create a connection with tmgsyn as published by Tsodyks, Pawelzik &
Markram, 1998.
The tmgsyn is a dynamic three state implicit resource synapse model.
The response onset is instantaneous and the decay is exponential.
It combines a frequency dependent depression and a facilitation
mechanism that both depend on independent time constants.
The synaptic targets are chosen by proximity, that is the target pool
are the cells in closest proximity.
Parameters
----------
pre_pop - gennetwork.Population
The presynaptic population
post_pop - gennetwork.Population
the postsynaptic population
target_pool - int
the number of cells in the target pool
target_segs - str
the name of the segments that are possible synaptic targets at the
postsynaptic population
divergence - int
divergence in absolute terms, that is the number of synapses each
presynaptic cell forms
tau_1 - numeric
the time constant of synaptic decay. conforms to the transition
from the active to the inactive resource state. units of time as in
neuron standard units
tau_facil - numeric
the time constant of facilitation decay. this essentially creates
the frequency dependence. set to 0 for no facilitation.
U - numeric
maximum of postsynaptic response. has to be considered together
with the weight from the netcon.
tau_rec - numeric
time constant of recovery from inactive for recovered state.
gives depression since inactive resources do not contribute to
postsynaptic signal. set to 0 for no depression.
e - numeric
equilibrium potential of the postsynaptic conductance
thr - numeric
threshold for synaptic event at the presynaptic source
delay - numeric
delay between presynaptic signal and onset of postsynaptic signal
weight - numeric
weight for the netcon object connecting source and target
Returns
-------
None
Use Cases
---------
>>> tmgsynConnection(nw.population[0], nw.population[1],
3, 'prox', 1, 6.0, 0, 0.04, 0, 0, 10, 3, 0)
A non-facilitating, non-depressing excitatory connection.
"""
self.init_parameters = locals()
self.pre_pop = pre_pop
self.post_pop = post_pop
pre_pop.add_connection(self)
post_pop.add_connection(self)
pre_pop_rad = (np.arange(pre_pop.get_cell_number(), dtype=float) /
pre_pop.get_cell_number()) * (2*np.pi)
post_pop_rad = (np.arange(post_pop.get_cell_number(), dtype=float) /
post_pop.get_cell_number()) * (2*np.pi)
pre_pop_pos = pos(pre_pop_rad)
post_pop_pos = pos(post_pop_rad)
pre_cell_target = []
synapses = []
netcons = []
for idx, curr_cell_pos in enumerate(pre_pop_pos):
curr_dist = []
for post_cell_pos in post_pop_pos:
curr_dist.append(euclidian_dist(curr_cell_pos, post_cell_pos))
sort_idc = np.argsort(curr_dist)
closest_cells = sort_idc[0:target_pool]
picked_cells = np.random.choice(closest_cells,
divergence,
replace=False)
pre_cell_target.append(picked_cells)
for tar_c in picked_cells:
curr_syns = []
curr_netcons = []
curr_seg_pool = post_pop[tar_c].get_segs_by_name(target_segs)
chosen_seg = np.random.choice(curr_seg_pool)
for seg in chosen_seg:
curr_syn = h.tmgsyn(chosen_seg(0.5))
curr_syn.tau_1 = tau_1
curr_syn.tau_facil = tau_facil
curr_syn.U = U
curr_syn.e = e
curr_syn.tau_rec = tau_rec
curr_syns.append(curr_syn)
curr_netcon = h.NetCon(pre_pop[idx].soma(0.5)._ref_v,
curr_syn, thr, delay, weight,
sec=pre_pop[idx].soma)
curr_netcons.append(curr_netcon)
netcons.append(curr_netcons)
synapses.append(curr_syns)
self.netcons = netcons
self.pre_cell_targets = np.array(pre_cell_target)
self.synapses = synapses
class Exp2SynConnection(GenConnection):
"""
This class connects a pre and a post synaptic population with a Exp2Syn
synapse.
"""
def __init__(self, pre_pop, post_pop, target_pool, target_segs, divergence,
tau1, tau2, e, thr, delay, weight):
"""
divergence,
tau1, tau2, e, g_max, thr, delay, weight, name = "GC->MC"
"""
self.init_parameters = locals()
self.pre_pop = pre_pop
self.post_pop = post_pop
pre_pop.add_connection(self)
post_pop.add_connection(self)
pre_pop_rad = (np.arange(pre_pop.get_cell_number(), dtype=float) /
pre_pop.get_cell_number()) * (2*np.pi)
post_pop_rad = (np.arange(post_pop.get_cell_number(), dtype=float) /
post_pop.get_cell_number()) * (2*np.pi)
self.pre_pop_rad = pre_pop_rad
self.post_pop_rad = post_pop_rad
pre_pop_pos = pos(pre_pop_rad)
post_pop_pos = pos(post_pop_rad)
pre_cell_target = []
synapses = []
netcons = []
for idx, curr_cell_pos in enumerate(pre_pop_pos):
curr_dist = []
for post_cell_pos in post_pop_pos:
curr_dist.append(euclidian_dist(curr_cell_pos, post_cell_pos))
sort_idc = np.argsort(curr_dist)
closest_cells = sort_idc[0:target_pool]
picked_cells = np.random.choice(closest_cells,
divergence,
replace=False)
pre_cell_target.append(picked_cells)
for tar_c in picked_cells:
curr_syns = []
curr_netcons = []
curr_seg_pool = post_pop[tar_c].get_segs_by_name(target_segs)
chosen_seg = np.random.choice(curr_seg_pool)
for seg in chosen_seg:
curr_syn = h.Exp2Syn(chosen_seg(0.5))
curr_syn.tau1 = tau1
curr_syn.tau2 = tau2
curr_syn.e = e
curr_syns.append(curr_syn)
curr_netcon = h.NetCon(pre_pop[idx].soma(0.5)._ref_v,
curr_syn, thr, delay, weight,
sec=pre_pop[idx].soma)
curr_netcons.append(curr_netcon)
netcons.append(curr_netcons)
synapses.append(curr_syns)
self.netcons = netcons
self.pre_cell_targets = np.array(pre_cell_target)
self.synapses = synapses
class PerforantPathStimulation(object):
"""
This class connects a pre and a post synaptic population with a Exp2Syn
synapse.
"""
def __init__(self, stim, post_pop, n_targets, target_segs,
tau1, tau2, e, thr, delay, weight):
"""
divergence,
tau1, tau2, e, g_max, thr, delay, weight, name = "GC->MC"
"""
self.pre_pop = stim
self.post_pop = post_pop
post_pop.add_connection(self)
synapses = []
netcons = []
if type(n_targets) == int:
# Select n_targets from post_pop
target_cells = np.random.choice(post_pop.cells, n_targets,
replace=False)
else:
target_cells = post_pop.cells[n_targets]
for curr_cell in target_cells:
curr_seg_pool = curr_cell.get_segs_by_name(target_segs)
for seg in curr_seg_pool:
curr_syn = h.Exp2Syn(seg(0.5))
curr_syn.tau1 = tau1
curr_syn.tau2 = tau2
curr_syn.e = e
curr_netcon = h.NetCon(stim, curr_syn, thr, delay, weight)
netcons.append(curr_netcon)
synapses.append(curr_syn)
self.netcons = netcons
self.pre_cell_targets = np.array(target_cells)
self.synapses = synapses
class PerforantPathPoissonStimulation(object):
"""
Patterned Perforant Path stimulation as in Yim et al. 2015.
uses vecevent.mod -> h.VecStim
"""
def __init__(self, post_pop, t_pattern, spat_pattern, target_segs,
tau1, tau2, e, weight):
post_pop.add_connection(self)
synapses = []
netcons = []
conductances = []
target_cells = post_pop.cells[spat_pattern]
self.pre_pop = "Implicit"
self.vecstim = h.VecStim()
self.pattern_vec = h.Vector(t_pattern)
self.vecstim.play(self.pattern_vec)
for curr_cell in target_cells:
curr_seg_pool = curr_cell.get_segs_by_name(target_segs)
curr_conductances = []
for seg in curr_seg_pool:
curr_syn = h.Exp2Syn(seg(0.5))
curr_syn.tau1 = tau1
curr_syn.tau2 = tau2
curr_syn.e = e
curr_netcon = h.NetCon(self.vecstim, curr_syn)
curr_gvec = h.Vector()
curr_gvec.record(curr_syn._ref_g)
curr_conductances.append(curr_gvec)
curr_netcon.weight[0] = weight
netcons.append(curr_netcon)
synapses.append(curr_syn)
"""for event in pattern:
curr_netcon.event(event)"""
conductances.append(curr_conductances)
self.netcons = netcons
self.pre_cell_targets = np.array(target_cells)
self.synapses = synapses
self.conductances = conductances
class PerforantPathPoissonTmgsyn(GenConnection):
"""
Patterned Perforant Path simulation as in Yim et al. 2015.
uses vecevent.mod -> h.VecStim
"""
def __init__(self, post_pop, t_pattern, spat_pattern, target_segs,
tau_1, tau_facil, U, tau_rec, e, weight):
self.init_parameters = locals()
post_pop.add_connection(self)
synapses = []
netcons = []
t_pattern = list(t_pattern) # nrn does not like np.ndarrays?
target_cells = post_pop[spat_pattern]
self.pre_pop = 'Implicit'
self.post_pop = post_pop
self.vecstim = h.VecStim()
self.pattern_vec = h.Vector(t_pattern)
self.vecstim.play(self.pattern_vec)
conductances = []
for curr_cell in target_cells:
curr_seg_pool = curr_cell.get_segs_by_name(target_segs)
curr_conductances = []
for seg in curr_seg_pool:
curr_syn = h.tmgsyn(seg(0.5))
curr_syn.tau_1 = tau_1
curr_syn.tau_facil = tau_facil
curr_syn.U = U
curr_syn.tau_rec = tau_rec
curr_syn.e = e
curr_netcon = h.NetCon(self.vecstim, curr_syn)
curr_gvec = h.Vector()
curr_gvec.record(curr_syn._ref_g)
curr_conductances.append(curr_gvec)
curr_netcon.weight[0] = weight
netcons.append(curr_netcon)
synapses.append(curr_syn)
conductances.append(curr_conductances)
self.conductances = conductances
self.netcons = netcons
self.pre_cell_targets = np.array(spat_pattern)
self.synapses = synapses
"""Population ONLY REMAINS IN gennetwork TO KEEP pyDentate RUNNING. THE NEW
IMPLEMENTATION OF POPULATION IS IN genpopulation"""
class Population(object):
"""This is the model of a generic population.
A population is a number of cells of a specific type derived from
genneuron.GenNeuron. The GenPopulation object keeps track of all
incoming and outgoing connections. It is recommended to create Populations
through the GenNetwork.mk_population interface of a network the population
is part of.
Attributes
----------
parent_network - gennetwork.GenNetwork or derived instances
The network the population takes part in
cell_type - genneuron.GenNeuron class or subclass thereof
The cell type making up the population
cells - list of genneuron.GenNeuron instances
A list of cells that currently exist within the population
connections - list of Connection objects
A list of outgoing and incoming connections
Methods
-------
__init__
make_cells
get_cell_number
record_aps
plot_aps
write_aps
current_clamp_rnd
current_clamp_range
voltage_recording
add_connection
Use cases
---------
>>> nw = GenNetwork()
>>> nw.mk_population(GranuleCell, 500)
Create an empty network and create a population of 500 granule cells in the
network.
"""
def __init__(self, cell_type=None, n_cells=None, parent_network=None):
self.parent_network = parent_network
self.cell_type = cell_type
self.cells = []
self.connections = []
self.VClamps = []
self.VClamps_i = []
self.VRecords = []
if cell_type and n_cells:
self.make_cells(cell_type, n_cells)
self.i = 0
def SEClamp(self, cells, dur1=200, amp1=0, rs=0.001):
for x in cells:
clamp = self.cells[x]._SEClamp(dur1=dur1, amp1=amp1, rs=rs)
self.VClamps.append(clamp)
curr_vec = h.Vector()
curr_vec.record(clamp._ref_i)
self.VClamps_i.append(curr_vec)
def voltage_recording(self, cells):
for x in cells:
record = self.cells[x]._voltage_recording()
self.VRecords.append(record)
def make_cells(self, cell_type, n_cells):
"""Create cells of a certain type
Parameters
----------
cell_type - genneuron.GenNeuron class of subclass thereof
the type of the cells to be created
n_cells - numeric
number of cells to be created
Returns
-------
None
Use Cases
---------
>>> popul = Population(parent_network = nw)
>>> popul.make_cells(GranuleCell, 500)
Create an empty population within nw and then create 500 granule cells
"""
if hasattr(self, 'cell_type'):
if self.cell_type != cell_type:
raise TypeError("cell_type inconsistent with population")
else:
self.cell_type = cell_type
if not hasattr(self, 'cells'):
self.cells = []
for x in range(n_cells):
self.cells.append(cell_type())
self.cells = np.array(self.cells, dtype=object)
def get_cell_number(self):
"""Return the number of cells"""
return len(self.cells)
def record_aps(self):
counters = []
for cell in self.cells:
counters.append(cell._AP_counter())
self.ap_counters = counters
return counters
def plot_aps(self, color='k'):
cells = []
for x in self.ap_counters:
# as_numpy() doesn't work on windows 10 ???
try:
cells.append(x[0].as_numpy())
except:
cells.append(np.array(x[0]))
# Workaround for matplotlib bug. plt.eventplot throws error when first
# element empty
if not np.array(cells[0]).any():
cells[0] = np.array([0], dtype=float)
plt.eventplot(cells, linewidth=2, color=color)
def write_aps(self, directory='', fname=''):
if not fname:
time_tup = time.gmtime()
time_str = time.asctime(time_tup)
time_str = '_'.join(time_str.split(' '))
nw_name = self.parent_network.__class__.name
pop_name = self.cell_type.name
fname = nw_name + '_' + pop_name + '_' + time_str
fname = fname.replace(':', '-')
if not directory:
directory = os.getcwd()
if not os.path.isdir(directory):
os.mkdir(directory)
path = directory + '\\' + fname + '.npz'
try:
ap_list = [x[0].as_numpy() for x in self.ap_counters]
except:
ap_list = [np.array(x[0]) for x in self.ap_counters]
np.savez(path, *ap_list)
def perc_active_cells(self):
try:
# as_numpy doesn't work on windows 10 ???
timing_arrays = [x[0].as_numpy() for x in self.ap_counters]
except:
timing_arrays = [np.array(x[0]) for x in self.ap_counters]
active_counter = 0
for x in timing_arrays:
if x.size != 0:
active_counter = active_counter + 1
return (active_counter / float(self.get_cell_number())) * 100
def mk_current_clamp(self, cells, amp=0.3, dur=5, delays=3):
if not hasattr(cells, '__iter__'):
cells = np.random.choice(self.get_cell_number(), cells,
replace=False)
if not hasattr(delays, '__iter__'):
delays = np.array(delays)
for cell in cells:
for delay in delays:
self.cells[cell]._current_clamp_soma(amp=amp, dur=dur,
delay=delay)
def current_clamp_rnd(self, n_cells, amp=0.3, dur=5, delay=3):
"""DEPRECATE"""
chosen_cells = np.random.choice(self.cells, n_cells, replace=False)
for x in chosen_cells:
for y in delay:
x._current_clamp_soma(amp=amp, dur=dur, delay=y)
return chosen_cells
def current_clamp_range(self, n_cells, amp=0.3, dur=5, delay=3):
"""DEPRECATE"""
if type(n_cells) == int:
n_cells = range(n_cells)
for cell in n_cells:
self.cells[cell]._current_clamp_soma(amp=amp, dur=dur, delay=delay)
"""def voltage_recording(self, cell_type):
rnd_int = random.randint(0, len(self.cells) - 1)
soma_v_vec = self.cells[rnd_int]._voltage_recording()
return soma_v_vec"""
def add_connection(self, conn):
self.connections.append(conn)
def get_properties(self):
"""Get the properties of the network"""
try:
ap_time_stamps = [x[0].as_numpy() for x in self.ap_counters]
except:
ap_time_stamps = [np.array(x[0]) for x in self.ap_counters]
ap_numbers = [x[1].n for x in self.ap_counters]
try:
v_rec = [x.as_numpy() for x in self.VRecords]
vclamp_i = [x.as_numpy() for x in self.VClamps_i]
except:
v_rec = [np.array(x) for x in self.VRecords]
vclamp_i = [np.array(x) for x in self.VClamps_i]
properties = {'parent_network': str(self.parent_network),
'cell_type': self.cell_type.name,
'cell_number': self.get_cell_number(),
'connections': [conn.get_properties()
for conn in self.connections],
'ap_time_stamps': ap_time_stamps,
'ap_number': ap_numbers,
'v_records': v_rec,
'VClamps_i': vclamp_i}
properties
return properties
def __str__(self):
return self.cell_type.name + 'Population'
def __iter__(self):
return self
def __getitem__(self, item):
return self.cells[item]
def __next__(self):
if self.i < (len(self.cells)):
i = self.i
self.i += 1
return self.cells[i]
else:
self.i = 0
raise StopIteration()
def next(self):
return self.__next__()
# HELPERS
def pos(rad):
"""
(x,y) position of a point on a circle with axis origin at (0,0)
and radius 1.
x = cx + r * cos(rad) -> x = cos(rad)
y = cy + r * sin(rad) -> y = sin(rad)
Returns a list of tuples that give the point of each radian passed.
"""
x_arr = list(np.cos(rad))
y_arr = list(np.sin(rad))
return [(x_arr[idx], y_arr[idx]) for idx in range(len(x_arr))]
def euclidian_dist(p1, p2):
""" p1 and p2 must both be of len 2 where p1 = (x1,y1); p2 = (x2,y2)"""
return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)