From c231fa53b3d62e17928b8fa221e690ecc2a94c41 Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Sat, 7 Nov 2026 00:12:10 +0100 Subject: [PATCH 1/9] Upgrade testthat to 3rd edition. --- DESCRIPTION | 1 + tests/testthat/test-as_draws.R | 30 ++++++----- tests/testthat/test-convergence.R | 4 +- tests/testthat/test-extract_variable_matrix.R | 4 +- tests/testthat/test-mutate_variables.R | 9 ++-- tests/testthat/test-pareto_smooth.R | 18 ++++--- tests/testthat/test-repair_draws.R | 22 ++++---- tests/testthat/test-resample_draws.R | 14 ++--- tests/testthat/test-rvar-.R | 19 ++++--- tests/testthat/test-rvar-bind.R | 6 +-- tests/testthat/test-rvar-cast.R | 10 ++-- tests/testthat/test-rvar-factor.R | 14 +++-- tests/testthat/test-rvar-print.R | 2 +- .../test-rvar-summaries-within-draws.R | 54 +++++++++---------- tests/testthat/test-subset_draws.R | 27 +++++----- tests/testthat/test-thin_draws.R | 7 ++- tests/testthat/test-weight_draws.R | 7 ++- 17 files changed, 138 insertions(+), 110 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 068f9bcd..1c844cbc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -58,6 +58,7 @@ Encoding: UTF-8 LazyData: false URL: https://mc-stan.org/posterior/, https://discourse.mc-stan.org/ BugReports: https://github.com/stan-dev/posterior/issues +Config/testthat/edition: 3 Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.3 VignetteBuilder: knitr diff --git a/tests/testthat/test-as_draws.R b/tests/testthat/test-as_draws.R index b5d55791..44ccb225 100644 --- a/tests/testthat/test-as_draws.R +++ b/tests/testthat/test-as_draws.R @@ -58,9 +58,15 @@ test_that("transformations to and from draws_df objects work", { expect_equal(draws_df, draws_df2) # test that a single draw does not lead to a drop of dimensions - draws_rvars2 <- subset_draws(draws_rvars, draw = 1) + expect_message( + draws_rvars2 <- subset_draws(draws_rvars, draw = 1), + "Merging chains in order to subset via 'draw'" + ) draws_df2 <- as_draws_df(draws_rvars2) - expect_equal(subset_draws(draws_df, draw = 1), draws_df2) + expect_message( + expect_equal(subset_draws(draws_df, draw = 1), draws_df2), + "Merging chains in order to subset via 'draw'" + ) }) test_that("transformations to and from draws_list objects work", { @@ -109,7 +115,7 @@ test_that("matrices can be transformed to draws_matrix objects", { dimnames(x) <- list(NULL, paste0("theta", 1:5)) y <- as_draws(x) - expect_is(y, "draws_matrix") + expect_s3_class(y, "draws_matrix") expect_equal(variables(y), colnames(x)) expect_equal(niterations(y), 40) expect_equal(nchains(y), 1) @@ -121,7 +127,7 @@ test_that("arrays can be transformed to draws_array objects", { dimnames(x) <- list(NULL, NULL, paste0("theta", 1:5)) y <- as_draws(x) - expect_is(y, "draws_array") + expect_s3_class(y, "draws_array") expect_equal(variables(y), dimnames(x)[[3]]) expect_equal(niterations(y), 10) expect_equal(nchains(y), 4) @@ -135,7 +141,7 @@ test_that("lists of matrices can be transformed to draws_array objects", { # one chain z1 <- list(x) y <- as_draws(z1) - expect_is(y, "draws_array") + expect_s3_class(y, "draws_array") expect_equal(variables(y), colnames(z1[[1]])) expect_equal(niterations(y), 50) expect_equal(nchains(y), 1) @@ -143,7 +149,7 @@ test_that("lists of matrices can be transformed to draws_array objects", { # multiple chains z3 <- list(x, x, x) y <- as_draws(z3) - expect_is(y, "draws_array") + expect_s3_class(y, "draws_array") expect_equal(variables(y), colnames(z3[[1]])) expect_equal(niterations(y), 50) expect_equal(nchains(y), 3) @@ -155,7 +161,7 @@ test_that("data.frames can be transformed to draws_df objects", { v2 = rnorm(100) ) y <- as_draws(x) - expect_is(y, "draws_df") + expect_s3_class(y, "draws_df") expect_equal(variables(y), names(x)) expect_equal(niterations(y), 100) expect_equal(nchains(y), 1) @@ -165,7 +171,7 @@ test_that("data.frames can be transformed to draws_df objects", { x2$.iteration <- 1:100 x2$.chain <- rep(1:4, each = 25) y2 <- as_draws(x2) - expect_is(y2, "draws_df") + expect_s3_class(y2, "draws_df") expect_equal(variables(y2), names(x)) expect_equal(niterations(y2), 25) expect_equal(nchains(y2), 4) @@ -183,7 +189,7 @@ test_that("lists can be transformed to draws_list objects", { ) ) y <- as_draws(x) - expect_is(y, "draws_list") + expect_s3_class(y, "draws_list") expect_equal(variables(y), names(x[[1]])) expect_equal(ndraws(y), 100) expect_equal(nchains(y), 2) @@ -192,7 +198,7 @@ test_that("lists can be transformed to draws_list objects", { test_that("numeric vectors can be transformed to draws_matrix objects", { draws_matrix <- draws_matrix(a = 1:10, b = 11:20, c = 1) draws_matrix2 <- as_draws_matrix(cbind(1:10, 11:20, 1)) - expect_equivalent(draws_matrix, draws_matrix2) + expect_equal(draws_matrix, draws_matrix2, ignore_attr = TRUE) }) test_that("numeric vectors can be transformed to draws_array objects", { @@ -486,14 +492,14 @@ test_that("1-length rvars can be cast to other formats", { # scalars should stay as scalars... x <- rvar(1:10) expect_equal(as_draws_array(x), draws_array(x = 1:10)) - expect_equal(as_draws_df(x), draws_df(x = 1:10)) + expect_equal(as.data.frame(as_draws_df(x)), as.data.frame(draws_df(x = 1:10))) expect_equal(as_draws_list(x), draws_list(x = 1:10)) expect_equal(as_draws_matrix(x), draws_matrix(x = 1:10)) # ... but extra dims should not be dropped x <- rvar(array(1:10, dim = c(10, 1, 1))) expect_equal(as_draws_array(x), draws_array(`x[1,1]` = 1:10)) - expect_equal(as_draws_df(x), draws_df(`x[1,1]` = 1:10)) + expect_equal(as.data.frame(as_draws_df(x)), as.data.frame(draws_df(`x[1,1]` = 1:10))) expect_equal(as_draws_list(x), draws_list(`x[1,1]` = 1:10)) expect_equal(as_draws_matrix(x), draws_matrix(`x[1,1]` = 1:10)) }) diff --git a/tests/testthat/test-convergence.R b/tests/testthat/test-convergence.R index e6d806c8..0a34c02d 100644 --- a/tests/testthat/test-convergence.R +++ b/tests/testthat/test-convergence.R @@ -91,10 +91,10 @@ test_that("mcse diagnostics return reasonable values", { # check if issue #252 remains fixed se <- mcse_quantile(tau, c(0.999999, 1)) - expect_equivalent(se[1], se[2]) + expect_equal(se[1], se[2], ignore_attr = TRUE) se <- mcse_quantile(tau, c(0.000001, 0)) - expect_equivalent(se[1], se[2]) + expect_equal(se[1], se[2], ignore_attr = TRUE) }) test_that("convergence diagnostics accept vectors as input", { diff --git a/tests/testthat/test-extract_variable_matrix.R b/tests/testthat/test-extract_variable_matrix.R index be7f6bbe..1ec29633 100644 --- a/tests/testthat/test-extract_variable_matrix.R +++ b/tests/testthat/test-extract_variable_matrix.R @@ -45,6 +45,6 @@ test_that("extract_variable_matrix default method works", { # it should convert matrix to draws object x <- matrix(1:20, nrow = 10, ncol = 2) colnames(x) <- c("A", "B") - expect_equivalent(extract_variable_matrix(x, "A"), x[, 1, drop=FALSE]) - expect_equivalent(extract_variable_matrix(x, "B"), x[, 2, drop=FALSE]) + expect_equal(extract_variable_matrix(x, "A"), x[, 1, drop=FALSE], ignore_attr = TRUE) + expect_equal(extract_variable_matrix(x, "B"), x[, 2, drop=FALSE], ignore_attr = TRUE) }) diff --git a/tests/testthat/test-mutate_variables.R b/tests/testthat/test-mutate_variables.R index a6a57997..a96763a5 100644 --- a/tests/testthat/test-mutate_variables.R +++ b/tests/testthat/test-mutate_variables.R @@ -46,20 +46,20 @@ test_that("mutate_variables works correctly for draws_list objects", { test_that("mutate_variables works correctly for draws_matrix objects", { x <- as_draws_matrix(example_draws()) x <- mutate_variables(x, tau2 = tau^2) - expect_equal(x[, "tau2"], x[, "tau"]^2, check.attributes = FALSE) + expect_equal(x[, "tau2"], x[, "tau"]^2, ignore_attr = TRUE) }) test_that("mutate_variables works correctly for draws_array objects", { x <- as_draws_array(example_draws()) x <- mutate_variables(x, tau2 = tau^2) - expect_equal(x[, , "tau2"], x[, , "tau"]^2, check.attributes = FALSE) + expect_equal(x[, , "tau2"], x[, , "tau"]^2, ignore_attr = TRUE) }) test_that("mutate_variables works correctly for draws_rvars objects", { x <- as_draws_rvars(example_draws()) x <- mutate_variables(x, tau_sq = tau^2, theta_2_sq = theta[[2]]^2) - expect_equal(x$tau_sq, x$tau^2, check.attributes = FALSE) - expect_equal(x$theta_2_sq, x$theta[[2]]^2, check.attributes = FALSE) + expect_equal(x$tau_sq, x$tau^2, ignore_attr = TRUE) + expect_equal(x$theta_2_sq, x$theta[[2]]^2, ignore_attr = TRUE) }) test_that("mutate_variables drops variables on assigning NULL", { @@ -67,4 +67,3 @@ test_that("mutate_variables drops variables on assigning NULL", { x <- mutate_variables(x, mu = NULL, tau = NULL) expect_equal(variables(x), paste0("theta[", 1:8, "]")) }) - diff --git a/tests/testthat/test-pareto_smooth.R b/tests/testthat/test-pareto_smooth.R index 78cb3500..d3d0b6db 100644 --- a/tests/testthat/test-pareto_smooth.R +++ b/tests/testthat/test-pareto_smooth.R @@ -17,7 +17,7 @@ test_that("pareto_khat handles both tails being constant correctly", { # left and right tails are constant, so khat should be NA in all cases x <- c(rep(-100, 10), sort(rnorm(100)), rep(100, 10)) - + expect_true(is.na(pareto_khat(x, tail = "left", ndraws_tail = 10))) expect_true(is.na(pareto_khat(x, tail = "right", ndraws_tail = 10))) expect_true(is.na(pareto_khat(x, tail = "both", ndraws_tail = 10))) @@ -64,13 +64,13 @@ test_that("pareto_khat handles ndraws_tail argument", { expect_warning(pareto_khat(tau, tail = "both", ndraws_tail = 4), "Number of tail draws cannot be less than 5. Changing to 5.") - + expect_warning(pareto_khat(tau, tail = "right", ndraws_tail = 4), "Number of tail draws cannot be less than 5. Changing to 5.") - + expect_warning(pareto_khat(tau, tail = "left", ndraws_tail = 4), "Number of tail draws cannot be less than 5. Changing to 5.") - + expect_warning(pareto_khat(tau, tail = "right", ndraws_tail = length(tau)), "Tail draws (400) not strictly less than total draws (400). Fitting of generalized Pareto distribution not performed.", fixed = TRUE) @@ -173,11 +173,12 @@ test_that("pareto_khat functions work with matrix with chains", { expect_equal(pareto_khat(tau_chains, ndraws_tail = 20), pareto_khat(tau_nochains, ndraws_tail = 20)) + suppressMessages({ ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, return_k = TRUE) ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, return_k = TRUE) + }) expect_equal(as.numeric(ps_chains$x), as.numeric(ps_nochains$x)) - }) test_that("pareto_khat functions work with rvars with and without chains", { @@ -201,22 +202,25 @@ test_that("pareto_khat functions work with rvars with and without chains", { expect_equal(pareto_diags(tau_rvar_chains, ndraws_tail = 20), pareto_diags(tau_rvar_nochains, ndraws_tail = 20)) + suppressMessages({ ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, return_k = TRUE) ps_rvar_chains <- pareto_smooth(tau_rvar_chains, ndraws_tail = 20, return_k = TRUE) ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, return_k = TRUE) ps_rvar_nochains <- pareto_smooth(tau_rvar_nochains, ndraws_tail = 20, return_k = TRUE) + }) expect_equal(ps_rvar_chains$x, rvar(ps_chains$x, with_chains = TRUE)) expect_equal(ps_rvar_nochains$x, rvar(ps_nochains$x)) - + suppressMessages({ ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE) ps_rvar_chains <- pareto_smooth(tau_rvar_chains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE) ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE) ps_rvar_nochains <- pareto_smooth(tau_rvar_nochains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE) + }) expect_equal(ps_rvar_chains$x, rvar(ps_chains$x, with_chains = TRUE)) @@ -227,11 +231,13 @@ test_that("pareto_khat functions work with rvars with and without chains", { test_that("pareto_smooth returns x with smoothed tail(s)", { mu <- extract_variable_matrix(example_draws(), "mu") + suppressMessages({ mu_smoothed_right <- pareto_smooth(mu, ndraws_tail = 10, tail = "right", return_k = TRUE)$x mu_smoothed_left <- pareto_smooth(mu, ndraws_tail = 10, tail = "left", return_k = TRUE)$x mu_smoothed_both <- pareto_smooth(mu, ndraws_tail = 10, tail = "both", return_k = TRUE)$x + }) expect_equal(sort(mu)[1:390], sort(mu_smoothed_right)[1:390]) expect_equal(sort(mu_smoothed_both)[11:400], sort(mu_smoothed_right)[11:400]) diff --git a/tests/testthat/test-repair_draws.R b/tests/testthat/test-repair_draws.R index d35046e1..85401ece 100644 --- a/tests/testthat/test-repair_draws.R +++ b/tests/testthat/test-repair_draws.R @@ -3,11 +3,11 @@ test_that("repair_draws works correctly on draws_matrix objects", { x <- x[10:6, ] x_rep <- repair_draws(x, order = TRUE) - expect_equal(x[5:1, ], x_rep, check.attributes = FALSE) + expect_equal(x[5:1, ], x_rep, ignore_attr = TRUE) expect_equal(rownames(x_rep), as.character(1:5)) x_rep <- repair_draws(x, order = FALSE) - expect_equal(x, x_rep, check.attributes = FALSE) + expect_equal(x, x_rep, ignore_attr = TRUE) expect_equal(rownames(x_rep), as.character(1:5)) }) @@ -16,12 +16,12 @@ test_that("repair_draws works correctly on draws_array objects", { x <- x[10:6, c(4, 1), ] x_rep <- repair_draws(x, order = TRUE) - expect_equal(x[5:1, 2:1, ], x_rep, check.attributes = FALSE) + expect_equal(x[5:1, 2:1, ], x_rep, ignore_attr = TRUE) expect_equal(rownames(x_rep), as.character(1:5)) expect_equal(colnames(x_rep), as.character(1:2)) x_rep <- repair_draws(x, order = FALSE) - expect_equal(x, x_rep, check.attributes = FALSE) + expect_equal(x, x_rep, ignore_attr = TRUE) expect_equal(rownames(x_rep), as.character(1:5)) expect_equal(colnames(x_rep), as.character(1:2)) }) @@ -37,7 +37,7 @@ test_that("repair_draws works correctly on draws_df objects", { expect_equal(x_rep$.chain, 1:4) x_rep <- repair_draws(x, order = FALSE) - expect_equal(x$mu, x_rep$mu, check.attributes = FALSE) + expect_equal(x$mu, x_rep$mu, ignore_attr = TRUE) expect_equal(x_rep$.iteration, rep(1, 4)) expect_equal(x_rep$.chain, 4:1) }) @@ -47,11 +47,11 @@ test_that("repair_draws works correctly on draws_list objects", { x <- x[c(4, 2)] x_rep <- repair_draws(x, order = TRUE) - expect_equal(x[2], x_rep[1], check.attributes = FALSE) + expect_equal(x[2], x_rep[1], ignore_attr = TRUE) expect_equal(names(x_rep), as.character(1:2)) x_rep <- repair_draws(x, order = FALSE) - expect_equal(x, x_rep, check.attributes = FALSE) + expect_equal(x, x_rep, ignore_attr = TRUE) expect_equal(names(x_rep), as.character(1:2)) }) @@ -65,13 +65,13 @@ test_that("repair_draws works correctly on draws_rvars objects", { expect_equal( draws_of(x_rep$mu), draws_of(x$mu)[order(as.integer(rownames(draws_of(x$mu)))),], - check.attributes = FALSE + ignore_attr = TRUE ) expect_equal(niterations(x_rep), 4) expect_equal(nchains(x_rep), 1) x_rep <- repair_draws(x, order = FALSE) - expect_equal(draws_of(x$mu), draws_of(x_rep$mu), check.attributes = FALSE) + expect_equal(draws_of(x$mu), draws_of(x_rep$mu), ignore_attr = TRUE) expect_equal(niterations(x_rep), 4) expect_equal(nchains(x_rep), 1) @@ -79,11 +79,11 @@ test_that("repair_draws works correctly on draws_rvars objects", { expect_equal( draws_of(x_ord$mu), draws_of(x$mu)[order(as.integer(rownames(draws_of(x$mu)))),], - check.attributes = FALSE + ignore_attr = TRUE ) expect_equal( draws_of(x_ord$theta), draws_of(x$theta)[order(as.integer(rownames(draws_of(x$theta)))),], - check.attributes = FALSE + ignore_attr = TRUE ) }) diff --git a/tests/testthat/test-resample_draws.R b/tests/testthat/test-resample_draws.R index bea49809..d108a420 100644 --- a/tests/testthat/test-resample_draws.R +++ b/tests/testthat/test-resample_draws.R @@ -3,22 +3,22 @@ test_that("resample_draws returns expected format", { w <- runif(ndraws(x), 0, 10) x <- as_draws_matrix(x) - x_rs <- resample_draws(x, weights = w, method = "stratified") + expect_message(x_rs <- resample_draws(x, weights = w, method = "stratified")) expect_true(is_draws_matrix(x_rs)) expect_equal(ndraws(x_rs), ndraws(x)) x <- as_draws_array(x) - x_rs <- resample_draws(x, weights = w, method = "deterministic", ndraws = 200) + expect_message(x_rs <- resample_draws(x, weights = w, method = "deterministic", ndraws = 200)) expect_true(is_draws_array(x_rs)) expect_equal(ndraws(x_rs), 200) x <- as_draws_df(x) - x_rs <- resample_draws(x, weights = w, method = "simple") + expect_message(x_rs <- resample_draws(x, weights = w, method = "simple")) expect_true(is_draws_df(x_rs)) expect_equal(ndraws(x_rs), ndraws(x)) x <- as_draws_list(x) - x_rs <- resample_draws(x, w, method = "simple_no_replace", ndraws = 100) + expect_message(x_rs <- resample_draws(x, w, method = "simple_no_replace", ndraws = 100)) expect_true(is_draws_list(x_rs)) expect_equal(ndraws(x_rs), 100) expect_error( @@ -27,7 +27,7 @@ test_that("resample_draws returns expected format", { ) x <- as_draws_rvars(x) - x_rs <- resample_draws(x, weights = w, method = "simple") + expect_message(x_rs <- resample_draws(x, weights = w, method = "simple")) expect_true(is_draws_rvars(x_rs)) expect_equal(ndraws(x_rs), ndraws(x)) }) @@ -56,13 +56,13 @@ test_that("Resampling algorithms return the correct result in expectation", { test_that("resample_draws uses stored weights when available", { x <- example_draws() - x_rs <- resample_draws(x) + expect_message(x_rs <- resample_draws(x)) expect_true(is_draws_array(x_rs)) expect_equal(ndraws(x_rs), ndraws(x)) w <- runif(ndraws(x), 0, 10) x <- weight_draws(x, w) - x_rs <- resample_draws(x) + expect_message(x_rs <- resample_draws(x)) expect_true(is_draws_array(x_rs)) expect_equal(ndraws(x_rs), ndraws(x)) # .log_weight variable has been dropped diff --git a/tests/testthat/test-rvar-.R b/tests/testthat/test-rvar-.R index 2d674b41..a7eb564a 100755 --- a/tests/testthat/test-rvar-.R +++ b/tests/testthat/test-rvar-.R @@ -159,13 +159,15 @@ test_that("rvars work in tibbles", { df = tibble::tibble(x, y = x + 1) expect_equal(df$x, x) - expect_equal(df$y, rvar(x_array + 1)) + expect_equal(df$y, rvar(x_array + 1), ignore_attr = TRUE) expect_equal(dplyr::mutate(df, z = x)$z, x) - expect_equal(dplyr::mutate(df, z = x * 2)$z, rvar(x_array * 2)) + expect_equal(dplyr::mutate(df, z = x * 2)$z, rvar(x_array * 2), + ignore_attr = TRUE) expect_equal( dplyr::mutate(dplyr::group_by(df, 1:4), z = x * 2)$z, - rvar(x_array * 2) + rvar(x_array * 2), + ignore_attr = TRUE ) df = tibble::tibble(g = letters[1:4], x) @@ -175,7 +177,8 @@ test_that("rvars work in tibbles", { c = rvar(x_array[,3, drop = FALSE]), d = rvar(x_array[,4, drop = FALSE]) ) - expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref) + expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref, + ignore_attr = TRUE) expect_equal(tidyr::pivot_longer(ref, a:d, names_to = "g", values_to = "x"), df) df$y = df$x + 1 @@ -186,13 +189,15 @@ test_that("rvars work in tibbles", { c = c(rvar(NA), NA, df$x[[3]], NA), d = c(rvar(NA), NA, NA, df$x[[4]]), ) - expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref2) + expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref2, + ignore_attr = TRUE) expect_equal(dplyr::first(df$x), x[1]) z = cbind(a = x, b = x + 1) df$z = z - expect_equal(dplyr::mutate(dplyr::rowwise(df), w = z[,"b"])$w, z[,"b"]) + expect_equal(dplyr::mutate(dplyr::rowwise(df), w = z[,"b"])$w, z[,"b"], + ignore_attr = TRUE) }) @@ -240,7 +245,7 @@ test_that("warnings for unequal draws/chains are correct", { # Store and restore option to ensure test isolation old_option <- getOption("posterior.warn_on_merge_chains") on.exit(options(posterior.warn_on_merge_chains = old_option)) - + options(posterior.warn_on_merge_chains = TRUE) expect_warning( expect_equal(rvar(1:10) + rvar(1:10, nchains = 2), rvar(1:10 + 1:10)), diff --git a/tests/testthat/test-rvar-bind.R b/tests/testthat/test-rvar-bind.R index 6f580ed5..d002811e 100755 --- a/tests/testthat/test-rvar-bind.R +++ b/tests/testthat/test-rvar-bind.R @@ -30,7 +30,7 @@ test_that("c works on rvar", { x_col <- x dim(x_col) <- c(3,1) - expect_equal(c(x_col), x) + expect_equal(c(x_col), x, ignore_attr = TRUE) expect_equal(c(x_col, y), x_y) }) @@ -77,7 +77,7 @@ test_that("c works on rvar_factor", { x_col <- x dim(x_col) <- c(3,1) - expect_equal(c(x_col), x) + expect_equal(c(x_col), x, ignore_attr = TRUE) expect_equal(c(x_col, y), x_y) }) @@ -139,7 +139,7 @@ test_that("c works on rvar_ordered", { x_col <- x dim(x_col) <- c(3,1) - expect_equal(c(x_col), x) + expect_equal(c(x_col), x, ignore_attr = TRUE) expect_equal(c(x_col, y), x_y) }) diff --git a/tests/testthat/test-rvar-cast.R b/tests/testthat/test-rvar-cast.R index 384cb05e..42c3ca6c 100755 --- a/tests/testthat/test-rvar-cast.R +++ b/tests/testthat/test-rvar-cast.R @@ -139,7 +139,9 @@ test_that("as_rvar_ordered works", { ) expect_equal( draws_of(as_rvar_ordered(as_rvar_factor(array(1:4, dim = c(2,2))))), - structure(1:4L, levels = c("1", "2", "3", "4"), dim = c(1, 2, 2), dimnames = list("1", NULL, NULL), class = c("ordered", "factor")) + structure(1:4L, levels = c("1", "2", "3", "4"), dim = c(1, 2, 2), dimnames = list("1", NULL, NULL), class = c("ordered", "factor"), + ignore_attr = TRUE), + ignore_attr = TRUE ) expect_equal( draws_of(as_rvar_ordered(array(c(TRUE, TRUE, FALSE, FALSE), dim = c(2,2)))), @@ -343,9 +345,9 @@ test_that("proxy restore works", { dimnames = list(A = paste0("a", 1:2), B = paste0("b", 1:2), C = paste0("c", 1:4)) ) - expect_equal(vec_restore(vec_proxy(unname(x1)), rvar()), unname(x1)) - expect_equal(vec_restore(vec_proxy(unname(x2)), rvar()), unname(x2)) - expect_equal(vec_restore(vec_proxy(unname(x3)), rvar()), unname(x3)) + expect_equal(vec_restore(vec_proxy(unname(x1)), rvar()), unname(x1), ignore_attr = TRUE) + expect_equal(vec_restore(vec_proxy(unname(x2)), rvar()), unname(x2), ignore_attr = TRUE) + expect_equal(vec_restore(vec_proxy(unname(x3)), rvar()), unname(x3), ignore_attr = TRUE) expect_equal(vec_restore(vec_proxy(x1), rvar()), x1) expect_equal(vec_restore(vec_proxy(x2), rvar()), x2) expect_equal(vec_restore(vec_proxy(x3), rvar()), x3) diff --git a/tests/testthat/test-rvar-factor.R b/tests/testthat/test-rvar-factor.R index 055e3e13..6d47ad71 100644 --- a/tests/testthat/test-rvar-factor.R +++ b/tests/testthat/test-rvar-factor.R @@ -55,13 +55,15 @@ test_that("rvar_factors work in tibbles", { df = tibble::tibble(x, y = as_rvar_ordered(x)) expect_equal(df$x, x) - expect_equal(df$y, rvar(x_array_ordered)) + expect_equal(df$y, rvar(x_array_ordered), ignore_attr = TRUE) expect_equal(dplyr::mutate(df, z = x)$z, x) - expect_equal(dplyr::mutate(df, z = as_rvar_ordered(x))$z, rvar(x_array_ordered)) + expect_equal(dplyr::mutate(df, z = as_rvar_ordered(x))$z, rvar(x_array_ordered), + ignore_attr = TRUE) expect_equal( dplyr::group_by(dplyr::mutate(df, z = as_rvar_ordered(x)), 1:4)$z, - rvar(x_array_ordered) + rvar(x_array_ordered), + ignore_attr = TRUE ) df = tibble::tibble(g = letters[1:4], x) @@ -71,7 +73,8 @@ test_that("rvar_factors work in tibbles", { c = rvar(x_array[,3, drop = FALSE]), d = rvar(x_array[,4, drop = FALSE]) ) - expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref) + expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref, + ignore_attr = TRUE) expect_equal(tidyr::pivot_longer(ref, a:d, names_to = "g", values_to = "x"), df) df$y = as_rvar_ordered(df$x) @@ -82,7 +85,8 @@ test_that("rvar_factors work in tibbles", { c = c(rvar(factor(NA)), NA, df$x[[3]], NA), d = c(rvar(factor(NA)), NA, NA, df$x[[4]]), ) - expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref2) + expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref2, + ignore_attr = TRUE) }) # rep --------------------------------------------------------------------- diff --git a/tests/testthat/test-rvar-print.R b/tests/testthat/test-rvar-print.R index 32cc66bb..cbf5d036 100755 --- a/tests/testthat/test-rvar-print.R +++ b/tests/testthat/test-rvar-print.R @@ -212,7 +212,7 @@ test_that("glimpse on rvar works", { expect_equal( format_glimpse(x_vec), c("3.5 ± 1.9", "9.5 ± 1.9", "15.5 ± 1.9", "21.5 ± 1.9"), - check.attributes = FALSE + ignore_attr = TRUE ) expect_equal(format_glimpse(x_matrix), "") }) diff --git a/tests/testthat/test-rvar-summaries-within-draws.R b/tests/testthat/test-rvar-summaries-within-draws.R index 50ad4faf..7ba9fb93 100755 --- a/tests/testthat/test-rvar-summaries-within-draws.R +++ b/tests/testthat/test-rvar-summaries-within-draws.R @@ -7,12 +7,12 @@ test_that("numeric summary functions work", { x_ord <- rvar_ordered(x_letters, levels = letters) x_fct <- rvar_factor(x_letters, levels = letters) - expect_equal(draws_of(rvar_mean(x)), apply(x_array, 1, mean), check.attributes = FALSE) - expect_equal(draws_of(rvar_median(x)), apply(x_array, 1, median), check.attributes = FALSE) - expect_equal(draws_of(rvar_sum(x)), apply(x_array, 1, sum), check.attributes = FALSE) - expect_equal(draws_of(rvar_prod(x)), apply(x_array, 1, prod), check.attributes = FALSE) - expect_equal(draws_of(rvar_min(x)), apply(x_array, 1, min), check.attributes = FALSE) - expect_equal(draws_of(rvar_max(x)), apply(x_array, 1, max), check.attributes = FALSE) + expect_equal(draws_of(rvar_mean(x)), apply(x_array, 1, mean), ignore_attr = TRUE) + expect_equal(draws_of(rvar_median(x)), apply(x_array, 1, median), ignore_attr = TRUE) + expect_equal(draws_of(rvar_sum(x)), apply(x_array, 1, sum), ignore_attr = TRUE) + expect_equal(draws_of(rvar_prod(x)), apply(x_array, 1, prod), ignore_attr = TRUE) + expect_equal(draws_of(rvar_min(x)), apply(x_array, 1, min), ignore_attr = TRUE) + expect_equal(draws_of(rvar_max(x)), apply(x_array, 1, max), ignore_attr = TRUE) expect_error(rvar_mean(x_ord)) expect_equal(rvar_median(x_ord), rvar_ordered(letters[apply(x_array, 1, median)], levels = letters)) @@ -41,7 +41,7 @@ test_that("numeric summary functions work", { expect_equal( draws_of(rvar_mean(x, na.rm = TRUE)), apply(draws_of(x), 1, function(x) mean(x, na.rm = TRUE)), - check.attributes = FALSE + ignore_attr = TRUE ) }) @@ -55,10 +55,10 @@ test_that("spread summary functions work", { x_ord <- rvar_ordered(x_letters, levels = letters) x_fct <- rvar_factor(x_letters, levels = letters) - expect_equal(draws_of(rvar_sd(x)), apply(x_array, 1, sd), check.attributes = FALSE) - expect_equal(draws_of(rvar_var(x)), apply(x_array, 1, function(x) var(as.vector(x))), check.attributes = FALSE) - expect_equal(draws_of(rvar_mad(x)), apply(x_array, 1, mad), check.attributes = FALSE) - expect_equal(draws_of(rvar_mad(x, constant = 1)), apply(x_array, 1, mad, constant = 1), check.attributes = FALSE) + expect_equal(draws_of(rvar_sd(x)), apply(x_array, 1, sd), ignore_attr = TRUE) + expect_equal(draws_of(rvar_var(x)), apply(x_array, 1, function(x) var(as.vector(x))), ignore_attr = TRUE) + expect_equal(draws_of(rvar_mad(x)), apply(x_array, 1, mad), ignore_attr = TRUE) + expect_equal(draws_of(rvar_mad(x, constant = 1)), apply(x_array, 1, mad, constant = 1), ignore_attr = TRUE) expect_error(rvar_sd(x_ord)) expect_error(rvar_var(x_ord)) @@ -78,7 +78,7 @@ test_that("spread summary functions work", { expect_equal( draws_of(rvar_var(x, na.rm = TRUE)), apply(draws_of(x), 1, function(x) var(as.vector(x), na.rm = TRUE)), - check.attributes = FALSE + ignore_attr = TRUE ) }) @@ -91,7 +91,7 @@ test_that("rvar_range works", { x_letters <- array(letters[1:24], dim = c(4,2,3)) x_ord <- rvar_ordered(x_letters, levels = letters) - expect_equal(draws_of(rvar_range(x)), t(apply(x_array, 1, range)), check.attributes = FALSE) + expect_equal(draws_of(rvar_range(x)), t(apply(x_array, 1, range)), ignore_attr = TRUE) expect_equal(rvar_range(x_ord), rvar_ordered(array(letters[t(apply(x_array, 1, range))], dim = c(4, 2)), levels = letters)) expect_error(rvar_range(rvar_factor("a"))) @@ -137,8 +137,8 @@ test_that("logical summaries work", { x_array <- array(1:24, dim = c(4,2,3)) x <- new_rvar(x_array) - expect_equal(draws_of(rvar_all(x > 6)), as.matrix(apply(x_array > 6, 1, all)), check.attributes = FALSE) - expect_equal(draws_of(rvar_any(x > 6)), as.matrix(apply(x_array > 6, 1, any)), check.attributes = FALSE) + expect_equal(draws_of(rvar_all(x > 6)), as.matrix(apply(x_array > 6, 1, all)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_any(x > 6)), as.matrix(apply(x_array > 6, 1, any)), ignore_attr = TRUE) expect_error(rvar_all(rvar("a"))) expect_error(rvar_any(rvar("a"))) @@ -158,18 +158,18 @@ test_that("special value predicates work", { x_ord <- rvar_ordered(x_letters) x_fct <- rvar_factor(x_letters) - expect_equal(draws_of(rvar_is_finite(x)), as.matrix(is.finite(x_array)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_infinite(x)), as.matrix(is.infinite(x_array)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_nan(x)), as.matrix(is.nan(x_array)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_na(x)), as.matrix(is.na(x_array)), check.attributes = FALSE) + expect_equal(draws_of(rvar_is_finite(x)), as.matrix(is.finite(x_array)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_is_infinite(x)), as.matrix(is.infinite(x_array)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_is_nan(x)), as.matrix(is.nan(x_array)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_is_na(x)), as.matrix(is.na(x_array)), ignore_attr = TRUE) - expect_equal(draws_of(rvar_is_finite(x_ord)), as.matrix(is.finite(x_letters)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_infinite(x_ord)), as.matrix(is.infinite(x_letters)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_nan(x_ord)), as.matrix(is.nan(x_letters)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_na(x_ord)), as.matrix(is.na(x_letters)), check.attributes = FALSE) + expect_equal(draws_of(rvar_is_finite(x_ord)), as.matrix(is.finite(x_letters)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_is_infinite(x_ord)), as.matrix(is.infinite(x_letters)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_is_nan(x_ord)), as.matrix(is.nan(x_letters)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_is_na(x_ord)), as.matrix(is.na(x_letters)), ignore_attr = TRUE) - expect_equal(draws_of(rvar_is_finite(x_fct)), as.matrix(is.finite(x_letters)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_infinite(x_fct)), as.matrix(is.infinite(x_letters)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_nan(x_fct)), as.matrix(is.nan(x_letters)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_na(x_fct)), as.matrix(is.na(x_letters)), check.attributes = FALSE) + expect_equal(draws_of(rvar_is_finite(x_fct)), as.matrix(is.finite(x_letters)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_is_infinite(x_fct)), as.matrix(is.infinite(x_letters)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_is_nan(x_fct)), as.matrix(is.nan(x_letters)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_is_na(x_fct)), as.matrix(is.na(x_letters)), ignore_attr = TRUE) }) diff --git a/tests/testthat/test-subset_draws.R b/tests/testthat/test-subset_draws.R index fa2d5235..f6e5adc3 100644 --- a/tests/testthat/test-subset_draws.R +++ b/tests/testthat/test-subset_draws.R @@ -3,12 +3,12 @@ test_that("subset_draws works correctly for draws_matrix objects", { x <- as_draws_matrix(example_draws()) x_sub <- subset_draws(x, variable = c("mu", "tau"), iteration = 5:10) x_sub2 <- x[c(5:10, 105:110, 205:210, 305:310), c("mu", "tau")] - expect_equal(x_sub, x_sub2, check.attributes = FALSE) + expect_equal(x_sub, x_sub2, ignore_attr = TRUE) expect_equal(iteration_ids(x_sub), 1:6) - x_sub <- subset_draws(x, draw = c(2, 2, 4, 4), unique = FALSE) + expect_message(x_sub <- subset_draws(x, draw = c(2, 2, 4, 4), unique = FALSE)) expect_equal(niterations(x_sub), 4) - expect_equivalent(x_sub[1, ], x_sub[2, ]) + expect_equal(x_sub[1, ], x_sub[2, ], ignore_attr = TRUE) x <- weight_draws(x, rep(1, ndraws(x))) x_sub <- subset_draws(x, variable = "mu") @@ -18,7 +18,7 @@ test_that("subset_draws works correctly for draws_matrix objects", { expect_equal(setdiff(variables(x, reserved = TRUE), "mu"), variables(x_sub, reserved = TRUE)) expect_equal(nchains(x_sub), 1) - x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE) + expect_message(x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE)) expect_equal(ndraws(x) - 3, ndraws(x_sub)) x_sub <- subset_draws(x, iteration = c(1, 2, 3), exclude = TRUE) @@ -38,13 +38,13 @@ test_that("subset_draws works correctly for draws_array objects", { x <- as_draws_array(example_draws()) x_sub <- subset_draws(x, variable = c("mu", "tau"), iteration = 5:10, chain = 3:4) - expect_equal(x[5:10, 3:4, c("mu", "tau")], x_sub, check.attributes = FALSE) + expect_equal(x[5:10, 3:4, c("mu", "tau")], x_sub, ignore_attr = TRUE) expect_equal(iteration_ids(x_sub), 1:6) expect_equal(chain_ids(x_sub), 1:2) x_sub <- subset_draws(x, chain = c(1, 1), unique = FALSE) expect_equal(nchains(x_sub), 2) - expect_equivalent(x_sub[, 1, ], x_sub[, 2, ]) + expect_equal(x_sub[, 1, ], x_sub[, 2, ], ignore_attr = TRUE) expect_message( x_sub <- subset_draws(x, draw = c(1, 200, 10)), @@ -60,7 +60,7 @@ test_that("subset_draws works correctly for draws_array objects", { expect_equal(setdiff(variables(x, reserved = TRUE), "mu"), variables(x_sub, reserved = TRUE)) expect_equal(nchains(x_sub), 1) - x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE) + expect_message(x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE)) expect_equal(ndraws(x) - 3, ndraws(x_sub)) x_sub <- subset_draws(x, iteration = c(1, 2, 3), exclude = TRUE) @@ -95,7 +95,7 @@ test_that("subset_draws works correctly for draws_df objects", { expect_equal(iteration_ids(x_sub), 1:6) expect_equal(chain_ids(x_sub), 1:2) - x_sub <- subset_draws(x, draw = c(5, 5, 6), unique = FALSE) + expect_message(x_sub <- subset_draws(x, draw = c(5, 5, 6), unique = FALSE)) expect_equal(ndraws(x_sub), 3) expect_equal(x_sub$mu[1], x_sub$mu[2]) @@ -107,7 +107,7 @@ test_that("subset_draws works correctly for draws_df objects", { expect_equal(setdiff(variables(x, reserved = TRUE), "mu"), variables(x_sub, reserved = TRUE)) expect_equal(nchains(x_sub), 1) - x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE) + expect_message(x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE)) expect_equal(ndraws(x) - 3, ndraws(x_sub)) x_sub <- subset_draws(x, iteration = c(1, 2, 3), exclude = TRUE) @@ -143,7 +143,7 @@ test_that("subset_draws works correctly for draws_list objects", { expect_equal(setdiff(variables(x, reserved = TRUE), "mu"), variables(x_sub, reserved = TRUE)) expect_equal(nchains(x_sub), 1) - x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE) + expect_message(x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE)) expect_equal(ndraws(x) - 3, ndraws(x_sub)) x_sub <- subset_draws(x, iteration = c(1, 2, 3), exclude = TRUE) @@ -179,7 +179,7 @@ test_that("subset_draws works correctly for draws_rvars objects", { expect_equal(setdiff(variables(x, reserved = TRUE), "mu"), variables(x_sub, reserved = TRUE)) expect_equal(nchains(x_sub), 1) - x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE) + expect_message(x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE)) expect_equal(ndraws(x) - 3, ndraws(x_sub)) x_sub <- subset_draws(x, iteration = c(1, 2, 3), exclude = TRUE) @@ -210,12 +210,11 @@ test_that("subset_draws works correctly for rvar objects", { expect_equal(niterations(x_sub), niterations(x) - 2) expect_equal(nchains(x_sub), 1) - x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE) + expect_message(x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE)) expect_equal(ndraws(x) - 3, ndraws(x_sub)) x_sub <- subset_draws(x, iteration = c(1, 2, 3), exclude = TRUE) expect_equal(ndraws(x) - 3 * nchains(x), ndraws(x_sub)) - }) test_that("variables can be subsetted via regular expressions", { @@ -367,7 +366,7 @@ test_that("subset_draws respects posterior.warn_on_merge_chains option", { x_sub <- subset_draws(x, draw = c(1, 200, 10)) ) expect_equal(niterations(x_sub), 3) - + # Test with default behavior (should be TRUE, showing message) options(posterior.warn_on_merge_chains = NULL) x <- as_draws_array(example_draws()) diff --git a/tests/testthat/test-thin_draws.R b/tests/testthat/test-thin_draws.R index 52def0b1..add05072 100644 --- a/tests/testthat/test-thin_draws.R +++ b/tests/testthat/test-thin_draws.R @@ -27,7 +27,10 @@ test_that("automatic thinning works as expected", { thin_by <- niterations(mu) / mean( c(ess_mu_1, ess_mu_2, ess_mu_3, ess_mu_4)) - - expect_equal(thin_draws(mu), thin_draws(mu, thin = thin_by)) + expect_message( + thinned <- thin_draws(mu), + "Automatically thinned by 1.6 based on ESS" + ) + expect_equal(thinned, thin_draws(mu, thin = thin_by)) }) diff --git a/tests/testthat/test-weight_draws.R b/tests/testthat/test-weight_draws.R index d947ae9e..ef8f0341 100644 --- a/tests/testthat/test-weight_draws.R +++ b/tests/testthat/test-weight_draws.R @@ -176,7 +176,7 @@ test_that("conversion between formats preserves weights", { for (type in names(draws)) { expect_equal(as_draws_matrix(draws[[!!type]]), draws$matrix) expect_equal(as_draws_array(draws[[!!type]]), draws$array) - expect_equal(as_draws_df(draws[[!!type]]), draws$df) + expect_equal(as.data.frame(as_draws_df(draws[[!!type]])), as.data.frame(draws$df)) expect_equal(as_draws_list(draws[[!!type]]), draws$list) expect_equal(as_draws_rvars(draws[[!!type]]), draws$rvars) } @@ -188,6 +188,9 @@ test_that("pareto smoothing smooths weights in weight_draws", { x <- example_draws() lw <- sort(log(abs(rt(ndraws(x), 1)))) weighted <- weight_draws(x, lw, pareto_smooth = FALSE, log = TRUE) - smoothed <- weight_draws(x, lw, pareto_smooth = TRUE, log = TRUE) + expect_message( + smoothed <- weight_draws(x, lw, pareto_smooth = TRUE, log = TRUE), + "Pareto khat for weights is high" + ) expect_false(all(weights(weighted) == weights(smoothed))) }) From 5e5cc8d14ec1215d516770359b670e12839c4a40 Mon Sep 17 00:00:00 2001 From: VisruthSK Date: Fri, 6 Nov 2026 19:55:51 -0800 Subject: [PATCH 2/9] Parallelize --- .github/workflows/rcmdcheck.yml | 1 + DESCRIPTION | 2 + ...marise_draws-multicore-external-function.R | 15 ++++++ ...summarise_draws-multicore-many-variables.R | 17 +++++++ ...t-summarise_draws-multicore-one-variable.R | 17 +++++++ ...-summarise_draws-multicore-two-variables.R | 17 +++++++ tests/testthat/test-summarise_draws.R | 51 ------------------- 7 files changed, 69 insertions(+), 51 deletions(-) create mode 100644 tests/testthat/test-summarise_draws-multicore-external-function.R create mode 100644 tests/testthat/test-summarise_draws-multicore-many-variables.R create mode 100644 tests/testthat/test-summarise_draws-multicore-one-variable.R create mode 100644 tests/testthat/test-summarise_draws-multicore-two-variables.R diff --git a/.github/workflows/rcmdcheck.yml b/.github/workflows/rcmdcheck.yml index 6ae98c8b..bd4abc2d 100644 --- a/.github/workflows/rcmdcheck.yml +++ b/.github/workflows/rcmdcheck.yml @@ -34,6 +34,7 @@ jobs: env: R_REMOTES_NO_ERRORS_FROM_WARNINGS: true GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + TESTTHAT_CPUS: 4 steps: - uses: actions/checkout@v7 diff --git a/DESCRIPTION b/DESCRIPTION index 1c844cbc..173508e8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -59,6 +59,8 @@ LazyData: false URL: https://mc-stan.org/posterior/, https://discourse.mc-stan.org/ BugReports: https://github.com/stan-dev/posterior/issues Config/testthat/edition: 3 +Config/testthat/parallel: true +Config/testthat/start-first: summarise_draws-multicore-many-variables, summarise_draws-multicore-two-variables, summarise_draws-multicore-external-function, summarise_draws-multicore-one-variable, rstar Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.3 VignetteBuilder: knitr diff --git a/tests/testthat/test-summarise_draws-multicore-external-function.R b/tests/testthat/test-summarise_draws-multicore-external-function.R new file mode 100644 index 00000000..affd386a --- /dev/null +++ b/tests/testthat/test-summarise_draws-multicore-external-function.R @@ -0,0 +1,15 @@ +test_that("multicore summarise_draws finds externally defined summary functions", { + set.seed(1) + cores <- 2 + nc <- 4 + + n <- 20 + test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) + x <- as_draws_array(test_array) + dimnames(x)$variable[2] <- reserved_variables()[1] + + mean2 <- function(x) sum(x) / length(x) + sum_x <- summarise_draws(x, mean2) + parsum_x <- summarise_draws(x, mean2, .cores = cores) + expect_identical(sum_x, parsum_x) +}) diff --git a/tests/testthat/test-summarise_draws-multicore-many-variables.R b/tests/testthat/test-summarise_draws-multicore-many-variables.R new file mode 100644 index 00000000..b43a43f5 --- /dev/null +++ b/tests/testthat/test-summarise_draws-multicore-many-variables.R @@ -0,0 +1,17 @@ +test_that("multicore summarise_draws is identical to single-core summarise_draws", { + set.seed(1) + cores <- 2 + nc <- 4 + + n <- 20 + test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) + x <- as_draws_array(test_array) + sum_x <- summarise_draws(x) + parsum_x <- summarise_draws(x, .cores = cores) + expect_equal(sum_x, parsum_x) + + dimnames(x)$variable[2] <- reserved_variables()[1] + sum_x <- summarise_draws(x) + parsum_x <- summarise_draws(x, .cores = cores) + expect_equal(sum_x, parsum_x) +}) diff --git a/tests/testthat/test-summarise_draws-multicore-one-variable.R b/tests/testthat/test-summarise_draws-multicore-one-variable.R new file mode 100644 index 00000000..30c02938 --- /dev/null +++ b/tests/testthat/test-summarise_draws-multicore-one-variable.R @@ -0,0 +1,17 @@ +test_that("multicore summarise_draws is identical to single-core summarise_draws with 1 variable", { + set.seed(1) + cores <- 2 + nc <- 4 + + n <- 1 + test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) + x <- as_draws_array(test_array) + sum_x <- summarise_draws(x) + parsum_x <- summarise_draws(x, .cores = cores) + expect_identical(sum_x, parsum_x) + + dimnames(x)$variable[1] <- reserved_variables()[1] + suppressWarnings(sum_x <- summarise_draws(x)) + suppressWarnings(parsum_x <- summarise_draws(x, .cores = cores)) + expect_identical(sum_x, parsum_x) +}) diff --git a/tests/testthat/test-summarise_draws-multicore-two-variables.R b/tests/testthat/test-summarise_draws-multicore-two-variables.R new file mode 100644 index 00000000..be8ae54b --- /dev/null +++ b/tests/testthat/test-summarise_draws-multicore-two-variables.R @@ -0,0 +1,17 @@ +test_that("multicore summarise_draws is identical to single-core summarise_draws with 2 variables", { + set.seed(1) + cores <- 2 + nc <- 4 + + n <- 2 + test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) + x <- as_draws_array(test_array) + sum_x <- summarise_draws(x) + parsum_x <- summarise_draws(x, .cores = cores) + expect_identical(sum_x, parsum_x) + + dimnames(x)$variable[2] <- reserved_variables()[1] + sum_x <- summarise_draws(x) + parsum_x <- summarise_draws(x, .cores = cores) + expect_identical(sum_x, parsum_x) +}) diff --git a/tests/testthat/test-summarise_draws.R b/tests/testthat/test-summarise_draws.R index 80ed971e..27d1b6ed 100644 --- a/tests/testthat/test-summarise_draws.R +++ b/tests/testthat/test-summarise_draws.R @@ -69,57 +69,6 @@ test_that("summarise_draws warns if all variable names are reserved", { expect_warning(summarize_draws(x), "no variables with unreserved names") }) -test_that(paste( - "multicore summarise_draws is identical to single-core summarise_draws", - "including if some chunks contain no variables" -), { - set.seed(1) - cores <- 2 - nc <- 4 - - n <- 20 - test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) - x <- as_draws_array(test_array) - sum_x <- summarise_draws(x) - parsum_x <- summarise_draws(x, .cores = cores) - expect_equal(sum_x, parsum_x) - - dimnames(x)$variable[2] <- reserved_variables()[1] - sum_x <- summarise_draws(x) - parsum_x <- summarise_draws(x, .cores = cores) - expect_equal(sum_x, parsum_x) - - # test that externally defined summary functions can be found - mean2 <- function(x) sum(x) / length(x) - sum_x <- summarise_draws(x, mean2) - parsum_x <- summarise_draws(x, mean2, .cores = cores) - expect_identical(sum_x, parsum_x) - - n <- 2 - test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) - x <- as_draws_array(test_array) - sum_x <- summarise_draws(x) - parsum_x <- summarise_draws(x, .cores = cores) - expect_identical(sum_x, parsum_x) - - dimnames(x)$variable[2] <- reserved_variables()[1] - sum_x <- summarise_draws(x) - parsum_x <- summarise_draws(x, .cores = cores) - expect_identical(sum_x, parsum_x) - - n <- 1 - test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) - x <- as_draws_array(test_array) - sum_x <- summarise_draws(x) - parsum_x <- summarise_draws(x, .cores = cores) - expect_identical(sum_x, parsum_x) - - dimnames(x)$variable[1] <- reserved_variables()[1] - suppressWarnings(sum_x <- summarise_draws(x)) - suppressWarnings(parsum_x <- summarise_draws(x, .cores = cores)) - expect_identical(sum_x, parsum_x) -}) - test_that("summarise_draws supports tibble::set_num_opts correctly", { x <- example_draws() expect_output( From b89c7ef31d609800dc3e5096aed8e4b7eef55783 Mon Sep 17 00:00:00 2001 From: VisruthSK Date: Tue, 10 Nov 2026 15:14:46 -0800 Subject: [PATCH 3/9] Bumped testthat suggests min version; removed stray ignore_attr --- DESCRIPTION | 2 +- tests/testthat/test-rvar-cast.R | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 173508e8..b7f0f49c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -41,7 +41,7 @@ Imports: parallel, matrixStats Suggests: - testthat (>= 2.1.0), + testthat (>= 3.0.0), caret (>= 6.0-84), gbm (>= 2.1.8), randomForest (>= 4.6.14), diff --git a/tests/testthat/test-rvar-cast.R b/tests/testthat/test-rvar-cast.R index 42c3ca6c..df160f03 100755 --- a/tests/testthat/test-rvar-cast.R +++ b/tests/testthat/test-rvar-cast.R @@ -139,9 +139,7 @@ test_that("as_rvar_ordered works", { ) expect_equal( draws_of(as_rvar_ordered(as_rvar_factor(array(1:4, dim = c(2,2))))), - structure(1:4L, levels = c("1", "2", "3", "4"), dim = c(1, 2, 2), dimnames = list("1", NULL, NULL), class = c("ordered", "factor"), - ignore_attr = TRUE), - ignore_attr = TRUE + structure(1:4L, levels = c("1", "2", "3", "4"), dim = c(1, 2, 2), dimnames = list("1", NULL, NULL), class = c("ordered", "factor")) ) expect_equal( draws_of(as_rvar_ordered(array(c(TRUE, TRUE, FALSE, FALSE), dim = c(2,2)))), From a3d501081a914020a2ae1fbf0eac457f04f8435e Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Sat, 14 Nov 2026 19:53:23 +0100 Subject: [PATCH 4/9] Ignore only the attributes specified. --- tests/testthat/test-as_draws.R | 2 +- tests/testthat/test-convergence.R | 4 +- tests/testthat/test-extract_variable_matrix.R | 4 +- tests/testthat/test-mutate_variables.R | 8 +-- tests/testthat/test-repair_draws.R | 22 ++++---- tests/testthat/test-rvar-.R | 12 ++--- tests/testthat/test-rvar-bind.R | 6 +-- tests/testthat/test-rvar-cast.R | 6 +-- tests/testthat/test-rvar-factor.R | 10 ++-- tests/testthat/test-rvar-print.R | 2 +- .../test-rvar-summaries-within-draws.R | 54 +++++++++---------- tests/testthat/test-subset_draws.R | 8 +-- 12 files changed, 69 insertions(+), 69 deletions(-) diff --git a/tests/testthat/test-as_draws.R b/tests/testthat/test-as_draws.R index 44ccb225..13ed2a78 100644 --- a/tests/testthat/test-as_draws.R +++ b/tests/testthat/test-as_draws.R @@ -198,7 +198,7 @@ test_that("lists can be transformed to draws_list objects", { test_that("numeric vectors can be transformed to draws_matrix objects", { draws_matrix <- draws_matrix(a = 1:10, b = 11:20, c = 1) draws_matrix2 <- as_draws_matrix(cbind(1:10, 11:20, 1)) - expect_equal(draws_matrix, draws_matrix2, ignore_attr = TRUE) + expect_equal(draws_matrix, draws_matrix2, ignore_attr = "dimnames") }) test_that("numeric vectors can be transformed to draws_array objects", { diff --git a/tests/testthat/test-convergence.R b/tests/testthat/test-convergence.R index 0a34c02d..f484c2b2 100644 --- a/tests/testthat/test-convergence.R +++ b/tests/testthat/test-convergence.R @@ -91,10 +91,10 @@ test_that("mcse diagnostics return reasonable values", { # check if issue #252 remains fixed se <- mcse_quantile(tau, c(0.999999, 1)) - expect_equal(se[1], se[2], ignore_attr = TRUE) + expect_equal(se[1], se[2], ignore_attr = "names") se <- mcse_quantile(tau, c(0.000001, 0)) - expect_equal(se[1], se[2], ignore_attr = TRUE) + expect_equal(se[1], se[2], ignore_attr = "names") }) test_that("convergence diagnostics accept vectors as input", { diff --git a/tests/testthat/test-extract_variable_matrix.R b/tests/testthat/test-extract_variable_matrix.R index 1ec29633..9a075322 100644 --- a/tests/testthat/test-extract_variable_matrix.R +++ b/tests/testthat/test-extract_variable_matrix.R @@ -45,6 +45,6 @@ test_that("extract_variable_matrix default method works", { # it should convert matrix to draws object x <- matrix(1:20, nrow = 10, ncol = 2) colnames(x) <- c("A", "B") - expect_equal(extract_variable_matrix(x, "A"), x[, 1, drop=FALSE], ignore_attr = TRUE) - expect_equal(extract_variable_matrix(x, "B"), x[, 2, drop=FALSE], ignore_attr = TRUE) + expect_equal(extract_variable_matrix(x, "A"), x[, 1, drop=FALSE], ignore_attr = "dimnames") + expect_equal(extract_variable_matrix(x, "B"), x[, 2, drop=FALSE], ignore_attr = "dimnames") }) diff --git a/tests/testthat/test-mutate_variables.R b/tests/testthat/test-mutate_variables.R index a96763a5..dcb2c5b1 100644 --- a/tests/testthat/test-mutate_variables.R +++ b/tests/testthat/test-mutate_variables.R @@ -46,20 +46,20 @@ test_that("mutate_variables works correctly for draws_list objects", { test_that("mutate_variables works correctly for draws_matrix objects", { x <- as_draws_matrix(example_draws()) x <- mutate_variables(x, tau2 = tau^2) - expect_equal(x[, "tau2"], x[, "tau"]^2, ignore_attr = TRUE) + expect_equal(x[, "tau2"], x[, "tau"]^2, ignore_attr = "dimnames") }) test_that("mutate_variables works correctly for draws_array objects", { x <- as_draws_array(example_draws()) x <- mutate_variables(x, tau2 = tau^2) - expect_equal(x[, , "tau2"], x[, , "tau"]^2, ignore_attr = TRUE) + expect_equal(x[, , "tau2"], x[, , "tau"]^2, ignore_attr = "dimnames") }) test_that("mutate_variables works correctly for draws_rvars objects", { x <- as_draws_rvars(example_draws()) x <- mutate_variables(x, tau_sq = tau^2, theta_2_sq = theta[[2]]^2) - expect_equal(x$tau_sq, x$tau^2, ignore_attr = TRUE) - expect_equal(x$theta_2_sq, x$theta[[2]]^2, ignore_attr = TRUE) + expect_equal(x$tau_sq, x$tau^2, ignore_attr = "cache") + expect_equal(x$theta_2_sq, x$theta[[2]]^2, ignore_attr = "cache") }) test_that("mutate_variables drops variables on assigning NULL", { diff --git a/tests/testthat/test-repair_draws.R b/tests/testthat/test-repair_draws.R index 85401ece..d2367aa8 100644 --- a/tests/testthat/test-repair_draws.R +++ b/tests/testthat/test-repair_draws.R @@ -3,11 +3,11 @@ test_that("repair_draws works correctly on draws_matrix objects", { x <- x[10:6, ] x_rep <- repair_draws(x, order = TRUE) - expect_equal(x[5:1, ], x_rep, ignore_attr = TRUE) + expect_equal(x[5:1, ], x_rep, ignore_attr = "dimnames") expect_equal(rownames(x_rep), as.character(1:5)) x_rep <- repair_draws(x, order = FALSE) - expect_equal(x, x_rep, ignore_attr = TRUE) + expect_equal(x, x_rep, ignore_attr = "dimnames") expect_equal(rownames(x_rep), as.character(1:5)) }) @@ -16,12 +16,12 @@ test_that("repair_draws works correctly on draws_array objects", { x <- x[10:6, c(4, 1), ] x_rep <- repair_draws(x, order = TRUE) - expect_equal(x[5:1, 2:1, ], x_rep, ignore_attr = TRUE) + expect_equal(x[5:1, 2:1, ], x_rep, ignore_attr = "dimnames") expect_equal(rownames(x_rep), as.character(1:5)) expect_equal(colnames(x_rep), as.character(1:2)) x_rep <- repair_draws(x, order = FALSE) - expect_equal(x, x_rep, ignore_attr = TRUE) + expect_equal(x, x_rep, ignore_attr = "dimnames") expect_equal(rownames(x_rep), as.character(1:5)) expect_equal(colnames(x_rep), as.character(1:2)) }) @@ -37,7 +37,7 @@ test_that("repair_draws works correctly on draws_df objects", { expect_equal(x_rep$.chain, 1:4) x_rep <- repair_draws(x, order = FALSE) - expect_equal(x$mu, x_rep$mu, ignore_attr = TRUE) + expect_equal(x$mu, x_rep$mu, ignore_attr = "cache") expect_equal(x_rep$.iteration, rep(1, 4)) expect_equal(x_rep$.chain, 4:1) }) @@ -47,11 +47,11 @@ test_that("repair_draws works correctly on draws_list objects", { x <- x[c(4, 2)] x_rep <- repair_draws(x, order = TRUE) - expect_equal(x[2], x_rep[1], ignore_attr = TRUE) + expect_equal(x[2], x_rep[1], ignore_attr = "names") expect_equal(names(x_rep), as.character(1:2)) x_rep <- repair_draws(x, order = FALSE) - expect_equal(x, x_rep, ignore_attr = TRUE) + expect_equal(x, x_rep, ignore_attr = "names") expect_equal(names(x_rep), as.character(1:2)) }) @@ -65,13 +65,13 @@ test_that("repair_draws works correctly on draws_rvars objects", { expect_equal( draws_of(x_rep$mu), draws_of(x$mu)[order(as.integer(rownames(draws_of(x$mu)))),], - ignore_attr = TRUE + ignore_attr = c("dim", "dimnames", "names") ) expect_equal(niterations(x_rep), 4) expect_equal(nchains(x_rep), 1) x_rep <- repair_draws(x, order = FALSE) - expect_equal(draws_of(x$mu), draws_of(x_rep$mu), ignore_attr = TRUE) + expect_equal(draws_of(x$mu), draws_of(x_rep$mu), ignore_attr = "dimnames") expect_equal(niterations(x_rep), 4) expect_equal(nchains(x_rep), 1) @@ -79,11 +79,11 @@ test_that("repair_draws works correctly on draws_rvars objects", { expect_equal( draws_of(x_ord$mu), draws_of(x$mu)[order(as.integer(rownames(draws_of(x$mu)))),], - ignore_attr = TRUE + ignore_attr = c("dim", "dimnames", "names") ) expect_equal( draws_of(x_ord$theta), draws_of(x$theta)[order(as.integer(rownames(draws_of(x$theta)))),], - ignore_attr = TRUE + ignore_attr = "cache" ) }) diff --git a/tests/testthat/test-rvar-.R b/tests/testthat/test-rvar-.R index a7eb564a..20ac27a6 100755 --- a/tests/testthat/test-rvar-.R +++ b/tests/testthat/test-rvar-.R @@ -159,15 +159,15 @@ test_that("rvars work in tibbles", { df = tibble::tibble(x, y = x + 1) expect_equal(df$x, x) - expect_equal(df$y, rvar(x_array + 1), ignore_attr = TRUE) + expect_equal(df$y, rvar(x_array + 1), ignore_attr = "cache") expect_equal(dplyr::mutate(df, z = x)$z, x) expect_equal(dplyr::mutate(df, z = x * 2)$z, rvar(x_array * 2), - ignore_attr = TRUE) + ignore_attr = "cache") expect_equal( dplyr::mutate(dplyr::group_by(df, 1:4), z = x * 2)$z, rvar(x_array * 2), - ignore_attr = TRUE + ignore_attr = "cache" ) df = tibble::tibble(g = letters[1:4], x) @@ -178,7 +178,7 @@ test_that("rvars work in tibbles", { d = rvar(x_array[,4, drop = FALSE]) ) expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref, - ignore_attr = TRUE) + ignore_attr = "cache") expect_equal(tidyr::pivot_longer(ref, a:d, names_to = "g", values_to = "x"), df) df$y = df$x + 1 @@ -190,14 +190,14 @@ test_that("rvars work in tibbles", { d = c(rvar(NA), NA, NA, df$x[[4]]), ) expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref2, - ignore_attr = TRUE) + ignore_attr = "cache") expect_equal(dplyr::first(df$x), x[1]) z = cbind(a = x, b = x + 1) df$z = z expect_equal(dplyr::mutate(dplyr::rowwise(df), w = z[,"b"])$w, z[,"b"], - ignore_attr = TRUE) + ignore_attr = "cache") }) diff --git a/tests/testthat/test-rvar-bind.R b/tests/testthat/test-rvar-bind.R index d002811e..30ebac6b 100755 --- a/tests/testthat/test-rvar-bind.R +++ b/tests/testthat/test-rvar-bind.R @@ -30,7 +30,7 @@ test_that("c works on rvar", { x_col <- x dim(x_col) <- c(3,1) - expect_equal(c(x_col), x, ignore_attr = TRUE) + expect_equal(c(x_col), x, ignore_attr = "cache") expect_equal(c(x_col, y), x_y) }) @@ -77,7 +77,7 @@ test_that("c works on rvar_factor", { x_col <- x dim(x_col) <- c(3,1) - expect_equal(c(x_col), x, ignore_attr = TRUE) + expect_equal(c(x_col), x, ignore_attr = "cache") expect_equal(c(x_col, y), x_y) }) @@ -139,7 +139,7 @@ test_that("c works on rvar_ordered", { x_col <- x dim(x_col) <- c(3,1) - expect_equal(c(x_col), x, ignore_attr = TRUE) + expect_equal(c(x_col), x, ignore_attr = "cache") expect_equal(c(x_col, y), x_y) }) diff --git a/tests/testthat/test-rvar-cast.R b/tests/testthat/test-rvar-cast.R index df160f03..971875cc 100755 --- a/tests/testthat/test-rvar-cast.R +++ b/tests/testthat/test-rvar-cast.R @@ -343,9 +343,9 @@ test_that("proxy restore works", { dimnames = list(A = paste0("a", 1:2), B = paste0("b", 1:2), C = paste0("c", 1:4)) ) - expect_equal(vec_restore(vec_proxy(unname(x1)), rvar()), unname(x1), ignore_attr = TRUE) - expect_equal(vec_restore(vec_proxy(unname(x2)), rvar()), unname(x2), ignore_attr = TRUE) - expect_equal(vec_restore(vec_proxy(unname(x3)), rvar()), unname(x3), ignore_attr = TRUE) + expect_equal(vec_restore(vec_proxy(unname(x1)), rvar()), unname(x1), ignore_attr = "cache") + expect_equal(vec_restore(vec_proxy(unname(x2)), rvar()), unname(x2), ignore_attr = "cache") + expect_equal(vec_restore(vec_proxy(unname(x3)), rvar()), unname(x3), ignore_attr = "cache") expect_equal(vec_restore(vec_proxy(x1), rvar()), x1) expect_equal(vec_restore(vec_proxy(x2), rvar()), x2) expect_equal(vec_restore(vec_proxy(x3), rvar()), x3) diff --git a/tests/testthat/test-rvar-factor.R b/tests/testthat/test-rvar-factor.R index 6d47ad71..1eb016fe 100644 --- a/tests/testthat/test-rvar-factor.R +++ b/tests/testthat/test-rvar-factor.R @@ -55,15 +55,15 @@ test_that("rvar_factors work in tibbles", { df = tibble::tibble(x, y = as_rvar_ordered(x)) expect_equal(df$x, x) - expect_equal(df$y, rvar(x_array_ordered), ignore_attr = TRUE) + expect_equal(df$y, rvar(x_array_ordered), ignore_attr = "cache") expect_equal(dplyr::mutate(df, z = x)$z, x) expect_equal(dplyr::mutate(df, z = as_rvar_ordered(x))$z, rvar(x_array_ordered), - ignore_attr = TRUE) + ignore_attr = "cache") expect_equal( dplyr::group_by(dplyr::mutate(df, z = as_rvar_ordered(x)), 1:4)$z, rvar(x_array_ordered), - ignore_attr = TRUE + ignore_attr = "cache" ) df = tibble::tibble(g = letters[1:4], x) @@ -74,7 +74,7 @@ test_that("rvar_factors work in tibbles", { d = rvar(x_array[,4, drop = FALSE]) ) expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref, - ignore_attr = TRUE) + ignore_attr = "cache") expect_equal(tidyr::pivot_longer(ref, a:d, names_to = "g", values_to = "x"), df) df$y = as_rvar_ordered(df$x) @@ -86,7 +86,7 @@ test_that("rvar_factors work in tibbles", { d = c(rvar(factor(NA)), NA, NA, df$x[[4]]), ) expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref2, - ignore_attr = TRUE) + ignore_attr = "cache") }) # rep --------------------------------------------------------------------- diff --git a/tests/testthat/test-rvar-print.R b/tests/testthat/test-rvar-print.R index cbf5d036..cee717a5 100755 --- a/tests/testthat/test-rvar-print.R +++ b/tests/testthat/test-rvar-print.R @@ -212,7 +212,7 @@ test_that("glimpse on rvar works", { expect_equal( format_glimpse(x_vec), c("3.5 ± 1.9", "9.5 ± 1.9", "15.5 ± 1.9", "21.5 ± 1.9"), - ignore_attr = TRUE + ignore_attr = c("dim", "dimnames") ) expect_equal(format_glimpse(x_matrix), "") }) diff --git a/tests/testthat/test-rvar-summaries-within-draws.R b/tests/testthat/test-rvar-summaries-within-draws.R index 7ba9fb93..3a26e26e 100755 --- a/tests/testthat/test-rvar-summaries-within-draws.R +++ b/tests/testthat/test-rvar-summaries-within-draws.R @@ -7,12 +7,12 @@ test_that("numeric summary functions work", { x_ord <- rvar_ordered(x_letters, levels = letters) x_fct <- rvar_factor(x_letters, levels = letters) - expect_equal(draws_of(rvar_mean(x)), apply(x_array, 1, mean), ignore_attr = TRUE) - expect_equal(draws_of(rvar_median(x)), apply(x_array, 1, median), ignore_attr = TRUE) - expect_equal(draws_of(rvar_sum(x)), apply(x_array, 1, sum), ignore_attr = TRUE) - expect_equal(draws_of(rvar_prod(x)), apply(x_array, 1, prod), ignore_attr = TRUE) - expect_equal(draws_of(rvar_min(x)), apply(x_array, 1, min), ignore_attr = TRUE) - expect_equal(draws_of(rvar_max(x)), apply(x_array, 1, max), ignore_attr = TRUE) + expect_equal(draws_of(rvar_mean(x)), apply(x_array, 1, mean), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_median(x)), apply(x_array, 1, median), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_sum(x)), apply(x_array, 1, sum), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_prod(x)), apply(x_array, 1, prod), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_min(x)), apply(x_array, 1, min), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_max(x)), apply(x_array, 1, max), ignore_attr = c("dim", "dimnames")) expect_error(rvar_mean(x_ord)) expect_equal(rvar_median(x_ord), rvar_ordered(letters[apply(x_array, 1, median)], levels = letters)) @@ -41,7 +41,7 @@ test_that("numeric summary functions work", { expect_equal( draws_of(rvar_mean(x, na.rm = TRUE)), apply(draws_of(x), 1, function(x) mean(x, na.rm = TRUE)), - ignore_attr = TRUE + ignore_attr = c("dim", "dimnames", "names") ) }) @@ -55,10 +55,10 @@ test_that("spread summary functions work", { x_ord <- rvar_ordered(x_letters, levels = letters) x_fct <- rvar_factor(x_letters, levels = letters) - expect_equal(draws_of(rvar_sd(x)), apply(x_array, 1, sd), ignore_attr = TRUE) - expect_equal(draws_of(rvar_var(x)), apply(x_array, 1, function(x) var(as.vector(x))), ignore_attr = TRUE) - expect_equal(draws_of(rvar_mad(x)), apply(x_array, 1, mad), ignore_attr = TRUE) - expect_equal(draws_of(rvar_mad(x, constant = 1)), apply(x_array, 1, mad, constant = 1), ignore_attr = TRUE) + expect_equal(draws_of(rvar_sd(x)), apply(x_array, 1, sd), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_var(x)), apply(x_array, 1, function(x) var(as.vector(x))), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_mad(x)), apply(x_array, 1, mad), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_mad(x, constant = 1)), apply(x_array, 1, mad, constant = 1), ignore_attr = c("dim", "dimnames")) expect_error(rvar_sd(x_ord)) expect_error(rvar_var(x_ord)) @@ -78,7 +78,7 @@ test_that("spread summary functions work", { expect_equal( draws_of(rvar_var(x, na.rm = TRUE)), apply(draws_of(x), 1, function(x) var(as.vector(x), na.rm = TRUE)), - ignore_attr = TRUE + ignore_attr = c("dim", "dimnames", "names") ) }) @@ -91,7 +91,7 @@ test_that("rvar_range works", { x_letters <- array(letters[1:24], dim = c(4,2,3)) x_ord <- rvar_ordered(x_letters, levels = letters) - expect_equal(draws_of(rvar_range(x)), t(apply(x_array, 1, range)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_range(x)), t(apply(x_array, 1, range)), ignore_attr = "dimnames") expect_equal(rvar_range(x_ord), rvar_ordered(array(letters[t(apply(x_array, 1, range))], dim = c(4, 2)), levels = letters)) expect_error(rvar_range(rvar_factor("a"))) @@ -137,8 +137,8 @@ test_that("logical summaries work", { x_array <- array(1:24, dim = c(4,2,3)) x <- new_rvar(x_array) - expect_equal(draws_of(rvar_all(x > 6)), as.matrix(apply(x_array > 6, 1, all)), ignore_attr = TRUE) - expect_equal(draws_of(rvar_any(x > 6)), as.matrix(apply(x_array > 6, 1, any)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_all(x > 6)), as.matrix(apply(x_array > 6, 1, all)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_any(x > 6)), as.matrix(apply(x_array > 6, 1, any)), ignore_attr = "dimnames") expect_error(rvar_all(rvar("a"))) expect_error(rvar_any(rvar("a"))) @@ -158,18 +158,18 @@ test_that("special value predicates work", { x_ord <- rvar_ordered(x_letters) x_fct <- rvar_factor(x_letters) - expect_equal(draws_of(rvar_is_finite(x)), as.matrix(is.finite(x_array)), ignore_attr = TRUE) - expect_equal(draws_of(rvar_is_infinite(x)), as.matrix(is.infinite(x_array)), ignore_attr = TRUE) - expect_equal(draws_of(rvar_is_nan(x)), as.matrix(is.nan(x_array)), ignore_attr = TRUE) - expect_equal(draws_of(rvar_is_na(x)), as.matrix(is.na(x_array)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_is_finite(x)), as.matrix(is.finite(x_array)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_infinite(x)), as.matrix(is.infinite(x_array)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_nan(x)), as.matrix(is.nan(x_array)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_na(x)), as.matrix(is.na(x_array)), ignore_attr = "dimnames") - expect_equal(draws_of(rvar_is_finite(x_ord)), as.matrix(is.finite(x_letters)), ignore_attr = TRUE) - expect_equal(draws_of(rvar_is_infinite(x_ord)), as.matrix(is.infinite(x_letters)), ignore_attr = TRUE) - expect_equal(draws_of(rvar_is_nan(x_ord)), as.matrix(is.nan(x_letters)), ignore_attr = TRUE) - expect_equal(draws_of(rvar_is_na(x_ord)), as.matrix(is.na(x_letters)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_is_finite(x_ord)), as.matrix(is.finite(x_letters)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_infinite(x_ord)), as.matrix(is.infinite(x_letters)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_nan(x_ord)), as.matrix(is.nan(x_letters)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_na(x_ord)), as.matrix(is.na(x_letters)), ignore_attr = "dimnames") - expect_equal(draws_of(rvar_is_finite(x_fct)), as.matrix(is.finite(x_letters)), ignore_attr = TRUE) - expect_equal(draws_of(rvar_is_infinite(x_fct)), as.matrix(is.infinite(x_letters)), ignore_attr = TRUE) - expect_equal(draws_of(rvar_is_nan(x_fct)), as.matrix(is.nan(x_letters)), ignore_attr = TRUE) - expect_equal(draws_of(rvar_is_na(x_fct)), as.matrix(is.na(x_letters)), ignore_attr = TRUE) + expect_equal(draws_of(rvar_is_finite(x_fct)), as.matrix(is.finite(x_letters)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_infinite(x_fct)), as.matrix(is.infinite(x_letters)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_nan(x_fct)), as.matrix(is.nan(x_letters)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_na(x_fct)), as.matrix(is.na(x_letters)), ignore_attr = "dimnames") }) diff --git a/tests/testthat/test-subset_draws.R b/tests/testthat/test-subset_draws.R index f6e5adc3..9d1c88f5 100644 --- a/tests/testthat/test-subset_draws.R +++ b/tests/testthat/test-subset_draws.R @@ -3,12 +3,12 @@ test_that("subset_draws works correctly for draws_matrix objects", { x <- as_draws_matrix(example_draws()) x_sub <- subset_draws(x, variable = c("mu", "tau"), iteration = 5:10) x_sub2 <- x[c(5:10, 105:110, 205:210, 305:310), c("mu", "tau")] - expect_equal(x_sub, x_sub2, ignore_attr = TRUE) + expect_equal(x_sub, x_sub2, ignore_attr = c("dimnames", "nchains")) expect_equal(iteration_ids(x_sub), 1:6) expect_message(x_sub <- subset_draws(x, draw = c(2, 2, 4, 4), unique = FALSE)) expect_equal(niterations(x_sub), 4) - expect_equal(x_sub[1, ], x_sub[2, ], ignore_attr = TRUE) + expect_equal(x_sub[1, ], x_sub[2, ], ignore_attr = "dimnames") x <- weight_draws(x, rep(1, ndraws(x))) x_sub <- subset_draws(x, variable = "mu") @@ -38,13 +38,13 @@ test_that("subset_draws works correctly for draws_array objects", { x <- as_draws_array(example_draws()) x_sub <- subset_draws(x, variable = c("mu", "tau"), iteration = 5:10, chain = 3:4) - expect_equal(x[5:10, 3:4, c("mu", "tau")], x_sub, ignore_attr = TRUE) + expect_equal(x[5:10, 3:4, c("mu", "tau")], x_sub, ignore_attr = "dimnames") expect_equal(iteration_ids(x_sub), 1:6) expect_equal(chain_ids(x_sub), 1:2) x_sub <- subset_draws(x, chain = c(1, 1), unique = FALSE) expect_equal(nchains(x_sub), 2) - expect_equal(x_sub[, 1, ], x_sub[, 2, ], ignore_attr = TRUE) + expect_equal(x_sub[, 1, ], x_sub[, 2, ], ignore_attr = "dimnames") expect_message( x_sub <- subset_draws(x, draw = c(1, 200, 10)), From e0b396fa5a2951f538715050a431c5a7035a57be Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Sat, 14 Nov 2026 20:09:51 +0100 Subject: [PATCH 5/9] Suppress messages via option instead of using expect_messages(). --- tests/testthat/test-as_draws.R | 11 ++----- tests/testthat/test-resample_draws.R | 27 +++++++++++----- tests/testthat/test-subset_draws.R | 47 ++++++++++++++++++---------- 3 files changed, 54 insertions(+), 31 deletions(-) diff --git a/tests/testthat/test-as_draws.R b/tests/testthat/test-as_draws.R index 13ed2a78..62a5e70e 100644 --- a/tests/testthat/test-as_draws.R +++ b/tests/testthat/test-as_draws.R @@ -58,15 +58,10 @@ test_that("transformations to and from draws_df objects work", { expect_equal(draws_df, draws_df2) # test that a single draw does not lead to a drop of dimensions - expect_message( - draws_rvars2 <- subset_draws(draws_rvars, draw = 1), - "Merging chains in order to subset via 'draw'" - ) + options(posterior.warn_on_merge_chains = FALSE) + draws_rvars2 <- subset_draws(draws_rvars, draw = 1) draws_df2 <- as_draws_df(draws_rvars2) - expect_message( - expect_equal(subset_draws(draws_df, draw = 1), draws_df2), - "Merging chains in order to subset via 'draw'" - ) + expect_equal(subset_draws(draws_df, draw = 1), draws_df2) }) test_that("transformations to and from draws_list objects work", { diff --git a/tests/testthat/test-resample_draws.R b/tests/testthat/test-resample_draws.R index d108a420..553555cb 100644 --- a/tests/testthat/test-resample_draws.R +++ b/tests/testthat/test-resample_draws.R @@ -1,24 +1,31 @@ test_that("resample_draws returns expected format", { + + # Store and restore option to ensure test isolation + old_option <- getOption("posterior.warn_on_merge_chains") + on.exit(options(posterior.warn_on_merge_chains = old_option)) + + options(posterior.warn_on_merge_chains = FALSE) + x <- example_draws() w <- runif(ndraws(x), 0, 10) x <- as_draws_matrix(x) - expect_message(x_rs <- resample_draws(x, weights = w, method = "stratified")) + x_rs <- resample_draws(x, weights = w, method = "stratified") expect_true(is_draws_matrix(x_rs)) expect_equal(ndraws(x_rs), ndraws(x)) x <- as_draws_array(x) - expect_message(x_rs <- resample_draws(x, weights = w, method = "deterministic", ndraws = 200)) + x_rs <- resample_draws(x, weights = w, method = "deterministic", ndraws = 200) expect_true(is_draws_array(x_rs)) expect_equal(ndraws(x_rs), 200) x <- as_draws_df(x) - expect_message(x_rs <- resample_draws(x, weights = w, method = "simple")) + x_rs <- resample_draws(x, weights = w, method = "simple") expect_true(is_draws_df(x_rs)) expect_equal(ndraws(x_rs), ndraws(x)) x <- as_draws_list(x) - expect_message(x_rs <- resample_draws(x, w, method = "simple_no_replace", ndraws = 100)) + x_rs <- resample_draws(x, w, method = "simple_no_replace", ndraws = 100) expect_true(is_draws_list(x_rs)) expect_equal(ndraws(x_rs), 100) expect_error( @@ -27,7 +34,7 @@ test_that("resample_draws returns expected format", { ) x <- as_draws_rvars(x) - expect_message(x_rs <- resample_draws(x, weights = w, method = "simple")) + x_rs <- resample_draws(x, weights = w, method = "simple") expect_true(is_draws_rvars(x_rs)) expect_equal(ndraws(x_rs), ndraws(x)) }) @@ -54,15 +61,21 @@ test_that("Resampling algorithms return the correct result in expectation", { }) test_that("resample_draws uses stored weights when available", { + # Store and restore option to ensure test isolation + old_option <- getOption("posterior.warn_on_merge_chains") + on.exit(options(posterior.warn_on_merge_chains = old_option)) + + options(posterior.warn_on_merge_chains = FALSE) + x <- example_draws() - expect_message(x_rs <- resample_draws(x)) + x_rs <- resample_draws(x) expect_true(is_draws_array(x_rs)) expect_equal(ndraws(x_rs), ndraws(x)) w <- runif(ndraws(x), 0, 10) x <- weight_draws(x, w) - expect_message(x_rs <- resample_draws(x)) + x_rs <- resample_draws(x) expect_true(is_draws_array(x_rs)) expect_equal(ndraws(x_rs), ndraws(x)) # .log_weight variable has been dropped diff --git a/tests/testthat/test-subset_draws.R b/tests/testthat/test-subset_draws.R index 9d1c88f5..9cce7f31 100644 --- a/tests/testthat/test-subset_draws.R +++ b/tests/testthat/test-subset_draws.R @@ -1,12 +1,18 @@ test_that("subset_draws works correctly for draws_matrix objects", { + # Store and restore option to ensure test isolation + old_option <- getOption("posterior.warn_on_merge_chains") + on.exit(options(posterior.warn_on_merge_chains = old_option)) + + options(posterior.warn_on_merge_chains = FALSE) + x <- as_draws_matrix(example_draws()) x_sub <- subset_draws(x, variable = c("mu", "tau"), iteration = 5:10) x_sub2 <- x[c(5:10, 105:110, 205:210, 305:310), c("mu", "tau")] expect_equal(x_sub, x_sub2, ignore_attr = c("dimnames", "nchains")) expect_equal(iteration_ids(x_sub), 1:6) - expect_message(x_sub <- subset_draws(x, draw = c(2, 2, 4, 4), unique = FALSE)) + x_sub <- subset_draws(x, draw = c(2, 2, 4, 4), unique = FALSE) expect_equal(niterations(x_sub), 4) expect_equal(x_sub[1, ], x_sub[2, ], ignore_attr = "dimnames") @@ -18,7 +24,7 @@ test_that("subset_draws works correctly for draws_matrix objects", { expect_equal(setdiff(variables(x, reserved = TRUE), "mu"), variables(x_sub, reserved = TRUE)) expect_equal(nchains(x_sub), 1) - expect_message(x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE)) + x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE) expect_equal(ndraws(x) - 3, ndraws(x_sub)) x_sub <- subset_draws(x, iteration = c(1, 2, 3), exclude = TRUE) @@ -35,6 +41,8 @@ test_that("subset_draws works correctly for draws_array objects", { old_option <- getOption("posterior.warn_on_merge_chains") on.exit(options(posterior.warn_on_merge_chains = old_option)) + options(posterior.warn_on_merge_chains = FALSE) + x <- as_draws_array(example_draws()) x_sub <- subset_draws(x, variable = c("mu", "tau"), iteration = 5:10, chain = 3:4) @@ -46,10 +54,8 @@ test_that("subset_draws works correctly for draws_array objects", { expect_equal(nchains(x_sub), 2) expect_equal(x_sub[, 1, ], x_sub[, 2, ], ignore_attr = "dimnames") - expect_message( - x_sub <- subset_draws(x, draw = c(1, 200, 10)), - "Merging chains in order to subset via 'draw'" - ) + + x_sub <- subset_draws(x, draw = c(1, 200, 10)) expect_equal(niterations(x_sub), 3) x <- weight_draws(x, rep(1, ndraws(x))) @@ -60,7 +66,7 @@ test_that("subset_draws works correctly for draws_array objects", { expect_equal(setdiff(variables(x, reserved = TRUE), "mu"), variables(x_sub, reserved = TRUE)) expect_equal(nchains(x_sub), 1) - expect_message(x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE)) + x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE) expect_equal(ndraws(x) - 3, ndraws(x_sub)) x_sub <- subset_draws(x, iteration = c(1, 2, 3), exclude = TRUE) @@ -89,13 +95,19 @@ test_that("subset_draws preserves weighted draws_df when selecting all variables }) test_that("subset_draws works correctly for draws_df objects", { + # Store and restore option to ensure test isolation + old_option <- getOption("posterior.warn_on_merge_chains") + on.exit(options(posterior.warn_on_merge_chains = old_option)) + + options(posterior.warn_on_merge_chains = FALSE) + x <- as_draws_df(example_draws()) x_sub <- subset_draws(x, variable = c("mu", "tau"), iteration = 5:10, chain = 3:4) expect_equal(x$mu[x$.iteration %in% 5:10 & x$.chain %in% 3:4], x_sub$mu) expect_equal(iteration_ids(x_sub), 1:6) expect_equal(chain_ids(x_sub), 1:2) - expect_message(x_sub <- subset_draws(x, draw = c(5, 5, 6), unique = FALSE)) + x_sub <- subset_draws(x, draw = c(5, 5, 6), unique = FALSE) expect_equal(ndraws(x_sub), 3) expect_equal(x_sub$mu[1], x_sub$mu[2]) @@ -107,7 +119,7 @@ test_that("subset_draws works correctly for draws_df objects", { expect_equal(setdiff(variables(x, reserved = TRUE), "mu"), variables(x_sub, reserved = TRUE)) expect_equal(nchains(x_sub), 1) - expect_message(x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE)) + x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE) expect_equal(ndraws(x) - 3, ndraws(x_sub)) x_sub <- subset_draws(x, iteration = c(1, 2, 3), exclude = TRUE) @@ -119,6 +131,8 @@ test_that("subset_draws works correctly for draws_list objects", { old_option <- getOption("posterior.warn_on_merge_chains") on.exit(options(posterior.warn_on_merge_chains = old_option)) + options(posterior.warn_on_merge_chains = FALSE) + x <- as_draws_list(example_draws()) x_sub <- subset_draws(x, variable = c("theta[1]"), iteration = 5:10, chain = 3:4) expect_equal(variables(x_sub), "theta[1]") @@ -129,10 +143,7 @@ test_that("subset_draws works correctly for draws_list objects", { expect_equal(niterations(x_sub), 3) expect_equal(x_sub[[1]]$mu[1], x_sub[[1]]$mu[2]) - expect_message( - x_sub <- subset_draws(x, draw = c(1, 200, 10)), - "Merging chains in order to subset via 'draw'" - ) + x_sub <- subset_draws(x, draw = c(1, 200, 10)) expect_equal(niterations(x_sub), 3) x <- weight_draws(x, rep(1, ndraws(x))) @@ -143,7 +154,7 @@ test_that("subset_draws works correctly for draws_list objects", { expect_equal(setdiff(variables(x, reserved = TRUE), "mu"), variables(x_sub, reserved = TRUE)) expect_equal(nchains(x_sub), 1) - expect_message(x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE)) + x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE) expect_equal(ndraws(x) - 3, ndraws(x_sub)) x_sub <- subset_draws(x, iteration = c(1, 2, 3), exclude = TRUE) @@ -171,6 +182,8 @@ test_that("subset_draws works correctly for draws_rvars objects", { ) expect_equal(niterations(x_sub), 3) + options(posterior.warn_on_merge_chains = FALSE) + x <- weight_draws(x, rep(1, ndraws(x))) x_sub <- subset_draws(x, variable = "mu") expect_equal(variables(x_sub, reserved = TRUE), c("mu", ".log_weight")) @@ -179,7 +192,7 @@ test_that("subset_draws works correctly for draws_rvars objects", { expect_equal(setdiff(variables(x, reserved = TRUE), "mu"), variables(x_sub, reserved = TRUE)) expect_equal(nchains(x_sub), 1) - expect_message(x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE)) + x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE) expect_equal(ndraws(x) - 3, ndraws(x_sub)) x_sub <- subset_draws(x, iteration = c(1, 2, 3), exclude = TRUE) @@ -206,11 +219,13 @@ test_that("subset_draws works correctly for rvar objects", { ) expect_equal(niterations(x_sub), 3) + options(posterior.warn_on_merge_chains = FALSE) + x_sub <- subset_draws(x, iteration = c(1, 2), chain = c(1, 2, 3), exclude = TRUE) expect_equal(niterations(x_sub), niterations(x) - 2) expect_equal(nchains(x_sub), 1) - expect_message(x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE)) + x_sub <- subset_draws(x, draw = c(1, 2, 3), exclude = TRUE) expect_equal(ndraws(x) - 3, ndraws(x_sub)) x_sub <- subset_draws(x, iteration = c(1, 2, 3), exclude = TRUE) From 44f420c08fd8dcafca0c891218c419e41d42f86e Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Sat, 14 Nov 2026 20:31:13 +0100 Subject: [PATCH 6/9] Replace suppressMessages() with verbose = FALSE. --- tests/testthat/test-pareto_smooth.R | 34 +++++++++++------------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/tests/testthat/test-pareto_smooth.R b/tests/testthat/test-pareto_smooth.R index d3d0b6db..5a42f8a2 100644 --- a/tests/testthat/test-pareto_smooth.R +++ b/tests/testthat/test-pareto_smooth.R @@ -173,10 +173,8 @@ test_that("pareto_khat functions work with matrix with chains", { expect_equal(pareto_khat(tau_chains, ndraws_tail = 20), pareto_khat(tau_nochains, ndraws_tail = 20)) - suppressMessages({ - ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, return_k = TRUE) - ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, return_k = TRUE) - }) + ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, return_k = TRUE, verbose = FALSE) + ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, return_k = TRUE, verbose = FALSE) expect_equal(as.numeric(ps_chains$x), as.numeric(ps_nochains$x)) }) @@ -202,25 +200,21 @@ test_that("pareto_khat functions work with rvars with and without chains", { expect_equal(pareto_diags(tau_rvar_chains, ndraws_tail = 20), pareto_diags(tau_rvar_nochains, ndraws_tail = 20)) - suppressMessages({ - ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, return_k = TRUE) - ps_rvar_chains <- pareto_smooth(tau_rvar_chains, ndraws_tail = 20, return_k = TRUE) + ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, return_k = TRUE, verbose = FALSE) + ps_rvar_chains <- pareto_smooth(tau_rvar_chains, ndraws_tail = 20, return_k = TRUE, verbose = FALSE) - ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, return_k = TRUE) - ps_rvar_nochains <- pareto_smooth(tau_rvar_nochains, ndraws_tail = 20, return_k = TRUE) - }) + ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, return_k = TRUE, verbose = FALSE) + ps_rvar_nochains <- pareto_smooth(tau_rvar_nochains, ndraws_tail = 20, return_k = TRUE, verbose = FALSE) expect_equal(ps_rvar_chains$x, rvar(ps_chains$x, with_chains = TRUE)) expect_equal(ps_rvar_nochains$x, rvar(ps_nochains$x)) - suppressMessages({ - ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE) - ps_rvar_chains <- pareto_smooth(tau_rvar_chains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE) + ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE, verbose = FALSE) + ps_rvar_chains <- pareto_smooth(tau_rvar_chains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE, verbose = FALSE) - ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE) - ps_rvar_nochains <- pareto_smooth(tau_rvar_nochains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE) - }) + ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE, verbose = FALSE) + ps_rvar_nochains <- pareto_smooth(tau_rvar_nochains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE, verbose = FALSE) expect_equal(ps_rvar_chains$x, rvar(ps_chains$x, with_chains = TRUE)) @@ -231,13 +225,11 @@ test_that("pareto_khat functions work with rvars with and without chains", { test_that("pareto_smooth returns x with smoothed tail(s)", { mu <- extract_variable_matrix(example_draws(), "mu") - suppressMessages({ - mu_smoothed_right <- pareto_smooth(mu, ndraws_tail = 10, tail = "right", return_k = TRUE)$x + mu_smoothed_right <- pareto_smooth(mu, ndraws_tail = 10, tail = "right", return_k = TRUE, verbose = FALSE)$x - mu_smoothed_left <- pareto_smooth(mu, ndraws_tail = 10, tail = "left", return_k = TRUE)$x + mu_smoothed_left <- pareto_smooth(mu, ndraws_tail = 10, tail = "left", return_k = TRUE, verbose = FALSE)$x - mu_smoothed_both <- pareto_smooth(mu, ndraws_tail = 10, tail = "both", return_k = TRUE)$x - }) + mu_smoothed_both <- pareto_smooth(mu, ndraws_tail = 10, tail = "both", return_k = TRUE, verbose = FALSE)$x expect_equal(sort(mu)[1:390], sort(mu_smoothed_right)[1:390]) expect_equal(sort(mu_smoothed_both)[11:400], sort(mu_smoothed_right)[11:400]) From 1b7d0e5d8e84b0addd8305105d6cc42d61ee8024 Mon Sep 17 00:00:00 2001 From: VisruthSK Date: Sat, 14 Nov 2026 20:46:39 -0800 Subject: [PATCH 7/9] small fixes --- tests/testthat/test-as_draws.R | 2 ++ tests/testthat/test-subset_draws.R | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-as_draws.R b/tests/testthat/test-as_draws.R index 62a5e70e..ca13c58f 100644 --- a/tests/testthat/test-as_draws.R +++ b/tests/testthat/test-as_draws.R @@ -58,6 +58,8 @@ test_that("transformations to and from draws_df objects work", { expect_equal(draws_df, draws_df2) # test that a single draw does not lead to a drop of dimensions + old_option <- getOption("posterior.warn_on_merge_chains") + on.exit(options(posterior.warn_on_merge_chains = old_option), add = TRUE) options(posterior.warn_on_merge_chains = FALSE) draws_rvars2 <- subset_draws(draws_rvars, draw = 1) draws_df2 <- as_draws_df(draws_rvars2) diff --git a/tests/testthat/test-subset_draws.R b/tests/testthat/test-subset_draws.R index 9cce7f31..b409fb57 100644 --- a/tests/testthat/test-subset_draws.R +++ b/tests/testthat/test-subset_draws.R @@ -164,8 +164,8 @@ test_that("subset_draws works correctly for draws_list objects", { test_that("subset_draws works correctly for draws_rvars objects", { # Store and restore option to ensure test isolation old_option <- getOption("posterior.warn_on_merge_chains") - on.exit(options(posterior.warn_on_merge_chains = old_option)) - + on.exit(options(posterior.warn_on_merge_chains = old_option), add = TRUE) + options(posterior.warn_on_merge_chains = TRUE) x <- as_draws_rvars(example_draws()) x_sub <- subset_draws(x, variable = c("mu"), iteration = 5:10, chain = 3:4) expect_equal(variables(x_sub), "mu") From 11576452742c0542a858b5b66a74067fecb1865d Mon Sep 17 00:00:00 2001 From: VisruthSK Date: Sat, 14 Nov 2026 20:56:04 -0800 Subject: [PATCH 8/9] Added some snaps --- tests/testthat/_snaps/pareto_smooth.md | 44 +++++++ tests/testthat/_snaps/rvar-print.md | 97 ++++++++++++++ tests/testthat/test-pareto_smooth.R | 51 +++++--- tests/testthat/test-rvar-print.R | 172 +++---------------------- 4 files changed, 187 insertions(+), 177 deletions(-) create mode 100644 tests/testthat/_snaps/pareto_smooth.md create mode 100644 tests/testthat/_snaps/rvar-print.md diff --git a/tests/testthat/_snaps/pareto_smooth.md b/tests/testthat/_snaps/pareto_smooth.md new file mode 100644 index 00000000..65792076 --- /dev/null +++ b/tests/testthat/_snaps/pareto_smooth.md @@ -0,0 +1,44 @@ +# pareto_khat diagnostics messages are as expected + + Code + pareto_k_diagmsg(diags) + Message + Pareto k-hat = 0.5. + Code + diags$khat <- 0.6 + pareto_k_diagmsg(diags) + Message + Pareto k-hat = 0.6. Sample size is too small, for given Pareto k-hat. Sample size larger than 10 is needed for reliable results. + + Code + diags$khat <- 0.71 + diags$khat_threshold <- 0.8 + pareto_k_diagmsg(diags) + Message + Pareto k-hat = 0.71. Bias dominates when k-hat > 0.7, making empirical mean estimate of the Pareto-smoothed draws unreliable. + + Code + diags$khat_threshold <- 0.55 + pareto_k_diagmsg(diags) + Message + Pareto k-hat = 0.71. Sample size is too small, for given Pareto k-hat. Sample size larger than 10 is needed for reliable results. + Bias dominates when k-hat > 0.7, making empirical mean estimate of the Pareto-smoothed draws unreliable. + + Code + diags$khat <- 1.1 + pareto_k_diagmsg(diags) + Message + Pareto k-hat = 1.1. Mean does not exist, making empirical mean estimate of the draws not applicable. + Code + diags$khat <- 0.8 + pareto_k_diagmsg(diags, are_weights = TRUE) + Message + Pareto k-hat = 0.8. Pareto khat for weights is high (0.8). This indicates a single or few weights dominate. + Inference based on weighted draws will be unreliable. + + Code + diags$khat <- 0.5 + pareto_k_diagmsg(diags, are_weights = TRUE) + Message + Pareto k-hat = 0.5. + diff --git a/tests/testthat/_snaps/rvar-print.md b/tests/testthat/_snaps/rvar-print.md new file mode 100644 index 00000000..12a49bb6 --- /dev/null +++ b/tests/testthat/_snaps/rvar-print.md @@ -0,0 +1,97 @@ +# print() works + + Code + print(rvar()) + Output + rvar<1>[0] mean ± sd: + rvar() + Code + print(x) + Output + rvar<2>[2,3] mean ± sd: + [,1] [,2] [,3] + [1,] 1.5 ± 0.71 5.5 ± 0.71 9.5 ± 0.71 + [2,] 3.5 ± 0.71 7.5 ± 0.71 11.5 ± 0.71 + Code + print(x_with_chains) + Output + rvar<1,2>[2,3] mean ± sd: + [,1] [,2] [,3] + [1,] 1.5 ± 0.71 5.5 ± 0.71 9.5 ± 0.71 + [2,] 3.5 ± 0.71 7.5 ± 0.71 11.5 ± 0.71 + Code + print(x, summary = "median_mad") + Output + rvar<2>[2,3] median ± mad: + [,1] [,2] [,3] + [1,] 1.5 ± 0.74 5.5 ± 0.74 9.5 ± 0.74 + [2,] 3.5 ± 0.74 7.5 ± 0.74 11.5 ± 0.74 + +# print() works + + Code + print(x) + Output + rvar_factor<1,2>[2,3] mode : + [,1] [,2] [,3] + [1,] a <0.28> e <0.28> i <0.28> + [2,] c <0.28> g <0.28> k <0.28> + 12 levels: a b c d e f g h i j k l + +# print() works + + Code + print(x) + Output + rvar_ordered<1,2>[2,3] mode : + [,1] [,2] [,3] + [1,] a <0.067> e <0.067> i <0.067> + [2,] c <0.067> g <0.067> k <0.067> + 12 levels: a < b < c < d < e < f < g < h < i < j < k < l + +# str() works + + Code + str(rvar()) + Output + rvar<1>[0] + Code + str(rvar(1:3)) + Output + rvar<3>[1] 2 ± 1 + Code + str(x, vec.len = 5) + Output + rvar<2>[3,4] 1.5 ± 0.71 3.5 ± 0.71 5.5 ± 0.71 7.5 ± 0.71 9.5 ± 0.71 ... + Code + str(x_with_attrs, vec.len = 5) + Output + rvar<2>[3,4] 1.5 ± 0.71 3.5 ± 0.71 5.5 ± 0.71 7.5 ± 0.71 9.5 ± 0.71 ... + - dimnames(*)=List of 2 + ..$ : chr [1:3] "a" "b" "c" + ..$ : NULL + - attr(draws_of(*), "foo")=List of 2 + ..$ : num 1 + ..$ : num 2 + - attr(*, "bar")=List of 2 + ..$ : num 1 + ..$ : num 2 + +# str() works + + Code + str(x, vec.len = 5) + Output + rvar_factor<1,2>[3,4] a <0.00> c <0.22> e <0.22> g <0.22> i <0.22> ... + - 24 levels: a b c d ... x + - attr(*, "foo")= chr "bar" + +# str() works + + Code + str(x, vec.len = 5) + Output + rvar_ordered<1,2>[3,4] a <0.000> c <0.032> e <0.032> g <0.032> i <0.032> ... + - 24 levels: a < b < c < d < ... < x + - attr(*, "foo")= chr "bar" + diff --git a/tests/testthat/test-pareto_smooth.R b/tests/testthat/test-pareto_smooth.R index 5a42f8a2..91d08f30 100644 --- a/tests/testthat/test-pareto_smooth.R +++ b/tests/testthat/test-pareto_smooth.R @@ -91,7 +91,6 @@ test_that("pareto_khat handles r_eff argument", { test_that("pareto_khat diagnostics messages are as expected", { - diags <- list( khat = 0.5, min_ss = 10, @@ -99,26 +98,36 @@ test_that("pareto_khat diagnostics messages are as expected", { convergence_rate = 0.99 ) - expect_message(pareto_k_diagmsg(diags), - paste0("Pareto k-hat = 0.5.\n")) - - diags$khat <- 0.6 - - expect_message(pareto_k_diagmsg(diags), - paste0("Pareto k-hat = 0.6. Sample size is too small, for given Pareto k-hat. Sample size larger than 10 is needed for reliable results.\n")) - - diags$khat <- 0.71 - diags$khat_threshold <- 0.8 - - expect_message(pareto_k_diagmsg(diags), - paste0("Pareto k-hat = 0.71. Bias dominates when k-hat > 0.7, making empirical mean estimate of the Pareto-smoothed draws unreliable.\n")) - - - diags$khat <- 1.1 - - expect_message(pareto_k_diagmsg(diags), - paste0("Pareto k-hat = 1.1. Mean does not exist, making empirical mean estimate of the draws not applicable.\n")) - + expect_snapshot( + { + # Ordinary message + pareto_k_diagmsg(diags) + + # Sample size warning + diags$khat <- 0.6 + pareto_k_diagmsg(diags) + + # Bias warning without sample size warning + diags$khat <- 0.71 + diags$khat_threshold <- 0.8 + pareto_k_diagmsg(diags) + + # Combined sample size and bias warnings + diags$khat_threshold <- 0.55 + pareto_k_diagmsg(diags) + + # Mean does not exist + diags$khat <- 1.1 + pareto_k_diagmsg(diags) + + # Weighted diagnostics, high and low k-hat + diags$khat <- 0.8 + pareto_k_diagmsg(diags, are_weights = TRUE) + diags$khat <- 0.5 + pareto_k_diagmsg(diags, are_weights = TRUE) + }, + cran = TRUE + ) }) test_that("pareto_diags returns expected diagnostics", { diff --git a/tests/testthat/test-rvar-print.R b/tests/testthat/test-rvar-print.R index cee717a5..f8d732b6 100755 --- a/tests/testthat/test-rvar-print.R +++ b/tests/testthat/test-rvar-print.R @@ -5,107 +5,24 @@ test_that("print() works", { x <- rvar(array(1:12, dim = c(2,2,3))) x_with_chains <- rvar(array(1:12, dim = c(2,2,3)), nchains = 2) - out <- capture.output(print(rvar(), color = FALSE)) - expect_match( - out, - regexp = "rvar<1>\\[0\\] mean . sd:", - all = FALSE - ) - expect_match( - out, - regexp = "rvar\\(\\)", - all = FALSE - ) - - out <- capture.output(print(x, color = FALSE)) - expect_match( - out, - regexp = "rvar<2>\\[2,3\\] mean . sd:", - all = FALSE - ) - expect_match( - out, - regexp = " \\[,1\\] \\[,2\\] \\[,3\\] ", - all = FALSE - ) - expect_match( - out, - regexp = "\\[1,\\] 1.5 . 0.71 5.5 . 0.71 9.5 . 0.71 ", - all = FALSE - ) - out <- capture.output(print(x_with_chains, color = FALSE)) - expect_match( - out, - regexp = "rvar<1,2>\\[2,3\\] mean . sd:", - all = FALSE - ) - expect_match( - out, - regexp = " \\[,1\\] \\[,2\\] \\[,3\\] ", - all = FALSE - ) - expect_match( - out, - regexp = "\\[1,\\] 1.5 . 0.71 5.5 . 0.71 9.5 . 0.71 ", - all = FALSE - ) - expect_match( - out, - regexp = "\\[2,\\] 3.5 . 0.71 7.5 . 0.71 11.5 . 0.71", - all = FALSE - ) + expect_snapshot({ + print(rvar()) + print(x) + print(x_with_chains) + print(x, summary = "median_mad") + }) }) test_that("print() works", { x <- rvar_factor(array(letters[1:12], dim = c(2,2,3)), nchains = 2) - out <- capture.output(print(x, color = FALSE)) - expect_match( - out, - regexp = "rvar_factor<1,2>\\[2,3\\] mode :", - all = FALSE - ) - expect_match( - out, - regexp = "\\[1,\\] a <0\\.28> e <0\\.28> i <0\\.28> ", - all = FALSE - ) - expect_match( - out, - regexp = "\\[2,\\] c <0\\.28> g <0\\.28> k <0\\.28> ", - all = FALSE - ) - expect_match( - out, - regexp = "12 levels: a b c d e f g h i j k l", - all = FALSE - ) + expect_snapshot(print(x)) }) test_that("print() works", { x <- rvar_ordered(array(letters[1:12], dim = c(2,2,3)), nchains = 2) - out <- capture.output(print(x, color = FALSE)) - expect_match( - out, - regexp = "rvar_ordered<1,2>\\[2,3\\] mode :", - all = FALSE - ) - expect_match( - out, - regexp = "\\[1,\\] a <0.067> e <0.067> i <0.067> ", - all = FALSE - ) - expect_match( - out, - regexp = "\\[2,\\] c <0.067> g <0.067> k <0.067> ", - all = FALSE - ) - expect_match( - out, - regexp = "12 levels: a < b < c < d < e < f < g < h < i < j < k < l", - all = FALSE - ) + expect_snapshot(print(x)) }) @@ -113,45 +30,18 @@ test_that("print() works", { test_that("str() works", { x <- rvar(array(1:24, dim = c(2,3,4))) - x_with_chains <- rvar(array(1:24, dim = c(2,3,4)), nchains = 2) - - expect_output(str(rvar()), - " rvar<1>\\[0\\] " - ) - out <- capture.output(str(rvar(1:3))) - expect_match( - out, - regexp = " rvar<3>\\[1\\] 2 . 1", - all = FALSE - ) - out <- capture.output(str(x, vec.len = 5)) - expect_match( - out, - regexp = " rvar<2>\\[3,4\\] 1.5 . 0.71 3.5 . 0.71 5.5 . 0.71 7.5 . 0.71 9.5 . 0.71 ...", - all = FALSE - ) x_with_attrs <- x dimnames(x_with_attrs)[1] <- list(c("a","b","c")) attr(draws_of(x_with_attrs), "foo") <- list(1,2) attr(x_with_attrs, "bar") <- list(1,2) - out <- capture.output(str(x_with_attrs, vec.len = 5)) - expect_match( - out, - regexp = " rvar<2>\\[3,4\\] 1.5 . 0.71 3.5 . 0.71 5.5 . 0.71 7.5 . 0.71 9.5 . 0.71 ...", - all = FALSE - ) - expect_match( - out, - regexp = " - dimnames\\(\\*\\)=List of 2", - all = FALSE - ) - expect_match( - out, - regexp = ' \\.\\.\\$ : chr \\[1:3\\] "a" "b" "c"', - all = FALSE - ) + expect_snapshot({ + str(rvar()) + str(rvar(1:3)) + str(x, vec.len = 5) + str(x_with_attrs, vec.len = 5) + }) }) test_that("str() works", { @@ -159,22 +49,7 @@ test_that("str() works", { x[[1]] <- "a" attr(x, "foo") <- "bar" - out <- capture.output(str(x, vec.len = 5)) - expect_match( - out, - regexp = " rvar_factor<1,2>\\[3,4\\] a <0\\.00> c <0\\.22> e <0\\.22> g <0\\.22> i <0\\.22> \\.\\.\\.", - all = FALSE - ) - expect_match( - out, - regexp = " - 24 levels: a b c d \\.\\.\\. x", - all = FALSE - ) - expect_match( - out, - regexp = " - attr\\(\\*, \"foo\"\\)= ?chr \"bar\"", - all = FALSE - ) + expect_snapshot(str(x, vec.len = 5)) }) test_that("str() works", { @@ -182,22 +57,7 @@ test_that("str() works", { x[[1]] <- "a" attr(x, "foo") <- "bar" - out <- capture.output(str(x, vec.len = 5)) - expect_match( - out, - regexp = " rvar_ordered<1,2>\\[3,4\\] a <0\\.000> c <0\\.032> e <0\\.032> g <0\\.032> i <0\\.032> \\.\\.\\.", - all = FALSE - ) - expect_match( - out, - regexp = " - 24 levels: a < b < c < d < \\.\\.\\. < x", - all = FALSE - ) - expect_match( - out, - regexp = " - attr\\(\\*, \"foo\"\\)= ?chr \"bar\"", - all = FALSE - ) + expect_snapshot(str(x, vec.len = 5)) }) From 35988d738ee7e795c585ba058046dd6ed8d75512 Mon Sep 17 00:00:00 2001 From: VisruthSK Date: Mon, 16 Nov 2026 14:51:35 -0800 Subject: [PATCH 9/9] Fixed snaps --- tests/testthat/test-rvar-print.R | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-rvar-print.R b/tests/testthat/test-rvar-print.R index f8d732b6..8ff9f074 100755 --- a/tests/testthat/test-rvar-print.R +++ b/tests/testthat/test-rvar-print.R @@ -1,3 +1,6 @@ +normalize_str_output <- function(x) { + sub('= *chr "bar"', '= chr "bar"', x) +} # print ------------------------------------------------------------------- @@ -49,7 +52,10 @@ test_that("str() works", { x[[1]] <- "a" attr(x, "foo") <- "bar" - expect_snapshot(str(x, vec.len = 5)) + expect_snapshot( + str(x, vec.len = 5), + transform = normalize_str_output + ) }) test_that("str() works", { @@ -57,7 +63,10 @@ test_that("str() works", { x[[1]] <- "a" attr(x, "foo") <- "bar" - expect_snapshot(str(x, vec.len = 5)) + expect_snapshot( + str(x, vec.len = 5), + transform = normalize_str_output + ) })