Merge pull request #222 from microsoft/fix/make-louvain-deterministic

Bugfix: make Louvain implementation in `network_p2p()` deterministic (#221)
This commit is contained in:
Martin Chan 2022-05-30 14:28:21 +01:00 коммит произвёл GitHub
Родитель f6e2942d0a 1d3a1d3420
Коммит 2a6bd724f2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 29 добавлений и 21 удалений

Просмотреть файл

@ -60,8 +60,10 @@
#' of the edges (only for 'ggraph' mode). Defaults to 1.
#' @param res Resolution parameter to be passed to `leiden::leiden()`. Defaults
#' to 0.5.
#' @param seed Seed for the random number generator passed to `leiden::leiden()`
#' to ensure consistency. Only applicable when `display` is set to `"leiden"`.
#' @param seed Seed for the random number generator passed to either
#' `set.seed()` when the Louvain algorithm is used, or `leiden::leiden()` when
#' the Leiden algorithm is used, to ensure consistency. Only applicable when
#' `display` is set to `"louvain"` or `"leiden"`.
#' @param algorithm String to specify the node placement algorithm to be used.
#' Defaults to `"mds"` for the deterministic multi-dimensional scaling of
#' nodes. See
@ -241,24 +243,26 @@ network_p2p <- function(data,
} else if(display == "louvain"){
## Convert to undirected
g_ud <- igraph::as.undirected(g_raw)
set.seed(seed = seed)
## Return a numeric vector of partitions / clusters / modules
## Set a low resolution parameter to have fewer groups
## weights = NULL means that if the graph as a `weight` edge attribute, this
## will be used by default.
lc <- igraph::cluster_louvain(g_ud, weights = NULL)
## Convert to undirected
g_ud <- igraph::as.undirected(g_raw)
## Add cluster
g <-
g_ud %>%
# Add louvain partitions to graph object
igraph::set_vertex_attr("cluster", value = as.character(igraph::membership(lc))) %>% # Return membership - diff from Leiden
igraph::simplify()
## Return a numeric vector of partitions / clusters / modules
## Set a low resolution parameter to have fewer groups
## weights = NULL means that if the graph as a `weight` edge attribute, this
## will be used by default.
lc <- igraph::cluster_louvain(g_ud, weights = NULL)
## Name of vertex attribute
v_attr <- "cluster"
## Add cluster
g <-
g_ud %>%
# Add louvain partitions to graph object
igraph::set_vertex_attr("cluster", value = as.character(igraph::membership(lc))) %>% # Return membership - diff from Leiden
igraph::simplify()
## Name of vertex attribute
v_attr <- "cluster"
} else if(display == "leiden"){

Просмотреть файл

@ -44,8 +44,10 @@ of the nodes. Defaults to 0.7.}
\item{res}{Resolution parameter to be passed to \code{leiden::leiden()}. Defaults
to 0.5.}
\item{seed}{Seed for the random number generator passed to \code{leiden::leiden()}
to ensure consistency. Only applicable when \code{display} is set to \code{"leiden"}.}
\item{seed}{Seed for the random number generator passed to either
\code{set.seed()} when the Louvain algorithm is used, or \code{leiden::leiden()} when
the Leiden algorithm is used, to ensure consistency. Only applicable when
\code{display} is set to \code{"louvain"} or \code{"leiden"}.}
\item{desc_hrvar}{Character vector of length 3 containing the HR attributes
to use when returning the \code{"describe"} output. See \code{network_describe()}.}

Просмотреть файл

@ -86,8 +86,10 @@ of the edges (only for 'ggraph' mode). Defaults to 1.}
\item{res}{Resolution parameter to be passed to \code{leiden::leiden()}. Defaults
to 0.5.}
\item{seed}{Seed for the random number generator passed to \code{leiden::leiden()}
to ensure consistency. Only applicable when \code{display} is set to \code{"leiden"}.}
\item{seed}{Seed for the random number generator passed to either
\code{set.seed()} when the Louvain algorithm is used, or \code{leiden::leiden()} when
the Leiden algorithm is used, to ensure consistency. Only applicable when
\code{display} is set to \code{"louvain"} or \code{"leiden"}.}
\item{algorithm}{String to specify the node placement algorithm to be used.
Defaults to \code{"mds"} for the deterministic multi-dimensional scaling of