-
Notifications
You must be signed in to change notification settings - Fork 10
/
workflow.R
249 lines (212 loc) · 7.01 KB
/
workflow.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
## set commit to FALSE and commit manually once you are happy with the
## outputs
basic_workflow <- function(week, use_draft = "newer", commit = FALSE) {
parameter <- list(week_ending = week)
message("Preparing data for week ", week)
m1 <- orderly_run("prepare_ecdc_data", parameters = parameter)
if (commit) orderly_commit(m1)
message("Running Model 1, this will take long.")
parameter <- list(week_ending = week, short_run = FALSE)
m1 <- orderly_run(
"run_rti0", parameters = parameter, use_draft = use_draft
)
if (commit) orderly_commit(m1)
message("Running Model 2.")
parameter <- list(week_ending = week)
m2 <- orderly_run(
"run_apeestim",
parameters = parameter, use_draft = use_draft
)
if (commit) orderly_commit(m2)
message("Running Model 3.")
m3 <- orderly_run(
"DeCa_model",
parameters = parameter, use_draft = use_draft
)
if (commit) orderly_commit(m3)
indv <- orderly_run(
"process_individual_models",
parameters = parameter, use_draft = use_draft
)
if (commit) orderly_commit(indv)
unwtd <- orderly_run(
"produce_ensemble_outputs",
parameters = parameter, use_draft = use_draft
)
if (commit) orderly_commit(unwtd)
}
## Performance assessment in week N requires observations for week N + 1
## to be available. That is why these tasks cannot be run with the
## weekly workflow
performance_workflow <- function(week, use_draft = "newer", commit = FALSE) {
message("Performance metrics for ensemble model; week = ", week)
x <- dependencies_ensb_performance(week)
con <- file(
here::here("src/produce_performance_metrics_ensemble/orderly.yml"),
"w"
)
yaml::write_yaml(x, con)
close(con)
parameter <- list(week_ending = as.character(week), window = 1)
m1 <- orderly_run(
"produce_performance_metrics_ensemble",
parameters = parameter, use_draft = use_draft
)
if (commit) orderly_commit(m1)
x <- dependencies_indvdl_performance(week)
con <- file(
here::here("src/produce_performace_metrics/orderly.yml"),
"w"
)
yaml::write_yaml(x, con)
close(con)
m2 <- orderly_run(
"produce_performace_metrics",
parameters = parameter, use_draft = use_draft
)
}
## These functions have not been configured to pull in week-specific
## outputs, they will always pull in the latest runs of dependancies.
## This is run locally rather than on the server.
report_workflow <- function(week, use_draft = "newer", commit = FALSE) {
orderly_pull_dependencies(
"format_model_outputs", parameter = list(week_ending = week)
)
a <- orderly_run(
"format_model_outputs",
use_draft = use_draft, parameter = list(week_ending = week)
)
if (commit) orderly_commit(a)
## This gives an error on the server, don't run it there,
a <- orderly_run(
"produce_maps",
use_draft = use_draft, parameter = list(week_ending = week)
)
if (commit) orderly_commit(a)
source("orderly-helper-scripts/dependencies_collate_retrospective_vis.R")
# The produce_retrosepctive_vis task may not run if you don't have some of the historic reports
# But can run on server
# First push the orderly.yml generated by the source command above to repo
# then pull latest branch on server. And then...
# orderly run produce_retrospective_vis week_ending=2021-12-05 (NB: put latest date here)
orderly_run(
# orderly_pull_archive(
"produce_retrospective_vis",
parameter = list(week_ending = week)#, use_draft = use_draft
)
if (commit) orderly_commit(a)
a <- orderly_run(
"produce_visualisations",
parameters = list(week_ending = week), use_draft = use_draft
)
if (commit) orderly_commit(a)
a <- orderly_run(
"produce_full_report", use_draft = use_draft,
parameter = list(week_ending = week, week_in_words = "30^th^ May 2022")
)
if (commit) orderly_commit(a)
## Uses dir structure specific to my
## machine, you will have to edit it.
## For Europe hub
orderly_run(
"reformat_forecasts", use_draft = use_draft,
parameter = list(week_ending = week)
)
## for MRIIDS. After running this task
## you will have to use AWS CLI or an FTP client
## to push outputs to mriids server.
orderly_run(
"prepare_hm_outputs", use_draft = use_draft,
parameter = list(week_ending = week)
)
}
## Collate week specific outputs
collation_workflow <- function(weeks, use_draft = "newer", commit = FALSE) {
source("orderly-helper-scripts/dependencies_collate_model_outputs.R")
a <- orderly_run("collate_model_outputs", use_draft = use_draft)
if (commit) orderly_commit(a)
##weeks <- head(weeks, -1)
source(
"orderly-helper-scripts/dependencies_collate_weighted_perf.R"
)
m1 <- orderly_run(
"collate_weighted_performance_metrics", use_draft = use_draft
)
if (commit) orderly_commit(m1)
}
post_collation_workflow <- function(latest_week, use_draft = "newer", commit = FALSE) {
a <- orderly_run(
"produce_performance_metrics_vis",
use_draft = use_draft,
parameters = list(use_si = "si_2")
)
if (commit) orderly_commit(a)
## x <- collated_outputs_viz(latest_week)
## con <- file(
## here::here("src/compare_ensemble_outputs/orderly.yml"), "w"
## )
## yaml::write_yaml(x, con)
## close(con)
a <- orderly_run(
"compare_ensemble_outputs",
use_draft = use_draft,
parameters = list(use_si = "si_2", latest_week = latest_week)
)
if (commit) orderly_commit(a)
}
library(purrr)
library(glue)
library(orderly)
source("orderly-helper-scripts/dependencies_weighted_performance.R")
source("orderly-helper-scripts/dependencies_collated_outputs_viz.R")
week <- "2022-06-05"
use_draft <- "newer"
## This is the sunday before the Monday for which we are producing
## the report.
weeks <- seq(
from = as.Date("2020-03-08"),
##to = as.Date("2021-03-01"),
to = as.Date("2020-11-29"),
by = "7 days"
)
weeks <- as.character(weeks)
purrr::walk(weeks, function(x) basic_workflow(x))
purrr::walk(head(weeks, -1), function(x) performance_workflow(x))
collation_workflow(weeks)
### Other tasks that need to be run with the latest data
orderly_run(
"produce_baseline_error",
use_draft = "newer",
parameters = list(week_starting = head(weeks, 1),
latest_week = tail(weeks, 1))
)
orderly_run(
"compare_with_null_model",
use_draft = "newer", parameters = list(use_si = "si_2")
)
post_collation_workflow(tail(weeks, 1))
## To run on server, one-off scripts
writeLines(
sprintf("orderly run prepare_ecdc_data week_ending=%s", weeks),
"runs-20210428.sh"
)
## baseline error only needs observed data
writeLines(
sprintf("orderly run produce_baseline_error week_starting=2020-02-22 latest_week=%s", weeks),
"runs-20210428.sh"
)
writeLines(
sprintf("orderly run run_rti0 short_run=FALSE week_ending=%s", weeks),
"runs-20210428.sh"
)
outfile <- "performance-metrics.sh"
for (week in weeks) {
cat(
sprintf("\n Rscript orderly-helper-scripts/dependencies_weighted_performance.R %s", week),
file = outfile, append = TRUE
)
cat(
sprintf("\n orderly run produce_performace_metrics window=1 week_ending=%s", week),
file = outfile, append = TRUE
)
}