-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuron_recalcResume.py
165 lines (118 loc) · 4.41 KB
/
neuron_recalcResume.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
#!/usr/bin/python
_usageStr=\
"""usage: neuron_recalcResume.py resumeFile
Back up resumeFile to resumeFile.backup then overwrite parameter values
with NaN, so that parameters will be re-calculated.
"""
import os, sys
###############################################################################
def makeBackupFile(resumeFile):
"""
backup resumeFile and return the name of the backup file
"""
backupFile = resumeFile + '.backup'
if os.path.isfile(backupFile):
sys.tracebacklimit = 0
raise TypeError('Backup file already exists: ' + backupFile)
os.rename(resumeFile, backupFile)
return backupFile
###############################################################################
def getNext(fIn, fOut):
line = next(fIn)
while (not line.strip()) or (line.strip().startswith('#')):
fOut.write(line)
line = next(fIn)
return line
###############################################################################
def rewriteHeaderLines(fIn, fOut, newPopulationSize):
# get the paramteter descriptions
firstLine = getNext(fIn, fOut)
numDesc = int(firstLine.split(None, 1)[0])
fOut.write(firstLine)
for n in range(numDesc):
fOut.write(getNext(fIn, fOut))
# set the best parameters' value to Inf
line = getNext(fIn, fOut)
val = line.split(None, 1)[0]
fOut.write(line.replace(val, 'inf'))
# write the total time without altering it
fOut.write(getNext(fIn, fOut))
# set the current generation time to 0
line = getNext(fIn, fOut)
timeStr = line.split('#')[0].strip()
fOut.write(line.replace(timeStr, '0.0s', 1))
# write the number of parameter sets evaluated without altering it
fOut.write(getNext(fIn, fOut))
# set the number of evaluations this generation to 0
line = getNext(fIn, fOut)
numEval = line.split(None, 1)[0]
fOut.write(line.replace(numEval, '0', 1))
# write the rest of the header without altering it, until the line declaring
# the population size
line = getNext(fIn, fOut)
while not line.endswith('# population\n'):
fOut.write(line)
line = getNext(fIn, fOut)
# set the new population size (if requested) and write last header line
if newPopulationSize < float('inf'):
splitLine = line.split()
oldPop = splitLine[0]
line = line.replace(oldPop, str(newPopulationSize), 1)
fOut.write(line)
###############################################################################
def rewritePopulation(fIn, fOut, newPopulationSize):
n = 0
for line in fIn:
n += 1
if n > newPopulationSize:
break
# replace value with nan
valueStr = line.split(None, 1)[0]
fixedLine = line.replace(valueStr, 'nan', 1)
fOut.write(fixedLine)
###############################################################################
def writeNewResumeFile(resumeFile, backupFile, newPopulationSize=None):
"""
read from backupFile and write a new resumeFile where all the parameters need
to be reevaluated
"""
if newPopulationSize is None:
newPopulationSize = float('inf')
with open(resumeFile, 'w') as fOut, open(backupFile, 'r') as fIn:
rewriteHeaderLines(fIn, fOut, newPopulationSize)
rewritePopulation(fIn, fOut, newPopulationSize)
###############################################################################
def recalcResume(resumeFile, newPopulationSize=None):
"""
backup resumeFile and write a new resumeFile where all the parameters need to
be reevaluated
"""
backupFile = makeBackupFile(resumeFile)
try:
writeNewResumeFile(resumeFile, backupFile, newPopulationSize)
except:
os.system('rm -f %s' % resumeFile)
os.system('mv %s %s' % (backupFile, resumeFile))
raise
###############################################################################
def _parseArguments():
arguments = sys.argv
if len(arguments) > 3:
print(_usageStr)
sys.tracebacklimit = 0
raise TypeError('Incorrect number of arguments.')
resumeFile = arguments[1]
if not os.path.isfile(resumeFile):
print(_usageStr)
sys.tracebacklimit = 0
raise TypeError("File doesn't exist: " + resumeFile)
if len(arguments) == 3:
populationSize = int(arguments[2])
else:
populationSize = float('inf')
return resumeFile, populationSize
###############################################################################
if __name__ == "__main__":
resumeFile, populationSize = _parseArguments()
recalcResume(resumeFile, populationSize)
sys.exit(0)