From 437653c0a273340c44f7306d429ea255b1f8ead5 Mon Sep 17 00:00:00 2001 From: Alex Hayes Date: Tue, 17 Nov 2026 16:50:59 -0800 Subject: [PATCH] Reject redundant or contradictory arguments in model constructors - directed_dcsbm(): `k_out` / `k_in` that disagree with `dim(B)` are an error instead of being silently ignored. Agreeing values are still allowed, as documented. - mmsbm(): specifying both `k` and `B`, or both `n` and `theta`, is an error, matching dcsbm(). `k = 1` errors clearly instead of failing inside igraph. - mmsbm() and overlapping_sbm(): `force_pure = TRUE` with `n < k` errors clearly. - chung_lu(): specifying both `n` and `theta` is an error. Co-Authored-By: Claude Opus 5.5 --- R/directed_dcsbm.R | 27 ++++++++++- R/undirected_chung_lu.R | 2 + R/undirected_mmsbm.R | 20 ++++++++ R/undirected_overlapping_sbm.R | 8 +++ man/directed_dcsbm.Rd | 4 +- tests/testthat/test-contradictory-args.R | 62 ++++++++++++++++++++++++ 6 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/test-contradictory-args.R diff --git a/R/directed_dcsbm.R b/R/directed_dcsbm.R index eb3a185..e1f7cf7 100644 --- a/R/directed_dcsbm.R +++ b/R/directed_dcsbm.R @@ -250,7 +250,7 @@ sample_block_memberships <- function(n, k, pi, block_sizes = NULL) { #' This is subject to change, and may not be reproducible. #' `k_out` defaults to `NULL`. You must specify either `k_out` and #' `k_in` together, or `B`. You may specify all three at once, in which -#' case the number of outgoing blocks is taken from `B`. +#' case `k_out` must equal `nrow(B)`. #' #' @param k_in (mixing matrix) The number of incoming blocks in the blockmodel. #' Use when you don't want to specify the mixing-matrix by hand. @@ -258,7 +258,7 @@ sample_block_memberships <- function(n, k, pi, block_sizes = NULL) { #' randomly from a `Uniform(0, 1)` distribution. #' This is subject to change, and may not be reproducible. #' `k_in` defaults to `NULL`. You may specify all three at once, in which -#' case the number of incoming blocks is taken from `B`. +#' case `k_in` must equal `ncol(B)`. #' #' @param B (mixing matrix) A `k_out` by `k_in` matrix of block connection #' probabilities. The probability that a node in block `i` connects @@ -705,6 +705,29 @@ directed_dcsbm <- function( ) } + # `k_out` and `k_in` may be given along with `B`, but must agree with it + if ( + !is.null(B) && + !is.null(k_out) && + !isTRUE(all.equal(as.numeric(k_out), as.numeric(nrow(B)))) + ) { + stop( + "`k_out` must equal `nrow(B)` when both are given.", + call. = FALSE + ) + } + + if ( + !is.null(B) && + !is.null(k_in) && + !isTRUE(all.equal(as.numeric(k_in), as.numeric(ncol(B)))) + ) { + stop( + "`k_in` must equal `ncol(B)` when both are given.", + call. = FALSE + ) + } + if (is.null(B) && (is.null(k_out) || is.null(k_in))) { stop( "Must specify either `k_out` and `k_in` together, or `B`.", diff --git a/R/undirected_chung_lu.R b/R/undirected_chung_lu.R index 56f5fbe..86a3d70 100644 --- a/R/undirected_chung_lu.R +++ b/R/undirected_chung_lu.R @@ -85,6 +85,8 @@ chung_lu <- function( theta <- stats::rlnorm(n, meanlog = 2, sdlog = 1) } else if (is.null(n)) { n <- length(theta) + } else { + stop("Must specify only one of `n` and `theta`, not both.", call. = FALSE) } chung_lu <- dcsbm( diff --git a/R/undirected_mmsbm.R b/R/undirected_mmsbm.R index 7a8f81f..05eea76 100644 --- a/R/undirected_mmsbm.R +++ b/R/undirected_mmsbm.R @@ -211,6 +211,8 @@ mmsbm <- function( theta <- stats::rlnorm(n, meanlog = 2, sdlog = 1) } else if (is.null(n)) { n <- length(theta) + } else { + stop("Must specify only one of `n` and `theta`, not both.", call. = FALSE) } ### mixing matrix @@ -235,6 +237,24 @@ mmsbm <- function( } k <- nrow(B) + } else { + stop("Must specify only one of `B` and `k`, not both.", call. = FALSE) + } + + if (k < 2) { + stop( + "`mmsbm()` needs at least two blocks. For a single block, use ", + "`chung_lu()` or `dcsbm()`.", + call. = FALSE + ) + } + + if (force_pure && n < k) { + stop( + "`force_pure = TRUE` needs at least as many nodes as blocks ", + "(`n >= k`).", + call. = FALSE + ) } ### block membership diff --git a/R/undirected_overlapping_sbm.R b/R/undirected_overlapping_sbm.R index cb67426..43a3b13 100644 --- a/R/undirected_overlapping_sbm.R +++ b/R/undirected_overlapping_sbm.R @@ -417,6 +417,14 @@ overlapping_sbm <- function( stop("Must specify only one of `B` and `k`, not both.", call. = FALSE) } + if (force_pure && n < k) { + stop( + "`force_pure = TRUE` needs at least as many nodes as blocks ", + "(`n >= k`).", + call. = FALSE + ) + } + ### block membership if (length(pi) != nrow(B) || length(pi) != ncol(B)) { diff --git a/man/directed_dcsbm.Rd b/man/directed_dcsbm.Rd index de005df..36455cb 100644 --- a/man/directed_dcsbm.Rd +++ b/man/directed_dcsbm.Rd @@ -65,7 +65,7 @@ randomly from a \code{Uniform(0, 1)} distribution. This is subject to change, and may not be reproducible. \code{k_out} defaults to \code{NULL}. You must specify either \code{k_out} and \code{k_in} together, or \code{B}. You may specify all three at once, in which -case the number of outgoing blocks is taken from \code{B}.} +case \code{k_out} must equal \code{nrow(B)}.} \item{k_in}{(mixing matrix) The number of incoming blocks in the blockmodel. Use when you don't want to specify the mixing-matrix by hand. @@ -73,7 +73,7 @@ When \code{k_in} is specified, the elements of \code{B} are drawn randomly from a \code{Uniform(0, 1)} distribution. This is subject to change, and may not be reproducible. \code{k_in} defaults to \code{NULL}. You may specify all three at once, in which -case the number of incoming blocks is taken from \code{B}.} +case \code{k_in} must equal \code{ncol(B)}.} \item{B}{(mixing matrix) A \code{k_out} by \code{k_in} matrix of block connection probabilities. The probability that a node in block \code{i} connects diff --git a/tests/testthat/test-contradictory-args.R b/tests/testthat/test-contradictory-args.R new file mode 100644 index 0000000..972954b --- /dev/null +++ b/tests/testthat/test-contradictory-args.R @@ -0,0 +1,62 @@ +test_that("directed_dcsbm() errors when k_out / k_in contradict B", { + B <- matrix(0.1, 2, 3) + + expect_error( + directed_dcsbm(n = 10, B = B, k_out = 5, k_in = 3), + "`k_out` must equal `nrow(B)`", + fixed = TRUE + ) + expect_error( + directed_dcsbm(n = 10, B = B, k_out = 2, k_in = 5), + "`k_in` must equal `ncol(B)`", + fixed = TRUE + ) + + # agreeing values are still fine, as documented + m <- suppressMessages(directed_dcsbm(n = 10, B = B, k_out = 2, k_in = 3)) + expect_equal(c(m$k1, m$k2), c(2, 3)) +}) + +test_that("mmsbm() rejects redundant arguments and degenerate k", { + B <- diag(2) * 0.5 + 0.1 + + expect_error( + mmsbm(theta = rep(1, 10), k = 3, B = B), + "Must specify only one of `B` and `k`, not both.", + fixed = TRUE + ) + expect_error( + mmsbm(n = 100, theta = rep(1, 10), B = B), + "Must specify only one of `n` and `theta`, not both.", + fixed = TRUE + ) + expect_error( + mmsbm(theta = rep(1, 10), B = matrix(0.5)), + "at least two blocks" + ) + expect_error( + mmsbm(theta = rep(1, 2), B = diag(3) + 0.1), + "`force_pure = TRUE` needs at least as many nodes as blocks", + fixed = TRUE + ) + + # without force_pure, n < k is fine + m <- mmsbm(theta = rep(1, 2), B = diag(3) + 0.1, force_pure = FALSE) + expect_equal(m$n, 2) +}) + +test_that("overlapping_sbm() errors clearly when force_pure and n < k", { + expect_error( + overlapping_sbm(n = 2, B = diag(3) * 0.5 + 0.1), + "`force_pure = TRUE` needs at least as many nodes as blocks", + fixed = TRUE + ) +}) + +test_that("chung_lu() errors when both n and theta are given", { + expect_error( + chung_lu(n = 100, theta = rep(1, 10)), + "Must specify only one of `n` and `theta`, not both.", + fixed = TRUE + ) +})