"git@developer.sourcefind.cn:renzhc/diffusers_dcu.git" did not exist on "e9fda3924f180e6c9cf91fd6a5443147d1bf6d0e"
Unverified Commit 28a24414 authored by Quan (Andy) Gan's avatar Quan (Andy) Gan Committed by GitHub
Browse files

[BUG] Fix empty heterograph creation with list & tuple (#1188)

* [BUG] Fix empty heterograph creation with list & tuple

* more fix
parent aa06e457
......@@ -397,17 +397,22 @@ def heterograph(data_dict, num_nodes_dict=None):
num_nodes_dict = defaultdict(int)
for (srctype, etype, dsttype), data in data_dict.items():
if isinstance(data, tuple):
nsrc = max(data[0]) + 1
ndst = max(data[1]) + 1
nsrc = (max(data[0]) + 1) if len(data[0]) > 0 else 0
ndst = (max(data[1]) + 1) if len(data[1]) > 0 else 0
elif isinstance(data, list):
src, dst = zip(*data)
nsrc = max(src) + 1
ndst = max(dst) + 1
if len(data) == 0:
nsrc = ndst = 0
else:
src, dst = zip(*data)
nsrc = max(src) + 1
ndst = max(dst) + 1
elif isinstance(data, sp.sparse.spmatrix):
nsrc = data.shape[0]
ndst = data.shape[1]
elif isinstance(data, nx.Graph):
if srctype == dsttype:
if data.number_of_nodes() == 0:
nsrc = ndst = 0
elif srctype == dsttype:
nsrc = ndst = data.number_of_nodes()
else:
nsrc = len({n for n, d in data.nodes(data=True) if d['bipartite'] == 0})
......@@ -728,6 +733,7 @@ def create_from_edges(u, v, utype, etype, vtype, urange=None, vrange=None, valid
"""
u = utils.toindex(u)
v = utils.toindex(v)
if validate:
if urange is not None and urange <= int(F.asnumpy(F.max(u.tousertensor(), dim=0))):
raise DGLError('Invalid node id {} (should be less than cardinality {}).'.format(
......@@ -735,8 +741,11 @@ def create_from_edges(u, v, utype, etype, vtype, urange=None, vrange=None, valid
if vrange is not None and vrange <= int(F.asnumpy(F.max(v.tousertensor(), dim=0))):
raise DGLError('Invalid node id {} (should be less than cardinality {}).'.format(
vrange, int(F.asnumpy(F.max(v.tousertensor(), dim=0)))))
urange = urange or (int(F.asnumpy(F.max(u.tousertensor(), dim=0))) + 1)
vrange = vrange or (int(F.asnumpy(F.max(v.tousertensor(), dim=0))) + 1)
urange = urange or (
0 if len(u) == 0 else (int(F.asnumpy(F.max(u.tousertensor(), dim=0))) + 1))
vrange = vrange or (
0 if len(v) == 0 else (int(F.asnumpy(F.max(v.tousertensor(), dim=0))) + 1))
if utype == vtype:
urange = vrange = max(urange, vrange)
num_ntypes = 1
......
......@@ -1230,6 +1230,33 @@ def test_backward():
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]]))
def test_empty_heterograph():
def assert_empty(g):
assert g.number_of_nodes('user') == 0
assert g.number_of_edges('plays') == 0
assert g.number_of_nodes('game') == 0
# empty edge list
assert_empty(dgl.heterograph({('user', 'plays', 'game'): []}))
# empty src-dst pair
assert_empty(dgl.heterograph({('user', 'plays', 'game'): ([], [])}))
# empty sparse matrix
assert_empty(dgl.heterograph({('user', 'plays', 'game'): ssp.coo_matrix((0, 0))}))
# empty networkx graph
assert_empty(dgl.heterograph({('user', 'plays', 'game'): nx.DiGraph()}))
g = dgl.heterograph({('user', 'follows', 'user'): []})
assert g.number_of_nodes('user') == 0
assert g.number_of_edges('follows') == 0
# empty relation graph with others
g = dgl.heterograph({('user', 'plays', 'game'): [], ('developer', 'develops', 'game'): [(0, 0), (1, 1)]})
assert g.number_of_nodes('user') == 0
assert g.number_of_edges('plays') == 0
assert g.number_of_nodes('game') == 2
assert g.number_of_edges('develops') == 2
assert g.number_of_nodes('developer') == 2
if __name__ == '__main__':
test_create()
test_query()
......@@ -1247,3 +1274,4 @@ if __name__ == '__main__':
test_level2()
test_updates()
test_backward()
test_empty_heterograph()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment