-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction_lib.py
2276 lines (2126 loc) · 103 KB
/
function_lib.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
__author__ = 'milsteina'
import math
import pickle
import os.path
import datetime
import copy
import time
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mm
import h5py
import scipy.optimize as optimize
import scipy.signal as signal
import random
import pprint
from neuron import h # must be found in system $PYTHONPATH
#---------------------------------------Some global variables and functions------------------------------
data_dir = 'data/'
#data_dir = '/Users/milsteina/PycharmProjects/NEURON/data/'
morph_dir = 'morphologies/'
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
"""
Structure of Mechanism Dictionary: dict of dicts
keys: description:
'mechanism name': Value is dictionary specifying how to set parameters at the mechanism level.
'cable': Value is dictionary specifying how to set basic cable parameters at the section level. Includes
'Ra', 'cm', and the special parameter 'spatial_res', which scales the number of segments per
section for the specified sec_type by a factor of an exponent of 3.
'ions': Value is dictionary specifying how to set parameters for ions at the section or segment level.
These parameters must be specified **after** all other mechanisms have been inserted.
values:
None: Use default values for all parameters within this mechanism.
dict:
keys:
'parameter name':
values: dict:
keys: value:
'origin': 'self': Use 'value' as a baseline value.
sec_type: Inherit value from last seg of the closest node with sec of
sec_type along the path to root.
'value': float: If 'origin' is 'self', contains the baseline value.
'slope': float: If exists, contains slope in units per um. If not, use
constant 'value' for the full length of sec.
'max': float: If 'slope' exists, 'max' is an upper limit for the value
'min': float: If 'slope' exists, min is a lower limit for the value
"""
default_mech_dict = {'ais': {'cable': {'Ra': {'origin': 'soma'}, 'cm': {'origin': 'soma'}},
'pas': {'e': {'origin': 'soma'}, 'g': {'origin': 'soma'}}},
'apical': {'cable': {'Ra': {'origin': 'soma'}, 'cm': {'origin': 'soma'}},
'pas': {'e': {'origin': 'soma'}, 'g': {'origin': 'soma'}}},
'axon': {'cable': {'Ra': {'origin': 'soma'}, 'cm': {'origin': 'soma'}},
'pas': {'e': {'origin': 'soma'}, 'g': {'origin': 'soma'}}},
'basal': {'cable': {'Ra': {'origin': 'soma'}, 'cm': {'origin': 'soma'}},
'pas': {'e': {'origin': 'soma'}, 'g': {'origin': 'soma'}}},
'soma': {'cable': {'Ra': {'value': 150.}, 'cm': {'value': 1.}},
'pas': {'e': {'value': -67.}, 'g': {'value': 2.5e-05}}},
'trunk': {'cable': {'Ra': {'origin': 'soma'}, 'cm': {'origin': 'soma'}},
'pas': {'e': {'origin': 'soma'}, 'g': {'origin': 'soma'}}},
'tuft': {'cable': {'Ra': {'origin': 'soma'}, 'cm': {'origin': 'soma'}},
'pas': {'e': {'origin': 'soma'}, 'g': {'origin': 'soma'}}},
'spine_neck': {'cable': {'Ra': {'origin': 'soma'}, 'cm': {'origin': 'soma'}},
'pas': {'e': {'origin': 'soma'}, 'g': {'origin': 'soma'}}},
'spine_head': {'cable': {'Ra': {'origin': 'soma'}, 'cm': {'origin': 'soma'}},
'pas': {'e': {'origin': 'soma'}, 'g': {'origin': 'soma'}}}}
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 = sec(0.5).diam
Ra = sec.Ra
cm = sec.cm
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 scaleSWC(filenameBase, mag=100, scope='neurolucida'):
# this function rescales the SWC file with the real distances.
f = open(morph_dir+filenameBase+'.swc')
lines = f.readlines()
f.close()
Points = []
if mag == 100:
if scope == 'neurolucida':
xyDist = 0.036909375 # 0.07381875
zDist = 1.0
else:
xyDist = 0.065
zDist = 0.05
else:
raise Exception('Calibration for {}X objective unknown.'.format(mag))
for line in lines:
ll = line.split(' ')
nn = int(float(ll[0])) # label of the point
tp = int(float(ll[1])) # point type
py = float(ll[2]) # note the inversion of x, y.
px = float(ll[3])
z = float(ll[4]) # z
r = float(ll[5]) # radius of the sphere.
np = int(float(ll[6])) # parent point id.
# get the length in micron
py *= xyDist; px *= xyDist; r = r*xyDist; z *= zDist
Points.append([nn,tp,py,px,z,r,np])
print 'Saving SWC to file '+filenameBase+'-scaled.swc'
f = open(morph_dir+filenameBase+'-scaled.swc', 'w')
for [nn,tp,py,px,z,r,np] in Points:
ll = str(int(nn))+' '+str(int(tp))+' '+str(py)+' '+str(px)+' '+str(z)+' '+str(r)+' '+str(int(np))+'\n'
f.write(ll)
f.close()
def investigateSWC(filenameBase):
# this function reports the min and max values for y, x, z, and radius from an SWC file.
f = open(morph_dir+filenameBase+'.swc')
lines = f.readlines()
f.close()
xvals = []
yvals = []
zvals = []
rvals = []
for line in lines:
ll = line.split(' ')
yvals.append(float(ll[2])) # note the inversion of x, y.
xvals.append(float(ll[3]))
zvals.append(float(ll[4])) # z
rvals.append(float(ll[5])) # radius of the sphere.
print 'x - ',min(xvals),':',max(xvals)
print 'y - ',min(yvals),':',max(yvals)
print 'z - ',min(zvals),':',max(zvals)
print 'r - ',min(rvals),':',max(rvals)
def translateSWCs():
"""
Eric Bloss has produced high resolution .swc files that each contain a volume 10 um deep in z. This method
determines from the filename the z offset of each file and translates the z coordinates of the .swc files to
facilitate stitching them together into a single volume. Also changes the sec_type of any node that is not a root
and has no children within a file to 7 to indicate a leaf that potentially needs to be connected to a nearby root.
Also attempts to connect unconnected nodes that are within 2 um of each other across consecutive slices, and labels
them with sec_type = 8. This doesn't work particularly well and files must be extensively proofread in NeuTuMac.
"""
num_nodes = 0
outputname = 'combined-offset-connected.swc'
out_f = open(outputname, 'w')
# out_test = open('combined-offset-connected.swc', 'w')
prev_nodes = {}
filenames = []
z_offsets = []
for filename in os.listdir('.'):
if '.swc' in filename and not '-offset' in filename:
filenames.append(filename)
z_offsets.append(float(filename.split('z=')[1].split(' ')[0])/10.0)
indexes = range(len(z_offsets))
indexes.sort(key=z_offsets.__getitem__)
for i in indexes:
f = open(filenames[i])
lines = f.readlines()
f.close()
num_nodes += len(prev_nodes)
nodes = {}
leaves = []
for line in [line.split(' ') for line in lines if not line.split(' ')[0] in ['#', '\r\n']]:
index = int(float(line[0])) + num_nodes # node index
nodes[index] = {}
nodes[index]['type'] = int(float(line[1])) # sec_type
nodes[index]['y'] = float(line[2]) # note the inversion of x, y.
nodes[index]['x'] = float(line[3])
nodes[index]['z'] = float(line[4]) + z_offsets[i]
nodes[index]['r'] = float(line[5]) # radius of the sphere.
nodes[index]['parent'] = int(float(line[6])) # index of parent node
if not nodes[index]['parent'] == -1:
nodes[index]['parent'] += num_nodes
leaves.append(index)
for index in nodes: # keep nodes with no children
parent = nodes[index]['parent']
if parent in leaves:
leaves.remove(parent)
for index in leaves:
nodes[index]['type'] = 7
print 'Saving '+filenames[i]+' to '+outputname
if prev_nodes:
leaves = [index for index in nodes if (nodes[index]['type'] == 7 or nodes[index]['parent'] == -1)]
for prev_index in [index for index in prev_nodes if (prev_nodes[index]['type'] == 7 or
prev_nodes[index]['parent'] == -1)]:
for index in leaves:
distance = math.sqrt((prev_nodes[prev_index]['x']-nodes[index]['x'])**2 +
(prev_nodes[prev_index]['y']-nodes[index]['y'])**2 +
(prev_nodes[prev_index]['z']-nodes[index]['z'])**2)
# print prev_index, index, distance
if distance < 2.:
prev_nodes[prev_index]['type'] = 8
nodes[index]['type'] = 8
nodes[index]['parent'] = prev_index
leaves.remove(index)
break
for index in prev_nodes:
line = str(index)+' '+str(prev_nodes[index]['type'])+' '+str(prev_nodes[index]['y'])+' '+\
str(prev_nodes[index]['x'])+' '+str(prev_nodes[index]['z'])+' '+str(prev_nodes[index]['r'])+' '+\
str(prev_nodes[index]['parent'])+'\n'
out_f.write(line)
prev_nodes = copy.deepcopy(nodes)
for index in prev_nodes:
line = str(index)+' '+str(prev_nodes[index]['type'])+' '+str(prev_nodes[index]['y'])+' '+\
str(prev_nodes[index]['x'])+' '+str(prev_nodes[index]['z'])+' '+str(prev_nodes[index]['r'])+' '+\
str(prev_nodes[index]['parent'])+'\n'
out_f.write(line)
out_f.close()
def write_to_pkl(fname, data):
"""
HocCell objects maintain a nested dictionary specifying membrane mechanism parameters for each subcellular
compartment. This method is used to save that dictionary to a .pkl file that can be read in during model
specification or after parameter optimization.
:param fname: str
:param data: picklable object
"""
output = open(fname, 'wb')
pickle.dump(data, output, 2)
output.close()
def read_from_pkl(fname):
"""
HocCell objects maintain a nested dictionary specifying membrane mechanism parameters for each subcellular
compartment. This method is used to load that dictionary from a .pkl file during model specification.
:param fname: str
:return: unpickled object
"""
if os.path.isfile(fname):
pkl_file = open(fname, 'rb')
data = pickle.load(pkl_file)
# pprint.pprint(data)
pkl_file.close()
return data
else:
raise Exception('File: {} does not exist.'.format(fname))
class QuickSim(object):
"""
This method is used to run a quick simulation with a set of current injections and a set of recording sites.
Can save detailed information about the simulation to an HDF5 file after each run. Once defined, IClamp objects
persist when using an interactive console, but not when executing standalone scripts. Therefore, the best practice
is simply to set amp to zero to turn off current injections, or move individual IClamp processes to different
locations rather then adding and deleting them.
class params:
self.stim_list:
self.rec_list:
"""
def __init__(self, tstop=400., cvode=1, dt=None, verbose=1):
self.rec_list = [] # list of dicts with keys for 'cell', 'node', 'loc' and 'vec': pointer to hoc Vector object.
# Also contains keys for 'ylabel' and 'units' for recording parameters other than Vm.
self.stim_list = [] # list of dicts with keys for 'cell', 'node', 'stim': pointer to hoc IClamp object, and
# 'vec': recording of actual stimulus for plotting later
self.tstop = tstop
h.load_file('stdrun.hoc')
h.celsius = 35.0
if cvode:
self.cvode = h.CVode()
self.cvode.active(1)
self.cvode.atol(0.001) # 0.0001
else:
self.cvode = None
if dt is None:
self.dt = h.dt
else:
self.dt = dt
self.verbose = verbose
self.tvec = h.Vector()
self.tvec.record(h._ref_t)
self.parameters = {}
def run(self, v_init=-65.):
start_time = time.time()
h.tstop = self.tstop
if self.cvode is None:
h.steps_per_ms = int(1. / self.dt)
h.dt = self.dt
h.v_init = v_init
h.init()
h.run()
if self.verbose:
print 'Simulation runtime: ', time.time()-start_time, ' sec'
def append_rec(self, cell, node, loc=None, param='_ref_v', object=None, ylabel='Vm', units='mV', description=None):
rec_dict = {'cell': cell, 'node': node, 'ylabel': ylabel, 'units': units}
if not description is None:
rec_dict['description'] = description
rec_dict['vec'] = h.Vector()
if object is None:
if loc is None:
loc = 0.5
rec_dict['vec'].record(getattr(node.sec(loc), param))
else:
if loc is None:
try:
loc = object.get_segment().x # this should not push the section to the hoc stack or risk overflow
except:
loc = 0.5 # if the object doesn't have a .get_loc() method, default to 0.5
if param is None:
rec_dict['vec'].record(object)
else:
rec_dict['vec'].record(getattr(object, param))
rec_dict['loc'] = loc
self.rec_list.append(rec_dict)
def append_stim(self, cell, node, loc, amp, delay, dur, description='IClamp'):
stim_dict = {'cell': cell, 'node': node, 'description': description}
stim_dict['stim'] = h.IClamp(node.sec(loc))
stim_dict['stim'].amp = amp
stim_dict['stim'].delay = delay
stim_dict['stim'].dur = dur
stim_dict['vec'] = h.Vector()
stim_dict['vec'].record(stim_dict['stim']._ref_i)
self.stim_list.append(stim_dict)
def modify_stim(self, index=0, node=None, loc=None, amp=None, delay=None, dur=None, description=None):
stim_dict = self.stim_list[index]
if not (node is None and loc is None):
if not node is None:
stim_dict['node'] = node
if loc is None:
loc = stim_dict['stim'].get_segment().x
stim_dict['stim'].loc(stim_dict['node'].sec(loc))
if not amp is None:
stim_dict['stim'].amp = amp
if not delay is None:
stim_dict['stim'].delay = delay
if not dur is None:
stim_dict['stim'].dur = dur
if not description is None:
stim_dict['description'] = description
def modify_rec(self, index=0, node=None, loc=None, object=None, param='_ref_v', ylabel=None, units=None,
description=None):
rec_dict = self.rec_list[index]
if not ylabel is None:
rec_dict['ylabel'] = ylabel
if not units is None:
rec_dict['units'] = units
if not node is None:
rec_dict['node'] = node
if not loc is None:
rec_dict['loc'] = loc
if object is None:
rec_dict['vec'].record(getattr(rec_dict['node'].sec(rec_dict['loc']), param))
elif param is None:
rec_dict['vec'].record(object)
else:
rec_dict['vec'].record(getattr(object, param))
if not description is None:
rec_dict['description'] = description
def plot(self):
for rec_dict in self.rec_list:
if 'description' in rec_dict:
description = str(rec_dict['description'])
else:
description = ''
plt.plot(self.tvec, rec_dict['vec'], label=rec_dict['node'].name+'('+str(rec_dict['loc'])+') - '+
description)
plt.xlabel("Time (ms)")
plt.ylabel(rec_dict['ylabel']+' ('+rec_dict['units']+')')
plt.legend(loc='upper right')
if 'description' in self.parameters:
plt.title(self.parameters['description'])
plt.show()
plt.close()
def export_to_file(self, f, simiter=0):
"""
Extracts important parameters from the lists of stimulation and recording sites, and exports to an HDF5
database. Arrays are saved as datasets and metadata is saved as attributes.
:param f: :class:'h5py.File'
:param simiter: int
"""
start_time = time.time()
if str(simiter) not in f:
f.create_group(str(simiter))
f[str(simiter)].create_dataset('time', compression='gzip', compression_opts=9, data=self.tvec)
f[str(simiter)]['time'].attrs['dt'] = self.dt
for parameter in self.parameters:
f[str(simiter)].attrs[parameter] = self.parameters[parameter]
if self.stim_list:
f[str(simiter)].create_group('stim')
for index, stim in enumerate(self.stim_list):
stim_out = f[str(simiter)]['stim'].create_dataset(str(index), compression='gzip', compression_opts=9,
data=stim['vec'])
cell = stim['cell']
stim_out.attrs['cell'] = cell.gid
node = stim['node']
stim_out.attrs['index'] = node.index
stim_out.attrs['type'] = node.type
loc = stim['stim'].get_segment().x
stim_out.attrs['loc'] = loc
distance = cell.get_distance_to_node(cell.tree.root, node, loc)
stim_out.attrs['soma_distance'] = distance
distance = cell.get_distance_to_node(cell.get_dendrite_origin(node), node, loc)
stim_out.attrs['branch_distance'] = distance
stim_out.attrs['amp'] = stim['stim'].amp
stim_out.attrs['delay'] = stim['stim'].delay
stim_out.attrs['dur'] = stim['stim'].dur
stim_out.attrs['description'] = stim['description']
f[str(simiter)].create_group('rec')
for index, rec in enumerate(self.rec_list):
rec_out = f[str(simiter)]['rec'].create_dataset(str(index), compression='gzip', compression_opts=9,
data=rec['vec'])
cell = rec['cell']
rec_out.attrs['cell'] = cell.gid
node = rec['node']
rec_out.attrs['index'] = node.index
rec_out.attrs['type'] = node.type
rec_out.attrs['loc'] = rec['loc']
distance = cell.get_distance_to_node(cell.tree.root, node, rec['loc'])
rec_out.attrs['soma_distance'] = distance
distance = cell.get_distance_to_node(cell.get_dendrite_origin(node), node, rec['loc'])
rec_out.attrs['branch_distance'] = distance
rec_out.attrs['ylabel'] = rec['ylabel']
rec_out.attrs['units'] = rec['units']
if 'description' in rec:
rec_out.attrs['description'] = rec['description']
if self.verbose:
print 'Simulation ', simiter, ': exporting took: ', time.time()-start_time, ' s'
class Normalized_Step(object):
"""
For use with scipy.optimize packages like basinhopping that allow a customized step-taking method.
Converts basinhopping absolute stepsize into different stepsizes for each parameter such that the stepsizes are
some fraction of the ranges specified by xmin and xmax. Also enforces bounds for x, and explores the range in
log10 space when the range is greater than 2 orders of magnitude.
xmin and xmax are delivered as raw, not relative values. Can handle negative values and ranges that cross zero. If
xmin and xmax are not provided, or contain None as values, the default is 0.1 and 10. * x0.
"""
def __init__(self, x0, xmin=None, xmax=None, stepsize=0.5):
self.stepsize = stepsize
if xmin is None:
xmin = [None for i in range(len(x0))]
if xmax is None:
xmax = [None for i in range(len(x0))]
for i in range(len(x0)):
if xmin[i] is None:
if x0[i] > 0.:
xmin[i] = 0.1 * x0[i]
else:
xmin[i] = 10. * x0[i]
if xmax[i] is None:
if x0[i] > 0.:
xmax[i] = 10. * x0[i]
else:
xmax[i] = 0.1 * x0[i]
self.x0 = x0
self.x_range = np.subtract(xmax, xmin)
self.order_mag = np.abs(np.log10(np.abs(np.divide(xmax, xmin))))
self.log10_range = np.log10(np.add(1., self.x_range))
self.x_offset = np.subtract(1., xmin)
def __call__(self, current_x):
x = np.add(current_x, self.x_offset)
x = np.maximum(x, 1.)
x = np.minimum(x, np.add(1., self.x_range))
for i in range(len(x)):
if self.order_mag[i] >= 2.:
x[i] = self.log10_step(i, x[i])
else:
x[i] = self.linear_step(i, x[i])
new_x = np.subtract(x, self.x_offset)
return new_x
def linear_step(self, i, xi):
step = self.stepsize * self.x_range[i] / 2.
new_xi = np.random.uniform(max(1., xi-step), min(xi+step, 1.+self.x_range[i]))
return new_xi
def log10_step(self, i, xi):
step = self.stepsize * self.log10_range[i] / 2.
xi = np.log10(xi)
new_xi = np.random.uniform(max(0., xi-step), min(xi+step, self.log10_range[i]))
new_xi = np.power(10., new_xi)
return new_xi
def combine_output_files(rec_file_list, new_rec_filename=None, local_data_dir=data_dir):
"""
List contains names of files generated by "embarassingly parallel" execution of simulations on separate cores.
This function combines the contents of the files into one .hdf5 file.
:param rec_file_list: list
:param new_rec_filename: str or None
:param local_data_dir: str
"""
if new_rec_filename is None:
new_rec_filename = 'combined_output_'+datetime.datetime.today().strftime('%m%d%Y%H%M')
new_f = h5py.File(local_data_dir+new_rec_filename+'.hdf5', 'w')
simiter = 0
for rec_filename in rec_file_list:
old_f = h5py.File(local_data_dir+rec_filename+'.hdf5', 'r')
for old_group in old_f.itervalues():
new_f.copy(old_group, new_f, name=str(simiter))
simiter += 1
old_f.close()
new_f.close()
print 'Combined data in list of files and exported to: '+new_rec_filename+'.hdf5'
return new_rec_filename
def time2index(tvec, start, stop):
"""
When using adaptive time step (cvode), indices corresponding to specific time points cannot be calculated from a
fixed dt. This method returns the indices closest to the duration bounded by the specified time points.
:param tvec: :class:'numpy.array'
:param start: float
:param stop: float
:return: tuple of int
"""
left = np.where(tvec >= start)[0]
if np.any(left): # at least one value was found
left = left[0]
else:
right = len(tvec) - 1 # just take the last two indices
left = right - 1
return left, right
if tvec[left] >= stop:
right = left
left -= 1
return left, right
right = np.where(tvec <= stop)[0][-1]
if right == left:
left -= 1
return left, right
def get_Rinp(tvec, vec, start, stop, amp):
"""
Calculate peak and steady-state input resistance from a step current injection. For waveform current injections, the
peak but not the steady-state will have meaning.
:param tvec: array
:param vec: array
:param start: float
:param stop: float
:param amp: float
:return: tuple of float
"""
interp_t = np.arange(0., stop, 0.01)
interp_vm = np.interp(interp_t, tvec, vec)
left, right = time2index(interp_t, start-3., start-1.)
baseline = np.mean(interp_vm[left:right])
temp_vec = np.abs(interp_vm - baseline)
peak = np.max(temp_vec[right:])
left, right = time2index(interp_t, stop-3., stop-1.)
plateau = np.mean(temp_vec[left:right])
return baseline, peak/abs(amp), plateau/abs(amp)
def model_exp_rise_decay(t, tau_rise, tau_decay):
shape = np.exp(-t/tau_decay)-np.exp(-t/tau_rise)
return shape/np.max(shape)
def model_exp_rise(t, tau):
return 1-np.exp(-t/tau)
def model_exp_decay(t, tau):
return np.exp(-t/tau)
def model_scaled_exp(t, A, tau, A0=0):
return A*np.exp(t/tau)+A0
def null_minimizer(fun, x0, args, **options):
"""
Rather than allow basinhopping to pass each local mimimum to a gradient descent algorithm for polishing, this method
just catches and passes all local minima so basinhopping can proceed.
"""
return optimize.OptimizeResult(x=x0, fun=fun(x0, *args), success=True, nfev=1)
class MyTakeStep(object):
"""
For use with scipy.optimize packages like basinhopping that allow a customized step-taking method.
Converts basinhopping absolute stepsize into different stepsizes for each parameter such that the stepsizes are
some fraction of the ranges specified by xmin and xmax. Also enforces bounds for x, and explores the range in
log space when the range is greater than 3 orders of magnitude.
"""
def __init__(self, blocksize, xmin, xmax, stepsize=0.5):
self.stepsize = stepsize
self.blocksize = blocksize
self.xmin = xmin
self.xmax = xmax
self.xrange = []
for i in range(len(self.xmin)):
self.xrange.append(self.xmax[i] - self.xmin[i])
def __call__(self, x):
for i in range(len(x)):
if x[i] < self.xmin[i]:
x[i] = self.xmin[i]
if x[i] > self.xmax[i]:
x[i] = self.xmax[i]
snew = self.stepsize / 0.5 * self.blocksize * self.xrange[i] / 2.
sinc = min(self.xmax[i] - x[i], snew)
sdec = min(x[i]-self.xmin[i], snew)
# chooses new value in log space to allow fair sampling across orders of magnitude
if np.log10(self.xmax[i]) - np.log10(self.xmin[i]) >= 3.:
x[i] = np.power(10, np.random.uniform(np.log10(x[i]-sdec), np.log10(x[i]+sinc)))
else:
x[i] += np.random.uniform(-sdec, sinc)
return x
def get_expected_spine_index_map(sim_file):
"""
There is a bug with HDF5 when reading from a file too often within a session. Instead of constantly reading from the
HDF5 file directly and searching for content by spine_index or path_index, the number of calls to the sim_file can
be reduced by creating a mapping from spine_index or path_index to HDF5 group key. It is possible for a spine to
have more than one entry in an expected_file, with branch recordings in different locations and therefore different
expected EPSP waveforms, so it is necessary to also distinguish those entries by path_index.
:param sim_file: :class:'h5py.File'
:return: dict
"""
index_map = {}
for key, sim in sim_file.iteritems():
path_index = sim.attrs['path_index']
spine_index = sim.attrs['spine_index']
if path_index not in index_map:
index_map[path_index] = {}
index_map[path_index][spine_index] = key
return index_map
def get_spine_group_info(sim_filename, verbose=1):
"""
Given a processed output file generated by export_nmdar_cooperativity, this method returns a dict that has
separated each group of stimulated spines by dendritic sec_type, and sorted by distance from soma. For ease of
inspection so that the appropriate path_index can be chosen for plotting expected and actual summation traces.
:param sim_filename: str
:return: dict
"""
spine_group_info = {}
with h5py.File(data_dir+sim_filename+'.hdf5', 'r') as f:
for path_index in f:
sim = f[path_index]
path_type = sim.attrs['path_type']
path_category = sim.attrs['path_category']
if path_type not in spine_group_info:
spine_group_info[path_type] = {}
if path_category not in spine_group_info[path_type]:
spine_group_info[path_type][path_category] = {'path_indexes': [], 'distances': []}
if path_index not in spine_group_info[path_type][path_category]['path_indexes']:
spine_group_info[path_type][path_category]['path_indexes'].append(path_index)
if path_type == 'apical':
# for obliques, sort by the distance of the branch origin from the soma
distance = sim.attrs['origin_distance']
else:
distance = sim.attrs['soma_distance']
spine_group_info[path_type][path_category]['distances'].append(distance)
for path_type in spine_group_info:
for path_category in spine_group_info[path_type]:
indexes = range(len(spine_group_info[path_type][path_category]['distances']))
indexes.sort(key=spine_group_info[path_type][path_category]['distances'].__getitem__)
spine_group_info[path_type][path_category]['distances'] = \
map(spine_group_info[path_type][path_category]['distances'].__getitem__, indexes)
spine_group_info[path_type][path_category]['path_indexes'] = \
map(spine_group_info[path_type][path_category]['path_indexes'].__getitem__, indexes)
if verbose:
for path_category in spine_group_info[path_type]:
print path_type, '-', path_category
for i, distance in enumerate(spine_group_info[path_type][path_category]['distances']):
print spine_group_info[path_type][path_category]['path_indexes'][i], distance
return spine_group_info
def get_expected_EPSP(sim_file, group_index, equilibrate, duration, dt=0.02):
"""
Given an output file generated by parallel_clustered_branch_cooperativity or build_expected_EPSP_reference, this
method returns a dict of numpy arrays, each containing the depolarization-rectified expected EPSP for each
recording site resulting from stimulating a single spine.
:param sim_file: :class:'h5py.File'
:param group_index: int
:param equilibrate: float
:param duration: float
:param dt: float
:return: dict of :class:'numpy.array'
"""
sim = sim_file[str(group_index)]
t = sim['time'][:]
interp_t = np.arange(0., duration, dt)
left, right = time2index(interp_t, equilibrate-3., equilibrate-1.)
start, stop = time2index(interp_t, equilibrate-2., duration)
trace_dict = {}
for rec in sim['rec'].itervalues():
location = rec.attrs['description']
vm = rec[:]
interp_vm = np.interp(interp_t, t, vm)
baseline = np.average(interp_vm[left:right])
interp_vm -= baseline
interp_vm = interp_vm[start:stop]
"""
rectified = np.zeros(len(interp_vm))
rectified[np.where(interp_vm>0.)[0]] += interp_vm[np.where(interp_vm>0.)[0]]
trace_dict[location] = rectified
"""
peak = np.max(interp_vm)
peak_index = np.where(interp_vm == peak)[0][0]
zero_index = np.where(interp_vm[peak_index:] <= 0.)[0]
if np.any(zero_index):
interp_vm[peak_index+zero_index[0]:] = 0.
trace_dict[location] = interp_vm
interp_t = interp_t[start:stop]
interp_t -= interp_t[0] + 2.
trace_dict['time'] = interp_t
return trace_dict
def get_expected_vs_actual(expected_sim_file, actual_sim_file, expected_index_map, sorted_actual_sim_keys,
interval=0.3, dt=0.02):
"""
Given an output file generated by parallel_clustered_branch_cooperativity, and an output file generated by
parallel_branch_cooperativity, this method returns a dict of lists, each containing an input-output function
relating expected to actual peak depolarization for each recording site from stimulating a group of spines on a
single branch or path. The variable expected_index_map contains a dictionary that converts an integer spine_index to
a string group_index to locate the expected EPSP for a given spine in the expected_sim_file. The variable
sorted_actual_sim_keys contains the indexes of the simulations in the actual_sim_file corresponding to the branch or
path, ordered by number of stimulated spines. These variables must be pre-computed.
:param expected_sim_file: :class:'h5py.File'
:param actual_sim_file: :class:'h5py.File'
:param expected_index_map: dict
:param sorted_actual_sim_keys: list of str
:param interval: float
:return: dict of list
"""
equilibrate = actual_sim_file[sorted_actual_sim_keys[0]].attrs['equilibrate']
duration = actual_sim_file[sorted_actual_sim_keys[0]].attrs['duration']
actual = {}
for sim in [actual_sim_file[key] for key in sorted_actual_sim_keys]:
t = sim['time'][:]
interp_t = np.arange(0., duration, dt)
left, right = time2index(interp_t, equilibrate-3., equilibrate-1.)
start, stop = time2index(interp_t, equilibrate-2., duration)
for rec in sim['rec'].itervalues():
location = rec.attrs['description']
if not location in actual:
actual[location] = []
vm = rec[:]
interp_vm = np.interp(interp_t, t, vm)
baseline = np.average(interp_vm[left:right])
interp_vm -= baseline
interp_vm = interp_vm[start:stop]
actual[location].append(np.max(interp_vm))
spine_list = sim.attrs['syn_indexes']
interp_t = interp_t[start:stop]
interp_t -= interp_t[0] + 2.
expected = {}
summed_traces = {}
equilibrate = expected_sim_file.itervalues().next().attrs['equilibrate']
duration = expected_sim_file.itervalues().next().attrs['duration']
for i, spine_index in enumerate(spine_list):
group_index = expected_index_map[spine_index]
trace_dict = get_expected_EPSP(expected_sim_file, group_index, equilibrate, duration, dt)
t = trace_dict['time']
left, right = time2index(interp_t, -2.+i*interval, interp_t[-1])
right = min(right, left+len(t))
for location in [location for location in trace_dict if not location == 'time']:
trace = trace_dict[location]
if not location in expected:
expected[location] = []
summed_traces[location] = np.zeros(len(interp_t))
summed_traces[location][left:right] += trace[:right-left]
expected[location].append(np.max(summed_traces[location]))
return expected, actual
def export_nmdar_cooperativity(expected_filename, actual_filename, description="", output_filename=None):
"""
Expects expected and actual files to be generated by parallel_clustered_ or
parallel_distributed_branch_cooperativity. Files contain simultaneous voltage recordings from 3 locations (soma,
trunk, dendrite origin) during synchronous stimulation of branches, and an NMDAR conductance recording from a single
spine in each group. Spines are distributed across 4 dendritic sec_types (basal, trunk, apical, tuft).
Generates a processed output file containing expected vs. actual data and metadata for each group of spines.
Can be used to generate plots of supralinearity, NMDAR conductance, or average across conditions, etc.
:param expected_filename: str
:param actual_filename: str
:param description: str
:param output_filename: str
"""
sim_key_dict = {}
with h5py.File(data_dir+actual_filename+'.hdf5', 'r') as actual_file:
for key, sim in actual_file.iteritems():
path_index = sim.attrs['path_index']
if path_index not in sim_key_dict:
sim_key_dict[path_index] = []
sim_key_dict[path_index].append(key)
with h5py.File(data_dir+expected_filename+'.hdf5', 'r') as expected_file:
expected_index_map = get_expected_spine_index_map(expected_file)
with h5py.File(data_dir+output_filename+'.hdf5', 'w') as output_file:
output_file.attrs['description'] = description
for path_index in sim_key_dict:
path_group = output_file.create_group(str(path_index))
sim_keys = sim_key_dict[path_index]
sim_keys.sort(key=lambda x: len(actual_file[x].attrs['syn_indexes']))
sim = actual_file[sim_keys[0]]
path_type = sim.attrs['path_type']
path_category = sim.attrs['path_category']
soma_distance = sim['rec']['4'].attrs['soma_distance']
branch_distance = sim['rec']['4'].attrs['branch_distance']
origin_distance = soma_distance - branch_distance
path_group.attrs['path_type'] = path_type
path_group.attrs['path_category'] = path_category
path_group.attrs['soma_distance'] = soma_distance
path_group.attrs['branch_distance'] = branch_distance
path_group.attrs['origin_distance'] = origin_distance
expected_dict, actual_dict = get_expected_vs_actual(expected_file, actual_file,
expected_index_map[path_index], sim_keys)
for rec in sim['rec'].itervalues():
location = rec.attrs['description']
rec_group = path_group.create_group(location)
rec_group.create_dataset('expected', compression='gzip', compression_opts=9,
data=expected_dict[location])
rec_group.create_dataset('actual', compression='gzip', compression_opts=9,
data=actual_dict[location])
def get_instantaneous_spike_probability(rate, dt=0.02, generator=None):
"""
Given an instantaneous spike rate in Hz and a time bin in ms, calculate whether a spike has occurred in that time
bin, assuming an exponential distribution of inter-spike intervals.
:param rate: float (Hz)
:param dt: float (ms)
:param generator: :class:'random.Random'
:return: bool
"""
if generator is None:
generator = random
x = generator.uniform(0., 1.)
rate /= 1000.
p = 1. - np.exp(-rate * dt)
return bool(x < p)
def get_inhom_poisson_spike_times(rate, t, dt=0.02, refractory=3., generator=None):
"""
Given a time series of instantaneous spike rates in Hz, produce a spike train consistent with an inhomogeneous
Poisson process with a refractory period after each spike.
:param rate: instantaneous rates in time (Hz)
:param t: corresponding time values (ms)
:param dt: temporal resolution for spike times (ms)
:param refractory: absolute deadtime following a spike (ms)
:param generator: :class:'random.Random()'
:return: list of m spike times (ms)
"""
if generator is None:
generator = random
interp_t = np.arange(t[0], t[-1]+dt, dt)
interp_rate = np.interp(interp_t, t, rate)
spike_times = []
i = 0
while i < len(interp_t):
if get_instantaneous_spike_probability(interp_rate[i], dt, generator):
spike_times.append(interp_t[i])
i += int(refractory / dt)
else:
i += 1
return spike_times
def get_inhom_poisson_spike_times_by_thinning(rate, t, dt=0.02, refractory=3., generator=None):
"""
Given a time series of instantaneous spike rates in Hz, produce a spike train consistent with an inhomogeneous
Poisson process with a refractory period after each spike.
:param rate: instantaneous rates in time (Hz)
:param t: corresponding time values (ms)
:param dt: temporal resolution for spike times (ms)
:param refractory: absolute deadtime following a spike (ms)
:param generator: :class:'random.Random()'
:return: list of m spike times (ms)
"""
if generator is None:
generator = random
interp_t = np.arange(t[0], t[-1] + dt, dt)
interp_rate = np.interp(interp_t, t, rate)
interp_rate /= 1000.
interp_rate = 1. / (1. / interp_rate - refractory)
spike_times = []
max_rate = np.max(interp_rate)
i = 0
ISI_memory = 0.
while i < len(interp_t):
x = generator.random()
if x > 0.:
ISI = -np.log(x) / max_rate
i += int(ISI / dt)
ISI_memory += ISI
if (i < len(interp_t)) and (generator.random() <= interp_rate[i] / max_rate) and ISI_memory >= 0.:
spike_times.append(interp_t[i])
ISI_memory = -refractory
return spike_times
def get_binned_firing_rate(train, t, bin_dur=10.):
"""
:param train: array of float
:param t: array
:param bin_dur: float (ms)
:return: array (Hz)
"""
bin_centers = np.arange(t[0]+bin_dur/2., t[-1], bin_dur)
count = np.zeros(len(bin_centers))
for spike_time in train:
if t[0] <= spike_time <= bin_centers[-1] + bin_dur / 2.:
i = np.where(bin_centers + bin_dur / 2. >= spike_time)[0][0]
count[i] += 1
rate = count / bin_dur * 1000.
rate = np.interp(t, bin_centers, rate)
return rate
def get_smoothed_firing_rate(train, t, bin_dur=50., bin_step=10., dt=0.1):
"""
:param train: array of float
:param t: array
:param bin_dur: float (ms)
:param dt: float (ms)
:return: array (Hz)
"""
count = np.zeros(len(t))
bin_centers = np.arange(t[0]+bin_dur/2., t[-1]-bin_dur/2., bin_step)
rate = np.zeros(len(bin_centers))
for spike_time in train:
if spike_time >= t[0] and spike_time <= t[-1]:
i = np.where(t >= spike_time)[0][0]
count[i] += 1
left = 0
right = int(bin_dur / dt)
interval = int(bin_step / dt)
for i, bin_t in enumerate(bin_centers):
rate[i] = np.sum(count[left:right]) / bin_dur * 1000.
left += interval
right += interval
rate = np.interp(t, bin_centers, rate)
return rate
def get_removed_spikes(rec_filename, before=1.6, after=6., dt=0.02, th=10., plot=1):
"""
:param rec_filename: str
:param before: float : time to remove before spike
:param after: float : time to remove after spike in case where trough or voltage recovery cannot be used
:param dt: float : temporal resolution for interpolation and dvdt
:param th: float : slope threshold
:param plot: int
:return: list of :class:'numpy.array'
"""
removed = []
with h5py.File(data_dir+rec_filename+'.hdf5', 'r') as f:
sim = f.itervalues().next()
equilibrate = sim.attrs['equilibrate']
duration = sim.attrs['duration']
track_equilibrate = sim.attrs['track_equilibrate']
for rec in f.itervalues():
t = np.arange(0., duration, dt)
vm = np.interp(t, rec['time'], rec['rec']['0'])
start = int((equilibrate + track_equilibrate) / dt)
t = np.subtract(t[start:], equilibrate + track_equilibrate)
vm = vm[start:]