r"""Converts a :class:`torch_geometric.data.Data` instance to a :obj:`easygraph.Graph` if :attr:`to_undirected` is set to :obj:`True`, or a directed :obj:`easygraph.DiGraph` otherwise. Args: data (torch_geometric.data.Data): The data object. node_attrs (iterable of str,
(
data: "torch_geometric.data.Data", # type: ignore
node_attrs: Optional[Iterable[str]] = None,
edge_attrs: Optional[Iterable[str]] = None,
graph_attrs: Optional[Iterable[str]] = None,
to_undirected: Optional[Union[bool, str]] = False,
remove_self_loops: bool = False,
)
| 446 | |
| 447 | |
| 448 | def from_pyg( |
| 449 | data: "torch_geometric.data.Data", # type: ignore |
| 450 | node_attrs: Optional[Iterable[str]] = None, |
| 451 | edge_attrs: Optional[Iterable[str]] = None, |
| 452 | graph_attrs: Optional[Iterable[str]] = None, |
| 453 | to_undirected: Optional[Union[bool, str]] = False, |
| 454 | remove_self_loops: bool = False, |
| 455 | ) -> Any: |
| 456 | r"""Converts a :class:`torch_geometric.data.Data` instance to a |
| 457 | :obj:`easygraph.Graph` if :attr:`to_undirected` is set to :obj:`True`, or |
| 458 | a directed :obj:`easygraph.DiGraph` otherwise. |
| 459 | |
| 460 | Args: |
| 461 | data (torch_geometric.data.Data): The data object. |
| 462 | node_attrs (iterable of str, optional): The node attributes to be |
| 463 | copied. (default: :obj:`None`) |
| 464 | edge_attrs (iterable of str, optional): The edge attributes to be |
| 465 | copied. (default: :obj:`None`) |
| 466 | graph_attrs (iterable of str, optional): The graph attributes to be |
| 467 | copied. (default: :obj:`None`) |
| 468 | to_undirected (bool or str, optional): If set to :obj:`True` or |
| 469 | "upper", will return a :obj:`easygraph.Graph` instead of a |
| 470 | :obj:`easygraph.DiGraph`. The undirected graph will correspond to |
| 471 | the upper triangle of the corresponding adjacency matrix. |
| 472 | Similarly, if set to "lower", the undirected graph will correspond |
| 473 | to the lower triangle of the adjacency matrix. (default: |
| 474 | :obj:`False`) |
| 475 | remove_self_loops (bool, optional): If set to :obj:`True`, will not |
| 476 | include self loops in the resulting graph. (default: :obj:`False`) |
| 477 | |
| 478 | Examples: |
| 479 | |
| 480 | >>> import torch_geometric as pyg |
| 481 | |
| 482 | >>> Data = pyg.data.Data # type: ignore |
| 483 | >>> edge_index = torch.tensor([ |
| 484 | ... [0, 1, 1, 2, 2, 3], |
| 485 | ... [1, 0, 2, 1, 3, 2], |
| 486 | ... ]) |
| 487 | >>> data = Data(edge_index=edge_index, num_nodes=4) |
| 488 | >>> from_pyg(data) |
| 489 | <easygraph.classes.digraph.DiGraph at 0x2713fdb40d0> |
| 490 | |
| 491 | """ |
| 492 | |
| 493 | try: |
| 494 | import torch_geometric as pyg |
| 495 | |
| 496 | pyg_to_networkx = pyg.utils.convert.to_networkx # type: ignore |
| 497 | networkx_to_pyg = pyg.utils.convert.from_networkx # type: ignore |
| 498 | except ImportError: |
| 499 | raise ImportError("pytorch_geometric not found. Please install it.") |
| 500 | g_nx = pyg_to_networkx( |
| 501 | data, node_attrs, edge_attrs, graph_attrs, to_undirected, remove_self_loops |
| 502 | ) |
| 503 | g_eg = from_networkx(g_nx) |
| 504 | return g_eg |
nothing calls this directly
no test coverage detected