-
Notifications
You must be signed in to change notification settings - Fork 2
/
pyramidal_readExportedGeometry.py
602 lines (480 loc) · 18.6 KB
/
pyramidal_readExportedGeometry.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
#!/usr/bin/python
_usageStr=\
"""usage: neuron_readExportedGeometry.py geoFile
get dictionary object describing neuron model geometry info by reading file
"""
import os, sys, re, math
from pyramidal_NeuronGeometry import *
import numpy as np
import networkx as nx
class HocGeometry(Geometry):
def __init__(self, _fileName=None):
Geometry.__init__(self)
self._openFilament = None
self._connections = []
self.connections = []
self._filamentNames = []
self._filaments = {}
self._filamentNameType = None
self._warnRepeatFilaments = True
if _fileName is not None:
self.setFileName(_fileName)
self.readGeometry()
def readGeometry(self):
"""
get dictionary object describing neuron model geometry info by reading file
"""
lineNum = 0
with open(self.fileName, 'r') as fIn:
# read the geometry file
try:
for line in fIn:
# loop through each line in the file
# inc the line number
lineNum = lineNum + 1
# parse the line in geometry file, adding info to geometryInfo
self._parseHocGeometryLine(line)
except IOError as err:
sys.tracebacklimit = 0
raise IOError('Error reading %s line %d: %s' % \
(self.fileName, lineNum, err.message))
if self._openFilament:
raise IOError('Error reading %s, filament %s open at end of file' %
(self.fileName, self._openFilament))
# connect filaments and remove filaments and _connections, leaving segments
# and nodes
self._connectFilaments()
# make compartments from hemispheres remaining at the end of unconnected
# segments
#self._addOneNodeCompartments()
def getSomaIndex(self):
"""
return (filamentIndex, position)
filamentIndex indexes the .hoc file filament that contains the Soma
position is a float between 0 and 1 that points to the soma centroid on
the filament
"""
# get the Soma
soma = self.soma
filamentIndex = self.getFilamentIndex(soma)
# get the centroid of the Soma
centroid = soma.centroidPosition(mandateTag='Soma')
return (filamentIndex, centroid)
def getTipIndices(self):
"""
return (filamentInds, positions)
filamentInds is a list of indices to .hoc file filaments that contain
terminal segments
positions is a list of floats (0 or 1) that point to the terminal end
of each terminal segment
NOTE: This will NOT contain Axon or Soma even if they are terminal segments
"""
self.checkConnectivity(removeDisconnected=False)
soma = self.soma
axons = self.findAxons()
def _terminalEnd(seg):
n0, n1 = False, False
for loc, nLoc, node in seg.neighborLocations:
if loc == 0.0:
if n1:
return None
else:
n0 = True
elif loc == 1.0:
if n0:
return None
else:
n1 = True
if n0:
return 1.0
else:
return 0.0
ends = ((self.getFilamentIndex(s), _terminalEnd(s)) for s in self.segments
if (s != soma and s not in axons))
try:
filamentInds, positions = zip( *((f, e) for f, e in ends
if e is not None))
except ValueError:
raise ValueError('No tip indices found!?!')
return filamentInds, positions
def getSegLengths(self):
"""
return (segLength)
"""
cnt = 0
for segs in len(range(self.segments)):
cnt += 1
return cnt
def getTips(self):
"""
return (filamentInds, positions)
filamentInds is a list of indices to .hoc file filaments that contain
terminal segments
positions is a list of floats (0 or 1) that point to the terminal end
of each terminal segment
NOTE: This will NOT contain Axon or Soma even if they are terminal segments
"""
h = self.checkConnectivity(removeDisconnected=False)
#print(len(h))
#for s in h:
# print([s.coordAt(0), s.coordAt(1)])
soma = self.soma
axons = self.findAxons()
print('Soma is %s.\n%i axons are:' %(self.soma.name, len(axons)))
print([a.name for a in axons])
def _terminalEnd(seg):
n0, n1 = False, False
for loc, nLoc, node in seg.neighborLocations:
if loc == 0.0:
if n1:
return None
else:
n0 = True
elif loc == 1.0:
if n0:
return None
else:
n1 = True
if n0:
return 1.0
else:
return 0.0
ends = ((s, _terminalEnd(s)) for s in self.segments
if (s != soma and s not in axons))
try:
terminalSegs, positions = zip( *((f, e) for f, e in ends
if e is not None))
except ValueError:
raise ValueError('No tip indices found!?!')
return terminalSegs, positions
def getAxonIndices(self):
"""
return (filamentInds, positions)
filamentInds is a list of indices to .hoc file filaments that contain
terminating branches
positions is a list of floats (0 or 1) that point to the terminal end
of each terminating branch
"""
self.checkConnectivity(removeDisconnected=False)
axons = self.findAxons()
if not axons:
return [], []
def _terminalEnd(seg):
n0, n1 = False, False
for loc, nLoc, node in seg.neighborLocations:
if loc == 0.0:
assert not n1, 'Axon is an isolated segment'
n0 = True
elif loc == 1.0:
assert not n0, 'Axon is an isolated segment'
n1 = True
if n0:
return 1.0
else:
return 0.0
ends = ((self.getFilamentIndex(s), _terminalEnd(s)) for s in axons)
filamentInds, positions = zip( *((f, e) for f, e in ends) )
return filamentInds, positions
def _parseHocGeometryLine(self, line):
"""
Read a line from hoc file specifying geometry, and update geometryInfo
appropriately.
openFilament = name of filament if in a declaration block, otherwise = None
"""
splitLine = line.split(None)
if not splitLine:
return
if self._openFilament:
self._parseDefineFilament(line)
elif splitLine[0] == 'connect':
self._addConnection(splitLine)
elif splitLine[0] == 'create':
self._createFilaments(splitLine)
elif splitLine[0] == 'neuron_name':
self.name = splitLine[-1]
elif splitLine[0].lower() == "range":
if len(splitLine) < 7:
raise IOError(\
'range should be of form "range minX maxX minY maxY minZ maxZ"')
self.minRange = tuple([float(x) for x in splitLine[1:6:2]])
self.maxRange = tuple([float(x) for x in splitLine[2:7:2]])
elif splitLine[0] in self._filamentNames:
self._openFilament = splitLine[0]
elif splitLine[0]+'[0]' in self._filamentNames:
self._openFilament = splitLine[0]+'[0]'
def _parseDefineFilament(self, line):
"""
Parse a line in a filament declaration block. Add node, clear nodes, or
close block. If multiple nodes are added in one declaration block, connect
them. Update geometryInfo['nodes'] and geometryInfo['filaments']
appropriately.
"""
splitLine = re.split(',|\)|\(', line.strip())
openSegment = self.segments[self._filamentNames.index(self._openFilament)]
if splitLine[0] == '}':
self._openFilament = None
elif splitLine[0] == 'pt3dclear':
openSegment.clear()
elif splitLine[0] == 'pt3dadd':
if not (len(splitLine) == 6 or
(len(splitLine) == 7 and splitLine[-2] == '0')):
raise IOError('Unexpected form for pt3dadd')
x,y,z,d = tuple(float(s) for s in splitLine[1:5])
if d <= 0:
if d == 0:
raise ValueError('pt3dadd with diameter = 0.0')
else:
raise ValueError('pt3dadd with diameter < 0.0')
self._addNode(openSegment, x, y, z, 0.5 * d)
if len(openSegment.nodes) > 1:
node0 = openSegment.nodes[-2]
node1 = openSegment.nodes[-1]
self._addCompartment(openSegment, node0, node1, append=True)
else:
raise IOError('Invalid filament command')
def _addConnection(self, splitLine):
"""
Return dict describing connection between two filaments
"""
(name1, location1) = re.split('\(|\)', splitLine[1])[0:2]
(name2, location2) = re.split('\(|\)', splitLine[2])[0:2]
connection = { \
'filament1' : name1, \
'location1' : float(location1), \
'filament2' : name2, \
'location2' : float(location2) \
}
self._connections.append(connection)
self.connections.append(connection)
def _createFilaments(self, splitLine):
"""
Add requested number of filaments to geometry, as segments
"""
if '[' and ']' in splitLine[1]:
# hoc produced by Imaris, requests variable number of filaments
# create baseName[numFilaments]
baseName, numFilamentsStr = re.split('\[|\]', splitLine[1])[0:2]
numFilaments = int(numFilamentsStr)
if self._filamentNameType in [None, 'Imaris']:
self._filamentNameType = 'Imaris'
else:
self._filamentNameType = 'Mixed'
warn('Filament index will not reliably match numbers in filament name')
thisType = 'Imaris'
else:
# hoc produce by Amira, requests 1 filament
# create baseName
baseName, numFilaments = splitLine[1], 1
if self._filamentNameType in [None, 'Amira']:
self._filamentNameType = 'Amira'
else:
self._filamentNameType = 'Mixed'
warn('Filament index will not reliably match numbers in filament name')
thisType = 'Amira'
for n in range(numFilaments):
if thisType == 'Imaris':
name = '%s[%d]' % (baseName, n)
else:
name = baseName
if name in self._filamentNames:
raise IOError('%s already created' % name)
newSeg = self._addSegment(name)
newSeg.filamentIndex = len(self._filamentNames)
self._filamentNames.append(name)
self._filaments[newSeg.filamentIndex] = newSeg
def _connectFilaments(self):
"""
Loop through requested filament _connections.
For each connection connect two filaments together by joining the nodes at
their ends. Note that this removes a node for each connection
"""
def _getSegmentFromFilament(_filament):
_segment = self._filaments[self._filamentNames.index(_filament)]
return _segment
while self._connections:
connection = self._connections.pop()
# get the filaments and locations
location0 = connection['location2']
filament0 = connection['filament2']
segment0 = _getSegmentFromFilament(filament0)
location1 = connection['location1']
filament1 = connection['filament1']
segment1 = _getSegmentFromFilament(filament1)
self._connectSegments(segment0, location0, segment1, location1)
self.nodes = [n for n in self.nodes if n not in self._removeNodes]
self._removeNodes = set()
def getFilamentIndex(self, seg):
"""
Return index to filament from original .hoc file
"""
#filamentIndex = int(seg.name.split('[')[1].split(']')[0])
return seg.filamentIndex
def getFilament(self, index):
"""
return a segment based upon filament number
"""
return self._filaments[index]
###############################################################################
def _parseArguments():
arguments = sys.argv
if len(arguments) != 2:
print(_usageStr)
raise TypeError('Incorrect number of arguments.')
geoFile = arguments[1]
return geoFile
def suggestProps(geometry, tau_m=215.2, tau_1=29.0, R_0=2.1, R_in=9.7):
# R in MOhm, Tau in ms
Cm = 1.0 # uF/cm^2, gospel
Rm = 1.0e-3 * tau_m / Cm # MOhm cm^2
g1 = 1.0e-6 / Rm # S/cm^2
RTau1 = 1.0e-3 * tau_1 / Cm
g2 = 1.0e-6 / RTau1
somaArea = geometry._soma.surfaceArea * 1.0e-2
cellArea = geometry.surfaceArea * 1.0e-2
g3 = 1.0e-6 / R_0 / somaArea
g4 = 1.0e-6 / R_0 / cellArea
g5 = 1.0e-6 / R_in / somaArea
g6 = 1.0e-6 / R_in / cellArea
print('Potential leak conductances:')
print(g1, g2, g3, g4, g5, g6)
"""
L = math.pi / math.sqrt(tau_m / tau_1 - 1) #unitless ratio of length / lambda
RInf = R_in * math.tanh(L)
print('Rm = %g MOhm cm^2' % Rm)
print('L = %g' % L)
print('RInf = %g MOhm' % RInf)
# get d (equivalent diameter in cm) somehow...
# maybe guess it's the smallest diameter in the primary neurite?
somaIndex, somaPos = geometry.getSomaIndex()
d = 2 * geometry.segments[somaIndex].minRadius * 1.0e-4 # cm
Ri = (RInf * math.pi / 2 * d**1.5)**2 / Rm # MOhm cm
Ra = 1.0e6 * Ri # Ohm cm
print('cm = %g uF/cm^2, gLeak = %g S/cm^2, Ra = %g Ohm cm'
% (Cm, g, Ra))
"""
###############################################################################
def demoReadOld(geoFile):
### Read in geometry file and pre-compute various quantities
geometry = HocGeometry(geoFile)
### This section gets indices to the Axon, Soma, and neurite tips,
### then measures distance from Axon to Soma, and from Axon to a random
### neurite tip.
axons = geometry.findAxons()
if axons:
axonInds, axonTipPos = geometry.getAxonIndices()
print('Axon tip length = %g' % axons[0].length)
print('Axon tip pos = %f' % axonTipPos[0])
pDF = PathDistanceFinder(geometry, axons[0], axonTipPos[0])
soma = geometry.soma
somaInd, somaPos = geometry.getSomaIndex()
print('Path distance from Axon tip to Soma = %g' %
pDF.distanceTo(soma, somaPos))
import random
random.seed
neuriteTipInd, neuriteTipPos = geometry.getTipIndices()
whichTip = random.randint(0, len(neuriteTipInd) - 1)
randomTip, randomPos = neuriteTipInd[whichTip], neuriteTipPos[whichTip]
print('Path distance from Axon tip to random neurite tip = %g' %
pDF.distanceTo(geometry.getFilament(randomTip), randomPos))
else:
print('No axons found')
if os.access('steady_voltages.pickle', os.R_OK):
import cPickle
from math import sqrt, log
from matplotlib import pyplot
pDF = PathDistanceFinder(geometry, somaInd)
dSeg = [pDF.distanceTo(s) for s in geometry.segments]
with open('steady_voltages.pickle', 'r') as fIn:
vSteady = cPickle.loads(fIn.read())
eLengths = pDF.getElectrotonicLengths(vSteady)
logELengths = [log(eL) for eL in eLengths]
dist = [pDF.distanceTo(s) for s in range(len(vSteady))]
diam = [sqrt(s.avgRadius) for s in geometry.segments]
"""
pyplot.figure()
pyplot.plot(dist, vSteady, 'r.')
pyplot.figure()
pyplot.plot(dist, logELengths, 'b.')
pyplot.figure()
pyplot.plot(diam, logELengths, 'b.')
pyplot.show()
"""
suggestProps(geometry)
### Display summary info
geometry.displaySummary()
return geometry
def demoReadsilent(geoFile):
### Read in geometry file and pre-compute various quantities
geometry = HocGeometry(geoFile)
tips, tipPositions = geometry.getTips()
pDF = PathDistanceFinder(geometry, geometry.soma)
#tortuosities = [geometry.pathTortuosity(pDF.pathTo(tip, pos))
# for tip, pos in zip(tips, tipPositions)]
tortuosities = [pDF.tortuosityTo(tip, pos)
for tip, pos in zip(tips, tipPositions)]
meanTort, stdTort = mean(tortuosities), std(tortuosities)
print('From soma to tips, tortuosity is %.1f +- %.1f'
% (meanTort, stdTort))
# i commented out a bunch of shit that doesn't work with imaris
#Cons = geometry.connections
#Seg1s, Seg2s = [], []
#for c in Cons:
# Seg1s.append(c['filament1']) # here, location1 is always 0
# Seg2s.append(c['filament2']) # here, location2 is always 1
# #geometry.c['filament1'].coordAt(c['location1'])
#used_segs = []
#for x,y in zip(Seg1s, Seg2s):
# used_segs.append(int(x.split('_')[1]))
# used_segs.append(int(y.split('_')[1]))
#for j in range(0,max(used_segs)):
# if not j in used_segs:
# print('Segment %i not present' %j)
#properties = geometry.getProperties()
# geometry.displaySummary()
return geometry#, properties
###############################################################################
def multi_demoRead(geoFile, passiveFile="", display=True, makePlots=False):
### Read in geometry file and pre-compute various quantities
# create geometry object
geometry = HocGeometry(geoFile)
#geometry.displaySummary()
# return the properties
return geometry.getProperties(passiveFile, display=display,
makePlots=makePlots)
def demoRead(geoFile):
### Read in geometry file and pre-compute various quantities
geometry = HocGeometry(geoFile)
tips, tipPositions = geometry.getTips()
pDF = PathDistanceFinder(geometry, geometry.soma)
#tortuosities = [geometry.pathTortuosity(pDF.pathTo(tip, pos))
# for tip, pos in zip(tips, tipPositions)]
tortuosities = [pDF.tortuosityTo(tip, pos)
for tip, pos in zip(tips, tipPositions)]
meanTort, stdTort = mean(tortuosities), std(tortuosities)
print('From soma to tips, tortuosity is %.1f +- %.1f'
% (meanTort, stdTort))
# i commented out a bunch of shit that doesn't work with imaris
#Cons = geometry.connections
#Seg1s, Seg2s = [], []
#for c in Cons:
# Seg1s.append(c['filament1']) # here, location1 is always 0
# Seg2s.append(c['filament2']) # here, location2 is always 1
# #geometry.c['filament1'].coordAt(c['location1'])
#used_segs = []
#for x,y in zip(Seg1s, Seg2s):
# used_segs.append(int(x.split('_')[1]))
# used_segs.append(int(y.split('_')[1]))
#for j in range(0,max(used_segs)):
# if not j in used_segs:
# print('Segment %i not present' %j)
geometry.displaySummary()
#properties = geometry.getProperties()
return geometry#, properties
###############################################################################
if __name__ == "__main__":
# get the geometry file
geoFile = _parseArguments()
# run a demo of capabilities
demoRead(geoFile)
#exit
sys.exit(0)