Skip to content

get_isolated_nodes_mask

get_isolated_nodes_mask(
    edge_index: array, num_nodes: int, complement: bool = True
) -> array

Returns the indices of non-isolated nodes, or of isolated nodes.

A node is isolated if it has no edge to another node (self-loops are ignored).

Parameters:

Name Type Description Default
edge_index array

Edge index from which to remove isolated nodes.

required
num_nodes int

Number of nodes of the graph.

required
complement bool

If True (default), returns the indices of the nodes that are not isolated. If False, returns the indices of the isolated nodes.

True

Returns:

Type Description
array

A sorted array of node indices.

Example:

edge_index = mx.array([[0, 2, 0], [2, 0, 0]])
mask = get_isolated_nodes_mask(edge_index, 3)
>>> mx.array([0,2])
mask = get_isolated_nodes_mask(edge_index, 3, complement=False)
>>> mx.array([1])
Source code in mlx_graphs/utils/transformations.py
@validate_edge_index
def get_isolated_nodes_mask(
    edge_index: mx.array, num_nodes: int, complement: bool = True
) -> mx.array:
    """Returns the indices of non-isolated nodes, or of isolated nodes.

    A node is isolated if it has no edge to another node (self-loops are
    ignored).

    Args:
        edge_index: Edge index from which to remove isolated nodes.
        num_nodes: Number of nodes of the graph.
        complement: If ``True`` (default), returns the indices of the nodes
            that are not isolated. If ``False``, returns the indices of the
            isolated nodes.

    Returns:
        A sorted array of node indices.

    Example:

    .. code-block:: python

        edge_index = mx.array([[0, 2, 0], [2, 0, 0]])
        mask = get_isolated_nodes_mask(edge_index, 3)
        >>> mx.array([0,2])
        mask = get_isolated_nodes_mask(edge_index, 3, complement=False)
        >>> mx.array([1])
    """
    connected = _connected_nodes(edge_index, num_nodes)
    return mx.array(np.nonzero(connected if complement else ~connected)[0])

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