# Leidenalg clustering error in Trajectory inference tutorial

**URL:** <https://discourse.scverse.org/t/leidenalg-clustering-error-in-trajectory-inference-tutorial/781>\
**Category:** scanpy\
**Created:** [September 26, 2022, 2:55pm UTC](https://discourse.scverse.org/t/leidenalg-clustering-error-in-trajectory-inference-tutorial/781 "2022-09-26T14:55:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![spkhowe](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@spkhowe](https://discourse.scverse.org/u/spkhowe)\
**Post date:** [September 26, 2022, 2:55pm UTC](https://discourse.scverse.org/t/leidenalg-clustering-error-in-trajectory-inference-tutorial/781/1 "2022-09-26T14:55:00Z")

</div>

I am trying to replicate [this tutorial](https://scanpy-tutorials.readthedocs.io/en/latest/paga-paul15.html#Clustering-and-PAGA) on trajectory inference. I have followed every step up until clustering, where I try to use sc.tl.leiden(adata) to cluster, but keep having the following error:

BaseException Traceback (most recent call last)  
Cell In [15], line 1  
----\> 1 sc.tl.leiden(adata)

File ~/miniconda3/envs/py39/lib/python3.9/site-packages/scanpy/tools/\_leiden.py:144, in leiden(adata, resolution, restrict\_to, random\_state, key\_added, adjacency, directed, use\_weights, n\_iterations, partition\_type, neighbors\_key, obsp, copy, \*\*partition\_kwargs)  
142 partition\_kwargs[‘resolution\_parameter’] = resolution  
143 # clustering proper  
 → 144 part = leidenalg.find\_partition(g, partition\_type, \*\*partition\_kwargs)  
145 # store output into adata.obs  
146 groups = np.array(part.membership)

File ~/miniconda3/envs/py39/lib/python3.9/site-packages/leidenalg/functions.py:81, in find\_partition(graph, partition\_type, initial\_membership, weights, n\_iterations, max\_comm\_size, seed, \*\*kwargs)  
79 if not weights is None:  
80 kwargs[‘weights’] = weights  
—\> 81 partition = partition\_type(graph,  
82 initial\_membership=initial\_membership,  
83 \*\*kwargs)  
84 optimiser = Optimiser()  
86 optimiser.max\_comm\_size = max\_comm\_size

File ~/miniconda3/envs/py39/lib/python3.9/site-packages/leidenalg/VertexPartition.py:855, in RBConfigurationVertexPartition. **init** (self, graph, initial\_membership, weights, node\_sizes, resolution\_parameter)  
851 else:  
852 # Make sure it is a list  
853 node\_sizes = list(node\_sizes)  
 → 855 self.\_partition = \_c\_leiden.\_new\_RBConfigurationVertexPartition(pygraph\_t,  
856 initial\_membership, weights, node\_sizes, resolution\_parameter)  
857 self.\_update\_internal\_membership()

BaseException: Could not construct partition: Weight vector not the same size as the number of edges.

Does anyone know why this doesn’t work? I assumed weight and edges would be taken from adata.obsp[‘connectivities’], the default storage output of sc.pp.neighbors adjacency graph.

---

<div class="post-metadata">

**Author:** ![nrclaudio](https://avatars.discourse-cdn.com/v4/letter/n/34f0e0/32.png) [@nrclaudio](https://discourse.scverse.org/u/nrclaudio)\
**Post date:** [September 28, 2022, 9:54am UTC](https://discourse.scverse.org/t/leidenalg-clustering-error-in-trajectory-inference-tutorial/781/2 "2022-09-28T09:54:42Z")

</div>

On Python 3.7 and a clean conda install from 2 days ago sc.tl.leiden throws the same error.

---

<div class="post-metadata">

**Author:** ![adamgayoso](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/adamgayoso/32/100_2.png) [@adamgayoso](https://discourse.scverse.org/u/adamgayoso)\
**Post date:** [September 28, 2022, 4:19pm UTC](https://discourse.scverse.org/t/leidenalg-clustering-error-in-trajectory-inference-tutorial/781/3 "2022-09-28T16:19:13Z")

</div>

I had the same issue, would you be able to make an issue at scanpy? I noticed installing leidenalg with pip fixes the issue.

---

<div class="post-metadata">

**Author:** ![spkhowe](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@spkhowe](https://discourse.scverse.org/u/spkhowe)\
**Post date:** [September 29, 2022, 1:47am UTC](https://discourse.scverse.org/t/leidenalg-clustering-error-in-trajectory-inference-tutorial/781/4 "2022-09-29T01:47:02Z")

</div>

Installing with pip didn’t work for me. Can you give more details on your setup?
