-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathg_scale.py
More file actions
341 lines (323 loc) · 16.4 KB
/
g_scale.py
File metadata and controls
341 lines (323 loc) · 16.4 KB
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
"""g_scale.py
g_scale(mech_str, scale, g='gbar') will scale the mech in mech_str where
ever it is present in the model by the "scale" as a multiplicative
factor of gbar_mech or a supplied conductance prefix g.
g_all(mech_str, set, g='gbar') will set the mech in mech_str where
ever it is present in the model by the "set" as gbar_mech or a
supplied conductance prefix g instead of gbar.
g_region(region, mech_str, set, g='gbar') will set the mech in mech_str in
the 'axon', 'soma', or 'dendrite' region of the model's gbar_mech or a
supplied conductance prefix g instead of gbar. The region is detected
on the first letter of the passed region string so 'a', 's', or 'd'
will work as well.
"""
from neuron import h
from run_simulation_python3 import *
def g_scale(mech_str, scale, g='gbar'):
"""g_scale(mech_str, scale, g='gbar') will scale the mech in mech_str where
ever it is present in the model by the "scale" as a multiplicative
factor of gbar_mech or a supplied conductance prefix g."""
for sec in h.allsec():
if h.ismembrane(mech_str, sec=sec):
# there are two formats: for accessing sections that are spines
# and another for accessing sections that are not spines:
if 'spine' in str(sec):
# below returns the 1000 in Spine[1000].spine
spine_number_string=str(sec).split('[')[1].split(']')[0]
cmd = \
f"spines[{spine_number_string}]" \
+ f".{g}_{mech_str} *= {scale}"
exec(cmd)
else:
for seg in sec: # assume that the gmax_mech is a range
# variable in case it is distance dependent
exec(f"h.{seg}.{g}_{mech_str} *= {scale}")
def g_all(mech_str, set_value, g='gbar'):
"""g_all(mech_str, set_value, g='gbar') will set the mech in mech_str where
ever it is present in the model by the "set_value" as gbar_mech or a
supplied conductance prefix g instead of gbar.
"""
if mech_str=='pass' and g=='gbar':
g='g' # if I forgot to set g for pass then correct that
for sec in h.allsec():
if h.ismembrane(mech_str, sec=sec):
# there are two formats: for accessing sections that are spines
# and another for accessing sections that are not spines:
if 'spine' in str(sec):
# below returns the 1000 in Spine[1000].spine
spine_number_string=str(sec).split('[')[1].split(']')[0]
cmd = \
f"spines[{spine_number_string}]" \
+ f".{g}_{mech_str} = {set_value}"
exec(cmd)
else:
for seg in sec: # assume that the gmax_mech is a range
# variable in case it is distance dependent
exec(f"h.{seg}.{g}_{mech_str} = {set_value}")
g_set=g_all # sometimes I type g_set when I mean g_all
def g_region(region, mech_str, set_value, g='gbar'):
"""g_region(region, mech_str, set_value, g='gbar') will set the mech in
mech_str in the model region 'a', 's', 'd' for axon soma or dendrite
to the "set_value" as gbar_mech or a supplied conductance prefix g
instead of gbar.
"""
if mech_str=='pass' and g=='gbar':
g='g' # if I forgot to set g for pass then correct that
axon_regions = ['axon'] # matches for axon compartments
soma_regions = ['soma', 'dend_0'] # matches for soma compartments
dend_regions = ['dend_5', 'dend_7', 'spine', 'apic'] # matches for dend
# compartments
matching_regions = 'unassigned'
if region[0] == 'a':
matching_regions = axon_regions
if region[0] == 's':
matching_regions = soma_regions
if region[0] == 'd':
matching_regions = dend_regions
if matching_regions == 'unassigned':
print("invalid region passed to gset(region, mech_str, set_value, g='gbar')")
print("please use 'a' axon, 's' soma, or 'd' dendrite for region")
return
for sec in h.allsec():
sec_in_region = False
for region_sec in matching_regions:
if region_sec in str(sec):
sec_in_region = True
if sec_in_region:
if h.ismembrane(mech_str, sec=sec):
# there are two formats: for accessing sections that are spines
# and another for accessing sections that are not spines:
if 'spine' in str(sec):
# below returns the 1000 in Spine[1000].spine
spine_number_string=str(sec).split('[')[1].split(']')[0]
cmd = \
f"spines[{spine_number_string}]" \
+ f".{g}_{mech_str} = {set_value}"
exec(cmd)
else:
for seg in sec: # assume that the gmax_mech is a range
# variable in case it is distance dependent
exec(f"h.{seg}.{g}_{mech_str} = {set_value}")
def g_linear_decay(region, mech_str, set_value_proximal, set_value_distal, g='gbar'):
"""g_linear_decay(region, mech_str, set_value_proximal,
set_value_distal, g='gbar') will set the mech in mech_str in the model
region 'a', 's', 'd' for axon soma or dendrite to interpolated value
starting at set_value_proximal and changing to set_value_distal at the
farthest reaches of the cell as gbar_mech or a supplied conductance
prefix g instead of gbar.
"""
m=0 # find the farthest distance in the cell to run interpolation
# from proximal to distal with
for sec in h.allsec():
if h.distance(sec(0.5), h.soma(0.5))>m:
m=h.distance(sec(0.5), h.soma(0.5))
if mech_str=='pass' and g=='gbar':
g='g' # if I forgot to set g for pass then correct that
axon_regions = ['axon'] # matches for axon compartments
soma_regions = ['soma', 'dend_0'] # matches for soma compartments
dend_regions = ['dend_5', 'dend_7', 'spine', 'apic'] # matches for dend
# compartments
matching_regions = 'unassigned'
if region[0] == 'a':
matching_regions = axon_regions
if region[0] == 's':
matching_regions = soma_regions
if region[0] == 'd':
matching_regions = dend_regions
if matching_regions == 'unassigned':
print("invalid region passed to gset(region, mech_str, set_value, g='gbar')")
print("please use 'a' axon, 's' soma, or 'd' dendrite for region")
return
for sec in h.allsec():
sec_in_region = False
for region_sec in matching_regions:
if region_sec in str(sec):
sec_in_region = True
if sec_in_region:
if h.ismembrane(mech_str, sec=sec):
# there are two formats: for accessing sections that are spines
# and another for accessing sections that are not spines:
if 'spine' in str(sec):
# below returns the 1000 in Spine[1000].spine
spine_number_string=str(sec).split('[')[1].split(']')[0]
current_distance = h.distance(sec(0.5), h.soma(0.5))
scaled_value = set_value_distal*(current_distance/m) + \
set_value_proximal*((m-current_distance)/m)
cmd = \
f"spines[{spine_number_string}]" \
+ f".{g}_{mech_str} = {scaled_value}"
exec(cmd)
else:
for seg in sec: # assume that the gmax_mech is a range
# variable in case it is distance dependent
current_distance = h.distance(seg, h.soma(0.5))
scaled_value = set_value_distal*(current_distance/m) + \
set_value_proximal*((m-current_distance)/m)
exec(f"h.{seg}.{g}_{mech_str} = {scaled_value}")
def g_exp_decay(region, mech_str, set_value_proximal, space_constant, g='gbar'):
"""g_exp_decay(region, mech_str, set_value_proximal, space_constant,
g='gbar') will set the mech in mech_str in the model region 'a', 's',
'd' for axon soma or dendrite to set_value_proximal *
exp(-distance/space_constant) as gbar_mech or a supplied conductance
prefix g instead of gbar.
"""
m=0 # find the farthest distance in the cell to run interpolation
# from proximal to distal with
for sec in h.allsec():
if h.distance(sec(0.5), h.soma(0.5))>m:
m=h.distance(sec(0.5), h.soma(0.5))
if mech_str=='pass' and g=='gbar':
g='g' # if I forgot to set g for pass then correct that
axon_regions = ['axon'] # matches for axon compartments
soma_regions = ['soma', 'dend_0'] # matches for soma compartments
dend_regions = ['dend_5', 'dend_7', 'spine', 'apic'] # matches for dend
# compartments
matching_regions = 'unassigned'
if region[0] == 'a':
matching_regions = axon_regions
if region[0] == 's':
matching_regions = soma_regions
if region[0] == 'd':
matching_regions = dend_regions
if matching_regions == 'unassigned':
print("invalid region passed to gset(region, mech_str, set_value, g='gbar')")
print("please use 'a' axon, 's' soma, or 'd' dendrite for region")
return
for sec in h.allsec():
sec_in_region = False
for region_sec in matching_regions:
if region_sec in str(sec):
sec_in_region = True
if sec_in_region:
if h.ismembrane(mech_str, sec=sec):
# there are two formats: for accessing sections that are spines
# and another for accessing sections that are not spines:
if 'spine' in str(sec):
# below returns the 1000 in Spine[1000].spine
spine_number_string=str(sec).split('[')[1].split(']')[0]
current_distance = h.distance(sec(0.5), h.soma(0.5))
scaled_value = set_value_proximal \
* np.exp(-current_distance/space_constant)
cmd = \
f"spines[{spine_number_string}]" \
+ f".{g}_{mech_str} = {scaled_value}"
exec(cmd)
else:
for seg in sec: # assume that the gmax_mech is a range
# variable in case it is distance dependent
current_distance = h.distance(seg, h.soma(0.5))
scaled_value = set_value_proximal \
* np.exp(-current_distance/space_constant)
exec(f"h.{seg}.{g}_{mech_str} = {scaled_value}")
def g_exp_reach(region, mech_str, proximal_value, distal_value, space_constant, g='gbar'):
"""g_exp_reach(region, mech_str, proximal_value, space_constant,
g='gbar') will set the mech in mech_str in the model region 'a', 's',
'd' for axon soma or dendrite to gbar_mech = proximal_value + C2 *
(1-exp(-distance/space_constant)) (or a supplied conductance prefix g
instead of gbar).
"""
m=0 # find the farthest distance in the cell to run interpolation
# from proximal to distal with
for sec in h.allsec():
if h.distance(sec(0.5), h.soma(0.5))>m:
m=h.distance(sec(0.5), h.soma(0.5))
if mech_str=='pass' and g=='gbar':
g='g' # if I forgot to set g for pass then correct that
axon_regions = ['axon'] # matches for axon compartments
soma_regions = ['soma', 'dend_0'] # matches for soma compartments
dend_regions = ['dend_5', 'dend_7', 'spine', 'apic'] # matches for dend
# compartments
matching_regions = 'unassigned'
if region[0] == 'a':
matching_regions = axon_regions
if region[0] == 's':
matching_regions = soma_regions
if region[0] == 'd':
matching_regions = dend_regions
if matching_regions == 'unassigned':
print("invalid region passed to gset(region, mech_str, set_value, g='gbar')")
print("please use 'a' axon, 's' soma, or 'd' dendrite for region")
return
#
C2=(distal_value - proximal_value)/(1-np.exp(-m/space_constant))
for sec in h.allsec():
sec_in_region = False
for region_sec in matching_regions:
if region_sec in str(sec):
sec_in_region = True
if sec_in_region:
if h.ismembrane(mech_str, sec=sec):
# there are two formats: for accessing sections that are spines
# and another for accessing sections that are not spines:
if 'spine' in str(sec):
# below returns the 1000 in Spine[1000].spine
spine_number_string=str(sec).split('[')[1].split(']')[0]
current_distance = h.distance(sec(0.5), h.soma(0.5))
scaled_value = proximal_value + C2 * \
(1-np.exp(-current_distance/space_constant))
cmd = \
f"spines[{spine_number_string}]" \
+ f".{g}_{mech_str} = {scaled_value}"
exec(cmd)
else:
for seg in sec: # assume that the gmax_mech is a range
# variable in case it is distance dependent
current_distance = h.distance(seg, h.soma(0.5))
scaled_value = proximal_value + C2 * \
(1-np.exp(-current_distance/space_constant))
exec(f"h.{seg}.{g}_{mech_str} = {scaled_value}")
def g_piecewise_linear(region, mech_str, start_distance, start_value, \
end_distance, end_value, g='gbar'):
"""g_piecewise_linear(region, mech_str, start_distance, start_value,
end_distance, end_value, g='gbar') will set the gbar_mech (or a
supplied conductance prefix g instead of gbar) the model region 'a',
's', 'd' for axon soma or dendrite to interpolated value starting at
start_value at start_distance and changing to end_value at the
end_distance.
"""
if mech_str=='pass' and g=='gbar':
g='g' # if I forgot to set g for pass then correct that
axon_regions = ['axon'] # matches for axon compartments
soma_regions = ['soma', 'dend_0'] # matches for soma compartments
dend_regions = ['dend_5', 'dend_7', 'spine', 'apic'] # matches for dend
# compartments
matching_regions = 'unassigned'
if region[0] == 'a':
matching_regions = axon_regions
if region[0] == 's':
matching_regions = soma_regions
if region[0] == 'd':
matching_regions = dend_regions
if matching_regions == 'unassigned':
print("invalid region passed to gset(region, mech_str, set_value, g='gbar')")
print("please use 'a' axon, 's' soma, or 'd' dendrite for region")
return
slope = (end_value - start_value) / (end_distance - start_distance)
for sec in h.allsec():
sec_in_region = False
for region_sec in matching_regions:
if region_sec in str(sec):
sec_in_region = True
if sec_in_region:
if h.ismembrane(mech_str, sec=sec):
# there are two formats: for accessing sections that are spines
# and another for accessing sections that are not spines:
if 'spine' in str(sec):
# below returns the 1000 in Spine[1000].spine
spine_number_string=str(sec).split('[')[1].split(']')[0]
current_distance = h.distance(sec(0.5), h.soma(0.5))
if start_distance < current_distance < end_distance :
scaled_value = start_value + \
slope * (current_distance - start_distance)
cmd = \
f"spines[{spine_number_string}]" \
+ f".{g}_{mech_str} = {scaled_value}"
exec(cmd)
else:
for seg in sec: # assume that the gmax_mech is a range
# variable in case it is distance dependent
current_distance = h.distance(seg, h.soma(0.5))
if start_distance < current_distance < end_distance :
scaled_value = start_value + \
slope * (current_distance - start_distance)
exec(f"h.{seg}.{g}_{mech_str} = {scaled_value}")