# Failure modes of from_networkx #830

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

# Failure modes of from_networkx#830

opened this issue Nov 26, 2019 · 2 comments
Labels

## ❓ 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()
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()
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()

# convert to pyg
data = from_networkx(graph)
``````

Thanks!!

### rusty1s commented Nov 27, 2019 • edited

 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 :)
``` fixes failure modes of from_networkx (#830) ```
``` c0458f0 ```