generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
command_line_eval.R
266 lines (202 loc) · 7.16 KB
/
command_line_eval.R
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env Rscript
# This script replicates the functionality of `_targets_eval.R`, but takes in
# command line arguments so that each config in eval_config.yaml can be executed
# in parallel on Azure batch
library(argparser, quietly = TRUE)
library(wweval)
library(ggplot2)
# some functions from plots.R complain about aes() function not existing if we don't load ggplot2
eval_fit <- function(config_index, eval_config_path, output_dir) {
save_object <- function(object_name, output_file_suffix) {
saveRDS(
object = get(object_name),
file = file.path(output_dir, paste0(object_name, output_file_suffix))
)
}
wwinference::create_dir(output_dir)
eval_config <- yaml::read_yaml(eval_config_path)
params <- wwinference::get_params(file.path(
"input", "params.toml"
)) |> as.data.frame()
location <- eval_config$location_ww[config_index]
forecast_date <- eval_config$forecast_date_ww[config_index]
scenario <- eval_config$scenario[config_index]
output_file_suffix <- paste("", location, format(as.Date(forecast_date), "%Y.%m.%d"), scenario,
sep = "_"
)
# Get the evaluation data from the specified evaluation date ----------------
eval_hosp_data <- get_input_hosp_data(
forecast_date_i = eval_config$eval_date,
location_i = unique(eval_config$location_ww),
hosp_data_dir = eval_config$hosp_data_dir,
calibration_time = 365 # Grab sufficient data for eval
)
save_object("eval_hosp_data", output_file_suffix)
eval_ww_data <- get_input_ww_data(
forecast_date_i = eval_config$eval_date,
location_i = unique(eval_config$location_ww),
scenario = "status_quo",
scenario_dir = eval_config$scenario_dir,
ww_data_dir = eval_config$ww_data_dir,
calibration_time = 365, # Grab sufficient data for eval
last_hosp_data_date = eval_config$eval_date,
ww_data_mapping = eval_config$ww_data_mapping
)
save_object("eval_ww_data", output_file_suffix)
stan_model_path_target <- get_model_path(
model_type = "ww",
stan_models_dir = eval_config$stan_models_dir
)
input_hosp_data <- get_input_hosp_data(
forecast_date_i = forecast_date,
location_i = location,
hosp_data_dir = eval_config$hosp_data_dir,
calibration_time = eval_config$calibration_time
)
save_object("input_hosp_data", output_file_suffix)
last_hosp_data_date <- get_last_hosp_data_date(input_hosp_data)
save_object("last_hosp_data_date", output_file_suffix)
input_ww_data <- get_input_ww_data(forecast_date,
location,
scenario,
scenario_dir = eval_config$scenario_dir,
ww_data_dir = eval_config$ww_data_dir,
calibration_time = eval_config$calibration_time,
last_hosp_data_date = last_hosp_data_date,
ww_data_mapping = eval_config$ww_data_mapping
)
save_object("input_ww_data", output_file_suffix)
standata <- get_stan_data_list(
model_type = "ww",
forecast_date, eval_config$forecast_time,
input_ww_data, input_hosp_data,
generation_interval = eval_config$generation_interval,
inf_to_hosp = eval_config$inf_to_hosp,
infection_feedback_pmf = eval_config$infection_feedback_pmf,
params
)
save_object("standata", output_file_suffix)
init_lists <- wweval:::get_inits(
model_type = "ww", standata, params,
n_chains = eval_config$n_chains
)
save_object("init_lists", output_file_suffix)
ww_fit_obj <- wweval::sample_model(
standata,
stan_model_path = stan_model_path_target,
stan_models_dir = eval_config$stan_models_dir,
init_lists,
iter_warmup = eval_config$iter_warmup,
iter_sampling = eval_config$iter_sampling,
adapt_delta = eval_config$adapt_delta,
n_chains = eval_config$n_chains,
max_treedepth = eval_config$max_treedepth,
seed = eval_config$seed
)
save_object("ww_fit_obj", output_file_suffix)
## Post-processing---------------------------------------------------------
ww_raw_draws <- ww_fit_obj$draws
save_object("ww_raw_draws", output_file_suffix)
ww_diagnostics <- ww_fit_obj$diagnostics
save_object("ww_diagnostics", output_file_suffix)
ww_diagnostic_summary <- ww_fit_obj$summary_diagnostics
save_object("ww_diagnostic_summary", output_file_suffix)
errors <- ww_fit_obj$error
save_object("errors", output_file_suffix)
# Get evaluation data from hospital admissions and wastewater
# Join draws with data
hosp_draws <- if (is.null(ww_raw_draws)) {
NULL
} else {
get_model_draws_w_data(
model_output = "hosp",
model_type = "ww",
draws = ww_raw_draws,
forecast_date = forecast_date,
scenario = scenario,
location = location,
input_data = input_hosp_data,
eval_data = eval_hosp_data,
last_hosp_data_date = last_hosp_data_date,
ot = eval_config$calibration_time,
forecast_time = eval_config$forecast_time
)
}
save_object("hosp_draws", output_file_suffix)
ww_draws <- if (is.null(ww_raw_draws)) {
NULL
} else {
get_model_draws_w_data(
model_output = "ww",
model_type = "ww",
draws = ww_raw_draws,
forecast_date = forecast_date,
scenario = scenario,
location = location,
input_data = input_ww_data,
eval_data = eval_ww_data,
last_hosp_data_date = last_hosp_data_date,
ot = eval_config$calibration_time,
forecast_time = eval_config$forecast_time
)
}
save_object("ww_draws", output_file_suffix)
full_hosp_quantiles <-
if (is.null(hosp_draws)) {
NULL
} else {
get_state_level_quantiles(
draws = hosp_draws
)
}
save_object("full_hosp_quantiles", output_file_suffix)
# @TODO Save forecasted hospital quantiles locally as well as via
# targets caching just for backup
plot_hosp_draws <-
if (is.null(full_hosp_quantiles)) {
NULL
} else {
full_hosp_quantiles |>
dplyr::filter(period != "calibration")
}
save_object("plot_hosp_draws", output_file_suffix)
### Plot the draw comparison-------------------------------------
plot_hosp_data_comparison <-
if (is.null(hosp_draws)) {
NULL
} else {
get_plot_hosp_data_comparison(
hosp_draws,
location,
model_type = "ww"
)
}
save_object("plot_hosp_data_comparison", output_file_suffix)
plot_ww_draws <-
if (is.null(ww_draws)) {
NULL
} else {
get_plot_ww_data_comparison(
ww_draws,
location,
model_type = "ww"
)
}
save_object("plot_ww_draws", output_file_suffix)
## Score hospital admissions forecasts----------------------------------
# @TODO save scores locally
hosp_scores <- get_full_scores(hosp_draws, scenario)
save_object("hosp_scores", output_file_suffix)
# Get a subset of samples for plotting
# Get a subset of quantiles for plotting
}
parsed_args <- arg_parser("Run eval pipeline for one config") |>
add_argument("config_index", help = "index of entry in eval_config to use", type = "integer") |>
add_argument("eval_config_path", help = "path to eval_config.yaml") |>
add_argument("output_dir", help = "directory to store output") |>
parse_args()
eval_fit(
config_index = parsed_args$config_index,
eval_config_path = parsed_args$eval_config_path,
output_dir = parsed_args$output_dir
)