-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfg.py
More file actions
48 lines (41 loc) · 1.45 KB
/
cfg.py
File metadata and controls
48 lines (41 loc) · 1.45 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
import os
import re
from omegaconf import OmegaConf
def parse_cfg(cfg_path: str) -> OmegaConf:
"""Parses a config file and returns an OmegaConf object."""
base = OmegaConf.load(cfg_path / 'default.yaml')
cli = OmegaConf.from_cli()
for k,v in cli.items():
if v == None:
cli[k] = True
base.merge_with(cli)
# Modality config
if cli.get('modality', base.modality) not in {'state', 'pixels'}:
raise ValueError('Invalid modality: {}'.format(cli.get('modality', base.modality)))
modality = cli.get('modality', base.modality)
if modality != 'state':
mode = OmegaConf.load(cfg_path / f'{modality}.yaml')
base.merge_with(mode, cli)
# Task config
try:
domain, task = base.task.split('-', 1)
except:
raise ValueError(f'Invalid task name: {base.task}')
domain_path = cfg_path / 'tasks' / f'{domain}.yaml'
if not os.path.exists(domain_path):
domain_path = cfg_path / 'tasks' / 'default.yaml'
domain_cfg = OmegaConf.load(domain_path)
base.merge_with(domain_cfg, cli)
# Algebraic expressions
for k,v in base.items():
if isinstance(v, str):
match = re.match(r'(\d+)([+\-*/])(\d+)', v)
if match:
base[k] = eval(match.group(1) + match.group(2) + match.group(3))
if isinstance(base[k], float) and base[k].is_integer():
base[k] = int(base[k])
# Convenience
base.task_title = base.task.replace('-', ' ').title()
base.device = 'cuda' if base.modality == 'state' else 'cpu'
base.exp_name = str(base.get('exp_name', 'default'))
return base