dgl.DGLGraph.to_canonical_etype

DGLGraph.to_canonical_etype(etype)[source]

Convert an edge type to the corresponding canonical edge type in the graph.

A canonical edge type is a string triplet (str, str, str) for source node type, edge type and destination node type.

The function expects the given edge type name can uniquely identify a canonical edge type. DGL will raise error if this is not the case.

Parameters

etype (str or (str, str, str)) – If etype is an edge type (str), it returns the corresponding canonical edge type in the graph. If etype is already a canonical edge type, it directly returns the input unchanged.

Returns

The canonical edge type corresponding to the edge type.

Return type

(str, str, str)

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Create a heterograph.

>>> g = dgl.heterograph({
...     ('user', 'follows', 'user'): ([0, 1], [1, 2]),
...     ('user', 'plays', 'game'): ([0, 1, 1, 2], [0, 0, 1, 1]),
...     ('developer', 'follows', 'game'): ([0, 1], [0, 1])
... })

Map an edge type to its corresponding canonical edge type.

>>> g.to_canonical_etype('plays')
('user', 'plays', 'game')
>>> g.to_canonical_etype(('user', 'plays', 'game'))
('user', 'plays', 'game')

See also

canonical_etypes