From 2709bb10fd23bbc44b45d39720b5f53ffbddaeeb Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sun, 3 Sep 2023 15:01:00 +0300 Subject: [PATCH 01/12] Update array syntax in website vignette --- vignettes/articles-online-only/opencl.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/articles-online-only/opencl.Rmd b/vignettes/articles-online-only/opencl.Rmd index bc8599efd..838447075 100644 --- a/vignettes/articles-online-only/opencl.Rmd +++ b/vignettes/articles-online-only/opencl.Rmd @@ -75,7 +75,7 @@ data { int k; int n; matrix[n, k] X; - int y[n]; + array[n] int y; } parameters { vector[k] beta; From 792c6b24ac963e6810890a9152a1e1e1be07cd9c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 4 Sep 2023 18:44:14 +0000 Subject: [PATCH 02/12] Bump actions/checkout from 3 to 4 Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/v3...v4) --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/R-CMD-check-wsl.yaml | 2 +- .github/workflows/R-CMD-check.yaml | 2 +- .github/workflows/Test-coverage.yaml | 4 ++-- .github/workflows/cmdstan-tarball-check.yaml | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/R-CMD-check-wsl.yaml b/.github/workflows/R-CMD-check-wsl.yaml index 8419dbd6d..4e2241f2d 100644 --- a/.github/workflows/R-CMD-check-wsl.yaml +++ b/.github/workflows/R-CMD-check-wsl.yaml @@ -35,7 +35,7 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'" - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-r@v2.6.4 with: diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 5d5719920..f5e0f438f 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -48,7 +48,7 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'" - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Install system dependencies if: runner.os == 'Linux' diff --git a/.github/workflows/Test-coverage.yaml b/.github/workflows/Test-coverage.yaml index dda0c325b..fcbbecd1e 100644 --- a/.github/workflows/Test-coverage.yaml +++ b/.github/workflows/Test-coverage.yaml @@ -32,7 +32,7 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} workflow: Test-coverage.yml if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'" - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-r@v2.6.4 - uses: r-lib/actions/setup-pandoc@v2.6.4 @@ -83,7 +83,7 @@ jobs: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} NOT_CRAN: true steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-r@v2.6.4 with: diff --git a/.github/workflows/cmdstan-tarball-check.yaml b/.github/workflows/cmdstan-tarball-check.yaml index 82c261ca4..bdcbc0f65 100644 --- a/.github/workflows/cmdstan-tarball-check.yaml +++ b/.github/workflows/cmdstan-tarball-check.yaml @@ -32,7 +32,7 @@ jobs: NOT_CRAN: true steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Install system dependencies if: runner.os == 'Linux' run: | From c5c68e7b30d730615b42aebe8793b467d08b82f1 Mon Sep 17 00:00:00 2001 From: jgabry Date: Wed, 6 Sep 2023 15:39:58 -0600 Subject: [PATCH 03/12] update old array syntax in some examples --- R/fit.R | 4 ++-- R/model.R | 8 ++++---- man/CmdStanGQ.Rd | 6 +++--- man/fit-method-profiles.Rd | 4 ++-- man/model-method-check_syntax.Rd | 2 +- man/model-method-generate-quantities.Rd | 6 +++--- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/R/fit.R b/R/fit.R index efd05fdd3..96e438b36 100644 --- a/R/fit.R +++ b/R/fit.R @@ -1158,7 +1158,7 @@ CmdStanFit$set("public", name = "return_codes", value = return_codes) #' mcmc_program <- write_stan_file( #' 'data { #' int N; -#' int y[N]; +#' array[N] int y; #' } #' parameters { #' real theta; @@ -1169,7 +1169,7 @@ CmdStanFit$set("public", name = "return_codes", value = return_codes) #' } #' } #' generated quantities { -#' int y_rep[N]; +#' array[N] int y_rep; #' profile("gq") { #' y_rep = bernoulli_rng(rep_vector(theta, N)); #' } diff --git a/R/model.R b/R/model.R index 35d333ba6..96cb99cad 100644 --- a/R/model.R +++ b/R/model.R @@ -806,7 +806,7 @@ CmdStanModel$set("public", name = "variables", value = variables) #' file <- write_stan_file(" #' data { #' int N; -#' int y[N]; +#' array[N] int y; #' } #' parameters { #' // should have but omitting to demonstrate pedantic mode @@ -1659,7 +1659,7 @@ CmdStanModel$set("public", name = "variational", value = variational) #' mcmc_program <- write_stan_file( #' "data { #' int N; -#' int y[N]; +#' array[N] int y; #' } #' parameters { #' real theta; @@ -1678,13 +1678,13 @@ CmdStanModel$set("public", name = "variational", value = variational) #' gq_program <- write_stan_file( #' "data { #' int N; -#' int y[N]; +#' array[N] int y; #' } #' parameters { #' real theta; #' } #' generated quantities { -#' int y_rep[N] = bernoulli_rng(rep_vector(theta, N)); +#' array[N] int y_rep = bernoulli_rng(rep_vector(theta, N)); #' }" #' ) #' diff --git a/man/CmdStanGQ.Rd b/man/CmdStanGQ.Rd index 6aa7a3ce8..860820561 100644 --- a/man/CmdStanGQ.Rd +++ b/man/CmdStanGQ.Rd @@ -52,7 +52,7 @@ all of which have their own (linked) documentation pages. mcmc_program <- write_stan_file( "data { int N; - int y[N]; + array[N] int y; } parameters { real theta; @@ -71,13 +71,13 @@ fit_mcmc <- mod_mcmc$sample(data = data, seed = 123, refresh = 0) gq_program <- write_stan_file( "data { int N; - int y[N]; + array[N] int y; } parameters { real theta; } generated quantities { - int y_rep[N] = bernoulli_rng(rep_vector(theta, N)); + array[N] int y_rep = bernoulli_rng(rep_vector(theta, N)); }" ) diff --git a/man/fit-method-profiles.Rd b/man/fit-method-profiles.Rd index 3aff8f11a..963111795 100644 --- a/man/fit-method-profiles.Rd +++ b/man/fit-method-profiles.Rd @@ -26,7 +26,7 @@ requires adding profiling statements to the Stan program. mcmc_program <- write_stan_file( 'data { int N; - int y[N]; + array[N] int y; } parameters { real theta; @@ -37,7 +37,7 @@ mcmc_program <- write_stan_file( } } generated quantities { - int y_rep[N]; + array[N] int y_rep; profile("gq") { y_rep = bernoulli_rng(rep_vector(theta, N)); } diff --git a/man/model-method-check_syntax.Rd b/man/model-method-check_syntax.Rd index 9193a684d..8f9623bf2 100644 --- a/man/model-method-check_syntax.Rd +++ b/man/model-method-check_syntax.Rd @@ -45,7 +45,7 @@ parsing succeeds. If invalid syntax in found an error is thrown. file <- write_stan_file(" data { int N; - int y[N]; + array[N] int y; } parameters { // should have but omitting to demonstrate pedantic mode diff --git a/man/model-method-generate-quantities.Rd b/man/model-method-generate-quantities.Rd index 5a80bb736..ebe5d1f60 100644 --- a/man/model-method-generate-quantities.Rd +++ b/man/model-method-generate-quantities.Rd @@ -119,7 +119,7 @@ based on previously fitted parameters. mcmc_program <- write_stan_file( "data { int N; - int y[N]; + array[N] int y; } parameters { real theta; @@ -138,13 +138,13 @@ fit_mcmc <- mod_mcmc$sample(data = data, seed = 123, refresh = 0) gq_program <- write_stan_file( "data { int N; - int y[N]; + array[N] int y; } parameters { real theta; } generated quantities { - int y_rep[N] = bernoulli_rng(rep_vector(theta, N)); + array[N] int y_rep = bernoulli_rng(rep_vector(theta, N)); }" ) From 0b2c3be70788f85fff3ccdfc47d2ddf50aeac9d5 Mon Sep 17 00:00:00 2001 From: jgabry Date: Wed, 6 Sep 2023 16:14:59 -0600 Subject: [PATCH 04/12] also fix deprecations vignette --- vignettes/deprecations.Rmd | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/vignettes/deprecations.Rmd b/vignettes/deprecations.Rmd index 920aebed6..cd1f18b16 100644 --- a/vignettes/deprecations.Rmd +++ b/vignettes/deprecations.Rmd @@ -46,7 +46,7 @@ data { int k; int n; matrix[n, k] X; - int y[n]; + array[n] int y; } parameters { vector[k] beta; @@ -64,22 +64,10 @@ mod <- cmdstan_model(stan_file) ``` -The first warning is about using the deprecated array syntax - -``` -int y[n]; -``` - -which should be replaced with the new syntax using the `array` keyword: - -``` -array[n] int y; -``` - -The second warning is about using the deprecated commenting symbol `#`, +The first warning is about using the deprecated commenting symbol `#`, which should be replaced by `//`. -The last warning is about the use of the deprecated `_log` suffix for +The second warning is about the use of the deprecated `_log` suffix for probability density and mass functions. In this case the `_log` suffix should be replaced with `_lpdf`. For probability mass functions the suffix `_lpmf` is used. From dff8d707cd6f168046ecedc84d5fdde2f8226780 Mon Sep 17 00:00:00 2001 From: jgabry Date: Thu, 7 Sep 2023 14:09:22 -0600 Subject: [PATCH 05/12] remove Deprecations vignette due to 2.33 changes --- _pkgdown.yml | 1 - vignettes/deprecations.Rmd | 119 ------------------------------------- 2 files changed, 120 deletions(-) delete mode 100644 vignettes/deprecations.Rmd diff --git a/_pkgdown.yml b/_pkgdown.yml index ca5375be8..e9133e951 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -72,7 +72,6 @@ articles: - cmdstanr-internals - posterior - r-markdown - - deprecations - profiling - articles-online-only/opencl diff --git a/vignettes/deprecations.Rmd b/vignettes/deprecations.Rmd deleted file mode 100644 index cd1f18b16..000000000 --- a/vignettes/deprecations.Rmd +++ /dev/null @@ -1,119 +0,0 @@ ---- -title: "Handling deprecated Stan features with the canonicalizer in CmdStanR" -author: "Rok Češnovar and Jonah Gabry" -output: - rmarkdown::html_vignette: - toc: true - toc_depth: 4 -params: - EVAL: !r identical(Sys.getenv("NOT_CRAN"), "true") -vignette: > - %\VignetteIndexEntry{Handling deprecated Stan features with the canonicalizer in CmdStanR} - %\VignetteEngine{knitr::rmarkdown} - %\VignetteEncoding{UTF-8} ---- - -```{r child="children/_settings-knitr.Rmd"} -``` - -## Introduction - -This vignette demonstrates how to handle cases where your Stan program contains -deprecated features resulting in deprecation warnings. In most cases, the -Stan-to-C++ compiler can be used to automatically update your code to a -non-deprecated feature that replaces the deprecated one. This vignette showcases -how that automatic conversion can be done using CmdStanR. - -The automatic conversion of deprecated features to non-deprecated features is -done using the so-called "canonicalizer", which is part of the Stan-to-C++ -compiler. We recommend using CmdStan 2.29.2 or later when using the -canonicalizer and this vignette. The minimum CmdStanR version to run the -code in the vignette is 0.5.0. - -```{r library, message=FALSE} -library(cmdstanr) -check_cmdstan_toolchain(fix = TRUE, quiet = TRUE) -``` - -## Deprecation warnings - -The following logistic regression model uses several deprecated language -features, resulting in several warnings during compilation. - -```{r logistic} -stan_file <- write_stan_file(" -data { - int k; - int n; - matrix[n, k] X; - array[n] int y; -} -parameters { - vector[k] beta; - real alpha; -} -model { - # priors - target += std_normal_log(beta); - alpha ~ std_normal(); - - y ~ bernoulli_logit(X * beta + alpha); -} -") -mod <- cmdstan_model(stan_file) -``` - - -The first warning is about using the deprecated commenting symbol `#`, -which should be replaced by `//`. - -The second warning is about the use of the deprecated `_log` suffix for -probability density and mass functions. In this case the `_log` suffix should be -replaced with `_lpdf`. For probability mass functions the suffix `_lpmf` is -used. - -We can go and fix these issues manually or use the canonicalizer as outlined -in the next section. - -## Using the canonicalizer - -The canonicalizer is available through the `canonicalize` argument of the -`$format()` method of the `CmdStanModel` class. The arguments accepts -`TRUE` and `FALSE` values, in which case all or none of the features of the -canonicalizer are used. It can also accept a list of character vectors that -determine which features of the canonicalizer to use. - -The canonincalizer in CmdStan 2.29.2 supports four features: `parentheses`, -`braces`, `includes` and `deprecations`. The `parentheses` and `braces` features -clean up the use of parentheses and braces, while `includes` will replace -`#include` statements with the code from the included files. See the -[canonicalizer section of the Stan User's Guide](https://mc-stan.org/docs/2_29/stan-users-guide/stanc-pretty-printing.html#canonicalizing) -for more details. - -In this vignette we will be using the `deprecations` feature that replaces -deprecated Stan model features with non-deprecated ones if possible. - -```{r canonicalize, message=FALSE} -mod$format(canonicalize = list("deprecations")) -``` - -By default, the format function will print the resulting model code. We -can see that all three issues were resolved. `y` is now defined using the -new array keyword, the comment uses `//` and the `std_normal_log()` is replaced -with `std_normal_lpdf()`. - -You can also use the `$format()` method to write the updated version of the -model directly to the Stan model file. That can be enabled by setting -`overwrite_file = TRUE`. The previous version of the file will automatically -be backed up to a file with the `.stan.bak` suffix. If that is not desired or -you are using a version system and making a backup is redundant, -you can disable it by setting `backup = FALSE`. - -```{r overwrite_file} -mod$format( - canonicalize = list("deprecations"), - overwrite_file = TRUE, - backup = FALSE -) -mod$print() -``` From b3025976057708e9885e8b05321ddb35f841e665 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 11 Sep 2023 12:58:25 +0200 Subject: [PATCH 06/12] Fix exposing functions under 2.33+ --- R/utils.R | 19 +++++++++++++++++-- tests/testthat/test-model-expose-functions.R | 3 ++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/R/utils.R b/R/utils.R index 2f7d2b60d..639daf0bd 100644 --- a/R/utils.R +++ b/R/utils.R @@ -816,7 +816,20 @@ get_standalone_hpp <- function(stan_file, stancflags) { get_function_name <- function(fun_start, fun_end, model_lines) { fun_string <- paste(model_lines[(fun_start+1):fun_end], collapse = " ") - fun_name <- gsub("auto ", "", fun_string, fixed = TRUE) + types <- c( + "auto", + "int", + "double", + "Eigen::Matrix<(.*)>", + "std::vector<(.*)>" + ) + pattern <- paste0( + # Only match if the type occurs at start of string + "^(\\s*)?(", + paste0(types, collapse="|"), + # Only match if type followed by a function name and opening bracket + ")\\s*(?=\\w*\\()") + fun_name <- gsub(pattern, "", fun_string, perl = TRUE) sub("\\(.*", "", fun_name, perl = TRUE) } @@ -864,7 +877,9 @@ get_plain_rtn <- function(fun_start, fun_end, model_lines) { # that instantiates an RNG prep_fun_cpp <- function(fun_start, fun_end, model_lines) { fun_body <- paste(model_lines[fun_start:fun_end], collapse = " ") - fun_body <- gsub("auto", get_plain_rtn(fun_start, fun_end, model_lines), fun_body) + if (cmdstan_version() < "2.33") { + fun_body <- gsub("auto", get_plain_rtn(fun_start, fun_end, model_lines), fun_body) + } fun_body <- gsub("// [[stan::function]]", "// [[Rcpp::export]]\n", fun_body, fixed = TRUE) fun_body <- gsub("std::ostream\\*\\s*pstream__\\s*=\\s*nullptr", "", fun_body) fun_body <- gsub("boost::ecuyer1988&\\s*base_rng__", "SEXP base_rng_ptr", fun_body) diff --git a/tests/testthat/test-model-expose-functions.R b/tests/testthat/test-model-expose-functions.R index e1e99d1bb..75db20f50 100644 --- a/tests/testthat/test-model-expose-functions.R +++ b/tests/testthat/test-model-expose-functions.R @@ -192,7 +192,8 @@ test_that("Exposing functions with precompiled model gives meaningful error", { parameters { real x; } model { x ~ std_normal(); } ") - mod1 <- cmdstan_model(stan_file, compile_standalone = TRUE) + mod1 <- cmdstan_model(stan_file, compile_standalone = TRUE, + force_recompile = TRUE) expect_equal(7.5, mod1$functions$a_plus_b(5, 2.5)) mod2 <- cmdstan_model(stan_file) From 01412e1072904a78ef2c4d21b32bc7356b7281cd Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 11 Sep 2023 15:03:40 +0200 Subject: [PATCH 07/12] Update deprecated syntax in tests/docs --- R/data.R | 8 ++++---- R/example.R | 2 +- R/model.R | 2 +- man/model-method-format.Rd | 2 +- man/write_stan_file.Rd | 2 +- man/write_stan_json.Rd | 8 ++++---- tests/testthat/test-data.R | 2 +- tests/testthat/test-example.R | 2 +- tests/testthat/test-fit-shared.R | 2 +- tests/testthat/test-model-sample_mpi.R | 8 ++++---- 10 files changed, 19 insertions(+), 19 deletions(-) diff --git a/R/data.R b/R/data.R index a1e4ab7c6..a9438bc0a 100644 --- a/R/data.R +++ b/R/data.R @@ -20,16 +20,16 @@ #' The `list` to `array` conversion is intended to make it easier to prepare #' the data for certain Stan declarations involving arrays: #' -#' * `vector[J] v[K]` (or equivalently `array[K] vector[J] v ` as of Stan 2.27) +#' * `array[K] vector[J] v` (or equivalently `array[K] vector[J] v ` as of Stan 2.27) #' can be constructed in \R as a list with `K` elements where each element a #' vector of length `J` -#' * `matrix[I,J] v[K]` (or equivalently `array[K] matrix[I,J] m ` as of Stan +#' * `array[K] matrix[I,J] v` (or equivalently `array[K] matrix[I,J] m ` as of Stan #' 2.27 ) can be constructed in \R as a list with `K` elements where each element #' an `IxJ` matrix #' #' These can also be passed in from \R as arrays instead of lists but the list #' option is provided for convenience. Unfortunately for arrays with more than -#' one dimension, e.g., `vector[J] v[K,L]` (or equivalently +#' one dimension, e.g., `array[K,L] vector[J] v` (or equivalently #' `array[K,L] vector[J] v ` as of Stan 2.27) it is not possible to use an \R #' list and an array must be used instead. For this example the array in \R #' should have dimensions `KxLxJ`. @@ -49,7 +49,7 @@ #' #' #' # demonstrating list to array conversion -#' # suppose x is declared as `vector[3] x[2]` (or equivalently `array[2] vector[3] x`) +#' # suppose x is declared as `array[2] vector[3] x` (or equivalently `array[2] vector[3] x`) #' # we can use a list of length 2 where each element is a vector of length 3 #' data <- list(x = list(1:3, 4:6)) #' file <- tempfile(fileext = ".json") diff --git a/R/example.R b/R/example.R index bb9b371db..f322185f5 100644 --- a/R/example.R +++ b/R/example.R @@ -124,7 +124,7 @@ print_example_program <- #' stan_program <- " #' data { #' int N; -#' int y[N]; +#' array[N] int y; #' } #' parameters { #' real theta; diff --git a/R/model.R b/R/model.R index 96cb99cad..14871b91f 100644 --- a/R/model.R +++ b/R/model.R @@ -932,7 +932,7 @@ CmdStanModel$set("public", name = "check_syntax", value = check_syntax) #' file <- write_stan_file(" #' data { #' int N; -#' int y[N]; +#' array[N] int y; #' } #' parameters { #' real lambda; diff --git a/man/model-method-format.Rd b/man/model-method-format.Rd index 1d7ed11bd..57944b589 100644 --- a/man/model-method-format.Rd +++ b/man/model-method-format.Rd @@ -49,7 +49,7 @@ model directly back to the file or prints it for inspection. file <- write_stan_file(" data { int N; - int y[N]; + array[N] int y; } parameters { real lambda; diff --git a/man/write_stan_file.Rd b/man/write_stan_file.Rd index cdb5cbc7b..d172cf70e 100644 --- a/man/write_stan_file.Rd +++ b/man/write_stan_file.Rd @@ -51,7 +51,7 @@ should ensure thread-safety in the rare cases when it is needed. stan_program <- " data { int N; - int y[N]; + array[N] int y; } parameters { real theta; diff --git a/man/write_stan_json.Rd b/man/write_stan_json.Rd index cc41c5985..c76d4db54 100644 --- a/man/write_stan_json.Rd +++ b/man/write_stan_json.Rd @@ -32,17 +32,17 @@ file: The \code{list} to \code{array} conversion is intended to make it easier to prepare the data for certain Stan declarations involving arrays: \itemize{ -\item \verb{vector[J] v[K]} (or equivalently \verb{array[K] vector[J] v } as of Stan 2.27) +\item \verb{array[K] vector[J] v} (or equivalently \verb{array[K] vector[J] v } as of Stan 2.27) can be constructed in \R as a list with \code{K} elements where each element a vector of length \code{J} -\item \verb{matrix[I,J] v[K]} (or equivalently \verb{array[K] matrix[I,J] m } as of Stan +\item \verb{array[K] matrix[I,J] v} (or equivalently \verb{array[K] matrix[I,J] m } as of Stan 2.27 ) can be constructed in \R as a list with \code{K} elements where each element an \code{IxJ} matrix } These can also be passed in from \R as arrays instead of lists but the list option is provided for convenience. Unfortunately for arrays with more than -one dimension, e.g., \verb{vector[J] v[K,L]} (or equivalently +one dimension, e.g., \verb{array[K,L] vector[J] v} (or equivalently \verb{array[K,L] vector[J] v } as of Stan 2.27) it is not possible to use an \R list and an array must be used instead. For this example the array in \R should have dimensions \code{KxLxJ}. @@ -62,7 +62,7 @@ cat(readLines(file), sep = "\n") # demonstrating list to array conversion -# suppose x is declared as `vector[3] x[2]` (or equivalently `array[2] vector[3] x`) +# suppose x is declared as `array[2] vector[3] x` (or equivalently `array[2] vector[3] x`) # we can use a list of length 2 where each element is a vector of length 3 data <- list(x = list(1:3, 4:6)) file <- tempfile(fileext = ".json") diff --git a/tests/testthat/test-data.R b/tests/testthat/test-data.R index bae7b49ec..01fa72914 100644 --- a/tests/testthat/test-data.R +++ b/tests/testthat/test-data.R @@ -339,7 +339,7 @@ test_that("process_data() corrrectly casts integers and floating point numbers", stan_file <- write_stan_file(" data { - int k[3,3]; + array[3,3] int k; } ") mod <- cmdstan_model(stan_file, compile = FALSE) diff --git a/tests/testthat/test-example.R b/tests/testthat/test-example.R index 842a30752..8d14d5d0e 100644 --- a/tests/testthat/test-example.R +++ b/tests/testthat/test-example.R @@ -23,7 +23,7 @@ test_that("cmdstanr_example works", { stan_program <- " data { int N; - int y[N]; + array[N] int y; } parameters { real theta; diff --git a/tests/testthat/test-fit-shared.R b/tests/testthat/test-fit-shared.R index a7ed10781..193a96645 100644 --- a/tests/testthat/test-fit-shared.R +++ b/tests/testthat/test-fit-shared.R @@ -308,7 +308,7 @@ test_that("sig_figs works with all methods", { m <- "data { int N; int K; - int y[N]; + array[N] int y; matrix[N, K] X; } parameters { diff --git a/tests/testthat/test-model-sample_mpi.R b/tests/testthat/test-model-sample_mpi.R index 6b348f2ed..1ea0f9a68 100644 --- a/tests/testthat/test-model-sample_mpi.R +++ b/tests/testthat/test-model-sample_mpi.R @@ -4,15 +4,15 @@ test_that("sample_mpi() works", { skip_if(!mpi_toolchain_present()) mpi_file <- write_stan_file(" functions { - vector test(vector beta, vector theta, real[] x, int[] y) { + vector test(vector beta, vector theta, array[] real x, array[] int y) { return theta; } } transformed data { vector[4] a; - vector[5] b[4] = {[1,1,1,1,1]', [2,2,2,2,2]', [3,3,3,3,3]', [4,4,4,4,4]'}; - real x[4,4]; - int y[4,4]; + array[4] vector[5] b = {[1,1,1,1,1]', [2,2,2,2,2]', [3,3,3,3,3]', [4,4,4,4,4]'}; + array[4,4] real x; + array[4,4] int y; } parameters { real beta; From 5cf67f7665fd4e39ca5aac0ebbdcff33a7c541b8 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 11 Sep 2023 15:32:59 +0200 Subject: [PATCH 08/12] Revert stray changes --- R/data.R | 8 ++++---- man/write_stan_json.Rd | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/R/data.R b/R/data.R index a9438bc0a..a1e4ab7c6 100644 --- a/R/data.R +++ b/R/data.R @@ -20,16 +20,16 @@ #' The `list` to `array` conversion is intended to make it easier to prepare #' the data for certain Stan declarations involving arrays: #' -#' * `array[K] vector[J] v` (or equivalently `array[K] vector[J] v ` as of Stan 2.27) +#' * `vector[J] v[K]` (or equivalently `array[K] vector[J] v ` as of Stan 2.27) #' can be constructed in \R as a list with `K` elements where each element a #' vector of length `J` -#' * `array[K] matrix[I,J] v` (or equivalently `array[K] matrix[I,J] m ` as of Stan +#' * `matrix[I,J] v[K]` (or equivalently `array[K] matrix[I,J] m ` as of Stan #' 2.27 ) can be constructed in \R as a list with `K` elements where each element #' an `IxJ` matrix #' #' These can also be passed in from \R as arrays instead of lists but the list #' option is provided for convenience. Unfortunately for arrays with more than -#' one dimension, e.g., `array[K,L] vector[J] v` (or equivalently +#' one dimension, e.g., `vector[J] v[K,L]` (or equivalently #' `array[K,L] vector[J] v ` as of Stan 2.27) it is not possible to use an \R #' list and an array must be used instead. For this example the array in \R #' should have dimensions `KxLxJ`. @@ -49,7 +49,7 @@ #' #' #' # demonstrating list to array conversion -#' # suppose x is declared as `array[2] vector[3] x` (or equivalently `array[2] vector[3] x`) +#' # suppose x is declared as `vector[3] x[2]` (or equivalently `array[2] vector[3] x`) #' # we can use a list of length 2 where each element is a vector of length 3 #' data <- list(x = list(1:3, 4:6)) #' file <- tempfile(fileext = ".json") diff --git a/man/write_stan_json.Rd b/man/write_stan_json.Rd index c76d4db54..cc41c5985 100644 --- a/man/write_stan_json.Rd +++ b/man/write_stan_json.Rd @@ -32,17 +32,17 @@ file: The \code{list} to \code{array} conversion is intended to make it easier to prepare the data for certain Stan declarations involving arrays: \itemize{ -\item \verb{array[K] vector[J] v} (or equivalently \verb{array[K] vector[J] v } as of Stan 2.27) +\item \verb{vector[J] v[K]} (or equivalently \verb{array[K] vector[J] v } as of Stan 2.27) can be constructed in \R as a list with \code{K} elements where each element a vector of length \code{J} -\item \verb{array[K] matrix[I,J] v} (or equivalently \verb{array[K] matrix[I,J] m } as of Stan +\item \verb{matrix[I,J] v[K]} (or equivalently \verb{array[K] matrix[I,J] m } as of Stan 2.27 ) can be constructed in \R as a list with \code{K} elements where each element an \code{IxJ} matrix } These can also be passed in from \R as arrays instead of lists but the list option is provided for convenience. Unfortunately for arrays with more than -one dimension, e.g., \verb{array[K,L] vector[J] v} (or equivalently +one dimension, e.g., \verb{vector[J] v[K,L]} (or equivalently \verb{array[K,L] vector[J] v } as of Stan 2.27) it is not possible to use an \R list and an array must be used instead. For this example the array in \R should have dimensions \code{KxLxJ}. @@ -62,7 +62,7 @@ cat(readLines(file), sep = "\n") # demonstrating list to array conversion -# suppose x is declared as `array[2] vector[3] x` (or equivalently `array[2] vector[3] x`) +# suppose x is declared as `vector[3] x[2]` (or equivalently `array[2] vector[3] x`) # we can use a list of length 2 where each element is a vector of length 3 data <- list(x = list(1:3, 4:6)) file <- tempfile(fileext = ".json") From 66a3b58e64a9523bf68de05b7c73cb4421a62cfd Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 12 Sep 2023 18:46:47 +0300 Subject: [PATCH 09/12] Additional test fixes --- tests/testthat/test-fit-mle.R | 2 +- tests/testthat/test-model-compile.R | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/tests/testthat/test-fit-mle.R b/tests/testthat/test-fit-mle.R index 40fddc2bc..f5828a60d 100644 --- a/tests/testthat/test-fit-mle.R +++ b/tests/testthat/test-fit-mle.R @@ -53,7 +53,7 @@ test_that("time is reported after optimization", { test_that("no error when checking estimates after failure", { fit <- cmdstanr_example("schools", method = "optimize", seed = 123) # optim ålways fails for this - expect_silent(fit$summary()) # no error + expect_silent(fit$summary(include_failed = TRUE)) # no error }) test_that("draws() works for different formats", { diff --git a/tests/testthat/test-model-compile.R b/tests/testthat/test-model-compile.R index 3134bbc0b..8fae1a026 100644 --- a/tests/testthat/test-model-compile.R +++ b/tests/testthat/test-model-compile.R @@ -424,14 +424,14 @@ test_that("check_syntax() works with pedantic=TRUE", { " stan_file <- write_stan_file(model_code) mod_dep_warning <- cmdstan_model(stan_file, compile = FALSE) - expect_message( + expect_error( mod_dep_warning$compile(), - "deprecated in the Stan language", + "An error occured during compilation! See the message above for more information.", fixed = TRUE ) - expect_message( + expect_error( mod_dep_warning$check_syntax(), - "deprecated in the Stan language", + "Syntax error found! See the message above for more information.", fixed = TRUE ) }) @@ -690,13 +690,9 @@ test_that("format() works", { stan_file_tmp <- write_stan_file(code) mod_1 <- cmdstan_model(stan_file_tmp, compile = FALSE) - expect_output( - expect_message( - mod_1$format(), - "is deprecated", - fixed = TRUE - ), - "target += normal_log(y, 0, 1);", + expect_error( + mod_1$format(), + "Syntax error found! See the message above for more information.", fixed = TRUE ) From 8707a0424e14956d3743acaf979c3a1db3dcaeb3 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 12 Sep 2023 20:31:13 +0300 Subject: [PATCH 10/12] More test fixes --- tests/testthat/test-fit-mle.R | 2 +- tests/testthat/test-model-compile.R | 23 ++++++++--------------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/tests/testthat/test-fit-mle.R b/tests/testthat/test-fit-mle.R index f5828a60d..96ef2b953 100644 --- a/tests/testthat/test-fit-mle.R +++ b/tests/testthat/test-fit-mle.R @@ -53,7 +53,7 @@ test_that("time is reported after optimization", { test_that("no error when checking estimates after failure", { fit <- cmdstanr_example("schools", method = "optimize", seed = 123) # optim ålways fails for this - expect_silent(fit$summary(include_failed = TRUE)) # no error + expect_error(fit$summary(), "Fitting failed. Unable to retrieve the draws.") }) test_that("draws() works for different formats", { diff --git a/tests/testthat/test-model-compile.R b/tests/testthat/test-model-compile.R index 8fae1a026..90d9579c6 100644 --- a/tests/testthat/test-model-compile.R +++ b/tests/testthat/test-model-compile.R @@ -278,7 +278,7 @@ test_that("compile() works with pedantic=TRUE", { } ") expect_message( - mod_pedantic_warn <- cmdstan_model(stan_file, pedantic = TRUE), + mod_pedantic_warn <- cmdstan_model(stan_file, pedantic = TRUE, force_recompile = TRUE), "The parameter x was declared but was not used", fixed = TRUE ) @@ -387,13 +387,10 @@ test_that("check_syntax() works with pedantic=TRUE", { fixed = TRUE ) - expect_output( - expect_message( - mod_pedantic_warn$check_syntax(pedantic = TRUE), - "The parameter x was declared but was not used", - fixed = TRUE - ), - regexp = NA + expect_message( + mod_pedantic_warn$check_syntax(pedantic = TRUE), + "The parameter x was declared but was not used", + fixed = TRUE ) }) @@ -706,13 +703,9 @@ test_that("format() works", { "target += normal_lpdf(y | 0, 1);", fixed = TRUE ) - expect_output( - expect_message( - mod_1$format(canonicalize = list("includes")), - "is deprecated", - fixed = TRUE - ), - "target += normal_log(y, 0, 1);", + expect_error( + mod_1$format(), + "Syntax error found! See the message above for more information.", fixed = TRUE ) From fa8ba5aeb4abd1cd89a505ac1a1aecda89b54801 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 12 Sep 2023 21:14:28 +0300 Subject: [PATCH 11/12] Parallel testthat --- DESCRIPTION | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index a3dae07ad..84167f5f6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -50,3 +50,5 @@ Suggests: Rcpp, RcppEigen VignetteBuilder: knitr +Config/testthat/parallel: true +Config/testthat/edition: 3 From 1caa73286eb4c7bdde1e48c6fe3988d6f3dfa8ef Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 12 Sep 2023 22:05:08 +0300 Subject: [PATCH 12/12] Revert parallel testthat --- DESCRIPTION | 2 -- 1 file changed, 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 84167f5f6..a3dae07ad 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -50,5 +50,3 @@ Suggests: Rcpp, RcppEigen VignetteBuilder: knitr -Config/testthat/parallel: true -Config/testthat/edition: 3