-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd_utils.py
62 lines (54 loc) · 1.97 KB
/
cmd_utils.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
"""
command-line argument parsing utilities
Example:
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, help='Config file')
parser.add_argument('--dataset', choices=['8gaussians', 'checkerboard', 'swissroll'], default='8gaussians', help='Dataset to use')
parser.add_argument('--run', default=None, type=str, help='Run name')
parser.add_argument('--device', default=0, type=str, help='Device to use')
args, unknown = parser.parse_known_args()
d_cmd_cfg = parse_unknown_args(unknown)
d_cmd_cfg = parse_nested_args(d_cmd_cfg)
"""
def parse_arg_type(val):
if val.isnumeric():
return int(val)
try:
return float(val)
except ValueError:
if val.lower() == 'true':
return True
elif val.lower() == 'false':
return False
elif val.lower() == 'null' or val.lower() == 'none':
return None
elif val.startswith('[') and val.endswith(']'): # parse list
return eval(val)
return val
def parse_unknown_args(l_args):
"""convert the list of unknown args into dict
this does similar stuff to OmegaConf.from_cli()
I may have invented the wheel again..."""
n_args = len(l_args) // 2
kwargs = {}
for i_args in range(n_args):
key = l_args[i_args*2]
val = l_args[i_args*2 + 1]
assert '=' not in key, 'optional arguments should be separated by space'
kwargs[key.strip('-')] = parse_arg_type(val)
return kwargs
def parse_nested_args(d_cmd_cfg):
"""produce a nested dictionary by parsing dot-separated keys
e.g. {key1.key2 : 1} --> {key1: {key2: 1}}"""
d_new_cfg = {}
for key, val in d_cmd_cfg.items():
l_key = key.split('.')
d = d_new_cfg
for i_key, each_key in enumerate(l_key):
if i_key == len(l_key) - 1:
d[each_key] = val
else:
if each_key not in d:
d[each_key] = {}
d = d[each_key]
return d_new_cfg