Skip to content

Commit b29374e

Browse files
authored
Merge pull request #843 from stan-dev/fix-old-array-syntax
Compatibility fixes for cmdstan 2.33+
2 parents 5e2551c + 1caa732 commit b29374e

19 files changed

+61
-188
lines changed

R/example.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ print_example_program <-
124124
#' stan_program <- "
125125
#' data {
126126
#' int<lower=0> N;
127-
#' int<lower=0,upper=1> y[N];
127+
#' array[N] int<lower=0,upper=1> y;
128128
#' }
129129
#' parameters {
130130
#' real<lower=0,upper=1> theta;

R/fit.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ CmdStanFit$set("public", name = "return_codes", value = return_codes)
11581158
#' mcmc_program <- write_stan_file(
11591159
#' 'data {
11601160
#' int<lower=0> N;
1161-
#' int<lower=0,upper=1> y[N];
1161+
#' array[N] int<lower=0,upper=1> y;
11621162
#' }
11631163
#' parameters {
11641164
#' real<lower=0,upper=1> theta;
@@ -1169,7 +1169,7 @@ CmdStanFit$set("public", name = "return_codes", value = return_codes)
11691169
#' }
11701170
#' }
11711171
#' generated quantities {
1172-
#' int y_rep[N];
1172+
#' array[N] int y_rep;
11731173
#' profile("gq") {
11741174
#' y_rep = bernoulli_rng(rep_vector(theta, N));
11751175
#' }

R/model.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ CmdStanModel$set("public", name = "variables", value = variables)
806806
#' file <- write_stan_file("
807807
#' data {
808808
#' int N;
809-
#' int y[N];
809+
#' array[N] int y;
810810
#' }
811811
#' parameters {
812812
#' // should have <lower=0> but omitting to demonstrate pedantic mode
@@ -932,7 +932,7 @@ CmdStanModel$set("public", name = "check_syntax", value = check_syntax)
932932
#' file <- write_stan_file("
933933
#' data {
934934
#' int N;
935-
#' int y[N];
935+
#' array[N] int y;
936936
#' }
937937
#' parameters {
938938
#' real lambda;
@@ -1659,7 +1659,7 @@ CmdStanModel$set("public", name = "variational", value = variational)
16591659
#' mcmc_program <- write_stan_file(
16601660
#' "data {
16611661
#' int<lower=0> N;
1662-
#' int<lower=0,upper=1> y[N];
1662+
#' array[N] int<lower=0,upper=1> y;
16631663
#' }
16641664
#' parameters {
16651665
#' real<lower=0,upper=1> theta;
@@ -1678,13 +1678,13 @@ CmdStanModel$set("public", name = "variational", value = variational)
16781678
#' gq_program <- write_stan_file(
16791679
#' "data {
16801680
#' int<lower=0> N;
1681-
#' int<lower=0,upper=1> y[N];
1681+
#' array[N] int<lower=0,upper=1> y;
16821682
#' }
16831683
#' parameters {
16841684
#' real<lower=0,upper=1> theta;
16851685
#' }
16861686
#' generated quantities {
1687-
#' int y_rep[N] = bernoulli_rng(rep_vector(theta, N));
1687+
#' array[N] int y_rep = bernoulli_rng(rep_vector(theta, N));
16881688
#' }"
16891689
#' )
16901690
#'

R/utils.R

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,20 @@ get_standalone_hpp <- function(stan_file, stancflags) {
816816

817817
get_function_name <- function(fun_start, fun_end, model_lines) {
818818
fun_string <- paste(model_lines[(fun_start+1):fun_end], collapse = " ")
819-
fun_name <- gsub("auto ", "", fun_string, fixed = TRUE)
819+
types <- c(
820+
"auto",
821+
"int",
822+
"double",
823+
"Eigen::Matrix<(.*)>",
824+
"std::vector<(.*)>"
825+
)
826+
pattern <- paste0(
827+
# Only match if the type occurs at start of string
828+
"^(\\s*)?(",
829+
paste0(types, collapse="|"),
830+
# Only match if type followed by a function name and opening bracket
831+
")\\s*(?=\\w*\\()")
832+
fun_name <- gsub(pattern, "", fun_string, perl = TRUE)
820833
sub("\\(.*", "", fun_name, perl = TRUE)
821834
}
822835

@@ -864,7 +877,9 @@ get_plain_rtn <- function(fun_start, fun_end, model_lines) {
864877
# that instantiates an RNG
865878
prep_fun_cpp <- function(fun_start, fun_end, model_lines) {
866879
fun_body <- paste(model_lines[fun_start:fun_end], collapse = " ")
867-
fun_body <- gsub("auto", get_plain_rtn(fun_start, fun_end, model_lines), fun_body)
880+
if (cmdstan_version() < "2.33") {
881+
fun_body <- gsub("auto", get_plain_rtn(fun_start, fun_end, model_lines), fun_body)
882+
}
868883
fun_body <- gsub("// [[stan::function]]", "// [[Rcpp::export]]\n", fun_body, fixed = TRUE)
869884
fun_body <- gsub("std::ostream\\*\\s*pstream__\\s*=\\s*nullptr", "", fun_body)
870885
fun_body <- gsub("boost::ecuyer1988&\\s*base_rng__", "SEXP base_rng_ptr", fun_body)

_pkgdown.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ articles:
7272
- cmdstanr-internals
7373
- posterior
7474
- r-markdown
75-
- deprecations
7675
- profiling
7776
- articles-online-only/opencl
7877

man/CmdStanGQ.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fit-method-profiles.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/model-method-check_syntax.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/model-method-format.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/model-method-generate-quantities.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/write_stan_file.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-data.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ test_that("process_data() corrrectly casts integers and floating point numbers",
339339

340340
stan_file <- write_stan_file("
341341
data {
342-
int<lower=0> k[3,3];
342+
array[3,3] int<lower=0> k;
343343
}
344344
")
345345
mod <- cmdstan_model(stan_file, compile = FALSE)

tests/testthat/test-example.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test_that("cmdstanr_example works", {
2323
stan_program <- "
2424
data {
2525
int<lower=0> N;
26-
int<lower=0,upper=1> y[N];
26+
array[N] int<lower=0,upper=1> y;
2727
}
2828
parameters {
2929
real<lower=0,upper=1> theta;

tests/testthat/test-fit-mle.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test_that("time is reported after optimization", {
5353

5454
test_that("no error when checking estimates after failure", {
5555
fit <- cmdstanr_example("schools", method = "optimize", seed = 123) # optim ålways fails for this
56-
expect_silent(fit$summary()) # no error
56+
expect_error(fit$summary(), "Fitting failed. Unable to retrieve the draws.")
5757
})
5858

5959
test_that("draws() works for different formats", {

tests/testthat/test-fit-shared.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ test_that("sig_figs works with all methods", {
308308
m <- "data {
309309
int<lower=0> N;
310310
int<lower=0> K;
311-
int<lower=0,upper=1> y[N];
311+
array[N] int<lower=0,upper=1> y;
312312
matrix[N, K] X;
313313
}
314314
parameters {

tests/testthat/test-model-compile.R

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ test_that("compile() works with pedantic=TRUE", {
278278
}
279279
")
280280
expect_message(
281-
mod_pedantic_warn <- cmdstan_model(stan_file, pedantic = TRUE),
281+
mod_pedantic_warn <- cmdstan_model(stan_file, pedantic = TRUE, force_recompile = TRUE),
282282
"The parameter x was declared but was not used",
283283
fixed = TRUE
284284
)
@@ -387,13 +387,10 @@ test_that("check_syntax() works with pedantic=TRUE", {
387387
fixed = TRUE
388388
)
389389

390-
expect_output(
391-
expect_message(
392-
mod_pedantic_warn$check_syntax(pedantic = TRUE),
393-
"The parameter x was declared but was not used",
394-
fixed = TRUE
395-
),
396-
regexp = NA
390+
expect_message(
391+
mod_pedantic_warn$check_syntax(pedantic = TRUE),
392+
"The parameter x was declared but was not used",
393+
fixed = TRUE
397394
)
398395
})
399396

@@ -424,14 +421,14 @@ test_that("check_syntax() works with pedantic=TRUE", {
424421
"
425422
stan_file <- write_stan_file(model_code)
426423
mod_dep_warning <- cmdstan_model(stan_file, compile = FALSE)
427-
expect_message(
424+
expect_error(
428425
mod_dep_warning$compile(),
429-
"deprecated in the Stan language",
426+
"An error occured during compilation! See the message above for more information.",
430427
fixed = TRUE
431428
)
432-
expect_message(
429+
expect_error(
433430
mod_dep_warning$check_syntax(),
434-
"deprecated in the Stan language",
431+
"Syntax error found! See the message above for more information.",
435432
fixed = TRUE
436433
)
437434
})
@@ -690,13 +687,9 @@ test_that("format() works", {
690687
stan_file_tmp <- write_stan_file(code)
691688
mod_1 <- cmdstan_model(stan_file_tmp, compile = FALSE)
692689

693-
expect_output(
694-
expect_message(
695-
mod_1$format(),
696-
"is deprecated",
697-
fixed = TRUE
698-
),
699-
"target += normal_log(y, 0, 1);",
690+
expect_error(
691+
mod_1$format(),
692+
"Syntax error found! See the message above for more information.",
700693
fixed = TRUE
701694
)
702695

@@ -710,13 +703,9 @@ test_that("format() works", {
710703
"target += normal_lpdf(y | 0, 1);",
711704
fixed = TRUE
712705
)
713-
expect_output(
714-
expect_message(
715-
mod_1$format(canonicalize = list("includes")),
716-
"is deprecated",
717-
fixed = TRUE
718-
),
719-
"target += normal_log(y, 0, 1);",
706+
expect_error(
707+
mod_1$format(),
708+
"Syntax error found! See the message above for more information.",
720709
fixed = TRUE
721710
)
722711

tests/testthat/test-model-expose-functions.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ test_that("Exposing functions with precompiled model gives meaningful error", {
192192
parameters { real x; }
193193
model { x ~ std_normal(); }
194194
")
195-
mod1 <- cmdstan_model(stan_file, compile_standalone = TRUE)
195+
mod1 <- cmdstan_model(stan_file, compile_standalone = TRUE,
196+
force_recompile = TRUE)
196197
expect_equal(7.5, mod1$functions$a_plus_b(5, 2.5))
197198

198199
mod2 <- cmdstan_model(stan_file)

tests/testthat/test-model-sample_mpi.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ test_that("sample_mpi() works", {
44
skip_if(!mpi_toolchain_present())
55
mpi_file <- write_stan_file("
66
functions {
7-
vector test(vector beta, vector theta, real[] x, int[] y) {
7+
vector test(vector beta, vector theta, array[] real x, array[] int y) {
88
return theta;
99
}
1010
}
1111
transformed data {
1212
vector[4] a;
13-
vector[5] b[4] = {[1,1,1,1,1]', [2,2,2,2,2]', [3,3,3,3,3]', [4,4,4,4,4]'};
14-
real x[4,4];
15-
int y[4,4];
13+
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]'};
14+
array[4,4] real x;
15+
array[4,4] int y;
1616
}
1717
parameters {
1818
real beta;

0 commit comments

Comments
 (0)