From 4951ee2118e780df93790e9455d5c46c8aa39542 Mon Sep 17 00:00:00 2001 From: Alex Hayes Date: Tue, 17 Nov 2026 13:56:51 -0800 Subject: [PATCH] Return type-stable empty graphs from all samplers sample_sparse() built an empty directed matrix from explicit zeros at 1:n and 1:d, which errors whenever n != d, breaking sample_sparse(), sample_igraph() and sample_tidygraph() on rectangular models with no sampled edges. Build empty matrices from zero-length index vectors instead, and make the empty undirected case symmetric like the non-empty one. sample_edgelist() returned a 0-row base matrix of doubles when no edges were sampled; it now returns a tibble with integer from/to columns, matching non-empty edgelists. Co-Authored-By: Claude Opus 5.5 --- NEWS.md | 2 + R/sample_edgelist.R | 5 +- R/sample_sparse.R | 19 +++++++- tests/testthat/test-empty-graphs.R | 77 ++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 tests/testthat/test-empty-graphs.R diff --git a/NEWS.md b/NEWS.md index 47cfdee..9ae7c19 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # fastRG (development version) +- Fixed `sample_sparse()`, `sample_igraph()` and `sample_tidygraph()` erroring on directed rectangular models (`n != d`) when no edges are sampled. Empty graphs now have the same output types as non-empty ones: `sample_edgelist()` returns a tibble with integer `from` and `to` columns instead of a base matrix of doubles, and `sample_sparse()` returns a symmetric `dsCMatrix` for undirected models. + - fastRG now requires R >= 4.4.0. irlba 2.4.1, which fastRG uses in tests and vignettes, fails on earlier R versions because `is.atomic(NULL)` changed in R 4.4.0. - Fixed undersampling of self-loops in undirected models. Since 0.4.0, the diagonal of `A` was sampled at half the intended rate, so `E[A | X, S]` did not equal `X S X'` on the diagonal. `sample_edgelist.undirected_factor_model()` now adds independent Poisson self-loops at the missing rate when `allow_self_loops = TRUE`, so `E[A | X, S] = X S X'` holds exactly. This only affects sampling: `S`, `expected_*()` and population spectra are unchanged, and so are the off-diagonal fixes from #19 and #31. Graphs sampled with a given seed will differ from previous versions (#46). diff --git a/R/sample_edgelist.R b/R/sample_edgelist.R index dad9827..cfe2ef0 100644 --- a/R/sample_edgelist.R +++ b/R/sample_edgelist.R @@ -309,9 +309,8 @@ sample_edgelist.matrix <- function( m <- rpois(n = 1, lambda = expected_edges) if (m == 0) { - edge_list <- matrix(0, nrow = 0, ncol = 2) - colnames(edge_list) <- c("from", "to") - return(edge_list) + # match the type of a non-empty edgelist: a tibble of integers + return(tibble(from = integer(0), to = integer(0))) } # varpi in section 2.4 of Rohe et al (2017), the number of diff --git a/R/sample_sparse.R b/R/sample_sparse.R index f7b2fe7..745d1aa 100644 --- a/R/sample_sparse.R +++ b/R/sample_sparse.R @@ -47,7 +47,14 @@ sample_sparse.undirected_factor_model <- function( n <- factor_model$n if (nrow(el) == 0) { - return(sparseMatrix(1:n, 1:n, x = 0, dims = c(n, n))) + A <- sparseMatrix( + i = integer(0), + j = integer(0), + x = numeric(0), + dims = c(n, n), + symmetric = TRUE + ) + return(A) } if (factor_model$poisson_edges) { @@ -101,7 +108,15 @@ sample_sparse.directed_factor_model <- function( d <- factor_model$d if (nrow(edgelist) == 0) { - return(sparseMatrix(1:n, 1:d, x = 0, dims = c(n, d))) + # NOTE: n and d can differ, so we can't build an empty matrix from + # explicit zeros on the diagonal (1:n and 1:d have different lengths) + A <- sparseMatrix( + i = integer(0), + j = integer(0), + x = numeric(0), + dims = c(n, d) + ) + return(A) } if (factor_model$poisson_edges) { diff --git a/tests/testthat/test-empty-graphs.R b/tests/testthat/test-empty-graphs.R new file mode 100644 index 0000000..b64a774 --- /dev/null +++ b/tests/testthat/test-empty-graphs.R @@ -0,0 +1,77 @@ +empty_rectangular <- function() { + directed_factor_model( + X = matrix(1, 20, 2), + S = diag(2), + Y = matrix(1, 15, 2), + expected_density = 1e-9 + ) +} + +test_that("sample_sparse() handles empty rectangular directed graphs", { + set.seed(27) + A <- sample_sparse(empty_rectangular()) + + expect_s4_class(A, "dgCMatrix") + expect_equal(dim(A), c(20, 15)) + expect_equal(Matrix::nnzero(A), 0) +}) + +test_that("sample_igraph() and sample_tidygraph() handle empty rectangular graphs", { + set.seed(27) + g <- sample_igraph(empty_rectangular()) + expect_equal(igraph::vcount(g), 35) + expect_equal(igraph::ecount(g), 0) + + set.seed(27) + tg <- sample_tidygraph(empty_rectangular()) + expect_equal(igraph::vcount(tg), 35) + expect_equal(igraph::ecount(tg), 0) +}) + +test_that("sample_sparse() returns the same class for empty and non-empty graphs", { + X <- matrix(1, 20, 2) + + set.seed(27) + empty_dir <- sample_sparse( + directed_factor_model(X, diag(2), X, expected_density = 1e-9) + ) + full_dir <- sample_sparse( + directed_factor_model(X, diag(2), X, expected_density = 0.5) + ) + expect_equal(Matrix::nnzero(empty_dir), 0) + expect_equal(class(empty_dir), class(full_dir)) + + empty_undir <- sample_sparse( + undirected_factor_model(X, diag(2), expected_density = 1e-9) + ) + full_undir <- sample_sparse( + undirected_factor_model(X, diag(2), expected_density = 0.5) + ) + expect_equal(Matrix::nnzero(empty_undir), 0) + expect_equal(dim(empty_undir), c(20, 20)) + expect_equal(class(empty_undir), class(full_undir)) +}) + +test_that("sample_edgelist() returns the same type for empty and non-empty graphs", { + X <- matrix(1, 20, 2) + Y <- matrix(1, 15, 2) + + models <- list( + undirected = function(d) undirected_factor_model(X, diag(2), expected_density = d), + directed = function(d) directed_factor_model(X, diag(2), X, expected_density = d), + rectangular = function(d) directed_factor_model(X, diag(2), Y, expected_density = d) + ) + + for (model in models) { + set.seed(27) + empty <- sample_edgelist(model(1e-9)) + full <- sample_edgelist(model(0.5)) + + expect_equal(nrow(empty), 0) + expect_gt(nrow(full), 0) + expect_s3_class(empty, "tbl_df") + expect_identical(class(empty), class(full)) + expect_identical(vapply(empty, typeof, ""), vapply(full, typeof, "")) + expect_identical(vapply(empty, typeof, ""), c(from = "integer", to = "integer")) + } +})