dgl.bipartite_from_networkx

dgl.bipartite_from_networkx(nx_graph, utype, etype, vtype, u_attrs=None, e_attrs=None, v_attrs=None, edge_id_attr_name=None, idtype=None, device=None)[source]

Create a unidirectional bipartite graph from a NetworkX graph and return.

The created graph will have two types of nodes utype and vtype as well as one edge type etype whose edges are from utype to vtype.

Note

Creating a DGLGraph from a NetworkX graph is not fast especially for large scales. It is recommended to first convert a NetworkX graph into a tuple of node-tensors and then construct a DGLGraph with dgl.heterograph().

Parameters
  • nx_graph (networkx.DiGraph) – The NetworkX graph holding the graph structure and the node/edge attributes. DGL will relabel the nodes using consecutive integers starting from zero if it is not the case. The graph must follow NetworkX’s bipartite graph convention, and furthermore the edges must be from nodes with attribute bipartite=0 to nodes with attribute bipartite=1.

  • utype (str, optional) – The name of the source node type.

  • etype (str, optional) – The name of the edge type.

  • vtype (str, optional) – The name of the destination node type.

  • u_attrs (list[str], optional) – The names of the node attributes for node type utype to retrieve from the NetworkX graph. If given, DGL stores the retrieved node attributes in nodes[utype].data of the returned graph using their original names. The attribute data must be convertible to Tensor type (e.g., scalar, numpy.ndarray, list, etc.).

  • e_attrs (list[str], optional) – The names of the edge attributes to retrieve from the NetworkX graph. If given, DGL stores the retrieved edge attributes in edata of the returned graph using their original names. The attribute data must be convertible to Tensor type (e.g., scalar, numpy.ndarray, list, etc.).

  • v_attrs (list[str], optional) – The names of the node attributes for node type vtype to retrieve from the NetworkX graph. If given, DGL stores the retrieved node attributes in nodes[vtype].data of the returned graph using their original names. The attribute data must be convertible to Tensor type (e.g., scalar, numpy.array, list, etc.).

  • edge_id_attr_name (str, optional) – The name of the edge attribute that stores the edge IDs. If given, DGL will assign edge IDs accordingly when creating the graph, so the attribute must be valid IDs, i.e. consecutive integers starting from zero. By default, the edge IDs of the returned graph can be arbitrary.

  • idtype (int32 or int64, optional) – The data type for storing the structure-related graph information such as node and edge IDs. It should be a framework-specific data type object (e.g., torch.int32). By default, DGL uses int64.

  • device (device context, optional) – The device of the resulting graph. It should be a framework-specific device object (e.g., torch.device). By default, DGL stores the graph on CPU.

Returns

The created graph.

Return type

DGLGraph

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import networkx as nx
>>> import numpy as np
>>> import torch

Create a 2-edge unidirectional bipartite graph.

>>> nx_g = nx.DiGraph()
>>> # Add nodes for the source type
>>> nx_g.add_nodes_from([1, 3], bipartite=0, feat1=np.zeros((2, 1)), feat2=np.ones((2, 1)))
>>> # Add nodes for the destination type
>>> nx_g.add_nodes_from([2, 4, 5], bipartite=1, feat3=np.zeros((3, 1)))
>>> nx_g.add_edge(1, 4, weight=np.ones((1, 1)), eid=np.array([1]))
>>> nx_g.add_edge(3, 5, weight=np.ones((1, 1)), eid=np.array([0]))

Convert it into a DGLGraph with structure only.

>>> g = dgl.bipartite_from_networkx(nx_g, utype='_U', etype='_E', vtype='_V')

Retrieve the node/edge features of the graph.

>>> g = dgl.bipartite_from_networkx(nx_g, utype='_U', etype='_E', vtype='_V',
...                                 u_attrs=['feat1', 'feat2'],
...                                 e_attrs=['weight'],
...                                 v_attrs=['feat3'])

Use a pre-specified ordering of the edges.

>>> g.edges()
(tensor([0, 1]), tensor([1, 2]))
>>> g = dgl.bipartite_from_networkx(nx_g,
...                                 utype='_U', etype='_E', vtype='_V',
...                                 edge_id_attr_name='eid')
(tensor([1, 0]), tensor([2, 1]))

Create a graph on the first GPU with data type int32.

>>> g = dgl.bipartite_from_networkx(nx_g, utype='_U', etype='_E', vtype='_V',
...                                 idtype=torch.int32, device='cuda:0')