Skip to content

Commit

Permalink
Add efficiency algos
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Nov 30, 2023
1 parent e687cd2 commit 4a061c6
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 2 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export(graph_clique_count)
export(graph_clique_num)
export(graph_component_count)
export(graph_diameter)
export(graph_efficiency)
export(graph_girth)
export(graph_is_bipartite)
export(graph_is_chordal)
Expand Down Expand Up @@ -301,6 +302,7 @@ export(node_diversity)
export(node_dominator)
export(node_eccentricity)
export(node_effective_network_size)
export(node_efficiency)
export(node_fareness_impact)
export(node_is_adjacent)
export(node_is_center)
Expand Down Expand Up @@ -519,6 +521,7 @@ importFrom(igraph,eulerian_cycle)
importFrom(igraph,eulerian_path)
importFrom(igraph,feedback_arc_set)
importFrom(igraph,girth)
importFrom(igraph,global_efficiency)
importFrom(igraph,gorder)
importFrom(igraph,graph_attr)
importFrom(igraph,graph_from_adj_list)
Expand All @@ -544,6 +547,7 @@ importFrom(igraph,is_simple)
importFrom(igraph,is_subgraph_isomorphic_to)
importFrom(igraph,knn)
importFrom(igraph,largest_component)
importFrom(igraph,local_efficiency)
importFrom(igraph,local_scan)
importFrom(igraph,make_chordal_ring)
importFrom(igraph,make_de_bruijn_graph)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
* Added `group_leiden()` to interface with `cluster_leiden()` in igraph
* Added `group_fluid()` to interface with `cluster_fluid_communities()` in igraph
* Added `edge_is_feedback_arc()` to interface with `feedback_arc_set()` in igraph
* Added `graph_efficiency()` and `node_effeciency()` interfacing with
`global_efficiency()` and `local_efficiency()` in igraph

# tidygraph 1.2.3

Expand Down
10 changes: 10 additions & 0 deletions R/graph_measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,13 @@ graph_modularity <- function(group, weights = NULL) {
weights <- eval_tidy(weights, .E(focused = FALSE))
modularity(graph, group, weights)
}
#' @describeIn graph_measures Calculate the global efficiency of the graph
#' @importFrom igraph global_efficiency
#' @importFrom rlang enquo eval_tidy
#' @export
graph_efficiency <- function(weights = NULL, directed = TRUE) {
graph <- .G()
weights <- enquo(weights)
weights <- eval_tidy(weights, .E(focused = FALSE)) %||% NA
global_efficiency(graph, weights = weights, directed = directed)
}
15 changes: 14 additions & 1 deletion R/node.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ node_is_connected <- function(nodes, mode = 'all', any = FALSE) {
#' into the class of [centrality] measures. For lack of a better place they are
#' collected under the `node_*` umbrella of functions.
#'
#' @inheritParams node_types
#' @param mode How edges are treated. In `node_coreness()` it chooses which kind
#' of coreness measure to calculate. In `node_efficiency()` it defines how the
#' local neighborhood is created
#' @param weights The weights to use for each node during calculation
#'
#' @return A numeric vector of the same length as the number of nodes in the
Expand Down Expand Up @@ -233,6 +235,17 @@ node_diversity <- function(weights) {
}
diversity(graph, weights = weights, vids = focus_ind(graph, 'nodes'))
}
#' @describeIn node_measures measures the local efficiency around each node. See [igraph::local_efficiency()]
#' @importFrom rlang enquo eval_tidy
#' @importFrom igraph local_efficiency
#' @export
node_efficiency <- function(weights = NULL, directed = TRUE, mode = 'all') {
expect_nodes()
graph <- .G()
weights <- enquo(weights)
weights <- eval_tidy(weights, .E()) %||% NA
local_efficiency(graph, focus_ind(graph, 'nodes'), weights, directed, mode)
}
#' @describeIn node_measures measures Valente's Bridging measures for detecting structural bridges (`influenceR`)
#' @export
node_bridging_score <- function() {
Expand Down
5 changes: 5 additions & 0 deletions man/graph_measures.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion man/node_measures.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/test-graph_measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test_that("graph measures returns scalars", {
expect_length(graph_reciprocity(), 1)
expect_length(graph_size(), 1)
expect_length(graph_modularity(type), 1)
expect_length(graph_efficiency(), 1)
.graph_context$clear()
gr <- create_ring(5, TRUE) %>%
mutate(type = c(1, 1, 1, 2, 2))
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-node_measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test_that("Node measures return corrent type", {
expect_type(get_val(gr, node_constraint()), 'double')
expect_type(get_val(gr, node_coreness()), 'double')
expect_type(get_val(gr_u, node_diversity(w)), 'double')
expect_type(get_val(gr_u, node_efficiency()), 'double')
expect_type(get_val(tree, node_dominator(node_is_root())), 'double')
expect_type(get_val(gr, node_eccentricity()), 'double')
expect_type(get_val(tree, node_topo_order()), 'integer')
Expand All @@ -31,6 +32,7 @@ test_that("Node measures return correct length", {
expect_length(get_val(gr, node_constraint()), igraph::gorder(gr))
expect_length(get_val(gr, node_coreness()), igraph::gorder(gr))
expect_length(get_val(gr_u, node_diversity(w)), igraph::gorder(gr))
expect_length(get_val(gr_u, node_efficiency()), igraph::gorder(gr))
expect_length(get_val(tree, node_dominator(node_is_root())), igraph::gorder(tree))
expect_length(get_val(gr, node_eccentricity()), igraph::gorder(gr))
expect_length(get_val(tree, node_topo_order()), igraph::gorder(tree))
Expand All @@ -51,6 +53,7 @@ test_that("Node measures return correct length for focus", {
expect_length(get_val(gr, node_constraint()), 2)
expect_length(get_val(gr, node_coreness()), 2)
expect_length(get_val(gr_u, node_diversity(w)), 2)
expect_length(get_val(gr_u, node_efficiency()), 2)
expect_length(get_val(tree, node_dominator(node_is_root())), 2)
expect_length(get_val(gr, node_eccentricity()), 2)
expect_length(get_val(tree, node_topo_order()), 2)
Expand All @@ -66,6 +69,7 @@ test_that("Node measures requires active nodes", {
expect_error(get_val(gr, node_constraint()))
expect_error(get_val(gr, node_coreness()))
expect_error(get_val(gr_u, node_diversity(w)))
expect_error(get_val(gr_u, node_efficiency()))
expect_error(get_val(tree, node_dominator(node_is_root())))
expect_error(get_val(gr, node_eccentricity()))
expect_error(get_val(tree, node_topo_order()))
Expand Down

0 comments on commit 4a061c6

Please sign in to comment.