dmlc / dgl

Python package built to ease deep learning on graph, on top of existing DL frameworks.
http://dgl.ai
Apache License 2.0
13.56k stars 3.02k forks source link

combine_frames doesn't allow for no edges #7826

Open lilyminium opened 1 month ago

lilyminium commented 1 month ago

🐛 Bug

To Reproduce

This is a niche corner case, but hopefully one with an easy-ish solution. If I create a DGL heterograph with no edges, trying to convert it with to_homogeneous raises an error from having no edges.

import dgl

heterograph = dgl.heterograph(
    {("node", "forward", "node"): ([], []), ("node", "reverse", "node"): ([], [])},
    num_nodes_dict={"node": 1}
)
dgl.to_homogeneous(heterograph)

raises the error:

TypeError                                 Traceback (most recent call last)
Cell In[24], line 1
----> 1 dgl.to_homogeneous(heterograph)

File dgl/convert.py:1026, in to_homogeneous(G, ndata, edata, store_type, return_count)
   1022     edata = []
   1023 comb_nf = combine_frames(
   1024     G._node_frames, range(len(G.ntypes)), col_names=ndata
   1025 )
-> 1026 comb_ef = combine_frames(
   1027     G._edge_frames, range(len(G.etypes)), col_names=edata
   1028 )
   1029 if comb_nf is not None:
   1030     retg.ndata.update(comb_nf)

File dgl/heterograph.py:6579, in combine_frames(frames, ids, col_names)
   6576         else:
   6577             del schemes[key]
-> 6579 if len(schemes) == 0:
   6580     return None
   6582 # concatenate the columns

TypeError: object of type 'NoneType' has no len()

Expected behavior

I was hoping DGL would simply be able to batch this through with all the other normal graphs. I think the error is due to this initial definition of schemes as None, and then skipping over each frame as they have 0 rows.

https://github.com/dmlc/dgl/blob/d92c98d927d6ef0c0bcb7475194495f6f417e091/python/dgl/heterograph.py#L6581

Would it be possible to incorporate a check for None for schemes?

Environment

Additional context