Skip to content

Commit

Permalink
Move get_all_forecast_dirs() to hewr
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanhmorris committed Nov 19, 2024
1 parent 40efe59 commit 36ec48c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 41 deletions.
43 changes: 43 additions & 0 deletions hewr/R/parse_dir_name.R → hewr/R/directory_utils.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#' Utilities for handling and parsing directory names
#' based on pyrenew-hew pipeline conventions.

disease_map_lower <- list(
"covid-19" = "COVID-19",
"influenza" = "Influenza"
)

#' Parse model batch directory name.
#'
#' Parse the name of a model batch directory
#' (i.e. a directory representing a single
#' report date and disease pair, but potentially
Expand Down Expand Up @@ -40,6 +45,8 @@ parse_model_batch_dir_name <- function(model_batch_dir_name) {
))
}

#' Parse model run directory path.
#'
#' Parse path to a model run directory
#' (i.e. a directory representing a run for a
#' particular location, disease, and reference
Expand All @@ -61,3 +68,39 @@ parse_model_run_dir_path <- function(model_run_dir_path) {
parse_model_batch_dir(batch_dir)
))
}


#' Get forecast directories.
#'
#' Get all the subdirectories within a parent directory
#' that match the pattern for a forecast run for a
#' given disease and optionally a given report date.
#'
#' @param dir_of_forecast_dirs Directory in which to look for
#' subdirectories representing individual forecast date / pathogen /
#' dataset combinations.
#' @param diseases Names of the diseases to match, as a vector of strings,
#' or a single disease as a string.
#' @return A vector of paths to the forecast subdirectories.
get_all_forecast_dirs <- function(dir_of_forecast_dirs,
diseases) {
# disease names are lowercase by convention
match_patterns <- stringr::str_c(tolower(diseases),
"_r",
collapse = "|"
)

dirs <- tibble::tibble(
dir_path = fs::dir_ls(
dir_of_forecast_dirs,
type = "directory"
)
) |>
dplyr::filter(str_starts(
fs::path_file(dir_path),
match_patterns
)) |>
dplyr::pull(dir_path)

return(dirs)
}
18 changes: 18 additions & 0 deletions hewr/man/disease_map_lower.Rd

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

24 changes: 24 additions & 0 deletions hewr/man/get_all_forecast_dirs.Rd

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

8 changes: 2 additions & 6 deletions hewr/man/parse_model_batch_dir_name.Rd

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

7 changes: 2 additions & 5 deletions hewr/man/parse_model_run_dir_path.Rd

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

31 changes: 1 addition & 30 deletions pipelines/collate_score_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,6 @@ purrr::walk(script_packages, \(pkg) {
})


#' Get all the subdirectories within a parent directory
#' that match the pattern for a forecast run for a
#' given disease and optionally a given report date.
#'
#' @param parent_dir Directory in which to look for forecast subdirectories.
#' @param diseases Names of the diseases to match, as a vector of strings,
#' or a single disease as a string.
#' @return A vector of paths to the forecast subdirectories.
get_all_forecast_dirs <- function(dir_of_forecast_date_dirs,
diseases) {
# disease names are lowercase by convention
match_patterns <- str_c(tolower(diseases), "_r", collapse = "|")

dirs <- tibble::tibble(
dir_path = fs::dir_ls(
dir_of_forecast_date_dirs,
type = "directory"
)
) |>
dplyr::filter(str_starts(
fs::path_file(dir_path),
match_patterns
)) |>
dplyr::pull(dir_path)

return(dirs)
}


process_loc_date_score_table <- function(model_run_dir) {
table_path <- fs::path(model_run_dir,
"score_table",
Expand Down Expand Up @@ -137,7 +108,7 @@ collate_scores_for_date <- function(model_run_dir,
collate_all_score_tables <- function(model_base_dir,
disease,
score_file_save_path = NULL) {
date_dirs_to_process <- get_all_forecast_dirs(
date_dirs_to_process <- hewr::get_all_forecast_dirs(
model_base_dir,
diseases = disease
)
Expand Down

0 comments on commit 36ec48c

Please sign in to comment.