Create a graph from a NetworkX graph and return. .. 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 :func:`dgl.gr
(
nx_graph,
node_attrs=None,
edge_attrs=None,
edge_id_attr_name=None,
idtype=None,
device=None,
)
| 1254 | |
| 1255 | |
| 1256 | def from_networkx( |
| 1257 | nx_graph, |
| 1258 | node_attrs=None, |
| 1259 | edge_attrs=None, |
| 1260 | edge_id_attr_name=None, |
| 1261 | idtype=None, |
| 1262 | device=None, |
| 1263 | ): |
| 1264 | """Create a graph from a NetworkX graph and return. |
| 1265 | |
| 1266 | .. note:: |
| 1267 | Creating a DGLGraph from a NetworkX graph is not fast especially for large scales. |
| 1268 | It is recommended to first convert a NetworkX graph into a tuple of node-tensors |
| 1269 | and then construct a DGLGraph with :func:`dgl.graph`. |
| 1270 | |
| 1271 | Parameters |
| 1272 | ---------- |
| 1273 | nx_graph : networkx.Graph |
| 1274 | The NetworkX graph holding the graph structure and the node/edge attributes. |
| 1275 | DGL will relabel the nodes using consecutive integers starting from zero if it is |
| 1276 | not the case. If the input graph is undirected, DGL converts it to a directed graph |
| 1277 | by :func:`networkx.Graph.to_directed`. |
| 1278 | node_attrs : list[str], optional |
| 1279 | The names of the node attributes to retrieve from the NetworkX graph. If given, DGL |
| 1280 | stores the retrieved node attributes in ``ndata`` of the returned graph using their |
| 1281 | original names. The attribute data must be convertible to Tensor type (e.g., scalar, |
| 1282 | numpy.ndarray, list, etc.). |
| 1283 | edge_attrs : list[str], optional |
| 1284 | The names of the edge attributes to retrieve from the NetworkX graph. If given, DGL |
| 1285 | stores the retrieved edge attributes in ``edata`` of the returned graph using their |
| 1286 | original names. The attribute data must be convertible to Tensor type (e.g., scalar, |
| 1287 | ``numpy.ndarray``, list, etc.). It must be None if :attr:`nx_graph` is undirected. |
| 1288 | edge_id_attr_name : str, optional |
| 1289 | The name of the edge attribute that stores the edge IDs. If given, DGL will assign edge |
| 1290 | IDs accordingly when creating the graph, so the attribute must be valid IDs, i.e. |
| 1291 | consecutive integers starting from zero. By default, the edge IDs of the returned graph |
| 1292 | can be arbitrary. It must be None if :attr:`nx_graph` is undirected. |
| 1293 | idtype : int32 or int64, optional |
| 1294 | The data type for storing the structure-related graph information such as node and |
| 1295 | edge IDs. It should be a framework-specific data type object (e.g., ``torch.int32``). |
| 1296 | By default, DGL uses int64. |
| 1297 | device : device context, optional |
| 1298 | The device of the resulting graph. It should be a framework-specific device object |
| 1299 | (e.g., ``torch.device``). By default, DGL stores the graph on CPU. |
| 1300 | |
| 1301 | Returns |
| 1302 | ------- |
| 1303 | DGLGraph |
| 1304 | The created graph. |
| 1305 | |
| 1306 | Notes |
| 1307 | ----- |
| 1308 | DGL internally maintains multiple copies of the graph structure in different sparse |
| 1309 | formats and chooses the most efficient one depending on the computation invoked. |
| 1310 | If memory usage becomes an issue in the case of large graphs, use |
| 1311 | :func:`dgl.DGLGraph.formats` to restrict the allowed formats. |
| 1312 | |
| 1313 | Examples |
no test coverage detected