Skip to content

Add output_basename #471

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

Merged
merged 6 commits into from
Mar 17, 2021
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 @@ -30,6 +30,7 @@ CmdStanArgs <- R6::R6Class(
init = NULL,
refresh = NULL,
output_dir = NULL,
output_basename = NULL,
validate_csv = TRUE,
sig_figs = NULL) {

Expand All @@ -51,6 +52,7 @@ CmdStanArgs <- R6::R6Class(
self$output_dir <- output_dir %||% tempdir(check = TRUE)
}
self$output_dir <- repair_path(self$output_dir)
self$output_basename <- output_basename
if (is.function(init)) {
init <- process_init_function(init, length(self$proc_ids))
} else if (is.list(init) && !is.data.frame(init)) {
Expand Down Expand Up @@ -83,12 +85,15 @@ CmdStanArgs <- R6::R6Class(
} else if (type == "profile") {
basename <- paste0(basename, "-profile")
}
if (type == "output" && !is.null(self$output_basename)) {
basename <- self$output_basename
}
generate_file_names( # defined in utils.R
basename = basename,
ext = ".csv",
ids = self$proc_ids,
timestamp = TRUE,
random = TRUE
timestamp = is.null(self$output_basename),
random = is.null(self$output_basename)
)
},
new_files = function(type = c("output", "diagnostic", "profile")) {
Expand Down
10 changes: 10 additions & 0 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ sample <- function(data = NULL,
init = NULL,
save_latent_dynamics = FALSE,
output_dir = NULL,
output_basename = NULL,
sig_figs = NULL,
chains = 4,
parallel_chains = getOption("mc.cores", 1),
Expand Down Expand Up @@ -808,6 +809,7 @@ sample <- function(data = NULL,
init = init,
refresh = refresh,
output_dir = output_dir,
output_basename = output_basename,
sig_figs = sig_figs,
validate_csv = validate_csv
)
Expand Down Expand Up @@ -889,6 +891,7 @@ sample_mpi <- function(data = NULL,
init = NULL,
save_latent_dynamics = FALSE,
output_dir = NULL,
output_basename = NULL,
chains = 1,
chain_ids = seq_len(chains),
iter_warmup = NULL,
Expand Down Expand Up @@ -944,6 +947,7 @@ sample_mpi <- function(data = NULL,
init = init,
refresh = refresh,
output_dir = output_dir,
output_basename = output_basename,
validate_csv = validate_csv,
sig_figs = sig_figs
)
Expand Down Expand Up @@ -1013,6 +1017,7 @@ optimize <- function(data = NULL,
init = NULL,
save_latent_dynamics = FALSE,
output_dir = NULL,
output_basename = NULL,
sig_figs = NULL,
threads = NULL,
algorithm = NULL,
Expand Down Expand Up @@ -1061,6 +1066,7 @@ optimize <- function(data = NULL,
init = init,
refresh = refresh,
output_dir = output_dir,
output_basename = output_basename,
sig_figs = sig_figs
)

Expand Down Expand Up @@ -1133,6 +1139,7 @@ variational <- function(data = NULL,
init = NULL,
save_latent_dynamics = FALSE,
output_dir = NULL,
output_basename = NULL,
sig_figs = NULL,
threads = NULL,
algorithm = NULL,
Expand Down Expand Up @@ -1183,6 +1190,7 @@ variational <- function(data = NULL,
init = init,
refresh = refresh,
output_dir = output_dir,
output_basename = output_basename,
sig_figs = sig_figs
)

Expand Down Expand Up @@ -1266,6 +1274,7 @@ generate_quantities <- function(fitted_params,
data = NULL,
seed = NULL,
output_dir = NULL,
output_basename = NULL,
sig_figs = NULL,
parallel_chains = getOption("mc.cores", 1),
threads_per_chain = NULL) {
Expand Down Expand Up @@ -1299,6 +1308,7 @@ generate_quantities <- function(fitted_params,
data_file = process_data(data),
seed = seed,
output_dir = output_dir,
output_basename = output_basename,
sig_figs = sig_figs
)
cmdstan_procs <- CmdStanGQProcs$new(
Expand Down
21 changes: 13 additions & 8 deletions man-roxygen/model-common-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,20 @@
#' `NULL` (temporary directory) since CmdStanR makes the CmdStan output
#' (posterior draws and diagnostics) available in \R via methods of the fitted
#' model objects. The behavior of `output_dir` is as follows:
#' * If `NULL` (the default), then the CSV files are written to a temporary
#' directory and only saved permanently if the user calls one of the `$save_*`
#' methods of the fitted model object (e.g.,
#' [`$save_output_files()`][fit-method-save_output_files]). These temporary
#' files are removed when the fitted model object is
#' [garbage collected][base::gc] (manually or automatically).
#' * If a path, then the files are created in `output_dir` with names
#' corresponding to the defaults used by `$save_output_files()`.
#' * If `NULL` (the default), then the CSV files are written to a temporary
#' directory and only saved permanently if the user calls one of the `$save_*`
#' methods of the fitted model object (e.g.,
#' [`$save_output_files()`][fit-method-save_output_files]). These temporary
#' files are removed when the fitted model object is
#' [garbage collected][base::gc] (manually or automatically).
#' * If a path, then the files are created in `output_dir` with names
#' corresponding to the defaults used by `$save_output_files()`.
#'
#' @param output_basename (string) A string to use as a prefix for the
#' names of the output CSV files of CmdStan.
#' * If `NULL` (the default), the basename of the output CSV files
#' will be comprised from the model name, timestamp and 5 random characters.
#'
#' @param sig_figs (positive integer) The number of significant figures used
#' when storing the output values. By default, CmdStan represent the output
#' values with 6 significant figures. The upper limit for `sig_figs` is 18.
Expand Down
8 changes: 8 additions & 0 deletions man/model-method-generate-quantities.Rd

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

8 changes: 8 additions & 0 deletions man/model-method-optimize.Rd

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

8 changes: 8 additions & 0 deletions man/model-method-sample.Rd

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

8 changes: 8 additions & 0 deletions man/model-method-sample_mpi.Rd

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

8 changes: 8 additions & 0 deletions man/model-method-variational.Rd

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

10 changes: 9 additions & 1 deletion tests/testthat/test-model-output_dir.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("model-output_dir")
context("model-output_dir-output-basename")

if (not_on_cran()) {
set_cmdstan_path()
Expand Down Expand Up @@ -29,6 +29,14 @@ test_that("all fitting methods work with output_dir", {
output_dir = method_dir)
expect_equal(fit$runset$args$output_dir, absolute_path(method_dir))
expect_equal(length(list.files(method_dir)), fit$num_procs())


# specifying output_dir
fit <- testing_fit("bernoulli", method = method, seed = 123,
output_basename = "custom")
n_files <- length(fit$output_files())
files <- paste0("custom-", 1:n_files, ".csv")
expect_equal(basename(fit$output_files()), files)
}

# specifying output_dir and save_latent_dynamics
Expand Down