-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_unfold.py
More file actions
executable file
·86 lines (68 loc) · 2.66 KB
/
run_unfold.py
File metadata and controls
executable file
·86 lines (68 loc) · 2.66 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
import os
import sys
import itertools
from util import read_dict_from_json, reportMemUsage
from unfoldv2 import getArgsParser, unfold
from modelUtils import reportGPUMemUsage
import logging
logger = logging.getLogger("run_unfold")
def run_unfold(fpath_run_config):
if not os.path.isfile(fpath_run_config):
raise FileNotFoundError(f"Cannot find run config: {fpath_run_config}")
run_cfgs = read_dict_from_json(fpath_run_config)
if not run_cfgs:
raise RuntimeError(f"Failed to load run config from {fpath_run_config}")
# run_cfgs could be a dictionary or a list of dictionaries
if not isinstance(run_cfgs, list):
run_cfgs = [run_cfgs]
# loop over the list of dict objects
for run_cfg_d in run_cfgs:
# If there is a entry: "skip": true, skip this one
if run_cfg_d.pop("skip", None):
continue
# get the default argument dictionary from getArgsParser
default_args = getArgsParser([])
default_args = vars(default_args)
# Loop over run_cfg_d and replace the default argument values
scan_args = list()
for k, v in run_cfg_d.items():
if isinstance(v, dict):
# We are going to run unfolding with different values of this
# argument handle it later
scan_args.append(k)
elif k in default_args:
default_args[k] = v
else:
raise ValueError(f"{k} is not a known argument")
# for arguments with labels and multiple values
labels = []
for k in scan_args:
assert(isinstance(run_cfg_d[k], dict))
labels.append( list(run_cfg_d[k].keys()) )
combinations = list(itertools.product(*labels))
# for each argument combination
for comb in combinations:
run_args = default_args.copy()
assert( len(comb) == len(scan_args) )
for k, l in zip(scan_args, comb):
run_args[k] = run_cfg_d[k][l]
# update output directory based on the labels
run_args['outputdir'] = os.path.join(run_args['outputdir'], *comb)
print("Run unfolding")
#print(run_args)
unfold(**run_args)
if __name__ == "__main__":
# Usage
if len(sys.argv) != 2:
print("Usage: ./run_unfold.py <run_config.json>")
sys.exit(0)
fpath_run_config = sys.argv[1]
try:
run_unfold(fpath_run_config)
except Exception as ex:
logger.setLevel(logging.DEBUG)
reportMemUsage(logger)
reportGPUMemUsage(logger)
logger.error(f"Unfold failed: {ex}")
sys.exit(1)