dgl.DGLHeteroGraph.to_format

DGLHeteroGraph.to_format(restrict_format)[source]

Return a cloned graph but stored in the given restrict format.

If 'any' is given, the restrict formats of the returned graph is relaxed. The returned graph share the same node/edge data of the original graph.

Parameters:restrict_format (str) – Desired restrict format ('any', 'coo', 'csr', 'csc').
Returns:
Return type:A new graph.

Examples

For a graph with single edge type:

>>> g = dgl.graph([(0, 1), (1, 2)], 'user', 'follows', restrict_format='csr')
>>> g.ndata['h'] = th.ones(3, 3)
>>> g.restrict_format()
'csr'
>>> g1 = g.to_format('coo')
>>> g1.ndata
{'h': tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])}
>>> g1.restrict_format()
'coo'

For a graph with multiple edge types:

>>> g = dgl.heterograph({
...     ('user', 'plays', 'game'): ([0, 1, 1, 2], [0, 0, 1, 1]),
...     ('developer', 'develops', 'game'): ([0, 1], [0, 1]),
...     }, restrict_format='coo')
>>> g.restrict_format('develops')
'coo'
>>> g1 = g.to_format('any')
>>> g1.restrict_format('plays')
'any'

See also

format_in_use(), restrict_format(), request_format()