-
Notifications
You must be signed in to change notification settings - Fork 1
/
readModelParams.py
45 lines (42 loc) · 1.26 KB
/
readModelParams.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
from modelfunc import myerror
import yaml
def readModelParams(paramsFile, key=None):
"""Parse a model params dict from a yaml file. File can either have a
single unnamed dict:
x: abc
y: xyz
or several:
key1:
x: abc
y: xyc
key2:
var1: 1
var2: 5
In the former case no key is need. In the latter case, if a specific dict
is required, it should be specified using "key", otherwise a dict of dicts
will be returned.
Parameters
----------
paramsFile : str
yaml file with desired model params
key: str [Optional]
key to select which dict to return.
Returns
-------
dict
dict with model params with 'params' added as file it was read from.
"""
if paramsFile is None:
return {}
try:
with open(paramsFile) as fp:
modelParams = yaml.load(fp, Loader=yaml.FullLoader)
# print(modelParams)
if key is not None:
if key in modelParams:
modelParams = modelParams[key]
# Force to use name of file actually being read
modelParams['params'] = paramsFile
except Exception:
myerror(f'Could not open params file: {paramsFile}')
return modelParams