Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.

- `overlapping_sbm()` bug fixes:
- `poisson_edges` and `allow_self_loops` were silently ignored, so every overlapping SBM was a Poisson graph with self-loops. They are now passed on, and Bernoulli overlapping SBMs follow the "noisy-OR" edge probability `1 - prod (1 - B[a, b])^(Z[i, a] Z[j, b])`.
- Specifying both `k` and `B` is now an error, as in `dcsbm()`.
Expand Down
5 changes: 2 additions & 3 deletions R/sample_edgelist.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions R/sample_sparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
77 changes: 77 additions & 0 deletions tests/testthat/test-empty-graphs.R
Original file line number Diff line number Diff line change
@@ -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"))
}
})
Loading