diff --git a/R/undirected_planted_partition.R b/R/undirected_planted_partition.R index 0790eab..2811f0f 100644 --- a/R/undirected_planted_partition.R +++ b/R/undirected_planted_partition.R @@ -132,14 +132,35 @@ planted_partition <- function( poisson_edges = TRUE, allow_self_loops = TRUE ) { - if (is.null(within_block)) { + check_scalar(within_block, "within_block") + check_scalar(between_block, "between_block") + check_scalar(a, "a") + check_scalar(b, "b") + + if (!is.null(within_block) && !is.null(a)) { + stop("Must specify only one of `within_block` and `a`.", call. = FALSE) + } + + if (!is.null(between_block) && !is.null(b)) { + stop("Must specify only one of `between_block` and `b`.", call. = FALSE) + } + + if (is.null(within_block) && !is.null(a)) { within_block <- a / n } - if (is.null(between_block)) { + if (is.null(between_block) && !is.null(b)) { between_block <- b / n } + if (is.null(within_block) || is.null(between_block)) { + stop( + "Must specify either `within_block` and `between_block`, ", + "or `a` and `b`.", + call. = FALSE + ) + } + B <- matrix(between_block, nrow = k, ncol = k) diag(B) <- rep(within_block, k) diff --git a/tests/testthat/test-undirected-planted-partition.R b/tests/testthat/test-undirected-planted-partition.R index a45831b..fd90193 100644 --- a/tests/testthat/test-undirected-planted-partition.R +++ b/tests/testthat/test-undirected-planted-partition.R @@ -121,3 +121,30 @@ test_that("planted partition validators check B", { fixed = TRUE ) }) + +test_that("planted_partition() requires exactly one way to give each probability", { + expect_error( + planted_partition(n = 10, k = 2, within_block = 0.5, a = 2, between_block = 0.1), + "only one of `within_block` and `a`" + ) + expect_error( + planted_partition(n = 10, k = 2, within_block = 0.5, between_block = 0.1, b = 2), + "only one of `between_block` and `b`" + ) + expect_error( + planted_partition(n = 10, k = 2), + "Must specify either `within_block` and `between_block`, or `a` and `b`" + ) + expect_error( + planted_partition(n = 10, k = 2, within_block = 0.5), + "Must specify either `within_block` and `between_block`, or `a` and `b`" + ) + expect_error( + planted_partition(n = 10, k = 2, within_block = c(0.5, 0.2), between_block = 0.1), + "`within_block` must be a scalar" + ) + + # mixing the two parameterizations is fine + pp <- planted_partition(n = 10, k = 2, within_block = 0.5, b = 2) + expect_equal(pp$between_block, 0.2) +})