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

naomi-agyw-tool-integration #478

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Imports:
glue,
ids,
jsonlite (>= 1.2.2),
naomi (>= 2.9.23),
naomi (>= 2.9.24),
naomi.options (>= 1.1.0),
porcelain (>= 0.1.8),
R6,
Expand All @@ -48,7 +48,6 @@ Suggests:
tidyselect,
withr
Remotes:
mrc-ide/geojsonio,
mrc-ide/rrq,
mrc-ide/mockr
Config/testthat/edition: 3
Expand Down
4 changes: 2 additions & 2 deletions R/downloads.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ download <- function(model_output, type, path_results, input, language = NULL) {
add_state_json(out$path, input$state)
}
} else if (type == "agyw") {
out <- naomi::hintr_prepare_agyw_download(model_output, input$pjnz,
download_path)
out <- naomi::hintr_prepare_shipp_download(model_output, input$pjnz$path,
download_path)
} else {
func <- switch(type,
coarse_output = naomi::hintr_prepare_coarse_age_group_download,
Expand Down
4 changes: 2 additions & 2 deletions R/endpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,13 @@ download_result <- function(queue) {
coarse_output = "coarse-output",
summary = "summary-report",
comparison = "comparison-report",
agyw = "AGYW")
shipp = "SHIPP")
ext <- switch(res$metadata$type,
spectrum = ".zip",
coarse_output = ".zip",
summary = ".html",
comparison = ".html",
agyw = ".xlsx")
shipp = ".xlsx")
bytes <- readBin(res$path, "raw", n = file.size(res$path))
bytes <- porcelain::porcelain_add_headers(bytes, list(
"Content-Disposition" = build_content_disp_header(res$metadata$areas,
Expand Down
1 change: 1 addition & 0 deletions R/payload_helpers.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# nocov start
# These are test helpers so skipping coverage checks, they should not
# be used by anything in the package

#' Build JSON from template and a set of params
#'
#' @param template Template JSON of payload
Expand Down
2 changes: 1 addition & 1 deletion docker/build
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ENTRYPOINT [ "/bin/bash", "-l", "-c" ]
EOF

rm -rf naomi
git clone https://github.com/mrc-ide/naomi
git clone --single-branch --branch agyw-tool-integration https://github.com/mrc-ide/naomi

if [ -z $NAOMI_SHA ]; then
git -C $PACKAGE_ROOT/naomi checkout $NAOMI_SHA
Expand Down
22 changes: 19 additions & 3 deletions scripts/run_model
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env Rscript

"Run a model and save outputs at specified paths
Usage:
run_model (--model-payload=<model-payload> --calibrate-payload=<calibrate-payload> --download-payload=<download-payload> --model-output-path=<output-path> --calibrate-output-path=<calibrate-output-path> --calibrate-plot-path=<calibrate-plot-path> --spectrum-path=<spectrum-path> --coarse-output-path=<coarse-output-path> --summary-report-path=<summary-report-path> --agyw-path=<agyw-path>)
Expand Down Expand Up @@ -70,12 +71,27 @@ message(sprintf("Saving coarse output at %s",

summary_report <- naomi::hintr_prepare_summary_report_download(
calibrated, dat$summary_report_path)

message(sprintf("Saving summary report at %s",
normalizePath(dat$summary_report_path, mustWork = TRUE)))

# Create dummy data
# Create naomi outputs with "MWI_demo" iso3 to align with testing data in
# naomi.resources
output <- naomi::read_hintr_output(calibrated$model_output_path)

if(output$output_package$fit$model_options$area_scope == "MWI"){

agyw_test_outputs <- naomi:::make_shipp_testfiles(calibrated)

} else {

agyw_test_outputs <- calibrated

}

agyw_tool <- naomi::hintr_prepare_agyw_download(calibrated,
model_payload$data$pjnz,
dat$agyw_path)
agyw_tool <- naomi::hintr_prepare_shipp_download(agyw_test_outputs,
model_payload$data$pjnz$path,
dat$agyw_path)
message(sprintf("Saving AGYW tool at %s",
normalizePath(dat$agyw_path, mustWork = TRUE)))
6 changes: 6 additions & 0 deletions tests/testthat/helper-queue.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ test_queue_result <- function(model = mock_model, calibrate = mock_calibrate,
)
}

add_queue_result <- function(queue, res) {
new_id <- queue$submit(quote(identity(res)))
queue$queue$task_wait(new_id)
new_id
}

prerun_inputs <- list(
pjnz = "testdata/Malawi2019.PJNZ",
population = "testdata/population.csv",
Expand Down
12 changes: 8 additions & 4 deletions tests/testthat/test-01-endpoints-download.R
Original file line number Diff line number Diff line change
Expand Up @@ -851,20 +851,24 @@ test_that("comparison report download errors for old model output", {
"Model output out of date please re-run model and try again."))
})

test_that("api can create agyw download", {
test_that("api can create agyw/shipp download", {
test_redis_available()
test_mock_model_available()
q <- test_queue_result()
api <- api_build(q$queue)

res <- q$queue$result(q$calibrate_id)
agyw_result <- naomi:::make_shipp_testfiles(res)
agyw_result_id <- add_queue_result(q$queue, agyw_result)

## Prepare body
payload <- setup_payload_download_request(include_notes = FALSE,
include_state = FALSE,
include_pjnz = TRUE)

## Submit download request
submit <- api$request("POST",
paste0("/download/submit/agyw/", q$calibrate_id),
paste0("/download/submit/agyw/", agyw_result_id),
body = payload)
submit_body <- jsonlite::fromJSON(submit$body)
expect_equal(submit$status, 200)
Expand All @@ -890,7 +894,7 @@ test_that("api can create agyw download", {
expect_equal(res$status, 200)
expect_equal(res$headers$`Content-Type`, "application/octet-stream")
expect_match(res$headers$`Content-Disposition`,
'attachment; filename="MWI_AGYW_\\d+-\\d+.xlsx"')
'attachment; filename="MWI_SHIPP_\\d+-\\d+.xlsx"')
## Size of bytes is close to expected
size <- length(res$body)
expect_equal(res$headers$`Content-Length`, size)
Expand All @@ -902,7 +906,7 @@ test_that("api can create agyw download", {
expect_equal(head_res$status, 200)
expect_equal(head_res$headers$`Content-Type`, "application/octet-stream")
expect_match(head_res$headers$`Content-Disposition`,
'attachment; filename="MWI_AGYW_\\d+-\\d+.xlsx"')
'attachment; filename="MWI_SHIPP_\\d+-\\d+.xlsx"')
expect_equal(head_res$headers$`Content-Length`, size)
## Plumber uses an empty string to represent an empty body
expect_null(head_res$body)
Expand Down