-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuron_readExportedGeometry.py
398 lines (330 loc) · 12.3 KB
/
neuron_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
#!/usr/bin/python
import os, sys, re, math
from NeuronGeometry import *
class HocGeometry(Geometry):
def __init__(self, _fileName=None):
Geometry.__init__(self)
self._openFilament = None
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=True)
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 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
"""
self.checkConnectivity(removeDisconnected=True)
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 = ((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=True)
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)
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()
for connection in self._connections:
# 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.connections = []
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 demoRead(geoFile, passiveFile="", display=True, makePlots=False):
### Read in geometry file and pre-compute various quantities
# create geometry object
geometry = HocGeometry(geoFile)
# return the properties
return geometry.getProperties(passiveFile, display=display,
makePlots=makePlots)
###############################################################################
def _parseArguments():
import argparse
parser = argparse.ArgumentParser(description=
"Read a neuron geometry exported in a .hoc file, and extract some"
+ "properties. If a passive properties file is specified, simulate the "
+ "neuron to obtain more properties.")
parser.add_argument("geoFile", help="file specifying neuron geometry",
type=str)
parser.add_argument("passive", nargs="?", default="",
help="specify passive properties", type=str)
parser.add_argument("--plots", action='store_true',
help="visualize some neuron data")
return parser.parse_args()
###############################################################################
if __name__ == "__main__":
# get the geometry file
options = _parseArguments()
# run a demo of capabilities
demoRead(options.geoFile, options.passive, makePlots=options.plots)
# display any plots
if options.plots:
pyplot.show()
#exit
sys.exit(0)