Skip to content

add_self_loops

add_self_loops(
    edge_index: array,
    edge_features: array,
    num_nodes: Optional[int] = None,
    fill_value: Optional[Union[float, array]] = 1,
    allow_repeated: Optional[bool] = True,
) -> tuple[array, array]
add_self_loops(
    edge_index: array,
    edge_features=None,
    num_nodes: Optional[int] = None,
    fill_value: Optional[Union[float, array]] = 1,
    allow_repeated: Optional[bool] = True,
) -> array
add_self_loops(
    edge_index: array,
    edge_features: Optional[array] = None,
    num_nodes: Optional[int] = None,
    fill_value: Optional[Union[float, array]] = 1,
    allow_repeated: Optional[bool] = True,
) -> Union[array, tuple[array, array]]

Adds self-loops to the given graph represented by edge_index and edge_features.

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 tensor representing features associated with each edge, with shape [num_edges, num_edge_features]

None
num_nodes Optional[int]

Optional number of nodes in the graph. If not provided, it is inferred from edge_index.

None
fill_value Optional[Union[float, array]]

Value used for filling the self-loop features. Default is 1.

1
allow_repeated Optional[bool]

Specify whether to add self-loops for all nodes, even if they are already in the edge_index. Defaults to True.

True

Returns:

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

A tuple containing the updated edge_index and edge_features with self-loops.

Source code in mlx_graphs/utils/transformations.py
@validate_edge_index_and_features
def add_self_loops(
    edge_index: mx.array,
    edge_features: Optional[mx.array] = None,
    num_nodes: Optional[int] = None,
    fill_value: Optional[Union[float, mx.array]] = 1,
    allow_repeated: Optional[bool] = True,
) -> Union[mx.array, tuple[mx.array, mx.array]]:
    """
    Adds self-loops to the given graph represented by edge_index and edge_features.

    Args:
        edge_index: a [2, num_edges] array representing the source and target nodes
            of each edge
        edge_features: Optional tensor representing features associated with each edge,
            with shape [num_edges, num_edge_features]
        num_nodes: Optional number of nodes in the graph. If not provided, it is
            inferred from edge_index.
        fill_value: Value used for filling the self-loop features. Default is 1.
        allow_repeated: Specify whether to add self-loops for all nodes, even if they
            are already in the edge_index. Defaults to True.

    Returns:
        A tuple containing the updated edge_index and edge_features with self-loops.

    """
    if num_nodes is not None:
        if edge_index.size > 0 and mx.max(edge_index).item() > num_nodes - 1:
            raise ValueError(
                "num_nodes must be >= than the number of nodes in the edge_index ",
                f"(got num_nodes={num_nodes} and {mx.max(edge_index).item() + 1}",
                "nodes in index",
            )
    else:
        num_nodes = _infer_num_nodes(edge_index)

    # add self loops to index
    loop_nodes = mx.arange(num_nodes, dtype=edge_index.dtype)
    if not allow_repeated:
        edges = _to_numpy(edge_index)
        has_loop = np.zeros(num_nodes, dtype=bool)
        has_loop[edges[0][edges[0] == edges[1]]] = True
        loop_nodes = mx.array(np.nonzero(~has_loop)[0], dtype=edge_index.dtype)
    self_loop_index = mx.stack([loop_nodes, loop_nodes])
    full_edge_index = mx.concatenate([edge_index, self_loop_index], 1)

    if edge_features is not None:
        # add self loops to features
        self_loop_features = mx.full(
            (self_loop_index.shape[1], *edge_features.shape[1:]),
            fill_value,
            dtype=edge_features.dtype,
        ).astype(edge_features.dtype)
        full_edge_features = mx.concatenate([edge_features, self_loop_features], 0)
        return full_edge_index, full_edge_features
    return full_edge_index

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