-
Notifications
You must be signed in to change notification settings - Fork 2
/
paramObj.py
65 lines (54 loc) · 2.31 KB
/
paramObj.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
#!/usr/bin/env python
import numpy as np
from ruamel.yaml import YAML
from copy import deepcopy
from pathlib import Path
# https://stackoverflow.com/questions/36831998/how-to-fill-default-parameters-in-yaml-file-using-python
def setdefault_recursively(tgt, default):
for k in default:
if isinstance(default[k], dict): # if the current item is a dict,
# expand it recursively
setdefault_recursively(tgt.setdefault(k, {}), default[k])
elif isinstance(tgt, dict):
# ... otherwise simply set a default value if it's not set before
tgt.setdefault(k, default[k])
# https://stackoverflow.com/a/38034502
class AttrDict(dict):
""" Dictionary subclass whose entries can be accessed by attributes
(as well as normally).
"""
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self
@staticmethod
def from_nested_dict(data):
""" Construct nested AttrDicts from nested dictionaries. """
if not isinstance(data, dict):
return data
else:
return AttrDict({key: AttrDict.from_nested_dict(data[key])
for key in data})
class Config(AttrDict):
"""
# Initialize as:
param = yamlclass('param.yml')
# Calling parameters as:
print(param.FileName)
print(param.ChiFit.Ntries)
# These values have been replaced with defaults if the user did not input them
# The actual user input is still available from param.usercfg.
"""
def __init__(self,paramyml, defaults=Path(__file__).parent/'defaults.yml'):
yaml=YAML(typ='safe')
with open(paramyml,'r') as ymlfile:
cfg = yaml.load(ymlfile)
with open(defaults,'r') as ymlfile:
defaults = yaml.load(ymlfile)
usercfg = deepcopy(cfg)
setdefault_recursively(cfg, default=defaults)
# Input validation can happen here
assert 'FileName' in usercfg, 'Provide observation parameters'
assert 'Output' in usercfg, 'Provide output information'
super(Config, self).__init__(AttrDict.from_nested_dict(cfg)) # Magic...
self.usercfg = AttrDict.from_nested_dict(usercfg)
self.defaults= AttrDict.from_nested_dict(defaults)