-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuron_testParams.py
170 lines (124 loc) · 4.83 KB
/
neuron_testParams.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
#!/usr/bin/python
_usageStr=\
"""usage: test_params.py
blah
"""
import sys, os
import matplotlib.pyplot as pyplot
import neuron_createModelHocFile
import neuron_simulate
import neuron_plot_trace
###############################################################################
def getNeuronTraceFile(startupFile, fileNames):
dataFile = fileNames['dataFile']
splitData = os.path.split(dataFile)
return os.path.join(splitData[0], 'Sim' + splitData[1])
###############################################################################
def simulateInNeuron(baseDir):
# point to the startup file
startupFile = os.path.join(baseDir, 'start_neuron.txt')
# get info about other files associated with the simulation
fileNames = neuron_createModelHocFile.getFileNames(startupFile)
# get the name of the file containing the output traces
traceFile = getNeuronTraceFile(startupFile, fileNames)
# run the simulation
startup_mtime = os.stat(startupFile).st_mtime
geo_mtime = os.stat(fileNames['geoFile']).st_mtime
try:
trace_mtime = os.stat(traceFile).st_mtime
except:
trace_mtime = float('-inf')
if startup_mtime > trace_mtime or geo_mtime > trace_mtime:
# need to simulate
neuron_simulate.neuron_simulate(startupFile)
# load the output traces
neuronTraces = neuron_plot_trace.loadTraces(traceFile)
# scale any current traces by -1
#for trace in neuronTraces:
# if trace['units'] == 'nA':
# trace['data'] = [-x for x in trace['data']]
# and return them
return neuronTraces
###############################################################################
def simulateInCpp(baseDir):
# point to the startup file and the output trace file
programFile = os.path.abspath(os.path.join(baseDir, 'simulate_neuron.bin'))
startupFile = os.path.join(baseDir, 'startup.txt')
traceFile = os.path.join(baseDir, 'recorded_traces.txt')
# run the simulation
startup_mtime = os.stat(startupFile).st_mtime
try:
trace_mtime = os.stat(traceFile).st_mtime
except:
trace_mtime = float('-inf')
if startup_mtime > trace_mtime:
# need to simulate
os.system('%s %s %s' % (programFile, startupFile, traceFile) )
# load the output traces
cppTraces = neuron_plot_trace.loadTraces(traceFile)
#print('Loaded %d cpp traces' % len(cppTraces))
# and return them
return cppTraces
###############################################################################
def checkDifference(neuronTrace, cppTraces, traces):
for cppTrace in cppTraces:
if cppTrace['name'] == (neuronTrace['name'] + '_0'):
# these traces record the same thing. Plot:
# -both traces overlayed
# -their difference (if they have the same length)
# remove them from the general plot
traces.remove(neuronTrace)
traces.remove(cppTrace)
# plot the traces overlayed
neuron_plot_trace.plotTrace(neuronTrace, cppTrace)
neuronData = neuronTrace['data']
cppData = cppTrace['data']
if len(neuronData) != len(cppData):
# the traces have different lengths, don't try to form difference
return
# form the difference between them and plot it
diffData = [neuronData[n] - cppData[n] for n in range(len(neuronData))]
diffTrace = {'name' : neuronTrace['name'] + '_difference', \
'units' : neuronTrace['units'], \
'numT' : neuronTrace['numT'], \
'dT' : neuronTrace['dT'], \
'data' : diffData}
neuron_plot_trace.plotTrace(diffTrace)
return
###############################################################################
def compareTraces(neuronTraces, cppTraces):
# join the two lists of traces
traces = []
traces.extend(neuronTraces)
traces.extend(cppTraces)
for neuronTrace in neuronTraces:
checkDifference(neuronTrace, cppTraces, traces)
return traces
###############################################################################
def _parseArguments():
arguments = sys.argv
if len(arguments) > 2:
print(_usageStr)
sys.tracebacklimit = 1
raise TypeError('Incorrect number of arguments.')
elif len(arguments) == 2:
baseDir = arguments[1]
else:
baseDir = os.getcwd()
return baseDir
###############################################################################
if __name__ == "__main__":
baseDir = _parseArguments()
# get traces from simulation via NEURON
neuronTraces = simulateInNeuron(baseDir)
# get trace from simulation via C++
cppTraces = simulateInCpp(baseDir)
# join traces, and make difference trace for v_soma
traces = compareTraces(neuronTraces, cppTraces)
for trace in traces:
# plot each trace
neuron_plot_trace.plotTrace(trace)
# wait until figures are closed
os.system('beep -l 100 -f 200 -n 2')
pyplot.show()
sys.exit(0)