Skip to content

Commit

Permalink
Merge pull request #6 from CDCgov/V1.6-2024-03-11-run
Browse files Browse the repository at this point in the history
V1.6 2024 03 11 run
  • Loading branch information
kaitejohnson authored Mar 11, 2024
2 parents eb36396 + a7b2103 commit 2b1a2b8
Show file tree
Hide file tree
Showing 24 changed files with 34,470 additions and 147 deletions.
16 changes: 7 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@ secrets.yaml
*.pdf
*.docx

# Don't track compiled model executable

cfaforecastrenewalww/inst/stan/renewal_ww_hosp
cfaforecastrenewalww/inst/stan/renewal_ww_hosp_hierarchical_w_dispersion
cfaforecastrenewalww/inst/stan/renewal_ww_hosp_site_level_phi
cfaforecastrenewalww/inst/stan/renewal_ww_hosp_site_level_phi_varying_C
cfaforecastrenewalww/inst/stan/renewal_ww_hosp_hierarchical
cfaforecastrenewalww/inst/stan/renewal_ww_hosp_site_level_inf_dynamics
# Don't track compiled model executables
bin

# Don't track data by default
repo_data/
Expand All @@ -37,10 +31,14 @@ output/*
!input/locations.csv
!input/params.toml

# Do track the output/forecasts folder,
# Do track the output/forecasts directory
!output/forecasts
!output/forecasts/*

# Do track the output/decision_archive directory
!output/decision_archive
!output/decision_archive/*

# compiled test executables
test/test_expgamma_lpdf

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ Alternatively, in an interactive R session with your R working directory set to
targets::tar_make()
```

The first time you run the pipeline after installing `cfaforecastrenewalww`, `cmdstan` will compile the model source `.stan` files to executible binaries, which by default are stored in a subdirectory `bin/` of the top-level project directory. Subsequent runs should use those precompiled executibles, without need for recompilation. To force recompilation, delete the binaries stored in the `bin/` directory or reinstall the `cfaforecastrenewalww` R package.

# Contributing to this project

## Git workflow
Expand Down
62 changes: 40 additions & 22 deletions _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,10 @@ list(
priority = 1
),
tar_target(
name = model_file_id,
command = ww_model(model_file_path_id),
name = model_object_id,
command = compile_model(model_file_path_id,
target_dir = "bin"
),
deployment = "main",
priority = 1
),
Expand Down Expand Up @@ -256,14 +258,16 @@ list(
name = df_of_filepaths_id,
command = do.call(
fit_site_level_model,
c(list(train_data = grouped_data_id),
list(params = params_id),
model_file = model_file_id,
c(
list(
train_data = grouped_data_id,
params = params_id,
model_file = model_object_id
),
config_vars_id
)
),
pattern = map(grouped_data_id),
iteration = "group",
deployment = "worker",
priority = 1
),
Expand Down Expand Up @@ -402,8 +406,10 @@ list(
deployment = "main"
),
tar_target(
name = model_file_ho,
command = ww_model(model_file_path_ho),
name = model_object_ho,
command = compile_model(model_file_path_ho,
target_dir = "bin"
),
deployment = "main"
),
tar_target(
Expand Down Expand Up @@ -431,13 +437,18 @@ list(
# generated quantities and the parameters
tar_target(
name = df_of_filepaths_ho,
command = do.call(fit_aggregated_model, c(list(train_data = grouped_data),
list(params = params_ho),
model_file = model_file_ho,
config_vars_ho
)),
command = do.call(
fit_aggregated_model,
c(
list(
train_data = grouped_data,
params = params_ho,
model_file = model_object_ho
),
config_vars_ho
)
),
pattern = map(grouped_data),
iteration = "group",
deployment = "worker"
),
tar_target(
Expand Down Expand Up @@ -526,8 +537,10 @@ list(
deployment = "main"
),
tar_target(
name = model_file_sa,
command = ww_model(model_file_path_sa),
name = model_object_sa,
command = compile_model(model_file_path_sa,
target_dir = "bin"
),
deployment = "main"
),
tar_target(
Expand Down Expand Up @@ -562,13 +575,18 @@ list(
# generated quantities and the parameters
tar_target(
name = df_of_filepaths_sa,
command = do.call(fit_aggregated_model, c(list(train_data = grouped_data_sa),
list(params = params_sa),
model_file = model_file_sa,
config_vars_sa
)),
command = do.call(
fit_aggregated_model,
c(
list(
train_data = grouped_data_sa,
params = params_sa,
model_file = model_object_sa
),
config_vars_sa
)
),
pattern = map(grouped_data_sa),
iteration = "group",
deployment = "worker"
),
tar_target(
Expand Down
5 changes: 3 additions & 2 deletions cfaforecastrenewalww/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ Imports:
posterior,
scoringutils,
tidyr,
targets,
targets (>= 1.5.1),
tarchetypes,
gridExtra,
rlang,
RcppTOML,
RcppEigen
RcppEigen,
crew (>= 0.9.0)
VignetteBuilder:
knitr
Suggests:
Expand Down
2 changes: 1 addition & 1 deletion cfaforecastrenewalww/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(aggregate_ww)
export(boxplot_whiskers)
export(categorical_entropy)
export(cleanup_secrets)
export(compile_model)
export(convert_to_logmean)
export(convert_to_logsd)
export(create_dir)
Expand Down Expand Up @@ -109,7 +110,6 @@ export(to_simplex)
export(trajectories_to_quantiles)
export(validate_paramlist)
export(varies)
export(ww_model)
export(zoom_boxplot_y)
import(boot)
import(cmdstanr)
Expand Down
91 changes: 91 additions & 0 deletions cfaforecastrenewalww/R/compile_model.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#' This code was adapted from code written
#' (under an MIT license) as part of the `epinowcast`
#' package (https://github.com/epinowcast/epinowcast)

#' Compile a stan model while pointing at the package default
#' include directory (`stan`) for #include statements
#'
#' compile_model
#' @description
#' This function reads in and optionally compiles a Stan model.
#' It is written to search the installed package `stan` directory
#' for additional source files to include. Within each stan file,
#' use #include {path to your file with the `stan` directory}.stan
#'
#' @param model_filepath path to .stan file defining the model
#' @param include_paths path(s) to directories to search for files
#' specified in #include statements. Passed to [cmdstanr::cmdstan_model()].
#' Defaults to the `stan` subdirectory of the installed
#' `cfaforecastrenewalww` package.
#' @param threads Number of threads to use in model compilation,
#' as an integer. Passed to [cmdstanr::cmdstan_model()].
#' Default `FALSE` (use single-threaded compilation).
#' @param target_dir Directory in which to save the compiled
#' stan model binary. Passed as the `dir` keyword argument to
#' [cmdstanr::cmdstan_model()]. Defaults to a temporary directory
#' for the R sessions (the output of [tempdir()]).
#' @param stanc_options Options for the stan compiler passed to
#' [cmdstanr::cmdstan_model()], as a list. See that function's
#' documentation for more details. Default `list()` (use default
#' options).
#' @param cpp_options Options for the C++ compiler passed to
#' [cmdstanr::cmdstan_model()], as a list. See that function's
#' documentation for more details. Default `list()` (use default
#' options).
#' @param verbose Write detailed output to the terminal while
#' executing the function? Boolean, default `TRUE`.
#' @param ... Additional keyword arguments passed to
#' [cmdstanr::cmdstan_model()].
#'
#' @return The resulting `cmdstanr` model object, as the output
#' of [cmdstanr::cmdstan_model()].
#' @export
compile_model <- function(model_filepath,
include_paths = system.file(
"stan",
package = "cfaforecastrenewalww"
),
threads = FALSE,
target_dir = tempdir(),
stanc_options = list(),
cpp_options = list(),
verbose = TRUE,
...) {
if (verbose) {
cli::cli_inform(
glue::glue(paste0(
"Using model source file: ",
"{model_filepath}"
))
)
cli::cli_inform(
sprintf(
"Using include paths: %s",
toString(include_paths)
)
)
}

create_dir(target_dir)

model <- cmdstanr::cmdstan_model(
model_filepath,
include_paths = include_paths,
compile = TRUE,
stanc_options = stanc_options,
cpp_options = cpp_options,
threads = threads,
dir = target_dir,
...
)

if (verbose) {
cli::cli_inform(paste0(
"Model compiled successfully; ",
"model executable binary located at: ",
"{model$exe_file()}"
))
}

return(model)
}
55 changes: 0 additions & 55 deletions cfaforecastrenewalww/R/fit_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,6 @@
#' (under an MIT license) as part of the `epinowcast`
#' package (https://github.com/epinowcast/epinowcast)

# Compile a stan model and include stan function files
#' ww_model
#' @description
#' This function compiles the stan model, and is written to include the 'stan'
#' folder. Within each stan file, to include the functions, use #include
#' functions/{your_function_file}.stan
#'
#'
#' @param model
#' @param include
#' @param compile
#' @param threads
#' @param target_dir
#' @param stanc_options
#' @param cpp_options
#' @param verbose
#' @param ...
#'
#' @return
#' @export
#'
#' @examples
ww_model <- function(model,
include = system.file("stan",
package = "cfaforecastrenewalww"
),
compile = TRUE, threads = FALSE,
target_dir = tempdir(), stanc_options = list(),
cpp_options = list(), verbose = TRUE, ...) {
if (verbose) {
message(glue::glue("Using model {model}."))
message(sprintf("include is %s.", toString(include)))
}

if (compile) {
monitor <- suppressMessages
if (verbose) {
monitor <- function(x) {
return(x)
}
}
cpp_options$stan_threads <- threads
model <- monitor(cmdstanr::cmdstan_model(
model,
include_paths = include,
stanc_options = stanc_options,
cpp_options = cpp_options,
...
))
}
return(model)
}



#' Get dataframe of filepaths
#'
#' @param output_dir
Expand Down
18 changes: 6 additions & 12 deletions cfaforecastrenewalww/R/get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ get_stan_data <- function(train_data,
p_hosp_mean = p_hosp_mean,
p_hosp_sd_logit = p_hosp_sd_logit,
p_hosp_w_sd_sd = p_hosp_w_sd_sd,
infection_feedback_prior_mean = infection_feedback_prior_mean,
infection_feedback_prior_sd = infection_feedback_prior_sd
inf_feedback_prior_logmean = infection_feedback_prior_logmean,
inf_feedback_prior_logsd = infection_feedback_prior_logsd
)

return(data_renewal)
Expand Down Expand Up @@ -475,8 +475,8 @@ get_stan_data_site_level_model <- function(train_data,
p_hosp_sd_logit = p_hosp_sd_logit,
p_hosp_w_sd_sd = p_hosp_w_sd_sd,
ww_site_mod_sd_sd = ww_site_mod_sd_sd,
infection_feedback_prior_mean = infection_feedback_prior_mean,
infection_feedback_prior_sd = infection_feedback_prior_sd
inf_feedback_prior_logmean = infection_feedback_prior_logmean,
inf_feedback_prior_logsd = infection_feedback_prior_logsd
)

if (model_type == "site-level time-varying concentration") {
Expand Down Expand Up @@ -551,10 +551,7 @@ state_agg_inits <- function(train_data, params, stan_data) {
dur_shed = rnorm(1, duration_shedding_mean, 0.1 * duration_shedding_sd),
log10_g = rnorm(1, log10_g_prior_mean, 0.5),
hosp_wday_effect = to_simplex(rnorm(7, 1 / 7, 0.01)),
infection_feedback = abs(rnorm(
1, infection_feedback_prior_mean,
0.1 * infection_feedback_prior_sd
))
infection_feedback = abs(rnorm(1, 500, 20))
)
return(init_list)
}
Expand Down Expand Up @@ -755,10 +752,7 @@ site_level_inf_inits <- function(train_data, params, stan_data) {
ww_site_mod_raw = abs(rnorm(n_ww_lab_sites, 0, 0.05)),
ww_site_mod_sd = abs(rnorm(1, 0, 0.05)),
hosp_wday_effect = to_simplex(rnorm(7, 1 / 7, 0.01)),
infection_feedback = abs(rnorm(
1, infection_feedback_prior_mean,
0.5 * infection_feedback_prior_sd
))
infection_feedback = abs(rnorm(1, 500, 20))
)


Expand Down
Binary file modified cfaforecastrenewalww/R/sysdata.rda
Binary file not shown.
Loading

0 comments on commit 2b1a2b8

Please sign in to comment.