benedekrozemberczki / karateclub

Karate Club: An API Oriented Open-source Python Framework for Unsupervised Learning on Graphs (CIKM 2020)
https://karateclub.readthedocs.io
GNU General Public License v3.0
2.17k stars 247 forks source link

Node indexing is wrong #72

Closed sclipman closed 3 years ago

sclipman commented 3 years ago

Thank you for creating this extension library! I saw your paper and am trying to test karateclub with a graph created from an edge list using the code:

import networkx as nx
import pandas as pd
from karateclub import DANMF

edges = pd.read_csv("Edge_List.csv")
graph = nx.from_pandas_edgelist(edges, "Source", "Target")

model = DANMF()
model.fit(graph)
X = model.get_memberships()

However, I get the following error below. Any tips on resolving this would be greatly appreciated!

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-10-5fcf8490ff43> in <module>
      8 
      9 model = DANMF()
---> 10 model.fit(graph)
     11 X = model.get_memberships()

~/opt/anaconda3/lib/python3.8/site-packages/karateclub/community_detection/overlapping/danmf.py in fit(self, graph)
    173         """
    174         self._set_seed()
--> 175         self._check_graph(graph)
    176         self._setup_target_matrices(graph)
    177         self._pre_training()

~/opt/anaconda3/lib/python3.8/site-packages/karateclub/estimator.py in _check_graph(self, graph)
     62         self._check_connectivity(graph)
     63         self._check_directedness(graph)
---> 64         self._check_indexing(graph)
     65 
     66 

~/opt/anaconda3/lib/python3.8/site-packages/karateclub/estimator.py in _check_indexing(self, graph)
     55         numeric_indices = [index for index in range(graph.number_of_nodes())]
     56         node_indices = sorted([node for node in graph.nodes()])
---> 57         assert numeric_indices == node_indices, "The node indexing is wrong."
     58 
     59 

AssertionError: The node indexing is wrong.
benedekrozemberczki commented 3 years ago

Hi there @sclipman,

You need numeric indices that start with zero.

Bests,

Benedek

benedekrozemberczki commented 3 years ago

@sclipman Could you star the repo and hit follow on github?

sclipman commented 3 years ago

Perfect, thank you!

For anyone running into this issue, node labels can be converted to integers in NetworkX with: graph_num = nx.convert_node_labels_to_integers(graph, first_label=0, ordering='default')