-
Notifications
You must be signed in to change notification settings - Fork 0
Add function to update hub format target data #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f2008b1
add update_hub_target_data
sbidari 7a020d2
add a test
sbidari 933e171
clean up
sbidari 87bd8bc
add r-cmd-check and test-coverage
sbidari 0f0ef5a
Merge branch 'main' into sb-r-package-setup
sbidari 103f810
pre-commit fixes
sbidari 2c3f578
update r-cmd-check
sbidari 7be562c
copilot suggestions
sbidari 46b5271
update comment prefix
sbidari 5169cfe
Apply suggestions from code review
sbidari 1c0bdc6
Apply suggestions from code review
sbidari 8ef1e27
use withr
sbidari 9fbf4a4
review suggestions
sbidari 9ef81aa
use httptest2
sbidari 328fc32
pre-commit fix, update DESC
sbidari 861f4b2
save excluded locations as constant
sbidari fab48a4
Apply suggestions from code review
sbidari cd6e981
update man page
sbidari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| ^LICENSE\.md$ | ||
| ^\.github$ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| *.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples | ||
| # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help | ||
| name: R-CMD-check.yaml | ||
|
|
||
| on: | ||
| pull_request: | ||
| push: | ||
| branches: [main] | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.head_ref || github.ref }} | ||
| cancel-in-progress: true | ||
|
|
||
| permissions: read-all | ||
|
|
||
| jobs: | ||
| R-CMD-check: | ||
| runs-on: ${{ matrix.config.os }} | ||
|
|
||
| name: ${{ matrix.config.os }} (${{ matrix.config.r }}) | ||
|
|
||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| config: | ||
| - {os: macos-latest, r: 'release'} | ||
| - {os: windows-latest, r: 'release'} | ||
| - {os: ubuntu-latest, r: 'release'} | ||
|
|
||
| env: | ||
| GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} | ||
| R_KEEP_PKG_SOURCE: yes | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: r-lib/actions/setup-pandoc@v2 | ||
|
|
||
| - uses: r-lib/actions/setup-r@v2 | ||
| with: | ||
| r-version: ${{ matrix.config.r }} | ||
| http-user-agent: ${{ matrix.config.http-user-agent }} | ||
| use-public-rspm: true | ||
|
|
||
| - uses: r-lib/actions/setup-r-dependencies@v2 | ||
| with: | ||
| extra-packages: any::rcmdcheck | ||
| needs: check | ||
|
|
||
| - uses: r-lib/actions/check-r-package@v2 | ||
| with: | ||
| upload-snapshots: true | ||
| build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples | ||
| # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help | ||
| on: | ||
| push: | ||
| branches: [main, master] | ||
| pull_request: | ||
|
|
||
| name: test-coverage.yaml | ||
|
|
||
| permissions: read-all | ||
|
|
||
| jobs: | ||
| test-coverage: | ||
| runs-on: ubuntu-latest | ||
| env: | ||
| GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: r-lib/actions/setup-r@v2 | ||
| with: | ||
| use-public-rspm: true | ||
|
|
||
| - uses: r-lib/actions/setup-r-dependencies@v2 | ||
| with: | ||
| extra-packages: any::covr, any::xml2 | ||
| needs: coverage | ||
|
|
||
| - name: Test coverage | ||
| run: | | ||
| cov <- covr::package_coverage( | ||
| quiet = FALSE, | ||
| clean = FALSE, | ||
| install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package") | ||
| ) | ||
| print(cov) | ||
| covr::to_cobertura(cov) | ||
| shell: Rscript {0} | ||
|
|
||
| - uses: codecov/codecov-action@v5 | ||
| with: | ||
| # Fail if error if not on PR, or if on PR and token is given | ||
| fail_ci_if_error: ${{ github.event_name != 'pull_request' || secrets.CODECOV_TOKEN }} | ||
| files: ./cobertura.xml | ||
| plugins: noop | ||
| disable_search: true | ||
| token: ${{ secrets.CODECOV_TOKEN }} | ||
|
|
||
| - name: Show testthat output | ||
| if: always() | ||
| run: | | ||
| ## -------------------------------------------------------------------- | ||
| find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true | ||
| shell: bash | ||
|
|
||
| - name: Upload test results | ||
| if: failure() | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: coverage-test-failures | ||
| path: ${{ runner.temp }}/package |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| # Generated by roxygen2: do not edit by hand | ||
|
|
||
| export(excluded_locations) | ||
| export(update_hub_target_data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #' Two digits FIPS codes for locations excluded from hubs' target data | ||
| #' | ||
| #' @export | ||
| excluded_locations <- c("78", "74", "69", "66", "60") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| nhsn_col_names <- list( | ||
| covid = "totalconfc19newadm", | ||
| rsv = "totalconfrsvnewadm" | ||
| ) | ||
| nssp_col_names <- list( | ||
| covid = "percent_visits_covid", | ||
| rsv = "percent_visits_rsv" | ||
| ) | ||
|
|
||
| #' Get and process target data for the hub | ||
| #' | ||
| #' This function pulls, formats and save NHSN and NSSP data for use in the hub. | ||
| #' | ||
| #' @param base_hub_path Path to the base hub directory. | ||
| #' @param disease Disease name ("covid" or "rsv"). | ||
| #' @param as_of As-of date of the data pull. Default is the system date as determined by [lubridate::today()]. | ||
| #' @param excluded_locations Vector of location codes to exclude from the output. | ||
| #' Default value `hubhelpr::excluded_locations`. | ||
| #' @param nhsn_first_weekending_date First week-ending date to include for | ||
| #' the NHSN dataset. Default value is "2024-11-09". | ||
| #' @param legacy_file Logical. Whether to write legacy CSV output (default: FALSE). | ||
| #' | ||
| #' @return Writes `time-series.parquet` and optionally legacy csv target data files | ||
| #' to the target-data directory in the hub. | ||
| #' @export | ||
| update_hub_target_data <- function( | ||
| base_hub_path, | ||
| disease, | ||
| as_of = lubridate::today(), | ||
| nhsn_first_weekending_date = lubridate::as_date("2024-11-09"), | ||
| excluded_locations = hubhelpr::excluded_locations, | ||
| legacy_file = FALSE | ||
| ) { | ||
| if (!disease %in% c("covid", "rsv")) { | ||
| stop("'disease' must be either 'covid' or 'rsv'") | ||
| } | ||
| output_dirpath <- fs::path(base_hub_path, "target-data") | ||
| fs::dir_create(output_dirpath) | ||
|
|
||
| nhsn_col_name <- nhsn_col_names[[disease]] | ||
| nssp_col_name <- nssp_col_names[[disease]] | ||
| hubverse_ts_req_cols <- c( | ||
| "date", | ||
| "observation", | ||
| "location", | ||
| "as_of", | ||
| "target" | ||
| ) | ||
| nhsn_data <- forecasttools::pull_data_cdc_gov_dataset( | ||
| dataset = "nhsn_hrd_prelim", | ||
| columns = nhsn_col_name, | ||
| start_date = nhsn_first_weekending_date | ||
| ) |> | ||
| dplyr::mutate( | ||
| date = lubridate::as_date(.data$weekendingdate), | ||
| observation = as.numeric(.data[[nhsn_col_name]]), | ||
| jurisdiction = stringr::str_replace(.data$jurisdiction, "USA", "US"), | ||
| location = forecasttools::us_location_recode( | ||
| .data$jurisdiction, | ||
| "abbr", | ||
| "code" | ||
| ), | ||
| as_of = !!as_of, | ||
dylanhmorris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| target = glue::glue("wk inc {disease} hosp") | ||
| ) |> | ||
| dplyr::filter(!(.data$location %in% !!excluded_locations)) | ||
|
|
||
| hubverse_format_nhsn_data <- nhsn_data |> | ||
| dplyr::select(tidyselect::all_of(hubverse_ts_req_cols)) | ||
|
|
||
| if (legacy_file) { | ||
| legacy_file_name <- glue::glue( | ||
| "{disease}-hospital-admissions.csv" | ||
| ) | ||
| nhsn_data |> | ||
| dplyr::rename( | ||
| value = "observation", | ||
| state = "jurisdiction" | ||
| ) |> | ||
| dplyr::arrange(state) |> | ||
| dplyr::select("state", "date", "value", "location") |> | ||
| readr::write_csv( | ||
| fs::path(output_dirpath, legacy_file_name) | ||
| ) | ||
| } | ||
|
|
||
| hubverse_format_nssp_data <- forecasttools::pull_data_cdc_gov_dataset( | ||
| dataset = "nssp_prop_ed_visits", | ||
| columns = c(nssp_col_name, "geography"), | ||
| locations = "All" | ||
| ) |> | ||
| dplyr::mutate( | ||
| date = lubridate::as_date(.data$week_end), | ||
| observation = as.numeric(.data[[nssp_col_name]]) / 100, | ||
| location = forecasttools::us_location_recode( | ||
| .data$geography, | ||
| "name", | ||
| "code" | ||
| ), | ||
| as_of = !!as_of, | ||
dylanhmorris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| target = glue::glue("wk inc {disease} prop ed visits") | ||
| ) |> | ||
| dplyr::select(dplyr::all_of(hubverse_ts_req_cols)) |> | ||
| dplyr::arrange(date, location) | ||
|
|
||
| output_file <- fs::path(output_dirpath, "time-series", ext = "parquet") | ||
| if (fs::file_exists(output_file)) { | ||
| existing_data <- forecasttools::read_tabular_file(output_file) | ||
| } else { | ||
| existing_data <- NULL | ||
| } | ||
| dplyr::bind_rows( | ||
| existing_data, | ||
| hubverse_format_nhsn_data, | ||
| hubverse_format_nssp_data | ||
| ) |> | ||
| forecasttools::write_tabular_file(output_file) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # This file is part of the standard setup for testthat. | ||
| # It is recommended that you do not modify it. | ||
| # | ||
| # Where should you do additional test configuration? | ||
| # Learn more about the roles of various files in: | ||
| # * https://r-pkgs.org/testing-design.html#sec-tests-files-overview | ||
| # * https://testthat.r-lib.org/articles/special-files.html | ||
|
|
||
| library(testthat) | ||
| library(hubhelpr) | ||
|
|
||
| test_check("hubhelpr") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.