Skip to content

index_to_mask

index_to_mask(index: array, size: Optional[int] = None) -> array

Converts indices to a mask representation.

Parameters:

Name Type Description Default
index array

Array of indices where the mask will be True.

required
size Optional[int]

Length of the returned mask array. By default, the size is set to the maximum index in the indices + 1.

None

Returns:

Type Description
array

A boolean array of length size, with True at the given

array

index indices and False elsewhere.

Example:

>>> index = mx.array([1, 2, 3])
>>> index_to_mask(index, size=5)
array([False, True, True, True, False], dtype=bool)
Source code in mlx_graphs/utils/array_ops.py
def index_to_mask(index: mx.array, size: Optional[int] = None) -> mx.array:
    """Converts indices to a mask representation.

    Args:
        index: Array of indices where the mask will be True.
        size: Length of the returned mask array. By default,
            the size is set to the maximum index in the indices + 1.

    Returns:
        A boolean array of length ``size``, with `True` at the given
        ``index`` indices and `False` elsewhere.

    Example:

    .. code-block:: python

        >>> index = mx.array([1, 2, 3])
        >>> index_to_mask(index, size=5)
        array([False, True, True, True, False], dtype=bool)
    """
    index = index.reshape(-1)
    size = index.max().item() + 1 if size is None else size
    mask = mx.zeros(size, dtype=mx.bool_)
    mask[index] = True
    return mask

from mlx_graphs.utils.array_ops import index_to_mask · mlx_graphs/utils/array_ops.py