MCPcopy
hub / github.com/dmlc/dgl / test_convert

Function test_convert

tests/python/common/test_heterograph.py:1300–1456  ·  view source on GitHub ↗
(idtype)

Source from the content-addressed store, hash-verified

1298
1299@parametrize_idtype
1300def test_convert(idtype):
1301 hg = create_test_heterograph(idtype)
1302 hs = []
1303 for ntype in hg.ntypes:
1304 h = F.randn((hg.num_nodes(ntype), 5))
1305 hg.nodes[ntype].data["h"] = h
1306 hs.append(h)
1307 hg.nodes["user"].data["x"] = F.randn((3, 3))
1308 ws = []
1309 for etype in hg.canonical_etypes:
1310 w = F.randn((hg.num_edges(etype), 5))
1311 hg.edges[etype].data["w"] = w
1312 ws.append(w)
1313 hg.edges["plays"].data["x"] = F.randn((4, 3))
1314
1315 g = dgl.to_homogeneous(hg, ndata=["h"], edata=["w"])
1316 assert g.idtype == idtype
1317 assert g.device == hg.device
1318 assert F.array_equal(F.cat(hs, dim=0), g.ndata["h"])
1319 assert "x" not in g.ndata
1320 assert F.array_equal(F.cat(ws, dim=0), g.edata["w"])
1321 assert "x" not in g.edata
1322
1323 src, dst = g.all_edges(order="eid")
1324 src = F.asnumpy(src)
1325 dst = F.asnumpy(dst)
1326 etype_id, eid = F.asnumpy(g.edata[dgl.ETYPE]), F.asnumpy(g.edata[dgl.EID])
1327 ntype_id, nid = F.asnumpy(g.ndata[dgl.NTYPE]), F.asnumpy(g.ndata[dgl.NID])
1328 for i in range(g.num_edges()):
1329 srctype = hg.ntypes[ntype_id[src[i]]]
1330 dsttype = hg.ntypes[ntype_id[dst[i]]]
1331 etype = hg.etypes[etype_id[i]]
1332 src_i, dst_i = hg.find_edges([eid[i]], (srctype, etype, dsttype))
1333 assert F.asnumpy(src_i).item() == nid[src[i]]
1334 assert F.asnumpy(dst_i).item() == nid[dst[i]]
1335
1336 mg = nx.MultiDiGraph(
1337 [
1338 ("user", "user", "follows"),
1339 ("user", "game", "plays"),
1340 ("user", "game", "wishes"),
1341 ("developer", "game", "develops"),
1342 ]
1343 )
1344
1345 for _mg in [None, mg]:
1346 hg2 = dgl.to_heterogeneous(
1347 g,
1348 hg.ntypes,
1349 hg.etypes,
1350 ntype_field=dgl.NTYPE,
1351 etype_field=dgl.ETYPE,
1352 metagraph=_mg,
1353 )
1354 assert hg2.idtype == hg.idtype
1355 assert hg2.device == hg.device
1356 assert set(hg.ntypes) == set(hg2.ntypes)
1357 assert set(hg.canonical_etypes) == set(hg2.canonical_etypes)

Callers

nothing calls this directly

Calls 12

appendMethod · 0.80
all_edgesMethod · 0.80
asnumpyMethod · 0.80
create_test_heterographFunction · 0.70
num_nodesMethod · 0.45
num_edgesMethod · 0.45
find_edgesMethod · 0.45
graphMethod · 0.45
ctxMethod · 0.45
copy_toMethod · 0.45
node_subgraphMethod · 0.45
keysMethod · 0.45

Tested by

no test coverage detected