Skip to content

Commit 2cb56f2

Browse files
committed
feat: add scoring to covid prod
1 parent f2f56c2 commit 2cb56f2

9 files changed

+642
-243
lines changed

R/aux_data_utils.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,12 @@ create_nhsn_data_archive <- function(disease_name) {
719719
as_epi_archive(compactify = TRUE)
720720
}
721721

722-
723722
up_to_date_nssp_state_archive <- function(disease = c("covid", "influenza")) {
724723
disease <- arg_match(disease)
725-
nssp_state <- pub_covidcast(
724+
nssp_state <- retry_fn(
725+
max_attempts = 10,
726+
wait_seconds = 1,
727+
fn = pub_covidcast,
726728
source = "nssp",
727729
signal = glue::glue("pct_ed_visits_{disease}"),
728730
time_type = "week",

R/utils.R

+38-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ data_substitutions <- function(dataset, disease, forecast_generation_date) {
162162
}
163163

164164
parse_prod_weights <- function(filename = here::here("covid_geo_exclusions.csv"),
165-
forecast_date_int, forecaster_fns) {
165+
forecast_date_int, forecaster_fn_names) {
166166
forecast_date <- as.Date(forecast_date_int)
167167
all_states <- c(
168168
unique(readr::read_csv("https://raw.githubusercontent.com/cmu-delphi/covidcast-indicators/refs/heads/main/_delphi_utils_python/delphi_utils/data/2020/state_pop.csv", show_col_types = FALSE)$state_id),
@@ -182,7 +182,7 @@ parse_prod_weights <- function(filename = here::here("covid_geo_exclusions.csv")
182182
) %>%
183183
unnest_longer(geo_value) %>%
184184
mutate(
185-
forecaster = ifelse(forecaster == "all", list(names(forecaster_fns)), forecaster),
185+
forecaster = ifelse(forecaster == "all", list(forecaster_fn_names), forecaster),
186186
) %>%
187187
unnest_longer(forecaster) %>%
188188
group_by(forecast_date, forecaster, geo_value) %>%
@@ -472,3 +472,39 @@ get_recent_targets_errors <- function(time_since = minutes(60)) {
472472

473473
return(invisible(meta_df %>% filter(time > Sys.time() - time_since)))
474474
}
475+
476+
#' Retry a function.
477+
#'
478+
#' @param max_attempts The maximum number of attempts.
479+
#' @param wait_seconds The number of seconds to wait between attempts.
480+
#' @param fn The function to retry.
481+
#' @param ... Additional arguments to pass to the function.
482+
#'
483+
#' @examples
484+
#' retry_fn(
485+
#' max_attempts = 10,
486+
#' wait_seconds = 1,
487+
#' fn = pub_covidcast,
488+
#' source = "nssp",
489+
#' signal = "pct_ed_visits_covid",
490+
#' geo_type = "state",
491+
#' geo_values = "*",
492+
#' time_type = "week"
493+
#' )
494+
retry_fn <- function(max_attempts = 10, wait_seconds = 1, fn, ...) {
495+
for (attempt in 1:max_attempts) {
496+
tryCatch(
497+
{
498+
result <- fn(...)
499+
return(result) # Return successful result
500+
},
501+
error = function(e) {
502+
if (attempt == max_attempts) {
503+
stop("Maximum retry attempts reached. Last error: ", e$message)
504+
}
505+
message(sprintf("Attempt %d failed. Retrying in %d second(s)...", attempt, wait_seconds))
506+
Sys.sleep(wait_seconds)
507+
}
508+
)
509+
}
510+
}

_targets.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ covid_hosp_explore:
22
script: scripts/covid_hosp_explore.R
33
store: covid_hosp_explore
44
use_crew: no
5-
error: null
5+
reporter_make: timestamp
66
flu_hosp_explore:
77
script: scripts/flu_hosp_explore.R
88
store: flu_hosp_explore
99
use_crew: no
10+
reporter_make: timestamp
1011
flu_hosp_tiny:
1112
script: scripts/flu_hosp_tiny.R
1213
store: flu_hosp_tiny
@@ -15,7 +16,9 @@ flu_hosp_prod:
1516
script: scripts/flu_hosp_prod.R
1617
store: flu_hosp_prod
1718
use_crew: no
19+
reporter_make: timestamp
1820
covid_hosp_prod:
1921
script: scripts/covid_hosp_prod.R
2022
store: covid_hosp_prod
2123
use_crew: no
24+
reporter_make: timestamp

scripts/covid_hosp_explore.R

+20-5
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ data_targets <- list2(
235235
tar_target(
236236
name = hhs_evaluation_data,
237237
command = {
238-
epidatr::pub_covidcast(
238+
retry_fn(
239+
max_attempts = 10,
240+
wait_seconds = 1,
241+
fn = pub_covidcast,
239242
source = "hhs",
240243
signals = hhs_signal,
241244
geo_type = "state",
@@ -262,15 +265,21 @@ data_targets <- list2(
262265
tar_target(
263266
name = nssp_archive,
264267
command = {
265-
nssp_state <- pub_covidcast(
268+
nssp_state <- retry_fn(
269+
max_attempts = 10,
270+
wait_seconds = 1,
271+
fn = pub_covidcast,
266272
source = "nssp",
267273
signal = "pct_ed_visits_covid",
268274
time_type = "week",
269275
geo_type = "state",
270276
geo_values = "*",
271277
fetch_args = epidatr::fetch_args_list(timeout_seconds = 400)
272278
)
273-
nssp_hhs <- pub_covidcast(
279+
nssp_hhs <- retry_fn(
280+
max_attempts = 10,
281+
wait_seconds = 1,
282+
fn = pub_covidcast,
274283
source = "nssp",
275284
signal = "pct_ed_visits_covid",
276285
time_type = "week",
@@ -297,14 +306,20 @@ data_targets <- list2(
297306
# source going down completely, which means we're actually just comparing
298307
# with the version without this source
299308
all_of_them <- lapply(used_searches, \(search_name) {
300-
google_symptoms_state_archive <- pub_covidcast(
309+
google_symptoms_state_archive <- retry_fn(
310+
max_attempts = 10,
311+
wait_seconds = 1,
312+
fn = pub_covidcast,
301313
source = "google-symptoms",
302314
signal = glue::glue("s0{search_name}_smoothed_search"),
303315
time_type = "day",
304316
geo_type = "state",
305317
geo_values = "*"
306318
)
307-
google_symptoms_hhs_archive <- pub_covidcast(
319+
google_symptoms_hhs_archive <- retry_fn(
320+
max_attempts = 10,
321+
wait_seconds = 1,
322+
fn = pub_covidcast,
308323
source = "google-symptoms",
309324
signal = glue::glue("s0{search_name}_smoothed_search"),
310325
time_type = "day",

0 commit comments

Comments
 (0)