Skip to content

Commit 3af034b

Browse files
committed
use capture.output in more places
1 parent 4b80205 commit 3af034b

9 files changed

Lines changed: 122 additions & 65 deletions

tests/testthat/test-fit-init.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set_cmdstan_path()
55
data_list_schools <- testing_data("schools")
66
data_list_logistic <- testing_data("logistic")
77
test_inits <- function(mod, fit_init, data_list = NULL) {
8+
utils::capture.output({
89
fit_sample <- mod$sample(data = data_list, chains = 1, init = fit_init,
910
iter_sampling = 100, iter_warmup = 100, refresh = 0, seed = 1234)
1011
fit_sample_multi <- mod$sample(data = data_list, chains = 5, init = fit_init,
@@ -20,6 +21,7 @@ test_inits <- function(mod, fit_init, data_list = NULL) {
2021
draws = posterior::as_draws_rvars(fit_init$draws())
2122
fit_sample_draws <- mod$sample(data = data_list, chains = 1, init = draws,
2223
iter_sampling = 100, iter_warmup = 100, refresh = 0, seed = 1234)
24+
})
2325
return(0)
2426
}
2527

tests/testthat/test-fit-shared.R

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -454,22 +454,26 @@ test_that("draws are returned for model with spaces", {
454454
test_that("sampling with inits works with include_paths", {
455455
stan_program_w_include <- testing_stan_file("bernoulli_include")
456456
exe <- cmdstan_ext(strip_ext(stan_program_w_include))
457-
if(file.exists(exe)) {
457+
if (file.exists(exe)) {
458458
file.remove(exe)
459459
}
460460

461-
mod_w_include <- cmdstan_model(stan_file = stan_program_w_include, quiet = FALSE,
462-
include_paths = test_path("resources", "stan"))
461+
mod_w_include <- cmdstan_model(stan_file = stan_program_w_include,
462+
include_paths = test_path("resources", "stan"))
463463

464464
data_list <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1))
465-
466-
fit <- mod_w_include$sample(
467-
data = data_list,
468-
seed = 123,
469-
chains = 4,
470-
parallel_chains = 4,
471-
refresh = 500,
472-
init = list(list(theta = 0.25), list(theta = 0.25), list(theta = 0.25), list(theta = 0.25))
465+
expect_no_error(
466+
fit <- mod_w_include$sample(
467+
data = data_list,
468+
seed = 123,
469+
chains = 4,
470+
parallel_chains = 4,
471+
refresh = 500,
472+
init = list(list(theta = 0.25),
473+
list(theta = 0.25),
474+
list(theta = 0.25),
475+
list(theta = 0.25))
476+
)
473477
)
474478
})
475479

@@ -548,8 +552,12 @@ test_that("code() warns if model not created with Stan file", {
548552
stan_program <- testing_stan_file("bernoulli")
549553
mod <- testing_model("bernoulli")
550554
mod_exe <- cmdstan_model(exe_file = mod$exe_file())
551-
fit_exe <- mod_exe$sample(data = list(N = 10, y = c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1)),
552-
refresh = 0)
555+
utils::capture.output(
556+
fit_exe <- mod_exe$sample(
557+
data = list(N = 10, y = c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1)),
558+
refresh = 0
559+
)
560+
)
553561
expect_warning(
554562
expect_null(fit_exe$code()),
555563
"'$code()' will return NULL because the 'CmdStanModel' was not created with a Stan file",

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ stan_prog <- paste(function_decl,
8080
model <- write_stan_file(stan_prog)
8181
data_list <- testing_data("bernoulli")
8282
mod <- cmdstan_model(model, force_recompile = TRUE)
83-
fit <- mod$sample(data = data_list)
83+
utils::capture.output(
84+
fit <- mod$sample(data = data_list)
85+
)
8486

8587

8688
test_that("Functions can be exposed in model object", {
87-
expect_no_error(mod$expose_functions(verbose = TRUE))
89+
expect_no_error(mod$expose_functions())
8890
})
8991

9092

@@ -260,7 +262,7 @@ test_that("Functions handle complex types correctly", {
260262
})
261263

262264
test_that("Functions can be exposed in fit object", {
263-
fit$expose_functions(verbose = TRUE)
265+
fit$expose_functions()
264266

265267
expect_equal(
266268
fit$functions$rtn_vec(c(1,2,3,4)),
@@ -284,7 +286,9 @@ test_that("Compiled functions can be copied to global environment", {
284286

285287
test_that("Functions can be compiled with model", {
286288
mod <- cmdstan_model(model, force_recompile = TRUE, compile_standalone = TRUE)
287-
fit <- mod$sample(data = data_list)
289+
utils::capture.output(
290+
fit <- mod$sample(data = data_list)
291+
)
288292

289293
expect_message(
290294
fit$expose_functions(),
@@ -344,9 +348,11 @@ test_that("rng functions can be exposed", {
344348
model <- write_stan_file(stan_prog)
345349
data_list <- testing_data("bernoulli")
346350
mod <- cmdstan_model(model, force_recompile = TRUE)
347-
fit <- mod$sample(data = data_list)
351+
utils::capture.output(
352+
fit <- mod$sample(data = data_list)
353+
)
348354

349-
fit$expose_functions(verbose = TRUE)
355+
fit$expose_functions()
350356
set.seed(10)
351357
res1_1 <- fit$functions$wrap_normal_rng(5,10)
352358
res2_1 <- fit$functions$wrap_normal_rng(5,10)

tests/testthat/test-model-init.R

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,12 @@ test_that("Initial values for single-element containers treated correctly", {
303303
"
304304
mod <- cmdstan_model(write_stan_file(modcode), force_recompile = TRUE)
305305
expect_no_error(
306-
fit <- mod$sample(
307-
data = list(y_mean = 0),
308-
init = list(list(y = c(0))),
309-
chains = 1
306+
utils::capture.output(
307+
fit <- mod$sample(
308+
data = list(y_mean = 0),
309+
init = list(list(y = c(0))),
310+
chains = 1
311+
)
310312
)
311313
)
312314
})
@@ -331,7 +333,13 @@ test_that("Pathfinder inits do not drop dimensions", {
331333
"
332334
mod <- cmdstan_model(write_stan_file(modcode), force_recompile = TRUE)
333335
data <- list(N = 100, y = rnorm(100))
334-
pf <- mod$pathfinder(data = data, psis_resample = FALSE)
335-
expect_no_error(fit <- mod$sample(data = data, init = pf, chains = 1,
336-
iter_warmup = 100, iter_sampling = 100))
337-
})
336+
utils::capture.output(
337+
pf <- mod$pathfinder(data = data, psis_resample = FALSE)
338+
)
339+
expect_no_error(
340+
utils::capture.output(
341+
fit <- mod$sample(data = data, init = pf, chains = 1,
342+
iter_warmup = 100, iter_sampling = 100)
343+
)
344+
)
345+
})

tests/testthat/test-model-laplace.R

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ test_that("laplace() runs when all arguments specified validly", {
6363
})
6464

6565
test_that("laplace() all valid 'mode' inputs give same results", {
66-
mode <- mod$optimize(data = data_list, jacobian = TRUE, seed = 100, refresh = 0)
67-
fit1 <- mod$laplace(data = data_list, mode = mode, seed = 100, refresh = 0)
68-
fit2 <- mod$laplace(data = data_list, mode = mode$output_files(), seed = 100, refresh = 0)
69-
fit3 <- mod$laplace(data = data_list, mode = NULL, seed = 100, refresh = 0)
66+
utils::capture.output({
67+
mode <- mod$optimize(data = data_list, jacobian = TRUE, seed = 100, refresh = 0)
68+
fit1 <- mod$laplace(data = data_list, mode = mode, seed = 100, refresh = 0)
69+
fit2 <- mod$laplace(data = data_list, mode = mode$output_files(), seed = 100, refresh = 0)
70+
fit3 <- mod$laplace(data = data_list, mode = NULL, seed = 100, refresh = 0)
71+
})
7072

7173
expect_is(fit1, "CmdStanLaplace")
7274
expect_is(fit2, "CmdStanLaplace")
@@ -85,17 +87,22 @@ test_that("laplace() all valid 'mode' inputs give same results", {
8587
})
8688

8789
test_that("laplace() allows choosing number of draws", {
88-
fit <- mod$laplace(data = data_list, draws = 10, refresh = 0)
90+
utils::capture.output({
91+
fit <- mod$laplace(data = data_list, draws = 10, refresh = 0)
92+
fit2 <- mod$laplace(data = data_list, draws = 100, refresh = 0)
93+
})
94+
8995
expect_equal(fit$metadata()$draws, 10)
9096
expect_equal(posterior::ndraws(fit$draws()), 10)
9197

92-
fit2 <- mod$laplace(data = data_list, draws = 100, refresh = 0)
9398
expect_equal(fit2$metadata()$draws, 100)
9499
expect_equal(posterior::ndraws(fit2$draws()), 100)
95100
})
96101

97102
test_that("laplace() errors if jacobian arg doesn't match what optimize used", {
98-
fit <- mod$optimize(data = data_list, jacobian = FALSE, refresh = 0)
103+
utils::capture.output(
104+
fit <- mod$optimize(data = data_list, jacobian = FALSE, refresh = 0)
105+
)
99106
expect_error(
100107
mod$laplace(data = data_list, mode = fit, jacobian = TRUE),
101108
"'jacobian' argument to optimize and laplace must match"
@@ -107,7 +114,9 @@ test_that("laplace() errors if jacobian arg doesn't match what optimize used", {
107114
})
108115

109116
test_that("laplace() errors with bad combinations of arguments", {
110-
fit <- mod$optimize(data = data_list, jacobian = TRUE, refresh = 0)
117+
utils::capture.output(
118+
fit <- mod$optimize(data = data_list, jacobian = TRUE, refresh = 0)
119+
)
111120
expect_error(
112121
mod$laplace(data = data_list, mode = mod, opt_args = list(iter = 10)),
113122
"Cannot specify both 'opt_args' and 'mode' arguments."

tests/testthat/test-model-methods.R

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ skip_if(os_is_wsl())
44
set_cmdstan_path()
55
mod <- cmdstan_model(testing_stan_file("bernoulli_log_lik"), force_recompile = TRUE)
66
data_list <- testing_data("bernoulli")
7-
fit <- mod$sample(data = data_list, chains = 1, refresh = 0)
7+
utils::capture.output(
8+
fit <- mod$sample(data = data_list, chains = 1, refresh = 0)
9+
)
810

911
test_that("Model methods automatically initialise when needed", {
1012
expect_no_error(fit$log_prob(unconstrained_variables=c(0.1)))
@@ -59,7 +61,9 @@ test_that("Model methods environments are independent", {
5961
data_list_2 <- data_list
6062
data_list_2$N <- 20
6163
data_list_2$y <- c(data_list$y, data_list$y)
62-
fit_2 <- mod$sample(data = data_list_2, chains = 1)
64+
utils::capture.output(
65+
fit_2 <- mod$sample(data = data_list_2, chains = 1)
66+
)
6367
fit_2$init_model_methods()
6468

6569
expect_equal(fit$log_prob(unconstrained_variables=c(0.1)), -8.6327599208828509347)
@@ -90,8 +94,10 @@ test_that("methods error for incorrect inputs", {
9094

9195
logistic_mod <- cmdstan_model(testing_stan_file("logistic"), force_recompile = TRUE)
9296
logistic_data_list <- testing_data("logistic")
93-
logistic_fit <- logistic_mod$sample(data = logistic_data_list, chains = 1)
94-
logistic_fit$init_model_methods(verbose = TRUE)
97+
utils::capture.output(
98+
logistic_fit <- logistic_mod$sample(data = logistic_data_list, chains = 1)
99+
)
100+
logistic_fit$init_model_methods()
95101

96102
expect_error(
97103
logistic_fit$unconstrain_variables(list(alpha = 0.5)),
@@ -104,7 +110,9 @@ test_that("Methods error with already-compiled model", {
104110
precompile_mod <- testing_model("bernoulli")
105111
mod <- testing_model("bernoulli")
106112
data_list <- testing_data("bernoulli")
107-
fit <- mod$sample(data = data_list, chains = 1)
113+
utils::capture.output(
114+
fit <- mod$sample(data = data_list, chains = 1)
115+
)
108116
expect_error(
109117
fit$init_model_methods(),
110118
"Model methods cannot be used with a pre-compiled Stan executable, the model must be compiled again",
@@ -116,7 +124,9 @@ test_that("Methods can be compiled with model", {
116124
mod <- cmdstan_model(testing_stan_file("bernoulli"),
117125
force_recompile = TRUE,
118126
compile_model_methods = TRUE)
119-
fit <- mod$sample(data = data_list, chains = 1)
127+
utils::capture.output(
128+
fit <- mod$sample(data = data_list, chains = 1)
129+
)
120130

121131
lp <- fit$log_prob(unconstrained_variables=c(0.6))
122132
expect_equal(lp, -10.649855405830624733)
@@ -156,7 +166,9 @@ test_that("unconstrain_variables correctly handles zero-length containers", {
156166
mod <- cmdstan_model(write_stan_file(model_code),
157167
force_recompile = TRUE,
158168
compile_model_methods = TRUE)
159-
fit <- mod$sample(data = list(N = 0), chains = 1)
169+
utils::capture.output(
170+
fit <- mod$sample(data = list(N = 0), chains = 1)
171+
)
160172
unconstrained <- fit$unconstrain_variables(variables = list(x = 5))
161173
expect_equal(unconstrained, 5)
162174
})
@@ -179,21 +191,23 @@ test_that("unconstrain_draws returns correct values", {
179191
mod <- cmdstan_model(write_stan_file(model_code),
180192
compile_model_methods = TRUE,
181193
force_recompile = TRUE)
182-
fit <- mod$sample(data = list(N = 0), chains = 2, save_warmup = TRUE)
183-
fit_no_warmup <- mod$sample(data = list(N = 0), chains = 2)
194+
utils::capture.output({
195+
fit <- mod$sample(data = list(N = 0), chains = 2, save_warmup = TRUE)
196+
fit_no_warmup <- mod$sample(data = list(N = 0), chains = 2)
197+
})
184198

185199
x_draws <- fit$draws(format = "draws_df")$x
186200
x_draws_warmup <- fit$draws(format = "draws_df", inc_warmup = TRUE)$x
187-
201+
188202
# Unconstrain all internal draws
189203
unconstrained_internal_draws <- fit$unconstrain_draws()
190204
unconstrained_internal_draws_warmup <- fit$unconstrain_draws(inc_warmup = TRUE)
191205
expect_equal(as.numeric(x_draws), as.numeric(unconstrained_internal_draws))
192206
expect_equal(as.numeric(x_draws_warmup), as.numeric(unconstrained_internal_draws_warmup))
193-
207+
194208
expect_error({unconstrained_internal_draws <- fit_no_warmup$unconstrain_draws(inc_warmup = TRUE)},
195209
"Warmup draws were requested from a fit object without them! Please rerun the model with save_warmup = TRUE.")
196-
210+
197211
# Unconstrain external CmdStan CSV files
198212
unconstrained_csv <- fit$unconstrain_draws(files = fit$output_files())
199213
unconstrained_csv_warmup <- fit$unconstrain_draws(files = fit$output_files(),
@@ -204,7 +218,7 @@ test_that("unconstrain_draws returns correct values", {
204218
# Unconstrain existing draws object
205219
unconstrained_draws <- fit$unconstrain_draws(draws = fit$draws())
206220
expect_equal(as.numeric(x_draws), as.numeric(unconstrained_draws))
207-
221+
208222
expect_message(fit$unconstrain_draws(draws = fit$draws(), inc_warmup = TRUE),
209223
"'inc_warmup' cannot be used with a draws object. Ignoring.")
210224

@@ -224,7 +238,9 @@ test_that("unconstrain_draws returns correct values", {
224238
mod <- cmdstan_model(write_stan_file(model_code),
225239
compile_model_methods = TRUE,
226240
force_recompile = TRUE)
227-
fit <- mod$sample(data = list(N = 0), chains = 2)
241+
utils::capture.output(
242+
fit <- mod$sample(data = list(N = 0), chains = 2)
243+
)
228244

229245
x_draws <- fit$draws(format = "draws_df")$x
230246

@@ -241,10 +257,13 @@ test_that("unconstrain_draws returns correct values", {
241257
})
242258

243259
test_that("Model methods can be initialised for models with no data", {
244-
245260
stan_file <- write_stan_file("parameters { real x; } model { x ~ std_normal(); }")
246261
mod <- cmdstan_model(stan_file, compile_model_methods = TRUE, force_recompile = TRUE)
247-
expect_no_error(fit <- mod$sample())
262+
expect_no_error(
263+
utils::capture.output(
264+
fit <- mod$sample()
265+
)
266+
)
248267
expect_equal(fit$log_prob(5), -12.5)
249268
})
250269

@@ -268,8 +287,10 @@ test_that("Variable skeleton returns correct dimensions for matrices", {
268287
force_recompile = TRUE)
269288
N <- 4
270289
K <- 3
271-
fit <- mod$sample(data = list(N = N, K = K), chains = 1,
272-
iter_warmup = 1, iter_sampling = 5)
290+
utils::capture.output(
291+
fit <- mod$sample(data = list(N = N, K = K), chains = 1,
292+
iter_warmup = 1, iter_sampling = 5)
293+
)
273294

274295
target_skeleton <- list(
275296
x_real = array(0, dim = 1),

tests/testthat/test-model-optimize.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,10 @@ test_that("optimize() method runs when the stan file is removed", {
146146
})
147147

148148
test_that("optimize() recognizes new jacobian argument", {
149-
fit <- mod$optimize(data = data_list, jacobian = FALSE)
149+
utils::capture.output({
150+
fit <- mod$optimize(data = data_list, jacobian = FALSE)
151+
fit2 <- mod$optimize(data = data_list, jacobian = TRUE)
152+
})
150153
expect_equal(fit$metadata()$jacobian, 0)
151-
152-
fit2 <- mod$optimize(data = data_list, jacobian = TRUE)
153154
expect_equal(fit2$metadata()$jacobian, 1)
154155
})

tests/testthat/test-model-sample.R

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,17 @@ test_that("Errors are suppressed with show_exceptions", {
350350
"
351351
errmod <- cmdstan_model(write_stan_file(errmodcode), force_recompile = TRUE)
352352

353-
expect_message(
354-
suppressWarnings(errmod$sample(data = list(y_mean = 1), chains = 1)),
355-
"Chain 1 Exception: vector[uni] assign: accessing element out of range",
356-
fixed = TRUE
357-
)
353+
expect_sample_output(
354+
expect_message(
355+
suppressWarnings(errmod$sample(data = list(y_mean = 1), chains = 1)),
356+
"Chain 1 Exception: vector[uni] assign: accessing element out of range",
357+
fixed = TRUE
358+
))
358359

359-
expect_no_message(
360-
suppressWarnings(errmod$sample(data = list(y_mean = 1), chains = 1, show_exceptions = FALSE))
361-
)
360+
expect_sample_output(
361+
expect_no_message(
362+
suppressWarnings(errmod$sample(data = list(y_mean = 1), chains = 1, show_exceptions = FALSE))
363+
))
362364
})
363365

364366
test_that("All output can be suppressed by show_messages", {

0 commit comments

Comments
 (0)