Repository: dmlc/dgl

Issue:
[Bug] DGLSubGraph.map_to_subgraph_nid error in v0.3
Hello,

The method map_to_subgraph_nid from DGLSubGraph seems to malfunction. 

The following example code:
`import dgl`
`g=dgl.DGLGraph()`
`g.add_nodes(10)`
`h=g.subgraph([0,1,2,5,8])`
`h.map_to_subgraph_nid([0,8,2])`


produces the following error chain: 
`File “.../python3.6/dist-packages/dgl/subgraph.py”, line 139, in map_to_subgraph_nid`
`    return map_to_subgraph_nid(self._graph,utils.toindex(parent_vids)).tousertensor()`
`File “.../python3.6/dist-packages/dgl/graph_index.py”, line 1137, in map_to_subgraph_nid`
`    return utils.toindex(_CAPI_DGLMapSubgraphNID(subgraph.induced_nodes.todgltensor(),`
`AttributeError: ‘GraphIndex’ object has no attribute ‘induced_nodes’
`

This bug does not occur in version 0.2 . The main difference I have found was that in v0.2 (in my code), `type(h._graph)` was 
`<class ‘dgl.graph_index.SubGraphIndex’>`, instead of 
`<class ‘dgl.graph_index.GraphIndex’>` as it is in v0.3 .

Am I using the library incorrectly? Is this behavior intended? If so, is there a replacement for the method?
Or is it a bug?

Thanks!
[Bug] DGLSubGraph.map_to_subgraph_nid error in v0.3
Hello,

The method map_to_subgraph_nid from DGLSubGraph seems to malfunction. 

The following example code:
`import dgl`
`g=dgl.DGLGraph()`
`g.add_nodes(10)`
`h=g.subgraph([0,1,2,5,8])`
`h.map_to_subgraph_nid([0,8,2])`


produces the following error chain: 
`File “.../python3.6/dist-packages/dgl/subgraph.py”, line 139, in map_to_subgraph_nid`
`    return map_to_subgraph_nid(self._graph,utils.toindex(parent_vids)).tousertensor()`
`File “.../python3.6/dist-packages/dgl/graph_index.py”, line 1137, in map_to_subgraph_nid`
`    return utils.toindex(_CAPI_DGLMapSubgraphNID(subgraph.induced_nodes.todgltensor(),`
`AttributeError: ‘GraphIndex’ object has no attribute ‘induced_nodes’
`

This bug does not occur in version 0.2 . The main difference I have found was that in v0.2 (in my code), `type(h._graph)` was 
`<class ‘dgl.graph_index.SubGraphIndex’>`, instead of 
`<class ‘dgl.graph_index.GraphIndex’>` as it is in v0.3 .

Am I using the library incorrectly? Is this behavior intended? If so, is there a replacement for the method?
Or is it a bug?

Thanks!


Discussion:
Thanks for reporting. Bug confirmed.
Hey @jermainewang, is there a way I could contribute to fix this issue? This issue prevents me from upgrading beyond 0.2
Thanks for reporting. Bug confirmed.
Hey @jermainewang, is there a way I could contribute to fix this issue? This issue prevents me from upgrading beyond 0.2

Resolving patch:
diff --git a/python/dgl/subgraph.py b/python/dgl/subgraph.py
--- a/python/dgl/subgraph.py
+++ b/python/dgl/subgraph.py
@@ -46,6 +46,7 @@ def __init__(self, parent, sgi, shared=False):
         self._parent = parent
         self._parent_nid = sgi.induced_nodes
         self._parent_eid = sgi.induced_edges
+        self._subgraph_index = sgi
 
     # override APIs
     def add_nodes(self, num, data=None):
@@ -136,4 +137,5 @@ def map_to_subgraph_nid(self, parent_vids):
         tensor
             The node ID array in the subgraph.
         """
-        return map_to_subgraph_nid(self._graph, utils.toindex(parent_vids)).tousertensor()
+        v = map_to_subgraph_nid(self._subgraph_index, utils.toindex(parent_vids))
+        return v.tousertensor()


Test patch:
diff --git a/tests/compute/test_subgraph.py b/tests/compute/test_subgraph.py
--- a/tests/compute/test_subgraph.py
+++ b/tests/compute/test_subgraph.py
@@ -76,6 +76,14 @@ def test_basics():
     sg.ndata['h'] = F.zeros((6, D))
     assert F.allclose(h, g.ndata['h'])
 
+def test_map_to_subgraph():
+    g = DGLGraph()
+    g.add_nodes(10)
+    g.add_edges(F.arange(0, 9), F.arange(1, 10))
+    h = g.subgraph([0, 1, 2, 5, 8])
+    v = h.map_to_subgraph_nid([0, 8, 2])
+    assert np.array_equal(F.asnumpy(v), np.array([0, 4, 2]))
+
 def test_merge():
     # FIXME: current impl cannot handle this case!!!
     #        comment out for now to test CI