-
-
Notifications
You must be signed in to change notification settings - Fork 206
feat: retire attr in favor of weights for adjacency matrix functions
#2677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
70aaeed
8792b50
c46a556
e3ee542
f1b705d
97bbcd6
47ef17f
0df5d1f
9d6f0d3
d783eec
c662399
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1760,11 +1760,12 @@ bonpow.dense <- function( | |
| loops = FALSE, | ||
| exponent = 1, | ||
| rescale = FALSE, | ||
| tol = 1e-7 | ||
| tol = 1e-7, | ||
| weights = NULL | ||
| ) { | ||
| ensure_igraph(graph) | ||
|
|
||
| d <- as_adjacency_matrix(graph) | ||
| d <- as_adjacency_matrix(graph, weights = weights, sparse = FALSE) | ||
| if (!loops) { | ||
| diag(d) <- 0 | ||
| } | ||
|
|
@@ -1788,7 +1789,8 @@ bonpow.sparse <- function( | |
| loops = FALSE, | ||
| exponent = 1, | ||
| rescale = FALSE, | ||
| tol = 1e-07 | ||
| tol = 1e-07, | ||
| weights = NULL | ||
| ) { | ||
| ## remove loops if requested | ||
| if (!loops) { | ||
|
|
@@ -1798,13 +1800,13 @@ bonpow.sparse <- function( | |
| vg <- vcount(graph) | ||
|
|
||
| ## sparse adjacency matrix | ||
| d <- as_adjacency_matrix(graph, sparse = TRUE) | ||
| d <- as_adjacency_matrix(graph, weights = weights, sparse = TRUE) | ||
|
|
||
| ## sparse identity matrix | ||
| id <- as(Matrix::Matrix(diag(vg), doDiag = FALSE), "generalMatrix") | ||
|
|
||
| ## solve it | ||
| ev <- Matrix::solve(id - exponent * d, degree(graph, mode = "out"), tol = tol) | ||
| ev <- Matrix::solve(id - exponent * d, Matrix::rowSums(d), tol = tol) | ||
|
|
||
| if (rescale) { | ||
| ev <- ev / sum(ev) | ||
|
|
@@ -1884,6 +1886,7 @@ bonpow.sparse <- function( | |
| #' @param sparse Logical scalar, whether to use sparse matrices for the | ||
| #' calculation. The \sQuote{Matrix} package is required for sparse matrix | ||
| #' support | ||
| #' @inheritParams as_adjacency_matrix | ||
| #' @return A vector, containing the centrality scores. | ||
| #' @note This function was ported (i.e. copied) from the SNA package. | ||
| #' @section Warning : Singular adjacency matrices cause no end of headaches for | ||
|
|
@@ -1936,13 +1939,30 @@ power_centrality <- function( | |
| exponent = 1, | ||
| rescale = FALSE, | ||
| tol = 1e-7, | ||
| sparse = TRUE | ||
| sparse = TRUE, | ||
| weights = NULL | ||
| ) { | ||
| nodes <- as_igraph_vs(graph, nodes) | ||
| if (sparse) { | ||
| res <- bonpow.sparse(graph, nodes, loops, exponent, rescale, tol) | ||
| res <- bonpow.sparse( | ||
| graph, | ||
| nodes, | ||
| loops, | ||
| exponent, | ||
| rescale, | ||
| tol, | ||
| weights = weights | ||
| ) | ||
| } else { | ||
| res <- bonpow.dense(graph, nodes, loops, exponent, rescale, tol) | ||
| res <- bonpow.dense( | ||
| graph, | ||
| nodes, | ||
| loops, | ||
| exponent, | ||
| rescale, | ||
| tol, | ||
| weights = weights | ||
| ) | ||
| } | ||
|
|
||
| if (igraph_opt("add.vertex.names") && is_named(graph)) { | ||
|
|
@@ -1966,25 +1986,7 @@ alpha.centrality.dense <- function( | |
| exo <- rep(exo, length.out = vcount(graph)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does exo mean? In my French-speaking head it's an abbreviation of exercise so I'm confused. |
||
| exo <- matrix(exo, ncol = 1) | ||
|
|
||
| if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { | ||
| ## weights == NULL and there is a "weight" edge attribute | ||
| attr <- "weight" | ||
| } else if (is.null(weights)) { | ||
| ## weights == NULL, but there is no "weight" edge attribute | ||
| attr <- NULL | ||
| } else if (is.character(weights) && length(weights) == 1) { | ||
| ## name of an edge attribute, nothing to do | ||
| attr <- weights | ||
| } else if (!all(is.na(weights))) { | ||
| ## weights != NULL and weights != rep(NA, x) | ||
| graph <- set_edge_attr(graph, "weight", value = as.numeric(weights)) | ||
| attr <- "weight" | ||
| } else { | ||
| ## weights != NULL, but weights == rep(NA, x) | ||
| attr <- NULL | ||
| } | ||
|
|
||
| d <- t(as_adjacency_matrix(graph, attr = attr, sparse = FALSE)) | ||
| d <- t(as_adjacency_matrix(graph, weights = weights, sparse = FALSE)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better name than d? |
||
| if (!loops) { | ||
| diag(d) <- 0 | ||
| } | ||
|
|
@@ -2013,25 +2015,7 @@ alpha.centrality.sparse <- function( | |
| graph <- simplify(graph, remove.multiple = FALSE, remove.loops = TRUE) | ||
| } | ||
|
|
||
| if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { | ||
| ## weights == NULL and there is a "weight" edge attribute | ||
| attr <- "weight" | ||
| } else if (is.null(weights)) { | ||
| ## weights == NULL, but there is no "weight" edge attribute | ||
| attr <- NULL | ||
| } else if (is.character(weights) && length(weights) == 1) { | ||
| ## name of an edge attribute, nothing to do | ||
| attr <- weights | ||
| } else if (!all(is.na(weights))) { | ||
| ## weights != NULL and weights != rep(NA, x) | ||
| graph <- set_edge_attr(graph, "weight", value = as.numeric(weights)) | ||
| attr <- "weight" | ||
| } else { | ||
| ## weights != NULL, but weights == rep(NA, x) | ||
| attr <- NULL | ||
| } | ||
|
|
||
| M <- Matrix::t(as_adjacency_matrix(graph, attr = attr, sparse = TRUE)) | ||
| M <- Matrix::t(as_adjacency_matrix(graph, weights = weights, sparse = TRUE)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or are these single letters sort of convention? |
||
|
|
||
| ## Create an identity matrix | ||
| M2 <- Matrix::sparseMatrix( | ||
|
|
@@ -2082,11 +2066,7 @@ alpha.centrality.sparse <- function( | |
| #' the same factor for every node, or a vector giving the factor for every | ||
| #' vertex. Note that too long vectors will be truncated and too short vectors | ||
| #' will be replicated to match the number of vertices. | ||
| #' @param weights A character scalar that gives the name of the edge attribute | ||
| #' to use in the adjacency matrix. If it is `NULL`, then the | ||
| #' \sQuote{weight} edge attribute of the graph is used, if there is one. | ||
| #' Otherwise, or if it is `NA`, then the calculation uses the standard | ||
| #' adjacency matrix. | ||
| #' @inheritParams as_adjacency_matrix | ||
| #' @param tol Tolerance for near-singularities during matrix inversion, see | ||
| #' [solve()]. | ||
| #' @param sparse Logical scalar, whether to use sparse matrices for the | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since
bonpow.sparse()is not exported, could we give it a better name? Or do we want to keep dot names for unexported functions?