Skip to content

Commit

Permalink
igraph 2.0.0 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Jan 30, 2024
1 parent 4258fdb commit d1fc8ba
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 48 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ BugReports: https://github.com/thomasp85/tidygraph/issues
Imports:
cli,
dplyr (>= 0.8.5),
igraph (>= 1.5.0),
igraph (>= 2.0.0),
lifecycle,
magrittr,
pillar,
Expand All @@ -45,5 +45,5 @@ LinkingTo:
cpp11
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Config/testthat/edition: 3
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ S3method(sample_n,tbl_graph)
S3method(select,morphed_tbl_graph)
S3method(select,tbl_graph)
S3method(semi_join,tbl_graph)
S3method(set_graph_data,grouped_tbl_graph)
S3method(set_graph_data,tbl_graph)
S3method(slice,morphed_tbl_graph)
S3method(slice,tbl_graph)
S3method(slice_head,morphed_tbl_graph)
Expand Down Expand Up @@ -461,6 +463,7 @@ importFrom(igraph,add_edges)
importFrom(igraph,add_vertices)
importFrom(igraph,adjacent_vertices)
importFrom(igraph,alpha_centrality)
importFrom(igraph,arpack_defaults)
importFrom(igraph,articulation_points)
importFrom(igraph,as.igraph)
importFrom(igraph,as_adjacency_matrix)
Expand Down Expand Up @@ -537,7 +540,6 @@ importFrom(igraph,has_eulerian_cycle)
importFrom(igraph,has_eulerian_path)
importFrom(igraph,hub_score)
importFrom(igraph,induced_subgraph)
importFrom(igraph,is.directed)
importFrom(igraph,is_bipartite)
importFrom(igraph,is_chordal)
importFrom(igraph,is_connected)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# tidygraph (development version)

* Fix a bug in `tbl_graph()` when edge `to` and `from` where encoded as factors
* Secure compitability with igraph 2.0.0

# tidygraph 1.3.0

Expand Down
4 changes: 2 additions & 2 deletions R/arrange.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ arrange.morphed_tbl_graph <- function(.data, ...) {
#' @export
dplyr::arrange

#' @importFrom igraph is.directed as_data_frame
#' @importFrom igraph is_directed as_data_frame
permute_edges <- function(graph, order) {
graph_mod <- as_data_frame(graph, what = 'both')
graph_mod$edges <- graph_mod$edges[order, ]
as_tbl_graph(graph_mod, directed = is.directed(graph))
as_tbl_graph(graph_mod, directed = is_directed(graph))
}
#' @importFrom igraph permute
permute_nodes <- function(graph, order) {
Expand Down
17 changes: 9 additions & 8 deletions R/centrality.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ centrality_alpha <- function(weights = NULL, alpha = 1, exo = 1, tol = 1e-7, loo
alpha_centrality(graph = graph, nodes = focus_ind(graph, 'nodes'), alpha = alpha, exo = exo, weights = weights, tol = tol, loops = loops)
}
#' @describeIn centrality Wrapper for [igraph::authority_score()]
#' @importFrom igraph authority_score
#' @importFrom igraph authority_score arpack_defaults
#' @export
centrality_authority <- function(weights = NULL, scale = TRUE, options = igraph::arpack_defaults) {
centrality_authority <- function(weights = NULL, scale = TRUE, options = arpack_defaults()) {
expect_nodes()
weights <- enquo(weights)
weights <- eval_tidy(weights, .E()) %||% NA
Expand Down Expand Up @@ -104,19 +104,19 @@ centrality_closeness <- function(weights = NULL, mode = 'out', normalized = FALS
closeness(graph = graph, vids = focus_ind(graph, 'nodes'), mode = mode, cutoff = cutoff, weights = weights, normalized = normalized)
}
#' @describeIn centrality Wrapper for [igraph::eigen_centrality()]
#' @importFrom igraph eigen_centrality
#' @importFrom igraph eigen_centrality arpack_defaults
#' @export
centrality_eigen <- function(weights = NULL, directed = FALSE, scale = TRUE, options = igraph::arpack_defaults) {
centrality_eigen <- function(weights = NULL, directed = FALSE, scale = TRUE, options = arpack_defaults()) {
expect_nodes()
weights <- enquo(weights)
weights <- eval_tidy(weights, .E()) %||% NA
graph <- .G()
eigen_centrality(graph = graph, directed = directed, scale = scale, weights = weights, options = options)$vector[focus_ind(graph, 'nodes')]
}
#' @describeIn centrality Wrapper for [igraph::hub_score()]
#' @importFrom igraph hub_score
#' @importFrom igraph hub_score arpack_defaults
#' @export
centrality_hub <- function(weights = NULL, scale = TRUE, options = igraph::arpack_defaults) {
centrality_hub <- function(weights = NULL, scale = TRUE, options = arpack_defaults()) {
expect_nodes()
weights <- enquo(weights)
weights <- eval_tidy(weights, .E()) %||% NA
Expand Down Expand Up @@ -151,7 +151,7 @@ centrality_degree <- function(weights = NULL, mode = 'out', loops = TRUE, normal
expect_nodes()
graph <- .G()
weights <- enquo(weights)
weights <- eval_tidy(weights, .E()) %||% NA
weights <- eval_tidy(weights, .E())
if (is.null(weights)) {
degree(graph = graph, v = focus_ind(graph, 'nodes'), mode = mode, loops = loops, normalized = normalized)
} else {
Expand Down Expand Up @@ -187,11 +187,12 @@ centrality_harmonic <- function(weights = NULL, mode = 'out', normalized = FALSE
#' @param aggregation The aggregation type to use on the indirect relations to be used in `netrankr::aggregate_positions`
#' @param ... Arguments to pass on to `netrankr::indirect_relations`
#' @export
#' @importFrom igraph is_directed
centrality_manual <- function(relation = 'dist_sp', aggregation = 'sum', ...) {
expect_netrankr()
expect_nodes()
graph <- .G()
if (is.directed(graph)) {
if (is_directed(graph)) {
cli::cli_abort("Centrality measures based on the {.pkg netrankr} package only works on undirected networks")
}
rel <- netrankr::indirect_relations(graph, type = relation, ...)
Expand Down
4 changes: 2 additions & 2 deletions R/graph_measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ graph_assortativity <- function(attr, in_attr = NULL, directed = TRUE) {
if (is.numeric(attr)) {
in_attr <- enquo(in_attr)
in_attr <- eval_tidy(in_attr, .N(focused = FALSE))
assortativity(graph, attr, in_attr, directed)
assortativity(graph, values = attr, values.in = in_attr, directed = directed)
} else {
assortativity_nominal(graph, as.factor(attr), directed)
assortativity_nominal(graph, types = as.factor(attr), directed = directed)
}
}
#' @describeIn graph_measures Calculate the number of automorphisms of the graph. Wraps [igraph::count_automorphisms()]
Expand Down
4 changes: 2 additions & 2 deletions R/group.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ group_label_prop <- function(weights = NULL, label = NULL, fixed = NULL) {
desc_enumeration(group)
}
#' @describeIn group_graph Group nodes based on the leading eigenvector of the modularity matrix using [igraph::cluster_leading_eigen()]
#' @importFrom igraph membership cluster_leading_eigen cut_at
#' @importFrom igraph membership cluster_leading_eigen cut_at arpack_defaults
#' @export
group_leading_eigen <- function(weights = NULL, steps = -1, label = NULL, options = igraph::arpack_defaults, n_groups = NULL) {
group_leading_eigen <- function(weights = NULL, steps = -1, label = NULL, options = arpack_defaults(), n_groups = NULL) {
expect_nodes()
weights <- enquo(weights)
weights <- eval_tidy(weights, .E()) %||% NA
Expand Down
8 changes: 4 additions & 4 deletions R/node.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ node_is_cut <- function() {
focus_ind(graph, 'nodes') %in% articulation_points(graph)
}
#' @describeIn node_types is the node a root in a tree
#' @importFrom igraph degree is.directed
#' @importFrom igraph degree is_directed
#' @export
node_is_root <- function() {
expect_nodes()
graph <- .G()
node_inds <- focus_ind(graph, 'nodes')
if ((!is_tree(graph) && !is_forest(graph)) || !is.directed(graph)) {
if ((!is_tree(graph) && !is_forest(graph)) || !is_directed(graph)) {
return(rep(FALSE, length(node_inds)))
}
deg_in <- degree(graph, mode = 'in') == 0
Expand All @@ -43,7 +43,7 @@ node_is_root <- function() {
root[node_inds]
}
#' @describeIn node_types is the node a leaf in a tree
#' @importFrom igraph degree is.directed
#' @importFrom igraph degree is_directed
#' @export
node_is_leaf <- function() {
expect_nodes()
Expand All @@ -52,7 +52,7 @@ node_is_leaf <- function() {
if ((!is_tree(graph) && !is_forest(graph))) {
return(rep(FALSE, length(node_inds)))
}
if (is.directed(graph)) {
if (is_directed(graph)) {
deg_in <- degree(graph, mode = 'in') == 0
deg_out <- degree(graph, mode = 'out') == 0
leaf <- if (sum(deg_out) > sum(deg_in)) deg_out else deg_in
Expand Down
6 changes: 3 additions & 3 deletions R/random_walk.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ random_walk_rank <- function(n, root = NULL, mode = "out", weights = NULL) {
if (!quo_is_null(weights)) {
cli::cli_warn('{.arg weights} is ignored when doing a random walk on nodes')
}
walk <- as.integer(random_walk(graph, root, n, mode))
walk <- as.integer(random_walk(graph, root, n, mode = mode))
len_out <- gorder(graph)
} else {
weights <- eval_tidy(weights, .E(focused = FALSE)) %||% NA
walk <- as.integer(random_edge_walk(graph, root, n, weights, mode))
walk <- as.integer(random_edge_walk(graph, root, n, weights, mode = mode))
len_out <- gsize(graph)
}
res <- rep(list(integer()), len_out)
ord <- split(seq_len(n), walk)
ord <- split(seq_along(walk), walk)
res[as.integer(names(ord))] <- ord
res[focus_ind(graph, active(graph))]
}
4 changes: 2 additions & 2 deletions R/reroute.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ reroute <- function(.data, from = NULL, to = NULL, subset = NULL) {
}
#' @export
#' @importFrom rlang enquo eval_tidy
#' @importFrom igraph is.directed
#' @importFrom igraph is_directed
reroute.tbl_graph <- function(.data, from = NULL, to = NULL, subset = NULL) {
.register_graph_context(.data)
expect_edges()
Expand All @@ -54,7 +54,7 @@ reroute.tbl_graph <- function(.data, from = NULL, to = NULL, subset = NULL) {
.data <- tbl_graph(
nodes = as_tibble(.data, active = 'nodes'),
edges = edges,
directed = is.directed(.data)
directed = is_directed(.data)
) %gr_attr% .data
active(.data) <- 'edges'
.data
Expand Down
2 changes: 2 additions & 0 deletions R/tbl_graph.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ as_tbl_graph.tbl_graph <- function(x, ...) {
set_graph_data <- function(x, value, active) {
UseMethod('set_graph_data')
}
#' @export
set_graph_data.tbl_graph <- function(x, value, active = NULL) {
if (is.null(active)) active <- active(x)
switch(
Expand All @@ -208,6 +209,7 @@ set_graph_data.tbl_graph <- function(x, value, active = NULL) {
cli::cli_abort('Unknown active element: {.val {active}}. Only nodes and edges supported')
)
}
#' @export
set_graph_data.grouped_tbl_graph <- function(x, value, active = NULL) {
x <- NextMethod()
apply_groups(x, value)
Expand Down
10 changes: 3 additions & 7 deletions man/centrality.Rd

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

9 changes: 5 additions & 4 deletions man/focus.Rd

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

2 changes: 1 addition & 1 deletion man/group_graph.Rd

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

4 changes: 2 additions & 2 deletions man/map_bfs.Rd

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

4 changes: 2 additions & 2 deletions man/map_bfs_back.Rd

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

2 changes: 1 addition & 1 deletion man/map_dfs.Rd

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

2 changes: 1 addition & 1 deletion man/map_dfs_back.Rd

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

8 changes: 4 additions & 4 deletions tests/testthat/test-random-walk.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test_that("random_walk_rank returns correct data", {
set.seed(1)
node_walk <- create_notable('zachary') |>
mutate(walk_rank = random_walk_rank(30, 5)) |>
mutate(walk_rank = random_walk_rank(29, 5)) |>
pull(walk_rank)

edge_walk <- create_notable('zachary') |>
Expand All @@ -13,9 +13,9 @@ test_that("random_walk_rank returns correct data", {
expect_length(edge_walk, 78)
expect_type(node_walk, 'list')
expect_type(edge_walk, 'list')
expect_equal(node_walk[[5]], c(1, 12))
expect_equal(node_walk[[2]], integer())
expect_equal(node_walk[[5]], c(1, 5, 7))
expect_equal(node_walk[[3]], integer())
expect_equal(edge_walk[[1]], integer())
skip_on_cran()
expect_equal(edge_walk[[36]], 1:4)
expect_equal(edge_walk[[4]], c(1, 18))
})

0 comments on commit d1fc8ba

Please sign in to comment.