-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcombine_runs.py
More file actions
74 lines (56 loc) · 2.1 KB
/
combine_runs.py
File metadata and controls
74 lines (56 loc) · 2.1 KB
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
import os
import pandas as pd
import json
from argparse import ArgumentParser
def compress_dict(dictionary, prefix=''):
output = {}
for key, value in dictionary.items():
if type(value) is dict:
output.update(compress_dict(value, prefix=key+'.'))
elif type(value) is list:
output[prefix+key] = str(value)
else:
output[prefix+key] = value
return output
def get_config_dictionary(filename):
with open(filename) as f:
data = json.load(f)
return compress_dict(data)
def remove_keys_from_dict(dictionary, keys):
for key in keys:
if key in dictionary.keys():
del dictionary[key]
return dictionary
def get_results_dictionary(filename):
with open(filename) as f:
data = json.load(f)
if data['status'] != 'COMPLETED':
print('Run {} not completed and will be ignored'.format(filename))
return None
result = remove_keys_from_dict(data['result'], ['default_factory', 'py/object'])
return result
def repeat_values(dictionary, repeats):
for key, value in dictionary.items():
dictionary[key] = [value]*repeats
return dictionary
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('run_dir', nargs='+', help='Directories containing sacred config.json and run.json files')
parser.add_argument('--output', required=True, help='Filename containing combined runs')
args = parser.parse_args()
run_outputs = []
for run_dir in args.run_dir:
try:
config = get_config_dictionary(os.path.join(run_dir, 'config.json'))
except FileNotFoundError:
print('{} does not seem to be a sacred run'.format(run_dir))
continue
results = get_results_dictionary(os.path.join(run_dir, 'run.json'))
if results is None:
continue
config = repeat_values(config, config['run.runs'])
joined = config.copy()
joined.update(results)
run_outputs.append(pd.DataFrame.from_dict(joined))
all_runs = pd.concat(run_outputs)
all_runs.to_csv(args.output)