-
Hi, I'm having issues working with heterogeneous data represented in networkx graphs. I have data represented in a networkx graph containing two types of nodes ("source" and "update") and two types of edges ("topology", which connects two "source" node types; and "involved", which connects an "update" to a "source"). I have created extra attributes in the nodes to make sure that all of them have the same attributes, as required (from_networkx). Even though the conversion works, when executing to_heterogeneous, I get an error:
The node_type attribute contains the following:
To reproduce the issue:
My main question is: what would be the best way of transforming a networkx heterogeneous graph to HeteroData?? Thank you for your time. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
import networkx as nx
import numpy as np
import torch
from torch_geometric.utils.convert import from_networkx
graph = nx.DiGraph()
graph.add_node(...)
graph.add_node(...)
graph.add_node(...)
graph.add_edge(1, 2, edge_type="topology")
graph.add_edge(123131243, 1, edge_type="involved")
data = from_networkx(graph)
data.node_type = np.unique(np.array(data.node_type), return_inverse=True)[1]
data.node_type = torch.from_numpy(data.node_type)
data.edge_type = np.unique(np.array(data.edge_type), return_inverse=True)[1]
data.edge_type = torch.from_numpy(data.edge_type)
hetero_data = data.to_heterogeneous(
node_type_names=['source', 'update'],
edge_type_names=[('source', 'topology', 'source'),
('update', 'involved', 'source')])
print(hetero_data) |
Beta Was this translation helpful? Give feedback.
to_heterogeneous
requires thatnode_type
andedge_type
denote index-based vectors, not lists of strings: