question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Clustering with leidenalg

See original GitHub issue

Hello,

It would appear that louvain-igraph has been obsoleted in favour of leidenalg, and the author makes a persuasive case as to the superiority of the new approach. To my untrained eye, the algorithm is conceptually similar to the Louvain modification used by Seurat, but introduces an extra collapsed network refinement step.

it should be easy to support this in Scanpy - the syntax appears to be identical to the old louvain innards, and I was able to construct a very minimal dummy function for testing by taking the key bits of sc.tl.louvain() and replacing louvain. with leidenalg.:

import leidenalg
import numpy as np
import pandas as pd
from scanpy import utils
from natsort import natsorted

def leiden(adata, use_weights=False, resolution=1, iterations=-1):
	g = utils.get_igraph_from_adjacency(adata.uns['neighbors']['connectivities'], directed=True)
	weights = None
	if use_weights:
		weights = np.array(g.es["weight"]).astype(np.float64)
	part = leidenalg.find_partition(
		g, leidenalg.RBConfigurationVertexPartition, 
		resolution_parameter = resolution, weights = weights, 
		n_iterations = iterations,
	)
	groups = np.array(part.membership)
	adata.obs['louvain'] = pd.Categorical(
		values=groups.astype('U'),
		categories=natsorted(np.unique(groups).astype('U')),
	)

As such, replacing any louvain. with leidenalg. in sc.tl.louvain() would do most of the work. Probably the only new thing that would need support would the the n_iterations parameter in leidenalg.find_partition(). The default value is 2, positive values control how many passes of the algorithm are performed. -1 just makes it run until it fails to improve the clustering.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:28 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
flying-sheepcommented, Nov 23, 2018

Well, “implemented”… I’d say “wrapped”.

1reaction
ktpolanskicommented, Nov 8, 2018

In that case, it’d probably be easiest to just add a 'leiden' flavour to sc.tl.louvain(). Due to the overlap of the syntax, you may even get away with explicitly recycling the existing Louvain code in the function and just swap the package behind the scenes:

if flavor == 'vtraag':
	import louvain
elif flavor ==  'leiden':
	import leidenalg as louvain
Read more comments on GitHub >

github_iconTop Results From Across the Web

Clustering with the Leiden Algorithm in R
This package requires the 'leidenalg' and 'igraph' modules for python (2) to be installed on your system. For example: pip install leidenalg igraph....
Read more >
Clustering with leidenalg · Issue #350 · scverse/scanpy - GitHub
Hello, It would appear that louvain-igraph has been obsoleted in favour of leidenalg, and the author makes a persuasive case as to the ......
Read more >
Introduction — leidenalg 0.9.2.dev0+gb530332.d20221214 ...
The result partition is in this case a ModularityVertexPartition which is derived from the igraph type ig.VertexClustering , see the documentation for more ......
Read more >
leidenalg Documentation - Read the Docs
VertexClustering, see the documentation for more details. Why then should you use this package rather than for example the Louvain algorithm ...
Read more >
Using the Leiden algorithm to find well-connected clusters in ...
We have dedicated quite some time developing clustering algorithms for ... implementation at https://github.com/vtraag/leidenalg vs the Java ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found