-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneuron_utils.py
294 lines (259 loc) · 10.7 KB
/
neuron_utils.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
import os, os.path, math
import numpy as np
try:
from mpi4py import MPI # Must come before importing NEURON
except Exception:
pass
from dentate.utils import get_module_logger
from neuron import h
from scipy import interpolate
# This logger will inherit its settings from the root logger, created in ca1.env
logger = get_module_logger(__name__)
freq = 100 # Hz, frequency at which AC length constant will be computed
d_lambda = 0.1 # no segment will be longer than this fraction of the AC length constant
default_ordered_sec_types = ['soma', 'hillock', 'ais', 'axon', 'basal', 'trunk', 'apical', 'tuft', 'spine']
default_hoc_sec_lists = {'soma': 'somaidx', 'hillock': 'hilidx', 'ais': 'aisidx', 'axon': 'axonidx',
'basal': 'basalidx', 'apical': 'apicalidx', 'trunk': 'trunkidx', 'tuft': 'tuftidx'}
def lambda_f(sec, f=freq):
"""
Calculates the AC length constant for the given section at the frequency f
Used to determine the number of segments per hoc section to achieve the desired spatial and temporal resolution
:param sec: :class:'h.Section'
:param f: int
:return: int
"""
diam = np.mean([seg.diam for seg in sec])
Ra = sec.Ra
cm = np.mean([seg.cm for seg in sec])
return 1e5 * math.sqrt(diam / (4. * math.pi * f * Ra * cm))
def d_lambda_nseg(sec, lam=d_lambda, f=freq):
"""
The AC length constant for this section and the user-defined fraction is used to determine the maximum size of each
segment to achieve the desired spatial and temporal resolution. This method returns the number of segments to set
the nseg parameter for this section. For tapered cylindrical sections, the diam parameter will need to be
reinitialized after nseg changes.
:param sec : :class:'h.Section'
:param lam : int
:param f : int
:return : int
"""
L = sec.L
return int(((L / (lam * lambda_f(sec, f))) + 0.9) / 2) * 2 + 1
def reinit_diam(sec, diam_bounds):
"""
For a node associated with a hoc section that is a tapered cylinder, every time the spatial resolution
of the section (nseg) is changed, the section diameters must be reinitialized. This method checks the
node's content dictionary for diameter boundaries and recalibrates the hoc section associated with this node.
"""
if diam_bounds is not None:
diam1, diam2 = diam_bounds
h(f'diam(0:1)={diam1}:{diam2}', sec=sec)
def init_nseg(sec, spatial_res=0, verbose=True):
"""
Initializes the number of segments in this section (nseg) based on the AC length constant. Must be re-initialized
whenever basic cable properties Ra or cm are changed. The spatial resolution parameter increases the number of
segments per section by a factor of an exponent of 3.
:param sec: :class:'h.Section'
:param spatial_res: int
:param verbose: bool
"""
sugg_nseg = d_lambda_nseg(sec)
sugg_nseg *= 3 ** spatial_res
if verbose:
logger.info(f'init_nseg: changed {sec.hname()}.nseg {sec.nseg} --> {sugg_nseg}')
sec.nseg = int(sugg_nseg)
def load_cell_template(env, pop_name, bcast_template=False):
"""
:param pop_name: str
"""
if pop_name in env.template_dict:
return env.template_dict[pop_name]
rank = env.comm.Get_rank()
if not (pop_name in env.celltypes):
raise KeyError('load_cell_templates: unrecognized cell population: %s' % pop_name)
template_name = env.celltypes[pop_name]['template']
if 'template file' in env.celltypes[pop_name]:
template_file = env.celltypes[pop_name]['template file']
else:
template_file = None
if not hasattr(h, template_name):
find_template(env, template_name, template_file=template_file, path=env.template_paths, bcast_template=bcast_template)
assert (hasattr(h, template_name))
template_class = getattr(h, template_name)
env.template_dict[pop_name] = template_class
return template_class
def find_template(env, template_name, path=['templates'], template_file=None, bcast_template=False, root=0):
"""
Finds and loads a template located in a directory within the given path list.
:param env: :class:'Env'
:param template_name: str; name of hoc template
:param path: list of str; directories to look for hoc template
:param template_file: str; file_name containing definition of hoc template
:param root: int; MPI.COMM_WORLD.rank
"""
if env.comm is None:
bcast_template = False
rank = env.comm.rank if env.comm is not None else 0
found = False
template_path = ''
if template_file is None:
template_file = '%s.hoc' % template_name
if bcast_template:
env.comm.barrier()
if (env.comm is None) or (not bcast_template) or (bcast_template and (rank == root)):
for template_dir in path:
if template_file is None:
template_path = '%s/%s.hoc' % (template_dir, template_name)
else:
template_path = '%s/%s' % (template_dir, template_file)
found = os.path.isfile(template_path)
if found and (rank == 0):
logger.info('Loaded %s from %s' % (template_name, template_path))
break
if bcast_template:
found = env.comm.bcast(found, root=root)
env.comm.barrier()
if found:
if bcast_template:
template_path = env.comm.bcast(template_path, root=root)
env.comm.barrier()
h.load_file(template_path)
else:
raise Exception('find_template: template %s not found: file %s; path is %s' %
(template_name, template_file, str(path)))
def configure_hoc_env(env, bcast_template=False):
"""
:param env: :class:'Env'
"""
h.load_file("stdrun.hoc")
h.load_file("loadbal.hoc")
for template_dir in env.template_paths:
path = "%s/rn.hoc" % template_dir
if os.path.exists(path):
h.load_file(path)
h.cvode.use_fast_imem(1)
h.cvode.cache_efficient(1)
h('objref pc, nc, nil')
h('strdef dataset_path')
if hasattr(env, 'dataset_path'):
h.dataset_path = env.dataset_path if env.dataset_path is not None else ""
if env.use_coreneuron:
from neuron import coreneuron
coreneuron.enable = True
coreneuron.verbose = 1 if env.verbose else 0
h.pc = h.ParallelContext()
h.pc.gid_clear()
env.pc = h.pc
h.dt = env.dt
h.tstop = env.tstop
env.t_vec = h.Vector() # Spike time of all cells on this host
env.id_vec = h.Vector() # Ids of spike times on this host
env.t_rec = h.Vector() # Timestamps of intracellular traces on this host
if 'celsius' in env.globals:
h.celsius = env.globals['celsius']
## more accurate integration of synaptic discontinuities
if hasattr(h, 'nrn_netrec_state_adjust'):
h.nrn_netrec_state_adjust = 1
## sparse parallel transfer
if hasattr(h, 'nrn_sparse_partrans'):
h.nrn_sparse_partrans = 1
def interplocs(sec):
"""Computes interpolants for xyz coords of locations in a section whose topology & geometry are defined by pt3d data.
Based on code by Ted Carnevale.
"""
nn = sec.n3d()
xx = h.Vector(nn)
yy = h.Vector(nn)
zz = h.Vector(nn)
dd = h.Vector(nn)
ll = h.Vector(nn)
for ii in range(0, nn):
xx.x[ii] = sec.x3d(ii)
yy.x[ii] = sec.y3d(ii)
zz.x[ii] = sec.z3d(ii)
dd.x[ii] = sec.diam
ll.x[ii] = sec.arc3d(ii)
## normalize length
ll.div(ll.x[nn - 1])
xx = np.array(xx)
yy = np.array(yy)
zz = np.array(zz)
dd = np.array(dd)
ll = np.array(ll)
u, indices = np.unique(ll, return_index=True)
indices = np.asarray(indices)
if len(u) < len(ll):
ll = ll[indices]
xx = xx[indices]
yy = yy[indices]
zz = zz[indices]
dd = dd[indices]
pch_x = interpolate.pchip(ll, xx)
pch_y = interpolate.pchip(ll, yy)
pch_z = interpolate.pchip(ll, zz)
pch_diam = interpolate.pchip(ll, dd)
return pch_x, pch_y, pch_z, pch_diam
def make_rec(recid, population, gid, cell, sec=None, loc=None, ps=None, param='v', label=None, dt=None, description=''):
"""
Makes a recording vector for the specified quantity in the specified section and location.
:param recid: str
:param population: str
:param gid: integer
:param cell: :class:'BiophysCell'
:param sec: :class:'HocObject'
:param loc: float
:param ps: :class:'HocObject'
:param param: str
:param dt: float
:param ylabel: str
:param description: str
"""
vec = h.Vector()
if (sec is None) and (loc is None) and (ps is not None):
hocobj = ps
seg = ps.get_segment()
if seg is not None:
loc = seg.x
sec = seg.sec
origin = list(cell.soma)[0]
distance = h.distance(origin(0.5), seg)
ri = h.ri(loc, sec=sec)
else:
distance = None
ri = None
elif (sec is not None) and (loc is not None):
hocobj = sec(loc)
if cell.soma.__class__.__name__.lower() == "section":
origin = cell.soma
else:
origin = list(cell.soma)[0]
h.distance(sec=origin)
distance = h.distance(loc, sec=sec)
ri = h.ri(loc, sec=sec)
else:
raise RuntimeError('make_rec: either sec and loc or ps must be specified')
section_index = None
if sec is not None:
for i, this_section in enumerate(cell.sections):
if this_section == sec:
section_index = i
break
if label is None:
label = param
if dt is None:
vec.record(getattr(hocobj, f'_ref_{param}'))
else:
vec.record(getattr(hocobj, f'_ref_{param}'), dt)
rec_dict = {'name': recid,
'gid': gid,
'cell': cell,
'population': population,
'loc': loc,
'section': section_index,
'distance': distance,
'ri': ri,
'description': description,
'vec': vec,
'label': label
}
return rec_dict