Skip to content

scatter

scatter(
    values: array,
    index: array,
    out_size: Optional[int] = None,
    aggr: ScatterAggregations = "add",
    axis: int = 0,
) -> array

Default function for performing all scattering operations. Scatters values at index in an empty array of out_size elements.

Parameters:

Name Type Description Default
values array

array with all the values to scatter in the output tensor

required
index array

array with index to which scatter the values

required
out_size Optional[int]

number of elements in the output array (size of the first dimension). If not provided, uses the number of elements in values

None
aggr ScatterAggregations

scattering method employed for reduction at index:

  • "add", "mean", "max", "min": sum, average, feature-wise maximum and minimum (0 for empty groups)
  • "mul": product (1 for empty groups, as in PyG)
  • "var", "std": population variance and standard deviation (see scatter_var() and scatter_std())
  • "median": lower median (see scatter_quantile())
  • "softmax": softmax normalization of the values within each group. Unlike the other reductions, the output has one row per value, not per group.
'add'
axis int

axis on which applying the scattering

0

Returns:

Type Description
array

Array with out_size elements containing the scattered values at given index

array

following the given aggr reduction method

Example:

src = mx.array([1., 1., 1., 1.])
index = mx.array([0, 0, 1, 2])
num_nodes = src.shape[0]

scatter(src, index, num_nodes, "softmax")
>>>  mx.array([0.5, 0.5, 1, 1])

num_nodes = index.max().item() + 1
scatter(src, index, num_nodes, "add")
>>>  mx.array([2, 1, 1])
Source code in mlx_graphs/utils/scatter.py
def scatter(
    values: mx.array,
    index: mx.array,
    out_size: Optional[int] = None,
    aggr: ScatterAggregations = "add",
    axis: int = 0,
) -> mx.array:
    """Default function for performing all scattering operations.
    Scatters `values` at `index` in an empty array of `out_size` elements.

    Args:
        values: array with all the values to scatter in the output tensor
        index: array with index to which scatter the values
        out_size: number of elements in the output array (size of the first dimension).
            If not provided, uses the number of elements in `values`
        aggr: scattering method employed for reduction at index:

            * ``"add"``, ``"mean"``, ``"max"``, ``"min"``: sum, average,
              feature-wise maximum and minimum (0 for empty groups)
            * ``"mul"``: product (1 for empty groups, as in PyG)
            * ``"var"``, ``"std"``: population variance and standard deviation
              (see :func:`scatter_var` and :func:`scatter_std`)
            * ``"median"``: lower median (see :func:`scatter_quantile`)
            * ``"softmax"``: softmax normalization of the values within each
              group. Unlike the other reductions, the output has one row per
              *value*, not per group.
        axis: axis on which applying the scattering

    Returns:
        Array with `out_size` elements containing the scattered values at given index
        following the given `aggr` reduction method

    Example:

    .. code-block:: python

        src = mx.array([1., 1., 1., 1.])
        index = mx.array([0, 0, 1, 2])
        num_nodes = src.shape[0]

        scatter(src, index, num_nodes, "softmax")
        >>>  mx.array([0.5, 0.5, 1, 1])

        num_nodes = index.max().item() + 1
        scatter(src, index, num_nodes, "add")
        >>>  mx.array([2, 1, 1])
    """
    if aggr not in get_args(ScatterAggregations):
        raise ValueError(
            "Invalid aggregation function.",
            f"Available values are {get_args(ScatterAggregations)}",
        )

    _out_size: int = out_size if out_size is not None else values.shape[0]

    if aggr == "softmax":
        return scatter_softmax(values, index, _out_size)
    if aggr == "mean":
        return scatter_mean(values, index, _out_size)
    if aggr == "std":
        return scatter_std(values, index, _out_size)
    if aggr == "var":
        return scatter_var(values, index, _out_size)
    if aggr == "mul":
        return scatter_mul(values, index, _out_size)
    if aggr == "median":
        return scatter_quantile(values, index, _out_size, q=0.5, interpolation="lower")

    out_shape = list(values.shape)
    out_shape[axis] = _out_size

    empty_tensor = mx.zeros(out_shape, dtype=values.dtype)

    if aggr == "add":
        return scatter_add(empty_tensor, index, values)
    if aggr == "max":
        return scatter_max(empty_tensor, index, values)
    if aggr == "min":
        return scatter_min(empty_tensor, index, values)

    raise NotImplementedError(f"Aggregation {aggr} not implemented yet.")

from mlx_graphs.utils.scatter import scatter · mlx_graphs/utils/scatter.py