-
Notifications
You must be signed in to change notification settings - Fork 1
/
simplesimdb.py
595 lines (516 loc) · 23.8 KB
/
simplesimdb.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
"""Creation and management of simple simulation data"""
import json
import hashlib # for the hashing
import os.path # to check for files
import subprocess # to run the create program
class Repeater :
""" Manage a single file pair (inputfile, outputfile)
The purpose of this class is to provide a simple tool when you do not want
to actually store simulation data on disc (except temporarily). It is
sometimes more efficient to simply write the data into a single file
and then reuse/overwrite it in all subsequent simulations.
"""
def __init__ (self, executable="./execute.sh", inputfile="temp.json", outputfile="temp.nc"):
""" Set the executable and files to use in the run method"""
self.executable = executable
self.inputfile = inputfile
self.outputfile = outputfile
@property
def executable(self):
return self.__executable
@property
def inputfile(self):
return self.__inputfile
@property
def outputfile(self):
return self.__outputfile
@executable.setter
def executable(self, executable) :
self.__executable = executable
@inputfile.setter
def inputfile(self, inputfile) :
self.__inputfile = inputfile
@outputfile.setter
def outputfile(self, outputfile) :
self.__outputfile = outputfile
def run( self, js, error="display", stdout="ignore"):
""" Write inputfile and then run a simulation
js (dict): the complete input file as a python dictionary. All keys
must be strings such that js can be converted to JSON.
error (string) :
"raise"
raise a subprocess.CalledProcessError error
if the executable returns a non-zero exit code
"display"
print(stderr ) then return
"ignore"
return
stdout (string) :
"ignore"
throw away std output
"display"
print( process.stdout)
return
"""
with open( self.inputfile, 'w') as f:
json.dump( js, f,
sort_keys=True, ensure_ascii=True, indent=4)
try :
process = subprocess.run( [self.__executable, self.__inputfile,
self.__outputfile],
check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if stdout == "display" :
print( process.stdout)
except subprocess.CalledProcessError as e:
if error == "display" :
print( e.stderr)
elif error == "raise" :
raise e
def clean(self) :
""" Remove inputfile and outputfile"""
if os.path.isfile( self.__inputfile) :
os.remove( self.__inputfile)
if os.path.isfile( self.__outputfile) :
os.remove( self.__outputfile)
class Manager :
""" Lightweight Simulation Database Manager
Create, access and display simulation data of a given code as pairs
(inputfile.json : outputfile [, restarted_01, restarted_02, ...]),
where all input is given by python dictionaries (stored as json files)
and the output files (of arbitrary type) are generated by an executable
that takes the json file as input. This executable is provided by the user.
NOTE: an executable may only take one sinlge input file and may only
generate one single output file (except for RESTART, see below)
NOTE: the executable can be a bash script. For example if the actual program
does not take json input files you could write a converter and let the bash
script chain the two programs. There are endless possibilities.
RESTART ADDON (ignore if not used):
Sometimes simulation outputs cannot be created in a single
run (due to file size, time limitations, etc.) but rather a simulation is
partitioned (in time) into a sequential number of separate smaller runs.
Correspondingly each run sequentially generates and stores only a partition
of the entire output file. Each run restarts the simulation with the result
of the previous run. The Manager solves this problem via the
simulation number n in its member functions. For n>0 create will pass the
result of the previous simulation as a third input to the executable
NAMING SCHEME:
By default the naming of the inputs and outputs is based on the sha1 of the
python dictionary as a sorted string. The user can override this behaviour by
giving a human readable name in the create function or register one manually.
This name is then mapped to the sha1 and stored in the file "simplesimdb.json"
in the data directory. All subsequent references to the input file then map to
the correct name.
"""
def __init__ (self, directory = './data', filetype='nc', executable="./execute.sh"):
""" init the Manager class
Parameters
directory (path) : the path of the folder this class manages.
Folder is created if it does not exist yet. The class will
recognize existing files that were generated by a previous session.
filetype (string) : file extension of the output files
executable (string) : The executable that generates the data
executable is called using subprocess.run([executable,
directory/hashid.json, directory/hashid.filetype],...) with 2 arguments
- a json file as input (do not change the input else the file is not
recognized any more) and one output file (that executable needs to
generate) (if your code does not take json as input you can for example
parse json in bash using jq)
RESTART ADDON (ignore if not used):
If you intend to use the restart option (by passing a simulation number
n>0 to create), executable is called with
subprocess.run([executable, directory/hashid.json,
directory/hashid0xN.filetype, directory/hashid0x(N-1).filetype],...)
that is it must take a third argument (the previous simulation)
"""
self.directory = directory
os.makedirs( self.__directory, exist_ok=True)
self.filetype = filetype
self.executable = executable
@property
def directory(self):
""" (path) : data directory
If the directory does not exist it will be created """
return self.__directory
@property
def executable(self):
"""
(string) : The executable that generates the data.
The create method calls subprocess.run([executable,
directory/hashid.json, directory/hashid.filetype],...) that is it must
take 2 arguments - a json file as input (do not change the input else
the file is not recognized any more) and one output file (that
executable needs to generate) (if your code does not take json as input
you can for example parse json in bash using jq)
RESTART ADDON (ignore if not used):
If you intend to use the restart option (by passing a simulation number
n>0 to create), the executable is called with
subprocess.run([executable, directory/hashid.json,
directory/hashid0xN.filetype, directory/hashid0x(N-1).filetype],...)
that is it must take a third argument (the previous simulation)
"""
return self.__executable
@property
def filetype(self):
""" (string) : file extension of the output files """
return self.__filetype
@directory.setter
def directory(self, directory) :
self.__directory = directory
os.makedirs( self.__directory, exist_ok=True)
@executable.setter
def executable(self, executable) :
self.__executable = executable
@filetype.setter
def filetype(self, filetype) :
self.__filetype = filetype
def create( self, js, n = 0, name = "", error = "raise", stdout="ignore"):
"""Run a simulation if outfile does not exist yet
Create (write) the in.json file to disc
Use subprocess.run( [executable, in.json, out])
If the executable returns a non-zero exit code the inputfile (if n!= 0)
and outputfile are removed
Parameters:
js (dict): the complete input file as a python dictionary. All keys
must be strings such that js can be converted to JSON.
n (integer) : (RESTART ADDON) the number of the simulation
beginning with 0. If n>0, we will use the previous simulation as a
third argument subprocess.run( [executable, in.json, out_n, out_(n-1)])
This can be used to restart simulations
name (string) : [OPTIONAL] human readable name/id that is used in the
naming of all files associated with js.
For example the input is called '<name>.json'.
If empty, the simplesimdb.json file will be searched for a name,
afterwards the default sha based naming scheme will be used,
for example '<sha1>.json'.
Once a name is given it can only be changed by deleting and
resimulating the output. Exceptions will be raised on name clashes.
Names will be stored and mapped to their sha1 in the file
"simplesimdb.json"
See also: register
error (string) :
"raise"
raise a subprocess.CalledProcessError error
if the executable returns a non-zero exit code
"display"
print(stderr ) then return
"ignore"
return
stdout (string) :
"ignore"
throw away std output
"display"
print( process.stdout)
return
ATTENTION: in order to generate a unique identifier
js needs to be normalized in the sense that the datatype must match
the required datatype documented (e.g. don't write 10 in a field
requiring float but 10.0, otherwise it is interpreted as an integer and
thus produces a different hash)
Return:
string: filename of new entry if it did not exist before
existing filename else
"""
hashid = self.hashinput(js)
if not name == "" :
self.register( js, name)
ncfile = self.outfile( js, n)
exists = os.path.isfile( ncfile)
if exists:
print( "Existing simulation " + hashid[0:6] + "..." + ncfile[-9:])
return ncfile
else :
print( "Running simulation " + hashid[0:6] + "..." + ncfile[-9:])
#First write the json file into the database
# so that the program can read it as input
if not os.path.isfile(self.jsonfile(js)) :
with open( self.jsonfile(js), 'w') as f:
json.dump( js, f,
sort_keys=True, ensure_ascii=True, indent=4)
#Run the code to create output file
try :
# Check if the simulation is a restart
if n == 0 :
process = subprocess.run( [self.__executable, self.jsonfile(js),
ncfile],
check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if stdout == "display" :
print( process.stdout)
else :
previous_ncfile = self.outfile( js, n-1)
process = subprocess.run( [self.__executable, self.jsonfile(js),
ncfile, previous_ncfile],
check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if stdout == "display" :
print( process.stdout)
except subprocess.CalledProcessError as e:
#clean up entry and escalate exception
if os.path.isfile( ncfile) :
os.remove( ncfile)
if n == 0 : # only remove input if not restarted
os.remove( self.jsonfile(js))
if error == "display" :
print( e.stderr)
elif error == "raise" :
raise e
return ncfile
def recreate( self, js, n = 0, name = "", error = "raise", stdout="ignore"):
""" Force a re-simulation:
delete(js, n) followed by create(js, n, name, error, stdout) """
self.delete(js, n)
return self.create(js, n, name, error, stdout)
def select( self, js, n = 0) :
""" Select an output file based on its input parameters
Raise a ValueError exception if the file does not exist
else it just returns self.outfile( js, n)
Parameters:
js (dict): the complete input file as a python dictionary. All keys
must be strings such that js can be converted to JSON.
n (integer) : (RESTART ADDON) the number of the simulation to select
beginning with 0.
WARNING: in order to generate a unique identifier
js needs to be normalized in the sense that the datatype must match
the required datatype documented (e.g. 10 in a field requiring float
is interpreted as an integer and thus produces a different hash)
Return:
string: self.outfile( js, n) if file exists
"""
ncfile = self.outfile( js, n)
exists = os.path.isfile( ncfile)
if not exists:
raise ValueError( 'Entry does not exist')
else :
return ncfile
def count( self, js) :
""" (RESTART ADDON) Count number of output files for given input
Count the output files associated with the given input parameters that
currently exist in directory
(i.e. count n as long as self.exists( js, n) returns True).
Parameters:
js (dict): the complete input file as a python dictionary. All keys
must be strings such that js can be converted to JSON.
Return:
integer: Number of simulations
"""
number = 0
while self.exists( js, number) :
number += 1
return number
def exists( self, js, n = 0) :
""" Check for existence of data
Parameters:
js (dict): the complete input file as a python dictionary. All keys
must be strings such that js can be converted to JSON.
n (integer) : (RESTART ADDON) the number of the simulation to select
beginning with 0.
Return:
bool: True if output data corresponding to js exists, False else
"""
ncfile = self.outfile( js, n)
return os.path.isfile( ncfile)
def files(self):
""" Return a list of dictionaries (sorted by id and number) with ids
and files existing in directory
The purpose here is to give the user an iterable object to search
or tabularize the content of outputfiles
Return:
list of dict : [ {"id": id, "n", n, "inputfile":jsonfile,
"outputfile" : outfile}], sorted by 'id' and 'n'
"""
table = []
for filename in os.listdir(self.__directory) :
if filename.endswith(".json") and not filename.endswith( "out.json") :
with open( os.path.join( self.__directory, filename), 'r') as f:
#load all json files and check if they are named correctly
# and have a corresponding output file
js = json.load( f)
number = self.count( js) # count how many exist
for n in range( 0, number) :
ncfile = self.outfile( js, n)
registry = self.get_registry()
entry = {
"id" : registry.get(self.hashinput( js),
self.hashinput( js)),
"n" : n,
"inputfile" : self.jsonfile(js),
"outputfile" : ncfile,
}
table.append(entry)
return sorted(table, key=lambda item : (item['id'], item['n']))
def table(self):
""" Return all exisiting (input)-data in a list of python dicts
Use json.dumps(table(), indent=4) to pretty print
Note that this list of dictionaries is searchable/ iteratable with standard
python methods.
RESTART ADDON: the input file for a restarted simulation shows only
once
Return:
list of dict : [ { ...}, {...},...] where ... represents the actual
content of the inputfiles
"""
files = self.files()
table=[]
for d in files :
with open( d["inputfile"]) as f :
js = json.load( f)
if d["n"] == 0 :
table.append( js)
return table
def hashinput( self, js):
"""Hash the input dictionary
Params:
js (dict): the complete input file as a python dictionary. All keys
must be strings such that js can be converted to JSON.
WARNING: in order to generate a unique identifier
js needs to be normalized in the sense that the datatype must match
the required datatype documented (e.g. 10 in a field requiring float
is interpreted as an integer and thus produces a different hash)
Return:
string: The hexadecimal sha1 hashid of the input dictionary
"""
inputstring = json.dumps( js, sort_keys=True, ensure_ascii=True)
hashed = hashlib.sha1( inputstring.encode( 'utf-8') ).hexdigest()
return hashed
def jsonfile( self, js) :
""" File path to json file from the input
Does not check if the file actually exists
Parameters:
js (dict): the complete input file as a python dictionary. All keys
must be strings such that js can be converted to JSON.
Return:
path: the file path of the input file
"""
registry = self.get_registry()
hashid = self.hashinput(js)
name = hashid
if hashid in registry :
name = registry[hashid]
return os.path.join(self.__directory, name+'.json')
def outfile( self, js, n = 0) :
""" File path to output file from the input
Do not check if the file actually exists
Parameters:
js (dict): the complete input file as a python dictionary. All keys
must be strings such that js can be converted to JSON.
n (integer) : (RESTART ADDON) the number of the simulation to select
beginning with 0.
Return:
path: the file path of the output file
"""
hashid = self.hashinput(js)
sim_num = ""
if n > 0 :
sim_num = hex(n)
registry = self.get_registry()
name = hashid
if hashid in registry :
name = registry[hashid]
if "json" == self.__filetype :
return os.path.join( self.__directory,
name + sim_num + '_out.json')
return os.path.join(self.__directory,
name + sim_num + '.' + self.__filetype)
def register(self, js, name):
""" Register a human readable name for the given input dictionary
The registry is stored in the file "simplesimdb.json"
If the given dictionary already has a name associated to it the name is
already in use or the input file exists under a different name an
Exception will be raised
Params:
js (dict): the complete input file as a python dictionary. All keys
must be strings such that js can be converted to JSON.
name (string) : A human readable name/id that is henceforth used in the
naming of all files associated with js.
"""
registry = self.get_registry()
hashid = self.hashinput(js)
if name == "simplesimdb" :
raise Exception("The name simplesimdb is not allowed. Choose a different name!")
if hashid in registry :
if name != registry[hashid]:
raise Exception( "The name '"+name+"' cannot be used! The\
input file is already known under the name '"+registry[hashid]+"'. Use\
delete to clear the registry.")
else :
jsonfile = os.path.join(self.__directory, hashid+'.json')
if os.path.isfile( jsonfile) :
raise Exception( "The name '"+name+"' cannot be used! The\
input file is already known under the name '"+jsonfile+"'. Use\
delete to clear the registry.")
registry[hashid] = name
for key, value in registry.items():
if (value == name) and key != hashid:
raise Exception( "The name '"+name+"' is already in use\
for a different simulation. Choose a different name!")
self.set_registry(registry)
def get_registry( self):
""" Get a dictionary containing the mapping from sha to names
Read the file "simplesimdb.json"
Return:
dict: may be empty, contains all registered names
"""
registryFile = os.path.join(self.__directory,
'simplesimdb.json')
registry = dict()
if os.path.isfile( registryFile) :
with open( registryFile, "r") as f :
registry = json.load(f)
return registry
def set_registry(self, registry):
""" Set the registry with a dictionary containing mapping from sha to names
Use with care as this operation can corrupt your naming scheme!
Params:
registry (dict) : if empty, the registry is deleted
"""
registryFile = os.path.join(self.__directory,
'simplesimdb.json')
with open( registryFile, "w") as f :
json.dump( registry, f,
sort_keys=True, ensure_ascii=True, indent=4)
if not registry :
os.remove( registryFile)
def delete( self, js, n = 0) :
""" Delete an entry if it exists
js (dict): the complete input file as a python dictionary. All keys
must be strings such that js can be converted to JSON.
n (integer) : (RESTART ADDON) the number of the simulation to select
beginning with 0.
In case n>0, only the outputfile outfile(js,n) will be removed.
In case n==0, both the outfile as well as the jsonfile(js) and
any eventual registered names will be removed
"""
ncfile = self.outfile( js, n)
exists = os.path.isfile( ncfile)
if exists :
os.remove( ncfile)
if n == 0 :
os.remove( self.jsonfile(js))
registry = self.get_registry()
hashid = self.hashinput(js)
if hashid in registry :
del registry[hashid]
self.set_registry( registry)
def delete_all (self) :
""" Delete all file pairs id'd by the files method
and the directory itself (if empty)
ATTENTION: if you want to continue to use the object afterwards
remember to reset the directory: m.directory = '...'
"""
files = self.files()
for entry in files :
if entry["n"] == 0 :
os.remove( entry["inputfile"])
os.remove( entry["outputfile"])
registry = dict()
self.set_registry( registry)
try :
os.rmdir( self.__directory)
except OSError as e:
pass # if the directory is non-empty nothing happens
#### Ideas on a file view class
# - for projects that are not managed or created with simplesimdb
# - We can have a class managing a view of (input.json, outfile) pairs
# without creating files but just managing the inputs
# - problem is that inputfiles are seldom separately stored but we would rather
# need a view of netcdf files where we can assume the input is stored inside
# - no functionality to create or delete files
# - add single files or whole folders (assuming json and nc file has the name)