Skip to content

to_undirected

to_undirected(
    edge_index: array,
    edge_features=None,
    coalesce: bool = False,
    num_nodes: Optional[int] = None,
    reduce: str = "add",
) -> array
to_undirected(
    edge_index: array,
    edge_features: array,
    coalesce: bool = False,
    num_nodes: Optional[int] = None,
    reduce: str = "add",
) -> tuple[array, array]
to_undirected(
    edge_index: array,
    edge_features: Optional[array] = None,
    coalesce: bool = False,
    num_nodes: Optional[int] = None,
    reduce: str = "add",
) -> Union[array, tuple[array, array]]

Converts a graph given as edge_index and, optionally, edge_features to an undirected one, by adding the reverse of every edge.

Parameters:

Name Type Description Default
edge_index array

The edge index of the graph.

required
edge_features Optional[array]

Edge features associated with each edge.

None
coalesce bool

If True, duplicate edges are merged afterwards (see coalesce()), so an edge that already had its reverse is kept once per direction. This is the behavior of PyG's to_undirected. Default: False (plain concatenation).

False
num_nodes Optional[int]

Number of nodes, only used when coalesce=True.

None
reduce str

How the features of merged edges are combined when coalesce=True. Default: "add".

'add'

Returns:

Type Description
Union[array, tuple[array, array]]

The undirected edge_index (and edge_features)

Source code in mlx_graphs/utils/transformations.py
@validate_edge_index_and_features
def to_undirected(
    edge_index: mx.array,
    edge_features: Optional[mx.array] = None,
    coalesce: bool = False,
    num_nodes: Optional[int] = None,
    reduce: str = "add",
) -> Union[mx.array, tuple[mx.array, mx.array]]:
    """
    Converts a graph given as `edge_index` and, optionally, `edge_features`
    to an undirected one, by adding the reverse of every edge.

    Args:
        edge_index: The edge index of the graph.
        edge_features: Edge features associated with each edge.
        coalesce: If ``True``, duplicate edges are merged afterwards (see
            :func:`coalesce`), so an edge that already had its reverse is kept
            once per direction. This is the behavior of PyG's
            ``to_undirected``. Default: ``False`` (plain concatenation).
        num_nodes: Number of nodes, only used when ``coalesce=True``.
        reduce: How the features of merged edges are combined when
            ``coalesce=True``. Default: ``"add"``.

    Returns:
        The undirected `edge_index` (and `edge_features`)
    """

    src, dst = edge_index
    undirected_edge_index = mx.concatenate(
        [mx.stack([src, dst]), mx.stack([dst, src])], 1
    )
    undirected_edge_features = None
    if edge_features is not None:
        undirected_edge_features = mx.concatenate([edge_features, edge_features], 0)

    if coalesce:
        return _coalesce(
            undirected_edge_index, undirected_edge_features, num_nodes, reduce
        )
    if undirected_edge_features is not None:
        return undirected_edge_index, undirected_edge_features
    return undirected_edge_index

from mlx_graphs.utils.transformations import to_undirected · mlx_graphs/utils/transformations.py