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

SHIPP tool integration #413

Open
wants to merge 69 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
2637919
add function to read Spectrum KP table
jeffeaton Oct 17, 2023
34a1730
initial commit
rtesra Nov 1, 2023
877056c
Merge branch 'master' into mrc-4652
r-ash Nov 3, 2023
b1607a7
Add WIP
r-ash Nov 6, 2023
f68acc0
Add WIP impl
r-ash Nov 6, 2023
0769c86
fix incidence calculations
rtesra Nov 14, 2023
d824b88
remove survey data from prevalence calculation
rtesra Nov 15, 2023
24be9e3
update workflow to pull in updated KP estimates
rtesra Nov 17, 2023
c634f8b
Merge branch 'agyw-tool-integration' into mrc-4652
r-ash Dec 6, 2023
d9ae04e
Use new format of workbook
r-ash Dec 7, 2023
4f79a95
add labels to aggregated data and patch for naomi.resource update
rtesra Dec 8, 2023
1437926
Merge branch 'agyw-tool-integration' into mrc-4652
rtesra Dec 11, 2023
b3d5ef7
Update test-downloads.R
rtesra Dec 12, 2023
34d1de2
update test data and fix bugs
rtesra Dec 12, 2023
0d48f75
Update to match renaming of columns in ASF data in naomi.resources
rtesra Dec 13, 2023
d18a72c
Update agyw integration code
krisher1 Dec 13, 2023
5cb895c
Merge branch 'master' into agyw-tool-integration
rtesra Dec 13, 2023
97cb487
Wire up data with AGYW template
r-ash Dec 13, 2023
f897636
Fix spdep warning by explicitly setting style for mat2listw
r-ash Dec 13, 2023
1030010
Suppress single warning
r-ash Dec 14, 2023
0ed3ac9
Merge branch 'pr/419' into agyw-tool-integration
rtesra Dec 19, 2023
df0e2e9
Merge branch 'agyw-tool-integration' of https://github.com/mrc-ide/na…
rtesra Dec 19, 2023
c87824d
align katie's code and clean up documentation
rtesra Dec 20, 2023
03b8ecd
Merge remote-tracking branch 'origin/pjnz-kp-extract' into agyw-tool-…
rtesra Dec 20, 2023
6484369
account for all label configurations
rtesra Dec 22, 2023
ee61e50
scale to spectrum kp workbook consensus estimates
rtesra Dec 22, 2023
4544245
remove tetsing files :(
rtesra Dec 22, 2023
3a48a40
Update NEWS.md
rtesra Dec 22, 2023
4f1358c
Use with_mocked_bindings over with_mock
r-ash Dec 22, 2023
b8721ea
Add note about with_mocked_bindings
r-ash Dec 22, 2023
dd18af0
Fix up test, regen docs
r-ash Dec 22, 2023
5b68931
Set min naomi.resources version
r-ash Dec 22, 2023
3e83bfe
Move hintr agyw test helper into Naomi
r-ash Jan 11, 2024
1dffc41
Update R/agyw-integration.R
rtesra Jan 12, 2024
873a7b1
fix up documentation
rtesra Jan 12, 2024
46f4feb
Merge branch 'agyw-tool-integration' of https://github.com/mrc-ide/na…
rtesra Jan 12, 2024
d4fc44f
add checks for alignment between agyw resources and naomi estimates
rtesra Jan 12, 2024
8ff8064
Merge branch 'master' into agyw-tool-integration
r-ash Jan 15, 2024
669ec99
Merge branch 'master' into agyw-tool-integration
r-ash Jan 16, 2024
bd34f08
Regen docs
r-ash Jan 16, 2024
ba7d6a2
Fix issue with incorrect function arg
r-ash Jan 16, 2024
eaa0d05
Remove unused helper function
r-ash Jan 17, 2024
d7aee39
add agyw vignette
rtesra Jan 22, 2024
31162e8
Squashed commit of the following:
rtesra Jan 22, 2024
04e7e40
update code to clean up country names for workbook
rtesra Jan 22, 2024
e923fce
add DOI for Hines 2020 paper
rtesra Jan 23, 2024
611c9fd
add warning for bad KP workbook consensus estimates
rtesra Jan 23, 2024
f84c756
Bump required version of naomi.resource and update branch pin
r-ash Jan 24, 2024
a4c6fe0
Update write sheet to work with file which has existing column headers
r-ash Jan 24, 2024
a10bfcd
Return error message with context should generation fail
r-ash Jan 24, 2024
dff5bf0
fix failing test for scaling to kp workbook consensus estimate
rtesra Jan 25, 2024
95da62c
remove warnings that are not output anywhere
rtesra Jan 25, 2024
69359ff
Remove branch pin, set duckdb name in DESCRIPTION
r-ash Jan 25, 2024
7c8d87a
Update agyw-integration.R
rtesra Jan 25, 2024
e2b0088
Add KP adjustment for new infections
krisher1 Jan 31, 2024
9160a40
Merge branch 'agyw-tool-integration' into pse-tool-update-incidence
rtesra Feb 5, 2024
318d350
Merge pull request #430 from krisher1/pse-tool-update-incidence
rtesra Feb 5, 2024
3063d17
rename agyw -> shipp
rtesra Feb 5, 2024
7b2ea08
Scale new infection to KP workbook or GOALS
rtesra Feb 5, 2024
e35a4a0
Update SHIPP vignette + some code fixes
krisher1 Feb 19, 2024
3b6db4d
Merge branch 'master' into pse-tool-update-incidence
rtesra Feb 19, 2024
7b6f99e
Sum infections_pwid not pwid
krisher1 Feb 19, 2024
7ad6134
Merge branch 'pse-tool-update-incidence' of https://github.com/mrc-id…
krisher1 Feb 19, 2024
aeb9ad6
Merge branch 'master' into agyw-tool-integration
rtesra Feb 21, 2024
2b649ce
Force installation of latest naomi.resources from github
r-ash Feb 21, 2024
2dc75ef
Bump version number
r-ash Feb 22, 2024
d37d5be
edge case for 0 pse
rtesra Feb 26, 2024
99f9349
Merge branch 'agyw-tool-integration' of https://github.com/mrc-ide/na…
rtesra Feb 26, 2024
8aaa93b
aggregate KP workbook from subnational pjnz files
rtesra Feb 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ tests/testthat/testdata/fit.RDS
.vscode
.Rprofile
.idea

.DS_Store
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: naomi
Title: Naomi Model for Subnational HIV Estimates
Version: 2.9.23
Version: 2.9.24
Authors@R:
person(given = "Jeff",
family = "Eaton",
Expand Down Expand Up @@ -36,6 +36,7 @@ Imports:
knitr,
magrittr,
mvtnorm,
naomi.resources (>= 0.0.3),
naomi.options (>= 1.2.0),
openxlsx,
plotly,
Expand All @@ -51,7 +52,6 @@ Imports:
traduire,
utils,
withr,
writexl,
yaml,
zip,
zoo
Expand All @@ -78,8 +78,9 @@ Remotes:
first90=mrc-ide/first90release,
reside-ic/traduire,
mrc-ide/naomi.options,
mrc-ide/naomi.resources,
mrc-ide/mockr,
mrc-ide/testthat.buildkite,
duckdb/[email protected]
duckdb=duckdb/[email protected]
Config/testthat/edition: 3
Config/testthat/parallel: true
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export(get_plotting_metadata)
export(hintr_calibrate)
export(hintr_calibrate_plot)
export(hintr_comparison_plot)
export(hintr_prepare_agyw_download)
export(hintr_prepare_coarse_age_group_download)
export(hintr_prepare_comparison_report_download)
export(hintr_prepare_shipp_download)
export(hintr_prepare_spectrum_download)
export(hintr_prepare_summary_report_download)
export(hintr_run_model)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# naomi 2.9.24

* Generate PSE workbook from naomi outputs.
* Add function `extract_kp_workbook()` to extract key population totals from the Spectrum PJNZ file. Data are extracted from summary table saved in AIM Programme Statistics input for key populations.

# naomi 2.9.23

* Update Datim UIDs for Ethiopia 2024 boundary division.
Expand Down
26 changes: 17 additions & 9 deletions R/downloads.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,39 @@ hintr_prepare_comparison_report_download <- function(output,
)
}

#' Prepare AGYW tool download
#' Prepare SHIPP tool download
#'
#' @param hintr_output object
#' @param path Path to save output file
#' @param pjnz Path to input PJNZ file
#'
#' @return Path to output file and metadata for file
#' @export
hintr_prepare_agyw_download <- function(output, pjnz,
hintr_prepare_shipp_download <- function(output, pjnz,
path = tempfile(fileext = ".xlsx")) {
## TODO: Do we need a version restriction on this?
assert_model_output_version(output, "2.7.16")
progress <- new_simple_progress()
progress$update_progress("PROGRESS_DOWNLOAD_AGYW")
dummy_data <- data.frame(x = c(1, 2, 3), y = c(3, 4, 5))
writexl::write_xlsx(list(sheet = dummy_data), path = path)
progress$update_progress("PROGRESS_DOWNLOAD_SHIPP")

risk_populations <- shipp_generate_risk_populations(output$model_output_path,
pjnz)

sheets <- list(
"All outputs - F" = risk_populations$female_incidence,
"All outputs - M" = risk_populations$male_incidence,
"NAOMI outputs" = risk_populations$naomi_output
)
write_shipp_workbook(sheets, dest = path)

model_output <- read_hintr_output(output$model_output_path)
options <- yaml::read_yaml(text = model_output$info$options.yml)
list(
path = path,
metadata = list(
description = build_agyw_tool_description(options),
description = build_shipp_tool_description(options),
areas = options$area_scope,
type = "agyw"
type = "shipp"
)
)
}
Expand All @@ -142,8 +150,8 @@ build_comparison_report_description <- function(options) {
build_description(t_("DOWNLOAD_COMPARISON_DESCRIPTION"), options)
}

build_agyw_tool_description <- function(options) {
build_description(t_("DOWNLOAD_AGYW_DESCRIPTION"), options)
build_shipp_tool_description <- function(options) {
build_description(t_("DOWNLOAD_SHIPP_DESCRIPTION"), options)
}

build_description <- function(type_text, options) {
Expand Down
114 changes: 110 additions & 4 deletions R/inputs-spectrum.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ extract_pjnz_one <- function(pjnz, extract_shiny90) {
#' @export
#'
extract_pjnz_program_data <- function(pjnz_list) {


pjnz_list <- unroll_pjnz(pjnz_list)

region_code <- lapply(pjnz_list, read_spectrum_region_code)
Expand Down Expand Up @@ -185,7 +187,7 @@ read_dp_art_dec31 <- function(dp) {

## In Spectrum 2023, "<NeedARTDec31 MV>" was updated to include children in the totals
## -> now need to sum over 5-year age groups for age 15+ to get the adult ART need

male_15plus_needart <- dpsub("<NeedARTDec31 MV>", 4:17*3 + 3, timedat.idx)
male_15plus_needart <- vapply(lapply(male_15plus_needart, as.numeric), sum, numeric(1))

Expand All @@ -195,14 +197,14 @@ read_dp_art_dec31 <- function(dp) {
art15plus_need <- rbind(male_15plus_needart, female_15plus_needart)
dimnames(art15plus_need) <- list(sex = c("male", "female"), year = proj.years)


if (any(art15plus_num[art15plus_isperc == 1] < 0 |
art15plus_num[art15plus_isperc == 1] > 100)) {
stop("Invalid percentage on ART entered for adult ART")
}

## # Adult on ART adjustment factor
##
##
## * Implemented from around Spectrum 6.2 (a few versions before)
## * Allows user to specify scalar to reduce number on ART in each year ("<AdultARTAdjFactor>")
## * Enabled / disabled by checkbox flag ("<AdultARTAdjFactorFlag>")
Expand Down Expand Up @@ -281,7 +283,7 @@ read_dp_art_dec31 <- function(dp) {
names(child_art) <- proj.years

## # Child on ART adjustment factor
##
##
## * Implemented same as adult adjustment factor above

if (exists_dptag("<ChildARTAdjFactorFlag>") &&
Expand Down Expand Up @@ -684,7 +686,111 @@ extract_eppasm_pregprev <- function(mod, fp, years = NULL) {
df
}



read_dp <- function(pjnz) {
dpfile <- grep(".DP$", utils::unzip(pjnz, list = TRUE)$Name, value = TRUE)
utils::read.csv(unz(pjnz, dpfile), as.is = TRUE)
}

#' Read key population summary data from PJNZ
#'
#' Reads key population summary data from Spectrum PJNZ.
#'
#' @param pjnz_list path to PJNZ file or zip of multiple PJNZ files
#'
#'
#' @noRd
#'

extract_kp_workbook <- function(pjnz_list){

# Extract spectrum files
pjnz_list <- unroll_pjnz(pjnz_list)

# Extract .DP files
dp <- lapply(pjnz_list, read_dp)

# Extract kp workbook summary
kp <- lapply(dp, read_dp_keypop_summary)

# Filter for spectrum file with consensus estimates
kp_out <- kp %>%
dplyr::bind_rows() %>%
dplyr::filter(!is.na(year))

# If no consensus estimates present, return empty dataframe
if(nrow(kp_out) == 0){kp_out <- kp[[1]]}

# If multiple pjnz files, aggreagte consensus estimates
if(nrow(kp_out) > 4){

kp_out <- kp_out |>
dplyr::group_by(key_population, year, workbook_file) |>
dplyr::summarise(population_size = sum(population_size),
hiv_prevalence = sum(hiv_prevalence),
art_coverage = sum(art_coverage),
infections = sum(infections))

}

kp_out

}


#' Read key population summary data from PJNZ
#'
#' Reads key population summary data from Spectrum PJNZ.
#'
#' @param pjnz path to PJNZ file
#'
#' @examples
#' pjnz <- system.file("extdata/demo_mwi2019.PJNZ", package = "naomi")
#' dp <- dp <- naomi:::read_dp(pjnz)
#' read_dp_keypop_summary(dp)
#'
#' @noRd
#'
read_dp_keypop_summary <- function(dp) {

exists_dptag <- function(tag, tagcol = 1) {
tag %in% dp[, tagcol]
}
dpsub <- function(tag, rows, cols, tagcol = 1) {
dp[which(dp[, tagcol] == tag) + rows, cols]
}

kp_name <- c("FSW", "MSM", "TG", "PWID")

if (exists_dptag("<KeyPops MV>")) {
kp_tab <- dpsub("<KeyPops MV>", 2:5, 4:7)
kp_tab <- sapply(kp_tab, as.numeric)
} else {
kp_tab <- matrix(NA, 4, 4)
}

if (exists_dptag("<KeyPopsYear MV>")) {
kp_year <- as.integer(dpsub("<KeyPopsYear MV>", 2, 4))
} else {
kp_year <- NA_integer_
}

if (exists_dptag("<KeyPopsFName MV>")) {
kp_file <- as.character(dpsub("<KeyPopsFName MV>", 2, 4))
} else {
kp_file <- NA_character_
}

kp_summary <- data.frame(
key_population = kp_name,
year = kp_year,
population_size = kp_tab[1, ],
hiv_prevalence = kp_tab[2, ],
art_coverage = kp_tab[3, ],
infections = kp_tab[4, ],
workbook_file = kp_file
)

kp_summary
}
Loading