From c32765d91624a395a7cc861b396fee1fc8994b0f Mon Sep 17 00:00:00 2001 From: Emma Date: Wed, 16 Oct 2024 17:07:41 +0100 Subject: [PATCH 1/8] fix grep result check --- R/country_helpers.R | 30 ++++++++++++++++++++++++++++++ R/daedalus.R | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 R/country_helpers.R diff --git a/R/country_helpers.R b/R/country_helpers.R new file mode 100644 index 0000000..8734c8b --- /dev/null +++ b/R/country_helpers.R @@ -0,0 +1,30 @@ +#' Get country name from arg +#' @description Function to get a supported country name given an arg which may be a country name or may +#' be an ISO2 or ISO3 code. In all cases, the function checks if the country identifier is supported. +#' +#' @param country A string which could be a country name, or an ISO2 or ISO3 code. +#' +#' @return A supported country name +#' @keywords internal +country_name_from_arg <- function(country) { + lookup_country_name_from_code <- function(code, code_list) { + # check code is known, and lookup country name from idx + rlang::arg_match(code, code_list) + idx <- match(code, code_list) + daedalus::country_names[[idx]] + } + + # regex check for ISO2 + if (length(grep("^[A-Z]{2}$", country)) != 0) { + country <- lookup_country_name_from_code(country, daedalus::country_codes_iso2c) + } + + # regex check for ISO3 + if (length(grep("^[A-Z]{3}$", country)) != 0) { + country <- lookup_country_name_from_code(country, daedalus::country_codes_iso3c) + } + + # check country name is known + country <- rlang::arg_match(country, daedalus::country_names) + country +} \ No newline at end of file diff --git a/R/daedalus.R b/R/daedalus.R index 1bff76b..adfeb42 100644 --- a/R/daedalus.R +++ b/R/daedalus.R @@ -122,7 +122,7 @@ daedalus <- function(country, # NOTE: names are case sensitive checkmate::assert_multi_class(country, c("daedalus_country", "character")) if (is.character(country)) { - country <- rlang::arg_match(country, daedalus::country_names) + country <- country_name_from_arg(country) country <- daedalus_country(country) } checkmate::assert_multi_class(infection, c("daedalus_infection", "character")) From 8da98457913ec3ef4a8182154b464f2afa3b80c1 Mon Sep 17 00:00:00 2001 From: Emma Date: Wed, 16 Oct 2024 17:28:03 +0100 Subject: [PATCH 2/8] add helper tests --- DESCRIPTION | 2 +- tests/testthat/test_country_helpers.R | 32 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test_country_helpers.R diff --git a/DESCRIPTION b/DESCRIPTION index bdefaf5..c36b540 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: daedalus Title: Model health, social, and economic costs of a pandemic using _DAEDALUS_ -Version: 0.0.18 +Version: 0.0.19 Authors@R: c( person("Pratik", "Gupte", , "p.gupte24@imperial.ac.uk", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-5294-7819")), diff --git a/tests/testthat/test_country_helpers.R b/tests/testthat/test_country_helpers.R new file mode 100644 index 0000000..3c07672 --- /dev/null +++ b/tests/testthat/test_country_helpers.R @@ -0,0 +1,32 @@ +test_that("country_name_from_arg looks up name from iso2", { + expect_identical(country_name_from_arg("CA"), "Canada") +}) + +test_that("country_name_from_arg looks up name from iso3", { + expect_identical(country_name_from_arg("CAN"), "Canada") +}) + +test_that("country_name_from_arg passes through supported country name", { + expect_identical(country_name_from_arg("Canada"), "Canada") +}) + +test_that("country_name_from_arg errors on unsupported iso2", { + expect_error( + country_name_from_arg("ZZ"), + regexp = "must be one of" + ) +}) + +test_that("country_name_from_arg errors on unsupported iso3", { + expect_error( + country_name_from_arg("ZZZ"), + regexp = "must be one of" + ) +}) + +test_that("country_name_from_arg errors on unsupported country name", { + expect_error( + country_name_from_arg("Narnia"), + regexp = "must be one of" + ) +}) \ No newline at end of file From 84669639edcd478de882073cc450920cccfb3581 Mon Sep 17 00:00:00 2001 From: Emma Date: Wed, 16 Oct 2024 17:37:08 +0100 Subject: [PATCH 3/8] add daedalus test --- tests/testthat/test-daedalus.R | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/testthat/test-daedalus.R b/tests/testthat/test-daedalus.R index 8b1391e..35bef83 100644 --- a/tests/testthat/test-daedalus.R +++ b/tests/testthat/test-daedalus.R @@ -69,6 +69,22 @@ test_that("daedalus: basic expectations", { ) }) +test_that("Can run with ISO2 country parameter", { + expect_no_condition({ + output <- daedalus("CA", "influenza_1918") + }) + data <- get_data(output) + expect_length(data, N_OUTPUT_COLS) +}) + +test_that("Can run with ISO3 country parameter", { + expect_no_condition({ + output <- daedalus("CAN", "influenza_1918") + }) + data <- get_data(output) + expect_length(data, N_OUTPUT_COLS) +}) + # test that daedalus runs for all epidemic infection parameter sets test_that("daedalus: Runs for all country x infection x response", { country_infection_combos <- data.table::CJ( From ebcb639d2e185931f23ad01c52e5d2a2d450bfe6 Mon Sep 17 00:00:00 2001 From: Emma Date: Wed, 16 Oct 2024 18:57:01 +0100 Subject: [PATCH 4/8] lint --- R/country_helpers.R | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/R/country_helpers.R b/R/country_helpers.R index 8734c8b..f6de3ae 100644 --- a/R/country_helpers.R +++ b/R/country_helpers.R @@ -1,6 +1,7 @@ #' Get country name from arg -#' @description Function to get a supported country name given an arg which may be a country name or may -#' be an ISO2 or ISO3 code. In all cases, the function checks if the country identifier is supported. +#' @description Function to get a supported country name given an arg which +#' may be a country name or may be an ISO2 or ISO3 code. In all cases, +#' the function checks if the country identifier is supported. #' #' @param country A string which could be a country name, or an ISO2 or ISO3 code. #' @@ -15,16 +16,20 @@ country_name_from_arg <- function(country) { } # regex check for ISO2 - if (length(grep("^[A-Z]{2}$", country)) != 0) { - country <- lookup_country_name_from_code(country, daedalus::country_codes_iso2c) + if (length(grep("^[A-Z]{2}$", country)) > 0) { + country <- lookup_country_name_from_code( + country, daedalus::country_codes_iso2c + ) } # regex check for ISO3 - if (length(grep("^[A-Z]{3}$", country)) != 0) { - country <- lookup_country_name_from_code(country, daedalus::country_codes_iso3c) + if (length(grep("^[A-Z]{3}$", country)) > 0) { + country <- lookup_country_name_from_code( + country, daedalus::country_codes_iso3c + ) } # check country name is known country <- rlang::arg_match(country, daedalus::country_names) country -} \ No newline at end of file +} From 64054eeaded70bc95ec0f4777ddf9fa66a697f18 Mon Sep 17 00:00:00 2001 From: Emma Date: Wed, 16 Oct 2024 19:04:54 +0100 Subject: [PATCH 5/8] more lint --- R/country_helpers.R | 7 ++++--- tests/testthat/test_country_helpers.R | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/R/country_helpers.R b/R/country_helpers.R index f6de3ae..807e48e 100644 --- a/R/country_helpers.R +++ b/R/country_helpers.R @@ -3,7 +3,8 @@ #' may be a country name or may be an ISO2 or ISO3 code. In all cases, #' the function checks if the country identifier is supported. #' -#' @param country A string which could be a country name, or an ISO2 or ISO3 code. +#' @param country A string which could be a country name, or an ISO2 or ISO3 +#' code. #' #' @return A supported country name #' @keywords internal @@ -16,14 +17,14 @@ country_name_from_arg <- function(country) { } # regex check for ISO2 - if (length(grep("^[A-Z]{2}$", country)) > 0) { + if (any(grep("^[A-Z]{2}$", country))) { country <- lookup_country_name_from_code( country, daedalus::country_codes_iso2c ) } # regex check for ISO3 - if (length(grep("^[A-Z]{3}$", country)) > 0) { + if (any(grep("^[A-Z]{3}$", country))) { country <- lookup_country_name_from_code( country, daedalus::country_codes_iso3c ) diff --git a/tests/testthat/test_country_helpers.R b/tests/testthat/test_country_helpers.R index 3c07672..e1aec5c 100644 --- a/tests/testthat/test_country_helpers.R +++ b/tests/testthat/test_country_helpers.R @@ -29,4 +29,4 @@ test_that("country_name_from_arg errors on unsupported country name", { country_name_from_arg("Narnia"), regexp = "must be one of" ) -}) \ No newline at end of file +}) From cf386f7b12819fb9088bf8b548f09f805a60eb55 Mon Sep 17 00:00:00 2001 From: Emma Date: Thu, 17 Oct 2024 14:51:18 +0100 Subject: [PATCH 6/8] changes for review --- R/class_country.R | 9 +++--- R/country_helpers.R | 12 ++++---- R/daedalus.R | 4 +-- man/class_country.Rd | 7 +++-- man/country_name_from_arg.Rd | 21 ++++++++++++++ man/daedalus.Rd | 3 +- man/make_initial_state.Rd | 3 +- tests/testthat/test-class_daedalus_country.R | 30 ++++++++++++++++++-- 8 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 man/country_name_from_arg.Rd diff --git a/R/class_country.R b/R/class_country.R index 5b53fb1..31f78de 100644 --- a/R/class_country.R +++ b/R/class_country.R @@ -33,8 +33,9 @@ new_daedalus_country <- function(name, parameters) { #' parameter access and editing, as well as processing raw country #' characteristics for the DAEDALUS model. #' -#' @param name A string giving the country or territory name; must be from among -#' [daedalus::country_names]. +#' @param country A string giving the country or territory name, or ISO2 or +#' ISO3 code; must be from among [daedalus::country_codes_iso2c] or +#' [daedalus::country_codes_iso3c] or [daedalus::country_names]. #' #' @param parameters An optional named list of country parameters that are #' allowed to be modified. Currently, users may only pass their own contact @@ -72,14 +73,14 @@ new_daedalus_country <- function(name, parameters) { #' # using assignment operators #' x$contact_matrix <- matrix(99, 4, 4) #' x -daedalus_country <- function(name, +daedalus_country <- function(country, parameters = list( contact_matrix = NULL, contacts_workplace = NULL, contacts_consumer_worker = NULL )) { # input checking - name <- rlang::arg_match(name, daedalus::country_names) + name <- country_name_from_arg(country) # check list but allow missing and NULL checkmate::assert_list( parameters, c("numeric", "matrix", "NULL") diff --git a/R/country_helpers.R b/R/country_helpers.R index 807e48e..2bc0718 100644 --- a/R/country_helpers.R +++ b/R/country_helpers.R @@ -11,7 +11,7 @@ country_name_from_arg <- function(country) { lookup_country_name_from_code <- function(code, code_list) { # check code is known, and lookup country name from idx - rlang::arg_match(code, code_list) + code <- rlang::arg_match(code, code_list) idx <- match(code, code_list) daedalus::country_names[[idx]] } @@ -22,15 +22,15 @@ country_name_from_arg <- function(country) { country, daedalus::country_codes_iso2c ) } - # regex check for ISO3 - if (any(grep("^[A-Z]{3}$", country))) { + else if (any(grep("^[A-Z]{3}$", country))) { country <- lookup_country_name_from_code( country, daedalus::country_codes_iso3c ) } - - # check country name is known - country <- rlang::arg_match(country, daedalus::country_names) + else { + # check country name is known + country <- rlang::arg_match(country, daedalus::country_names) + } country } diff --git a/R/daedalus.R b/R/daedalus.R index adfeb42..fdf4ff6 100644 --- a/R/daedalus.R +++ b/R/daedalus.R @@ -4,7 +4,8 @@ #' #' @param country A country or territory object of class ``, #' **or** a country or territory name from those included in the package; -#' see [daedalus::country_names]. +#' see [daedalus::country_names], **or** a country ISO2 or ISO3 code; see +#' [daedalus::country_codes_iso2c] and [daedalus::country_codes_iso3c] . #' Country-specific data such as the community and workplace contacts, the #' demography, and the distribution of the workforce into economic sectors is #' automatically accessed from package data for the relevant country name if it @@ -122,7 +123,6 @@ daedalus <- function(country, # NOTE: names are case sensitive checkmate::assert_multi_class(country, c("daedalus_country", "character")) if (is.character(country)) { - country <- country_name_from_arg(country) country <- daedalus_country(country) } checkmate::assert_multi_class(infection, c("daedalus_infection", "character")) diff --git a/man/class_country.Rd b/man/class_country.Rd index 37c155c..e96b505 100644 --- a/man/class_country.Rd +++ b/man/class_country.Rd @@ -8,7 +8,7 @@ \title{Represent countries and territories for DAEDALUS} \usage{ daedalus_country( - name, + country, parameters = list(contact_matrix = NULL, contacts_workplace = NULL, contacts_consumer_worker = NULL) ) @@ -18,8 +18,9 @@ is_daedalus_country(x) \method{print}{daedalus_country}(x, ...) } \arguments{ -\item{name}{A string giving the country or territory name; must be from among -\link{country_names}.} +\item{country}{A string giving the country or territory name, or ISO2 or +ISO3 code; must be from among \link{country_codes_iso2c} or +\link{country_codes_iso3c} or \link{country_names}.} \item{parameters}{An optional named list of country parameters that are allowed to be modified. Currently, users may only pass their own contact diff --git a/man/country_name_from_arg.Rd b/man/country_name_from_arg.Rd new file mode 100644 index 0000000..2263e79 --- /dev/null +++ b/man/country_name_from_arg.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/country_helpers.R +\name{country_name_from_arg} +\alias{country_name_from_arg} +\title{Get country name from arg} +\usage{ +country_name_from_arg(country) +} +\arguments{ +\item{country}{A string which could be a country name, or an ISO2 or ISO3 +code.} +} +\value{ +A supported country name +} +\description{ +Function to get a supported country name given an arg which +may be a country name or may be an ISO2 or ISO3 code. In all cases, +the function checks if the country identifier is supported. +} +\keyword{internal} diff --git a/man/daedalus.Rd b/man/daedalus.Rd index ca3b276..d02e66c 100644 --- a/man/daedalus.Rd +++ b/man/daedalus.Rd @@ -19,7 +19,8 @@ daedalus( \arguments{ \item{country}{A country or territory object of class \verb{}, \strong{or} a country or territory name from those included in the package; -see \link{country_names}. +see \link{country_names}, \strong{or} a country ISO2 or ISO3 code; see +\link{country_codes_iso2c} and \link{country_codes_iso3c} . Country-specific data such as the community and workplace contacts, the demography, and the distribution of the workforce into economic sectors is automatically accessed from package data for the relevant country name if it diff --git a/man/make_initial_state.Rd b/man/make_initial_state.Rd index 7484b43..c42da0b 100644 --- a/man/make_initial_state.Rd +++ b/man/make_initial_state.Rd @@ -9,7 +9,8 @@ make_initial_state(country, initial_state_manual) \arguments{ \item{country}{A country or territory object of class \verb{}, \strong{or} a country or territory name from those included in the package; -see \link{country_names}. +see \link{country_names}, \strong{or} a country ISO2 or ISO3 code; see +\link{country_codes_iso2c} and \link{country_codes_iso3c} . Country-specific data such as the community and workplace contacts, the demography, and the distribution of the workforce into economic sectors is automatically accessed from package data for the relevant country name if it diff --git a/tests/testthat/test-class_daedalus_country.R b/tests/testthat/test-class_daedalus_country.R index e5fddeb..684b9ff 100644 --- a/tests/testthat/test-class_daedalus_country.R +++ b/tests/testthat/test-class_daedalus_country.R @@ -20,13 +20,27 @@ test_that("class : basic expectations", { ) }) -test_that("class `: works for all countries", { +test_that("class `: works for all country names", { expect_no_condition({ countries <- lapply(daedalus::country_names, daedalus_country) # nolint }) checkmate::expect_list(countries, "daedalus_country") }) +test_that("class `: works for all country ISO2 codes", { + expect_no_condition({ + countries <- lapply(daedalus::country_codes_iso2c, daedalus_country) # nolint + }) + checkmate::expect_list(countries, "daedalus_country") +}) + +test_that("class `: works for all country ISO3 codes", { + expect_no_condition({ + countries <- lapply(daedalus::country_codes_iso3c, daedalus_country) # nolint + }) + checkmate::expect_list(countries, "daedalus_country") +}) + test_that("class : access and assignment", { name <- "Canada" country_x <- daedalus_country(name) @@ -121,7 +135,19 @@ test_that("class `: errors", { # invalid name expect_error( daedalus_country("dummy"), - regexp = "`name` must be one of" + regexp = "`country` must be one of" + ) + + # invalid ISO2 + expect_error( + daedalus_country("ZZ"), + regexp = "`code` must be one of" + ) + + # invalid ISO3 + expect_error( + daedalus_country("ZZZ"), + regexp = "`code` must be one of" ) # invalid `parameter` type From a6bdac44ceded939ab04fae6a4446cf19321c56e Mon Sep 17 00:00:00 2001 From: Emma Date: Thu, 17 Oct 2024 15:03:07 +0100 Subject: [PATCH 7/8] lint --- R/country_helpers.R | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/R/country_helpers.R b/R/country_helpers.R index 2bc0718..5295dd2 100644 --- a/R/country_helpers.R +++ b/R/country_helpers.R @@ -16,19 +16,15 @@ country_name_from_arg <- function(country) { daedalus::country_names[[idx]] } - # regex check for ISO2 - if (any(grep("^[A-Z]{2}$", country))) { + if (any(grep("^[A-Z]{2}$", country))) { # regex check for ISO2 country <- lookup_country_name_from_code( country, daedalus::country_codes_iso2c ) - } - # regex check for ISO3 - else if (any(grep("^[A-Z]{3}$", country))) { + } else if (any(grep("^[A-Z]{3}$", country))) { # regex check for ISO3 country <- lookup_country_name_from_code( country, daedalus::country_codes_iso3c ) - } - else { + } else { # check country name is known country <- rlang::arg_match(country, daedalus::country_names) } From 3c8cccccc22b706f11dbdd8bae0270e7d3cc8a7b Mon Sep 17 00:00:00 2001 From: Emma Date: Thu, 17 Oct 2024 15:26:53 +0100 Subject: [PATCH 8/8] spellcheck --- inst/WORDLIST | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/WORDLIST b/inst/WORDLIST index 592987f..d0615c7 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -14,6 +14,7 @@ Pianella SARS SEIR Sumali +arg asymp doi econ @@ -24,6 +25,5 @@ rootfinding started’ symp th -timeframe timeseries timesteps