dgl.remove_nodes() on heterogeneous graph updates node data for other node types
- Dominant language
- Python
- Stars
- 14.3k
- Forks
- 3.1k
- PR merge metrics
- No merged PRs in 30d
Description
## 🐛 Bug
When removing nodes of a particular node type in a heterogeneous graph, the other node types have `dgl.NID` updated as well even if they are not the node type being removed.
## To Reproduce
Steps to reproduce the behavior:
Modified from the DGL documentation for dgl.remove_nodes():
```
g = dgl.heterograph({
('user', 'plays', 'game'): (torch.tensor([0, 1, 1, 2]),
torch.tensor([0, 0, 1, 1])),
('developer', 'develops', 'game'): (torch.tensor([0, 1]),
torch.tensor([0, 1]))
})
g = dgl.remove_nodes(g, torch.tensor([0]), ntype='game', store_ids=True)
```
From here, if one does `g.nodes['game'].data[dgl.NID]`, we get the expected result `tensor([1])` (as node 0 was removed, node 1 became node 0 ID-wise and its former ID was stored as 1 in `dgl.NID`).
However, if we now remove a node of a different node type:
`g = dgl.remove_nodes(g, torch.tensor([0]), ntype='developer', store_ids=True)`
and perform `g.nodes['game'].data[dgl.NID]`, we get `tensor([0])` rather than the expected `tensor([1])` (expected since the 'game' node type was untouched in the above operation.
Full code example:
```
g = dgl.heterograph({
('user', 'plays', 'game'): (torch.tensor([0, 1, 1, 2]),
torch.tensor([0, 0, 1, 1])),
('developer', 'develops', 'game'): (torch.tensor([0, 1]),
torch.tensor([0, 1]))
})
g = dgl.remove_nodes(g, torch.tensor([0]), ntype='game', store_ids=True)
g.nodes['game'].data[dgl.NID]
g = dgl.remove_nodes(g, torch.tensor([0]), ntype='developer', store_ids=True)
g.nodes['game'].data[dgl.NID]
```
## Expected behavior
See above. The stored node IDs should not be updated (or stored) for node types that are not removed. You can only remove nodes of one particular node type in remove_nodes, so if you need to remove nodes of multiple node types sequentially (for example, dropping orphaned nodes for each node type) but need to be able to access the original node IDs, you will run into this issue.
## Environment
- DGL Version (e.g., 1.0): 0.9.0
- Backend Library & Version (e.g., PyTorch 0.4.1, MXNet/Gluon 1.3): PyTorch 1.12.1
- OS (e.g., Linux): Mac OSX
- How you installed DGL (`conda`, `pip`, source): pip
- Python version: 3.8.3
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.