Skip to content

coalesce

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

Sorts edge_index by source then destination and merges duplicate edges.

The features of merged edges are combined with reduce.

Parameters:

Name Type Description Default
edge_index array

Edges to coalesce.

required
edge_features Optional[array]

Optional features of each edge, of shape [num_edges, ...].

None
num_nodes Optional[int]

Number of nodes. Inferred from edge_index if not given.

None
reduce str

How the features of duplicate edges are combined: "add", "mean", "max" or "min". Default: "add".

'add'

Returns:

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

The sorted edges without duplicates, and their features if

Union[array, tuple[array, array]]

edge_features was given.

Example:

edge_index = mx.array([[1, 0, 1, 0], [0, 1, 0, 2]])
edge_features = mx.array([1.0, 2.0, 3.0, 4.0])

coalesce(edge_index)
# mx.array([[0, 0, 1], [1, 2, 0]])

coalesce(edge_index, edge_features)
# (mx.array([[0, 0, 1], [1, 2, 0]]), mx.array([2.0, 4.0, 4.0]))
Source code in mlx_graphs/utils/transformations.py
@validate_edge_index_and_features
def coalesce(
    edge_index: mx.array,
    edge_features: Optional[mx.array] = None,
    num_nodes: Optional[int] = None,
    reduce: str = "add",
) -> Union[mx.array, tuple[mx.array, mx.array]]:
    """
    Sorts ``edge_index`` by source then destination and merges duplicate edges.

    The features of merged edges are combined with ``reduce``.

    Args:
        edge_index: Edges to coalesce.
        edge_features: Optional features of each edge, of shape
            ``[num_edges, ...]``.
        num_nodes: Number of nodes. Inferred from ``edge_index`` if not given.
        reduce: How the features of duplicate edges are combined: ``"add"``,
            ``"mean"``, ``"max"`` or ``"min"``. Default: ``"add"``.

    Returns:
        The sorted edges without duplicates, and their features if
        ``edge_features`` was given.

    Example:

    .. code-block:: python

        edge_index = mx.array([[1, 0, 1, 0], [0, 1, 0, 2]])
        edge_features = mx.array([1.0, 2.0, 3.0, 4.0])

        coalesce(edge_index)
        # mx.array([[0, 0, 1], [1, 2, 0]])

        coalesce(edge_index, edge_features)
        # (mx.array([[0, 0, 1], [1, 2, 0]]), mx.array([2.0, 4.0, 4.0]))
    """
    return _coalesce(edge_index, edge_features, num_nodes, reduce)

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