Skip to content

add_remaining_self_loops

add_remaining_self_loops(
    edge_index: array,
    edge_features: Optional[array] = None,
    fill_value: FillValue = None,
    num_nodes: Optional[int] = None,
) -> tuple[array, Optional[array]]

Adds a self-loop (i, i) to every node of the graph that does not already have one.

The existing self-loops are kept with their features (if a node has several self-loops, only the last one is kept), while the new ones get features computed from fill_value. All the self-loops are placed after the other edges, in node order.

Parameters:

Name Type Description Default
edge_index array

a [2, num_edges] array representing the source and target nodes of each edge

required
edge_features Optional[array]

Optional features of each edge, of shape [num_edges, ...]. Default: None

None
fill_value FillValue

Features of the added self-loops. Either None (ones), a number, an array broadcastable to the shape of one edge's features, or a reduction ("add"/"sum", "mean", "max", "min") of the features of the incoming edges of each node. Default: None

None
num_nodes Optional[int]

Number of nodes in the graph. Inferred from edge_index if not given.

None

Returns:

Type Description
array

A tuple (edge_index, edge_features) (edge_features is None

Optional[array]

if it was not given).

Example:

edge_index = mx.array([[0, 1, 1], [1, 0, 1]])
edge_features = mx.array([0.5, 0.5, 2.0])

add_remaining_self_loops(edge_index, edge_features, num_nodes=3)
# (mx.array([[0, 1, 0, 1, 2], [1, 0, 0, 1, 2]]),
#  mx.array([0.5, 0.5, 1.0, 2.0, 1.0]))
Source code in mlx_graphs/utils/loop.py
@validate_edge_index_and_features
def add_remaining_self_loops(
    edge_index: mx.array,
    edge_features: Optional[mx.array] = None,
    fill_value: FillValue = None,
    num_nodes: Optional[int] = None,
) -> tuple[mx.array, Optional[mx.array]]:
    """
    Adds a self-loop ``(i, i)`` to every node of the graph that does not
    already have one.

    The existing self-loops are kept with their features (if a node has
    several self-loops, only the last one is kept), while the new ones get
    features computed from ``fill_value``. All the self-loops are placed after
    the other edges, in node order.

    Args:
        edge_index: a [2, num_edges] array representing the source and target
            nodes of each edge
        edge_features: Optional features of each edge, of shape
            ``[num_edges, ...]``. Default: ``None``
        fill_value: Features of the added self-loops. Either ``None`` (ones),
            a number, an array broadcastable to the shape of one edge's
            features, or a reduction (``"add"``/``"sum"``, ``"mean"``,
            ``"max"``, ``"min"``) of the features of the incoming edges of
            each node. Default: ``None``
        num_nodes: Number of nodes in the graph. Inferred from ``edge_index``
            if not given.

    Returns:
        A tuple ``(edge_index, edge_features)`` (``edge_features`` is ``None``
        if it was not given).

    Example:

    .. code-block:: python

        edge_index = mx.array([[0, 1, 1], [1, 0, 1]])
        edge_features = mx.array([0.5, 0.5, 2.0])

        add_remaining_self_loops(edge_index, edge_features, num_nodes=3)
        # (mx.array([[0, 1, 0, 1, 2], [1, 0, 0, 1, 2]]),
        #  mx.array([0.5, 0.5, 1.0, 2.0, 1.0]))
    """
    num_nodes = _check_num_nodes(edge_index, num_nodes)
    edges = _to_numpy(edge_index)
    is_loop = edges[0] == edges[1]
    keep = np.nonzero(~is_loop)[0]

    loop_nodes = mx.arange(num_nodes, dtype=edge_index.dtype)
    loop_index = mx.stack([loop_nodes, loop_nodes])
    out_index = mx.concatenate([_take_edges(edge_index, keep), loop_index], axis=1)

    if edge_features is None:
        return out_index, None

    loop_features = _loop_features(edge_index, edge_features, num_nodes, fill_value)
    loop_positions = np.nonzero(is_loop)[0]
    if loop_positions.size > 0:
        loop_owner = edges[0][loop_positions]
        # Keep the last self-loop of each node, as in PyG.
        _, last = np.unique(loop_owner[::-1], return_index=True)
        last = loop_positions.size - 1 - last
        loop_features = mx.array(loop_features)
        loop_features[mx.array(loop_owner[last])] = edge_features[
            mx.array(loop_positions[last])
        ]
    out_features = mx.concatenate(
        [_take_rows(edge_features, keep), loop_features], axis=0
    )
    return out_index, out_features

from mlx_graphs.utils import add_remaining_self_loops · mlx_graphs/utils/loop.py