sknetwork-team / scikit-network

Graph Algorithms
Other
602 stars 67 forks source link

Original node name mappings #532

Closed levrone1987 closed 2 years ago

levrone1987 commented 2 years ago

Description

I am trying to form a weighted graph from a weighted edge list. Given that my nodes have specific names, I wonder how I may figure out the mapping between the actual node names and the indices.

This is the code I use for graph formation:

adjacency = skn.data.from_edge_list(cui_data_exported, weighted=True, reindex=True, matrix_only=False)

The idea is to calculate node embeddings with the Spectral approach:

spectral = Spectral(n_components=self.factors, decomposition='laplacian', normalized=False, regularization=0.0)
pos = spectral.fit_transform(adjacency)

In the above, pos will not be a dict which I may query by the actual node name, but a list of coordinates. How could I access the position of a node with its original name?

tbonald commented 2 years ago

The function from_edge_list returns a Bunch object with the attribute names giving the original name of each node indexed from 0 to n-1 where n is the number of nodes (except if no reindexing was necessary).

Here is an example for your problem:

from sknetwork.data import from_edge_list
graph = from_edge_list([('barbara', 'albert', 2), ['albert', 'edith', 1]], weighted=True, reindex=True, matrix_only=False)

adjacency = graph.adjacency
names = graph.names
print(names)

Output: ['albert' 'barbara' 'edith']

from sknetwork.embedding import Spectral

spectral = Spectral(1)
pos = spectral.fit_transform(adjacency)
name_id = {name: i for i, name in enumerate(names)}
pos[name_id['edith']]

Output: array([1.])