Convert pytorch geometric data sample to its corresponding line graph
I'm trying to convert a dataset of torch geometric so that its content is represented as line graphs of the original samples. My code looks likes the following:
G = to_networkx(data,
                node_attrs=['x'],
                edge_attrs=['edge_attr'],
                to_undirected=not directed)
line_graph = nx.line_graph(G, create_using=nx.Graph)
result = from_networkx(line_graph)

However, the resulting samples don't have any attribute, neither edge_attr nor x. At the same time, the label y is gone too. Is there a better way to convert it?

As noted in the previous answer, the attributes are not propagated by line_graph. Since I'm interested in preserving only the edge attributes, i.e. converting edges to nodes, my solution looks like this:
original_edge_attrs = data.edge_attr
original_edge_names = [(from_.item(), to_.item()) for from_, to_ in zip(data.edge_index[0, :], data.edge_index[1, :])]
original_edge_to_attr = {e: attr for e, attr in zip(original_edge_names, original_edge_attrs)}
G = to_networkx(data,
                node_attrs=['x'],
                edge_attrs=['edge_attr'],
                to_undirected=not directed)
line_graph = nx.line_graph(G, create_using=nx.DiGraph)
res_data = from_networkx(line_graph)

# Copy original attribtues
res_data.x = torch.stack([original_edge_to_attr[e] for e in line_graph.nodes])
res_data.y = data.y

I hope this helps someone in the future.
