-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcells.py
3623 lines (3124 loc) · 135 KB
/
cells.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
import collections, os, sys, traceback, copy, datetime, math, pprint
from collections import deque
import numpy as np
from dentate.neuron_utils import h, d_lambda, default_hoc_sec_lists, default_ordered_sec_types, freq, make_rec, \
load_cell_template, HocCellInterface, IzhiCellAttrs, default_izhi_cell_attrs_dict, PRconfig
from dentate.utils import get_module_logger, map, range, zip, zip_longest, viewitems, read_from_yaml, write_to_yaml, Promise
from neuroh5.io import read_cell_attribute_selection, read_graph_selection, read_tree_selection
# This logger will inherit its settings from the root logger, created in dentate.env
logger = get_module_logger(__name__)
##
## SNode2/STree2 structures from btmorph by B.T.Nielsen.
##
class P3D2(object):
"""
Basic container to represent and store 3D information
"""
def __init__(self, xyz, radius, type=7):
""" Constructor.
Parameters
-----------
xyz : numpy.array
3D location
radius : float
type : int
Type associated with the segment according to SWC standards
"""
self.xyz = xyz
self.radius = radius
self.type = type
def __str__(self):
return "P3D2 [%.2f %.2f %.2f], R=%.2f" % (self.xyz[0], self.xyz[1], self.xyz[2], self.radius)
class SNode2(object):
"""
Simple Node for use with a simple Tree (STree)
By design, the "content" should be a dictionary. (2013-03-08)
"""
def __init__(self, index):
"""
Constructor.
Parameters
-----------
index : int
Index, unique name of the :class:`SNode2`
"""
self.parent = None
self.index = index
self.children = []
self.content = {}
def get_parent(self):
"""
Return the parent node of this one.
Returns
-------
parent : :class:`SNode2`
In case of the root, None is returned.Otherwise a :class:`SNode2` is returned
"""
return self.__parent
def set_parent(self, parent):
"""
Set the parent node of a given other node
Parameters
----------
node : :class:`SNode2`
"""
self.__parent = parent
parent = property(get_parent, set_parent)
def get_index(self):
"""
Return the index of this node
Returns
-------
index : int
"""
return self.__index
def set_index(self, index):
"""
Set the unqiue name of a node
Parameters
----------
index : int
"""
self.__index = index
index = property(get_index, set_index)
def get_children(self):
"""
Return the children nodes of this one (if any)
Returns
-------
children : list :class:`SNode2`
In case of a leaf an empty list is returned
"""
return self.__children
def set_children(self, children):
"""
Set the children nodes of this one
Parameters
----------
children: list :class:`SNode2`
"""
self.__children = children
children = property(get_children, set_children)
def get_content(self):
"""
Return the content dict of a :class:`SNode2`
Returns
-------
parent : :class:`SNode2`
In case of the root, None is returned.Otherwise a :class:`SNode2` is returned
"""
return self.__content
def set_content(self, content):
"""
Set the content of a node. The content must be a dict
Parameters
----------
content : dict
dict with content. For use in btmorph at least a 'p3d' entry should be present
"""
if isinstance(content, dict):
self.__content = content
else:
raise Exception("SNode2.set_content must receive a dict")
content = property(get_content, set_content)
def add_child(self, child_node):
"""
add a child to the children list of a given node
Parameters
-----------
node : :class:`SNode2`
"""
self.children.append(child_node)
def make_empty(self):
"""
Clear the node. Unclear why I ever implemented this. Probably to cover up some failed garbage collection
"""
self.parent = None
self.content = {}
self.children = []
def remove_child(self, child):
"""
Remove a child node from the list of children of a specific node
Parameters
-----------
node : :class:`SNode2`
If the child doesn't exist, you get into problems.
"""
self.children.remove(child)
def __str__(self):
return 'SNode2 (ID: ' + str(self.index) + ')'
def __lt__(self, other):
if self.index < other.index:
return True
def __le__(self, other):
if self.index <= other.index:
return True
def __gt__(self, other):
if self.index > other.index:
return True
def __ge__(self, other):
if self.index >= other.index:
return True
def __copy__(self): # customization of copy.copy
ret = SNode2(self.index)
for child in self.children:
ret.add_child(child)
ret.content = self.content
ret.parent = self.parent
return ret
class STree2(object):
'''
Simple tree for use with a simple Node (:class:`SNode2`).
While the class is designed to contain binary trees (for neuronal morphologies) the number of children is not limited.
As such, this is a generic implementation of a tree structure as a linked list.
'''
def __init__(self):
"""
Default constructor. No arguments are passed.
"""
self.root = None
def __iter__(self):
nodes = []
self._gather_nodes(self.root, nodes)
for n in nodes:
yield n
def __getitem__(self, index):
return self._find_node(self.root, index)
def set_root(self, node):
"""
Set the root node of the tree
Parameters
-----------
node : :class:`SNode2`
to-be-root node
"""
if not node is None:
node.parent = None
self.__root = node
def get_root(self):
"""
Obtain the root node
Returns
-------
root : :class:`SNode2`
"""
return self.__root
root = property(get_root, set_root)
def is_root(self, node):
"""
Check whether a node is the root node
Returns
--------
is_root : boolean
True is the queried node is the root, False otherwise
"""
if node.parent is None:
return True
else:
return False
def is_leaf(self, node):
"""
Check whether a node is a leaf node, i.e., a node without children
Returns
--------
is_leaf : boolean
True is the queried node is a leaf, False otherwise
"""
if len(node.children) == 0:
return True
else:
return False
def add_node_with_parent(self, node, parent):
"""
Add a node to the tree under a specific parent node
Parameters
-----------
node : :class:`SNode2`
node to be added
parent : :class:`SNode2`
parent node of the newly added node
"""
node.parent = parent
if not parent is None:
parent.add_child(node)
def remove_node(self, node):
"""
Remove a node from the tree
Parameters
-----------
node : :class:`SNode2`
node to be removed
"""
node.parent.remove_child(node)
self._deep_remove(node)
def _deep_remove(self, node):
children = node.children
node.make_empty()
for child in children:
self._deep_remove(child)
def get_nodes(self):
"""
Obtain a list of all nodes int the tree
Returns
-------
all_nodes : list of :class:`SNode2`
"""
n = []
self._gather_nodes(self.root, n)
return n
def get_sub_tree(self, fake_root):
"""
Obtain the subtree starting from the given node
Parameters
-----------
fake_root : :class:`SNode2`
Node which becomes the new root of the subtree
Returns
-------
sub_tree : STree2
New tree with the node from the first argument as root node
"""
ret = STree2()
cp = fake_root.__copy__()
cp.parent = None
ret.root = cp
return ret
def _gather_nodes(self, node, node_list):
if not node is None:
node_list.append(node)
for child in node.children:
self._gather_nodes(child, node_list)
def get_node_with_index(self, index):
"""
Get a node with a specific name. The name is always an integer
Parameters
----------
index : int
Name of the node to be found
Returns
-------
node : :class:`SNode2`
Node with the specific index
"""
return self._find_node(self.root, index)
def get_node_in_subtree(self, index, fake_root):
"""
Get a node with a specific name in a the subtree rooted at fake_root. The name is always an integer
Parameters
----------
index : int
Name of the node to be found
fake_root: :class:`SNode2`
Root node of the subtree in which the node with a given index is searched for
Returns
-------
node : :class:`SNode2`
Node with the specific index
"""
return self._find_node(fake_root, index)
def _find_node(self, node, index):
"""
Sweet breadth-first/stack iteration to replace the recursive call.
Traverses the tree until it finds the node you are looking for.
Parameters
-----------
Returns
-------
node : :class:`SNode2`
when found and None when not found
"""
stack = [];
stack.append(node)
while (len(stack) != 0):
for child in stack:
if child.index == index:
return child
else:
stack.remove(child)
for cchild in child.children:
stack.append(cchild)
return None # Not found!
def degree_of_node(self, node):
"""
Get the degree of a given node. The degree is defined as the number of leaf nodes in the subtree rooted at this node.
Parameters
----------
node : :class:`SNode2`
Node of which the degree is to be computed.
Returns
-------
degree : int
"""
sub_tree = self.get_sub_tree(node)
st_nodes = sub_tree.get_nodes()
leafs = 0
for n in st_nodes:
if sub_tree.is_leaf(n):
leafs = leafs + 1
return leafs
def order_of_node(self, node):
"""
Get the order of a given node. The order or centrifugal order is defined as 0 for the root and increased with any bifurcation.
Hence, a node with 2 branch points on the shortest path between that node and the root has order 2.
Parameters
----------
node : :class:`SNode2`
Node of which the order is to be computed.
Returns
-------
order : int
"""
ptr = self.path_to_root(node)
order = 0
for n in ptr:
if len(n.children) > 1:
order = order + 1
# order is on [0,max_order] thus subtract 1 from this calculation
return order - 1
def path_to_root(self, node):
"""
Find and return the path between a node and the root.
Parameters
----------
node : :class:`SNode2`
Node at which the path starts
Returns
-------
path : list of :class:`SNode2`
list of :class:`SNode2` with the provided node and the root as first and last entry, respectively.
"""
n = []
self._go_up_from(node, n)
return n
def _go_up_from(self, node, n):
n.append(node)
if not node.parent is None:
self._go_up_from(node.parent, n)
def path_between_nodes(self, from_node, to_node):
"""
Find the path between two nodes. The from_node needs to be of higher \
order than the to_node. In case there is no path between the nodes, \
the path from the from_node to the soma is given.
Parameters
-----------
from_node : :class:`SNode2`
to_node : :class:`SNode2`
"""
n = []
self._go_up_from_until(from_node, to_node, n)
return n
def _go_up_from_until(self, from_node, to_node, n):
n.append(from_node)
if from_node == to_node:
return
if not from_node.parent is None:
self._go_up_from_until(from_node.parent, to_node, n)
def _make_soma_from_cylinders(self, soma_cylinders, all_nodes):
"""Now construct 3-point soma
step 1: calculate surface of all cylinders
step 2: make 3-point representation with the same surface"""
total_surf = 0
for (node, parent_index) in soma_cylinders:
n = node.content["p3d"]
p = all_nodes[parent_index][1].content["p3d"]
H = np.sqrt(np.sum((n.xyz - p.xyz) ** 2))
surf = 2 * np.pi * p.radius * H
total_surf = total_surf + surf
logger.error("found 'multiple cylinder soma' w/ total soma surface=*.3f" % total_surf)
# define apropriate radius
radius = np.sqrt(total_surf / (4 * np.pi))
s_node_1 = SNode2(2)
r = self.root.content["p3d"]
rp = r.xyz
s_p_1 = P3D2(np.array([rp[0], rp[1] - radius, rp[2]]), radius, 1)
s_node_1.content = {'p3d': s_p_1}
s_node_2 = SNode2(3)
s_p_2 = P3D2(np.array([rp[0], rp[1] + radius, rp[2]]), radius, 1)
s_node_2.content = {'p3d': s_p_2}
return s_node_1, s_node_2
def _determine_soma_type(self, file_n):
"""
Costly method to determine the soma type used in the SWC file.
This method searches the whole file for soma entries.
Parameters
----------
file_n : string
Name of the file containing the SWC description
Returns
-------
soma_type : int
Integer indicating one of the su[pported SWC soma formats.
1: Default three-point soma, 2: multiple cylinder description,
3: otherwise [not suported in btmorph]
"""
file = open(file_n, "r")
somas = 0
for line in file:
if not line.startswith('#'):
split = line.split()
index = int(split[0].rstrip())
s_type = int(split[1].rstrip())
if s_type == 1:
somas = somas + 1
file.close()
if somas == 3:
return 1
elif somas < 3:
return 3
else:
return 2
def __str__(self):
return "STree2 (" + str(len(self.get_nodes())) + " nodes)"
class PRneuron(object):
"""
An implementation of a Pinsky-Rinzel-type reduced biophysical neuron model for simulation in NEURON.
Conforms to the same API as BiophysCell.
"""
def __init__(self, gid, pop_name, template_name="PR_nrn", env=None, cell_config=None, mech_dict=None):
"""
:param gid: int
:param pop_name: str
:param env: :class:'Env'
:param cell_config: :namedtuple:'PRconfig'
"""
self._gid = gid
self._pop_name = pop_name
self.tree = STree2() # Builds a simple tree to store nodes of type 'SHocNode'
self.count = 0 # Keep track of number of nodes
if env is not None:
for sec_type in env.SWC_Types:
if sec_type not in default_ordered_sec_types:
raise AttributeError('Warning! unexpected SWC Type definitions found in Env')
self.nodes = {key: [] for key in default_ordered_sec_types}
self.mech_file_path = None
self.init_mech_dict = dict(mech_dict) if mech_dict is not None else None
self.mech_dict = dict(mech_dict) if mech_dict is not None else None
self.random = np.random.RandomState()
self.random.seed(self.gid)
self.spike_detector = None
self.spike_onset_delay = 0.
self.is_reduced = True
if not isinstance(cell_config, PRconfig):
raise RuntimeError('PRneuron: argument cell_attrs must be of type PRconfig')
param_dict = { k: v for k,v in
(('pp', cell_config.pp),
('Ltotal', cell_config.Ltotal),
('gc', cell_config.gc),
('soma_gmax_Na', cell_config.soma_gmax_Na),
('soma_gmax_K', cell_config.soma_gmax_K),
('soma_g_pas', cell_config.soma_g_pas),
('dend_gmax_Ca', cell_config.dend_gmax_Ca),
('dend_gmax_KCa', cell_config.dend_gmax_KCa),
('dend_gmax_KAHP', cell_config.dend_gmax_KAHP),
('dend_gmax_HCN', cell_config.dend_gmax_HCN),
('dend_aqs_KAHP', cell_config.dend_aqs_KAHP),
('dend_bq_KAHP', cell_config.dend_bq_KAHP),
('dend_g_pas', cell_config.dend_g_pas),
('dend_d_Caconc', cell_config.dend_d_Caconc),
('dend_beta_Caconc', cell_config.dend_beta_Caconc),
('cm_ratio', cell_config.cm_ratio),
('global_cm', cell_config.global_cm),
('global_diam', cell_config.global_diam),
('e_pas', cell_config.e_pas),
) if v is not None }
template_class = getattr(h, template_name)
PR_nrn = template_class(param_dict)
PR_nrn.soma.ic_constant = cell_config.ic_constant
self.hoc_cell = PR_nrn
soma_node = append_section(self, 'soma', sec_index=0, sec=PR_nrn.soma)
apical_node = append_section(self, 'apical', sec_index=1, sec=PR_nrn.dend)
connect_nodes(self.soma[0], self.apical[0], connect_hoc_sections=False)
init_spike_detector(self, self.tree.root, loc=0.5, threshold=cell_config.V_threshold)
def update_cell_attrs(self, **kwargs):
for attr_name, attr_val in kwargs.items():
if attr_name in PRconfig._fields:
setattr(self.hoc_cell, attr_name, attr_val)
@property
def gid(self):
return self._gid
@property
def pop_name(self):
return self._pop_name
@property
def soma(self):
return self.nodes['soma']
@property
def axon(self):
return self.nodes['axon']
@property
def basal(self):
return self.nodes['basal']
@property
def apical(self):
return self.nodes['apical']
@property
def trunk(self):
return self.nodes['trunk']
@property
def tuft(self):
return self.nodes['tuft']
@property
def spine(self):
return self.nodes['spine_head']
@property
def spine_head(self):
return self.nodes['spine_head']
@property
def spine_neck(self):
return self.nodes['spine_neck']
@property
def ais(self):
return self.nodes['ais']
@property
def hillock(self):
return self.nodes['hillock']
class IzhiCell(object):
"""
An implementation of an Izhikevich adaptive integrate-and-fire-type cell model for simulation in NEURON.
Conforms to the same API as BiophysCell.
"""
def __init__(self, gid, pop_name, env=None, cell_type='RS', cell_attrs=None, mech_dict=None):
"""
:param gid: int
:param pop_name: str
:param env: :class:'Env'
:param cell_type: str
:param cell_attrs: :namedtuple:'IzhiCellAttrs'
"""
self._gid = gid
self._pop_name = pop_name
self.tree = STree2() # Builds a simple tree to store nodes of type 'SHocNode'
self.count = 0 # Keep track of number of nodes
if env is not None:
for sec_type in env.SWC_Types:
if sec_type not in default_ordered_sec_types:
raise AttributeError('Warning! unexpected SWC Type definitions found in Env')
self.nodes = {key: [] for key in default_ordered_sec_types}
self.mech_file_path = None
self.init_mech_dict = dict(mech_dict) if mech_dict is not None else None
self.mech_dict = dict(mech_dict) if mech_dict is not None else None
self.random = np.random.RandomState()
self.random.seed(self.gid)
self.spike_detector = None
self.spike_onset_delay = 0.
self.is_reduced = True
if cell_attrs is not None:
if not isinstance(cell_attrs, IzhiCellAttrs):
raise RuntimeError('IzhiCell: argument cell_attrs must be of type IzhiCellAttrs')
cell_type = 'custom'
elif cell_type not in default_izhi_cell_attrs_dict:
raise RuntimeError('IzhiCell: unknown value for cell_type: %s' % str(cell_type))
else:
cell_attrs = default_izhi_cell_attrs_dict[cell_type]
self.cell_type = cell_type
append_section(self, 'soma')
sec = self.tree.root.sec
sec.L, sec.diam = 10., 10.
self.izh = h.Izhi2019(.5, sec=sec)
self.base_cm = 31.831 # Produces membrane time constant of 8 ms for a RS cell with izh.C = 1. and izi.k = 0.7
for attr_name, attr_val in cell_attrs._asdict().items():
setattr(self.izh, attr_name, attr_val)
sec.cm = self.base_cm * self.izh.C
self.hoc_cell = HocCellInterface(sections=[sec], is_art=lambda: 0, is_reduced=True,
all=[sec], soma=[sec], apical=[], basal=[],
axon=[], ais=[], hillock=[], state=[self.izh])
init_spike_detector(self, self.tree.root, loc=0.5, threshold=self.izh.vpeak - 1.)
def update_cell_attrs(self, **kwargs):
for attr_name, attr_val in kwargs.items():
if attr_name in IzhiCellAttrs._fields:
setattr(self.izh, attr_name, attr_val)
if attr_name == 'C':
self.tree.root.sec.cm = self.base_cm * attr_val
elif attr_name == 'vpeak':
self.spike_detector.threshold = attr_val - 1.
@property
def gid(self):
return self._gid
@property
def pop_name(self):
return self._pop_name
@property
def soma(self):
return self.nodes['soma']
@property
def axon(self):
return self.nodes['axon']
@property
def basal(self):
return self.nodes['basal']
@property
def apical(self):
return self.nodes['apical']
@property
def trunk(self):
return self.nodes['trunk']
@property
def tuft(self):
return self.nodes['tuft']
@property
def spine(self):
return self.nodes['spine_head']
@property
def spine_head(self):
return self.nodes['spine_head']
@property
def spine_neck(self):
return self.nodes['spine_neck']
@property
def ais(self):
return self.nodes['ais']
@property
def hillock(self):
return self.nodes['hillock']
class SCneuron(object):
"""
Single-compartment biophysical neuron model for simulation in NEURON.
Conforms to the same API as BiophysCell.
"""
def __init__(
self,
gid,
pop_name,
env,
mech_dict = None,
mech_file_path = None,
):
"""
:param gid: int
:param pop_name: str
:param env: :class:'Env'
:param mech_dict: dict
"""
self._gid = gid
self._pop_name = pop_name
self.tree = STree2() # Builds a simple tree to store nodes of type 'SHocNode'
self.count = 0 # Keep track of number of nodes
if env is not None:
for sec_type in env.SWC_Types:
if sec_type not in default_ordered_sec_types:
raise AttributeError(
"Warning! unexpected SWC Type definitions found in Env"
)
self.nodes = {key: [] for key in default_ordered_sec_types}
self.mech_file_path = mech_file_path
self.init_mech_dict = dict(mech_dict) if mech_dict is not None else None
self.mech_dict = dict(mech_dict) if mech_dict is not None else None
if (mech_dict is None) and (mech_file_path is not None):
import_mech_dict_from_file(self, self.mech_file_path)
elif mech_dict is None:
# Allows for a cell to be created and for a new mech_dict to be constructed programmatically from scratch
self.init_mech_dict = dict()
self.mech_dict = dict()
self.random = np.random.RandomState()
self.random.seed(self.gid)
self.spike_detector = None
self.spike_onset_delay = 0.0
self.is_reduced = True
SC_nrn = h.SC_nrn()
self.hoc_cell = SC_nrn
soma_node = append_section(self, 'soma', sec_index=0, sec=SC_nrn.soma)
init_cable(self)
init_spike_detector(self, node=soma_node)
@property
def gid(self) -> int:
return self._gid
@property
def pop_name(self):
return self._pop_name
@property
def soma(self):
return self.nodes["soma"]
@property
def axon(self):
return self.nodes["axon"]
@property
def basal(self):
return self.nodes["basal"]
@property
def apical(self):
return self.nodes["apical"]
@property
def trunk(self):
return self.nodes["trunk"]
@property
def tuft(self):
return self.nodes["tuft"]
@property
def spine(self):
return self.nodes["spine_head"]
@property
def spine_head(self):
return self.nodes["spine_head"]
@property
def spine_neck(self):
return self.nodes["spine_neck"]
@property
def ais(self):
return self.nodes["ais"]
@property
def hillock(self):
return self.nodes["hillock"]
class BiophysCell(object):
"""
A Python wrapper for neuronal cell objects specified in the NEURON language hoc.
Extends STree to provide an tree interface to facilitate:
1) Iteration through connected neuronal compartments, and
2) Specification of complex distributions of compartment attributes like gradients of ion channel density or
synaptic properties.
"""
def __init__(self, gid, pop_name, hoc_cell=None, neurotree_dict=None, mech_file_path=None, mech_dict=None, env=None):
"""
:param gid: int
:param pop_name: str
:param hoc_cell: :class:'h.hocObject': instance of a NEURON cell template
:param mech_file_path: str (path)
:param env: :class:'Env'
"""
self._gid = gid
self._pop_name = pop_name
self.tree = STree2() # Builds a simple tree to store nodes of type 'SHocNode'
self.count = 0 # Keep track of number of nodes
if env is not None:
for sec_type in env.SWC_Types:
if sec_type not in default_ordered_sec_types:
raise AttributeError('Unexpected SWC Type definitions found in Env')
self.nodes = {key: [] for key in default_ordered_sec_types}
self.mech_file_path = mech_file_path
self.init_mech_dict = dict(mech_dict) if mech_dict is not None else None
self.mech_dict = dict(mech_dict) if mech_dict is not None else None
self.random = np.random.RandomState()
self.random.seed(self.gid)
self.spike_detector = None
self.spike_onset_delay = 0.
self.hoc_cell = hoc_cell
if hoc_cell is not None:
import_morphology_from_hoc(self, hoc_cell)
elif neurotree_dict is not None:
import_morphology_from_neurotree_dict(self, neurotree_dict)
import_morphology_from_hoc(self, hoc_cell)
if (mech_dict is None) and (mech_file_path is not None):
import_mech_dict_from_file(self, self.mech_file_path)
elif mech_dict is None:
# Allows for a cell to be created and for a new mech_dict to be constructed programmatically from scratch
self.init_mech_dict = dict()
self.mech_dict = dict()
init_cable(self)
init_spike_detector(self)
@property
def gid(self):
return self._gid
@property
def pop_name(self):
return self._pop_name
@property
def soma(self):
return self.nodes['soma']
@property
def axon(self):
return self.nodes['axon']
@property