Skip to content

Commit

Permalink
no longer canonicalize the Stan code for now
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-buerkner committed Sep 26, 2023
1 parent 158bfa3 commit 177e02f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: brms
Encoding: UTF-8
Type: Package
Title: Bayesian Regression Models using 'Stan'
Version: 2.20.4
Version: 2.20.5
Date: 2023-09-25
Authors@R:
c(person("Paul-Christian", "Bürkner", email = "paul.buerkner@gmail.com",
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# brms 2.20.3+

### Other Changes

* No longer automatically canonicalize the Stan code if cmdstanr is used
as backend. (#1544)


# brms 2.20.3

### Other Changes
Expand Down
34 changes: 18 additions & 16 deletions R/backends.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ parse_model <- function(model, backend, ...) {
.parse_model_cmdstanr <- function(model, silent = 1, ...) {
require_package("cmdstanr")
temp_file <- cmdstanr::write_stan_file(model)
if (cmdstanr::cmdstan_version() >= "2.29.0") {
.canonicalize_stan_model(temp_file, overwrite_file = TRUE)
}
# if (cmdstanr::cmdstan_version() >= "2.29.0") {
# .canonicalize_stan_model(temp_file, overwrite_file = TRUE)
# }
out <- eval_silent(
cmdstanr::cmdstan_model(temp_file, compile = FALSE, ...),
type = "message", try = TRUE, silent = silent
Expand Down Expand Up @@ -97,9 +97,9 @@ compile_model <- function(model, backend, ...) {
require_package("cmdstanr")
args <- list(...)
args$stan_file <- cmdstanr::write_stan_file(model)
if (cmdstanr::cmdstan_version() >= "2.29.0") {
.canonicalize_stan_model(args$stan_file, overwrite_file = TRUE)
}
# if (cmdstanr::cmdstan_version() >= "2.29.0") {
# .canonicalize_stan_model(args$stan_file, overwrite_file = TRUE)
# }
if (use_threading(threads)) {
args$cpp_options$stan_threads <- TRUE
}
Expand Down Expand Up @@ -632,16 +632,18 @@ file_refit_options <- function() {
}

# canonicalize Stan model file in accordance with the current Stan version
.canonicalize_stan_model <- function(stan_file, overwrite_file = TRUE) {
cmdstan_mod <- cmdstanr::cmdstan_model(stan_file, compile = FALSE)
out <- utils::capture.output(
cmdstan_mod$format(
canonicalize = list("deprecations", "braces", "parentheses"),
overwrite_file = overwrite_file, backup = FALSE
)
)
paste0(out, collapse = "\n")
}
# this function may no longer be needed due to rstan 2.26+ now being on CRAN
# for more details see https://github.com/paul-buerkner/brms/issues/1544
# .canonicalize_stan_model <- function(stan_file, overwrite_file = TRUE) {
# cmdstan_mod <- cmdstanr::cmdstan_model(stan_file, compile = FALSE)
# out <- utils::capture.output(
# cmdstan_mod$format(
# canonicalize = list("deprecations", "braces", "parentheses"),
# overwrite_file = overwrite_file, backup = FALSE
# )
# )
# paste0(out, collapse = "\n")
# }

# read in stan CSVs via cmdstanr and repackage into a stanfit object
# efficient replacement of rstan::read_stan_csv
Expand Down
14 changes: 7 additions & 7 deletions R/make_stancode.R
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,13 @@ make_stancode <- function(formula, data, family = gaussian(),
if (parse) {
scode <- parse_model(scode, backend, silent = silent)
}
if (backend == "cmdstanr") {
if (requireNamespace("cmdstanr", quietly = TRUE) &&
cmdstanr::cmdstan_version() >= "2.29.0") {
tmp_file <- cmdstanr::write_stan_file(scode)
scode <- .canonicalize_stan_model(tmp_file, overwrite_file = FALSE)
}
}
# if (backend == "cmdstanr") {
# if (requireNamespace("cmdstanr", quietly = TRUE) &&
# cmdstanr::cmdstan_version() >= "2.29.0") {
# tmp_file <- cmdstanr::write_stan_file(scode)
# scode <- .canonicalize_stan_model(tmp_file, overwrite_file = FALSE)
# }
# }
if (is.character(save_model)) {
cat(scode, file = save_model)
}
Expand Down

0 comments on commit 177e02f

Please sign in to comment.