From e565e9e3814d4f0096aaef8e2bd2e024fe72b8e4 Mon Sep 17 00:00:00 2001 From: Serena-Wang Date: Fri, 13 Aug 2021 17:25:32 -0400 Subject: [PATCH 1/2] make results locale-independent --- R/get_all_models.R | 32 +++++++++++++++++++------------- R/load_forecasts_repo.R | 3 +++ R/load_forecasts_zoltar.R | 1 + R/load_latest_forecasts_repo.R | 3 +++ man/get_all_models.Rd | 5 ++++- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/R/get_all_models.R b/R/get_all_models.R index dbfd685a..20beada9 100644 --- a/R/get_all_models.R +++ b/R/get_all_models.R @@ -2,14 +2,15 @@ #' #' @param source string specifying where to get all valid model names #' Currently support `"local_hub_repo"`, `"remote_hub_repo"` and `"zoltar"`. +#' @param hub character vector, where the first element indicates the hub +#' from which to load forecasts. Possible options are "US" and "ECDC" #' @param hub_repo_path path to local clone of the `reichlab/covid19-forecast-hub` #' repository #' @importFrom httr GET stop_for_status content #' @return a list of valid model names #' #' @export -get_all_models <- function(source = "zoltar", hub_repo_path) { - +get_all_models <- function(source = "zoltar", hub = c("US", "ECDC"), hub_repo_path) { # validate source source <- match.arg(source, choices = c("remote_hub_repo", "local_hub_repo", "zoltar"), @@ -26,10 +27,16 @@ get_all_models <- function(source = "zoltar", hub_repo_path) { stop("Error in get_all_models: data-processed subdirectory does not exist.") } models <- list.dirs(data_processed, full.names = FALSE) - models <- models[nchar(models) > 0] + models <- unique(models[nchar(models) > 0]) + models <- sort(models, method = "radix") } else if (source == "remote_hub_repo") { + if (hub[1] == "US") { + req_url <- "https://api.github.com/repos/reichlab/covid19-forecast-hub/git/trees/master?recursive=1" + } else if (hub[1] == "ECDC") { + stop("Error in get_all_models: loading all model names for ECDC hub from remote hub repo is not supported now.") + } # set up remote hub repo request - req <- httr::GET("https://api.github.com/repos/reichlab/covid19-forecast-hub/git/trees/master?recursive=1") + req <- httr::GET(req_url) httr::stop_for_status(req) # get all files in data-processed/ from tree structure @@ -45,21 +52,20 @@ get_all_models <- function(source = "zoltar", hub_repo_path) { models <- sapply(folders, function(filename) { unlist(strsplit(filename, "/"))[2] }) + models <- sort(unique(models), method = "radix") } else if (source == "zoltar") { # set up Zoltar connection - zoltar_connection <- zoltr::new_connection() - if (Sys.getenv("Z_USERNAME") == "" | Sys.getenv("Z_PASSWORD") == "") { - zoltr::zoltar_authenticate(zoltar_connection, "zoltar_demo", "Dq65&aP0nIlG") - } else { - zoltr::zoltar_authenticate(zoltar_connection, Sys.getenv("Z_USERNAME"), Sys.getenv("Z_PASSWORD")) - } + zoltar_connection <- setup_zoltar_connection(staging = FALSE) # construct Zoltar project url - the_projects <- zoltr::projects(zoltar_connection) - project_url <- the_projects[the_projects$name == "COVID-19 Forecasts", "url"] + project_url <- get_zoltar_project_url( + hub = hub, + zoltar_connection = zoltar_connection + ) models <- zoltr::models(zoltar_connection, project_url)$model_abbr + models <- sort(unique(models), method = "radix") } - return(unique(models)) + return(models) } diff --git a/R/load_forecasts_repo.R b/R/load_forecasts_repo.R index 841c3d39..c8a20a9f 100644 --- a/R/load_forecasts_repo.R +++ b/R/load_forecasts_repo.R @@ -49,6 +49,7 @@ load_forecasts_repo <- function( # validate models all_valid_models <- list.dirs(file_path, full.names = FALSE) all_valid_models <- all_valid_models[nchar(all_valid_models) > 0] + if (!is.null(models)) { models <- unlist(purrr::map(models, function(model) { match.arg(model, choices = all_valid_models) @@ -56,6 +57,8 @@ load_forecasts_repo <- function( } else { models <- all_valid_models } + + models <- sort(models, method = "radix") # get valid location codes if (hub[1] == "US") { diff --git a/R/load_forecasts_zoltar.R b/R/load_forecasts_zoltar.R index 6f811177..8818a63e 100644 --- a/R/load_forecasts_zoltar.R +++ b/R/load_forecasts_zoltar.R @@ -70,6 +70,7 @@ load_forecasts_zoltar <- function(models = NULL, if (is.null(models)){ models <- all_models$model_abbr + models <- sort(models, method = "radix") } # set 2 workers diff --git a/R/load_latest_forecasts_repo.R b/R/load_latest_forecasts_repo.R index e5b96366..77a5c581 100644 --- a/R/load_latest_forecasts_repo.R +++ b/R/load_latest_forecasts_repo.R @@ -32,6 +32,7 @@ load_latest_forecasts_repo <- function(file_path, # validate models all_valid_models <- list.dirs(file_path, full.names = FALSE) all_valid_models <- all_valid_models[nchar(all_valid_models) > 0] + if (!is.null(models)) { models <- unlist(purrr::map(models, function(model) { match.arg(model, choices = all_valid_models) @@ -39,6 +40,8 @@ load_latest_forecasts_repo <- function(file_path, } else { models <- all_valid_models } + + models <- sort(models, method = "radix") # get valid location codes if (hub[1] == "US") { diff --git a/man/get_all_models.Rd b/man/get_all_models.Rd index 1f9ae548..b1e36aca 100644 --- a/man/get_all_models.Rd +++ b/man/get_all_models.Rd @@ -4,12 +4,15 @@ \alias{get_all_models} \title{Get all valid model names} \usage{ -get_all_models(source = "zoltar", hub_repo_path) +get_all_models(source = "zoltar", hub = c("US", "ECDC"), hub_repo_path) } \arguments{ \item{source}{string specifying where to get all valid model names Currently support \code{"local_hub_repo"}, \code{"remote_hub_repo"} and \code{"zoltar"}.} +\item{hub}{character vector, where the first element indicates the hub +from which to load forecasts. Possible options are "US" and "ECDC"} + \item{hub_repo_path}{path to local clone of the \code{reichlab/covid19-forecast-hub} repository} } From f313d33ab5cf8eccfff9512791774573b8768f7d Mon Sep 17 00:00:00 2001 From: Serena-Wang Date: Fri, 13 Aug 2021 17:34:25 -0400 Subject: [PATCH 2/2] update NEWS --- NEWS.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/NEWS.md b/NEWS.md index f07e916a..ec1d0806 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +## Changes since last release +- sort `models` parameter in `load_forecasts()` and `load_latest_forecasts()` so that the resulting data frame is +locale-independent +- add `hub` parameter in `get_all_models()`. It does not support loading model names for ECDC hub from remote hub repo for now. + ## covidHubUtils 0.1.6 This is a release focusing on new features in most of the major functions.