vtraag / leidenalg

Implementation of the Leiden algorithm for various quality functions to be used with igraph in Python.
GNU General Public License v3.0
596 stars 78 forks source link

"Membership cannot exceed number of nodes" error with leidenalg 0.8.4 #66

Closed AvantiShri closed 3 years ago

AvantiShri commented 3 years ago

Hi, I'm getting an odd error with the latest version of leidenalg that wasn't appearing with 0.8.3. Please see this github gist for a reproduction of the error:

https://colab.research.google.com/gist/AvantiShri/46060b5fe0fac540830a3078758a487b/-membership-cannot-exceed-number-of-nodes-error-with-leidenalg-0-8-4.ipynb

Consider the following test:

import numpy as np
import leidenalg
import igraph as ig

def test():
  g = ig.Graph(directed=None)
  g.add_vertices(2)
  g.add_edges([(0,1)])
  g.es['weight'] = np.array([0.5])
  leidenalg.find_partition(graph=g, partition_type=leidenalg.ModularityVertexPartition,
      weights=g.es['weight'], n_iterations=-1,
      initial_membership=np.array([0,1]), seed=1234)

With leidenalg 0.8.3, test() runs without any error. However, with version 0.8.4, it produces:

/usr/local/lib/python3.7/dist-packages/leidenalg/functions.py in find_partition(graph, partition_type, initial_membership, weights, n_iterations, max_comm_size, seed, **kwargs)
     86   partition = partition_type(graph,
     87                              initial_membership=initial_membership,
---> 88                              **kwargs)
     89   optimiser = Optimiser()
     90 

/usr/local/lib/python3.7/dist-packages/leidenalg/VertexPartition.py in __init__(self, graph, initial_membership, weights, node_sizes)
    467 
    468     self._partition = _c_leiden._new_ModularityVertexPartition(pygraph_t,
--> 469         initial_membership, weights, node_sizes)
    470     self._update_internal_membership()
    471 

TypeError: Membership cannot exceed number of nodes.
vtraag commented 3 years ago

Hi @AvantiShri, thanks for reporting this! I now realise that I changed something in 0.8.4 to correct some warnings, but that was previously used to accommodate for numpy. I will correct this again. As a workaround, please pass in any arguments, including the initial_membership, function as a Python list, not as a numpy array. You can use .tolist() of the numpy array.

AvantiShri commented 3 years ago

For future people encountering this: @vtraag's suggestion of using .tolist() (rather than simply wrapping the numpy array in a list(...) call) seems to be important.