From d260cf910dc432fb5799bd02e5891e28aad5e0dd Mon Sep 17 00:00:00 2001 From: Emma Russell <44669576+EmmaLRussell@users.noreply.github.com> Date: Mon, 21 Oct 2024 09:50:40 +0100 Subject: [PATCH] jidea-136 use iso3 codes as country ids (#28) * use iso3 codes as country ids * use iso in test and do not require name match in gdp fetch * default country to THA * use setNames sensibly when getting hospital capacity Co-authored-by: Rich FitzJohn --------- Co-authored-by: Rich FitzJohn --- DESCRIPTION | 4 ++-- R/api.R | 13 +++++++------ R/util.R | 9 +++------ inst/json/metadata_0.1.0.json | 2 +- tests/testthat/test-endpoints.R | 13 +++++++------ tests/testthat/test-zzz-e2e.R | 2 +- tests/testthat/test_model_run.R | 4 ++-- 7 files changed, 23 insertions(+), 24 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6c99008..0e46b4c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: daedalus.api Title: Serve the 'daedalus' model via an API -Version: 0.0.5 +Version: 0.0.7 Authors@R: c( person("Pratik", "Gupte", , "p.gupte24@imperial.ac.uk", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-5294-7819")), @@ -12,7 +12,7 @@ License: MIT + file LICENSE URL: https://github.com/jameel-institute/daedalus.api BugReports: https://github.com/jameel-institute/daedalus.api/issues Imports: - daedalus (>= 0.0.17), + daedalus (>= 0.0.19), docopt, dplyr, jsonlite, diff --git a/R/api.R b/R/api.R index 65edab6..38a419c 100644 --- a/R/api.R +++ b/R/api.R @@ -58,11 +58,11 @@ metadata <- function() { # Set available countries from daedalus package # JIDEA-62: use the right version of daedalus/model - # we will get ISO ids from daedalus when available country_names <- daedalus::country_names + country_codes <- daedalus::country_codes_iso3c - country_options <- lapply(country_names, function(country) { - get_option(country, country) + country_options <- lapply(seq_along(country_names), function(idx) { + get_option(country_codes[[idx]], country_names[[idx]]) }) country_idx <- match("country", param_ids) response$parameters[[country_idx]]$options <- country_options @@ -84,9 +84,10 @@ metadata <- function() { step <- response$parameters[[hospital_capacity_idx]]$step # setNames to get json object not array hospital_capacities <- lapply( - setNames(country_names, country_names), - function(country) { - default <- daedalus::daedalus_country(country)$hospital_capacity + setNames(country_names, country_codes), + function(country_name) { + default <- daedalus::daedalus_country(country_name)$hospital_capacity + get_hospital_capacity_range(default, step) }) diff --git a/R/util.R b/R/util.R index 0e5d5ac..d4b2889 100644 --- a/R/util.R +++ b/R/util.R @@ -119,17 +119,14 @@ get_nested_costs <- function(raw_costs) { #' #' @description Convert daily GVA values to annual GDP values. #' -#' @param x A string giving a country name from among `daedalus::country_names`. +#' @param x A string giving a country name from among `daedalus::country_names` +#' or an ISO2 code from among `daedalus::country_codes_iso2c` or an ISO3 code +#' from among `daedalus::country_codes_iso3c`. #' #' @return A single number value for the annual GDP of a country in terms of #' million dollars. Values are in 2018 terms. #' @keyword internal get_annual_gdp <- function(x) { - stopifnot( - "`x` must be a string giving a country name from `daedalus::country_names" = - is.character(x) && x %in% daedalus::country_names - ) - n_days <- 365 x <- daedalus::daedalus_country(x) diff --git a/inst/json/metadata_0.1.0.json b/inst/json/metadata_0.1.0.json index 894f1b2..d3d3fba 100644 --- a/inst/json/metadata_0.1.0.json +++ b/inst/json/metadata_0.1.0.json @@ -6,7 +6,7 @@ "label": "Country", "description": "Select a country to set model parameters for country-specific demographic, patterns of social mixing and economic data", "parameterType": "globeSelect", - "defaultOption": "Thailand", + "defaultOption": "THA", "ordered": false, "options": null }, diff --git a/tests/testthat/test-endpoints.R b/tests/testthat/test-endpoints.R index aa27815..c52b6a9 100644 --- a/tests/testthat/test-endpoints.R +++ b/tests/testthat/test-endpoints.R @@ -53,26 +53,27 @@ test_that("Can get metadata", { ) country_idx <- match("country", expected_parameters) country_options <- params[[country_idx]]$options - daedalus_countries <- daedalus::country_names + daedalus_country_codes <- daedalus::country_codes_iso3c # expect country ids to match those from daedalus expect_identical( vapply(country_options, function(option) { option$id }, character(1)), - daedalus_countries + daedalus_country_codes ) # expect country labels to match those from daedalus + daedalus_country_names <- daedalus::country_names expect_identical( vapply(country_options, function(option) { option$label }, character(1)), - daedalus_countries + daedalus_country_names ) - expect_identical(params[[country_idx]]$defaultOption, "Thailand") + expect_identical(params[[country_idx]]$defaultOption, "THA") hosp_cap_idx <- match("hospital_capacity", expected_parameters) update_values <- res$data$parameters[[hosp_cap_idx]]$updateNumericFrom$values - expect_named(update_values, daedalus_countries) - for (country in daedalus_countries) { + expect_named(update_values, daedalus_country_codes) + for (country in daedalus_country_codes) { values <- update_values[[country]] expect_identical(values$min %% 100, 0) expect_identical(values$default %% 100, 0) diff --git a/tests/testthat/test-zzz-e2e.R b/tests/testthat/test-zzz-e2e.R index 7be565e..be26484 100644 --- a/tests/testthat/test-zzz-e2e.R +++ b/tests/testthat/test-zzz-e2e.R @@ -38,7 +38,7 @@ test_that("can run model, get status and results", { data <- list( modelVersion = "0.0.2", parameters = list( - country = "United Kingdom", + country = "GBR", pathogen = "sars_cov_1", response = "economic_closures", vaccine = "low", diff --git a/tests/testthat/test_model_run.R b/tests/testthat/test_model_run.R index 4ef3b42..297a314 100644 --- a/tests/testthat/test_model_run.R +++ b/tests/testthat/test_model_run.R @@ -21,7 +21,7 @@ test_that("can run model and return results", { mockery::stub(model_run, "get_annual_gdp", mock_get_gdp) parameters <- list( - country = "Canada", + country = "CAN", pathogen = "influenza_1918", response = "elimination", vaccine = "high", @@ -31,7 +31,7 @@ test_that("can run model and return results", { expect_identical( mockery::mock_args(mock_daedalus)[[1]], - list("Canada", + list("CAN", "influenza_1918", response_strategy = "elimination", response_threshold = 4500,