-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuron_refine_results.py
126 lines (97 loc) · 3.53 KB
/
neuron_refine_results.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
#!/usr/bin/python
_usageStr=\
"""usage: neuron_refine_results.py [startupFile] [resultsFile]
Creates a new startup file that implements a fit centered closely on the
results of a previous fit.
"""
import sys
###############################################################################
def refineLog(paramName, resultVal, nameLength):
"""
Create a new fitting range for a log-distributed parameter
"""
minRange = resultVal / 1.018
maxRange = resultVal * 1.018
minAllowed = resultVal / 2.0
maxAllowed = resultVal * 2.0
formatStr = '%%-%ds %%8.3g %%8.3g %%8.3g %%8.3g\n' % nameLength
return formatStr % \
(paramName, minAllowed, maxAllowed, minRange, maxRange)
###############################################################################
def refineUniform(paramName, resultVal, nameLength):
"""
Create a new fitting range for a uniform-distributed parameter
"""
minRange = resultVal - 0.5
maxRange = resultVal + 0.5
minAllowed = resultVal - 20.0
maxAllowed = resultVal + 20.0
formatStr = '%%-%ds %%8.1f %%8.1f %%8.1f %%8.1f\n' % nameLength
return formatStr % \
(paramName, minAllowed, maxAllowed, minRange, maxRange)
###############################################################################
def refineResults(startupFile, resultsFile):
"""
Create a new startup file to do a follow-up fit centered around the results
of a previous fit
"""
results = {}
maxNameLen = 0
with open(resultsFile, 'r') as fIn:
for line in fIn:
try:
paramName, paramValue = line.split()
if paramName == 'value':
continue
except ValueError:
continue
results[paramName] = paramValue
maxNameLen = max(maxNameLen, len(paramName))
if startupFile == 'startup.txt':
refineFile = startupFile + '.refine'
else:
refineFile = 'startup.txt'
with open(startupFile, 'r') as fIn, open(refineFile, 'w') as fOut:
for line in fIn:
splitLine = line.split()
if len(splitLine) in [3, 5] and splitLine[0] in results:
# this is a fit parameter, refine it
paramName = splitLine[0]
resultVal = float(results[paramName])
if float(splitLine[1]) * float(splitLine[2]) > 0:
# this is a log distributed parameter
outLine = refineLog(paramName, resultVal, maxNameLen)
else:
# this is a uniform-distributed parameter
outLine = refineUniform(paramName, resultVal, maxNameLen)
fOut.write(outLine)
elif len(splitLine) == 5 and splitLine[0] == 'fit':
# this line defines a fit, increase the control time by a factor of 4
tauInd = line.rindex(splitLine[4])
newTau = 4 * float(splitLine[4])
outLine = line[0:tauInd] + '%.1f\n' % newTau
fOut.write(outLine)
else:
# write out line as usual
fOut.write(line)
###############################################################################
def _parseArguments():
arguments = sys.argv
if len(arguments) not in [1, 2, 3]:
print(_usageStr)
sys.tracebacklimit = 1
raise TypeError('Incorrect number of arguments.')
if len(arguments) >= 2:
startupFile = arguments[1]
else:
startupFile = 'startup.txt'
if len(arguments) == 3:
resultsFile = arguments[2]
else:
resultsFile = 'results.txt'
return startupFile, resultsFile
###############################################################################
if __name__ == "__main__":
startupFile, resultsFile = _parseArguments()
refineResults(startupFile, resultsFile)
sys.exit(0)