generated from AFg6K7h4fhy2/AFg6K7h4fhy2-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
241 lines (216 loc) · 6.7 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""
Loads config (params.toml) for any model.
Authors
-------
118777
Date Created
------------
2024-04-23
Last Updated
------------
2024-04-30
"""
import os
from copy import copy
import toml
from toml.decoder import TomlDecodeError
def quick_load_and_valid_config(
cf_path: str,
) -> dict[str, dict[str, bool | int | float]]:
"""
Loads and validations model params.toml configuration files in a risky
manner, with fewer checks.
Parameters
----------
cf_path : str
The configuration file path
Returns
-------
dict[str, dict[str, bool | int | float]]
A dictionary of config params.
"""
if not os.path.exists(cf_path):
raise ValueError(
f"{cf_path} does not point to a valid configuration file"
)
try:
with open(cf_path, "r") as toml_file:
cf = toml.load(toml_file)
toml_file.close()
except FileNotFoundError:
raise FileNotFoundError(
f"The configuration file was not found at {cf_path}."
)
except TomlDecodeError as e:
raise TomlDecodeError(f"Error decoding the TOML file: {e}")
except Exception as e:
raise Exception(
f"{e}\nThe toml configuration file was unable to be loaded."
)
return cf
def load_and_valid_config(
cf_path: str,
) -> dict[str, dict[str, bool | int | float]]:
"""
Loads and validations model params.toml configuration files
Parameters
----------
cf_path : str
The configuration file path
Returns
-------
dict[str, dict[str, bool | int | float]]
A dictionary of config params.
Notes
-----
Access the configuration parameters in the following manner
cf["<category>"]["<category_config_param"]
"""
cf = quick_load_and_valid_config(cf_path)
# make sure all inputted primary keys are supported
valid_key_combinations = {
"reproducibility": {"seed"},
"data": {
"preliminary",
"start_year",
"forecast_month",
"forecast_state",
"forecast_year",
},
"tornado_parameters": {},
"inference": {
"adapt_delta",
"max_tree_depth",
"num_warmup",
"num_samples",
"num_chains",
"progress_bar",
"print_summary",
"print_post_prior",
"save_obs_summary",
"save_samples",
"save_prior_pred",
"save_post_pred",
},
"output": {
"report",
"show_map_plot",
"show_bar_plots",
"show_bar_plots",
"show_mcmc_trace",
"show_post_pred",
"show_prior_pred",
"show_bayes_plot",
},
}
for primary_key, secondary_keys in cf.items():
if primary_key not in valid_key_combinations:
raise ValueError(
f"The category (primary key) you had ({primary_key}) in your toml file is invalid"
)
for secondary_key in secondary_keys:
if secondary_key not in valid_key_combinations[primary_key]:
raise ValueError(
f"The secondary key ({secondary_key}) you entered in section {primary_key} is not a valid option"
)
# make sure necessary keys combinations are present
necessary_key_combinations = {
"reproducibility": {"seed"},
"data": {
"start_year",
"forecast_month",
"forecast_state",
"forecast_year",
},
"inference": {"num_samples", "num_warmup"},
}
set_necessary = set(
[(k, sk) for k, v in necessary_key_combinations.items() for sk in v]
)
set_cf = set([(k, sk) for k, v in cf.items() for sk in v.keys()])
if not set_necessary.issubset(set_cf):
raise ValueError(
"The following primary keys and their settings are not present in the configuration file you entered, but are necessary :\n{necessary_key_combinations}"
)
# fix non-necessary keys to defaults if not entered
optional_key_defaults = {
"data": {"preliminary": True},
"inference": {
"adapt_delta": 0.85,
"max_tree_depth": 10,
"num_warmup": 100,
"num_samples": 250,
"num_chains": 1,
"progress_bar": True,
"print_summary": True,
"print_post_prior": True,
"save_obs_summary": False,
"save_samples": False,
"save_prior_pred": False,
"save_post_pred": False,
},
"output": {
"report": False,
"show_map_plot": True,
"show_bar_plots": False,
"show_mcmc_trace": True,
"show_post_pred": True,
"show_prior_pred": True,
"show_bayes_plot": True,
},
}
# make sure all necessary keys are inputted
for primary_key, secondary_keys in optional_key_defaults.items():
if primary_key not in cf:
cf[primary_key] = copy(optional_key_defaults[primary_key])
else:
for secondary_key in secondary_keys:
if secondary_key not in cf[primary_key]:
cf[primary_key][secondary_key] = copy(
optional_key_defaults[primary_key][secondary_key]
)
# verify that config parameter values are appropriate and correctly typed
assert isinstance(cf["reproducibility"]["seed"], int), ""
assert 1 <= cf["reproducibility"]["seed"] <= 1000000
assert isinstance(
cf["data"]["preliminary"], bool
), "Preliminary must be of type boolean"
assert isinstance(
cf["data"]["start_year"], int
), "The start year must be any integer."
assert 1 <= cf["data"]["start_year"], "The start year must be positive."
assert isinstance(
cf["data"]["forecast_year"], int
), "The forecast year must be any integer."
assert (
1 <= cf["data"]["forecast_year"]
), "The forecast year must be positive."
return cf
# NOTE: there are missing checks that still need to be completed
# preliminary = true
# start_year = 2019
# forecast_month = "April"
# forecast_state = "US"
# forecast_year = 2024
# [tornado_parameters]
# [inference]
# adapt_delta = 0.85
# max_tree_depth = 12
# num_warmup = 50
# num_samples = 400
# num_chains = 1
# progress_bar = true
# print_summary = true
# print_post_prior = true
# save_obs_summary = false
# save_samples = false
# save_prior_pred = false
# save_post_pred = false
# [output]
# report = true
# show_map_plot = true
# show_bar_plots = false
# show_mcmc_trace = true
# show_prior_pred = true
# show_post_pred = true
# show_bayes_plot = true