Error occured when converting networkx graph to dgl graph

Hi,

I encountered an error when trying converting a graph of the type <class ‘networkx.classes.digraph.DiGraph’> to a dgl graph via dgl.DGLgraph() function or .from_networkx() method of a dgl.graph.DGLGraph object. The graph is given by calling the .to_networkx() method of another dgl.graph.DGLGraph object.

To be short, the error occured when I try converting networkx graph given by a dgl graph to a new dgl graph.

You may see the details from the attached following screenshots.



Hope you can help me! Thanks!

It seems that your networkx graph has non-contiguous node ids, i.e. node ids are not 0, 1, …, number of nodes - 1. DGL requires the input graph to have contiguous node ids.

Hi,

Thank you for your reply! But the node ids are contiguous:

Screen Shot 2020-04-09 at 10.13.15 PM

My bad. It’s actually the edge id that is out of the bound.

  1. Our current version is 0.4.3. You can try uninstalling 0.4.2 and installing the latest version. See if the issue still exists.
  2. If the issue still exists, for L15, you can try:
cocs_1 = dgl.DGLGraph()
cocs_1.from(cocs_nx)

instead.

I noticed that you are doing a series of conversions with DGLGraph->networkx.DiGraph->DGLGraph. Any particular reason for doing so?

Hi,

Thank you for your reply! Updating to 0.4.3 helps :grin:

I am doing this because I want to do some graph-level operations to this graph first (e.g. removing some nodes or edges etc.), which seems to be out of dgl’s scope, then convert the processed graph back to a dgl graph and feed it to a GNN model. I really hope dgl can provide with more graph-level operations so that I do not need such cumbersome conversions :rofl:

Thank you very much!

Actually you can perform operations like remove_nodes and remove_edges. See the example below:

import dgl

g = dgl.DGLGraph()
g.add_nodes(10)
g.add_edges([2, 3, 4], [5, 6, 7])
print(g) # DGLGraph(num_nodes=10, num_edges=3, ndata_schemes={} edata_schemes={})
g.remove_nodes([8, 9])
print(g) # DGLGraph(num_nodes=8, num_edges=3, ndata_schemes={} edata_schemes={})
g.remove_edges([0, 2]) # Remove edges with id 0 and 2
print(g.edges()) # (tensor([3]), tensor([6]))

Wow these are really great functions :grin:.
I had been using networkx to do these things following the gcn example here:https://github.com/dmlc/dgl/blob/master/examples/pytorch/gcn/train.py
(line 63 - 68).

Maybe this example should be updated :grin:

Thank you very much!