Skip to content

Commit

Permalink
Add group_fluid
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Nov 29, 2023
1 parent a0d23cc commit 689bbff
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export(group_components)
export(group_data)
export(group_edge_betweenness)
export(group_fast_greedy)
export(group_fluid)
export(group_indices)
export(group_infomap)
export(group_keys)
Expand Down Expand Up @@ -473,6 +474,7 @@ importFrom(igraph,clique_num)
importFrom(igraph,closeness)
importFrom(igraph,cluster_edge_betweenness)
importFrom(igraph,cluster_fast_greedy)
importFrom(igraph,cluster_fluid_communities)
importFrom(igraph,cluster_infomap)
importFrom(igraph,cluster_label_prop)
importFrom(igraph,cluster_leading_eigen)
Expand Down Expand Up @@ -617,6 +619,7 @@ importFrom(pillar,tbl_format_footer)
importFrom(pillar,tbl_sum)
importFrom(rlang,"!!!")
importFrom(rlang,"%||%")
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(rlang,UQS)
importFrom(rlang,as_quosure)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
and flexible igraph implementation.
* Added `group_color()` as an interface to `greedy_vertex_coloring()` in igraph
* Added `group_leiden()` to interface with `cluster_leiden()` in igraph
* Added `group_fluid()` to interface with `cluster_fluid_communities()` in igraph

# tidygraph 1.2.3

Expand Down
11 changes: 10 additions & 1 deletion R/group.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ group_louvain <- function(weights = NULL, resolution = 1) {
}
#' @describeIn group_graph Group nodes according to the Leiden algorithm ([igraph::cluster_leiden()]) which is similar, but more efficient and provides higher quality results than `cluster_louvain()`
#' @importFrom igraph membership cluster_leiden
#' @importFrom rlang list2 inject
#' @importFrom rlang list2 inject :=
#' @export
group_leiden <- function(weights = NULL, resolution = 1, objective_function = 'CPM', beta = 0.01, label = NULL, n = 2, node_weights = NULL) {
expect_nodes()
Expand Down Expand Up @@ -235,6 +235,15 @@ group_walktrap <- function(weights = NULL, steps = 4, n_groups = NULL) {
group <- as.integer(group[focus_ind(.G(), 'nodes')])
desc_enumeration(group)
}
#' @describeIn group_graph Group nodes by simulating fluid interactions on the graph topology using [igraph::cluster_fluid_communities()]
#' @importFrom igraph cluster_fluid_communities
#' @export
group_fluid <- function(n_groups = 2) {
expect_nodes()
graph <- .G()
group <- as.integer(membership(cluster_fluid_communities(graph, n_groups))[focus_ind(graph, 'nodes')])
desc_enumeration(group)
}
#' @describeIn group_graph Group edges by their membership of the maximal binconnected components using [igraph::biconnected_components()]
#' @importFrom igraph biconnected_components
#' @export
Expand Down
5 changes: 5 additions & 0 deletions man/group_graph.Rd

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

6 changes: 6 additions & 0 deletions tests/testthat/test-group.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test_that("grouping returns integer vector", {
#expect_type(get_group(gr, group_optimal()), 'integer')
expect_type(get_group(gr, group_spinglass()), 'integer')
expect_type(get_group(gr, group_walktrap()), 'integer')
expect_type(get_group(gr, group_fluid()), 'integer')
expect_type(get_group(gr, group_color()), 'integer')
gr1 <- activate(gr, edges)
expect_type(get_group(gr1, group_biconnected_component()), 'integer')
Expand All @@ -40,6 +41,7 @@ test_that("grouping returns integer of correct length", {
#expect_length(get_group(gr, group_optimal()), igraph::gorder(gr))
expect_length(get_group(gr, group_spinglass()), igraph::gorder(gr))
expect_length(get_group(gr, group_walktrap()), igraph::gorder(gr))
expect_length(get_group(gr, group_fluid()), igraph::gorder(gr))
expect_length(get_group(gr, group_color()), igraph::gorder(gr))
gr1 <- activate(gr, edges)
expect_length(get_group(gr1, group_biconnected_component()), igraph::gsize(gr1))
Expand All @@ -61,6 +63,7 @@ test_that("grouping requires correct activation", {
#expect_error(get_group(gr1, group_optimal()))
expect_error(get_group(gr1, group_spinglass()))
expect_error(get_group(gr1, group_walktrap()))
expect_error(get_group(gr1, group_fluid()))
expect_error(get_group(gr1, group_color()))

skip_on_os('windows')
Expand All @@ -81,6 +84,9 @@ test_that("grouping with fixed number of groups", {
expect_equal(
get_number_of_groups(gr, group_walktrap(n_groups = 7)), 7
)
expect_equal(
get_number_of_groups(gr, group_fluid(n_groups = 7)), 7
)
})

test_empty_context()

0 comments on commit 689bbff

Please sign in to comment.