Create a uni-directional bipartite graph from a SciPy sparse matrix 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``. Parameters ---------- sp_mat : scipy.sparse.spm
(
sp_mat, utype, etype, vtype, eweight_name=None, idtype=None, device=None
)
| 1147 | |
| 1148 | |
| 1149 | def bipartite_from_scipy( |
| 1150 | sp_mat, utype, etype, vtype, eweight_name=None, idtype=None, device=None |
| 1151 | ): |
| 1152 | """Create a uni-directional bipartite graph from a SciPy sparse matrix and return. |
| 1153 | |
| 1154 | The created graph will have two types of nodes ``utype`` and ``vtype`` as well as one |
| 1155 | edge type ``etype`` whose edges are from ``utype`` to ``vtype``. |
| 1156 | |
| 1157 | Parameters |
| 1158 | ---------- |
| 1159 | sp_mat : scipy.sparse.spmatrix |
| 1160 | The graph adjacency matrix. Each nonzero entry ``sp_mat[i, j]`` |
| 1161 | represents an edge from node ``i`` of type :attr:`utype` to ``j`` of type :attr:`vtype`. |
| 1162 | Let the matrix shape be ``(N, M)``. There will be ``N`` nodes of type :attr:`utype` |
| 1163 | and ``M`` nodes of type ``vtype`` in the resulting graph. |
| 1164 | utype : str, optional |
| 1165 | The name of the source node type. |
| 1166 | etype : str, optional |
| 1167 | The name of the edge type. |
| 1168 | vtype : str, optional |
| 1169 | The name of the destination node type. |
| 1170 | eweight_name : str, optional |
| 1171 | The edata name for storing the nonzero values of :attr:`sp_mat`. |
| 1172 | If given, DGL will store the nonzero values of :attr:`sp_mat` in ``edata[eweight_name]`` |
| 1173 | of the returned graph. |
| 1174 | idtype : int32 or int64, optional |
| 1175 | The data type for storing the structure-related graph information such as node and |
| 1176 | edge IDs. It should be a framework-specific data type object (e.g., ``torch.int32``). |
| 1177 | By default, DGL uses int64. |
| 1178 | device : device context, optional |
| 1179 | The device of the resulting graph. It should be a framework-specific device object |
| 1180 | (e.g., ``torch.device``). By default, DGL stores the graph on CPU. |
| 1181 | |
| 1182 | Returns |
| 1183 | ------- |
| 1184 | DGLGraph |
| 1185 | The created graph. |
| 1186 | |
| 1187 | Notes |
| 1188 | ----- |
| 1189 | 1. The function supports all kinds of SciPy sparse matrix classes (e.g., |
| 1190 | :class:`scipy.sparse.csr.csr_matrix`). It converts the input matrix to the COOrdinate |
| 1191 | format using :func:`scipy.sparse.spmatrix.tocoo` before creates a :class:`DGLGraph`. |
| 1192 | Creating from a :class:`scipy.sparse.coo.coo_matrix` is hence the most efficient way. |
| 1193 | 2. DGL internally maintains multiple copies of the graph structure in different sparse |
| 1194 | formats and chooses the most efficient one depending on the computation invoked. |
| 1195 | If memory usage becomes an issue in the case of large graphs, use |
| 1196 | :func:`dgl.DGLGraph.formats` to restrict the allowed formats. |
| 1197 | |
| 1198 | Examples |
| 1199 | -------- |
| 1200 | |
| 1201 | The following example uses PyTorch backend. |
| 1202 | |
| 1203 | >>> import dgl |
| 1204 | >>> import numpy as np |
| 1205 | >>> import torch |
| 1206 | >>> from scipy.sparse import coo_matrix |
nothing calls this directly
no test coverage detected