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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import(ggplot2)
importFrom(RSpectra,eigs_sym)
importFrom(RSpectra,svds)
importFrom(glue,glue)
importFrom(rlang,.data)
importFrom(stats,rmultinom)
importFrom(stats,rpois)
importFrom(tibble,tibble)
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)

- `sample_edgelist()`, `sample_igraph()` and `sample_tidygraph()` gain a `multigraph` argument. The default, `multigraph = TRUE`, keeps the current behavior: Poisson graphs are returned as multigraphs with repeated edges. With `multigraph = FALSE`, repeated edges are collapsed into a single edge with an integer `weight` attribute (an edgelist column, or an `igraph` edge attribute) that equals the corresponding entry of `sample_sparse()`. In undirected graphs `(i, j)` and `(j, i)` are collapsed together. The `weight` attribute is always present when `multigraph = FALSE`, so Bernoulli graphs (`poisson_edges = FALSE`) get weights of 1 (#30).

- 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:
Expand Down
1 change: 1 addition & 0 deletions R/fastRG-package.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' @import Matrix
#' @importFrom glue glue
#' @importFrom tibble tibble
#' @importFrom rlang .data
NULL

# The following block is used by usethis to automatically manage
Expand Down
62 changes: 55 additions & 7 deletions R/sample_edgelist.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,25 @@
#'
#' @param ... Ignored. Do not use.
#'
#' @param multigraph Logical scalar, only relevant for Poisson graphs
#' (`poisson_edges = TRUE`), where a pair of nodes can be connected by
#' more than one edge. When `TRUE` (the default), such repeated edges are
#' represented as a multigraph, that is, as repeated edges (or repeated
#' rows of an edgelist) without any edge weights. When `FALSE`, repeated
#' edges between the same pair of nodes are collapsed into a single edge
#' with an integer `weight` attribute that counts the number of times
#' the edge was sampled. This weight is the corresponding entry of the
#' adjacency matrix returned by [sample_sparse()]. In undirected graphs,
#' the edges `(i, j)` and `(j, i)` are the same edge and are collapsed
#' together. When `multigraph = FALSE` the `weight` attribute is always
#' present, even for Bernoulli graphs (`poisson_edges = FALSE`), where
#' every weight is `1`.
#'
#' @return A single realization of a random Poisson (or Bernoulli)
#' Dot Product Graph, represented as a [tibble::tibble()] with two
#' integer columns, `from` and `to`.
#' integer columns, `from` and `to`. When `multigraph = FALSE`, there
#' is an additional integer column `weight` and each `(from, to)` pair
#' appears at most once.
#'
#' **NOTE**: Indices for isolated nodes will not appear in the edgelist!
#' This can lead to issues if you construct network objects from the
Expand Down Expand Up @@ -92,6 +108,13 @@
#'
#' sample_tidygraph(ufm)
#'
#' ### weighted graphs instead of multigraphs ------------
#'
#' # collapse repeated edges into a single edge with an integer weight
#'
#' sample_edgelist(ufm, multigraph = FALSE)
#' sample_igraph(ufm, multigraph = FALSE)
#'
#' ##### directed examples ----------------------------
#'
#' n2 <- 100
Expand Down Expand Up @@ -145,17 +168,20 @@
#'
sample_edgelist <- function(
factor_model,
...
...,
multigraph = TRUE
) {
rlang::check_dots_unnamed()
check_multigraph(multigraph)
UseMethod("sample_edgelist")
}

#' @rdname sample_edgelist
#' @export
sample_edgelist.undirected_factor_model <- function(
factor_model,
...
...,
multigraph = TRUE
) {
X <- factor_model$X
n <- factor_model$n
Expand Down Expand Up @@ -191,14 +217,22 @@ sample_edgelist.undirected_factor_model <- function(
edgelist <- dplyr::filter(edgelist, to != from)
}

# from <= to for every edge here (including the self-loops added above),
# so (i, j) and (j, i) are already the same row and collapsing by
# (from, to) counts undirected edges, matching sample_sparse()
if (!multigraph) {
edgelist <- collapse_multiedges(edgelist)
}

edgelist
}

#' @rdname sample_edgelist
#' @export
sample_edgelist.directed_factor_model <- function(
factor_model,
...
...,
multigraph = TRUE
) {
X <- factor_model$X
S <- factor_model$S
Expand All @@ -210,7 +244,8 @@ sample_edgelist.directed_factor_model <- function(
Y,
TRUE,
factor_model$poisson_edges,
factor_model$allow_self_loops
factor_model$allow_self_loops,
multigraph = multigraph
)
}

Expand Down Expand Up @@ -256,6 +291,7 @@ sample_edgelist.directed_factor_model <- function(
#'
#' @param ... Ignored, for generic consistency only.
#'
#' @inheritParams sample_edgelist
#' @inherit sample_edgelist return details references
#'
#' @export
Expand Down Expand Up @@ -285,13 +321,15 @@ sample_edgelist.matrix <- function(
directed,
poisson_edges,
allow_self_loops,
...
...,
multigraph = TRUE
) {
X <- factor_model

stopifnot(is.logical(directed))
stopifnot(is.logical(poisson_edges))
stopifnot(is.logical(allow_self_loops))
check_multigraph(multigraph)

n <- nrow(X)
d <- nrow(Y)
Expand All @@ -310,7 +348,13 @@ sample_edgelist.matrix <- function(

if (m == 0) {
# match the type of a non-empty edgelist: a tibble of integers
return(tibble(from = integer(0), to = integer(0)))
edgelist <- tibble(from = integer(0), to = integer(0))

if (!multigraph) {
return(collapse_multiedges(edgelist))
}

return(edgelist)
}

# varpi in section 2.4 of Rohe et al (2017), the number of
Expand Down Expand Up @@ -430,6 +474,10 @@ sample_edgelist.matrix <- function(
edgelist <- dplyr::filter(edgelist, to != from)
}

if (!multigraph) {
edgelist <- collapse_multiedges(edgelist)
}

edgelist
}

Expand Down
43 changes: 34 additions & 9 deletions R/sample_igraph.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
#'
#' @inherit sample_edgelist params details references examples description
#'
#' @return An [igraph::igraph()] object that is possibly a
#' multigraph (that is, we take there to be multiple edges
#' rather than weighted edges).
#' @return An [igraph::igraph()] object. For Poisson graphs
#' (`poisson_edges = TRUE`) with `multigraph = TRUE` (the default), this
#' is possibly a multigraph (that is, we take there to be multiple edges
#' rather than weighted edges). With `multigraph = FALSE`, repeated edges
#' are instead collapsed into a single edge, and every edge has an integer
#' `weight` attribute equal to the corresponding entry of the
#' [sample_sparse()] adjacency matrix (all weights are `1` when
#' `poisson_edges = FALSE`).
#'
#' When `factor_model` is **undirected**:
#'
Expand All @@ -26,9 +31,11 @@
#'
sample_igraph <- function(
factor_model,
...
...,
multigraph = TRUE
) {
rlang::check_dots_unnamed()
check_multigraph(multigraph)

rlang::check_installed("igraph", "to return graphs as `igraph` objects.")

Expand All @@ -39,9 +46,10 @@ sample_igraph <- function(
#' @export
sample_igraph.undirected_factor_model <- function(
factor_model,
...
...,
multigraph = TRUE
) {
edgelist <- sample_edgelist(factor_model, ...)
edgelist <- sample_edgelist(factor_model, ..., multigraph = multigraph)
nodes <- tibble(name = 1:nrow(factor_model$X))
igraph::graph_from_data_frame(edgelist, directed = FALSE, vertices = nodes)
}
Expand All @@ -50,10 +58,11 @@ sample_igraph.undirected_factor_model <- function(
#' @export
sample_igraph.directed_factor_model <- function(
factor_model,
...
...,
multigraph = TRUE
) {
if (factor_model$n == factor_model$d) {
edgelist <- sample_edgelist(factor_model, ...)
edgelist <- sample_edgelist(factor_model, ..., multigraph = multigraph)
nodes <- tibble(name = 1:nrow(factor_model$X))
one_mode <- igraph::graph_from_data_frame(
edgelist,
Expand All @@ -67,5 +76,21 @@ sample_igraph.directed_factor_model <- function(
# a bipartite graph rather than using the usual one-mode tools

A <- sample_sparse(factor_model, ...)
igraph::graph_from_biadjacency_matrix(A, directed = FALSE, multiple = TRUE)

if (multigraph) {
return(
igraph::graph_from_biadjacency_matrix(A, directed = FALSE, multiple = TRUE)
)
}

bipartite <- igraph::graph_from_biadjacency_matrix(
A,
directed = FALSE,
weighted = TRUE
)

# entries of A are counts stored as doubles, keep weights integer to match
# sample_edgelist(multigraph = FALSE)
igraph::E(bipartite)$weight <- as.integer(igraph::E(bipartite)$weight)
bipartite
}
25 changes: 17 additions & 8 deletions R/sample_tidygraph.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
#'
#' @inherit sample_edgelist params details references examples description
#'
#' @return A [tidygraph::tbl_graph()] object that is possibly a
#' multigraph (that is, we take there to be multiple edges
#' rather than weighted edges).
#' @return A [tidygraph::tbl_graph()] object. For Poisson graphs
#' (`poisson_edges = TRUE`) with `multigraph = TRUE` (the default), this
#' is possibly a multigraph (that is, we take there to be multiple edges
#' rather than weighted edges). With `multigraph = FALSE`, repeated edges
#' are instead collapsed into a single edge, and the edge data has an
#' integer `weight` column equal to the corresponding entry of the
#' [sample_sparse()] adjacency matrix (all weights are `1` when
#' `poisson_edges = FALSE`).
#'
#' When `factor_model` is **undirected**:
#'
Expand All @@ -26,9 +31,11 @@
#'
sample_tidygraph <- function(
factor_model,
...
...,
multigraph = TRUE
) {
rlang::check_dots_unnamed()
check_multigraph(multigraph)
rlang::check_installed("igraph", "to return graphs as `tidygraph` objects.")

UseMethod("sample_tidygraph")
Expand All @@ -38,18 +45,20 @@ sample_tidygraph <- function(
#' @export
sample_tidygraph.undirected_factor_model <- function(
factor_model,
...
...,
multigraph = TRUE
) {
ig <- sample_igraph(factor_model, ...)
ig <- sample_igraph(factor_model, ..., multigraph = multigraph)
tidygraph::as_tbl_graph(ig, directed = FALSE)
}

#' @rdname sample_tidygraph
#' @export
sample_tidygraph.directed_factor_model <- function(
factor_model,
...
...,
multigraph = TRUE
) {
ig <- sample_igraph(factor_model, ...)
ig <- sample_igraph(factor_model, ..., multigraph = multigraph)
tidygraph::as_tbl_graph(ig, directed = TRUE)
}
33 changes: 33 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ set_membership_weights <- function(X, theta) {
X
}

check_multigraph <- function(multigraph, call = rlang::caller_env()) {
if (!rlang::is_bool(multigraph)) {
rlang::abort(
paste0(
"`multigraph` must be a single `TRUE` or `FALSE`, not ",
obj_type(multigraph),
"."
),
call = call
)
}
invisible(multigraph)
}

obj_type <- function(x) {
if (is.null(x)) {
return("`NULL`")
}
if (length(x) != 1) {
return(paste0("a ", class(x)[1], " vector of length ", length(x)))
}
paste0("`", deparse(x), "`")
}

# collapse repeated (from, to) rows of an edgelist into a single row with
# an integer `weight` column counting the repeats. for undirected
# edgelists, callers must ensure from <= to beforehand so that (i, j) and
# (j, i) are collapsed together
collapse_multiedges <- function(edgelist) {
edgelist <- tibble::as_tibble(edgelist)
dplyr::count(edgelist, .data$from, .data$to, name = "weight")
}

check_flag <- function(x, arg) {
if (!is.logical(x) || length(x) != 1 || is.na(x)) {
stop(glue("`{arg}` must be `TRUE` or `FALSE`."), call. = FALSE)
Expand Down
Loading
Loading