Skip to content

Commit

Permalink
Continue cleaning up deprecations and backwards compat
Browse files Browse the repository at this point in the history
  • Loading branch information
andrjohns committed May 18, 2024
1 parent 1e886a0 commit 16724a3
Show file tree
Hide file tree
Showing 23 changed files with 79 additions and 262 deletions.
3 changes: 0 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,14 @@ export(cmdstanr_example)
export(draws_to_csv)
export(eng_cmdstan)
export(install_cmdstan)
export(num_threads)
export(print_example_program)
export(read_cmdstan_csv)
export(read_sample_csv)
export(rebuild_cmdstan)
export(register_knitr_engine)
export(set_cmdstan_path)
export(set_num_threads)
export(write_stan_file)
export(write_stan_json)
export(write_stan_tempfile)
import(R6)
importFrom(posterior,as_draws)
importFrom(stats,aggregate)
23 changes: 21 additions & 2 deletions R/args.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ CmdStanArgs <- R6::R6Class(
self$output_dir <- ifelse(is.null(output_dir),
file.path(wsl_dir_prefix(), wsl_tempdir()),
wsl_safe_path(output_dir))
} else if (getRversion() < "3.5.0") {
self$output_dir <- output_dir %||% tempdir()
} else {
self$output_dir <- output_dir %||% tempdir(check = TRUE)
if (getRversion() < "3.5.0") {
self$output_dir <- output_dir %||% tempdir()
} else {
self$output_dir <- output_dir %||% tempdir(check = TRUE)
}
}
self$output_dir <- repair_path(self$output_dir)
self$output_basename <- output_basename
Expand Down Expand Up @@ -692,6 +698,9 @@ validate_cmdstan_args <- function(self) {
checkmate::assert_integerish(self$refresh, lower = 0, null.ok = TRUE)
checkmate::assert_integerish(self$sig_figs, lower = 1, upper = 18, null.ok = TRUE)
checkmate::assert_integerish(self$save_cmdstan_config, lower = 0, upper = 1, len = 1, null.ok = TRUE)
if (!is.null(self$sig_figs) && cmdstan_version() < "2.25") {
warning("The 'sig_figs' argument is only supported with cmdstan 2.25+ and will be ignored!", call. = FALSE)
}
if (!is.null(self$refresh)) {
self$refresh <- as.integer(self$refresh)
}
Expand All @@ -707,6 +716,9 @@ validate_cmdstan_args <- function(self) {
validate_init(self$init, num_inits)
validate_seed(self$seed, num_procs)
if (!is.null(self$opencl_ids)) {
if (cmdstan_version() < "2.26") {
stop("Runtime selection of OpenCL devices is only supported with CmdStan version 2.26 or newer.", call. = FALSE)
}
checkmate::assert_vector(self$opencl_ids, len = 2)
}
invisible(TRUE)
Expand Down Expand Up @@ -824,6 +836,9 @@ validate_optimize_args <- function(self) {
choices = c("bfgs", "lbfgs", "newton"))
checkmate::assert_flag(self$jacobian, null.ok = TRUE)
if (!is.null(self$jacobian)) {
if (cmdstan_version() < "2.32") {
warning("The 'jacobian' argument is only supported with cmdstan 2.32+ and will be ignored!", call. = FALSE)
}
self$jacobian <- as.integer(self$jacobian)
}

Expand Down Expand Up @@ -1461,7 +1476,11 @@ validate_seed <- function(seed, num_procs) {
if (is.null(seed)) {
return(invisible(TRUE))
}
lower_seed <- 1
if (cmdstan_version() < "2.26") {
lower_seed <- 1
} else {
lower_seed <- 0
}
checkmate::assert_integerish(seed, lower = lower_seed)
if (length(seed) > 1 && length(seed) != num_procs) {
stop("If 'seed' is specified it must be a single integer or one per chain.",
Expand Down
9 changes: 6 additions & 3 deletions R/install.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ install_cmdstan <- function(dir = NULL,
.cmdstanr$WSL <- FALSE
}
if (check_toolchain) {
check_cmdstan_toolchain(fix = FALSE, quiet = quiet)
check_cmdstan_toolchain(quiet = quiet)
}
make_local_msg <- NULL
if (!is.null(cmdstan_version(error_on_NA = FALSE))) {
Expand Down Expand Up @@ -321,7 +321,7 @@ check_cmdstan_toolchain <- function(quiet = FALSE) {
if (os_is_wsl()) {
check_wsl_toolchain()
} else if (R.version$major >= "4") {
check_rtools4x_windows_toolchain(fix = fix, quiet = quiet)
check_rtools4x_windows_toolchain(quiet = quiet)
} else {
stop("CmdStanR requires R version 4.0.0 or later on Windows.",
"Please update R and try again.", call. = FALSE)
Expand Down Expand Up @@ -562,7 +562,7 @@ check_wsl_toolchain <- function() {
}
}

check_rtools4x_windows_toolchain <- function(fix = FALSE, quiet = FALSE) {
check_rtools4x_windows_toolchain <- function(quiet = FALSE) {
rtools_path <- rtools_home_path()
rtools_version <- paste0("Rtools", rtools4x_version())
# If RTOOLS4X_HOME is not set (the env. variable gets set on install)
Expand Down Expand Up @@ -636,6 +636,9 @@ check_unix_cpp_compiler <- function() {
cmdstan_arch_suffix <- function(version = NULL) {
os_needs_arch <- os_is_linux() || os_is_wsl()
arch <- NULL
if (!os_needs_arch) {
return(arch)
}
if (os_is_wsl()) {
arch <- wsl_compatible_run(command = "uname", args = "-m")$stdout
arch <- gsub("\n", "", arch, fixed = TRUE)
Expand Down
4 changes: 2 additions & 2 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -1128,8 +1128,8 @@ sample <- function(data = NULL,
show_messages = TRUE,
show_exceptions = TRUE,
diagnostics = c("divergences", "treedepth", "ebfmi"),
save_metric = TRUE,
save_cmdstan_config = TRUE) {
save_metric = NULL,
save_cmdstan_config = NULL) {
if (!fixed_param) {
if (self$has_stan_file() && file.exists(self$stan_file())) {
if (!is.null(self$variables()) && length(self$variables()$parameters) == 0) {
Expand Down
16 changes: 13 additions & 3 deletions R/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ CmdStanRun <- R6::R6Class(
self$procs <- procs
private$output_files_ <- self$new_output_files()
private$profile_files_ <- self$new_profile_files()
if (isTRUE(self$args$save_cmdstan_config)) {
if (!is.null(self$args$save_cmdstan_config) && as.logical(self$args$save_cmdstan_config)) {
private$config_files_ <- self$new_config_files()
}
if (isTRUE(self$args$method_args$save_metric)) {
if (!is.null(self$args$method_args$save_metric) && as.logical(self$args$method_args$save_metric)) {
private$metric_files_ <- self$new_metric_files()
}
if (self$args$save_latent_dynamics) {
Expand Down Expand Up @@ -73,6 +73,9 @@ CmdStanRun <- R6::R6Class(
paste0(tools::file_path_sans_ext(private$output_files_), "_metric.json")
},
config_files = function(include_failed = FALSE) {
if (!isTRUE(self$args$save_cmdstan_config)) {
return(invisible(NULL))
}
files <- private$config_files_
files_win_path <- sapply(private$config_files_, wsl_safe_path, revert = TRUE)
if (!length(files) || !any(file.exists(files_win_path))) {
Expand All @@ -90,6 +93,13 @@ CmdStanRun <- R6::R6Class(
}
},
metric_files = function(include_failed = FALSE) {
if (!isTRUE(self$args$method_args$save_metric)) {
stop(
"No metric files found. ",
"Set 'save_metric=TRUE' when fitting the model.",
call. = FALSE
)
}
files <- private$metric_files_
files_win_path <- sapply(private$metric_files_, wsl_safe_path, revert = TRUE)
if (!length(files) || !any(file.exists(files_win_path))) {
Expand Down Expand Up @@ -400,7 +410,7 @@ CmdStanRun <- R6::R6Class(
self$latent_dynamics_files(include_failed = TRUE),
if (!private$profile_files_saved_)
private$profile_files_,
if (isTRUE(self$args$save_cmdstan_config)
if (isTRUE(self$args$save_cmdstan_config) &&
!private$config_files_saved_)
self$config_files(include_failed = TRUE),
if (isTRUE(self$args$method_args$save_metric) &&
Expand Down
8 changes: 2 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -809,12 +809,8 @@ prep_fun_cpp <- function(fun_start, fun_end, model_lines) {
fun_body <- paste(model_lines[fun_start:fun_end], collapse = " ")
fun_body <- gsub("// [[stan::function]]", "// [[Rcpp::export]]\n", fun_body, fixed = TRUE)
fun_body <- gsub("std::ostream\\*\\s*pstream__\\s*=\\s*nullptr", "", fun_body)
if (grepl("(stan::rng_t|boost::ecuyer1988)", fun_body)) {
if (cmdstan_version() < "2.35.0") {
fun_body <- gsub("boost::ecuyer1988&\\s*base_rng__", "SEXP base_rng_ptr, SEXP seed", fun_body)
} else {
fun_body <- gsub("stan::rng_t&\\s*base_rng__", "SEXP base_rng_ptr, SEXP seed", fun_body)
}
if (grepl("stan::rng_t", fun_body)) {
fun_body <- gsub("stan::rng_t&\\s*base_rng__", "SEXP base_rng_ptr, SEXP seed", fun_body)
rng_seed <- "Rcpp::XPtr<stan::rng_t> base_rng(base_rng_ptr);base_rng->seed(Rcpp::as<int>(seed));"
fun_body <- gsub("return", paste(rng_seed, "return"), fun_body)
fun_body <- gsub("base_rng__,", "*(base_rng.get()),", fun_body, fixed = TRUE)
Expand Down
7 changes: 1 addition & 6 deletions man/install_cmdstan.Rd

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

10 changes: 1 addition & 9 deletions man/model-method-compile.Rd

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

7 changes: 1 addition & 6 deletions man/model-method-laplace.Rd

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

7 changes: 1 addition & 6 deletions man/model-method-optimize.Rd

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

7 changes: 1 addition & 6 deletions man/model-method-pathfinder.Rd

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

22 changes: 2 additions & 20 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: 1 addition & 7 deletions man/model-method-sample_mpi.Rd

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

7 changes: 1 addition & 6 deletions man/model-method-variational.Rd

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

22 changes: 0 additions & 22 deletions man/stan_threads.Rd

This file was deleted.

23 changes: 0 additions & 23 deletions man/write_stan_tempfile.Rd

This file was deleted.

4 changes: 0 additions & 4 deletions tests/testthat/test-example.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ test_that("write_stan_file by default creates the same file for the same Stan mo
try(file.remove(f1, f2, f4), silent = TRUE)
})

test_that("write_stan_tempfile is deprecated", {
expect_warning(write_stan_tempfile(stan_program), "deprecated")
})

test_that("cmdstanr_write_stan_file_dir option works", {
base_dir <- tempdir()
test_dir <- file.path(base_dir, "option_test")
Expand Down
Loading

0 comments on commit 16724a3

Please sign in to comment.