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

Global option for turning off warning for partial initial values #913

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions R/args.R
Original file line number Diff line number Diff line change
Expand Up @@ -1021,8 +1021,12 @@ validate_exe_file <- function(exe_file) {
#' @param num_procs Number of CmdStan processes.
#' @param model_variables A list of all parameters with their types and
#' number of dimensions. Typically the output of model$variables().
#' @param warn_partial Should a warning be thrown if inits are only specified
#' for a subset of parameters? Can be controlled by global option
#' `cmdstanr_warn_inits`.
#' @return A character vector of file paths.
process_init_list <- function(init, num_procs, model_variables = NULL) {
process_init_list <- function(init, num_procs, model_variables = NULL,
warn_partial = getOption("cmdstanr_warn_inits", TRUE)) {
if (!all(sapply(init, function(x) is.list(x) && !is.data.frame(x)))) {
stop("If 'init' is a list it must be a list of lists.", call. = FALSE)
}
Expand All @@ -1048,7 +1052,7 @@ process_init_list <- function(init, num_procs, model_variables = NULL) {
}
}
}
if (length(missing_parameter_values) > 0) {
if (length(missing_parameter_values) > 0 && isTRUE(warn_partial)) {
warning_message <- c(
"Init values were only set for a subset of parameters. \nMissing init values for the following parameters:\n"
)
Expand All @@ -1062,6 +1066,7 @@ process_init_list <- function(init, num_procs, model_variables = NULL) {
warning_message <- c(warning_message, paste0(line_text, paste0(missing_parameter_values[[i]], collapse = ", "), "\n"))
}
}
warning_message <- c(warning_message, "\nTo disable this message use options(cmdstanr_warn_inits = FALSE).\n")
message(warning_message)
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-model-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ test_that("error if init function specified incorrectly", {
})

test_that("print message if not all parameters are initialized", {
options(cmdstanr_warn_inits = NULL) # should default to TRUE
init_list <- list(
list(
alpha = 1
Expand Down Expand Up @@ -271,6 +272,23 @@ test_that("print message if not all parameters are initialized", {
)
})

test_that("No message printed if options(cmdstanr_warn_inits=FALSE)", {
options(cmdstanr_warn_inits = FALSE)
expect_message(
utils::capture.output(mod_logistic$optimize(data = data_list_logistic, init = list(list(a = 0)), seed = 123)),
regexp = NA
)
expect_message(
utils::capture.output(mod_logistic$optimize(data = data_list_logistic, init = list(list(alpha = 1)), seed = 123)),
regexp = NA
)
expect_message(
utils::capture.output(mod_logistic$sample(data = data_list_logistic, init = list(list(alpha = 1),list(alpha = 1)), chains = 2, seed = 123)),
regexp = NA
)
options(cmdstanr_warn_inits = TRUE)
})

test_that("Initial values for single-element containers treated correctly", {
modcode <- "
data {
Expand Down
Loading