Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure modes of from_networkx #830

Closed
seyuboglu opened this issue Nov 26, 2019 · 2 comments
Closed

Failure modes of from_networkx #830

seyuboglu opened this issue Nov 26, 2019 · 2 comments
Labels

Comments

@seyuboglu
Copy link

@seyuboglu seyuboglu commented Nov 26, 2019

Questions & Help

Hi! What kinds of networkx graphs are expected to work with from_networkx. Right now, the documentation just says that it expects a networkx graph, but there are definitely some networkx graphs for which it doesn't work:

Graphs without contiguous node labels:
The following cell runs without error. Except the output is a little funky-

from torch_geometric.utils.convert import from_networkx
import networkx as nx 

# build an nx graph 
graph = nx.Graph()
graph.add_node(4)
graph.add_node(2)
graph.add_node(6)
graph.add_edge(4, 6)
graph.add_edge(2, 6)
for node in graph.nodes():
    graph.nodes[node]["x"] = node

# convert to pyg
data = from_networkx(graph)   
print(f"X: {data.x}")
print(f"Edge index: {data.edge_index}")

The output is:

X: tensor([4, 2, 6])
Edge index: tensor([[4, 2, 6, 6],
        [6, 6, 4, 2]])

The node indices in the edge_index have no correspondence with the order of the nodes in the feature matrix x. It seems to me like from_networkx is dependent on: (1) node labels of nx.Graph being consecutive numbers (i.e. from 0 to len(graph) - 1) and (2) that graph.nodes() returns these nodes in order. That being said, perhaps I am missing something with how Data is being used in message passing etc. In either case, I think it would be helpful to add a note in the documentation about the kinds of networkx graphs the function will work with.

Graphs without edges:
The following crashes with a IndexError: list index out of range

from torch_geometric.utils.convert import from_networkx
import networkx as nx 

graph = nx.Graph()
graph.add_node(1)
graph.add_node(2)
from_networkx(graph)

Graphs with non-numeric node labels
Crashes with ValueError: too many dimensions 'str'

from torch_geometric.utils.convert import from_networkx
import networkx as nx 

# build an nx graph 
graph = nx.Graph()
graph.add_node("4")
graph.add_node("2")
graph.add_edge("4", "2")

# convert to pyg
data = from_networkx(graph)

Thanks!!

@rusty1s

This comment has been minimized.

Copy link
Owner

@rusty1s rusty1s commented Nov 27, 2019

Thanks for this issue. It is correct that from_networkx currently expects consecutive numbers as node indices. This should be fixed and I will work on it :)

rusty1s added a commit that referenced this issue Jan 6, 2020
@rusty1s

This comment has been minimized.

Copy link
Owner

@rusty1s rusty1s commented Jan 6, 2020

Fixed in master (c0458f0).

@rusty1s rusty1s closed this Jan 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.