How to map edge indices in block to parent graph

g = dgl.graph([(0, 2), (0, 3), (1, 2)])
frontier = dgl.sampling.sample_neighbors(g, torch.tensor([3]), fanout=5)
block = dgl.to_block(frontier, torch.tensor([3]))
print(block.srcdata[dgl.NID])
print(block.dstdata[dgl.NID])
print(block.edata[dgl.EID])
print(block[’_E’].edata[dgl.EID])

I want to get the node indices and edge indices in the parent graph. In line 4 and line 5 I get [3, 0] and [3], that’s right. But in line 6 and line 7 I get [0] and [0], that’s not the edge indicies in parent graph. So how can I get the edge indices in the parent graph?

1 Like

That looks like a bug. Would you mind raising an issue to our github? We will look into this. Thanks.

OK :rofl: And I find a temporary solution, frontier.edata[dgl.EID] returns the right answer.

Well, maybe the following codes can solve this problem. But I’m not sure.

src, dst = block.all_edges(order='eid')
raw_src, raw_dst = block.srcdata[dgl.NID][src], block.dstdata[dgl.NID][dst]
edge_ids = raw_graph.edge_ids(raw_src, raw_dst)

Currently in DGL, sample_neighbors and to_block records the edge mapping to the input graph. So frontier stores an edge mapping to g, while block stores an edge mapping to frontier. Therefore to get the true edge mapping from block to g you need to perform the mapping twice.

frontier.edata[dgl.EID][block.edata[dgl.EID]]

See also https://github.com/dmlc/dgl/issues/1450.