dmlc / dmlc/dgl

Sort IDs when using dgl.to_heterogeneous()

Open
#2,445 1 comment 0 reactions 0 assignees View on GitHub
feature request
Dominant language
Python
Stars
14.3k
Forks
3.1k
PR merge metrics
No merged PRs in 30d

Description

## 🚀 Feature

Sort output IDs when using dgl.to_heterogeneous()

## Motivation

When transforming a heterograph to homogeneous and back to heterogenous, the IDs are not sorted, which can cause confusion.

In the code below, you see this output:

```
NodeSpace(data={
'data': tensor([[-1., -1.],
[-1., 0.],
[-1., -1.]]),
'_ID': tensor([31, 30, 32])
})
```

The [-1,0] should be in index 0. I thought it was a bug, until I realized that '_ID' is out of order [31, 30, 32]. If I used the data as is, it would cause a bug that would be hard to find.

```

import dgl
import torch

hg = dgl.heterograph({
('user', 'plays', 'game'): (torch.tensor([1, 2, 1]), torch.tensor([14, 4, 9])),
('store', 'sells', 'game'): (torch.tensor([14, 5, 7]), torch.tensor([14, 9, 11])),
})

data = {'game': torch.tensor([[ 0., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.]]),
'store': torch.tensor([[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.],
[-1., -1.]]),
'user': torch.tensor([[-1., 0.],
[-1., -1.],
[-1., -1.]])}

for node_type in hg.ntypes:
hg.nodes[node_type].data['data'] = data[node_type]

g = dgl.to_homogeneous(hg, ndata = ['data'])
hg2 = dgl.to_heterogeneous(g, ntypes = hg.ntypes, etypes = hg.etypes)

print(hg.nodes['user'])
print(hg2.nodes['user'])
```

print
```
NodeSpace(data={'data': tensor([[-1., 0.],
[-1., -1.],
[-1., -1.]])})
NodeSpace(data={'data': tensor([[-1., -1.],
[-1., 0.],
[-1., -1.]]), '_ID': tensor([31, 30, 32])})
```

## Alternatives

If doing it automatically can create an overhead, then add an argument: sort_output_ids

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.