GiulioRossetti / cdlib

Community Discovery Library
http://cdlib.readthedocs.io
BSD 2-Clause "Simplified" License
375 stars 71 forks source link

AttributeError in crisp_partition.py #222

Closed iglesiasignacio closed 1 year ago

iglesiasignacio commented 1 year ago

Describe the bug Some community detection algorithms (e.g. surprise_communities) fail when calling crisp_partition.py even running the examples in the docs

To Reproduce I'm running this in Google Colab

!pip install cdlib
# example from the docs
from cdlib import algorithms
import networkx as nx
G = nx.karate_club_graph()
coms = algorithms.surprise_communities(G)

Full traceback

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[<ipython-input-1-49c0388a8ce2>](https://localhost:8080/#) in <cell line: 6>()
      4 import networkx as nx
      5 G = nx.karate_club_graph()
----> 6 coms = algorithms.surprise_communities(G)

[/usr/local/lib/python3.10/dist-packages/cdlib/algorithms/crisp_partition.py](https://localhost:8080/#) in surprise_communities(g_original, initial_membership, weights, node_sizes)
    991     g = convert_graph_formats(g_original, ig.Graph)
    992 
--> 993     part = leidenalg.find_partition(
    994         g,
    995         leidenalg.SurpriseVertexPartition,

AttributeError: 'NoneType' object has no attribute 'find_partition'
GiulioRossetti commented 1 year ago

Hi,

Fortunately, the issue is straightforward to address. It seems you don't have the leidenalg package installed (it's among the optional dependencies since it relies on C code): install it (pip install leidenalg) and everything will work fine.

I updated the import check in crisp_partition.py: the fix will be integrated into the next release of the library.