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

V1.6 2024 03 11 run #6

Merged
merged 6 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
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
68 changes: 43 additions & 25 deletions _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ list(
command = write_config(
save_config = TRUE,
location = NULL,
prod_run = TRUE,
prod_run = FALSE,
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
run_id = run_id,
ww_geo_type = "state",
date_run = date_run,
Expand All @@ -132,7 +132,7 @@ list(
command = write_config(
save_config = TRUE,
location = "US",
prod_run = TRUE,
prod_run = FALSE,
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
run_id = run_id,
ww_geo_type = "state",
date_run = date_run,
Expand All @@ -152,7 +152,7 @@ list(
command = write_config(
save_config = TRUE,
location = NULL,
prod_run = TRUE,
prod_run = FALSE,
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
run_id = run_id,
date_run = date_run,
model_type = "site-level infection dynamics",
Expand Down 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
Loading
Loading