From c607412b3fc616da4d3af311cd14fa09fe255bc9 Mon Sep 17 00:00:00 2001 From: Alex Hayes Date: Tue, 17 Nov 2026 18:42:23 -0800 Subject: [PATCH] Give chung_lu() objects class undirected_chung_lu chung_lu() documented that it returns an `undirected_chung_lu` object, but never set that class, so it printed as a one-block DC-SBM and print.undirected_chung_lu() was unreachable. Pass the subclass through dcsbm(), as planted_partition() does, and tidy the print method's spacing. Co-Authored-By: Claude Opus 5.5 --- R/undirected_chung_lu.R | 15 +++++++-------- man/chung_lu.Rd | 3 ++- tests/testthat/test-chung-lu.R | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 tests/testthat/test-chung-lu.R diff --git a/R/undirected_chung_lu.R b/R/undirected_chung_lu.R index 56f5fbe..c589043 100644 --- a/R/undirected_chung_lu.R +++ b/R/undirected_chung_lu.R @@ -34,7 +34,8 @@ #' @inheritParams undirected_factor_model #' @inheritParams dcsbm #' -#' @return An `undirected_chung_lu` S3 object, a subclass of [dcsbm()]. +#' @return An `undirected_chung_lu` S3 object, a subclass of the +#' `undirected_dcsbm` object returned by [dcsbm()], with a single block. #' #' @export #' @family undirected graphs @@ -95,7 +96,8 @@ chung_lu <- function( poisson_edges = poisson_edges, force_identifiability = force_identifiability, allow_self_loops = allow_self_loops, - ... + ..., + subclass = "undirected_chung_lu" ) # don't validate the chung-ln-ness because there is no low level @@ -107,17 +109,14 @@ chung_lu <- function( #' @export print.undirected_chung_lu <- function(x, ...) { cat(glue("Undirected Chung-Lu Graph\n", .trim = FALSE)) - cat(glue( - "-------------------------------------------------\n\n", - .trim = FALSE - )) + cat(glue("-------------------------\n\n", .trim = FALSE)) sorted <- if (x$sorted) "arranged by block" else "not arranged by block" - cat(glue("Nodes (n): {x$n} ({sorted})\n", .trim = FALSE)) + cat(glue("Nodes (n): {x$n} ({sorted})\n\n", .trim = FALSE)) cat("Traditional Chung-Lu parameterization:\n\n") - cat("Degree heterogeneity (theta):", dim_and_class(x$theta), "\n") + cat("Degree heterogeneity (theta):", dim_and_class(x$theta), "\n\n") cat("Factor model parameterization:\n\n") cat("X:", dim_and_class(x$X), "\n") diff --git a/man/chung_lu.Rd b/man/chung_lu.Rd index 2916ea0..1b046ec 100644 --- a/man/chung_lu.Rd +++ b/man/chung_lu.Rd @@ -72,7 +72,8 @@ to \code{FALSE}, since this behavior can be surprise when \code{theta} is set to a vector of all ones to recover the SBM case.} } \value{ -An \code{undirected_chung_lu} S3 object, a subclass of \code{\link[=dcsbm]{dcsbm()}}. +An \code{undirected_chung_lu} S3 object, a subclass of the +\code{undirected_dcsbm} object returned by \code{\link[=dcsbm]{dcsbm()}}, with a single block. } \description{ To specify a Chung-Lu graph, you must specify diff --git a/tests/testthat/test-chung-lu.R b/tests/testthat/test-chung-lu.R new file mode 100644 index 0000000..cc2e6cf --- /dev/null +++ b/tests/testthat/test-chung-lu.R @@ -0,0 +1,15 @@ +test_that("chung_lu() returns an undirected_chung_lu object", { + set.seed(6) + m <- chung_lu(theta = rexp(15) + 0.5) + + expect_s3_class( + m, + c("undirected_chung_lu", "undirected_dcsbm", "undirected_factor_model"), + exact = TRUE + ) + + out <- paste(capture.output(print(m)), collapse = "\n") + expect_match(out, "Undirected Chung-Lu Graph", fixed = TRUE) + expect_match(out, "Degree heterogeneity (theta): 15 [numeric]", fixed = TRUE) + expect_no_match(out, "Degree-Corrected", fixed = TRUE) +})