dgl.DGLGraph.formats

DGLGraph.formats(formats=None)[source]

Get a cloned graph with the specified sparse format(s) or query for the usage status of sparse formats

The API copies both the graph structure and the features.

If the input graph has multiple edge types, they will have the same sparse format.

Parameters

formats (str or list of str or None) –

  • If formats is None, return the usage status of sparse formats

  • Otherwise, it can be 'coo'/'csr'/'csc' or a sublist of them, specifying the sparse formats to use.

Returns

  • If formats is None, the result will be a dict recording the usage status of sparse formats.

  • Otherwise, a DGLGraph will be returned, which is a clone of the original graph with the specified sparse format(s) formats.

Return type

dict or DGLGraph

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Homographs or Heterographs with A Single Edge Type

>>> g = dgl.graph(([0, 0, 1], [2, 3, 2]))
>>> g.ndata['h'] = torch.ones(4, 1)
>>> # Check status of format usage
>>> g.formats()
{'created': ['coo'], 'not created': ['csr', 'csc']}
>>> # Get a clone of the graph with 'csr' format
>>> csr_g = g.formats('csr')
>>> # Only allowed formats will be displayed in the status query
>>> csr_g.formats()
{'created': ['csr'], 'not created': []}
>>> # Features are copied as well
>>> csr_g.ndata['h']
tensor([[1.],
        [1.],
        [1.],
        [1.]])

Heterographs with Multiple Edge Types

>>> g = dgl.heterograph({
...     ('user', 'plays', 'game'): (torch.tensor([0, 1, 1, 2]),
...                                 torch.tensor([0, 0, 1, 1])),
...     ('developer', 'develops', 'game'): (torch.tensor([0, 1]),
...                                         torch.tensor([0, 1]))
...     })
>>> g.formats()
{'created': ['coo'], 'not created': ['csr', 'csc']}
>>> # Get a clone of the graph with 'csr' format
>>> csr_g = g.formats('csr')
>>> # Only allowed formats will be displayed in the status query
>>> csr_g.formats()
{'created': ['csr'], 'not created': []}