Skip to content
Draft
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
8 changes: 4 additions & 4 deletions R/directed_dcsbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -905,13 +905,13 @@ directed_dcsbm <- function(
if (k_out > 1) {
X <- sparse.model.matrix(~ z_out + 0)
} else {
X <- Matrix(1, nrow = n, ncol = 1)
X <- Matrix(1, nrow = n, ncol = 1, doDiag = FALSE)
}

if (k_in > 1) {
Y <- sparse.model.matrix(~ z_in + 0)
} else {
Y <- Matrix(1, nrow = n, ncol = 1)
Y <- Matrix(1, nrow = n, ncol = 1, doDiag = FALSE)
}

X <- set_membership_weights(X, as.numeric(theta_out))
Expand Down Expand Up @@ -949,13 +949,13 @@ directed_dcsbm <- function(
if (k_out > 1) {
X <- sparse.model.matrix(~ z_out + 0)
} else {
X <- Matrix(1, nrow = n, ncol = 1)
X <- Matrix(1, nrow = n, ncol = 1, doDiag = FALSE)
}

if (k_in > 1) {
Y <- sparse.model.matrix(~ z_in + 0)
} else {
Y <- Matrix(1, nrow = n, ncol = 1)
Y <- Matrix(1, nrow = n, ncol = 1, doDiag = FALSE)
}

if (force_identifiability) {
Expand Down
4 changes: 2 additions & 2 deletions R/directed_erdos_renyi.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ directed_erdos_renyi <- function(
poisson_edges = TRUE,
allow_self_loops = TRUE
) {
X <- Matrix(1, nrow = n, ncol = 1)
Y <- Matrix(1, nrow = n, ncol = 1)
X <- Matrix(1, nrow = n, ncol = 1, doDiag = FALSE)
Y <- Matrix(1, nrow = n, ncol = 1, doDiag = FALSE)

# the scaling arguments arrive via `...`, so look for them by name (a
# bare `expected_in_degree` would find the generic function)
Expand Down
2 changes: 1 addition & 1 deletion R/undirected_dcsbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ dcsbm <- function(
if (k > 1) {
X <- sparse.model.matrix(~ z + 0)
} else {
X <- Matrix(1, nrow = n, ncol = 1)
X <- Matrix(1, nrow = n, ncol = 1, doDiag = FALSE)
}

if (force_identifiability) {
Expand Down
2 changes: 1 addition & 1 deletion R/undirected_erdos_renyi.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ erdos_renyi <- function(
poisson_edges = TRUE,
allow_self_loops = TRUE
) {
X <- Matrix(1, nrow = n, ncol = 1)
X <- Matrix(1, nrow = n, ncol = 1, doDiag = FALSE)

# `expected_degree` and `expected_density` arrive via `...`, so look for
# them by name (a bare `expected_degree` would find the generic function)
Expand Down
4 changes: 2 additions & 2 deletions R/undirected_mmsbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ mmsbm <- function(

if (sort_nodes) {
idx <- sort_by_all_columns_idx(X)
X <- X[idx, ]
Z <- Z[idx, ]
X <- X[idx, , drop = FALSE]
Z <- Z[idx, , drop = FALSE]
theta <- theta[idx]
}

Expand Down
3 changes: 2 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ sort_by_all_columns <- function(X) {
args <- as.list(as.data.frame(as.matrix(X)))
args$decreasing <- TRUE

X <- X[do.call(order, args), ]
# drop = FALSE keeps a matrix when there is a single node or block
X <- X[do.call(order, args), , drop = FALSE]
X
}

Expand Down
43 changes: 43 additions & 0 deletions tests/testthat/test-single-node.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# models with a single node, or a single block, must keep matrix-valued
# X, Y and Z

test_that("dcsbm() and sbm() work with a single node", {
m <- dcsbm(theta = 1, B = matrix(1))
expect_s4_class(m$X, "Matrix")
expect_equal(dim(m$X), c(1, 1))
expect_equal(expected_edges(m), 1)
expect_equal(dim(sample_sparse(m)), c(1, 1))

m <- dcsbm(theta = 2, B = matrix(1), sort_nodes = FALSE)
expect_equal(unname(as.matrix(m$X)), matrix(2))

set.seed(1)
m <- sbm(n = 1, B = diag(2))
expect_s4_class(m$X, "Matrix")
expect_equal(dim(m$X), c(1, 2))
expect_equal(dim(sample_sparse(m)), c(1, 1))

m <- erdos_renyi(n = 1, p = 0.5)
expect_equal(dim(m$X), c(1, 1))
})

test_that("directed models work with a single node", {
m <- directed_dcsbm(
theta_out = 1, theta_in = 1, B = matrix(1),
force_identifiability = FALSE
)
expect_equal(dim(m$X), c(1, 1))
expect_equal(dim(m$Y), c(1, 1))
expect_equal(dim(sample_sparse(m)), c(1, 1))

m <- directed_erdos_renyi(n = 1, p = 0.5)
expect_equal(dim(m$X), c(1, 1))
})

test_that("overlapping_sbm() keeps Z a matrix with a single block", {
set.seed(1)
m <- overlapping_sbm(n = 10, B = matrix(0.5), pi = 1)
expect_s4_class(m$Z, "Matrix")
expect_equal(dim(m$Z), c(10, 1))
expect_equal(dim(m$X), c(10, 1))
})
Loading