Skip to content

Commit

Permalink
Merge pull request #897 from stan-dev/show_messages-methods
Browse files Browse the repository at this point in the history
Add show_messages and show_exceptions arguments to all methods
  • Loading branch information
andrjohns authored Jan 12, 2024
2 parents 5ae46d3 + 00ca320 commit d444e7f
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 33 deletions.
28 changes: 20 additions & 8 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -1495,10 +1495,13 @@ optimize <- function(data = NULL,
tol_grad = NULL,
tol_rel_grad = NULL,
tol_param = NULL,
history_size = NULL) {
history_size = NULL,
show_messages = TRUE,
show_exceptions = TRUE) {
procs <- CmdStanProcs$new(
num_procs = 1,
show_stdout_messages = (is.null(refresh) || refresh != 0),
show_stderr_messages = show_exceptions,
show_stdout_messages = show_messages,
threads_per_proc = assert_valid_threads(threads, self$cpp_options())
)
model_variables <- NULL
Expand Down Expand Up @@ -1624,7 +1627,9 @@ laplace <- function(data = NULL,
mode = NULL,
opt_args = NULL,
jacobian = TRUE, # different default than for optimize!
draws = NULL) {
draws = NULL,
show_messages = TRUE,
show_exceptions = TRUE) {
if (cmdstan_version() < "2.32") {
stop("This method is only available in cmdstan >= 2.32", call. = FALSE)
}
Expand All @@ -1633,7 +1638,8 @@ laplace <- function(data = NULL,
}
procs <- CmdStanProcs$new(
num_procs = 1,
show_stdout_messages = (is.null(refresh) || refresh != 0),
show_stderr_messages = show_exceptions,
show_stdout_messages = show_messages,
threads_per_proc = assert_valid_threads(threads, self$cpp_options())
)
model_variables <- NULL
Expand Down Expand Up @@ -1775,10 +1781,13 @@ variational <- function(data = NULL,
tol_rel_obj = NULL,
eval_elbo = NULL,
output_samples = NULL,
draws = NULL) {
draws = NULL,
show_messages = TRUE,
show_exceptions = TRUE) {
procs <- CmdStanProcs$new(
num_procs = 1,
show_stdout_messages = (is.null(refresh) || refresh != 0),
show_stderr_messages = show_exceptions,
show_stdout_messages = show_messages,
threads_per_proc = assert_valid_threads(threads, self$cpp_options())
)
model_variables <- NULL
Expand Down Expand Up @@ -1902,10 +1911,13 @@ pathfinder <- function(data = NULL,
num_paths = NULL,
max_lbfgs_iters = NULL,
num_elbo_draws = NULL,
save_single_paths = NULL) {
save_single_paths = NULL,
show_messages = TRUE,
show_exceptions = TRUE) {
procs <- CmdStanProcs$new(
num_procs = 1,
show_stdout_messages = (is.null(refresh) || refresh != 0),
show_stderr_messages = show_exceptions,
show_stdout_messages = show_messages,
threads_per_proc = assert_valid_threads(num_threads, self$cpp_options())
)
model_variables <- NULL
Expand Down
3 changes: 2 additions & 1 deletion R/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,8 @@ CmdStanProcs <- R6::R6Class(
report_time = function(id = NULL) {
if (self$proc_state(id) == 7) {
warning("Fitting finished unexpectedly! Use the $output() method for more information.\n", immediate. = TRUE, call. = FALSE)
} else {
}
if (private$show_stdout_messages_) {
cat("Finished in ",
base::format(round(self$total_time(), 1), nsmall = 1),
"seconds.\n")
Expand Down
13 changes: 13 additions & 0 deletions man-roxygen/model-common-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,16 @@
#' be compiled with `cpp_options = list(stan_opencl = TRUE)` for this
#' argument to have an effect.
#'
#' @param show_messages (logical) When `TRUE` (the default), prints all
#' output during the execution process, such as iteration numbers and elapsed times.
#' If the output is silenced then the [`$output()`][fit-method-output] method of
#' the resulting fit object can be used to display the silenced messages.
#'
#' @param show_exceptions (logical) When `TRUE` (the default), prints all
#' informational messages, for example rejection of the current proposal.
#' Disable if you wish to silence these messages, but this is not usually
#' recommended unless you are very confident that the model is correct up to
#' numerical error. If the messages are silenced then the
#' [`$output()`][fit-method-output] method of the resulting fit object can be
#' used to display the silenced messages.
#'
11 changes: 0 additions & 11 deletions man-roxygen/model-sample-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,6 @@
#' quantities block. If the parameters block is empty then using
#' `fixed_param=TRUE` is mandatory. When `fixed_param=TRUE` the `chains` and
#' `parallel_chains` arguments will be set to `1`.
#' @param show_messages (logical) When `TRUE` (the default), prints all
#' output during the sampling process, such as iteration numbers and elapsed times.
#' If the output is silenced then the [`$output()`][fit-method-output] method of
#' the resulting fit object can be used to display the silenced messages.
#' @param show_exceptions (logical) When `TRUE` (the default), prints all
#' informational messages, for example rejection of the current proposal.
#' Disable if you wish to silence these messages, but this is not usually
#' recommended unless you are very confident that the model is correct up to
#' numerical error. If the messages are silenced then the
#' [`$output()`][fit-method-output] method of the resulting fit object can be
#' used to display the silenced messages.
#' @param diagnostics (character vector) The diagnostics to automatically check
#' and warn about after sampling. Setting this to an empty string `""` or
#' `NULL` can be used to prevent CmdStanR from automatically reading in the
Expand Down
17 changes: 16 additions & 1 deletion man/model-method-laplace.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion man/model-method-optimize.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion man/model-method-pathfinder.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/model-method-sample.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/model-method-sample_mpi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion man/model-method-variational.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions tests/testthat/test-fit-shared.R
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ test_that("CmdStanArgs erorrs if idx is out of proc_ids range", {
)
})

test_that("no output with refresh = 0", {
test_that("no output with show_messages = FALSE", {
mod <- testing_model("logistic")
data_list <- testing_data("logistic")
output <- utils::capture.output(tmp <- mod$variational(data = data_list, seed = 123))
Expand All @@ -298,12 +298,12 @@ test_that("no output with refresh = 0", {
output <- utils::capture.output(tmp <- mod$sample(data = data_list, chains = 1, seed = 123))
expect_gt(length(output), 1)

output <- utils::capture.output(tmp <- mod$variational(data = data_list, refresh = 0, seed = 123))
expect_equal(length(output), 1)
output <- utils::capture.output(tmp <- mod$optimize(data = data_list, refresh = 0, seed = 123))
expect_equal(length(output), 1)
output <- utils::capture.output(tmp <- mod$sample(data = data_list, refresh = 0, chains = 1, seed = 123))
expect_equal(length(output), 3)
output <- utils::capture.output(tmp <- mod$variational(data = data_list, show_messages = FALSE, seed = 123))
expect_equal(length(output), 0)
output <- utils::capture.output(tmp <- mod$optimize(data = data_list, show_messages = FALSE, seed = 123))
expect_equal(length(output), 0)
output <- utils::capture.output(tmp <- mod$sample(data = data_list, show_messages = FALSE, chains = 1, seed = 123))
expect_equal(length(output), 0)
})

test_that("sig_figs works with all methods", {
Expand Down

0 comments on commit d444e7f

Please sign in to comment.