forked from acsutt0n/pyramidal_Neurons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyramidal_traceback.py
163 lines (127 loc) · 3.9 KB
/
pyramidal_traceback.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
# this removes all of the redundant sections, catching tracebacks
# into a file and parsing them to figure out which sections to remove
# temp out file = temp_out.txt
import sys, traceback
from pyramidal_readExportedGeometry import *
########################################################################
# xmlwhich
def is_this_the_line(splitline, seg0, seg1):
# check if this connect line is the right one to delete
# returns true to keep, false to delete
zer = int(splitline[1].split('[')[1].split(']')[0])
one = int(splitline[2].split('[')[1].split(']')[0])
if zer == seg0 or zero == seg1:
if one == seg1 or one == seg0:
print('Removed it')
return False
return True
def remove(hocfile, seg0, seg1):
print('Reading file %s and removing connection between %i and %i'
%(hocfile, seg1, seg0))
lol = []
with open(hocfile, 'r') as fIn:
lineNum = 0
for line in fIn:
splitLine = line.split(None)
try:
if splitLine[0] == 'connect':
if is_this_the_line(splitLine, seg0, seg1):
lol.append(line)
else:
lol.append(line)
except:
lol.append(line)
return lol
def write_new(listoflines, newfilename='temphoc.hoc'):
# write the lines list to a new file
print('Writing new hoc file %s' %newfilename)
with open(newfilename, 'w') as fOut:
for l in listoflines:
fOut.write(l)
fOut.write('\n')
return
def xmlwhich_control(hocfile, seg0, seg1):
lol = remove(hocfile, seg0, seg1)
write_new(lol)
return
#######################################################################
# remove_carriage_return
def remove_carriage_returns(filein, fileout):
lol = []
with open(filein, 'r') as fIn:
lineNum, carrcount = 0, 0
for line in fIn:
lineNum = lineNum + 1
if line == '\n':
carrcount = carrcount + 1
else:
carrcount = 0
if carrcount < 2:
lol.append(line)
print('File %s was %i lines long' %(filein, lineNum))
with open(fileout, 'w') as fOut:
for l in lol:
fOut.write(l)
print('New file %s created is %i lines long' %(fileout, len(lol)))
return
######################################################################
# traceback stuff
def call_demoRead(hocfile):
try:
geo = demoRead(hocfile)
return True
except:
print('Logging traceback')
tb = traceback.format_exc()
return tb
######################################################################
# loop through removing redundant sections until none exist
def parse_traceback(tb):
splitTb = [str(k) for k in tb.split(None)]
if splitTb[0] == 'Traceback':
if splitTb[-1] == 'connected':
seg1 = int(splitTb[-7].split('[')[1].split(']')[0])
seg0 = int(splitTb[-5].split('[')[1].split(']')[0])
return [seg0, seg1]
else:
print('UNEXPECTED TRACEBACK!')
print(tb)
return [None]
else:
print(splitTb[0])
return [None]
def remove_redundant(hocfile):
"""
Hocfile is a str, not file or openfile obj.
"""
tempout = 'temp_outfile.txt'
# Run first pass
tb = call_demoRead(hocfile)
if tb is True or tb.split(None)[0] == 'None':
return # file is already ready
else:
go = True
# Loop until the file is ready
while go:
print('Got new traceback')
seglist = parse_traceback(tb)
if len(seglist) == 1:
return # file is ready
elif len(seglist) == 2:
# two segments returned -- remove them
xmlwhich_control(hocfile, seglist[0], seglist[1])
# remove carriage returns
remove_carriage_returns('temphoc.hoc', hocfile)
# check new hocfile
tb = call_demoRead(hocfile)
else:
print('bad seglist:')
print(seglist)
return
if tb is True or tb.split(None)[0] == 'None':
return # file is ready
# end of while loop and function
if __name__ == '__main__':
hocfile = sys.argv[1]
remove_redundant(hocfile)
#sys.exit(1)