How to convert from a heterogeneous graph to a homogeneous graph with data?

I want to convert from a heterogeneous graph to a homogeneous graph while maintaining edata.
I referred to the two documents below.
However, it failed to pass the edata together.

>>> g = dgl.heterograph({
...    ('drug', 'interacts', 'drug'): (th.tensor([0, 1]), th.tensor([1, 2])),
...    ('drug', 'treats', 'disease'): (th.tensor([1]), th.tensor([2]))})

>>> g.nodes['drug'].data['hv'] = th.zeros(3, 1)
>>> g.nodes['disease'].data['hv'] = th.ones(3, 1)
>>> g.edges['interacts'].data['he'] = th.zeros(2, 1)

>>> hg = dgl.to_homogeneous(g, edata=['he'])
>>> hg.edata
{'_ID': tensor([0, 1, 0]), '_TYPE': tensor([0, 0, 1])} # there is no edata['he']

However, it is possible to pass ndata.

>>> hg = dgl.to_homogeneous(g, ndata=['hv'])
>>> hg.ndata
{'hv': tensor([[1.],
        [1.],
        [1.],
        [0.],
        [0.],
        [0.]]), '_ID': tensor([0, 1, 2, 0, 1, 2]), '_TYPE': tensor([0, 0, 0, 1, 1, 1])}

How can I solve it?

https://docs.dgl.ai/en/latest/generated/dgl.to_homogeneous.html?highlight=to_homogeneous
https://docs.dgl.ai/en/latest/guide/graph-heterogeneous.html?highlight=to_homogeneous

1 Like

Oh, I solved it!
This is because edata must exist for all edges to pass edata.

When I put edata on the ‘treats’ edge, it was solved.

>>> g.edges['treats'].data['he'] = th.zeros(1, 1)
>>> hg = dgl.to_homogeneous(g, edata=['he'])
>>> hg.edata
{'he': tensor([[0.],
        [0.],
        [0.]]), '_ID': tensor([0, 1, 0]), '_TYPE': tensor([0, 0, 1])}
1 Like

It looks like something we could improve. I don’t like this silent behavior either. How about:

  • If edata or ndata is specified in to_homogeneous, DGL will check if all the node types or edge types contain the specified feature names. If no, throw error.
  • Add a padding=False option to to_homogeneous. If True, DGL will automatically pad zero values to mis-aligned features.
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.