-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.py
118 lines (95 loc) · 3.36 KB
/
config.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import sys
import yaml
import os
class Config:
variable_names = ('a', 'b', 'Dc', 'mu0', 's')
def __init__(self, path=None):
self.samplename = None
self.mintime = None
self.maxtime = None
self.mindisp = None
self.maxdisp = None
self.section_id = None
self.k = None
self.lc = None
self.rootpath = os.path.join(os.path.expanduser('~'), 'PycharmProjects', 'mcmcrsf_xfiles')
self.vel_windowlen = None
self.filter_windowlen = None
self.q = None
self.ndr = None
self.nch = None
self.ntune = None
self.ncores = None
self.sim_name = None
self.mu_sim = None
self.threshold = None
self.input_data_dir = None
self.output_mcmc_dir = None
self.mcmc_out_dirname = None
if path is None:
path = 'config.yaml'
self._load_from_file(path)
def _load_from_file(self, path):
if not os.path.isfile(path):
print(f'Warning, {path} is not a path')
sys.exit()
with open(path, 'r') as rfile:
cfg = yaml.safe_load(rfile)
attrs = ('samplename', 'section_id', 'mintime', 'maxtime',
'k', 'lc', 'vel_windowlen', 'filter_windowlen', 'q',
'ndr', 'nch', 'ntune', 'ncores', 'threshold',
'input_data_dir', 'output_mcmc_dirname',
)
for a in attrs:
if a not in cfg:
print(f'Warning: {a} not in cfg')
print(cfg)
sys.exit()
setattr(self, a, cfg.get(a))
self.sim_name = f'out_{self.ndr}d{self.nch}ch_{self.section_id}'
# load priors
for p in self.variable_names:
p = f'{p}_prior'
setattr(self, p, Prior(cfg.get(p)))
self._create_directory(cfg['output_mcmc_dirname'])
def _create_directory(self, dname):
p = self.make_path(dname, self.samplename, self.sim_name)
if not os.path.exists(p):
print(f'Creating output directory: {p}')
os.makedirs(p)
print(f'Directory exists. Any existing files from previous runs will be '
f'overwritten: {p}')
self.mcmc_out_dir = p
def set_disp_bounds(self, x):
self.mindisp = x[0]
self.maxdisp = x[-1]
def get_prior_parameters(self):
# prior parameters for a, b, Dc, mu0, s (in that order)
mus = []
sigmas = []
alphas = []
betas = []
dist_types = []
for a in self.variable_names:
prior = getattr(self, f'{a}_prior')
mus.append(prior.mu)
sigmas.append(prior.sigma)
alphas.append(prior.alpha)
betas.append(prior.beta)
dist_types.append(prior.dist_type)
return mus, sigmas, alphas, betas, dist_types
def make_path(self, *args):
return os.path.join(self.rootpath, *args)
class Prior:
def __init__(self, obj):
try:
self.dist_type = obj['dist_type']
self.mu = obj['mu']
self.sigma = obj['sigma']
self.alpha = obj['alpha']
self.beta = obj['beta']
except KeyError as e:
print('Invalid prior.')
print(e)
sys.exit()
cfig = Config()