Skip to content

Commit

Permalink
Merge branch 'main' into dhm-add-categorization
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanhmorris authored Dec 7, 2024
2 parents af11743 + e42ff17 commit 18ba933
Show file tree
Hide file tree
Showing 12 changed files with 369 additions and 128 deletions.
5 changes: 4 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export(get_prism_cutpoints)
export(inferencedata_to_tidy_draws)
export(location_lookup)
export(nhsn_soda_query)
export(nullable_comparison)
export(pivot_hubverse_quantiles_wider)
export(plot_coverage_by_date)
export(plot_forecast_quantiles)
Expand All @@ -31,7 +32,9 @@ export(sample_aggregated_trajectories)
export(scale_color_prism)
export(scale_colour_prism)
export(scale_fill_prism)
export(soql_is_in)
export(soql_nullable_is_in)
export(soql_nullable_select)
export(soql_nullable_where)
export(target_end_dates_from_horizons)
export(theme_forecasttools)
export(to_location_table_column)
Expand Down
72 changes: 31 additions & 41 deletions R/filter_for_scoring.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,56 +32,46 @@ filter_for_scoring <- function(forecasts_and_targets,
models = NULL) {
to_score <- forecasts_and_targets

loc_codes <- if (!is.null(locations)) {
us_loc_abbr_to_code(locations)
} else {
NULL
}
min_forecast_date <- if (!is.null(min_forecast_date)) {
as.Date(min_forecast_date)
} else {
NULL
}

max_forecast_date <- if (!is.null(max_forecast_date)) {
as.Date(max_forecast_date)
} else {
NULL
}

## strip NA values from
## not yet scored tables
if ("true_value" %in% names(to_score)) {
cli::cli_inform(
"Filtering out forecasts without corresponding truth data..."
)
to_score <- to_score |>
dplyr::filter(!is.na(true_value))
dplyr::filter(!is.na(.data$true_value))
}

# filter forecasts to score
if (!is.null(min_forecast_date)) {
cli::cli_inform(
"Filtering out forecast dates before {min_forecast_date}"
)
to_score <- to_score |>
dplyr::filter(forecast_date >= as.Date(!!min_forecast_date))
}
if (!is.null(max_forecast_date)) {
cli::cli_inform(
"Filtering out forecast dates after {max_forecast_date}"
)
to_score <- to_score |>
dplyr::filter(forecast_date <= as.Date(!!max_forecast_date))
}
if (!is.null(horizons)) {
cli::cli_inform(
"Filtering out horizons not in {horizons}"
to_score <- to_score |>
dplyr::filter(
nullable_comparison(
.data$forecast_date, ">=",
!!min_forecast_date
),
nullable_comparison(
.data$forecast_date, "<=",
!!max_forecast_date
),
nullable_comparison(.data$horizon, "%in%", !!horizons),
nullable_comparison(.data$location, "%in%", !!loc_codes),
nullable_comparison(.data$model, "%in%", !!models)
)
to_score <- to_score |>
dplyr::filter(horizon %in% c(!!horizons))
}
if (!is.null(locations)) {
cli::cli_inform(
"Filtering out locations not in {locations}"
)

loc_codes <- us_loc_abbr_to_code(
locations
)
to_score <- to_score |>
dplyr::filter(location %in% c(!!loc_codes))
}
if (!is.null(models)) {
cli::cli_inform(
"Filtering out models not in {models}"
)
to_score <- to_score |>
dplyr::filter(model %in% c(!!models))
}

return(to_score)
}
73 changes: 19 additions & 54 deletions R/pull_nhsn.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,11 @@ pull_nhsn <- function(api_endpoint =
return(df)
}

#' Return a [soql::soql_where()] construct
#' for a given column being in a list of values
#'
#' @param soql_list A `soql` query object, which
#' can be piped in. If one hasn't been
#' created yet, use or pipe in [soql::soql()].
#' @param column The column to filter on
#' @param match_values A vector of values that column
#' must match
#' @return A new soql object with the filter added,
#' for use in other functions.
#' @export
soql_is_in <- function(soql_list, column, match_values) {
query <- stringr::str_glue(
"{column}='{match_values}'"
) |>
paste(collapse = " OR ")
return(soql::soql_where(soql_list, query))
}

#' Construct a Socrata open data
#' API (SODA) query for the NSHN
#' dataset
#' API (SODA) query for the NHSN Hospital Respiratory
#' Data set.
#'
#' @param api_endpoint Base API endpoint URL to use
#' when constructing the query.
#' @param start_date Pull only rows with dates
Expand Down Expand Up @@ -187,41 +169,24 @@ nhsn_soda_query <- function(api_endpoint,
),
desc = FALSE,
...) {
query <- soql::soql() |>
soql::soql_add_endpoint(api_endpoint)

if (!is.null(columns)) {
query <- query |>
soql::soql_select(paste(
unique(
c("jurisdiction", "weekendingdate", columns)
),
collapse = ","
))
}

if (!is.null(start_date)) {
query <- query |>
soql::soql_where(
stringr::str_glue("weekendingdate >= '{start_date}'")
)
}

if (!is.null(end_date)) {
query <- query |>
soql::soql_where(
stringr::str_glue("weekendingdate <= '{end_date}'")
)
}

if (!is.null(jurisdictions)) {
query <- query |>
soql_is_in(
"jurisdiction", jurisdictions
)
cols <- if (!is.null(columns)) {
c("jurisdiction", "weekendingdate", columns)
} else {
NULL
}

query <- query |>
query <- soql::soql() |>
soql::soql_add_endpoint(api_endpoint) |>
soql_nullable_select(cols) |>
soql_nullable_where(
"weekendingdate", ">=", start_date
) |>
soql_nullable_where(
"weekendingdate", "<=", end_date
) |>
soql_nullable_is_in(
"jurisdiction", jurisdictions
) |>
soql::soql_order(
paste(unique(order_by),
collapse = ","
Expand Down
139 changes: 139 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#' Compare two quantities `a` and `b` with a given
#' `comparison operator`,
#' returning a single `TRUE` if `b` is
#' `NULL`.
#'
#' Useful for letting `NULL` reflect "all values"
#' if [dplyr::filter()] calls. Internal function.
#'
#' @param a First set of values for the comparison
#' @param comparison_operator Comparison operator for the comparison,
#' as a string, e.g. `"=="`, `">="` `"%in%"`.
#' @param b Second set of values for the comparison, or `NULL`.
#' @return A logical vector. Equivalent to `b {comparison_operator} a`
#' if `b` is not `NULL` and to `TRUE` if `b` is `NULL`.
#' #'
#' @examples
#'
#' x <- 6
#' nullable_comparison(5, ">", x)
#'
#' x <- NULL
#' nullable_comparison(5, ">", x)
#'
#' df <- tibble::tibble(y = 1:6)
#' x <- 3
#' df |> dplyr::filter(
#' nullable_comparison(y, ">", !!x),
#' y < 5
#' )
#' x <- NULL
#' df |> dplyr::filter(
#' nullable_comparison(y, ">", !!x),
#' y < 5
#' )
#'
#' @export
nullable_comparison <- function(a,
comparison_operator,
b) {
comparison_func <- getFunction(comparison_operator)
return(
if (!is.null(b)) {
comparison_func(a, b)
} else {
TRUE
}
)
}


#' Add a [soql::soql_where()] statement to a SOQL query,
#' or return the original query if `where_value` is `NULL`.
#'
#' @param soql_list A `soql` query object, which
#' can be piped in. If one hasn't been
#' created yet, use or pipe in [soql::soql()].
#' @param column column name for the [soql::soql_where()] component
#' of the query, as a string.
#' @param comparison_operator comparison operator for the
#' [soql::soql_where()] component of the query, as a string.
#' @param where_value A value for the comparison, or `NULL`
#' for no filtering.
#' @return A new [soql::soql()] object with the filter added, or
#' simply the input object if `where_value` is `NULL`.
#' @export
soql_nullable_where <- function(soql_list,
column,
comparison_operator,
where_value) {
return(
if (!is.null(where_value)) {
soql::soql_where(
soql_list,
glue::glue(paste0(
"{column} ",
"{comparison_operator} ",
"'{where_value}'"
))
)
} else {
soql_list
}
)
}


#' Add an "is in" statement to a SOQL query, or return the original
#' query if `match_values` is `NULL`.
#'
#' An is in statement is a [soql::soql_where()] statement
#' that requires the values of a given column to match one of the
#' entries of a vector of `match_values`.
#'
#' @param soql_list A `soql` query object, which
#' can be piped in. If one hasn't been
#' created yet, use or pipe in [soql::soql()].
#' @param column The column to filter on
#' @param match_values A vector of values that column
#' must match, or `NULL` for no filtering.
#' @return A new [soql::soql()] object with the filter added,
#' or simply the input object if `match_value` is `NULL`.
#' @export
soql_nullable_is_in <- function(soql_list, column, match_values) {
if (is.null(match_values)) {
return(soql_list)
} else {
query <- glue::glue(
"{column}='{unique(match_values)}'"
) |>
paste(collapse = " OR ")
return(soql::soql_where(soql_list, query))
}
}

#' Add a [soql::soql_select()] statement to a query
#' to select a set of columns `columns`, or return
#' the original query if `columns` is `NULL`.
#'
#' @param soql_list A `soql` query object, which
#' can be piped in. If one hasn't been
#' created yet, use or pipe in [soql::soql()].
#' @param columns The columns to select, or `NULL`.
#' @return A new [soql::soql()] object with the selection statement
#' added, or the input object if `columns` is `NULL`.
#' @export
soql_nullable_select <- function(soql_list, columns) {
return(
if (!is.null(columns)) {
soql::soql_select(
soql_list,
paste(unique(columns),
collapse = ","
)
)
} else {
soql_list
}
)
}
8 changes: 4 additions & 4 deletions man/nhsn_soda_query.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 18ba933

Please sign in to comment.