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
31 changes: 24 additions & 7 deletions R/community.R
Original file line number Diff line number Diff line change
Expand Up @@ -1287,9 +1287,10 @@ cluster_spinglass <- function(graph, weights = NULL, vertex = NULL, spins = 25,
#' weights. Set this to `NA` if the graph was a \sQuote{weight} edge
#' attribute, but you don't want to use it for community detection. A larger
#' edge weight means a stronger connection for this function.
#' @param resolution_parameter The resolution parameter to use. Higher
#' @param resolution The resolution parameter to use. Higher
Comment thread
vtraag marked this conversation as resolved.
#' resolutions lead to more smaller communities, while lower resolutions lead
#' to fewer larger communities.
#' @param resolution_parameter `r lifecycle::badge("superseded")` Use `resolution` instead.
#' @param beta Parameter affecting the randomness in the Leiden algorithm.
#' This affects only the refinement step of the algorithm.
#' @param initial_membership If provided, the Leiden algorithm
Expand All @@ -1301,6 +1302,7 @@ cluster_spinglass <- function(graph, weights = NULL, vertex = NULL, spins = 25,
#' If this is not provided, it will be automatically determined on the basis
#' of the `objective_function`. Please see the details of this function
#' how to interpret the vertex weights.
#' @inheritParams rlang::args_dots_empty
#' @return `cluster_leiden()` returns a [communities()]
#' object, please see the [communities()] manual page for details.
#' @author Vincent Traag
Expand Down Expand Up @@ -1330,12 +1332,27 @@ cluster_spinglass <- function(graph, weights = NULL, vertex = NULL, spins = 25,
#' r <- quantile(strength(g))[2] / (gorder(g) - 1)
#' # Set seed for sake of reproducibility
#' set.seed(1)
#' ldc <- cluster_leiden(g, resolution_parameter = r)
#' ldc <- cluster_leiden(g, resolution = r)
#' print(ldc)
#' plot(ldc, g)
cluster_leiden <- function(graph, objective_function = c("CPM", "modularity"),
weights = NULL, resolution_parameter = 1, beta = 0.01,
initial_membership = NULL, n_iterations = 2, vertex_weights = NULL) {
...,
weights = NULL, resolution = 1,
# FIXME: change to deprecated() once we have @importFrom lifecycle deprecated,
# after igraph:::deprecated() is removed
resolution_parameter, beta = 0.01,
initial_membership = NULL,
n_iterations = 2, vertex_weights = NULL) {

check_dots_empty()

if (lifecycle::is_present(resolution_parameter)) {
lifecycle::deprecate_soft("2.0.4",
"cluster_leiden(resolution_parameter)",
"cluster_leiden(resolution)")
resolution <- resolution_parameter
}

ensure_igraph(graph)

# Parse objective function argument
Expand Down Expand Up @@ -1373,7 +1390,7 @@ cluster_leiden <- function(graph, objective_function = c("CPM", "modularity"),
# Set correct node weights
vertex_weights <- strength(graph, weights = weights)
# Also correct resolution parameter
resolution_parameter <- resolution_parameter / sum(vertex_weights)
resolution <- resolution / sum(vertex_weights)
}
}

Expand All @@ -1382,7 +1399,7 @@ cluster_leiden <- function(graph, objective_function = c("CPM", "modularity"),
if (n_iterations > 0) {
res <- .Call(
R_igraph_community_leiden, graph, weights,
vertex_weights, as.numeric(resolution_parameter),
vertex_weights, as.numeric(resolution),
as.numeric(beta), !is.null(membership), as.numeric(n_iterations),
membership
)
Expand All @@ -1394,7 +1411,7 @@ cluster_leiden <- function(graph, objective_function = c("CPM", "modularity"),
prev_quality <- quality
res <- .Call(
R_igraph_community_leiden, graph, weights,
vertex_weights, as.numeric(resolution_parameter),
vertex_weights, as.numeric(resolution),
as.numeric(beta), !is.null(membership), 1,
membership
)
Expand Down
12 changes: 9 additions & 3 deletions man/cluster_leiden.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-leiden.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ test_that("cluster_leiden works", {
withr::local_seed(42)

g <- make_graph("Zachary")
mc <- cluster_leiden(g, resolution_parameter = 0.06)
mc <- cluster_leiden(g, resolution = 0.06)

expect_that(
as.vector(membership(mc)),
Expand Down