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

Function _coalesce_edge_frame

python/dgl/transforms/functional.py:2315–2368  ·  view source on GitHub ↗

r"""Coalesce edge features of duplicate edges via given aggregator in g. Parameters ---------- g : DGLGraph The input graph. edge_maps : List[Tensor] The edge mapping corresponding to each edge type in g. counts : List[Tensor] The number of duplicated edg

(g, edge_maps, counts, aggregator)

Source from the content-addressed store, hash-verified

2313
2314
2315def _coalesce_edge_frame(g, edge_maps, counts, aggregator):
2316 r"""Coalesce edge features of duplicate edges via given aggregator in g.
2317
2318 Parameters
2319 ----------
2320 g : DGLGraph
2321 The input graph.
2322 edge_maps : List[Tensor]
2323 The edge mapping corresponding to each edge type in g.
2324 counts : List[Tensor]
2325 The number of duplicated edges from the original graph for each edge type.
2326 aggregator : str
2327 Indicates how to coalesce edge features, could be ``arbitrary``, ``sum``
2328 or ``mean``.
2329
2330 Returns
2331 -------
2332 List[Frame]
2333 The frames corresponding to each edge type.
2334 """
2335 if aggregator == "arbitrary":
2336 eids = []
2337 for i in range(len(g.canonical_etypes)):
2338 feat_idx = F.asnumpy(edge_maps[i])
2339 _, indices = np.unique(feat_idx, return_index=True)
2340 eids.append(F.zerocopy_from_numpy(indices))
2341
2342 edge_frames = utils.extract_edge_subframes(g, eids)
2343 elif aggregator in ["sum", "mean"]:
2344 edge_frames = []
2345 for i in range(len(g.canonical_etypes)):
2346 feat_idx = edge_maps[i]
2347 _, indices = np.unique(F.asnumpy(feat_idx), return_index=True)
2348 _num_rows = len(indices)
2349 _data = {}
2350 for key, col in g._edge_frames[i]._columns.items():
2351 data = col.data
2352 new_data = F.scatter_add(data, feat_idx, _num_rows)
2353 if aggregator == "mean":
2354 norm = F.astype(counts[i], F.dtype(data))
2355 norm = F.reshape(
2356 norm, (F.shape(norm)[0],) + (1,) * (F.ndim(data) - 1)
2357 )
2358 new_data /= norm
2359 _data[key] = new_data
2360
2361 newf = Frame(data=_data, num_rows=_num_rows)
2362 edge_frames.append(newf)
2363 else:
2364 raise DGLError(
2365 "Aggregator {} not regonized, cannot coalesce edge feature in the "
2366 "specified way".format(aggregator)
2367 )
2368 return edge_frames
2369
2370
2371def to_simple(

Callers 1

to_simpleFunction · 0.85

Calls 9

FrameClass · 0.85
DGLErrorClass · 0.85
asnumpyMethod · 0.80
appendMethod · 0.80
formatMethod · 0.80
itemsMethod · 0.45
astypeMethod · 0.45
dtypeMethod · 0.45
shapeMethod · 0.45

Tested by

no test coverage detected