|
1 | | -#' Generate text content for forecast hub visualization webpage. |
| 1 | +#' Check hospital reporting data latency and completeness. |
2 | 2 | #' |
3 | | -#' This function generates formatted text summaries for the |
4 | | -#' forecast hub visualization webpage. It processes |
5 | | -#' ensemble forecast data, target hospital admissions data, |
6 | | -#' and contributing team information to create a text |
7 | | -#' description of the current week's hospitalization |
8 | | -#' forecasts. |
| 3 | +#' This function retrieves hospital reporting data from |
| 4 | +#' data.cdc.gov and checks for data latency and reporting |
| 5 | +#' completeness issues. It returns a flag string |
| 6 | +#' describing any reporting issues. |
9 | 7 | #' |
10 | | -#' @param reference_date Character, the reference date for |
11 | | -#' the forecast in YYYY-MM-DD format (ISO-8601). |
| 8 | +#' @param reference_date Date, the reference date for the |
| 9 | +#' forecast. |
12 | 10 | #' @param disease Character, disease name ("covid" or |
13 | | -#' "rsv"). Used to derive hub name, file prefix, and |
14 | | -#' disease display name. |
15 | | -#' @param base_hub_path Character, path to the forecast hub |
16 | | -#' directory. |
17 | | -#' @param hub_reports_path Character, path to forecast hub |
18 | | -#' reports directory. |
| 11 | +#' "rsv"). |
19 | 12 | #' @param excluded_locations Character vector of location |
20 | | -#' codes to exclude from reporting calculations. Default: |
21 | | -#' character(0). |
| 13 | +#' codes to exclude. Default: character(0). |
22 | 14 | #' |
23 | | -#' @export |
24 | | -get_webtext <- function( |
| 15 | +#' @return Character string describing reporting issues, |
| 16 | +#' or empty string if no issues. |
| 17 | +check_hospital_reporting_latency <- function( |
25 | 18 | reference_date, |
26 | 19 | disease, |
27 | | - base_hub_path, |
28 | | - hub_reports_path, |
29 | 20 | excluded_locations = character(0) |
30 | 21 | ) { |
31 | | - checkmate::assert_choice(disease, choices = c("covid", "rsv")) |
32 | | - |
33 | | - reference_date <- lubridate::as_date(reference_date) |
34 | | - |
35 | | - hub_name <- get_hub_name(disease) |
36 | | - disease_name <- dplyr::case_match( |
37 | | - disease, |
38 | | - "covid" ~ "COVID-19", |
39 | | - "rsv" ~ "RSV" |
40 | | - ) |
41 | | - |
42 | | - weekly_data_path <- fs::path( |
43 | | - hub_reports_path, |
44 | | - "weekly-summaries", |
45 | | - reference_date |
46 | | - ) |
47 | | - |
48 | | - # could possibly use write_ref_date_summary_ensemble() or |
49 | | - # summarize_ref_date_forecasts()? |
50 | | - ensemble_us_1wk_ahead <- forecasttools::read_tabular( |
51 | | - fs::path( |
52 | | - weekly_data_path, |
53 | | - glue::glue("{reference_date}_{disease}_map_data"), |
54 | | - ext = "csv" |
55 | | - ) |
56 | | - ) |> |
57 | | - dplyr::filter(horizon == 1, location_name == "US") |
58 | | - |
59 | | - target_data <- forecasttools::read_tabular( |
60 | | - fs::path( |
61 | | - weekly_data_path, |
62 | | - glue::glue( |
63 | | - "{reference_date}_{disease}_target_hospital_admissions_data" |
64 | | - ), |
65 | | - ext = "csv" |
66 | | - ) |
67 | | - ) |
68 | | - |
69 | | - contributing_teams <- forecasttools::read_tabular( |
70 | | - fs::path( |
71 | | - weekly_data_path, |
72 | | - glue::glue("{reference_date}_{disease}_forecasts_data"), |
73 | | - ext = "csv" |
74 | | - ) |
75 | | - ) |> |
76 | | - dplyr::filter(model != glue::glue("{hub_name}-ensemble")) |> |
77 | | - dplyr::pull(model) |> |
78 | | - unique() |
79 | | - |
80 | | - wkly_submissions <- hubData::load_model_metadata( |
81 | | - base_hub_path, |
82 | | - model_ids = contributing_teams |
83 | | - ) |> |
84 | | - dplyr::distinct(.data$model_id, .data$designated_model, .keep_all = TRUE) |> |
85 | | - dplyr::mutate( |
86 | | - team_model_url = glue::glue( |
87 | | - "[{team_name} (Model: {model_abbr})]({website_url})" |
88 | | - ) |
89 | | - ) |> |
90 | | - dplyr::select( |
91 | | - model_id, |
92 | | - team_abbr, |
93 | | - model_abbr, |
94 | | - team_model_url, |
95 | | - designated_model |
96 | | - ) |
97 | | - |
98 | 22 | desired_weekendingdate <- as.Date(reference_date) - lubridate::dweeks(1) |
99 | 23 |
|
100 | 24 | disease_abbr <- dplyr::case_match( |
@@ -183,6 +107,111 @@ get_webtext <- function( |
183 | 107 | "" |
184 | 108 | } |
185 | 109 |
|
| 110 | + return(reporting_rate_flag) |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +#' Generate forecast hub webpage text block. |
| 115 | +#' |
| 116 | +#' This function creates formatted text content for |
| 117 | +#' forecast hub visualizations. It processes forecast |
| 118 | +#' data, target data, and team metadata to generate a |
| 119 | +#' text description. |
| 120 | +#' |
| 121 | +#' @param reference_date Character, the reference date for |
| 122 | +#' the forecast in YYYY-MM-DD format (ISO-8601). |
| 123 | +#' @param disease Character, disease name ("covid" or |
| 124 | +#' "rsv"). |
| 125 | +#' @param base_hub_path Character, path to the forecast |
| 126 | +#' hub directory. |
| 127 | +#' @param hub_reports_path Character, path to forecast |
| 128 | +#' hub reports directory. |
| 129 | +#' @param excluded_locations Character vector of location |
| 130 | +#' codes to exclude. Default: character(0). |
| 131 | +#' |
| 132 | +#' @return Character string containing the formatted |
| 133 | +#' webpage text. |
| 134 | +#' @export |
| 135 | +generate_webtext_block <- function( |
| 136 | + reference_date, |
| 137 | + disease, |
| 138 | + base_hub_path, |
| 139 | + hub_reports_path, |
| 140 | + excluded_locations = character(0) |
| 141 | +) { |
| 142 | + checkmate::assert_choice(disease, choices = c("covid", "rsv")) |
| 143 | + |
| 144 | + reference_date <- lubridate::as_date(reference_date) |
| 145 | + |
| 146 | + hub_name <- get_hub_name(disease) |
| 147 | + disease_name <- dplyr::case_match( |
| 148 | + disease, |
| 149 | + "covid" ~ "COVID-19", |
| 150 | + "rsv" ~ "RSV" |
| 151 | + ) |
| 152 | + |
| 153 | + weekly_data_path <- fs::path( |
| 154 | + hub_reports_path, |
| 155 | + "weekly-summaries", |
| 156 | + reference_date |
| 157 | + ) |
| 158 | + |
| 159 | + # could possibly use write_ref_date_summary_ensemble() or |
| 160 | + # summarize_ref_date_forecasts()? |
| 161 | + ensemble_us_1wk_ahead <- forecasttools::read_tabular( |
| 162 | + fs::path( |
| 163 | + weekly_data_path, |
| 164 | + glue::glue("{reference_date}_{disease}_map_data"), |
| 165 | + ext = "csv" |
| 166 | + ) |
| 167 | + ) |> |
| 168 | + dplyr::filter(horizon == 1, location_name == "US") |
| 169 | + |
| 170 | + target_data <- forecasttools::read_tabular( |
| 171 | + fs::path( |
| 172 | + weekly_data_path, |
| 173 | + glue::glue( |
| 174 | + "{reference_date}_{disease}_target_hospital_admissions_data" |
| 175 | + ), |
| 176 | + ext = "csv" |
| 177 | + ) |
| 178 | + ) |
| 179 | + |
| 180 | + contributing_teams <- forecasttools::read_tabular( |
| 181 | + fs::path( |
| 182 | + weekly_data_path, |
| 183 | + glue::glue("{reference_date}_{disease}_forecasts_data"), |
| 184 | + ext = "csv" |
| 185 | + ) |
| 186 | + ) |> |
| 187 | + dplyr::filter(model != glue::glue("{hub_name}-ensemble")) |> |
| 188 | + dplyr::pull(model) |> |
| 189 | + unique() |
| 190 | + |
| 191 | + wkly_submissions <- hubData::load_model_metadata( |
| 192 | + base_hub_path, |
| 193 | + model_ids = contributing_teams |
| 194 | + ) |> |
| 195 | + dplyr::distinct(.data$model_id, .data$designated_model, .keep_all = TRUE) |> |
| 196 | + dplyr::mutate( |
| 197 | + team_model_url = glue::glue( |
| 198 | + "[{team_name} (Model: {model_abbr})]({website_url})" |
| 199 | + ) |
| 200 | + ) |> |
| 201 | + dplyr::select( |
| 202 | + model_id, |
| 203 | + team_abbr, |
| 204 | + model_abbr, |
| 205 | + team_model_url, |
| 206 | + designated_model |
| 207 | + ) |
| 208 | + |
| 209 | + reporting_rate_flag <- check_hospital_reporting_latency( |
| 210 | + reference_date = reference_date, |
| 211 | + disease = disease, |
| 212 | + excluded_locations = excluded_locations |
| 213 | + ) |
| 214 | + |
186 | 215 | round_to_place <- function(value) { |
187 | 216 | if (value >= 1000) { |
188 | 217 | rounded_val <- round(value, -2) |
@@ -266,6 +295,53 @@ get_webtext <- function( |
266 | 295 | "{paste(model_not_incl_in_hub_ensemble, collapse = '\n')}" |
267 | 296 | ) |
268 | 297 |
|
| 298 | + return(web_text) |
| 299 | +} |
| 300 | + |
| 301 | + |
| 302 | +#' Generate and save text content for forecast hub |
| 303 | +#' visualization webpage. |
| 304 | +#' |
| 305 | +#' Light wrapper function that generates formatted text |
| 306 | +#' summaries and saves them to disk. |
| 307 | +#' |
| 308 | +#' @param reference_date Character, the reference date for |
| 309 | +#' the forecast in YYYY-MM-DD format (ISO-8601). |
| 310 | +#' @param disease Character, disease name ("covid" or |
| 311 | +#' "rsv"). Used to derive hub name, file prefix, and |
| 312 | +#' disease display name. |
| 313 | +#' @param base_hub_path Character, path to the forecast hub |
| 314 | +#' directory. |
| 315 | +#' @param hub_reports_path Character, path to forecast hub |
| 316 | +#' reports directory. |
| 317 | +#' @param excluded_locations Character vector of location |
| 318 | +#' codes to exclude from reporting calculations. Default: |
| 319 | +#' character(0). |
| 320 | +#' |
| 321 | +#' @export |
| 322 | +get_webtext <- function( |
| 323 | + reference_date, |
| 324 | + disease, |
| 325 | + base_hub_path, |
| 326 | + hub_reports_path, |
| 327 | + excluded_locations = character(0) |
| 328 | +) { |
| 329 | + reference_date <- lubridate::as_date(reference_date) |
| 330 | + |
| 331 | + web_text <- generate_webtext_block( |
| 332 | + reference_date = reference_date, |
| 333 | + disease = disease, |
| 334 | + base_hub_path = base_hub_path, |
| 335 | + hub_reports_path = hub_reports_path, |
| 336 | + excluded_locations = excluded_locations |
| 337 | + ) |
| 338 | + |
| 339 | + weekly_data_path <- fs::path( |
| 340 | + hub_reports_path, |
| 341 | + "weekly-summaries", |
| 342 | + reference_date |
| 343 | + ) |
| 344 | + |
269 | 345 | output_filepath <- fs::path( |
270 | 346 | weekly_data_path, |
271 | 347 | glue::glue("{reference_date}_webtext"), |
|
0 commit comments