-
Notifications
You must be signed in to change notification settings - Fork 1
/
rae4.py
326 lines (254 loc) · 9.35 KB
/
rae4.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
#!/usr/bin/env python3
# encoding: utf-8
# rae4.py
# juanfc 2019-07-08
# 2020-02-17 Simple copy from ae3
#
# Builds the ranges for each parameter and make the corresponding
# ae4.py calls
# 2020-07-15
# Fallos desde el principio. Se construía un comandos.txt y lanzaba sbatch,
# lo cual funciona si no tocas el comandos.txt. Al renombrar el
# comandos.txt, cuando finalmente entraba el sbatch, no veía el
# comandos.txt.
# Arreglado del tirón creando un comandos.personalizado para cada rae4 que
# se lanza, y así no se toca más
# TODO: --species can have more than one range… so it has to be expanded by itself
# naming the output folder and each file
# SOLUTION:
# --species has been treated especially. As an special parameter, sorry
# its ranges do not admit , yet
# rae4.py --algo=[1:3] --uno=[a] lacagaste --none=nada --species=1,a=[2:4],b=[2:6]
# FOR LISTS INSIDE RANGES
#
# USE ;
#
# rae4.py --algo=[1:3] --uno=[a] lacagaste --none=nada --species="1,2,a=[2:4;7],b=[2:6]"
# WORKING!:
# rae4.py --outDir=kkkkkkkk 0619Amen2 --species="-1,IndirectOffspring=[-8:-4]"
# --numGen int
# --verbose
# --saveExcel
# i - initFile
# r - --setRandomSeed int
# v - --varia
# n - --NumberOfCells int
# s - --NumberOfRsrcsInEachCell int
# d - --Distribution 'str'
# --species 'str'
# "NumberOfItems": 100,
# "DirectOffspring": 5,
# "GroupPartners": "A",
# "PhenotypicFlexibility": 0.0,
# "AssociatedSpecies": "",
# "IndirectOffspring": 2,
# "FitnessVariationLimit": 0
# --outDir 'str'
# --outFName 'str'
# print(sys.argv)
# status = os.system('./ae4.py 0619Amen2 --species="-1,IndirectOffspring=-8"')
# print(status)
# print(buildList("1,2,4:8,100,1000:1010"))
# print(expandArg("--algo=[1,2,4:8,100,1000:1010]"))
# print(expandArg("--algo=[1,2,3]"))
# print(expandArg("--algo=[a]"))
# print(expandArg("--algo=a=[1:6],b=[-1,8:10]"))
# print(expandArg("--algo=nada"))
# print(ranges)
# ranges = [expandArg("--algo=[1:3]"), expandArg("--uno=[a]"), expandArg("lacagaste"), expandArg("--none=nada"), expandArg("--vars=[1,2,4:8]")]
# --algo=[1:3] --uno=[a] lacagaste --none=nada --vars=[1,2,4:8]
import sys
import os
import subprocess
from pathlib import Path
import shutil
import argparse
import re
from datetime import datetime
import time
def frange(x, y, jump):
while x < y:
yield x
x += jump
def buildList(pyExpr):
r = []
tmp = pyExpr.split(",")
for item in tmp:
if item.count(':') == 1:
first, last = item.split(':')
if '.' in first or '.' in last:
first = float(first)
last = float(last)
else:
first = int(first)
last = int(last)
r += list(map(str,list(frange(first, last))))
elif item.count(':') == 2:
first, last, step = item.split(':')
if '.' in first or '.' in last or '.' in step:
first = float(first)
last = float(last)
step = float(step)
else:
first = int(first)
last = int(last)
step = int(step)
r += list(map(str,list(frange(first, last, step))))
else:
r.append(item)
return r
def expandArg(anArg):
r = []
if "[" not in anArg or anArg.startswith("GroupPartners"):
r = ["", [anArg]]
else:
r = anArg.strip()[:-1].split("[")
# print(r)
r = [r[0], buildList(r[1])]
return r
def build(base, ranges, sep):
tsep = ""
commandList = [base]
for theName, theRange in ranges:
temp = []
for i in theRange:
for prev in commandList:
temp += [prev + tsep + theName + str(i)]
commandList = temp
tsep = sep
return commandList
def quoteSpecies(commandList):
r = []
for command in commandList:
r.append( re.sub(r'--species=([^ ]+)', r'--species="\1"', command))
return r
def fileNumbering(n):
return "%04d" % n
outDir = datetime.now().strftime('%Y%m%d-%H%M%S') # easying no range execs
# def commandsBack(cmds):
# os.rename(cmds, "comandos_"+str(outDir)+"_"+datetime.now().strftime('%Y%m%d-%H%M%S')+".txt")
if len(sys.argv) > 1 and "-h" == sys.argv[1]:
print("""Examples:
rae4.py --outDir=kkkkkkkk 0619Amen2 --species="-1;IndirectOffspring=[-8:-4,2];0;DirectOffspring=[1,2]"
rae4.py --outDir=kkkkkkkk 0619Amen2 --NumberOfCells=[100:10000:100] --species="-1;IndirectOffspring=[-8:-4,2];0;DirectOffspring=[1,2]"
rae4.py --outDir=kkkkkkkk -t 0619Amen2 --NumberOfCells=[100:103] --species="-1;IndirectOffspring=[-8:-4,2];0;DirectOffspring=[1,2]"
rae4.py --outDir=kkkkkkkk -t 0619Amen2 --NumberOfCells="[100:110:3,8,10:14:2]" --species="-1;IndirectOffspring=[-8:-4:2,2];0;DirectOffspring=[1,2]"
You must express an output directory in the first parameter:
--outDir=nameOfTheDirectory
if it it starts in / it'd be an absolute path
in other case, it will be created inside 'results' directory
UPDATE: if none provided it defaults to a default out dir, to ease no-range rae
You can add the argument:
--time=7-0 # for allotting the maximum 7 days
--time=0:30:00 # for allotting 30 minutes
IMPORTANT:
Observe in the last examples:
To express a list of different values or ranges, separate them with ;
but to prevent the Terminal shell from an undesired interpretation, surround it in ';' or ";"
""")
exit(0)
onLinux = sys.platform == "linux" or sys.platform == "linux2"
COMMANDSLEFT = "comandosRestan.txt"
TIMESLURM="7-0"
if onLinux:
if os.path.isfile(COMMANDSLEFT):
print("****** RESUMING PREVIOUS UNFINISHED EXECUTION *****")
print()
with open(COMMANDSLEFT) as f:
n = sum(1 for _ in f)
x = subprocess.call(["sbatch",
"--array=1-"+str(n),
"--time="+TIMESLURM,
"arbatch.sh",
COMMANDSLEFT
])
# if not x:
# shutil.move(COMMANDSLEFT, COMMANDSLEFT+'_'+datetime.now().strftime('%Y%m%d-%H%M%S')+'.txt')
sys.exit(x)
jsonFile = ''
testMode = False
ranges = []
for arg in sys.argv[1:]:
if "-t" == arg:
testMode = True
continue
if not arg.startswith("-"): # json ??
jsonFile = arg
if jsonFile.endswith('.json'):
jsonFile = jsonFile[:jsonFile.rfind('.')]
continue
if arg.startswith("--time="):
_, TIMESLURM = arg.split("=")
continue
if arg.startswith("--outDir="):
_, outDir = arg.split("=")
continue
if not arg.startswith("--species"):
ranges.append(expandArg(arg))
else:
#print("*****************", arg[10:])
ranges.append(["", build("--species=", map(expandArg, arg[10:].split(";")), ";" )])
commandList = build("./ae4.py ", ranges, " ")
commandList = quoteSpecies(commandList)
if not outDir:
print("You must give an output directory in parameters:")
print(" --outDir=nameOfTheDirectory")
print("if the dir starts in / it'd be an absolute path")
print("in other case, it will be created inside 'results' directory")
exit(1)
outDir = Path(outDir)
if not outDir.root:
finalOutDir = Path("results") / outDir
if not testMode:
if not os.path.isdir(str(finalOutDir)):
finalOutDir.mkdir(parents=True)
# finalOutDir.mkdir(parents=True, exist_ok=True)
outListFName = finalOutDir / "_list.txt"
n = 1
if not testMode:
listing = open(str(outListFName), "w")
ae4CommandsFile = "comandos_"+str(outDir)+"_"+datetime.now().strftime('%Y%m%d-%H%M%S')+".txt"
comandos = open(ae4CommandsFile, "w") # adding lines for next sbatch arbatch
for command in commandList:
fname = fileNumbering(n)
command += " --outDir=" + str(outDir)
command += " --outFName=" + fname
command += " --setRandomSeed=1"
command += " --redirectStdout"
# command += " --sendTelegram"
if jsonFile:
newjsonFile = jsonFile+"_"+fname
command += " "+newjsonFile
if jsonFile.startswith('/'):
realjsonFile = jsonFile
realnewjsonFile = newjsonFile
else:
realjsonFile = "data/"+jsonFile
realnewjsonFile = "data/"+newjsonFile
if testMode:
print("to copy: "+ realjsonFile+'.json'+" -> "+realnewjsonFile+'.json')
else:
try:
shutil.copy2(realjsonFile+'.json', realnewjsonFile+'.json')
except Exception as e:
print("ERROR: File do not exist: "+jsonFile+'.json')
print(command)
if not testMode:
print(command, file=listing)
print(command, file=comandos)
# os.system(command)
n += 1
if not testMode:
listing.close()
comandos.close()
if not testMode:
if onLinux:
# print(["sbatch", "--array=1-"+str(len(commandList)), "arbatch.sh"])
subprocess.call(["sbatch",
"--array=1-"+str(len(commandList)),
"--time="+TIMESLURM,
"arbatch.sh",
ae4CommandsFile
])
# print("Dando 3 segundos…")
# time.sleep(3)