Skip to content

Commit

Permalink
Merge feat/vpc
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleam committed Jun 13, 2024
2 parents e48af87 + f81cf2d commit 77cbc1f
Show file tree
Hide file tree
Showing 78 changed files with 3,628 additions and 469 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: bbr
Title: R package for bbi
Version: 1.10.0.8007
Version: 1.10.0.8010
Authors@R:
c(person(given = "Seth",
family = "Green",
Expand Down Expand Up @@ -66,11 +66,12 @@ Imports:
magrittr,
Matrix,
mrgmisc,
nmrec,
nmrec (>= 0.3.0),
processx,
purrr,
readr,
rlang,
stringfish,
stringr,
stringi,
tibble,
Expand Down
31 changes: 22 additions & 9 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,29 @@ S3method(check_nonmem_finished,bbi_base_model)
S3method(check_nonmem_finished,list)
S3method(check_output_dir,bbi_model)
S3method(check_output_dir,character)
S3method(check_up_to_date,bbi_base_model)
S3method(check_up_to_date,bbi_log_df)
S3method(check_up_to_date,bbi_nonmem_model)
S3method(check_up_to_date,bbi_nmboot_model)
S3method(check_up_to_date,bbi_nonmem_summary)
S3method(config_log_make_entry,bbi_nonmem_model)
S3method(config_log_make_entry,default)
S3method(copy_model_from,bbi_nonmem_model)
S3method(cov_cor,bbi_nonmem_model)
S3method(cov_cor,bbi_nonmem_summary)
S3method(create_model_hook,bbi_nmboot_model)
S3method(create_model_hook,bbi_nmsim_model)
S3method(create_model_hook,bbi_nonmem_model)
S3method(create_model_hook,default)
S3method(get_based_on,bbi_run_log_df)
S3method(get_based_on,character)
S3method(get_based_on,default)
S3method(get_config_path,bbi_log_df)
S3method(get_config_path,bbi_model)
S3method(get_config_path,bbi_nonmem_summary)
S3method(get_data_path,bbi_log_df)
S3method(get_data_path,bbi_model)
S3method(get_data_path,bbi_nmboot_model)
S3method(get_data_path,bbi_nmsim_model)
S3method(get_data_path,bbi_nonmem_model)
S3method(get_data_path,bbi_nonmem_summary)
S3method(get_model_ancestry,bbi_run_log_df)
Expand All @@ -39,19 +43,22 @@ S3method(get_model_ancestry,default)
S3method(get_model_id,bbi_model)
S3method(get_model_id,character)
S3method(get_model_path,bbi_log_df)
S3method(get_model_path,bbi_nmboot_model)
S3method(get_model_path,bbi_nonmem_model)
S3method(get_model_path,bbi_model)
S3method(get_model_path,bbi_nonmem_summary)
S3method(get_model_status,default)
S3method(get_output_dir,bbi_log_df)
S3method(get_output_dir,bbi_nmboot_model)
S3method(get_output_dir,bbi_nonmem_model)
S3method(get_output_dir,bbi_model)
S3method(get_output_dir,bbi_nonmem_summary)
S3method(get_yaml_path,bbi_log_df)
S3method(get_yaml_path,bbi_model)
S3method(model_diff,default)
S3method(get_yaml_path,bbi_nonmem_summary)
S3method(model_diff,bbi_nmboot_model)
S3method(model_diff,bbi_nmsim_model)
S3method(model_diff,bbi_nonmem_model)
S3method(model_summaries,bbi_run_log_df)
S3method(model_summaries,list)
S3method(model_summary,bbi_nmboot_model)
S3method(model_summary,bbi_nmsim_model)
S3method(model_summary,bbi_nonmem_model)
S3method(nm_file,bbi_model)
S3method(nm_file,character)
Expand All @@ -65,16 +72,17 @@ S3method(print,bbi_nmboot_summary)
S3method(print,bbi_nonmem_summary)
S3method(print,bbi_process)
S3method(print_model_files,default)
S3method(submit_model,bbi_base_model)
S3method(submit_model,bbi_nmboot_model)
S3method(submit_model,bbi_nonmem_model)
S3method(submit_model,bbi_nmsim_model)
S3method(submit_models,bbi_nonmem_models)
S3method(submit_models,default)
S3method(submit_models,list)
S3method(tags_diff,bbi_model)
S3method(tags_diff,bbi_run_log_df)
S3method(tail_lst,bbi_nonmem_model)
S3method(tail_lst,bbi_base_model)
S3method(tail_lst,character)
S3method(tail_output,bbi_nonmem_model)
S3method(tail_output,bbi_base_model)
S3method(tail_output,character)
S3method(wait_for_nonmem,default)
export("%>%")
Expand All @@ -84,6 +92,7 @@ export(add_bbi_args)
export(add_config)
export(add_description)
export(add_notes)
export(add_simulation)
export(add_star)
export(add_summary)
export(add_tags)
Expand Down Expand Up @@ -129,8 +138,10 @@ export(get_model_status)
export(get_omega)
export(get_output_dir)
export(get_sigma)
export(get_simulation)
export(get_theta)
export(get_yaml_path)
export(has_simulation)
export(inherit_param_estimates)
export(initial_estimates)
export(mod_ext)
Expand All @@ -143,8 +154,10 @@ export(new_ext)
export(new_model)
export(nm_data)
export(nm_file)
export(nm_file_multi_tab)
export(nm_grd)
export(nm_join)
export(nm_join_sim)
export(nm_par_tab)
export(nm_tab)
export(nm_table_files)
Expand Down
27 changes: 25 additions & 2 deletions R/aaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,12 @@ BBI_ARGS = list(
# models _and_ summary objects. BBI_BASE_MODEL_CLASS ("bbi_base_model") came
# later and applies only to regular models, not summary objects.
BBI_PARENT_CLASS <- "bbi_model" # SHARED with bbr.bayes
BBI_BASE_MODEL_CLASS <- "bbi_base_model"
BBI_BASE_MODEL_CLASS <- "bbi_base_model" # SHARED with bbr.bayes
NM_MOD_CLASS <- "bbi_nonmem_model" # SHARED with bbr.bayes
NM_SUM_CLASS <- "bbi_nonmem_summary" # SHARED with bbr.bayes
NMBOOT_MOD_CLASS <- "bbi_nmboot_model"
NMBOOT_SUM_CLASS <- "bbi_nmboot_summary"
NMSIM_MOD_CLASS <- "bbi_nmsim_model"
SL_CLASS <- "bbi_summary_list"
PROC_CLASS <- "bbi_process"
RUN_LOG_CLASS <- "bbi_run_log_df"
Expand All @@ -217,7 +218,7 @@ YAML_TAGS <- "tags" # SHARED with bbr.bayes
YAML_NOTES <- "notes"
YAML_BBI_ARGS <- "bbi_args"
YAML_MOD_TYPE <- "model_type" # SHARED with bbr.bayes
YAML_STAR <- 'star'
YAML_STAR <- "star"

YAML_REQ_INPUT_KEYS <- c(
YAML_MOD_TYPE
Expand All @@ -226,6 +227,24 @@ YAML_REQ_INPUT_KEYS <- c(
ABS_MOD_PATH <- "absolute_model_path" # SHARED with bbr.bayes
RUN_ID_COL <- "run" # SHARED with bbr.bayes

# SPEC keys that are hard-coded
SPEC_NMSIM_NSIM <- "n_sim"

SPEC_NMBOOT_NSAMPLE <- "n_samples"
SPEC_NMBOOT_STRAT <- "strat_cols"

# List of keys required to print a bbi_nmsim_model object
SPEC_NMSIM_KEYS <- c(
SPEC_NMSIM_NSIM
)

# List of keys required to print a bbi_nmboot_model object
SPEC_NMBOOT_KEYS <- c(
SPEC_NMBOOT_NSAMPLE,
SPEC_NMBOOT_STRAT
)


# keys required to create a model object
MODEL_REQ_INPUT_KEYS <- c(
ABS_MOD_PATH,
Expand Down Expand Up @@ -268,6 +287,7 @@ YAML_SCALAR_TO_LIST_KEYS <- c(
YAML_STAR
)


SUMMARY_DETAILS <- "run_details"
SUMMARY_HEURISTICS <- "run_heuristics"
SUMMARY_COND_NUM <- "condition_number"
Expand All @@ -288,6 +308,9 @@ CONFIG_MODEL_MD5 <- "model_md5" # SHARED with bbr.bayes
CONFIG_DATA_PATH <- "data_path" # SHARED with bbr.bayes
CONFIG_DATA_MD5 <- "data_md5" # SHARED with bbr.bayes

# keys for bootstrap runs
CONFIG_DATA_BASED_ON_MD5 <- "based_on_data_md5"

# keys required for a summary object to have
SUMMARY_REQ_KEYS <- c(
ABS_MOD_PATH,
Expand Down
66 changes: 46 additions & 20 deletions R/bootstrap-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
#' Both default to `TRUE`.
#'
#' @seealso setup_bootstrap_run summarize_bootstrap_run
#' @examples
#' \dontrun{
#'
#' # Create new bootstrap object
#' .boot_run <- new_bootstrap_run(.mod)
#'
#' # Optionally inherit final parameter estimates
#' .boot_run <- inherit_param_estimates(.boot_run)
#'
#' # Set up the run
#' setup_bootstrap_run(.boot_run)
#' }
#' @return S3 object of class `bbi_nmboot_model`.
#' @export
new_bootstrap_run <- function(
Expand Down Expand Up @@ -44,12 +56,10 @@ new_bootstrap_run <- function(

# Optionally remove $TABLE and $COV statements here
if(isTRUE(remove_cov)) remove_records(boot_run, type = "covariance")
if(isTRUE(remove_tables)){
remove_records(boot_run, type = "table")
}else{
# Update table names if present (not used, but for consistency)
boot_run <- update_model_id(boot_run) %>% suppressMessages()
}
if(isTRUE(remove_tables)) remove_records(boot_run, type = "table")

# Update table and estimation record file paths
boot_run <- update_model_id(boot_run) %>% suppressMessages()

# Return read-in model to get the updated class as part of the model object
return(read_model(file.path(model_dir, boot_dir)))
Expand All @@ -74,7 +84,12 @@ new_bootstrap_run <- function(
#'
#' # Setup
#' .boot_run <- new_bootstrap_run(.mod)
#' .boot_run <- setup_bootstrap_run(.boot_run, n = 1000, seed = 1234)
#' .boot_run <- setup_bootstrap_run(
#' .boot_run,
#' n = 1000,
#' seed = 1234,
#' strat_cols = c("STUDY", "ETN")
#' )
#'
#' # Submit
#' submit_model(.boot_run)
Expand Down Expand Up @@ -574,6 +589,7 @@ get_boot_models <- function(.boot_run){
.boot_run <- read_model(.boot_run[[ABS_MOD_PATH]])
}

boot_dir <- .boot_run[[ABS_MOD_PATH]]
output_dir <- get_output_dir(.boot_run, .check_exists = FALSE)
if(!fs::file_exists(output_dir)){
verbose_msg(
Expand All @@ -590,29 +606,39 @@ get_boot_models <- function(.boot_run){
}

boot_spec <- get_boot_spec(.boot_run)
boot_models <- tryCatch(
purrr::map(boot_spec$bootstrap_runs$mod_path_abs, read_model),
error = function(cond){
# Suppress 'does not exist' message - handle separately
if(!stringr::str_detect(cond$parent$message, "does not exist")){
# Likely would only happen if there was a bbi/submission issue
message(cond$parent$message)
}
return(NULL)
boot_model_ids <- fs::path_ext_remove(basename(boot_spec$bootstrap_runs$mod_path_abs))

boot_models <- tryCatch({
find_models(.boot_run[[ABS_MOD_PATH]], .recurse = FALSE, .include = boot_model_ids)
}, warning = function(cond){
if(!stringr::str_detect(cond$message, "All models excluded|Found no valid model")){
warning(cond)
}
)
return(NULL)
})


# This shouldnt happen, but could if the directory existed and models
# referenced in the spec file aren't found for any reason _other than_
# cleaning up the run
if(is.null(boot_models) || rlang::is_empty(boot_models)){
boot_dir <- .boot_run[[ABS_MOD_PATH]]
rlang::warn(
rlang::abort(
c(
glue("At least one bootstrap run model does not exist in `{boot_dir}`")
)
)
}else{
if(length(boot_model_ids) != length(boot_models)){
rlang::warn(
c(
glue("Found an unexpected number of models in {boot_dir}"),
glue("Expected number of models: {length(boot_model_ids)}"),
glue("Discovered number of models: {length(boot_models)}")
)
)
}
}

return(boot_models)
}

Expand Down Expand Up @@ -672,7 +698,7 @@ cleanup_bootstrap_run <- function(.boot_run, .force = FALSE){
}

# Overwrite spec file
spec_path <- get_boot_spec_path(.boot_run)
spec_path <- get_spec_path(.boot_run)
boot_spec <- jsonlite::read_json(spec_path, simplifyVector = TRUE)
# Set cleaned up - impacts status checking
boot_spec$bootstrap_spec$cleaned_up <- TRUE
Expand Down
Loading

0 comments on commit 77cbc1f

Please sign in to comment.