Skip to content

Commit

Permalink
jidea-136 use iso3 codes as country ids (#28)
Browse files Browse the repository at this point in the history
* 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 <r.fitzjohn@imperial.ac.uk>

---------

Co-authored-by: Rich FitzJohn <r.fitzjohn@imperial.ac.uk>
  • Loading branch information
EmmaLRussell and richfitz authored Oct 21, 2024
1 parent 0e437f5 commit d260cf9
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 24 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand All @@ -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,
Expand Down
13 changes: 7 additions & 6 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
})

Expand Down
9 changes: 3 additions & 6 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion inst/json/metadata_0.1.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
13 changes: 7 additions & 6 deletions tests/testthat/test-endpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-zzz-e2e.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_model_run.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down

0 comments on commit d260cf9

Please sign in to comment.