Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jidea-110 New time series #30

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions R/model_run.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ model_run <- function(parameters, model_version) {
)
time_series$vaccinated <- vax_time_series$vaccinated

# get incidence time series
incidences <- daedalus::get_incidence(model_results)
incidences <- tidyr::pivot_wider(
incidences, id_cols = "time", names_from = "measure"
)
time_series$new_infected <- incidences$daily_infections
time_series$new_hospitalised <- incidences$daily_hospitalisations
time_series$new_dead <- incidences$daily_deaths

time_series$new_vaccinated <-
daedalus::get_new_vaccinations(model_results)$new_vaccinations

raw_costs <- daedalus::get_costs(model_results)
costs <- get_nested_costs(raw_costs)

Expand Down
44 changes: 43 additions & 1 deletion inst/json/metadata_0.1.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,49 @@
"description": "Number of infectious individuals, comprising both symptomatic and asymptomatic infections, including those in need of hospitalisation." },
{ "id": "hospitalised", "label": "Hospital demand", "description": "Infections requiring hospitalisation" },
{ "id": "dead", "label": "Dead", "description": "Total deaths" },
{ "id": "vaccinated", "label": "Vaccinated", "description": "Total number of vaccinations administered" }
{ "id": "vaccinated", "label": "Vaccinated", "description": "Total number of vaccinations administered" },
{ "id": "new_infected", "label": "New infections", "description": "Number of new infections per day" },
{ "id": "new_hospitalised", "label": "New hospitalisations", "description": "Number of new patients in need of hospitalisation per day" },
{ "id": "new_dead", "label": "New deaths", "description": "Number of deaths per day" },
{ "id": "new_vaccinated", "label": "New vaccinations", "description": "Number of vaccinations per day" }
],
"time_series_roles": [
{"id": "total", "label": "Total"},
{"id": "daily", "label": "Daily"}
],
"time_series_groups": [
{
"id": "infections",
"label": "Infections",
"time_series": {
"total": "prevalence",
"daily": "new_infected"
}
},
{
"id": "hospitalisations",
"label": "Hospitalisations",
"time_series": {
"total": "hospitalised",
"daily": "new_hospitalised"
}
},
{
"id": "deaths",
"label": "Deaths",
"time_series": {
"total": "dead",
"daily": "new_dead"
}
},
{
"id": "vaccinations",
"label": "Vaccinations",
"time_series": {
"total": "vaccinated",
"daily": "new_vaccinated"
}
}
]
}
}
19 changes: 19 additions & 0 deletions inst/schema/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@
"items": {
"$ref": "#/$defs/displayInfo"
}
},
"time_series_roles": {
"type": "array",
"items": {
"$ref": "#/$defs/displayInfo"
}
},
"time_series_groups": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "string" },
"label": { "type": "string" },
"time_series": { "type": "object" }
Copy link
Contributor

@david-mears-2 david-mears-2 Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no way to specify that this object's keys must be 'time_series_role's? If not, what good does it do to define the time_series_roles array?

Copy link
Contributor Author

@EmmaLRussell EmmaLRussell Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, in the schema. The reason for putting in the time_series_roles array was to include metadata, which the front end might choose to use i.e. label.

},
"additionalProperties": false,
"required": ["id", "label", "time_series"]
}
}
},
"additionalProperties": false,
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-zzz-e2e.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ test_that("can run model, get status and results", {
expect_length(results_data$time_series$prevalence, time_series_length)
expect_length(results_data$time_series$hospitalised, time_series_length)
expect_length(results_data$time_series$dead, time_series_length)
expect_length(results_data$time_series$new_infected, time_series_length)
expect_length(results_data$time_series$new_hospitalised,
time_series_length)
expect_length(results_data$time_series$new_dead, time_series_length)
expect_length(results_data$time_series$new_vaccinated, time_series_length)

expect_gt(results_data$gdp, 0)

Expand Down
32 changes: 31 additions & 1 deletion tests/testthat/test_model_run.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ test_that("can run model and return results", {

mockery::stub(model_run, "daedalus::get_data", mock_model_data)

mock_incidence_result <- data.frame(
time = c(1, 2, 1, 2, 1, 2),
measure = c("daily_infections", "daily_infections",
"daily_hospitalisations", "daily_hospitalisations",
"daily_deaths", "daily_deaths"),
value = c(100L, 200L, 10L, 20L, 1L, 2L),
stringsAsFactors = FALSE
)
mock_get_incidence <- mockery::mock(mock_incidence_result,
cycle = TRUE)
mockery::stub(model_run, "daedalus::get_incidence", mock_get_incidence)

mock_new_vaccinations_result <- list(new_vaccinations = c(100, 200))
mockery::stub(model_run, "daedalus::get_new_vaccinations",
mock_new_vaccinations_result)

mock_costs_data <- daedalus_mock_costs()
mock_get_costs <- mockery::mock(mock_costs_data)
mockery::stub(model_run, "daedalus::get_costs", mock_get_costs)
Expand All @@ -39,6 +55,11 @@ test_that("can run model and return results", {
)
)

expect_identical(
mockery::mock_args(mock_get_incidence)[[1]],
list(mock_results)
)

expect_named(res, c(
"parameters",
"costs",
Expand All @@ -50,11 +71,20 @@ test_that("can run model and return results", {
expect_named(res$time_series, c("prevalence",
"hospitalised",
"dead",
"vaccinated"))
"vaccinated",
"new_infected",
"new_hospitalised",
"new_dead",
"new_vaccinated"))
expect_identical(res$time_series$prevalence, c(28L, 87L))
expect_identical(res$time_series$hospitalised, c(11L, 31L))
expect_identical(res$time_series$dead, c(15L, 37L))
expect_identical(res$time_series$vaccinated, c(1L, 5L))
expect_identical(res$time_series$new_infected, c(100L, 200L))
expect_identical(res$time_series$new_hospitalised, c(10L, 20L))
expect_identical(res$time_series$new_dead, c(1L, 2L))
expect_identical(res$time_series$new_vaccinated,
mock_new_vaccinations_result$new_vaccinations)
expect_identical(res$parameters, parameters)
expect_nested_mock_costs(res$costs)
expect_identical(res$interventions, list(
Expand Down
Loading