Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from leiden to leidenbase #6792

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

alanocallaghan
Copy link
Contributor

@alanocallaghan alanocallaghan commented Dec 16, 2022

The Leiden implementation provided by leiden is absurdly slow, way more than I'd expect from the overhead of calling reticulate. This switches for the (mostly) equivalent leidenbase. Tests pass locally for me, see also the demo below comparing 3 Leiden implementations.

See #6754

library("leidenbase")
library("leidenAlg")
library("leiden")
library("igraph")
library("microbenchmark")

fpath <- system.file('testdata', 'igraph_n1500_edgelist.txt.gz', package='leidenbase')
zfp <- gzfile(fpath)
igraph <- read_graph(file = zfp, format='edgelist')

microbenchmark(
    leidenAlg::leiden.community(igraph),
    leidenbase::leiden_find_partition(igraph),
    leiden::leiden(igraph),
    times = 10
)
# Unit: milliseconds
#                           expr         min          lq        mean      median
#       leiden.community(igraph)    52.98505    55.16530    63.16430    55.95267
#  leiden_find_partition(igraph)    29.22238    30.54163    39.15174    37.57693
#                 leiden(igraph) 17140.52418 20671.12398 21172.11517 21128.94100
#           uq         max neval cld
#     75.38524    80.10432    10  a 
#     40.30238    56.91094    10  a 
#  22268.67866 24597.89747    10   b


r1 <- leiden_find_partition(igraph,
    partition_type = "RBConfigurationVertexPartition",
    seed = 1234,
    resolution_parameter = 1
    
)
r2 <- leiden(igraph,
    partition_type = "RBConfigurationVertexPartition",
    resolution_parameter = 1, 
    seed = 1234
)
table(r1$membership, r2)
#       1   2   3   4   5   6   7   8   9
#   1 295   0   0   0   0   0   0   0   0
#   2   0 209   0   0   0   0   0   0   0
#   3   0   0 201   0   0   0   0   0   0
#   4   0   0   0 191   0   0   0   0   0
#   5   0   0   0   0 175   0   0   0   0
#   6   0   0   0   0   0 170   0   0   0
#   7   0   0   0   0   0   0  93   0   0
#   8   0   0   0   0   0   0   0  85   0
#   9   0   0   0   0   0   0   0   0  81

@szhorvat
Copy link

szhorvat commented Dec 20, 2022

If you are looking to reduce overhead to a minimum, verify whether you are relying on any feature not already available in igraph::cluster_leiden(), and if not, just use that function. The various "partition type" settings are reproducible by setting the vertex_weight values appropriately. See the general form of the objective function in the documentation, where $n$ represents the vertex weigths, and compare with the objective functions of the various "partition types". For example, setting $n_i = k_i / \sqrt{2m}$ where $k_i$ is the vertex degree/strength and $m$ is the edge count / total edge weight yields the modularity objective function (i.e. RBConfigurationVertexPartition).

@alanocallaghan
Copy link
Contributor Author

It's a bit out of my wheelhouse to go through and reimplement the different objective functions to be honest, I just thought this was a useful contribution that doesn't increase the dependency count.

@alanocallaghan
Copy link
Contributor Author

Pinging @saketkc @samuel-marsh as people who seem to be merging code:

Can you have a look at this? Feel free to say you're not interested and close it, or that you want more info/more tests showing equivalence. Either is fine

@samuel-marsh
Copy link
Collaborator

I will second @alanocallaghan ping that would be great to have this included!

I’m not member of dev team so my actions are limited here but adding @Gesmira from Seurat team.

Best,
Sam

@dcollins15 dcollins15 self-requested a review March 1, 2024 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants