graspologic-org / graspologic

Python package for graph statistics

Home Page:https://graspologic-org.github.io/graspologic/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] Node2Vec fitting fails on graph with isolated nodes

dokato opened this issue · comments

Expected Behavior

It checks that graph has isolated nodes and stops at the beginning before running costly _learn_embeddings or filters them out.

Actual Behavior

It doesn't find a key in the remapped_labels

Example Code

adj2 = np.array([
    [0,0, 0, 0, 0],
    [0, 1, 1, 0, 1],
    [0, 0, 1, 1, 1],
    [0, 1, 1, 0, 1],
    [0, 1, 1, 1, 0]
])
Xhat, _ = node2vec(nx.from_numpy_matrix(adj2), iterations=100)

Full Traceback

KeyError                                  Traceback (most recent call last)
/var/folders/zt/s4rb4_kj4pnbmcqw7xyw52hw0000gn/T/ipykernel_65152/1282715379.py in <module>
----> 1 Xhat, _ = node2vec(nx.from_numpy_matrix(adj2),
      2                      iterations=100)

~/miniforge3/envs/grasp/lib/python3.9/site-packages/graspologic/embed/n2v.py in node2vec_embed(graph, num_walks, walk_length, return_hyperparameter, inout_hyperparameter, dimensions, window_size, workers, iterations, interpolate_walk_lengths_by_node_degree, random_seed)
    150 
    151     return (
--> 152         np.array([model.wv.get_vector(remapped_labels[node]) for node in labels]),
    153         labels,
    154     )

~/miniforge3/envs/grasp/lib/python3.9/site-packages/graspologic/embed/n2v.py in <listcomp>(.0)
    150 
    151     return (
--> 152         np.array([model.wv.get_vector(remapped_labels[node]) for node in labels]),
    153         labels,
    154     )

KeyError: 0

Your Environment

  • Python version: 3.9.7
  • graspologic version: 1.0.0