(relabel_nodes)
| 56 | |
| 57 | @pytest.mark.parametrize("relabel_nodes", [True, False]) |
| 58 | def test_subgraph_relabel_nodes(relabel_nodes): |
| 59 | g = generate_graph() |
| 60 | h = g.ndata["h"] |
| 61 | l = g.edata["l"] |
| 62 | nid = [0, 2, 3, 6, 7, 9] |
| 63 | sg = g.subgraph(nid, relabel_nodes=relabel_nodes) |
| 64 | eid = {2, 3, 4, 5, 10, 11, 12, 13, 16} |
| 65 | assert set(F.asnumpy(sg.edata[dgl.EID])) == eid |
| 66 | eid = sg.edata[dgl.EID] |
| 67 | # the subgraph is empty initially except for EID field |
| 68 | # the subgraph is empty initially except for NID field if relabel_nodes |
| 69 | if relabel_nodes: |
| 70 | assert len(sg.ndata) == 2 |
| 71 | assert len(sg.edata) == 2 |
| 72 | sh = sg.ndata["h"] |
| 73 | # The node number is not reduced if relabel_node=False. |
| 74 | # The subgraph keeps the same node information as the original graph. |
| 75 | if relabel_nodes: |
| 76 | assert F.allclose(F.gather_row(h, F.tensor(nid)), sh) |
| 77 | else: |
| 78 | assert F.allclose( |
| 79 | F.gather_row(h, F.tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])), sh |
| 80 | ) |
| 81 | # The s,d,eid means the source node, destination node and edge id of the subgraph. |
| 82 | # The edges labeled 1 are those selected by the subgraph. |
| 83 | """ |
| 84 | s, d, eid |
| 85 | 0, 1, 0 |
| 86 | 1, 9, 1 |
| 87 | 0, 2, 2 1 |
| 88 | 2, 9, 3 1 |
| 89 | 0, 3, 4 1 |
| 90 | 3, 9, 5 1 |
| 91 | 0, 4, 6 |
| 92 | 4, 9, 7 |
| 93 | 0, 5, 8 |
| 94 | 5, 9, 9 3 |
| 95 | 0, 6, 10 1 |
| 96 | 6, 9, 11 1 3 |
| 97 | 0, 7, 12 1 |
| 98 | 7, 9, 13 1 3 |
| 99 | 0, 8, 14 |
| 100 | 8, 9, 15 3 |
| 101 | 9, 0, 16 1 |
| 102 | """ |
| 103 | assert F.allclose(F.gather_row(l, eid), sg.edata["l"]) |
| 104 | # update the node/edge features on the subgraph should NOT |
| 105 | # reflect to the parent graph. |
| 106 | if relabel_nodes: |
| 107 | sg.ndata["h"] = F.zeros((6, D)) |
| 108 | else: |
| 109 | sg.ndata["h"] = F.zeros((10, D)) |
| 110 | assert F.allclose(h, g.ndata["h"]) |
| 111 | |
| 112 | |
| 113 | def _test_map_to_subgraph(): |
nothing calls this directly
no test coverage detected