Skip to content

pipekit-array

Array-API operators on top of pipekit-ops. One operator runs on NumPy, JAX, CuPy, PyTorch or dask because it dispatches on array_namespace(x). The design pages have the architecture, the ADRs and the operator catalogue the package was planned from.

What ships today: namespace dispatch, MeanScalar, StackAlong and ConcatenateAlong, the named carrier DimArray, all the dim-level functions with their operator twins (ApplyAlong included), padded batches and BatchAlong, sample tables and Estimator, the einx bridge, KernelOp, the xarray adapter and the test kit. What is still to build from the v0.1 catalogue: the numeric QC family (AssertClose, AssertValueRange, AssertNoNaN, AssertValidFraction), the array-aware Histogram and the trained-model wrapper ModelOp.

The package is six areas, each with one job. Import a name from its area, never from a file in it. An area imports only from the ones before it, and testing from all of them (ADR A30).

Plain arrays

pipekit_array.arrays is for functions of plain arrays, such as the formulas of geophysix and radiomax ([numpy,compat]). Python numbers, sequences and NumPy arrays join the library and the device of the other arrays (ADR A23). What the array API standard leaves out (indexed updates, loops, padding) is JAX's own call on JAX data, and a plain equivalent on every other library (ADR A27).

pipekit_array.arrays

Functions of plain arrays: any array library, no dimension names.

A function that is no Operator (a formula of a physics library) is written once, in functions of the array API standard, and runs on NumPy, JAX, torch, CuPy, dask and any other library that array_api_compat knows:

@elementwise()                                       # labelled arrays
def density(pressure, temperature):
    xp, (p, t) = floating_inputs(pressure, temperature)  # one dtype, device
    return p / (like(287.0, t, xp) * t)
  • The call: namespace_of, floating_inputs, as_inexact, concrete, is_dataarray.
  • Values of the host: like, constants_like, table_like, boolean_like.
  • Numerics: divide_or_nan, safe_sqrt, safe_power, safe_atan2, squared_magnitude, stand_in.
  • Copies with changes: set_at, add_at, multiply_at, pad.
  • Transformations: vmap, scan, stop_gradient, optimization_barrier.
  • Checks: check_values, require_floating.
  • Masked, weighted reductions: weighted_sum, weighted_mean, Groups, reduction_inputs, used_weights.
  • Records: register_record.

pipekit_array.kernels.elementwise lets such a function take labelled arrays. This area needs pipekit-array[numpy,compat], so importing pipekit_array does not import it. It imports no other area, and no array library but NumPy.

Groups dataclass

Which samples belong to which group; built once, on the host.

The tables are NumPy arrays and static: under JAX a Groups is a closure or a static argument, and never a traced one. It is compared by identity, so build it once; a new one compiles again.

G groups of at most M samples cost G M reads, so one very large group among many small ones costs memory.

Attributes:

Name Type Description
index ndarray

Positions of each group's samples, padded with 0, (G, M).

member ndarray

Which entries of index are real, (G, M).

labels ndarray

The group of each of the N samples, -1 for none.

Example

import numpy as np groups = Groups.from_labels([0, 1, 0, -1], n_groups=3) x = np.asarray([1.0, 2.0, 3.0, 100.0]) groups.sum(x) array([4., 2., 0.]) groups.mean(x) array([ 2., 2., nan]) groups.count(x) array([2, 1, 0], dtype=int32) x - groups.spread(groups.mean(x)) array([-1., 0., 1., nan])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_reductions.py
@dataclasses.dataclass(frozen=True, eq=False)
class Groups:
    """Which samples belong to which group; built once, on the host.

    The tables are NumPy arrays and static: under JAX a `Groups` is a closure or
    a static argument, and never a traced one. It is compared by identity, so
    build it once; a new one compiles again.

    G groups of at most M samples cost G M reads, so one very large group
    among many small ones costs memory.

    Attributes:
        index: Positions of each group's samples, padded with 0, (G, M).
        member: Which entries of ``index`` are real, (G, M).
        labels: The group of each of the N samples, -1 for none.

    Example:
        >>> import numpy as np
        >>> groups = Groups.from_labels([0, 1, 0, -1], n_groups=3)
        >>> x = np.asarray([1.0, 2.0, 3.0, 100.0])
        >>> groups.sum(x)
        array([4., 2., 0.])
        >>> groups.mean(x)
        array([ 2.,  2., nan])
        >>> groups.count(x)
        array([2, 1, 0], dtype=int32)
        >>> x - groups.spread(groups.mean(x))
        array([-1.,  0.,  1., nan])
    """

    index: np.ndarray
    member: np.ndarray
    labels: np.ndarray

    @classmethod
    def from_labels(cls, labels: Any, n_groups: int) -> Groups:
        """The groups of samples with the same label.

        Args:
            labels: The group of each sample, concrete integers in [0, G), or
                -1 for a sample in no group.
            n_groups: G, at least 1; a group may be empty.

        Returns:
            The groups.

        Raises:
            ValueError: If ``labels`` is not a 1-D array of integers, if
                one is neither -1 nor in [0, G), or if ``n_groups`` is below 1.
        """
        labels = np.asarray(labels)
        if n_groups < 1:
            msg = f"'n_groups' must be at least 1, got {n_groups}"
            raise ValueError(msg)
        if labels.ndim != 1 or not np.issubdtype(labels.dtype, np.integer):
            msg = (
                "'labels' must be a 1-D array of integers, got shape "
                f"{labels.shape} and dtype {labels.dtype}"
            )
            raise ValueError(msg)
        # a copy: the caller may write to its array later
        labels = labels.astype(np.int64)
        outside = (labels < NO_GROUP) | (labels >= n_groups)
        if outside.any():
            # segment_sum would drop it silently
            msg = (
                f"'labels' must be -1 or in [0, {n_groups}), got "
                f"{labels[outside][0]} at position {int(np.argmax(outside))}"
            )
            raise ValueError(msg)

        # --- the samples in a group, in group order ---
        position = np.flatnonzero(labels != NO_GROUP)
        order = position[np.argsort(labels[position], kind="stable")]
        sizes = np.bincount(labels[position], minlength=n_groups)

        # --- one padded row per group ---
        member = np.arange(int(sizes.max())) < sizes[:, None]
        index = np.zeros(member.shape, dtype=np.int64)
        # row by row, as `order` is
        index[member] = order
        for table in (index, member, labels):
            table.setflags(write=False)
        return cls(index=index, member=member, labels=labels)

    @property
    def n_groups(self) -> int:
        """G, the number of groups, empty ones included."""
        return self.index.shape[0]

    @property
    def n_samples(self) -> int:
        """N, the number of samples, those in no group included."""
        return self.labels.shape[0]

    def sum(self, values: Any, *, axis: int = -1, valid: Any = None) -> Any:
        """The sum of each group's valid samples.

        Sⱼ = ∑_{n : g(n) = j} mₙ xₙ, with g(n) the group of sample n and m the
        validity, 1 or 0.

        Args:
            values: x, with the N samples on ``axis``.
            axis: The axis of the samples.
            valid: Which samples to use (True = use), broadcast against
                ``values``. A NaN in a valid sample gives NaN.

        Returns:
            S, with the G groups on ``axis``. An empty group has sum 0.

        Raises:
            BackendError: If an argument is a labelled array, or of another library.
            ValueError: If ``axis`` does not hold N samples, or ``valid``
                is not boolean.
        """
        xp, x, _, m = self._inputs(values, None, valid, axis)
        x_rows, used = self._rows(xp, x, m)
        kept = xp.where(used, x_rows, xp.zeros_like(x_rows))
        # Sⱼ = ∑ mₙ xₙ over the row of group j
        return xp.moveaxis(xp.sum(kept, axis=-1, dtype=x.dtype), -1, axis)

    def count(self, values: Any, *, axis: int = -1, valid: Any = None) -> Any:
        """The number of valid samples in each group.

        Args:
            values: x, with the N samples on ``axis``; only its shape, its
                library and its device matter, so a NaN counts.
            axis: The axis of the samples.
            valid: Which samples to count (True = count), broadcast against
                ``values``.

        Returns:
            The counts, int32, with the G groups on ``axis``.

        Raises:
            BackendError: If an argument is a labelled array, or of another library.
            ValueError: If ``axis`` does not hold N samples, or ``valid``
                is not boolean.
        """
        xp, x, _, m = self._inputs(values, None, valid, axis)
        x_rows, used = self._rows(xp, x, m)
        ones = xp.astype(xp.broadcast_to(used, x_rows.shape), xp.int32)
        return xp.moveaxis(xp.sum(ones, axis=-1, dtype=xp.int32), -1, axis)

    def mean(
        self,
        values: Any,
        *,
        axis: int = -1,
        valid: Any = None,
        weights: Any = None,
    ) -> Any:
        """The mean of each group's valid samples, weighted by w.

        x̄ⱼ = Sⱼ / ∑_{n : g(n) = j} mₙ wₙ, with Sⱼ = ∑_{n : g(n) = j} mₙ wₙ xₙ.

        Args:
            values: x, with the N samples on ``axis``.
            axis: The axis of the samples.
            valid: Which samples to use (True = use), broadcast against
                ``values``. A NaN in a valid sample gives NaN.
            weights: w ≥ 0, broadcast against ``values``; they need not sum to 1.

        Returns:
            x̄, with the G groups on ``axis``, NaN where a group is empty or its
            weights sum to 0.

        Raises:
            BackendError: If an argument is a labelled array, or of another library.
            ValueError: If ``axis`` does not hold N samples, or ``valid``
                is not boolean.
        """
        xp, x, w, m = self._inputs(values, weights, valid, axis)
        x_rows, used = self._rows(xp, x, m)
        used = xp.broadcast_to(used, x_rows.shape)
        w_rows = None if w is None else self._gather(xp, w)
        # mₙ wₙ, 0 on the padding too
        weight_rows = used_weights(xp, x_rows, w_rows, used)
        kept = xp.where(used, x_rows, xp.zeros_like(x_rows))
        # Sⱼ = ∑ mₙ wₙ xₙ over the row of group j
        total = xp.sum(kept * weight_rows, axis=-1, dtype=x.dtype)
        weight = xp.sum(weight_rows, axis=-1, dtype=x.dtype)
        return xp.moveaxis(divide_or_nan(total, weight, xp), -1, axis)

    def spread(self, grouped: Any, *, axis: int = -1) -> Any:
        """Each sample's group value: the inverse lookup.

        yₙ = x_{g(n)}, so that ``x - groups.spread(groups.mean(x))`` is the
        anomaly from the group's mean.

        Args:
            grouped: One value per group, on ``axis``.
            axis: The axis of the groups.

        Returns:
            y, with the N samples on ``axis``, NaN for a sample in no group.

        Raises:
            BackendError: If ``grouped`` is a labelled array.
            ValueError: If ``axis`` does not hold G groups.
        """
        xp, (x,) = floating_inputs(grouped)
        self._check_axis(x, axis, self.n_groups, "groups")
        grouped_last = xp.moveaxis(x, axis, -1)
        labelled = self.labels != NO_GROUP
        # yₙ = x_{g(n)}; a sample in no group reads group 0, and is NaN below
        lookup = table_like(np.where(labelled, self.labels, 0), x, xp)
        values = xp.take(grouped_last, lookup, axis=-1)
        in_a_group = table_like(labelled, x, xp)
        spread = xp.where(in_a_group, values, xp.full_like(values, xp.nan))
        return xp.moveaxis(spread, -1, axis)

    # --- the gather -----------------------------------------------------------

    def _inputs(
        self,
        values: Any,
        weights: Any,
        valid: Any,
        axis: int,
    ) -> tuple[ModuleType, Any, Any, Any]:
        """The namespace, and x, w and m with the samples on the last axis."""
        xp, *arrays = reduction_inputs(values, weights, valid)
        self._check_axis(arrays[0], axis, self.n_samples, "samples")
        x, w, m = (
            None if array is None else xp.moveaxis(array, axis, -1) for array in arrays
        )
        return xp, x, w, m

    @staticmethod
    def _check_axis(x: Any, axis: int, size: int, what: str) -> None:
        """Refuse an axis of another size: a gather would not notice it."""
        if not -x.ndim <= axis < x.ndim or x.shape[axis] != size:
            msg = f"axis {axis} of shape {x.shape} must hold the {size} {what}"
            raise ValueError(msg)

    def _gather(self, xp: ModuleType, x: Any) -> Any:
        """(…, N) samples as (…, G, M) rows, padding included."""
        flat = table_like(self.index.reshape(-1), x, xp)
        rows = xp.take(x, flat, axis=-1)
        return xp.reshape(rows, (*x.shape[:-1], *self.index.shape))

    def _rows(self, xp: ModuleType, x: Any, m: Any) -> tuple[Any, Any]:
        """The rows of x, and which of their entries are real and valid."""
        x_rows = self._gather(xp, x)
        used = table_like(self.member, x, xp)
        if m is not None:
            used = xp.logical_and(used, self._gather(xp, m))
        return x_rows, used

n_groups property

G, the number of groups, empty ones included.

n_samples property

N, the number of samples, those in no group included.

count(values, *, axis=-1, valid=None)

The number of valid samples in each group.

Parameters:

Name Type Description Default
values Any

x, with the N samples on axis; only its shape, its library and its device matter, so a NaN counts.

required
axis int

The axis of the samples.

-1
valid Any

Which samples to count (True = count), broadcast against values.

None

Returns:

Type Description
Any

The counts, int32, with the G groups on axis.

Raises:

Type Description
BackendError

If an argument is a labelled array, or of another library.

ValueError

If axis does not hold N samples, or valid is not boolean.

Source code in packages/pipekit-array/src/pipekit_array/arrays/_reductions.py
def count(self, values: Any, *, axis: int = -1, valid: Any = None) -> Any:
    """The number of valid samples in each group.

    Args:
        values: x, with the N samples on ``axis``; only its shape, its
            library and its device matter, so a NaN counts.
        axis: The axis of the samples.
        valid: Which samples to count (True = count), broadcast against
            ``values``.

    Returns:
        The counts, int32, with the G groups on ``axis``.

    Raises:
        BackendError: If an argument is a labelled array, or of another library.
        ValueError: If ``axis`` does not hold N samples, or ``valid``
            is not boolean.
    """
    xp, x, _, m = self._inputs(values, None, valid, axis)
    x_rows, used = self._rows(xp, x, m)
    ones = xp.astype(xp.broadcast_to(used, x_rows.shape), xp.int32)
    return xp.moveaxis(xp.sum(ones, axis=-1, dtype=xp.int32), -1, axis)

from_labels(labels, n_groups) classmethod

The groups of samples with the same label.

Parameters:

Name Type Description Default
labels Any

The group of each sample, concrete integers in [0, G), or -1 for a sample in no group.

required
n_groups int

G, at least 1; a group may be empty.

required

Returns:

Type Description
Groups

The groups.

Raises:

Type Description
ValueError

If labels is not a 1-D array of integers, if one is neither -1 nor in [0, G), or if n_groups is below 1.

Source code in packages/pipekit-array/src/pipekit_array/arrays/_reductions.py
@classmethod
def from_labels(cls, labels: Any, n_groups: int) -> Groups:
    """The groups of samples with the same label.

    Args:
        labels: The group of each sample, concrete integers in [0, G), or
            -1 for a sample in no group.
        n_groups: G, at least 1; a group may be empty.

    Returns:
        The groups.

    Raises:
        ValueError: If ``labels`` is not a 1-D array of integers, if
            one is neither -1 nor in [0, G), or if ``n_groups`` is below 1.
    """
    labels = np.asarray(labels)
    if n_groups < 1:
        msg = f"'n_groups' must be at least 1, got {n_groups}"
        raise ValueError(msg)
    if labels.ndim != 1 or not np.issubdtype(labels.dtype, np.integer):
        msg = (
            "'labels' must be a 1-D array of integers, got shape "
            f"{labels.shape} and dtype {labels.dtype}"
        )
        raise ValueError(msg)
    # a copy: the caller may write to its array later
    labels = labels.astype(np.int64)
    outside = (labels < NO_GROUP) | (labels >= n_groups)
    if outside.any():
        # segment_sum would drop it silently
        msg = (
            f"'labels' must be -1 or in [0, {n_groups}), got "
            f"{labels[outside][0]} at position {int(np.argmax(outside))}"
        )
        raise ValueError(msg)

    # --- the samples in a group, in group order ---
    position = np.flatnonzero(labels != NO_GROUP)
    order = position[np.argsort(labels[position], kind="stable")]
    sizes = np.bincount(labels[position], minlength=n_groups)

    # --- one padded row per group ---
    member = np.arange(int(sizes.max())) < sizes[:, None]
    index = np.zeros(member.shape, dtype=np.int64)
    # row by row, as `order` is
    index[member] = order
    for table in (index, member, labels):
        table.setflags(write=False)
    return cls(index=index, member=member, labels=labels)

mean(values, *, axis=-1, valid=None, weights=None)

The mean of each group's valid samples, weighted by w.

x̄ⱼ = Sⱼ / ∑{n : g(n) = j} mₙ wₙ, with Sⱼ = ∑ mₙ wₙ xₙ.

Parameters:

Name Type Description Default
values Any

x, with the N samples on axis.

required
axis int

The axis of the samples.

-1
valid Any

Which samples to use (True = use), broadcast against values. A NaN in a valid sample gives NaN.

None
weights Any

w ≥ 0, broadcast against values; they need not sum to 1.

None

Returns:

Type Description
Any

x̄, with the G groups on axis, NaN where a group is empty or its

Any

weights sum to 0.

Raises:

Type Description
BackendError

If an argument is a labelled array, or of another library.

ValueError

If axis does not hold N samples, or valid is not boolean.

Source code in packages/pipekit-array/src/pipekit_array/arrays/_reductions.py
def mean(
    self,
    values: Any,
    *,
    axis: int = -1,
    valid: Any = None,
    weights: Any = None,
) -> Any:
    """The mean of each group's valid samples, weighted by w.

    x̄ⱼ = Sⱼ / ∑_{n : g(n) = j} mₙ wₙ, with Sⱼ = ∑_{n : g(n) = j} mₙ wₙ xₙ.

    Args:
        values: x, with the N samples on ``axis``.
        axis: The axis of the samples.
        valid: Which samples to use (True = use), broadcast against
            ``values``. A NaN in a valid sample gives NaN.
        weights: w ≥ 0, broadcast against ``values``; they need not sum to 1.

    Returns:
        x̄, with the G groups on ``axis``, NaN where a group is empty or its
        weights sum to 0.

    Raises:
        BackendError: If an argument is a labelled array, or of another library.
        ValueError: If ``axis`` does not hold N samples, or ``valid``
            is not boolean.
    """
    xp, x, w, m = self._inputs(values, weights, valid, axis)
    x_rows, used = self._rows(xp, x, m)
    used = xp.broadcast_to(used, x_rows.shape)
    w_rows = None if w is None else self._gather(xp, w)
    # mₙ wₙ, 0 on the padding too
    weight_rows = used_weights(xp, x_rows, w_rows, used)
    kept = xp.where(used, x_rows, xp.zeros_like(x_rows))
    # Sⱼ = ∑ mₙ wₙ xₙ over the row of group j
    total = xp.sum(kept * weight_rows, axis=-1, dtype=x.dtype)
    weight = xp.sum(weight_rows, axis=-1, dtype=x.dtype)
    return xp.moveaxis(divide_or_nan(total, weight, xp), -1, axis)

spread(grouped, *, axis=-1)

Each sample's group value: the inverse lookup.

yₙ = x_{g(n)}, so that x - groups.spread(groups.mean(x)) is the anomaly from the group's mean.

Parameters:

Name Type Description Default
grouped Any

One value per group, on axis.

required
axis int

The axis of the groups.

-1

Returns:

Type Description
Any

y, with the N samples on axis, NaN for a sample in no group.

Raises:

Type Description
BackendError

If grouped is a labelled array.

ValueError

If axis does not hold G groups.

Source code in packages/pipekit-array/src/pipekit_array/arrays/_reductions.py
def spread(self, grouped: Any, *, axis: int = -1) -> Any:
    """Each sample's group value: the inverse lookup.

    yₙ = x_{g(n)}, so that ``x - groups.spread(groups.mean(x))`` is the
    anomaly from the group's mean.

    Args:
        grouped: One value per group, on ``axis``.
        axis: The axis of the groups.

    Returns:
        y, with the N samples on ``axis``, NaN for a sample in no group.

    Raises:
        BackendError: If ``grouped`` is a labelled array.
        ValueError: If ``axis`` does not hold G groups.
    """
    xp, (x,) = floating_inputs(grouped)
    self._check_axis(x, axis, self.n_groups, "groups")
    grouped_last = xp.moveaxis(x, axis, -1)
    labelled = self.labels != NO_GROUP
    # yₙ = x_{g(n)}; a sample in no group reads group 0, and is NaN below
    lookup = table_like(np.where(labelled, self.labels, 0), x, xp)
    values = xp.take(grouped_last, lookup, axis=-1)
    in_a_group = table_like(labelled, x, xp)
    spread = xp.where(in_a_group, values, xp.full_like(values, xp.nan))
    return xp.moveaxis(spread, -1, axis)

sum(values, *, axis=-1, valid=None)

The sum of each group's valid samples.

Sⱼ = ∑_{n : g(n) = j} mₙ xₙ, with g(n) the group of sample n and m the validity, 1 or 0.

Parameters:

Name Type Description Default
values Any

x, with the N samples on axis.

required
axis int

The axis of the samples.

-1
valid Any

Which samples to use (True = use), broadcast against values. A NaN in a valid sample gives NaN.

None

Returns:

Type Description
Any

S, with the G groups on axis. An empty group has sum 0.

Raises:

Type Description
BackendError

If an argument is a labelled array, or of another library.

ValueError

If axis does not hold N samples, or valid is not boolean.

Source code in packages/pipekit-array/src/pipekit_array/arrays/_reductions.py
def sum(self, values: Any, *, axis: int = -1, valid: Any = None) -> Any:
    """The sum of each group's valid samples.

    Sⱼ = ∑_{n : g(n) = j} mₙ xₙ, with g(n) the group of sample n and m the
    validity, 1 or 0.

    Args:
        values: x, with the N samples on ``axis``.
        axis: The axis of the samples.
        valid: Which samples to use (True = use), broadcast against
            ``values``. A NaN in a valid sample gives NaN.

    Returns:
        S, with the G groups on ``axis``. An empty group has sum 0.

    Raises:
        BackendError: If an argument is a labelled array, or of another library.
        ValueError: If ``axis`` does not hold N samples, or ``valid``
            is not boolean.
    """
    xp, x, _, m = self._inputs(values, None, valid, axis)
    x_rows, used = self._rows(xp, x, m)
    kept = xp.where(used, x_rows, xp.zeros_like(x_rows))
    # Sⱼ = ∑ mₙ xₙ over the row of group j
    return xp.moveaxis(xp.sum(kept, axis=-1, dtype=x.dtype), -1, axis)

add_at(x, index, values)

A copy of an array with some entries added to: y = x, then y[index] += v.

An entry that the index names twice is added to twice, which y[index] += v does not do on NumPy. On JAX it is x.at[index].add(values).

Parameters:

Name Type Description Default
x Any

The array, which is not changed.

required
index Any

As in set_at; an integer array needs NumPy or JAX.

required
values Any

What to add, which broadcasts to x[index].

required

Returns:

Type Description
Any

y, in x's library, shape and dtype.

Raises:

Type Description
BackendError

As set_at.

IndexError

As set_at.

Example

import numpy as np add_at(np.zeros(4), np.asarray([0, 2, 2]), [1.0, 10.0, 100.0]) array([ 1., 0., 110., 0.])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_indexing.py
def add_at(x: Any, index: Any, values: Any) -> Any:
    """A copy of an array with some entries added to: y = x, then y[index] += v.

    An entry that the index names twice is added to twice, which ``y[index] += v``
    does not do on NumPy. On JAX it is ``x.at[index].add(values)``.

    Args:
        x: The array, which is not changed.
        index: As in `set_at`; an integer array needs NumPy or JAX.
        values: What to add, which broadcasts to ``x[index]``.

    Returns:
        y, in ``x``'s library, shape and dtype.

    Raises:
        BackendError: As `set_at`.
        IndexError: As `set_at`.

    Example:
        >>> import numpy as np
        >>> add_at(np.zeros(4), np.asarray([0, 2, 2]), [1.0, 10.0, 100.0])
        array([  1.,   0., 110.,   0.])
    """
    xp, x, index = _update_inputs(x, index, values)
    if array_api_compat.is_jax_namespace(xp):
        return x.at[index].add(values)
    if _has_integer_array(xp, index):
        return _accumulate("add_at", np.add, xp, x, index, values)
    # the index names an entry once
    return _assign("add_at", xp, x, index, x[index] + like(values, x, xp))

as_inexact(x, xp=None)

An array as it is if floating or complex, else in the default float.

Unlike floating_inputs, it takes one array, and complex numbers. The default float is the namespace's: float64 on NumPy, and on JAX what jax_enable_x64 says.

Parameters:

Name Type Description Default
x Any

An array, a sequence or a number.

required
xp ModuleType | None

The namespace of the call; that of x if not given.

None

Returns:

Type Description
Any

An array of xp, on x's device.

Raises:

Type Description
BackendError

If x is not an array of a known library.

ValueError

If x is not numeric, such as a date.

Example

import numpy as np as_inexact(np.arange(3)).dtype, as_inexact([1j]).dtype (dtype('float64'), dtype('complex128'))

Source code in packages/pipekit-array/src/pipekit_array/arrays/_backend.py
def as_inexact(x: Any, xp: ModuleType | None = None) -> Any:
    """An array as it is if floating or complex, else in the default float.

    Unlike `floating_inputs`, it takes one array, and complex numbers. The
    default float is the namespace's: float64 on NumPy, and on JAX what
    ``jax_enable_x64`` says.

    Args:
        x: An array, a sequence or a number.
        xp: The namespace of the call; that of ``x`` if not given.

    Returns:
        An array of ``xp``, on ``x``'s device.

    Raises:
        BackendError: If ``x`` is not an array of a known library.
        ValueError: If ``x`` is not numeric, such as a date.

    Example:
        >>> import numpy as np
        >>> as_inexact(np.arange(3)).dtype, as_inexact([1j]).dtype
        (dtype('float64'), dtype('complex128'))
    """
    xp = namespace_of(x) if xp is None else xp
    array = xp.asarray(x) if is_host(x) else x
    if xp.isdtype(array.dtype, ("real floating", "complex floating")):
        return array
    if not xp.isdtype(array.dtype, ("bool", "integral")):
        msg = f"the input must be numeric, got dtype {array.dtype}"
        raise ValueError(msg)
    return xp.astype(array, xp.asarray(0.0).dtype)

boolean_like(value, ref, xp, *, name='valid')

A boolean argument (a mask) in ref's library, on its device.

Parameters:

Name Type Description Default
value Any

A boolean, a sequence of them, or a boolean array. A value on the host joins ref; any other array must be of its library already.

required
ref Any

An array of the namespace xp.

required
xp ModuleType

The namespace of the call.

required
name str

What the argument is called, for the error.

'valid'

Returns:

Type Description
Any

The mask as an array of xp, in its own shape.

Raises:

Type Description
ValueError

If value is not boolean.

Example

import numpy as np boolean_like([True, False], np.zeros(2), np).tolist() [True, False] boolean_like([1, 0], np.zeros(2), np) Traceback (most recent call last): ... ValueError: 'valid' must be boolean (True = use), got dtype int64

Source code in packages/pipekit-array/src/pipekit_array/arrays/_host.py
def boolean_like(value: Any, ref: Any, xp: ModuleType, *, name: str = "valid") -> Any:
    """A boolean argument (a mask) in ``ref``'s library, on its device.

    Args:
        value: A boolean, a sequence of them, or a boolean array. A value on the
            host joins ``ref``; any other array must be of its library already.
        ref: An array of the namespace ``xp``.
        xp: The namespace of the call.
        name: What the argument is called, for the error.

    Returns:
        The mask as an array of ``xp``, in its own shape.

    Raises:
        ValueError: If ``value`` is not boolean.

    Example:
        >>> import numpy as np
        >>> boolean_like([True, False], np.zeros(2), np).tolist()
        [True, False]
        >>> boolean_like([1, 0], np.zeros(2), np)
        Traceback (most recent call last):
            ...
        ValueError: 'valid' must be boolean (True = use), got dtype int64
    """
    on_host = is_host(value)
    if on_host:
        value = np.asarray(value)
    # a NumPy dtype is one of every namespace that NumPy arrays join
    namespace = numpy_namespace if on_host else xp
    if not namespace.isdtype(value.dtype, "bool"):
        msg = f"'{name}' must be boolean (True = use), got dtype {value.dtype}"
        raise ValueError(msg)
    return table_like(value, ref, xp) if on_host else value

check_values(array, bad, message, *, error=ValueError)

An array, or an error if its values are wrong.

The check belongs to the array that is returned: under a trace, use that one, because JAX drops a check whose result nothing uses.

Parameters:

Name Type Description Default
array Any

The array that the check is about.

required
bad Any

Whether the check fails, a boolean scalar.

required
message str

What must hold, naming the argument ("'x' must be > 0").

required
error type[Exception]

The exception to raise on concrete arrays, such as a library's own. Under a trace it is equinox's, with the same message.

ValueError

Returns:

Type Description
Any

array.

Raises:

Type Description
ValueError

error, if bad holds on concrete arrays; its message ends with the array.

ImportError

Under a trace, if equinox is not installed.

Example

import numpy as np x = np.asarray([1.0, 2.0]) check_values(x, np.any(x <= 0), "'x' must be positive") array([1., 2.]) check_values(x, np.any(x > 1), "'x' must be ≤ 1") Traceback (most recent call last): ... ValueError: 'x' must be ≤ 1, got array([1., 2.])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_checks.py
def check_values(
    array: Any, bad: Any, message: str, *, error: type[Exception] = ValueError
) -> Any:
    """An array, or an error if its values are wrong.

    The check belongs to the array that is returned: under a trace, use that one,
    because JAX drops a check whose result nothing uses.

    Args:
        array: The array that the check is about.
        bad: Whether the check fails, a boolean scalar.
        message: What must hold, naming the argument (``"'x' must be > 0"``).
        error: The exception to raise on concrete arrays, such as a library's
            own. Under a trace it is equinox's, with the same message.

    Returns:
        ``array``.

    Raises:
        ValueError: ``error``, if ``bad`` holds on concrete arrays; its message
            ends with the array.
        ImportError: Under a trace, if equinox is not installed.

    Example:
        >>> import numpy as np
        >>> x = np.asarray([1.0, 2.0])
        >>> check_values(x, np.any(x <= 0), "'x' must be positive")
        array([1., 2.])
        >>> check_values(x, np.any(x > 1), "'x' must be ≤ 1")
        Traceback (most recent call last):
            ...
        ValueError: 'x' must be ≤ 1, got array([1., 2.])
    """
    if not is_concrete(array, bad):
        equinox = import_optional(
            "equinox", install="equinox", needed_by="check_values under a JAX trace"
        )
        return equinox.error_if(array, bad, message)
    if bool(bad):
        raise error(f"{message}, got {_shown(array)!r}")
    return array

concrete(x)

The values of an array on the host, or None if it has none yet.

For a check or a table that needs values and is skipped under jax.jit. No gradient goes through the copy, and a dask array is computed.

Parameters:

Name Type Description Default
x Any

An array, a sequence or a number.

required

Returns:

Type Description
ndarray | None

The values as a NumPy array, or None for a JAX tracer.

Example

concrete([1.0, 2.0]) array([1., 2.])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_backend.py
def concrete(x: Any) -> np.ndarray | None:
    """The values of an array on the host, or ``None`` if it has none yet.

    For a check or a table that needs values and is skipped under ``jax.jit``.
    No gradient goes through the copy, and a dask array is computed.

    Args:
        x: An array, a sequence or a number.

    Returns:
        The values as a NumPy array, or ``None`` for a JAX tracer.

    Example:
        >>> concrete([1.0, 2.0])
        array([1., 2.])
    """
    if is_traced(x):
        return None
    if array_api_compat.is_torch_array(x):
        return x.detach().cpu().numpy()
    if array_api_compat.is_cupy_array(x):
        return x.get()  # CuPy refuses an implicit copy to the host
    return np.asarray(x)

constants_like(table, ref, xp)

A table of numbers in ref's library and dtype, on its device.

The numbers are constants of the problem (weights, the spacing of a grid), and no request for a precision, so a float64 table leaves a float32 field in float32. A table that is an array of xp already, such as the metric of a grid that is traced with its field, keeps its gradient.

Parameters:

Name Type Description Default
table Any

The numbers: an array, a sequence or a number.

required
ref Any

An array of the namespace xp. If its dtype is not floating, the table keeps its own, so that an integer field does not truncate it.

required
xp ModuleType

The namespace of the call.

required

Returns:

Type Description
Any

The table as an array of xp, in its own shape.

Example

import numpy as np constants_like([0.5, 1.5], np.zeros(3, dtype=np.float32), np) array([0.5, 1.5], dtype=float32) constants_like([0.5, 1.5], np.zeros(3, dtype=np.int64), np) array([0.5, 1.5])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_host.py
def constants_like(table: Any, ref: Any, xp: ModuleType) -> Any:
    """A table of numbers in ``ref``'s library and dtype, on its device.

    The numbers are constants of the problem (weights, the spacing of a grid),
    and no request for a precision, so a float64 table leaves a float32 field in
    float32. A table that is an array of ``xp`` already, such as the metric of a
    grid that is traced with its field, keeps its gradient.

    Args:
        table: The numbers: an array, a sequence or a number.
        ref: An array of the namespace ``xp``. If its dtype is not floating, the
            table keeps its own, so that an integer field does not truncate it.
        xp: The namespace of the call.

    Returns:
        The table as an array of ``xp``, in its own shape.

    Example:
        >>> import numpy as np
        >>> constants_like([0.5, 1.5], np.zeros(3, dtype=np.float32), np)
        array([0.5, 1.5], dtype=float32)
        >>> constants_like([0.5, 1.5], np.zeros(3, dtype=np.int64), np)
        array([0.5, 1.5])
    """
    if xp.isdtype(ref.dtype, ("real floating", "complex floating")):
        return place(table, xp, ref.dtype, device_of(ref, xp))
    return table_like(np.asarray(table), ref, xp) if is_host(table) else table

divide_or_nan(numerator, denominator, xp)

The quotient of two arrays, NaN where the denominator is 0.

It goes through a double where, so a gradient is finite in the other elements: the division that is not used divides by 1.

Parameters:

Name Type Description Default
numerator Any

An array of the namespace xp.

required
denominator Any

An array that broadcasts against it, of its dtype.

required
xp ModuleType

The namespace of the call.

required

Returns:

Type Description
Any

The quotient.

Example

import numpy as np divide_or_nan(np.asarray([1.0, 1.0]), np.asarray([2.0, 0.0]), np) array([0.5, nan])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_numerics.py
def divide_or_nan(numerator: Any, denominator: Any, xp: ModuleType) -> Any:
    """The quotient of two arrays, NaN where the denominator is 0.

    It goes through a double ``where``, so a gradient is finite in the other
    elements: the division that is not used divides by 1.

    Args:
        numerator: An array of the namespace ``xp``.
        denominator: An array that broadcasts against it, of its dtype.
        xp: The namespace of the call.

    Returns:
        The quotient.

    Example:
        >>> import numpy as np
        >>> divide_or_nan(np.asarray([1.0, 1.0]), np.asarray([2.0, 0.0]), np)
        array([0.5, nan])
    """
    zero = denominator == 0
    safe = xp.where(zero, xp.ones_like(denominator), denominator)
    quotient = numerator / safe
    return xp.where(zero, xp.full_like(quotient, xp.nan), quotient)

floating_inputs(*values, params=())

The namespace of a call and its array inputs in one floating dtype.

The dtype is the common one of the floating arrays (float32 stays float32). Python numbers and integer or boolean arrays adopt it; with no floating array, everything takes the namespace's default float. Values on the host join the device of the first array that is not.

Parameters:

Name Type Description Default
*values Any

The array inputs: arrays, sequences or Python numbers.

()
params tuple[Any, ...]

Other parameters, Python floats or 0-d arrays. They help to choose the namespace and the device, so a traced JAX scalar selects JAX, and never decide the dtype.

()

Returns:

Type Description
tuple[ModuleType, list[Any]]

The namespace, and values as arrays of one floating dtype.

Raises:

Type Description
BackendError

As namespace_of.

ValueError

If an input is complex, a date or not numeric.

Example

import numpy as np xp, (x, y) = floating_inputs(np.zeros(2, dtype=np.float32), 1.5) x.dtype, y.dtype (dtype('float32'), dtype('float32'))

Source code in packages/pipekit-array/src/pipekit_array/arrays/_backend.py
def floating_inputs(
    *values: Any, params: tuple[Any, ...] = ()
) -> tuple[ModuleType, list[Any]]:
    """The namespace of a call and its array inputs in one floating dtype.

    The dtype is the common one of the floating arrays (float32 stays
    float32). Python numbers and integer or boolean arrays adopt it; with no
    floating array, everything takes the namespace's default float. Values on
    the host join the device of the first array that is not.

    Args:
        *values: The array inputs: arrays, sequences or Python numbers.
        params: Other parameters, Python floats or 0-d arrays. They help to
            choose the namespace and the device, so a traced JAX scalar
            selects JAX, and never decide the dtype.

    Returns:
        The namespace, and ``values`` as arrays of one floating dtype.

    Raises:
        BackendError: As `namespace_of`.
        ValueError: If an input is complex, a date or not numeric.

    Example:
        >>> import numpy as np
        >>> xp, (x, y) = floating_inputs(np.zeros(2, dtype=np.float32), 1.5)
        >>> x.dtype, y.dtype
        (dtype('float32'), dtype('float32'))
    """
    xp = namespace_of(*values, *params)
    devices = (device_of(value, xp) for value in (*values, *params))
    device = next((found for found in devices if found is not None), None)
    floating = []
    for value in values:
        if is_number(value):
            continue
        dtype = xp.asarray(value).dtype if is_host(value) else value.dtype
        if xp.isdtype(dtype, "real floating"):
            floating.append(dtype)
        elif not xp.isdtype(dtype, ("bool", "integral")):
            msg = f"inputs must be real numbers, got dtype {dtype}"
            raise ValueError(msg)
    dtype = xp.result_type(*floating) if floating else xp.asarray(0.0).dtype
    if guard_level() != "off":
        _report_moves((*values, *params), xp, dtype)
    return xp, [place(value, xp, dtype, device) for value in values]

is_dataarray(value)

Say whether a value is an xarray DataArray, without importing xarray.

Example

is_dataarray(1.0) False

Source code in packages/pipekit-array/src/pipekit_array/arrays/_backend.py
def is_dataarray(value: Any) -> bool:
    """Say whether a value is an xarray DataArray, without importing xarray.

    Example:
        >>> is_dataarray(1.0)
        False
    """
    # a DataArray can only exist once xarray is imported
    xarray = sys.modules.get("xarray")
    return xarray is not None and isinstance(value, xarray.DataArray)

like(value, ref, xp)

value (a number or a 0-d array) in ref's dtype, on its device.

Parameters:

Name Type Description Default
value Any

The constant.

required
ref Any

An array of the namespace xp.

required
xp ModuleType

The namespace of the call.

required

Returns:

Type Description
Any

A 0-d array, or value itself if nothing changes.

Example

import numpy as np like(0.1, np.zeros(2, dtype=np.float32), np).dtype dtype('float32')

Source code in packages/pipekit-array/src/pipekit_array/arrays/_host.py
def like(value: Any, ref: Any, xp: ModuleType) -> Any:
    """``value`` (a number or a 0-d array) in ``ref``'s dtype, on its device.

    Args:
        value: The constant.
        ref: An array of the namespace ``xp``.
        xp: The namespace of the call.

    Returns:
        A 0-d array, or ``value`` itself if nothing changes.

    Example:
        >>> import numpy as np
        >>> like(0.1, np.zeros(2, dtype=np.float32), np).dtype
        dtype('float32')
    """
    return place(value, xp, ref.dtype, device_of(ref, xp))

multiply_at(x, index, values)

A copy of an array with some entries multiplied: y = x, then y[index] *= v.

An entry that the index names twice is multiplied twice. On JAX it is x.at[index].multiply(values).

Parameters:

Name Type Description Default
x Any

The array, which is not changed.

required
index Any

As in set_at; an integer array needs NumPy or JAX.

required
values Any

The factors, which broadcast to x[index].

required

Returns:

Type Description
Any

y, in x's library, shape and dtype.

Raises:

Type Description
BackendError

As set_at.

IndexError

As set_at.

Example

import numpy as np multiply_at(np.ones(3), np.asarray([1, 1]), [2.0, 3.0]) array([1., 6., 1.])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_indexing.py
def multiply_at(x: Any, index: Any, values: Any) -> Any:
    """A copy of an array with some entries multiplied: y = x, then y[index] *= v.

    An entry that the index names twice is multiplied twice. On JAX it is
    ``x.at[index].multiply(values)``.

    Args:
        x: The array, which is not changed.
        index: As in `set_at`; an integer array needs NumPy or JAX.
        values: The factors, which broadcast to ``x[index]``.

    Returns:
        y, in ``x``'s library, shape and dtype.

    Raises:
        BackendError: As `set_at`.
        IndexError: As `set_at`.

    Example:
        >>> import numpy as np
        >>> multiply_at(np.ones(3), np.asarray([1, 1]), [2.0, 3.0])
        array([1., 6., 1.])
    """
    xp, x, index = _update_inputs(x, index, values)
    if array_api_compat.is_jax_namespace(xp):
        return x.at[index].multiply(values)
    if _has_integer_array(xp, index):
        return _accumulate("multiply_at", np.multiply, xp, x, index, values)
    return _assign("multiply_at", xp, x, index, x[index] * like(values, x, xp))

namespace_of(*values)

The array namespace that a computation on values runs in.

Python numbers, None, sequences and NumPy arrays join the library of the other arrays; on their own they resolve to NumPy.

Parameters:

Name Type Description Default
*values Any

Arguments of the function being dispatched.

()

Returns:

Type Description
ModuleType

The namespace, such as array_api_compat.numpy or jax.numpy.

Raises:

Type Description
BackendError

If a value is not an array of a known library, or if two libraries other than NumPy meet in one call.

Example

import numpy as np namespace_of(np.zeros(3), 2.0, None).name 'array_api_compat.numpy'

Source code in packages/pipekit-array/src/pipekit_array/arrays/_backend.py
def namespace_of(*values: Any) -> ModuleType:
    """The array namespace that a computation on ``values`` runs in.

    Python numbers, ``None``, sequences and NumPy arrays join the library of
    the other arrays; on their own they resolve to NumPy.

    Args:
        *values: Arguments of the function being dispatched.

    Returns:
        The namespace, such as ``array_api_compat.numpy`` or ``jax.numpy``.

    Raises:
        BackendError: If a value is not an array of a known library, or if two
            libraries other than NumPy meet in one call.

    Example:
        >>> import numpy as np
        >>> namespace_of(np.zeros(3), 2.0, None).__name__
        'array_api_compat.numpy'
    """
    found: dict[str, ModuleType] = {}
    for value in values:
        if is_host(value):
            continue
        try:
            namespace = array_api_compat.array_namespace(value)
        except TypeError as err:
            msg = f"{type(value).__name__} is not an array of a known library"
            raise BackendError(msg) from err
        found[namespace.__name__] = namespace
    if len(found) > 1:
        msg = f"arrays of several libraries in one call: {', '.join(sorted(found))}"
        raise BackendError(msg)
    return next(iter(found.values()), numpy_namespace)

optimization_barrier(x)

The same values, which XLA may not fuse with what comes before.

Parameters:

Name Type Description Default
x Any

An array, or nested tuples, lists and dicts of them.

required

Returns:

Type Description
Any

jax.lax.optimization_barrier(x) on JAX, and x itself elsewhere.

Raises:

Type Description
BackendError

If a leaf is not an array of a known library.

Example

optimization_barrier([1.0]) [1.0]

Source code in packages/pipekit-array/src/pipekit_array/arrays/_transforms.py
def optimization_barrier(x: Any) -> Any:
    """The same values, which XLA may not fuse with what comes before.

    Args:
        x: An array, or nested tuples, lists and dicts of them.

    Returns:
        ``jax.lax.optimization_barrier(x)`` on JAX, and ``x`` itself elsewhere.

    Raises:
        BackendError: If a leaf is not an array of a known library.

    Example:
        >>> optimization_barrier([1.0])
        [1.0]
    """
    if _on_jax(x):
        import jax

        return jax.lax.optimization_barrier(x)
    namespace_of(*_leaves(x))
    return x

pad(x, widths)

An array with zeros around it.

Parameters:

Name Type Description Default
x Any

The array.

required
widths tuple[tuple[int, int], ...]

How many zeros before and after, for each axis.

required

Returns:

Type Description
Any

The larger array, in x's library and dtype, on its device.

Raises:

Type Description
BackendError

If x is not an array of a known library.

ValueError

If widths is not one pair per axis.

Example

import numpy as np pad(np.ones(2), ((1, 2),)) array([0., 1., 1., 0., 0.])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_indexing.py
def pad(x: Any, widths: tuple[tuple[int, int], ...]) -> Any:
    """An array with zeros around it.

    Args:
        x: The array.
        widths: How many zeros before and after, for each axis.

    Returns:
        The larger array, in ``x``'s library and dtype, on its device.

    Raises:
        BackendError: If ``x`` is not an array of a known library.
        ValueError: If ``widths`` is not one pair per axis.

    Example:
        >>> import numpy as np
        >>> pad(np.ones(2), ((1, 2),))
        array([0., 1., 1., 0., 0.])
    """
    xp = namespace_of(x)
    x = xp.asarray(x) if is_host(x) else x
    if len(widths) != x.ndim:
        msg = f"pad needs one pair of widths per axis, got {len(widths)} for {x.ndim}"
        raise ValueError(msg)
    native = getattr(xp, "pad", None)
    if native is not None:
        return native(x, widths)

    def zeros(width: int, axis: int) -> Any:
        shape = (*x.shape[:axis], width, *x.shape[axis + 1 :])
        return xp.zeros(shape, dtype=x.dtype, device=array_api_compat.device(x))

    for axis, (before, after) in enumerate(widths):
        x = xp.concat([zeros(before, axis), x, zeros(after, axis)], axis=axis)
    return x

reduction_inputs(values, weights, valid, *, params=())

The namespace of a reduction, and its inputs broadcast against each other.

For a reduction that weighted_sum, weighted_mean and Groups do not cover, with their rules for masks and weights.

Parameters:

Name Type Description Default
values Any

x.

required
weights Any

w, or None.

required
valid Any

The validity, boolean (True = use), or None.

required
params tuple[Any, ...]

Other arguments, as in floating_inputs: they help to choose the namespace and the device, and never decide the dtype.

()

Returns:

Type Description
ModuleType

The namespace, x and w in one floating dtype, and the validity as a

Any

boolean array; w and the validity stay None where they are.

Raises:

Type Description
BackendError

If an argument is a labelled array, or of another library.

ValueError

If valid is not boolean.

Example

import numpy as np xp, x, w, m = reduction_inputs(np.ones((2, 3)), [1.0, 2.0, 3.0], None) x.shape, w.shape, m ((2, 3), (2, 3), None)

Source code in packages/pipekit-array/src/pipekit_array/arrays/_reductions.py
def reduction_inputs(
    values: Any, weights: Any, valid: Any, *, params: tuple[Any, ...] = ()
) -> tuple[ModuleType, Any, Any, Any]:
    """The namespace of a reduction, and its inputs broadcast against each other.

    For a reduction that `weighted_sum`, `weighted_mean` and `Groups` do not
    cover, with their rules for masks and weights.

    Args:
        values: x.
        weights: w, or ``None``.
        valid: The validity, boolean (True = use), or ``None``.
        params: Other arguments, as in `floating_inputs`: they help to choose the
            namespace and the device, and never decide the dtype.

    Returns:
        The namespace, x and w in one floating dtype, and the validity as a
        boolean array; w and the validity stay ``None`` where they are.

    Raises:
        BackendError: If an argument is a labelled array, or of another library.
        ValueError: If ``valid`` is not boolean.

    Example:
        >>> import numpy as np
        >>> xp, x, w, m = reduction_inputs(np.ones((2, 3)), [1.0, 2.0, 3.0], None)
        >>> x.shape, w.shape, m
        ((2, 3), (2, 3), None)
    """
    if weights is None:
        xp, (x,) = floating_inputs(values, params=(valid, *params))
        w = None
    else:
        xp, (x, w) = floating_inputs(values, weights, params=(valid, *params))
    m = None if valid is None else boolean_like(valid, x, xp)
    given = [array for array in (x, w, m) if array is not None]
    broadcast = iter(xp.broadcast_arrays(*given))
    return (
        xp,
        next(broadcast),
        None if w is None else next(broadcast),
        None if m is None else next(broadcast),
    )

register_record(cls, *, meta_fields=())

Register a frozen dataclass as a JAX pytree, now or once JAX loads.

Without JAX the record is never registered, and works as it is.

Parameters:

Name Type Description Default
cls T

A frozen dataclass whose fields are arrays, numbers or records.

required
meta_fields tuple[str, ...]

The names of the fields that are static under JAX: they are compared and hashed rather than traced.

()

Returns:

Type Description
T

cls itself.

Example

@register_record ... @dataclasses.dataclass(frozen=True) ... class Trend: ... slope: float Trend(2.0) Trend(slope=2.0)

Source code in packages/pipekit-array/src/pipekit_array/arrays/_records.py
def register_record[T: type](cls: T, *, meta_fields: tuple[str, ...] = ()) -> T:
    """Register a frozen dataclass as a JAX pytree, now or once JAX loads.

    Without JAX the record is never registered, and works as it is.

    Args:
        cls: A frozen dataclass whose fields are arrays, numbers or records.
        meta_fields: The names of the fields that are static under JAX: they
            are compared and hashed rather than traced.

    Returns:
        ``cls`` itself.

    Example:
        >>> @register_record
        ... @dataclasses.dataclass(frozen=True)
        ... class Trend:
        ...     slope: float
        >>> Trend(2.0)
        Trend(slope=2.0)
    """
    names = [field.name for field in dataclasses.fields(cast("Any", cls))]
    data_fields = [name for name in names if name not in meta_fields]

    def register(jax: ModuleType) -> None:
        jax.tree_util.register_dataclass(
            cls, data_fields=data_fields, meta_fields=list(meta_fields)
        )

    on_import("jax", register)
    return cls

require_floating(array, name, *, error=ValueError)

Raise unless an array has a real floating dtype; nothing is cast.

Parameters:

Name Type Description Default
array Any

An array of any library, concrete or traced, a sequence or a number.

required
name str

What the argument is called, for the error.

required
error type[Exception]

The exception to raise.

ValueError

Raises:

Type Description
ValueError

error, if the dtype is not real floating.

BackendError

If array is not an array of a known library.

Example

import numpy as np require_floating(np.zeros(3), "x") require_floating(np.arange(3), "x") Traceback (most recent call last): ... ValueError: 'x' must have a floating dtype, got int64

Source code in packages/pipekit-array/src/pipekit_array/arrays/_checks.py
def require_floating(
    array: Any, name: str, *, error: type[Exception] = ValueError
) -> None:
    """Raise unless an array has a real floating dtype; nothing is cast.

    Args:
        array: An array of any library, concrete or traced, a sequence or a
            number.
        name: What the argument is called, for the error.
        error: The exception to raise.

    Raises:
        ValueError: ``error``, if the dtype is not real floating.
        BackendError: If ``array`` is not an array of a known library.

    Example:
        >>> import numpy as np
        >>> require_floating(np.zeros(3), "x")
        >>> require_floating(np.arange(3), "x")
        Traceback (most recent call last):
            ...
        ValueError: 'x' must have a floating dtype, got int64
    """
    xp = namespace_of(array)
    dtype = np.asarray(array).dtype if is_host(array) else array.dtype
    if not xp.isdtype(dtype, "real floating"):
        msg = f"'{name}' must have a floating dtype, got {dtype}"
        raise error(msg)

safe_atan2(y, x, xp)

atan2(y, x), and 0 with a zero gradient at the origin.

Parameters:

Name Type Description Default
y Any

A floating array of the namespace xp.

required
x Any

An array that broadcasts against it, of its dtype.

required
xp ModuleType

The namespace of the call.

required

Returns:

Type Description
Any

The angles [rad], in (-π, π].

Example

import numpy as np safe_atan2(np.asarray([0.0, 1.0]), np.asarray([0.0, 0.0]), np).round(6) array([0. , 1.570796])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_numerics.py
def safe_atan2(y: Any, x: Any, xp: ModuleType) -> Any:
    """atan2(y, x), and 0 with a zero gradient at the origin.

    Args:
        y: A floating array of the namespace ``xp``.
        x: An array that broadcasts against it, of its dtype.
        xp: The namespace of the call.

    Returns:
        The angles [rad], in (-π, π].

    Example:
        >>> import numpy as np
        >>> safe_atan2(np.asarray([0.0, 1.0]), np.asarray([0.0, 0.0]), np).round(6)
        array([0.      , 1.570796])
    """
    origin = (x == 0) & (y == 0)
    # atan2(0, 1) = 0 at the origin, so that the unused branch is finite
    safe_x = xp.where(origin, xp.ones_like(x), x)
    safe_y = xp.where(origin, xp.zeros_like(y), y)
    return xp.atan2(safe_y, safe_x)

safe_power(x, p, xp)

xᵖ for x > 0, 0 with a zero gradient for x ≤ 0, and NaN for NaN.

The rule at x ≤ 0 is that of safe_sqrt, and does not depend on p: it is 0 for a negative p, too. The gradient by p is finite.

Parameters:

Name Type Description Default
x Any

A floating array of the namespace xp.

required
p Any

The exponent, a number or an array that broadcasts against x.

required
xp ModuleType

The namespace of the call.

required

Returns:

Type Description
Any

The powers.

Example

import numpy as np safe_power(np.asarray([4.0, 0.0, -1.0, np.nan]), 1.5, np) array([ 8., 0., 0., nan])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_numerics.py
def safe_power(x: Any, p: Any, xp: ModuleType) -> Any:
    """xᵖ for x > 0, 0 with a zero gradient for x ≤ 0, and NaN for NaN.

    The rule at x ≤ 0 is that of `safe_sqrt`, and does not depend on p: it is 0
    for a negative p, too. The gradient by p is finite.

    Args:
        x: A floating array of the namespace ``xp``.
        p: The exponent, a number or an array that broadcasts against ``x``.
        xp: The namespace of the call.

    Returns:
        The powers.

    Example:
        >>> import numpy as np
        >>> safe_power(np.asarray([4.0, 0.0, -1.0, np.nan]), 1.5, np)
        array([ 8.,  0.,  0., nan])
    """
    zero = x <= 0
    power = xp.pow(xp.where(zero, xp.ones_like(x), x), constants_like(p, x, xp))
    return xp.where(zero, xp.zeros_like(power), power)

safe_sqrt(x, xp)

√x for x > 0, 0 with a zero gradient for x ≤ 0, and NaN for NaN.

A difference of squares can round below 0, and its root is then 0; a missing value stays missing. It goes through a double where, so the gradient is finite at 0.

Parameters:

Name Type Description Default
x Any

A floating array of the namespace xp.

required
xp ModuleType

The namespace of the call.

required

Returns:

Type Description
Any

The roots.

Example

import numpy as np safe_sqrt(np.asarray([4.0, 0.0, -1.0, np.nan]), np) array([ 2., 0., 0., nan])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_numerics.py
def safe_sqrt(x: Any, xp: ModuleType) -> Any:
    """√x for x > 0, 0 with a zero gradient for x ≤ 0, and NaN for NaN.

    A difference of squares can round below 0, and its root is then 0; a missing
    value stays missing. It goes through a double ``where``, so the gradient is
    finite at 0.

    Args:
        x: A floating array of the namespace ``xp``.
        xp: The namespace of the call.

    Returns:
        The roots.

    Example:
        >>> import numpy as np
        >>> safe_sqrt(np.asarray([4.0, 0.0, -1.0, np.nan]), np)
        array([ 2.,  0.,  0., nan])
    """
    zero = x <= 0  # False for NaN
    # the root of 1 where the result is 0, so that the unused branch is finite
    root = xp.sqrt(xp.where(zero, xp.ones_like(x), x))
    return xp.where(zero, xp.zeros_like(x), root)

scan(f, init, xs=None, length=None, reverse=False)

A recurrence: (c_k, y_k) = f(c_{k-1}, x_k) for k = 0, …, K - 1.

On JAX data, in init or xs, it is jax.lax.scan. Elsewhere f is called once per step, which is slower than compiled code: a recurrence should have few steps over large arrays there.

Parameters:

Name Type Description Default
f Callable[[Any, Any], tuple[Any, Any]]

The step, from the carry and a slice of xs to the next carry and an output.

required
init Any

c_{-1}, the first carry. Off JAX it may be any object.

required
xs Any

The inputs, sliced along their first axis: an array, or nested tuples, lists and dicts of arrays. None gives each step None.

None
length int | None

K, for xs=None; otherwise the size of the first axis.

None
reverse bool

Run from the last input to the first. Each output keeps the place of its input.

False

Returns:

Type Description
Any

The last carry, and the outputs stacked along a new first axis; an

Any

output of None stays None.

Raises:

Type Description
ValueError

If the inputs differ in length, or their number is unknown. Off JAX also if there is no step, because the shape of an output is then unknown.

BackendError

If an input is not an array of a known library.

Example

import numpy as np total, partial = scan(lambda c, x: (c + x, c + x), 0.0, np.arange(4.0)) float(total), partial.tolist() (6.0, [0.0, 1.0, 3.0, 6.0])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_transforms.py
def scan(
    f: Callable[[Any, Any], tuple[Any, Any]],
    init: Any,
    xs: Any = None,
    length: int | None = None,
    reverse: bool = False,
) -> tuple[Any, Any]:
    """A recurrence: (c_k, y_k) = f(c_{k-1}, x_k) for k = 0, …, K - 1.

    On JAX data, in ``init`` or ``xs``, it is ``jax.lax.scan``. Elsewhere ``f``
    is called once per step, which is slower than compiled code: a recurrence
    should have few steps over large arrays there.

    Args:
        f: The step, from the carry and a slice of ``xs`` to the next carry and
            an output.
        init: c_{-1}, the first carry. Off JAX it may be any object.
        xs: The inputs, sliced along their first axis: an array, or nested
            tuples, lists and dicts of arrays. ``None`` gives each step ``None``.
        length: K, for ``xs=None``; otherwise the size of the first axis.
        reverse: Run from the last input to the first. Each output keeps the
            place of its input.

    Returns:
        The last carry, and the outputs stacked along a new first axis; an
        output of ``None`` stays ``None``.

    Raises:
        ValueError: If the inputs differ in length, or their number is unknown.
            Off JAX also if there is no step, because the shape of an output is
            then unknown.
        BackendError: If an input is not an array of a known library.

    Example:
        >>> import numpy as np
        >>> total, partial = scan(lambda c, x: (c + x, c + x), 0.0, np.arange(4.0))
        >>> float(total), partial.tolist()
        (6.0, [0.0, 1.0, 3.0, 6.0])
    """
    if _on_jax((init, xs)):
        import jax

        return jax.lax.scan(f, init, xs, length=length, reverse=reverse)
    inputs = [] if xs is None else [xs]
    if not inputs and length is None:
        msg = "scan needs xs or a length"
        raise ValueError(msg)
    size = _leading_size("scan", inputs, length=length)
    steps = range(size)
    carry, outputs = init, []
    for k in reversed(steps) if reverse else steps:
        carry, output = f(carry, _entry(xs, k))
        outputs.append(output)
    return carry, _stacked("scan", outputs[::-1] if reverse else outputs)

set_at(x, index, values)

A copy of an array with some entries replaced: y = x, then y[index] = v.

On JAX it is x.at[index].set(values).

Parameters:

Name Type Description Default
x Any

The array, which is not changed.

required
index Any

What x[index] accepts: integers, slices, an ellipsis, a boolean mask or an integer array. An entry that an integer array names twice takes one of its two values.

required
values Any

The new entries, which broadcast to x[index] and take the dtype of x.

required

Returns:

Type Description
Any

y, in x's library, shape and dtype.

Raises:

Type Description
BackendError

If the array library does not take that kind of index, or if two libraries other than NumPy meet in one call.

IndexError

If the index is out of bounds. JAX does not report it, and leaves the array as it is.

Example

import numpy as np set_at(np.zeros(3), slice(1, None), [1.0, 2.0]) array([0., 1., 2.])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_indexing.py
def set_at(x: Any, index: Any, values: Any) -> Any:
    """A copy of an array with some entries replaced: y = x, then y[index] = v.

    On JAX it is ``x.at[index].set(values)``.

    Args:
        x: The array, which is not changed.
        index: What ``x[index]`` accepts: integers, slices, an ellipsis, a
            boolean mask or an integer array. An entry that an integer array
            names twice takes one of its two values.
        values: The new entries, which broadcast to ``x[index]`` and take the
            dtype of ``x``.

    Returns:
        y, in ``x``'s library, shape and dtype.

    Raises:
        BackendError: If the array library does not take that kind of index, or
            if two libraries other than NumPy meet in one call.
        IndexError: If the index is out of bounds. JAX does not report it, and
            leaves the array as it is.

    Example:
        >>> import numpy as np
        >>> set_at(np.zeros(3), slice(1, None), [1.0, 2.0])
        array([0., 1., 2.])
    """
    xp, x, index = _update_inputs(x, index, values)
    if array_api_compat.is_jax_namespace(xp):
        return x.at[index].set(values)
    return _assign("set_at", xp, x, index, values)

squared_magnitude(z, xp)

|z|² = Re(z)² + Im(z)², with a gradient at 0, which that of |z| has not.

Parameters:

Name Type Description Default
z Any

A real or complex array of the namespace xp.

required
xp ModuleType

The namespace of the call.

required

Returns:

Type Description
Any

The squares, real.

Example

import numpy as np squared_magnitude(np.asarray([3.0 + 4.0j, 0.0]), np) array([25., 0.])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_numerics.py
def squared_magnitude(z: Any, xp: ModuleType) -> Any:
    """|z|² = Re(z)² + Im(z)², with a gradient at 0, which that of |z| has not.

    Args:
        z: A real or complex array of the namespace ``xp``.
        xp: The namespace of the call.

    Returns:
        The squares, real.

    Example:
        >>> import numpy as np
        >>> squared_magnitude(np.asarray([3.0 + 4.0j, 0.0]), np)
        array([25.,  0.])
    """
    if not xp.isdtype(z.dtype, "complex floating"):
        return z * z
    return xp.real(z) ** 2 + xp.imag(z) ** 2

stand_in(x, mask, xp, *, reference=None)

Rows of an array, with a valid one in place of each row to leave out.

A function that is undefined at a row gives NaN there, and masking its result alone leaves 0 times NaN in the reverse pass, which spoils the gradient of every row. So it is evaluated somewhere valid instead, and its result masked.

Parameters:

Name Type Description Default
x Any

The rows, (n, *rest), an array of the namespace xp.

required
mask Any

Which rows to use (True = use), boolean (n,), of the same library.

required
xp ModuleType

The namespace of the call.

required
reference Any

The row, (*rest), to put in place of the others. If not given, the first one in use, or the first one of all if none is.

None

Returns:

Type Description
Any

The rows, in x's shape and dtype. No gradient reaches a row that is

Any

not in use, or the reference.

Example

import numpy as np x = np.asarray([[np.nan, 0.0], [1.0, 2.0]]) stand_in(x, np.asarray([False, True]), np) array([[1., 2.], [1., 2.]])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_numerics.py
def stand_in(x: Any, mask: Any, xp: ModuleType, *, reference: Any = None) -> Any:
    """Rows of an array, with a valid one in place of each row to leave out.

    A function that is undefined at a row gives NaN there, and masking its result
    alone leaves 0 times NaN in the reverse pass, which spoils the gradient of every
    row. So it is evaluated somewhere valid instead, and its result masked.

    Args:
        x: The rows, (n, *rest), an array of the namespace ``xp``.
        mask: Which rows to use (True = use), boolean (n,), of the same library.
        xp: The namespace of the call.
        reference: The row, (*rest), to put in place of the others. If not given,
            the first one in use, or the first one of all if none is.

    Returns:
        The rows, in ``x``'s shape and dtype. No gradient reaches a row that is
        not in use, or the reference.

    Example:
        >>> import numpy as np
        >>> x = np.asarray([[np.nan, 0.0], [1.0, 2.0]])
        >>> stand_in(x, np.asarray([False, True]), np)
        array([[1., 2.],
               [1., 2.]])
    """
    if reference is None:
        first = xp.argmax(xp.astype(mask, xp.int32), keepdims=True)
        reference = xp.take(x, first, axis=0)[0, ...]
    reference = constant(like(reference, x, xp), xp)
    return xp.where(xp.reshape(mask, (-1, *[1] * (x.ndim - 1))), x, reference)

stop_gradient(x)

The same values, as a constant of the derivative.

Parameters:

Name Type Description Default
x Any

An array, or nested tuples, lists and dicts of them.

required

Returns:

Type Description
Any

jax.lax.stop_gradient(x) on JAX, the detached tensors on torch, and

Any

x itself on a library without automatic differentiation.

Raises:

Type Description
BackendError

If a leaf is not an array of a known library.

Example

stop_gradient([1.0]) [1.0]

Source code in packages/pipekit-array/src/pipekit_array/arrays/_transforms.py
def stop_gradient(x: Any) -> Any:
    """The same values, as a constant of the derivative.

    Args:
        x: An array, or nested tuples, lists and dicts of them.

    Returns:
        ``jax.lax.stop_gradient(x)`` on JAX, the detached tensors on torch, and
        ``x`` itself on a library without automatic differentiation.

    Raises:
        BackendError: If a leaf is not an array of a known library.

    Example:
        >>> stop_gradient([1.0])
        [1.0]
    """
    if _on_jax(x):
        import jax

        return jax.lax.stop_gradient(x)
    xp = namespace_of(*_leaves(x))
    if array_api_compat.is_torch_namespace(xp):
        return _tree_map(lambda leaf: leaf if is_host(leaf) else constant(leaf, xp), x)
    return x

table_like(table, ref, xp)

A NumPy table (an index, a mask) in ref's library, on its device.

Unlike like, it keeps its own shape and dtype.

Parameters:

Name Type Description Default
table ndarray

The table.

required
ref Any

An array of the namespace xp.

required
xp ModuleType

The namespace of the call.

required

Returns:

Type Description
Any

The table as an array of xp.

Example

import numpy as np table_like(np.arange(3), np.zeros(2, dtype=np.float32), np).tolist() [0, 1, 2]

Source code in packages/pipekit-array/src/pipekit_array/arrays/_host.py
def table_like(table: np.ndarray, ref: Any, xp: ModuleType) -> Any:
    """A NumPy table (an index, a mask) in ``ref``'s library, on its device.

    Unlike `like`, it keeps its own shape and dtype.

    Args:
        table: The table.
        ref: An array of the namespace ``xp``.
        xp: The namespace of the call.

    Returns:
        The table as an array of ``xp``.

    Example:
        >>> import numpy as np
        >>> table_like(np.arange(3), np.zeros(2, dtype=np.float32), np).tolist()
        [0, 1, 2]
    """
    device = device_of(ref, xp)
    return xp.asarray(table) if device is None else xp.asarray(table, device=device)

used_weights(xp, x, w, m)

The weight m w that each sample enters a reduction with.

Parameters:

Name Type Description Default
xp ModuleType

The namespace of the call.

required
x Any

The samples, as reduction_inputs returns them.

required
w Any

The weights in x's shape and dtype, or None for 1.

required
m Any

The validity in x's shape, or None if every sample is valid.

required

Returns:

Type Description
Any

m w, in x's shape and dtype.

Example

import numpy as np x = np.asarray([1.0, np.nan]) used_weights(np, x, None, np.asarray([True, False])) array([1., 0.])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_reductions.py
def used_weights(xp: ModuleType, x: Any, w: Any, m: Any) -> Any:
    """The weight m w that each sample enters a reduction with.

    Args:
        xp: The namespace of the call.
        x: The samples, as `reduction_inputs` returns them.
        w: The weights in ``x``'s shape and dtype, or ``None`` for 1.
        m: The validity in ``x``'s shape, or ``None`` if every sample is valid.

    Returns:
        m w, in ``x``'s shape and dtype.

    Example:
        >>> import numpy as np
        >>> x = np.asarray([1.0, np.nan])
        >>> used_weights(np, x, None, np.asarray([True, False]))
        array([1., 0.])
    """
    weight = xp.ones_like(x) if w is None else w
    return weight if m is None else xp.where(m, weight, xp.zeros_like(weight))

vmap(fn, in_axes=0)

A function mapped over an axis of its arguments.

On JAX data it is jax.vmap. Elsewhere fn is called once per entry of the axis, and the results are stacked, which is slower than a function that broadcasts.

Parameters:

Name Type Description Default
fn Callable[..., Any]

A function of arrays, or of nested tuples, lists and dicts of them.

required
in_axes int | None | tuple

The axis to map over, of every argument or, in a tuple, of each one; None passes an argument whole to every call.

0

Returns:

Type Description
Callable[..., Any]

The mapped function. The mapped axis comes first in its results.

Raises:

Type Description
ValueError

(the mapped function) if no argument is mapped, or if the mapped axes differ in size or, off JAX, are empty.

BackendError

(the mapped function) if a mapped argument is not an array of a known library.

Example

import numpy as np vmap(np.linalg.norm)(np.asarray([[3.0, 4.0], [6.0, 8.0]])) array([ 5., 10.]) vmap(np.dot, in_axes=(0, None))(np.eye(2), np.asarray([1.0, 2.0])) array([1., 2.])

Source code in packages/pipekit-array/src/pipekit_array/arrays/_transforms.py
def vmap(fn: Callable[..., Any], in_axes: int | None | tuple = 0) -> Callable[..., Any]:
    """A function mapped over an axis of its arguments.

    On JAX data it is ``jax.vmap``. Elsewhere ``fn`` is called once per entry of
    the axis, and the results are stacked, which is slower than a function that
    broadcasts.

    Args:
        fn: A function of arrays, or of nested tuples, lists and dicts of them.
        in_axes: The axis to map over, of every argument or, in a tuple, of each
            one; ``None`` passes an argument whole to every call.

    Returns:
        The mapped function. The mapped axis comes first in its results.

    Raises:
        ValueError: (the mapped function) if no argument is mapped, or if the
            mapped axes differ in size or, off JAX, are empty.
        BackendError: (the mapped function) if a mapped argument is not an array
            of a known library.

    Example:
        >>> import numpy as np
        >>> vmap(np.linalg.norm)(np.asarray([[3.0, 4.0], [6.0, 8.0]]))
        array([ 5., 10.])
        >>> vmap(np.dot, in_axes=(0, None))(np.eye(2), np.asarray([1.0, 2.0]))
        array([1., 2.])
    """

    def mapped(*args: Any) -> Any:
        if _on_jax(args):
            import jax

            return jax.vmap(fn, in_axes=in_axes)(*args)
        axes = in_axes if isinstance(in_axes, tuple) else (in_axes,) * len(args)
        fronted = [
            (arg, None) if axis is None else (_moved(arg, axis), 0)
            for arg, axis in zip(args, axes, strict=True)
        ]
        size = _leading_size("vmap", [arg for arg, axis in fronted if axis == 0])
        calls = [
            fn(*(arg if axis is None else _entry(arg, k) for arg, axis in fronted))
            for k in range(size)
        ]
        return _stacked("vmap", calls)

    return mapped

weighted_mean(values, *, weights=None, valid=None, axis=None)

The mean of x over the valid samples, weighted by w.

x̄ = ∑ₙ mₙ wₙ xₙ / ∑ₙ mₙ wₙ, with m the validity, 1 or 0.

Parameters:

Name Type Description Default
values Any

x, samples of any shape.

required
weights Any

w ≥ 0, broadcast against values; they need not sum to 1.

None
valid Any

Which samples to use (True = use), broadcast against values. A NaN in a valid sample gives NaN, and one in an invalid sample reaches neither the mean nor its gradient.

None
axis Axis

The axis or axes to average over; every axis if not given.

None

Returns:

Type Description
Any

x̄, the array without the reduced axes, NaN where the weights used sum

Any

to 0.

Raises:

Type Description
BackendError

If an argument is a labelled array, or of another library.

ValueError

If valid is not boolean.

Example

import numpy as np x = np.asarray([[1.0, 4.0], [2.0, np.nan]]) weighted_mean(x, weights=[3.0, 1.0], valid=~np.isnan(x), axis=-1) array([1.75, 2. ]) float(weighted_mean(x[0], weights=[0.0, 0.0])) nan

Source code in packages/pipekit-array/src/pipekit_array/arrays/_reductions.py
def weighted_mean(
    values: Any,
    *,
    weights: Any = None,
    valid: Any = None,
    axis: Axis = None,
) -> Any:
    """The mean of x over the valid samples, weighted by w.

    x̄ = ∑ₙ mₙ wₙ xₙ / ∑ₙ mₙ wₙ, with m the validity, 1 or 0.

    Args:
        values: x, samples of any shape.
        weights: w ≥ 0, broadcast against ``values``; they need not sum to 1.
        valid: Which samples to use (True = use), broadcast against ``values``.
            A NaN in a valid sample gives NaN, and one in an invalid sample
            reaches neither the mean nor its gradient.
        axis: The axis or axes to average over; every axis if not given.

    Returns:
        x̄, the array without the reduced axes, NaN where the weights used sum
        to 0.

    Raises:
        BackendError: If an argument is a labelled array, or of another library.
        ValueError: If ``valid`` is not boolean.

    Example:
        >>> import numpy as np
        >>> x = np.asarray([[1.0, 4.0], [2.0, np.nan]])
        >>> weighted_mean(x, weights=[3.0, 1.0], valid=~np.isnan(x), axis=-1)
        array([1.75, 2.  ])
        >>> float(weighted_mean(x[0], weights=[0.0, 0.0]))
        nan
    """
    xp, x, w, m = reduction_inputs(values, weights, valid)
    # S = ∑ₙ mₙ wₙ xₙ
    total = xp.sum(_kept(xp, x, w, m), axis=axis, dtype=x.dtype)
    # W = ∑ₙ mₙ wₙ
    weight = xp.sum(used_weights(xp, x, w, m), axis=axis, dtype=x.dtype)
    return divide_or_nan(total, weight, xp)

weighted_sum(values, *, weights=None, valid=None, axis=None)

The sum of w x over the valid samples.

S = ∑ₙ mₙ wₙ xₙ, with m the validity, 1 or 0.

Parameters:

Name Type Description Default
values Any

x, samples of any shape.

required
weights Any

w, broadcast against values; 1 if not given.

None
valid Any

Which samples to use (True = use), broadcast against values. A NaN in a valid sample gives NaN, and one in an invalid sample reaches neither the sum nor its gradient.

None
axis Axis

The axis or axes to sum over; every axis if not given.

None

Returns:

Type Description
Any

S, the array without the reduced axes. An empty sum is 0.

Raises:

Type Description
BackendError

If an argument is a labelled array, or of another library.

ValueError

If valid is not boolean.

Example

import numpy as np x = np.asarray([1.0, np.nan, 3.0]) float(weighted_sum(x, valid=[True, False, True])) 4.0 float(weighted_sum(x[[0, 2]], weights=[0.5, 2.0])) 6.5

Source code in packages/pipekit-array/src/pipekit_array/arrays/_reductions.py
def weighted_sum(
    values: Any,
    *,
    weights: Any = None,
    valid: Any = None,
    axis: Axis = None,
) -> Any:
    """The sum of w x over the valid samples.

    S = ∑ₙ mₙ wₙ xₙ, with m the validity, 1 or 0.

    Args:
        values: x, samples of any shape.
        weights: w, broadcast against ``values``; 1 if not given.
        valid: Which samples to use (True = use), broadcast against ``values``.
            A NaN in a valid sample gives NaN, and one in an invalid sample
            reaches neither the sum nor its gradient.
        axis: The axis or axes to sum over; every axis if not given.

    Returns:
        S, the array without the reduced axes. An empty sum is 0.

    Raises:
        BackendError: If an argument is a labelled array, or of another library.
        ValueError: If ``valid`` is not boolean.

    Example:
        >>> import numpy as np
        >>> x = np.asarray([1.0, np.nan, 3.0])
        >>> float(weighted_sum(x, valid=[True, False, True]))
        4.0
        >>> float(weighted_sum(x[[0, 2]], weights=[0.5, 2.0]))
        6.5
    """
    xp, x, w, m = reduction_inputs(values, weights, valid)
    # S = ∑ₙ mₙ wₙ xₙ
    return xp.sum(_kept(xp, x, w, m), axis=axis, dtype=x.dtype)

Named carriers

pipekit_array.named is for carriers with dimension names: a DimArray, an xr.DataArray, or any carrier with a registered adapter. It joins NumPy to JAX, and refuses other mixes (ADR A24). to_rows and from_rows turn a carrier into a (n_samples, n_features) table and back (geostack OP-O3), and batch groups carriers into fixed-size Padded batches (ADR A10).

pipekit_array.named

Functions of named carriers: arrays with dimension names.

A carrier is a DimArray, an xr.DataArray ([xarray]), a geoarray carrier: anything with a registered pipekit_ops.NamedArrayAdapter. Each function is written once, over that adapter, and behaves the same on every carrier. The rule for joining this list (GA-Q3): a function never reads coordinate values or geometry. resample, groupby and rolling stay xarray's; reprojection and sampling at points stay in geoarray.

  • Select: isel, sel, take_along.
  • Shape: transpose, rename, expand_dims, squeeze, broadcast_like, merge_dims, split_dims.
  • Reduce: reduce, argreduce, cumreduce, quantile, dot.
  • Shift: pad, roll, shift, diff, where.
  • Window: window, coarsen.
  • Join: stack, stack_items, concat, combine.
  • Map: map_over.
  • Batches and tables: batch, Padded, to_rows, from_rows, RowLayout.
  • The plain carrier: DimArray, DimArrayAdapter; core_dims and vmapped_dims for a carrier that is a pytree.
  • Dispatch: array_namespace, join_backends.

Fast paths. An adapter may provide a method of the same name (the xarray adapter's reduce stays lazy on dask; geoarray's map_over is one jax.vmap over data and transform rows). It is called with the same arguments instead of the generic path; the test kit (pipekit_array.testing) proves the two agree.

The Operator forms live in pipekit_array.operators, in files of the same names. This area needs no extra.

DimArray dataclass

A raw array with one name per axis and static labels on some dims.

Parameters:

Name Type Description Default
data Any

A NumPy, JAX or dask array.

required
dims tuple[str, ...]

One unique name per axis of data.

required
labels Mapping[str, tuple[Any, ...]]

Static labels per dim, e.g. {"band": ("B11", "B12")}; each has one entry per index of its dim. Dims left out have none. Values are stored as tuples.

dict()

Raises:

Type Description
ValueError

The dims do not match the data's rank, repeat a name, or a label tuple's length differs from its dim's size.

Example

import numpy as np mu = DimArray(np.zeros((3, 2)), ("col", "band"), {"band": ["B11", "B12"]}) mu.dims, mu.shape, mu.labels["band"] (('col', 'band'), (3, 2), ('B11', 'B12'))

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
@dataclasses.dataclass(frozen=True, eq=False)
class DimArray:
    """A raw array with one name per axis and static labels on some dims.

    Args:
        data: A NumPy, JAX or dask array.
        dims: One unique name per axis of ``data``.
        labels: Static labels per dim, e.g. ``{"band": ("B11", "B12")}``;
            each has one entry per index of its dim. Dims left out have
            none. Values are stored as tuples.

    Raises:
        ValueError: The dims do not match the data's rank, repeat a name,
            or a label tuple's length differs from its dim's size.

    Example:
        >>> import numpy as np
        >>> mu = DimArray(np.zeros((3, 2)), ("col", "band"), {"band": ["B11", "B12"]})
        >>> mu.dims, mu.shape, mu.labels["band"]
        (('col', 'band'), (3, 2), ('B11', 'B12'))
    """

    data: Any
    dims: tuple[str, ...]
    labels: Mapping[str, tuple[Any, ...]] = dataclasses.field(default_factory=dict)

    def __post_init__(self) -> None:
        dims = tuple(self.dims)
        shape = tuple(self.data.shape)
        if len(dims) != len(shape):
            raise ValueError(f"DimArray dims {dims} for data of shape {shape}.")
        if len(set(dims)) != len(dims):
            raise ValueError(f"DimArray dims must be unique, got {dims}.")
        labels: dict[str, tuple[Any, ...]] = {}
        for dim, values in dict(self.labels).items():
            if dim not in dims:
                raise ValueError(f"labels for {dim!r}, which is not in dims {dims}.")
            entries = tuple(values)
            size = shape[dims.index(dim)]
            if len(entries) != size:
                raise ValueError(
                    f"{len(entries)} labels for dim {dim!r} of size {size}."
                )
            labels[dim] = entries
        object.__setattr__(self, "dims", dims)
        object.__setattr__(self, "labels", labels)

    @property
    def shape(self) -> tuple[int, ...]:
        """The data's shape."""
        return tuple(self.data.shape)

    @property
    def dtype(self) -> Any:
        """The data's dtype."""
        return self.data.dtype

    def __array__(self, dtype: Any = None, copy: bool | None = None) -> Any:
        """The data as a NumPy array, so ``np.asarray(d)`` works (dims are dropped)."""
        import numpy as np

        if backend_name(self.data) == "jax":
            report_move("DimArray.__array__", "JAX data copied to the host")
        if copy is None:
            return np.asarray(self.data, dtype=dtype)
        # copy=True always copies; copy=False raises if a copy is needed
        return np.array(self.data, dtype=dtype, copy=copy)

    def to_xarray(self) -> Any:
        """An ``xr.DataArray`` with the same data and dims; labels become coordinates.

        Raises:
            ImportError: xarray is not installed (``pipekit-array[xarray]``).
        """
        try:
            import xarray as xr
        except ImportError as exc:
            raise ImportError(_XARRAY_HINT) from exc
        coords = {dim: (dim, list(values)) for dim, values in self.labels.items()}
        return xr.DataArray(self.data, dims=self.dims, coords=coords)

    def __repr__(self) -> str:
        labelled = f", labels={sorted(self.labels)}" if self.labels else ""
        return f"DimArray[{' '.join(self.dims)}]{self.shape}{labelled}"

dtype property

The data's dtype.

shape property

The data's shape.

__array__(dtype=None, copy=None)

The data as a NumPy array, so np.asarray(d) works (dims are dropped).

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
def __array__(self, dtype: Any = None, copy: bool | None = None) -> Any:
    """The data as a NumPy array, so ``np.asarray(d)`` works (dims are dropped)."""
    import numpy as np

    if backend_name(self.data) == "jax":
        report_move("DimArray.__array__", "JAX data copied to the host")
    if copy is None:
        return np.asarray(self.data, dtype=dtype)
    # copy=True always copies; copy=False raises if a copy is needed
    return np.array(self.data, dtype=dtype, copy=copy)

to_xarray()

An xr.DataArray with the same data and dims; labels become coordinates.

Raises:

Type Description
ImportError

xarray is not installed (pipekit-array[xarray]).

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
def to_xarray(self) -> Any:
    """An ``xr.DataArray`` with the same data and dims; labels become coordinates.

    Raises:
        ImportError: xarray is not installed (``pipekit-array[xarray]``).
    """
    try:
        import xarray as xr
    except ImportError as exc:
        raise ImportError(_XARRAY_HINT) from exc
    coords = {dim: (dim, list(values)) for dim, values in self.labels.items()}
    return xr.DataArray(self.data, dims=self.dims, coords=coords)

DimArrayAdapter

The reference pipekit_ops.NamedArrayAdapter, for DimArray.

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
class DimArrayAdapter:
    """The reference `pipekit_ops.NamedArrayAdapter`, for `DimArray`."""

    def dims(self, x: DimArray) -> tuple[str, ...]:
        """``x.dims``."""
        return x.dims

    def data(self, x: DimArray) -> Any:
        """``x.data``."""
        return x.data

    def labels(self, x: DimArray, dim: str) -> tuple[Any, ...] | None:
        """The labels of ``dim``, or None."""
        return x.labels.get(dim)

    def isel(self, x: DimArray, indexers: dict[str, Any]) -> DimArray:
        """Select by position; integers drop their dim, labels follow the data.

        Raises:
            KeyError: An indexer names a dim ``x`` does not have.
        """
        unknown = sorted(set(indexers) - set(x.dims))
        if unknown:
            raise KeyError(f"isel: unknown dim(s) {unknown}; dims are {x.dims}.")
        sizes = dict(zip(x.dims, x.data.shape, strict=True))
        normalized = {
            dim: _positions(index, dim, sizes[dim]) for dim, index in indexers.items()
        }
        # ints and slices at once; each position array on its own axis, so
        # two arrays select independently instead of pairing up (outer indexing)
        basic = tuple(
            index if isinstance(index, slice) or _is_scalar(index) else slice(None)
            for index in (normalized.get(dim, slice(None)) for dim in x.dims)
        )
        data = x.data[basic]
        kept_dims = [
            dim
            for dim, index in zip(x.dims, basic, strict=True)
            if not _is_scalar(index)
        ]
        for dim, index in normalized.items():
            if isinstance(index, slice) or _is_scalar(index):
                continue
            axis = kept_dims.index(dim)
            data = data[(slice(None),) * axis + (index,)]
        key = tuple(normalized.get(dim, slice(None)) for dim in x.dims)
        dims: list[str] = []
        labels: dict[str, tuple[Any, ...]] = {}
        for dim, index in zip(x.dims, key, strict=True):
            if _is_scalar(index):
                continue
            dims.append(dim)
            if dim in x.labels:
                kept = _index_labels(x.labels[dim], index)
                if kept is not None:
                    labels[dim] = kept
        return _build(data, tuple(dims), labels)

    def rewrap(self, x: DimArray, data: Any, dims: tuple[str, ...]) -> DimArray:
        """A DimArray around new data; a kept dim keeps its labels if its size did.

        Raises:
            ValueError: ``dims`` does not match ``data``'s rank or repeats.
        """
        labels: dict[str, tuple[Any, ...]] = {}
        shape = tuple(data.shape)
        if len(shape) == len(dims):
            for dim, size in zip(dims, shape, strict=True):
                values = x.labels.get(dim)
                if values is not None and len(values) == size:
                    labels[dim] = values
        return DimArray(data, tuple(dims), labels)

    def relabel(
        self, x: DimArray, labels: Mapping[str, tuple[Any, ...] | None]
    ) -> DimArray:
        """``x`` with the labels of the named dims set; None removes them.

        The optional `pipekit_ops.LabelledAdapter` method (ADR A22).

        Raises:
            ValueError: A dim is not in ``x.dims``, or a label tuple's
                length differs from its dim's size.
        """
        unknown = sorted(set(labels) - set(x.dims))
        if unknown:
            raise ValueError(f"relabel: unknown dim(s) {unknown}; dims are {x.dims}.")
        merged = dict(x.labels)
        for dim, values in labels.items():
            if values is None:
                merged.pop(dim, None)
            else:
                merged[dim] = tuple(values)
        # the constructor checks each tuple against its dim's size
        return DimArray(x.data, x.dims, merged)

    def check_compatible(self, *xs: DimArray) -> tuple[DimArray, ...]:
        """The arrays back, if every shared dim has one size and one set of labels.

        Raises:
            ValueError: A dim has two sizes, or two different label sets.
        """
        sizes: dict[str, int] = {}
        labels: dict[str, tuple[Any, ...]] = {}
        for i, x in enumerate(xs):
            for dim, size in zip(x.dims, x.shape, strict=True):
                if sizes.setdefault(dim, size) != size:
                    raise ValueError(
                        f"input {i}: dim {dim!r} has size {size}, not {sizes[dim]}."
                    )
                values = x.labels.get(dim)
                if values is None:
                    continue
                if labels.setdefault(dim, values) != values:
                    raise ValueError(
                        f"input {i}: dim {dim!r} has labels {values}, "
                        f"not {labels[dim]}."
                    )
        return xs

    def signature(self, x: DimArray) -> Signature:
        """Dims with their sizes, and the dtype's name."""
        dims = tuple(zip(x.dims, x.shape, strict=True))
        return Signature(dims=dims, dtype=str(x.dtype))

check_compatible(*xs)

The arrays back, if every shared dim has one size and one set of labels.

Raises:

Type Description
ValueError

A dim has two sizes, or two different label sets.

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
def check_compatible(self, *xs: DimArray) -> tuple[DimArray, ...]:
    """The arrays back, if every shared dim has one size and one set of labels.

    Raises:
        ValueError: A dim has two sizes, or two different label sets.
    """
    sizes: dict[str, int] = {}
    labels: dict[str, tuple[Any, ...]] = {}
    for i, x in enumerate(xs):
        for dim, size in zip(x.dims, x.shape, strict=True):
            if sizes.setdefault(dim, size) != size:
                raise ValueError(
                    f"input {i}: dim {dim!r} has size {size}, not {sizes[dim]}."
                )
            values = x.labels.get(dim)
            if values is None:
                continue
            if labels.setdefault(dim, values) != values:
                raise ValueError(
                    f"input {i}: dim {dim!r} has labels {values}, "
                    f"not {labels[dim]}."
                )
    return xs

data(x)

x.data.

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
def data(self, x: DimArray) -> Any:
    """``x.data``."""
    return x.data

dims(x)

x.dims.

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
def dims(self, x: DimArray) -> tuple[str, ...]:
    """``x.dims``."""
    return x.dims

isel(x, indexers)

Select by position; integers drop their dim, labels follow the data.

Raises:

Type Description
KeyError

An indexer names a dim x does not have.

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
def isel(self, x: DimArray, indexers: dict[str, Any]) -> DimArray:
    """Select by position; integers drop their dim, labels follow the data.

    Raises:
        KeyError: An indexer names a dim ``x`` does not have.
    """
    unknown = sorted(set(indexers) - set(x.dims))
    if unknown:
        raise KeyError(f"isel: unknown dim(s) {unknown}; dims are {x.dims}.")
    sizes = dict(zip(x.dims, x.data.shape, strict=True))
    normalized = {
        dim: _positions(index, dim, sizes[dim]) for dim, index in indexers.items()
    }
    # ints and slices at once; each position array on its own axis, so
    # two arrays select independently instead of pairing up (outer indexing)
    basic = tuple(
        index if isinstance(index, slice) or _is_scalar(index) else slice(None)
        for index in (normalized.get(dim, slice(None)) for dim in x.dims)
    )
    data = x.data[basic]
    kept_dims = [
        dim
        for dim, index in zip(x.dims, basic, strict=True)
        if not _is_scalar(index)
    ]
    for dim, index in normalized.items():
        if isinstance(index, slice) or _is_scalar(index):
            continue
        axis = kept_dims.index(dim)
        data = data[(slice(None),) * axis + (index,)]
    key = tuple(normalized.get(dim, slice(None)) for dim in x.dims)
    dims: list[str] = []
    labels: dict[str, tuple[Any, ...]] = {}
    for dim, index in zip(x.dims, key, strict=True):
        if _is_scalar(index):
            continue
        dims.append(dim)
        if dim in x.labels:
            kept = _index_labels(x.labels[dim], index)
            if kept is not None:
                labels[dim] = kept
    return _build(data, tuple(dims), labels)

labels(x, dim)

The labels of dim, or None.

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
def labels(self, x: DimArray, dim: str) -> tuple[Any, ...] | None:
    """The labels of ``dim``, or None."""
    return x.labels.get(dim)

relabel(x, labels)

x with the labels of the named dims set; None removes them.

The optional pipekit_ops.LabelledAdapter method (ADR A22).

Raises:

Type Description
ValueError

A dim is not in x.dims, or a label tuple's length differs from its dim's size.

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
def relabel(
    self, x: DimArray, labels: Mapping[str, tuple[Any, ...] | None]
) -> DimArray:
    """``x`` with the labels of the named dims set; None removes them.

    The optional `pipekit_ops.LabelledAdapter` method (ADR A22).

    Raises:
        ValueError: A dim is not in ``x.dims``, or a label tuple's
            length differs from its dim's size.
    """
    unknown = sorted(set(labels) - set(x.dims))
    if unknown:
        raise ValueError(f"relabel: unknown dim(s) {unknown}; dims are {x.dims}.")
    merged = dict(x.labels)
    for dim, values in labels.items():
        if values is None:
            merged.pop(dim, None)
        else:
            merged[dim] = tuple(values)
    # the constructor checks each tuple against its dim's size
    return DimArray(x.data, x.dims, merged)

rewrap(x, data, dims)

A DimArray around new data; a kept dim keeps its labels if its size did.

Raises:

Type Description
ValueError

dims does not match data's rank or repeats.

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
def rewrap(self, x: DimArray, data: Any, dims: tuple[str, ...]) -> DimArray:
    """A DimArray around new data; a kept dim keeps its labels if its size did.

    Raises:
        ValueError: ``dims`` does not match ``data``'s rank or repeats.
    """
    labels: dict[str, tuple[Any, ...]] = {}
    shape = tuple(data.shape)
    if len(shape) == len(dims):
        for dim, size in zip(dims, shape, strict=True):
            values = x.labels.get(dim)
            if values is not None and len(values) == size:
                labels[dim] = values
    return DimArray(data, tuple(dims), labels)

signature(x)

Dims with their sizes, and the dtype's name.

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
def signature(self, x: DimArray) -> Signature:
    """Dims with their sizes, and the dtype's name."""
    dims = tuple(zip(x.dims, x.shape, strict=True))
    return Signature(dims=dims, dtype=str(x.dtype))

Padded

Bases: NamedTuple

A batch: the items as one carrier and a mask of the real ones.

A NamedTuple, so JAX treats it as a pytree: a jitted step taking a Padded does not retrace when the mask changes. A full batch is a Padded whose valid is all True.

Attributes:

Name Type Description
value Any

The items stacked along the batch dim, first (stack_items): (n, *item_dims).

valid Any

(n,) bool, True for real items, in the value's array namespace.

Example

import numpy as np from pipekit_array import DimArray, reduce items = [DimArray(np.full(3, float(i)), ("x",)) for i in range(3)] padded = next(batch(items, size=4, dim="sample")) padded.valid.tolist() [True, True, True, False] reduce(padded.value, "mean", "sample", where=padded.valid).data array([1., 1., 1.])

Source code in packages/pipekit-array/src/pipekit_array/named/_batches.py
class Padded(NamedTuple):
    """A batch: the items as one carrier and a mask of the real ones.

    A `NamedTuple`, so JAX treats it as a pytree: a jitted step taking a
    `Padded` does not retrace when the mask changes. A full batch is a
    `Padded` whose ``valid`` is all True.

    Attributes:
        value: The items stacked along the batch dim, first
            (`stack_items`): ``(n, *item_dims)``.
        valid: ``(n,)`` bool, True for real items, in the value's array
            namespace.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray, reduce
        >>> items = [DimArray(np.full(3, float(i)), ("x",)) for i in range(3)]
        >>> padded = next(batch(items, size=4, dim="sample"))
        >>> padded.valid.tolist()
        [True, True, True, False]
        >>> reduce(padded.value, "mean", "sample", where=padded.valid).data
        array([1., 1., 1.])
    """

    value: Any
    valid: Any

RowLayout dataclass

How to_rows flattened a carrier, so from_rows can rebuild one.

Parameters:

Name Type Description Default
dims tuple[str, ...]

The carrier's dims, in its order.

required
sizes tuple[int, ...]

The size of each dim, in the same order.

required
sample_dims tuple[str, ...]

The dims that became rows, in the carrier's order.

required
feature_dims tuple[str, ...]

The dims that became columns, in the carrier's order.

required
template Any

The carrier to rewrap like (left out of equality and repr).

None
Source code in packages/pipekit-array/src/pipekit_array/named/_rows.py
@dataclasses.dataclass(frozen=True)
class RowLayout:
    """How `to_rows` flattened a carrier, so `from_rows` can rebuild one.

    Args:
        dims: The carrier's dims, in its order.
        sizes: The size of each dim, in the same order.
        sample_dims: The dims that became rows, in the carrier's order.
        feature_dims: The dims that became columns, in the carrier's order.
        template: The carrier to rewrap like (left out of equality and repr).
    """

    dims: tuple[str, ...]
    sizes: tuple[int, ...]
    sample_dims: tuple[str, ...]
    feature_dims: tuple[str, ...]
    template: Any = dataclasses.field(default=None, compare=False, repr=False)

    @property
    def n_samples(self) -> int:
        """The number of rows: the product of the sample dims' sizes."""
        return math.prod(self._size(d) for d in self.sample_dims)

    @property
    def n_features(self) -> int:
        """The number of columns: the product of the feature dims' sizes."""
        return math.prod(self._size(d) for d in self.feature_dims)

    def _size(self, dim: str) -> int:
        return self.sizes[self.dims.index(dim)]

n_features property

The number of columns: the product of the feature dims' sizes.

n_samples property

The number of rows: the product of the sample dims' sizes.

argreduce(x, fn, dim)

The index of a reduction along one dim ("argmax", "argmin").

Example

import numpy as np from pipekit_array import DimArray ndvi = DimArray(np.random.default_rng(0).random((12, 4)), ("time", "x")) argreduce(ndvi, "argmax", "time").dims ('x',)

Source code in packages/pipekit-array/src/pipekit_array/named/_reduce.py
def argreduce(x: Any, fn: Reduction, dim: str) -> Any:
    """The index of a reduction along one dim (``"argmax"``, ``"argmin"``).

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> ndvi = DimArray(np.random.default_rng(0).random((12, 4)), ("time", "x"))
        >>> argreduce(ndvi, "argmax", "time").dims
        ('x',)
    """
    adapter = adapter_of(x, "argreduce")
    fast = fast_path(adapter, "argreduce")
    if fast is not None:
        return fast(x, fn, dim)
    dims = adapter.dims(x)
    (axis,) = axes_of(dims, (dim,), "argreduce")
    data = adapter.data(x)
    func = resolve_function(fn, namespace_of(data), "argreduce")
    out = func(data, axis=axis)
    kept = tuple(d for d in dims if d != dim)
    return adapter.rewrap(x, out, kept)

array_namespace(*xs)

Return the Array API namespace shared by all input arrays.

NumPy arrays next to JAX ones join them, so the namespace is JAX's.

Parameters:

Name Type Description Default
*xs Any

One or more array-like objects. All must share the same Array API backend, but for NumPy with JAX.

()

Returns:

Type Description
Any

The module-like namespace object (e.g. numpy, jax.numpy,

Any

array_api_compat.torch).

Raises:

Type Description
TypeError

If no input implements the Array API, or if the inputs span more than one backend. The error message is consistent whether or not array-api-compat is installed.

Source code in packages/pipekit-array/src/pipekit_array/named/_dispatch.py
def array_namespace(*xs: Any) -> Any:
    """Return the Array API namespace shared by all input arrays.

    NumPy arrays next to JAX ones join them, so the namespace is JAX's.

    Args:
        *xs: One or more array-like objects. All must share the same
            Array API backend, but for NumPy with JAX.

    Returns:
        The module-like namespace object (e.g. `numpy`, `jax.numpy`,
        `array_api_compat.torch`).

    Raises:
        TypeError: If no input implements the Array API, or if the
            inputs span more than one backend. The error message is
            consistent whether or not `array-api-compat` is installed.
    """
    if len(xs) > 1:
        # ADR A24: NumPy joins JAX, whatever the order
        own = [x for x in xs if not type(x).__module__.startswith("numpy")]
        if own and all(backend_name(x) == "jax" for x in own):
            xs = tuple(own)
    compat_ns = _compat_namespace()

    if compat_ns is not None:
        try:
            return compat_ns(*xs)
        except TypeError as exc:
            # array-api-compat's error messages differ from our
            # contract; re-raise with stable wording so tests and
            # users see a consistent shape.
            msg = str(exc)
            if "Multiple namespaces" in msg:
                names = sorted({type(x).__module__.split(".")[0] for x in xs})
                raise TypeError(_multi_backend_msg(names)) from exc
            raise TypeError(_NO_BACKEND_MSG) from exc

    namespaces: set[Any] = set()
    for x in xs:
        ns = getattr(x, "__array_namespace__", None)
        if ns is None:
            raise TypeError(
                f"Input of type {type(x).__name__!r} does not implement "
                f"the Array API (`__array_namespace__` missing). " + _NO_BACKEND_MSG
            )
        namespaces.add(ns())
    if len(namespaces) == 0:
        raise TypeError(_NO_BACKEND_MSG)
    if len(namespaces) > 1:
        names = sorted({ns.__name__ for ns in namespaces})
        raise TypeError(_multi_backend_msg(names))
    return next(iter(namespaces))

batch(items, *, size, dim, pad='last')

Group named carriers into Padded batches of size along dim.

Parameters:

Name Type Description Default
items Iterable[Any]

Named carriers with the same dims (each registered with pipekit's adapter registry), consumed lazily.

required
size int

Items per batch; must be positive.

required
dim str

The new leading batch dim ("sample", "patch", "member"); must not be a dim of the items.

required
pad PadPolicy

What to do with a short last batch:

  • "last" (default): pad it to size with copies of its last real item, valid False for the copies, so every batch has one shape;
  • "none": yield it short, all valid;
  • "drop": drop it (drop_remainder in tf.data / Grain).
'last'

Yields:

Type Description
Padded

One Padded per size items, plus the last short batch per

Padded

pad. Each value is stack_items of its items.

Raises:

Type Description
ValueError

size is not positive, pad is not a policy, or the items' dims differ (from stack_items).

Example

import numpy as np from pipekit_array import DimArray items = [DimArray(np.ones((2, 2)), ("y", "x")) for _ in range(5)] def sizes(pad): ... return [len(b.valid) for b in batch(items, size=2, dim="s", pad=pad)] sizes("last"), sizes("none"), sizes("drop") ([2, 2, 2], [2, 2, 1], [2, 2])

Source code in packages/pipekit-array/src/pipekit_array/named/_batches.py
def batch(
    items: Iterable[Any], *, size: int, dim: str, pad: PadPolicy = "last"
) -> Iterator[Padded]:
    """Group named carriers into `Padded` batches of ``size`` along ``dim``.

    Args:
        items: Named carriers with the same dims (each registered with
            pipekit's adapter registry), consumed lazily.
        size: Items per batch; must be positive.
        dim: The new leading batch dim (``"sample"``, ``"patch"``,
            ``"member"``); must not be a dim of the items.
        pad: What to do with a short last batch:

            - ``"last"`` (default): pad it to ``size`` with copies of its
              last real item, ``valid`` False for the copies, so every
              batch has one shape;
            - ``"none"``: yield it short, all valid;
            - ``"drop"``: drop it (``drop_remainder`` in tf.data / Grain).

    Yields:
        One `Padded` per ``size`` items, plus the last short batch per
        ``pad``. Each ``value`` is `stack_items` of its items.

    Raises:
        ValueError: ``size`` is not positive, ``pad`` is not a policy, or
            the items' dims differ (from `stack_items`).

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> items = [DimArray(np.ones((2, 2)), ("y", "x")) for _ in range(5)]
        >>> def sizes(pad):
        ...     return [len(b.valid) for b in batch(items, size=2, dim="s", pad=pad)]
        >>> sizes("last"), sizes("none"), sizes("drop")
        ([2, 2, 2], [2, 2, 1], [2, 2])
    """
    if size <= 0:
        raise ValueError(f"batch: size must be positive, got {size}.")
    if pad not in PAD_POLICIES:
        raise ValueError(f"batch: pad must be one of {PAD_POLICIES}, got {pad!r}.")
    group: list[Any] = []
    for item in items:
        group.append(item)
        if len(group) == size:
            yield _padded(group, dim, 0)
            group = []
    if group and pad != "drop":
        yield _padded(group, dim, size - len(group) if pad == "last" else 0)

broadcast_like(x, other, /, *, exclude=())

Add other's missing dims to x, in front, in other's order.

y_{a…, d…} = x_{d…}, for every index a of the added dims.

Shared dims must agree exactly in size and labels (xarray outer-joins instead). x's own dims keep their order, so the result's order is xarray's only when the shared dims are in other's order. The added dims carry other's labels.

Parameters:

Name Type Description Default
x Any

A named carrier.

required
other Any

A carrier of the same family; only its dims, sizes and labels are read.

required
exclude Sequence[str]

Dims of other neither added nor checked.

()

Raises:

Type Description
TypeError

The adapter has no relabel and an added dim has labels.

ValueError

A shared dim differs in size or labels.

Example

import numpy as np from pipekit_array import DimArray cube = DimArray(np.zeros((12, 2, 4)), ("time", "band", "x")) median = DimArray(np.ones((2, 4)), ("band", "x")) broadcast_like(median, cube).shape (12, 2, 4)

Source code in packages/pipekit-array/src/pipekit_array/named/_shape.py
def broadcast_like(x: Any, other: Any, /, *, exclude: Sequence[str] = ()) -> Any:
    """Add ``other``'s missing dims to ``x``, in front, in ``other``'s order.

    y_{a…, d…} = x_{d…}, for every index a of the added dims.

    Shared dims must agree exactly in size and labels (xarray outer-joins
    instead). ``x``'s own dims keep their order, so the result's order is
    xarray's only when the shared dims are in ``other``'s order. The added
    dims carry ``other``'s labels.

    Args:
        x: A named carrier.
        other: A carrier of the same family; only its dims, sizes and
            labels are read.
        exclude: Dims of ``other`` neither added nor checked.

    Raises:
        TypeError: The adapter has no ``relabel`` and an added dim has
            labels.
        ValueError: A shared dim differs in size or labels.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> cube = DimArray(np.zeros((12, 2, 4)), ("time", "band", "x"))
        >>> median = DimArray(np.ones((2, 4)), ("band", "x"))
        >>> broadcast_like(median, cube).shape
        (12, 2, 4)
    """
    adapter = adapter_of(x, "broadcast_like")
    other_adapter = adapter_of(other, "broadcast_like")
    excluded = tuple(exclude)
    fast = fast_path(adapter, "broadcast_like")
    if fast is not None:
        return fast(x, other, exclude=excluded)
    dims = adapter.dims(x)
    other_dims = other_adapter.dims(other)
    shared = [d for d in dims if d in other_dims and d not in excluded]
    added = tuple(d for d in other_dims if d not in dims and d not in excluded)
    # only the shared dims must combine: an empty slice of every other dim
    # checks them without touching the data
    x_part = adapter.isel(x, {d: slice(0, 0) for d in dims if d not in shared})
    other_part = other_adapter.isel(
        other, {d: slice(0, 0) for d in other_dims if d not in shared}
    )
    adapter.check_compatible(x_part, other_part)
    data = adapter.data(x)
    xp = namespace_of(data)
    sizes = dict(zip(other_dims, other_adapter.data(other).shape, strict=True))
    expanded = data
    for _ in added:
        expanded = xp.expand_dims(expanded, axis=0)
    target = (*(sizes[d] for d in added), *data.shape)
    broadcast = xp.broadcast_to(expanded, target)
    out = adapter.rewrap(x, broadcast, (*added, *dims))
    labels = {d: other_adapter.labels(other, d) for d in added}
    return set_labels(adapter, out, labels, "broadcast_like")

coarsen(x, fn, /, *, boundary='exact', **windows)

Reduce non-overlapping blocks of k entries along each named dim.

z_{…, i', …} = fn_{0 ≤ r < k} x_{…, k·i' + r, …}, i' < ⌊N/k⌋.

A composition: split_dims each dim into (dim, block) then reduce over the blocks, so coarsen(x, "mean", time=3) is reduce(split_dims(x, time={"time": 4, "window": 3}), "mean", "window"). A coarsened dim loses its labels: xarray's coord_func reads coordinate values, which this layer never does (A15).

Parameters:

Name Type Description Default
x Any

A named carrier.

required
fn Reduction

An array-namespace name ("mean", "nanmean", "max") or a function taking axis=.

required
boundary str

"exact" requires k to divide N; "trim" drops the last N mod k entries.

'exact'
**windows int

dim=k for each dim to coarsen, with k ≥ 1.

{}

Raises:

Type Description
KeyError

A dim is not a dim of x.

ValueError

No dim is given, k is not positive, boundary is not one of BOUNDARIES, or under "exact" k does not divide N.

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> ndvi = DimArray(np.arange(24.0).reshape(2, 12), ("y", "x"))
>>> coarse = coarsen(ndvi, "mean", x=4)
>>> coarse.dims, coarse.data.tolist()
(('y', 'x'), [[1.5, 5.5, 9.5], [13.5, 17.5, 21.5]])
>>> coarsen(ndvi, "max", boundary="trim", x=5, y=2).data.tolist()
[[16.0, 21.0]]
>>> coarsen(ndvi, "mean", x=5)
Traceback (most recent call last):
...
ValueError: coarsen: 5 does not divide 'x' of size 12; ...
Source code in packages/pipekit-array/src/pipekit_array/named/_window.py
def coarsen(
    x: Any, fn: Reduction, /, *, boundary: str = "exact", **windows: int
) -> Any:
    """Reduce non-overlapping blocks of k entries along each named dim.

    z_{…, i', …} = fn_{0 ≤ r < k} x_{…, k·i' + r, …},  i' < ⌊N/k⌋.

    A composition: `split_dims` each dim into ``(dim, block)`` then
    `reduce` over the blocks, so ``coarsen(x, "mean", time=3)`` is
    ``reduce(split_dims(x, time={"time": 4, "window": 3}), "mean",
    "window")``. A coarsened dim loses its labels: xarray's ``coord_func``
    reads coordinate values, which this layer never does (A15).

    Args:
        x: A named carrier.
        fn: An array-namespace name (``"mean"``, ``"nanmean"``, ``"max"``)
            or a function taking ``axis=``.
        boundary: ``"exact"`` requires k to divide N; ``"trim"`` drops
            the last N mod k entries.
        **windows: ``dim=k`` for each dim to coarsen, with k ≥ 1.

    Raises:
        KeyError: A dim is not a dim of ``x``.
        ValueError: No dim is given, k is not positive, ``boundary`` is
            not one of `BOUNDARIES`, or under ``"exact"`` k does not
            divide N.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> ndvi = DimArray(np.arange(24.0).reshape(2, 12), ("y", "x"))
        >>> coarse = coarsen(ndvi, "mean", x=4)
        >>> coarse.dims, coarse.data.tolist()
        (('y', 'x'), [[1.5, 5.5, 9.5], [13.5, 17.5, 21.5]])
        >>> coarsen(ndvi, "max", boundary="trim", x=5, y=2).data.tolist()
        [[16.0, 21.0]]
        >>> coarsen(ndvi, "mean", x=5)  # doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
        ...
        ValueError: coarsen: 5 does not divide 'x' of size 12; ...
    """
    adapter = adapter_of(x, "coarsen")
    if not windows:
        raise ValueError("coarsen: give at least one dim=k.")
    if boundary not in BOUNDARIES:
        raise ValueError(
            f"coarsen: boundary must be one of {BOUNDARIES}, got {boundary!r}."
        )
    dims = adapter.dims(x)
    axes = axes_of(dims, list(windows), "coarsen")
    data = adapter.data(x)
    lengths = {dim: data.shape[axis] for dim, axis in zip(windows, axes, strict=True)}
    for dim, block in windows.items():
        if block < 1:
            raise ValueError(f"coarsen: {dim}={block} must be positive.")
        if boundary == "exact" and lengths[dim] % block != 0:
            raise ValueError(
                f"coarsen: {block} does not divide {dim!r} of size {lengths[dim]}; "
                "pass boundary='trim' or pad first."
            )
    fast = fast_path(adapter, "coarsen")
    if fast is not None:
        return fast(x, fn, dict(windows), boundary=boundary)
    # trim: keep the first k·⌊N/k⌋ entries
    tails = {
        dim: slice(0, lengths[dim] - lengths[dim] % block)
        for dim, block in windows.items()
        if lengths[dim] % block != 0
    }
    trimmed = adapter.isel(x, tails) if tails else x
    blocks: dict[str, str] = {}
    for dim in windows:
        blocks[dim] = _block_dim(dim, (*dims, *blocks.values()))
    # x_{…, i', r, …} = x_{…, k·i' + r, …}
    parts = {
        dim: {dim: lengths[dim] // block, blocks[dim]: block}
        for dim, block in windows.items()
    }
    split = split_dims(trimmed, **parts)
    # z_{…, i', …} = fn over r of x_{…, i', r, …}
    return _reduce.reduce(split, fn, tuple(blocks.values()))

combine(fn, *xs)

fn of the data of carriers with the same dims; never aligns.

Inputs are put in the first one's dim order by name before fn sees them, and the result has those dims. NumPy inputs join JAX ones; fn then decides what it returns (np.add of JAX arrays is NumPy's).

Example

import numpy as np from pipekit_array import DimArray nir = DimArray(np.full((2, 3), 0.4), ("y", "x")) red = DimArray(np.full((2, 3), 0.1), ("y", "x")) nd = combine(lambda a, b: (a - b) / (a + b), nir, red) nd.dims, bool(np.allclose(nd.data, 0.6)) (('y', 'x'), True)

Source code in packages/pipekit-array/src/pipekit_array/named/_join.py
def combine(fn: Callable[..., Any], *xs: Any) -> Any:
    """``fn`` of the data of carriers with the same dims; never aligns.

    Inputs are put in the first one's dim order by name before ``fn`` sees
    them, and the result has those dims. NumPy inputs join JAX ones; ``fn``
    then decides what it returns (``np.add`` of JAX arrays is NumPy's).

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> nir = DimArray(np.full((2, 3), 0.4), ("y", "x"))
        >>> red = DimArray(np.full((2, 3), 0.1), ("y", "x"))
        >>> nd = combine(lambda a, b: (a - b) / (a + b), nir, red)
        >>> nd.dims, bool(np.allclose(nd.data, 0.6))
        (('y', 'x'), True)
    """
    if len(xs) == 0:
        raise ValueError("combine needs at least one carrier.")
    adapter = adapter_of(xs[0], "combine")
    fast = fast_path(adapter, "combine")
    if fast is not None:
        return fast(fn, *xs)
    checked = adapter.check_compatible(*xs)
    joined = join_backends([adapter.data(x) for x in checked], "combine")
    dims = adapter.dims(checked[0])
    datas = []
    for x, data in zip(checked, joined, strict=True):
        other = adapter.dims(x)
        if sorted(other) != sorted(dims):
            raise ValueError(f"combine: dims {other} differ from {dims}.")
        if other != dims:
            data = namespace_of(data).permute_dims(
                data, axes_of(other, dims, "combine")
            )
        datas.append(data)
    return adapter.rewrap(checked[0], fn(*datas), dims)

concat(xs, dim)

Join carriers along an existing dim; the other dims must agree.

The generic path drops dim's labels (the joined dim has a new size); an adapter's concat fast path may join them.

Source code in packages/pipekit-array/src/pipekit_array/named/_join.py
def concat(xs: Sequence[Any], dim: str) -> Any:
    """Join carriers along an existing dim; the other dims must agree.

    The generic path drops ``dim``'s labels (the joined dim has a new size);
    an adapter's ``concat`` fast path may join them.
    """
    if len(xs) == 0:
        raise ValueError("concat needs at least one carrier.")
    adapter = adapter_of(xs[0], "concat")
    fast = fast_path(adapter, "concat")
    if fast is not None:
        return fast(list(xs), dim)
    dims = _same_dims(adapter, xs, "concat")
    (axis,) = axes_of(dims, (dim,), "concat")
    # the other dims must combine; dim itself may differ in size
    # the other dims must combine; an empty slice of dim checks them even
    # when an input has no entries along dim
    others = [adapter.isel(x, {dim: slice(0, 0)}) for x in xs]
    adapter.check_compatible(*others)
    datas = join_backends([adapter.data(x) for x in xs], "concat")
    xp = namespace_of(datas[0])
    return adapter.rewrap(xs[0], xp.concat(datas, axis=axis), dims)

core_dims(dims)

dims without the leading ones that vmapped_dims would add back.

A stack of carriers (from vmap, scan or jnp.stack) and one of them then have the same tree structure, as a stack of arrays and one array have: a solver that allocates its output and writes one step into it compares the two. Leading dims called "batch", "batch_1", … are taken to be added ones, also when the caller named them so.

Example

core_dims(("batch", "y", "x")), core_dims(("time", "y", "x")) (('y', 'x'), ('time', 'y', 'x'))

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
def core_dims(dims: tuple[str, ...]) -> tuple[str, ...]:
    """``dims`` without the leading ones that `vmapped_dims` would add back.

    A stack of carriers (from ``vmap``, ``scan`` or ``jnp.stack``) and one of
    them then have the same tree structure, as a stack of arrays and one array
    have: a solver that allocates its output and writes one step into it
    compares the two. Leading dims called ``"batch"``, ``"batch_1"``, … are
    taken to be added ones, also when the caller named them so.

    Example:
        >>> core_dims(("batch", "y", "x")), core_dims(("time", "y", "x"))
        (('y', 'x'), ('time', 'y', 'x'))
    """
    k = 0
    while k < len(dims) and dims[: k + 1] == _batch_names(k + 1, dims[k + 1 :]):
        k += 1
    return dims[k:]

cumreduce(x, fn, dim)

A cumulative function along dim; labels and length unchanged.

c_t = ∑_{τ ≤ t} x_τ for "cumulative_sum".

No implicit skipna (xarray's cumsum skips NaN by default): ask for "nancumsum".

Parameters:

Name Type Description Default
x Any

A named carrier.

required
fn Reduction

An array-namespace name ("cumulative_sum", "cumulative_prod", "nancumsum") or a function taking axis= that keeps the length.

required
dim str

The dim to accumulate along.

required

Raises:

Type Description
KeyError

dim is not a dim of x.

ValueError

The namespace has no fn, or fn changed the length of dim.

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> change = DimArray(np.array([1.0, 2.0, 3.0]), ("time",))
>>> cumreduce(change, "cumulative_sum", "time").data
array([1., 3., 6.])
>>> gaps = DimArray(np.array([1.0, np.nan, 3.0]), ("time",))
>>> cumreduce(gaps, "nancumsum", "time").data
array([1., 1., 4.])
Source code in packages/pipekit-array/src/pipekit_array/named/_reduce.py
def cumreduce(x: Any, fn: Reduction, dim: str) -> Any:
    """A cumulative function along ``dim``; labels and length unchanged.

    c_t = ∑_{τ ≤ t} x_τ for ``"cumulative_sum"``.

    No implicit ``skipna`` (xarray's ``cumsum`` skips NaN by default): ask
    for ``"nancumsum"``.

    Args:
        x: A named carrier.
        fn: An array-namespace name (``"cumulative_sum"``,
            ``"cumulative_prod"``, ``"nancumsum"``) or a function taking
            ``axis=`` that keeps the length.
        dim: The dim to accumulate along.

    Raises:
        KeyError: ``dim`` is not a dim of ``x``.
        ValueError: The namespace has no ``fn``, or ``fn`` changed the
            length of ``dim``.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> change = DimArray(np.array([1.0, 2.0, 3.0]), ("time",))
        >>> cumreduce(change, "cumulative_sum", "time").data
        array([1., 3., 6.])
        >>> gaps = DimArray(np.array([1.0, np.nan, 3.0]), ("time",))
        >>> cumreduce(gaps, "nancumsum", "time").data
        array([1., 1., 4.])
    """
    adapter = adapter_of(x, "cumreduce")
    dims = adapter.dims(x)
    (axis,) = axes_of(dims, (dim,), "cumreduce")
    fast = fast_path(adapter, "cumreduce")
    if fast is not None:
        return fast(x, fn, dim)
    data = adapter.data(x)
    func = resolve_function(fn, namespace_of(data), "cumreduce")
    # c_t = ∑_{τ ≤ t} x_τ (or fn's own accumulation)
    out = func(data, axis=axis)
    if tuple(out.shape) != tuple(data.shape):
        raise ValueError(
            f"cumreduce: fn changed the shape {tuple(data.shape)} to "
            f"{tuple(out.shape)}; a cumulative function keeps the length of {dim!r}."
        )
    return adapter.rewrap(x, out, dims)

diff(x, dim, /, n=1, *, label='upper')

The n-th forward difference along dim (xarray's diff).

Δy_t = y_{t+1} - y_t, applied n times: N entries become N - n.

The result's labels on dim are the minuend's ("upper", positions n … N - 1) or the subtrahend's ("lower", positions 0 … N - n - 1).

Parameters:

Name Type Description Default
x Any

A named carrier.

required
dim str

The dim to difference along.

required
n int

The order, n ≥ 0; static under jit.

1
label str

"upper" or "lower".

'upper'

Raises:

Type Description
KeyError

dim is not a dim of x.

ValueError

n is negative, or label is not "upper" or "lower".

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> series = DimArray(np.array([1.0, 4.0, 9.0, 16.0]), ("time",))
>>> diff(series, "time").data
array([3., 5., 7.])
>>> labelled = DimArray(series.data, ("time",), {"time": tuple("abcd")})
>>> second = diff(labelled, "time", 2, label="lower")
>>> second.data, second.labels["time"]
(array([2., 2.]), ('a', 'b'))
Source code in packages/pipekit-array/src/pipekit_array/named/_shift.py
def diff(x: Any, dim: str, /, n: int = 1, *, label: str = "upper") -> Any:
    """The n-th forward difference along ``dim`` (xarray's ``diff``).

    Δy_t = y_{t+1} - y_t, applied n times: N entries become N - n.

    The result's labels on ``dim`` are the minuend's (``"upper"``, positions
    n … N - 1) or the subtrahend's (``"lower"``, positions 0 … N - n - 1).

    Args:
        x: A named carrier.
        dim: The dim to difference along.
        n: The order, n ≥ 0; static under ``jit``.
        label: ``"upper"`` or ``"lower"``.

    Raises:
        KeyError: ``dim`` is not a dim of ``x``.
        ValueError: ``n`` is negative, or ``label`` is not ``"upper"`` or
            ``"lower"``.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> series = DimArray(np.array([1.0, 4.0, 9.0, 16.0]), ("time",))
        >>> diff(series, "time").data
        array([3., 5., 7.])
        >>> labelled = DimArray(series.data, ("time",), {"time": tuple("abcd")})
        >>> second = diff(labelled, "time", 2, label="lower")
        >>> second.data, second.labels["time"]
        (array([2., 2.]), ('a', 'b'))
    """
    adapter = adapter_of(x, "diff")
    dims = adapter.dims(x)
    axes_of(dims, (dim,), "diff")
    if n < 0:
        raise ValueError(f"diff: the order n must be ≥ 0, got {n}.")
    if label not in ("upper", "lower"):
        raise ValueError(f"diff: label must be 'upper' or 'lower', got {label!r}.")
    fast = fast_path(adapter, "diff")
    if fast is not None:
        return fast(x, dim, n=n, label=label)
    out = x
    for _ in range(n):
        upper = adapter.isel(out, {dim: slice(1, None)})
        lower = adapter.isel(out, {dim: slice(None, -1)})
        xp = namespace_of(adapter.data(upper))
        # Δy_t = y_{t+1} - y_t
        difference = xp.subtract(adapter.data(upper), adapter.data(lower))
        # the labelled slice carries its labels onto the difference
        labelled = upper if label == "upper" else lower
        out = adapter.rewrap(labelled, difference, dims)
    return out

dot(*xs, dim)

The sum of the product of carriers over named dims (xr.dot).

s_{o…} = ∑{d ∈ dim} ∏ₖ x⁽ᵏ⁾, where o… are the dims not in dim.

The output dims are every input dim not in dim, in order of first appearance. A dim shared by several inputs and not in dim is a batch dim: it appears once, so dot is one named-index einsum, never tensordot (A17). Shared dims must agree exactly in size and labels; dims from inputs after the first carry those inputs' labels.

Algorithm
  1. Give each distinct dim a letter, in order of first appearance.
  2. Write one subscript per input from its dims, and the output's from the dims not in dim.
  3. xp.einsum; for a namespace without it, broadcast every input to the union of the dims, multiply, and xp.sum over dim (exact, but holds the full product).
  4. rewrap on the first input, then relabel the dims taken from later inputs.

Parameters:

Name Type Description Default
*xs Any

Named carriers of one family and one backend.

()
dim str | Sequence[str]

A dim name, or several, to sum over; required (A17): xarray would sum every shared dim, so a dim shared by accident would vanish.

required

Raises:

Type Description
ValueError

No carrier is given, a shared dim differs in size or labels, or there are more dims than letters.

KeyError

A name in dim is a dim of no input.

BackendError

The inputs sit on more than one backend.

TypeError

The adapter has no relabel and a dim from a later input has labels.

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> x = DimArray(np.ones((12, 13)), ("time", "band"))
>>> w = DimArray(np.ones((12, 13, 3)), ("time", "band", "component"))
>>> s = dot(x, w, dim="band")
>>> s.dims, bool(np.allclose(s.data, 13.0))
(('time', 'component'), True)
>>> pixels = DimArray(np.ones((2, 13, 6)), ("time", "band", "pixel"))
>>> labelled = {"component": ("pc1", "pc2", "pc3")}
>>> weights = DimArray(np.ones((13, 3)), ("band", "component"), labelled)
>>> scores = dot(pixels, weights, dim="band")
>>> scores.dims, scores.labels["component"]
(('time', 'pixel', 'component'), ('pc1', 'pc2', 'pc3'))
Source code in packages/pipekit-array/src/pipekit_array/named/_reduce.py
def dot(*xs: Any, dim: str | Sequence[str]) -> Any:
    """The sum of the product of carriers over named dims (``xr.dot``).

    s_{o…} = ∑_{d ∈ dim} ∏ₖ x⁽ᵏ⁾_{…}, where o… are the dims not in ``dim``.

    The output dims are every input dim not in ``dim``, in order of first
    appearance. A dim shared by several inputs and not in ``dim`` is a
    batch dim: it appears once, so ``dot`` is one named-index ``einsum``,
    never ``tensordot`` (A17). Shared dims must agree exactly in size and
    labels; dims from inputs after the first carry those inputs' labels.

    Algorithm:
        1. Give each distinct dim a letter, in order of first appearance.
        2. Write one subscript per input from its dims, and the output's
           from the dims not in ``dim``.
        3. ``xp.einsum``; for a namespace without it, broadcast every
           input to the union of the dims, multiply, and ``xp.sum`` over
           ``dim`` (exact, but holds the full product).
        4. ``rewrap`` on the first input, then ``relabel`` the dims taken
           from later inputs.

    Args:
        *xs: Named carriers of one family and one backend.
        dim: A dim name, or several, to sum over; required (A17): xarray
            would sum every shared dim, so a dim shared by accident would
            vanish.

    Raises:
        ValueError: No carrier is given, a shared dim differs in size or
            labels, or there are more dims than letters.
        KeyError: A name in ``dim`` is a dim of no input.
        BackendError: The inputs sit on more than one backend.
        TypeError: The adapter has no ``relabel`` and a dim from a later
            input has labels.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> x = DimArray(np.ones((12, 13)), ("time", "band"))
        >>> w = DimArray(np.ones((12, 13, 3)), ("time", "band", "component"))
        >>> s = dot(x, w, dim="band")
        >>> s.dims, bool(np.allclose(s.data, 13.0))
        (('time', 'component'), True)
        >>> pixels = DimArray(np.ones((2, 13, 6)), ("time", "band", "pixel"))
        >>> labelled = {"component": ("pc1", "pc2", "pc3")}
        >>> weights = DimArray(np.ones((13, 3)), ("band", "component"), labelled)
        >>> scores = dot(pixels, weights, dim="band")
        >>> scores.dims, scores.labels["component"]
        (('time', 'pixel', 'component'), ('pc1', 'pc2', 'pc3'))
    """
    if not xs:
        raise ValueError("dot needs at least one carrier.")
    adapters = [adapter_of(x, "dot") for x in xs]
    adapter = adapters[0]
    names = (dim,) if isinstance(dim, str) else tuple(dim)
    each_dims = [a.dims(x) for a, x in zip(adapters, xs, strict=True)]
    union = tuple(dict.fromkeys(d for dims in each_dims for d in dims))
    axes_of(union, names, "dot")
    if len(union) > len(string.ascii_letters):
        raise ValueError(
            f"dot: {len(union)} distinct dims, but einsum has only "
            f"{len(string.ascii_letters)} letters."
        )
    datas = [a.data(x) for a, x in zip(adapters, xs, strict=True)]
    datas = join_backends(datas, "dot")
    fast = fast_path(adapter, "dot")
    if fast is not None:
        return fast(list(xs), names)
    # only the shared dims must combine: an empty slice of every other dim
    # checks them without touching the data
    counts = Counter(d for dims in each_dims for d in dims)
    parts = [
        a.isel(x, {d: slice(0, 0) for d in dims if counts[d] == 1})
        for a, x, dims in zip(adapters, xs, each_dims, strict=True)
    ]
    adapter.check_compatible(*parts)
    out_dims = tuple(d for d in union if d not in names)
    xp = namespace_of(datas[0])
    einsum = getattr(xp, "einsum", None)
    if einsum is None:
        out = _product_sum(xp, datas, each_dims, union, names)
    else:
        letters = dict(zip(union, string.ascii_letters, strict=False))
        inputs = ",".join("".join(letters[d] for d in dims) for dims in each_dims)
        output = "".join(letters[d] for d in out_dims)
        # s_{o…} = ∑_{d ∈ dim} ∏ₖ x⁽ᵏ⁾, e.g. "ab,abc->ac" for time band, time
        # band component over band
        out = einsum(f"{inputs}->{output}", *datas)
    result = adapter.rewrap(xs[0], out, out_dims)
    # dims the first input lacks take the labels of the first input with them
    labels: dict[str, Labels] = {}
    for a, x, dims in zip(adapters[1:], xs[1:], each_dims[1:], strict=True):
        for d in dims:
            if d in out_dims and d not in each_dims[0] and d not in labels:
                labels[d] = a.labels(x, d)
    return set_labels(adapter, result, labels, "dot")

expand_dims(x, dim, /, *, axis=0)

Insert length-1 dims, consecutively, at position axis of the result.

y_{…, 0, …} = x_{…}: the new dims have size 1 and no labels.

A negative axis counts from the end, so axis=-1 appends.

Parameters:

Name Type Description Default
x Any

A named carrier.

required
dim str | Sequence[str]

A new dim name, or several.

required
axis int

Where the first new dim goes in the result.

0

Raises:

Type Description
ValueError

A new name is already a dim, a name repeats, or axis is out of range.

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> scene = DimArray(np.zeros((2, 4)), ("band", "x"))
>>> expand_dims(scene, "member").dims
('member', 'band', 'x')
>>> expand_dims(scene, ("member", "run"), axis=-1).shape
(2, 4, 1, 1)
Source code in packages/pipekit-array/src/pipekit_array/named/_shape.py
def expand_dims(x: Any, dim: str | Sequence[str], /, *, axis: int = 0) -> Any:
    """Insert length-1 dims, consecutively, at position ``axis`` of the result.

    y_{…, 0, …} = x_{…}: the new dims have size 1 and no labels.

    A negative ``axis`` counts from the end, so ``axis=-1`` appends.

    Args:
        x: A named carrier.
        dim: A new dim name, or several.
        axis: Where the first new dim goes in the result.

    Raises:
        ValueError: A new name is already a dim, a name repeats, or
            ``axis`` is out of range.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> scene = DimArray(np.zeros((2, 4)), ("band", "x"))
        >>> expand_dims(scene, "member").dims
        ('member', 'band', 'x')
        >>> expand_dims(scene, ("member", "run"), axis=-1).shape
        (2, 4, 1, 1)
    """
    adapter = adapter_of(x, "expand_dims")
    names = (dim,) if isinstance(dim, str) else tuple(dim)
    dims = adapter.dims(x)
    taken = [n for n in names if n in dims]
    if taken:
        raise ValueError(f"expand_dims: {taken} are already dims of {dims}.")
    if len(set(names)) != len(names):
        raise ValueError(f"expand_dims: the new dims {names} repeat a name.")
    # a negative axis counts from the end of the result: -1 appends
    start = axis if axis >= 0 else axis + len(dims) + 1
    if not 0 <= start <= len(dims):
        raise ValueError(f"expand_dims: axis {axis} is out of range for dims {dims}.")
    fast = fast_path(adapter, "expand_dims")
    if fast is not None:
        return fast(x, names, axis=start)
    data = adapter.data(x)
    xp = namespace_of(data)
    expanded = data
    for offset in range(len(names)):
        expanded = xp.expand_dims(expanded, axis=start + offset)
    new_dims = (*dims[:start], *names, *dims[start:])
    return adapter.rewrap(x, expanded, new_dims)

from_rows(rows, layout, *, feature_dim=None)

A carrier like layout.template from a table of rows.

Parameters:

Name Type Description Default
rows Any

(n_samples, n_features) gives back the original feature dims; (n_samples, k) with feature_dim gives one new dim of size k in their place (PCA components, class probabilities); (n_samples,) gives the sample dims only (a label or a score per sample).

required
layout RowLayout

From to_rows.

required
feature_dim str | None

The name of the new feature dim, for a table whose columns are not the original features.

None

Returns:

Type Description
Any

A carrier with layout's dims in their original order, the new

Any

feature dim (if any) where the first feature dim was, rewrapped by

Any

the template's adapter.

Raises:

Type Description
ValueError

The rows do not match the layout, or a new feature dim is needed and feature_dim is missing or already a dim.

Example

import numpy as np from pipekit_array import DimArray scene = DimArray(np.arange(24.0).reshape(2, 3, 4), ("band", "y", "x")) rows, layout = to_rows(scene, sample_dims=("y", "x")) from_rows(rows[:, :1] * 0, layout, feature_dim="score").dims ('score', 'y', 'x')

Source code in packages/pipekit-array/src/pipekit_array/named/_rows.py
def from_rows(rows: Any, layout: RowLayout, *, feature_dim: str | None = None) -> Any:
    """A carrier like ``layout.template`` from a table of rows.

    Args:
        rows: ``(n_samples, n_features)`` gives back the original feature
            dims; ``(n_samples, k)`` with ``feature_dim`` gives one new dim
            of size ``k`` in their place (PCA components, class
            probabilities); ``(n_samples,)`` gives the sample dims only
            (a label or a score per sample).
        layout: From `to_rows`.
        feature_dim: The name of the new feature dim, for a table whose
            columns are not the original features.

    Returns:
        A carrier with ``layout``'s dims in their original order, the new
        feature dim (if any) where the first feature dim was, rewrapped by
        the template's adapter.

    Raises:
        ValueError: The rows do not match the layout, or a new feature dim
            is needed and ``feature_dim`` is missing or already a dim.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> scene = DimArray(np.arange(24.0).reshape(2, 3, 4), ("band", "y", "x"))
        >>> rows, layout = to_rows(scene, sample_dims=("y", "x"))
        >>> from_rows(rows[:, :1] * 0, layout, feature_dim="score").dims
        ('score', 'y', 'x')
    """
    template = layout.template
    if template is None:
        raise ValueError("from_rows needs a layout from to_rows (it has no template).")
    adapter = adapter_of(template, "from_rows")
    shape = tuple(int(n) for n in rows.shape)
    if len(shape) not in (1, 2) or shape[0] != layout.n_samples:
        raise ValueError(
            f"from_rows: rows of shape {shape} do not fit {layout.n_samples} samples "
            f"over {layout.sample_dims}."
        )
    sample_sizes = tuple(layout._size(d) for d in layout.sample_dims)
    if len(shape) == 1:
        block_dims = layout.sample_dims
        block_shape = sample_sizes
        order = layout.sample_dims
    elif feature_dim is None:
        if shape[1] != layout.n_features:
            raise ValueError(
                f"from_rows: {shape[1]} columns are not the {layout.n_features} "
                f"features over {layout.feature_dims}; pass feature_dim= to name "
                "the new columns."
            )
        block_dims = (*layout.sample_dims, *layout.feature_dims)
        block_shape = (*sample_sizes, *(layout._size(d) for d in layout.feature_dims))
        order = layout.dims
    else:
        if feature_dim in layout.dims:
            raise ValueError(
                f"from_rows: feature_dim {feature_dim!r} is already a dim of "
                f"{layout.dims}."
            )
        block_dims = (*layout.sample_dims, feature_dim)
        block_shape = (*sample_sizes, shape[1])
        order = _with_new_feature_dim(layout, feature_dim)
    xp = namespace_of(rows)
    block = xp.reshape(rows, block_shape)
    axes = tuple(block_dims.index(d) for d in order)
    if axes != tuple(range(len(axes))):
        block = xp.permute_dims(block, axes)
    return adapter.rewrap(template, block, tuple(order))

isel(x, /, **indexers)

Select by position on named dims; an integer drops its dim.

x is positional-only, so a dim may be called x.

Parameters:

Name Type Description Default
x Any

A named carrier.

required
**indexers Any

dim= an int, a slice or an integer index array.

{}
Example

import numpy as np from pipekit_array import DimArray cube = DimArray(np.zeros((12, 2, 4)), ("time", "band", "x")) isel(cube, time=0).dims ('band', 'x')

Source code in packages/pipekit-array/src/pipekit_array/named/_select.py
def isel(x: Any, /, **indexers: Any) -> Any:
    """Select by position on named dims; an integer drops its dim.

    ``x`` is positional-only, so a dim may be called ``x``.

    Args:
        x: A named carrier.
        **indexers: ``dim=`` an int, a slice or an integer index array.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> cube = DimArray(np.zeros((12, 2, 4)), ("time", "band", "x"))
        >>> isel(cube, time=0).dims
        ('band', 'x')
    """
    adapter = adapter_of(x, "isel")
    axes_of(adapter.dims(x), list(indexers), "isel")
    return adapter.isel(x, dict(indexers))

join_backends(datas, what)

The raw arrays on one backend: NumPy arrays next to JAX ones join them.

The order of the inputs does not matter, and a NumPy array is a constant when a JAX one is traced (ADR A24). pipekit_ops.guard reports the arrays of one dimension or more that join eagerly, and a dtype they widen.

Parameters:

Name Type Description Default
datas Sequence[Any]

Raw arrays.

required
what str

The function that joins them, for the guard and the errors.

required

Raises:

Type Description
BackendError

Any other mix: dask with NumPy or JAX, or another library with JAX.

Example

import numpy as np first, second = join_backends([np.ones(2), np.ones(3)], "stack") first.shape, second.shape ((2,), (3,))

Source code in packages/pipekit-array/src/pipekit_array/named/_dispatch.py
def join_backends(datas: Sequence[Any], what: str) -> list[Any]:
    """The raw arrays on one backend: NumPy arrays next to JAX ones join them.

    The order of the inputs does not matter, and a NumPy array is a constant
    when a JAX one is traced (ADR A24). `pipekit_ops.guard` reports the arrays
    of one dimension or more that join eagerly, and a dtype they widen.

    Args:
        datas: Raw arrays.
        what: The function that joins them, for the guard and the errors.

    Raises:
        BackendError: Any other mix: dask with NumPy or JAX, or another
            library with JAX.

    Example:
        >>> import numpy as np
        >>> first, second = join_backends([np.ones(2), np.ones(3)], "stack")
        >>> first.shape, second.shape
        ((2,), (3,))
    """
    joined = list(datas)
    backends = {backend_name(d) for d in joined}
    if len(backends) < 2:
        return joined
    strangers = [d for d in joined if backend_name(d) == "numpy" and not _is_numpy(d)]
    if backends != {"numpy", "jax"} or strangers:
        names = sorted(backends - {"numpy"} | {_library(d) for d in joined})
        raise BackendError(
            f"{what}: inputs on {names}; put them on one backend first (NumPy joins "
            "JAX, and nothing else moves)."
        )
    import jax.numpy as jnp

    if guard_level() != "off":
        _report_join(joined, what)
    # a Python number stays one: JAX gives it the arrays' dtype
    numbers = (bool, int, float, complex)
    return [
        d if backend_name(d) == "jax" or type(d) in numbers else jnp.asarray(d)
        for d in joined
    ]

map_over(x, fn, dim)

fn on each item along dim; the results stacked along dim, first.

Each item is isel(x, dim=i); fn returns a named carrier, and every result must have the same dims.

On JAX data this is one jax.vmap, for a carrier whose only array is its data (an adapter with more has a fast path of its own). A fn that cannot be traced (it branches on a value, or runs on NumPy only) is called on each item in a loop instead, unless x is traced itself.

Raises:

Type Description
KeyError

dim is not a dim of x.

TypeError

fn returns something other than a named carrier.

ValueError

The results have different dims, or have dim already.

Example

import numpy as np from pipekit_array import DimArray, reduce cube = DimArray(np.ones((3, 2, 4)), ("time", "band", "x")) map_over(cube, lambda item: reduce(item, "sum", "band"), "time").dims ('time', 'x')

Source code in packages/pipekit-array/src/pipekit_array/named/_map.py
def map_over(x: Any, fn: Callable[[Any], Any], dim: str) -> Any:
    """``fn`` on each item along ``dim``; the results stacked along ``dim``, first.

    Each item is ``isel(x, dim=i)``; ``fn`` returns a named carrier, and every
    result must have the same dims.

    On JAX data this is one ``jax.vmap``, for a carrier whose only array is its
    data (an adapter with more has a fast path of its own). A ``fn`` that cannot
    be traced (it branches on a value, or runs on NumPy only) is called on each
    item in a loop instead, unless ``x`` is traced itself.

    Raises:
        KeyError: ``dim`` is not a dim of ``x``.
        TypeError: ``fn`` returns something other than a named carrier.
        ValueError: The results have different dims, or have ``dim`` already.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray, reduce
        >>> cube = DimArray(np.ones((3, 2, 4)), ("time", "band", "x"))
        >>> map_over(cube, lambda item: reduce(item, "sum", "band"), "time").dims
        ('time', 'x')
    """
    adapter = adapter_of(x, "map_over")
    fast = fast_path(adapter, "map_over")
    if fast is not None:
        return fast(x, fn, dim)
    dims = adapter.dims(x)
    (axis,) = axes_of(dims, (dim,), "map_over")
    if backend_name(adapter.data(x)) == "jax" and _only_array(adapter, x):
        mapped = _map_over_vmap(adapter, x, fn, dim, axis)
        if mapped is not None:
            return mapped
    size = adapter.data(x).shape[axis]
    items = [fn(adapter.isel(x, {dim: i})) for i in range(size)]
    return _with_labels_of(adapter, x, stack(items, dim), dim)

merge_dims(x, /, **dims)

Merge the listed dims into one, placed last, in C order (xarray's stack).

y_{…, p} = x_{…, i, j}, with p = i·X + j for pixel=("y", "x").

The merged dim's labels are tuples of the source labels, with positions for an unlabelled source; it has none when no source has labels. split_dims undoes it.

Parameters:

Name Type Description Default
x Any

A named carrier.

required
**dims Sequence[str]

new=(source, …) for each merged dim; several merges go last in the order given.

{}

Raises:

Type Description
KeyError

A source is not a dim of x.

ValueError

A merge lists no source, a source is merged twice, or the result repeats a dim name.

TypeError

A merge's sources are one string, or the adapter has no relabel and the merged dim has labels.

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> bands = {"band": ("B4", "B8")}
>>> cube = DimArray(np.zeros((2, 3, 4)), ("band", "y", "x"), bands)
>>> pixels = merge_dims(cube, pixel=("y", "x"))
>>> pixels.dims, pixels.shape
(('band', 'pixel'), (2, 12))
>>> merge_dims(cube, pb=("band", "y")).labels["pb"][:2]
(('B4', 0), ('B4', 1))
Source code in packages/pipekit-array/src/pipekit_array/named/_shape.py
def merge_dims(x: Any, /, **dims: Sequence[str]) -> Any:
    """Merge the listed dims into one, placed last, in C order (xarray's ``stack``).

    y_{…, p} = x_{…, i, j}, with p = i·X + j for ``pixel=("y", "x")``.

    The merged dim's labels are tuples of the source labels, with
    positions for an unlabelled source; it has none when no source has
    labels. `split_dims` undoes it.

    Args:
        x: A named carrier.
        **dims: ``new=(source, …)`` for each merged dim; several merges go
            last in the order given.

    Raises:
        KeyError: A source is not a dim of ``x``.
        ValueError: A merge lists no source, a source is merged twice, or
            the result repeats a dim name.
        TypeError: A merge's sources are one string, or the adapter has no
            ``relabel`` and the merged dim has labels.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> bands = {"band": ("B4", "B8")}
        >>> cube = DimArray(np.zeros((2, 3, 4)), ("band", "y", "x"), bands)
        >>> pixels = merge_dims(cube, pixel=("y", "x"))
        >>> pixels.dims, pixels.shape
        (('band', 'pixel'), (2, 12))
        >>> merge_dims(cube, pb=("band", "y")).labels["pb"][:2]
        (('B4', 0), ('B4', 1))
    """
    adapter = adapter_of(x, "merge_dims")
    current = adapter.dims(x)
    for name, group in dims.items():
        if isinstance(group, str):
            raise TypeError(
                f"merge_dims: {name}= takes a sequence of dim names, got {group!r}."
            )
        if len(group) == 0:
            raise ValueError(f"merge_dims: {name}= lists no dim to merge.")
    groups = {name: tuple(group) for name, group in dims.items()}
    sources = [s for group in groups.values() for s in group]
    axes_of(current, sources, "merge_dims")
    if len(set(sources)) != len(sources):
        raise ValueError(f"merge_dims: a dim is merged twice in {groups}.")
    kept = tuple(d for d in current if d not in sources)
    new_dims = (*kept, *groups)
    if len(set(new_dims)) != len(new_dims):
        raise ValueError(f"merge_dims: the new dims {new_dims} repeat a name.")
    fast = fast_path(adapter, "merge_dims")
    if fast is not None:
        return fast(x, groups)
    data = adapter.data(x)
    xp = namespace_of(data)
    sizes = dict(zip(current, data.shape, strict=True))
    moved = xp.permute_dims(data, axes_of(current, (*kept, *sources), "merge_dims"))
    # each merged dim has the product of its sources' sizes
    merged_sizes = [math.prod(sizes[s] for s in group) for group in groups.values()]
    merged = xp.reshape(moved, (*(sizes[d] for d in kept), *merged_sizes))
    out = adapter.rewrap(x, merged, new_dims)
    labels = {
        name: merged_labels(
            [adapter.labels(x, s) for s in group], [sizes[s] for s in group]
        )
        for name, group in groups.items()
    }
    return set_labels(adapter, out, labels, "merge_dims")

pad(x, /, *, mode='constant', constant_values=None, **pad_width)

Widen named dims at either end (xarray's pad, four modes).

y_{…, t, …} = x_{…, t - b, …} for b ≤ t < b + N; the new entries follow mode.

A padded dim loses its labels (xarray pads its coordinate with NaN).

Parameters:

Name Type Description Default
x Any

A named carrier.

required
mode str

"constant" (constant_values), "edge" (repeat the end entry), "reflect" (mirror, end excluded) or "wrap" (the other end's entries).

'constant'
constant_values float | None

The fill for mode="constant"; None is NaN.

None
**pad_width int | tuple[int, int]

dim= a width b for both ends, or a pair (before, after).

{}

Raises:

Type Description
KeyError

A padded dim is not a dim of x.

ValueError

mode is not one of the four, a width is negative, or constant_values is given with another mode.

TypeError

mode="constant" on integer or bool data without constant_values.

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> series = DimArray(np.arange(3.0), ("time",))
>>> pad(series, time=(2, 0)).data
array([nan, nan,  0.,  1.,  2.])
>>> pad(series, mode="wrap", time=1).data
array([2., 0., 1., 2., 0.])
>>> counts = DimArray(np.arange(3), ("time",))
>>> pad(counts, time=1)
Traceback (most recent call last):
...
TypeError: pad: int64 data has no NaN to fill with; pass constant_values=
Source code in packages/pipekit-array/src/pipekit_array/named/_shift.py
def pad(
    x: Any,
    /,
    *,
    mode: str = "constant",
    constant_values: float | None = None,
    **pad_width: int | tuple[int, int],
) -> Any:
    """Widen named dims at either end (xarray's ``pad``, four modes).

    y_{…, t, …} = x_{…, t - b, …} for b ≤ t < b + N; the new entries follow
    ``mode``.

    A padded dim loses its labels (xarray pads its coordinate with NaN).

    Args:
        x: A named carrier.
        mode: ``"constant"`` (``constant_values``), ``"edge"`` (repeat the
            end entry), ``"reflect"`` (mirror, end excluded) or ``"wrap"``
            (the other end's entries).
        constant_values: The fill for ``mode="constant"``; None is NaN.
        **pad_width: ``dim=`` a width b for both ends, or a pair
            ``(before, after)``.

    Raises:
        KeyError: A padded dim is not a dim of ``x``.
        ValueError: ``mode`` is not one of the four, a width is negative,
            or ``constant_values`` is given with another mode.
        TypeError: ``mode="constant"`` on integer or bool data without
            ``constant_values``.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> series = DimArray(np.arange(3.0), ("time",))
        >>> pad(series, time=(2, 0)).data
        array([nan, nan,  0.,  1.,  2.])
        >>> pad(series, mode="wrap", time=1).data
        array([2., 0., 1., 2., 0.])
        >>> counts = DimArray(np.arange(3), ("time",))
        >>> pad(counts, time=1)  # doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
        ...
        TypeError: pad: int64 data has no NaN to fill with; pass constant_values=
    """
    adapter = adapter_of(x, "pad")
    dims = adapter.dims(x)
    axes_of(dims, list(pad_width), "pad")
    if mode not in PAD_MODES:
        raise ValueError(f"pad: mode {mode!r} is not one of {PAD_MODES}.")
    if mode != "constant" and constant_values is not None:
        raise ValueError(f"pad: constant_values is for mode='constant', not {mode!r}.")
    widths = {d: _widths(d, w) for d, w in pad_width.items()}
    data = adapter.data(x)
    xp = namespace_of(data)
    if (
        mode == "constant"
        and constant_values is None
        and not _holds_nan(xp, data.dtype)
    ):
        raise _no_nan_fill("pad", "constant_values", data.dtype)
    fast = fast_path(adapter, "pad")
    if fast is not None:
        return fast(x, widths, mode=mode, constant_values=constant_values)
    full_widths = tuple(widths.get(d, (0, 0)) for d in dims)
    if mode == "constant":
        fill = float("nan") if constant_values is None else constant_values
        padded = xp.pad(data, full_widths, mode=mode, constant_values=fill)
    else:
        padded = xp.pad(data, full_widths, mode=mode)
    return adapter.rewrap(x, padded, dims)

quantile(x, q, dim, /, *, method='linear', skipna=False)

Quantiles over named dims (xarray's quantile).

Q_{k…} = quantile q_k of {x_{…, d…} : d over dim}.

A sequence q adds a leading dim quantile, labelled with q; a float drops it. A float input keeps its dtype. NaN propagates unless skipna (no implicit skipna, A12).

Parameters:

Name Type Description Default
x Any

A named carrier.

required
q float | Sequence[float]

A probability in [0, 1], or a sequence of them; static under jit.

required
dim str | Sequence[str]

A dim name, or several, to reduce over.

required
method str

One of METHODS, the six JAX supports.

'linear'
skipna bool

Ignore NaN (nanquantile).

False

Raises:

Type Description
KeyError

A name is not a dim of x.

ValueError

method is not one of METHODS, a probability is outside [0, 1], or a sequence q meets a kept dim named quantile.

TypeError

The adapter has no relabel and q is a sequence.

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> cube = DimArray(np.arange(24.0).reshape(4, 2, 3), ("time", "band", "x"))
>>> spread = quantile(cube, [0.1, 0.9], "time")
>>> spread.dims, spread.labels["quantile"]
(('quantile', 'band', 'x'), (0.1, 0.9))
>>> quantile(cube, 0.5, ("time", "x"), method="lower").dims
('band',)
Source code in packages/pipekit-array/src/pipekit_array/named/_reduce.py
def quantile(
    x: Any,
    q: float | Sequence[float],
    dim: str | Sequence[str],
    /,
    *,
    method: str = "linear",
    skipna: bool = False,
) -> Any:
    """Quantiles over named dims (xarray's ``quantile``).

    Q_{k…} = quantile q_k of {x_{…, d…} : d over ``dim``}.

    A sequence ``q`` adds a leading dim ``quantile``, labelled with ``q``;
    a float drops it. A float input keeps its dtype. NaN propagates unless
    ``skipna`` (no implicit ``skipna``, A12).

    Args:
        x: A named carrier.
        q: A probability in [0, 1], or a sequence of them; static under
            ``jit``.
        dim: A dim name, or several, to reduce over.
        method: One of `METHODS`, the six JAX supports.
        skipna: Ignore NaN (``nanquantile``).

    Raises:
        KeyError: A name is not a dim of ``x``.
        ValueError: ``method`` is not one of `METHODS`, a probability is
            outside [0, 1], or a sequence ``q`` meets a kept dim named
            ``quantile``.
        TypeError: The adapter has no ``relabel`` and ``q`` is a sequence.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> cube = DimArray(np.arange(24.0).reshape(4, 2, 3), ("time", "band", "x"))
        >>> spread = quantile(cube, [0.1, 0.9], "time")
        >>> spread.dims, spread.labels["quantile"]
        (('quantile', 'band', 'x'), (0.1, 0.9))
        >>> quantile(cube, 0.5, ("time", "x"), method="lower").dims
        ('band',)
    """
    adapter = adapter_of(x, "quantile")
    names = (dim,) if isinstance(dim, str) else tuple(dim)
    dims = adapter.dims(x)
    axes = axes_of(dims, names, "quantile")
    check_method(method, "quantile")
    values = probabilities(q, "quantile")
    many = isinstance(values, tuple)
    kept = tuple(d for d in dims if d not in names)
    if many and "quantile" in kept:
        raise ValueError(
            f"quantile: 'quantile' is already a dim of {dims}; rename it first."
        )
    fast = fast_path(adapter, "quantile")
    if fast is not None:
        return fast(x, values, names, method=method, skipna=skipna)
    import numpy as np

    data = adapter.data(x)
    if backend_name(data) == "dask":
        # dask's quantile needs every reduced axis in one chunk
        data = data.rechunk(dict.fromkeys(axes, -1))
    xp = namespace_of(data)
    func = resolve_function("nanquantile" if skipna else "quantile", xp, "quantile")
    floating = np.issubdtype(data.dtype, np.floating)
    # q in the data's float dtype: NumPy promotes float32 to float64 for a
    # float64 sequence q; dask needs a NumPy q
    q_array = np.asarray(values, dtype=data.dtype if floating else None)
    # one q as a NumPy scalar: dask takes a 0-d array for a sequence
    q_value = q_array if many else q_array[()]
    out = func(data, q_value, axis=axes, method=method)
    result = adapter.rewrap(x, out, ("quantile", *kept) if many else kept)
    if not isinstance(values, tuple):
        return result
    labels: dict[str, Labels] = {"quantile": values}
    return set_labels(adapter, result, labels, "quantile")

reduce(x, fn, dim, *, where=None)

Reduce over one or more named dims.

Parameters:

Name Type Description Default
x Any

A named carrier.

required
fn Reduction

An array-namespace name ("mean", "max", "nanmean") or a function taking axis= (and where=, when given).

required
dim str | Sequence[str]

A dim name, or several.

required
where Any | None

Optional bool mask of the entries to keep, passed to fn as where= ("mean" then divides by the kept count). Either a named carrier, aligned to x by dim name (its dims must be among x's), or a plain array covering x's leading dims (where.shape == shape[:where.ndim]): a (sample,) mask on (sample, y, x) data, as Padded.valid is. Never right-broadcast. "max" / "min" need an initial for a mask: pass a function that supplies one.

None

Raises:

Type Description
ValueError

A plain where does not match x's leading dims, or a named one has a dim x lacks.

TypeError

where on data other than NumPy or JAX (the reductions of dask, PyTorch and CuPy take no where=).

Example

import numpy as np from pipekit_array import DimArray cube = DimArray(np.ones((12, 2, 4)), ("time", "band", "x")) reduce(cube, "mean", "time").dims ('band', 'x') batch = DimArray(np.array([[1.0], [3.0], [9.0]]), ("sample", "x")) reduce(batch, "mean", "sample", where=np.array([True, True, False])).data array([2.])

Source code in packages/pipekit-array/src/pipekit_array/named/_reduce.py
def reduce(
    x: Any, fn: Reduction, dim: str | Sequence[str], *, where: Any | None = None
) -> Any:
    """Reduce over one or more named dims.

    Args:
        x: A named carrier.
        fn: An array-namespace name (``"mean"``, ``"max"``, ``"nanmean"``) or a
            function taking ``axis=`` (and ``where=``, when given).
        dim: A dim name, or several.
        where: Optional bool mask of the entries to keep, passed to ``fn``
            as ``where=`` (``"mean"`` then divides by the kept count). Either
            a named carrier, aligned to ``x`` by dim name (its dims must be
            among ``x``'s), or a plain array covering ``x``'s *leading* dims
            (``where.shape == shape[:where.ndim]``): a ``(sample,)`` mask on
            ``(sample, y, x)`` data, as `Padded.valid` is. Never
            right-broadcast. ``"max"`` / ``"min"`` need an ``initial`` for
            a mask: pass a function that supplies one.

    Raises:
        ValueError: A plain ``where`` does not match ``x``'s leading dims,
            or a named one has a dim ``x`` lacks.
        TypeError: ``where`` on data other than NumPy or JAX (the
            reductions of dask, PyTorch and CuPy take no ``where=``).

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> cube = DimArray(np.ones((12, 2, 4)), ("time", "band", "x"))
        >>> reduce(cube, "mean", "time").dims
        ('band', 'x')
        >>> batch = DimArray(np.array([[1.0], [3.0], [9.0]]), ("sample", "x"))
        >>> reduce(batch, "mean", "sample", where=np.array([True, True, False])).data
        array([2.])
    """
    adapter = adapter_of(x, "reduce")
    names = (dim,) if isinstance(dim, str) else tuple(dim)
    fast = fast_path(adapter, "reduce")
    dims = adapter.dims(x)
    data = adapter.data(x)
    if where is not None:
        _require_where_backend(data)
    mask = None if where is None else aligned_mask(where, dims, data, "reduce")
    if mask is not None:
        x, data, mask = join_mask(adapter, x, data, mask, "reduce")
    if fast is not None:
        return fast(x, fn, names) if mask is None else fast(x, fn, names, where=mask)
    axes = axes_of(dims, names, "reduce")
    func = resolve_function(fn, namespace_of(data), "reduce")
    out = func(data, axis=axes) if mask is None else func(data, axis=axes, where=mask)
    kept = tuple(d for d in dims if d not in names)
    return adapter.rewrap(x, out, kept)

rename(x, /, **names)

New dim names; data, order and labels unchanged.

y = x, with each dim d called names[d].

x is positional-only, so a dim may be called x.

Parameters:

Name Type Description Default
x Any

A named carrier.

required
**names str

old=new for each dim to rename.

{}

Raises:

Type Description
KeyError

An old name is not a dim of x.

ValueError

The new names repeat a dim.

TypeError

The adapter has neither a rename fast path nor relabel, and a renamed dim has labels.

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> scene = DimArray(np.zeros((2, 4)), ("band", "x"), {"band": ("B4", "B8")})
>>> channels = rename(scene, band="channel")
>>> channels.dims, channels.labels["channel"]
(('channel', 'x'), ('B4', 'B8'))
>>> rename(channels, channel="band").labels == scene.labels
True
Source code in packages/pipekit-array/src/pipekit_array/named/_shape.py
def rename(x: Any, /, **names: str) -> Any:
    """New dim names; data, order and labels unchanged.

    y = x, with each dim ``d`` called ``names[d]``.

    ``x`` is positional-only, so a dim may be called ``x``.

    Args:
        x: A named carrier.
        **names: ``old=new`` for each dim to rename.

    Raises:
        KeyError: An old name is not a dim of ``x``.
        ValueError: The new names repeat a dim.
        TypeError: The adapter has neither a ``rename`` fast path nor
            ``relabel``, and a renamed dim has labels.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> scene = DimArray(np.zeros((2, 4)), ("band", "x"), {"band": ("B4", "B8")})
        >>> channels = rename(scene, band="channel")
        >>> channels.dims, channels.labels["channel"]
        (('channel', 'x'), ('B4', 'B8'))
        >>> rename(channels, channel="band").labels == scene.labels
        True
    """
    adapter = adapter_of(x, "rename")
    dims = adapter.dims(x)
    axes_of(dims, list(names), "rename")
    new_dims = tuple(names.get(d, d) for d in dims)
    if len(set(new_dims)) != len(new_dims):
        raise ValueError(f"rename: the new dims {new_dims} repeat a name.")
    fast = fast_path(adapter, "rename")
    if fast is not None:
        return fast(x, dict(names))
    out = adapter.rewrap(x, adapter.data(x), new_dims)
    labels = {new: adapter.labels(x, old) for old, new in names.items()}
    return set_labels(adapter, out, labels, "rename")

roll(x, /, **shifts)

Shift data cyclically along named dims; labels stay (xarray's roll).

y_{…, t, …} = x_{…, (t - k) mod N, …}.

Parameters:

Name Type Description Default
x Any

A named carrier.

required
**shifts int

dim=k; a positive k moves data to higher positions. k may be traced under jit.

{}

Raises:

Type Description
KeyError

A rolled dim is not a dim of x.

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> series = DimArray(np.arange(4.0), ("time",), {"time": tuple("abcd")})
>>> rolled = roll(series, time=1)
>>> rolled.data, rolled.labels["time"]
(array([3., 0., 1., 2.]), ('a', 'b', 'c', 'd'))
>>> bool(np.array_equal(roll(rolled, time=-1).data, series.data))
True
Source code in packages/pipekit-array/src/pipekit_array/named/_shift.py
def roll(x: Any, /, **shifts: int) -> Any:
    """Shift data cyclically along named dims; labels stay (xarray's ``roll``).

    y_{…, t, …} = x_{…, (t - k) mod N, …}.

    Args:
        x: A named carrier.
        **shifts: ``dim=k``; a positive k moves data to higher positions.
            k may be traced under ``jit``.

    Raises:
        KeyError: A rolled dim is not a dim of ``x``.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> series = DimArray(np.arange(4.0), ("time",), {"time": tuple("abcd")})
        >>> rolled = roll(series, time=1)
        >>> rolled.data, rolled.labels["time"]
        (array([3., 0., 1., 2.]), ('a', 'b', 'c', 'd'))
        >>> bool(np.array_equal(roll(rolled, time=-1).data, series.data))
        True
    """
    adapter = adapter_of(x, "roll")
    dims = adapter.dims(x)
    axes = axes_of(dims, list(shifts), "roll")
    fast = fast_path(adapter, "roll")
    if fast is not None:
        return fast(x, dict(shifts))
    data = adapter.data(x)
    xp = namespace_of(data)
    # y_t = x_{(t - k) mod N}
    rolled = xp.roll(data, shift=tuple(shifts.values()), axis=axes)
    return adapter.rewrap(x, rolled, dims)

sel(x, /, **labels)

Select by exact label on named dims: no ranges, no nearest.

x is positional-only, so a dim may be called x.

A single label drops its dim; a list of labels keeps it.

Raises:

Type Description
KeyError

A dim has no labels, or a label is not among them.

Example

import numpy as np from pipekit_array import DimArray scene = DimArray(np.zeros((2, 4)), ("band", "x"), {"band": ("B4", "B8")}) sel(scene, band="B8").dims ('x',)

Source code in packages/pipekit-array/src/pipekit_array/named/_select.py
def sel(x: Any, /, **labels: Any) -> Any:
    """Select by exact label on named dims: no ranges, no nearest.

    ``x`` is positional-only, so a dim may be called ``x``.

    A single label drops its dim; a list of labels keeps it.

    Raises:
        KeyError: A dim has no labels, or a label is not among them.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> scene = DimArray(np.zeros((2, 4)), ("band", "x"), {"band": ("B4", "B8")})
        >>> sel(scene, band="B8").dims
        ('x',)
    """
    adapter = adapter_of(x, "sel")
    axes_of(adapter.dims(x), list(labels), "sel")
    indexers: dict[str, Any] = {}
    for dim, wanted in labels.items():
        values = adapter.labels(x, dim)
        if values is None:
            raise KeyError(f"sel: dim {dim!r} has no labels; use isel.")
        position = {value: i for i, value in enumerate(values)}
        many = _many_labels(wanted, position)
        if many and not isinstance(wanted, (list, tuple)):
            wanted = list(wanted.tolist())
        for label in wanted if many else (wanted,):
            if label not in position:
                raise KeyError(f"sel: {label!r} is not a label of {dim!r}: {values}.")
        if many:
            indexers[dim] = [position[label] for label in wanted]
        else:
            indexers[dim] = position[wanted]
    return adapter.isel(x, indexers)

shift(x, /, *, fill_value=None, **shifts)

Shift data along named dims, filling the vacated entries (xarray's shift).

y_{…, t, …} = x_{…, t - k, …} if 0 ≤ t - k < N, else the fill.

Labels stay on their positions. A shift of at least the dim's length fills it entirely.

Parameters:

Name Type Description Default
x Any

A named carrier.

required
fill_value float | None

The value of the vacated entries; None is NaN.

None
**shifts int

dim=k, a Python int (static under jit); a positive k moves data to higher positions.

{}

Raises:

Type Description
KeyError

A shifted dim is not a dim of x.

TypeError

Integer or bool data without fill_value.

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> series = DimArray(np.arange(4.0), ("time",))
>>> shift(series, time=1).data
array([nan,  0.,  1.,  2.])
>>> shift(DimArray(np.arange(4), ("time",)), time=-2, fill_value=-1).data
array([ 2,  3, -1, -1])
Source code in packages/pipekit-array/src/pipekit_array/named/_shift.py
def shift(x: Any, /, *, fill_value: float | None = None, **shifts: int) -> Any:
    """Shift data along named dims, filling the vacated entries (xarray's ``shift``).

    y_{…, t, …} = x_{…, t - k, …} if 0 ≤ t - k < N, else the fill.

    Labels stay on their positions. A shift of at least the dim's length
    fills it entirely.

    Args:
        x: A named carrier.
        fill_value: The value of the vacated entries; None is NaN.
        **shifts: ``dim=k``, a Python int (static under ``jit``); a
            positive k moves data to higher positions.

    Raises:
        KeyError: A shifted dim is not a dim of ``x``.
        TypeError: Integer or bool data without ``fill_value``.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> series = DimArray(np.arange(4.0), ("time",))
        >>> shift(series, time=1).data
        array([nan,  0.,  1.,  2.])
        >>> shift(DimArray(np.arange(4), ("time",)), time=-2, fill_value=-1).data
        array([ 2,  3, -1, -1])
    """
    adapter = adapter_of(x, "shift")
    dims = adapter.dims(x)
    axes = axes_of(dims, list(shifts), "shift")
    data = adapter.data(x)
    xp = namespace_of(data)
    if fill_value is None and not _holds_nan(xp, data.dtype):
        raise _no_nan_fill("shift", "fill_value", data.dtype)
    fast = fast_path(adapter, "shift")
    if fast is not None:
        return fast(x, dict(shifts), fill_value=fill_value)
    fill = float("nan") if fill_value is None else fill_value
    shifted = data
    for axis, offset in zip(axes, shifts.values(), strict=True):
        size = shifted.shape[axis]
        vacated = min(abs(offset), size)
        if vacated == 0:
            continue
        # the entries that stay: the first N - |k| for k > 0, the last for k < 0
        kept = slice(0, size - vacated) if offset > 0 else slice(vacated, size)
        index = tuple(kept if a == axis else slice(None) for a in range(data.ndim))
        filled_shape = tuple(
            vacated if a == axis else n for a, n in enumerate(shifted.shape)
        )
        filled = xp.full(filled_shape, fill, dtype=data.dtype)
        parts = [filled, shifted[index]] if offset > 0 else [shifted[index], filled]
        shifted = xp.concat(parts, axis=axis)
    return adapter.rewrap(x, shifted, dims)

split_dims(x, /, **dims)

Split one dim into several, in place, in C order (xarray's unstack).

y_{…, i, j} = x_{…, p}, with p = i·X + j for pixel={"y": Y, "x": X}.

The sizes are given, not read from the labels, so they are static under jit (A14). Tuple labels from merge_dims are split back.

Parameters:

Name Type Description Default
x Any

A named carrier.

required
**dims Mapping[str, int]

dim={part: size, …} for each dim to split; the sizes must multiply to the dim's length. A part may reuse the dim's name (time={"time": 4, "window": 3}).

{}

Raises:

Type Description
KeyError

A dim to split is not a dim of x.

ValueError

The sizes do not multiply to the dim's length, a split lists no part, or the result repeats a dim name.

TypeError

The adapter has no relabel and a part has labels.

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> pixels = DimArray(np.zeros((2, 12)), ("band", "pixel"))
>>> split_dims(pixels, pixel={"y": 3, "x": 4}).dims
('band', 'y', 'x')
>>> cube = DimArray(np.zeros((3, 4)), ("y", "x"), {"x": tuple("abcd")})
>>> back = split_dims(merge_dims(cube, p=("y", "x")), p={"y": 3, "x": 4})
>>> back.labels
{'x': ('a', 'b', 'c', 'd')}
Source code in packages/pipekit-array/src/pipekit_array/named/_shape.py
def split_dims(x: Any, /, **dims: Mapping[str, int]) -> Any:
    """Split one dim into several, in place, in C order (xarray's ``unstack``).

    y_{…, i, j} = x_{…, p}, with p = i·X + j for ``pixel={"y": Y, "x": X}``.

    The sizes are given, not read from the labels, so they are static
    under ``jit`` (A14). Tuple labels from `merge_dims` are split back.

    Args:
        x: A named carrier.
        **dims: ``dim={part: size, …}`` for each dim to split; the sizes
            must multiply to the dim's length. A part may reuse the
            dim's name (``time={"time": 4, "window": 3}``).

    Raises:
        KeyError: A dim to split is not a dim of ``x``.
        ValueError: The sizes do not multiply to the dim's length, a split
            lists no part, or the result repeats a dim name.
        TypeError: The adapter has no ``relabel`` and a part has labels.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> pixels = DimArray(np.zeros((2, 12)), ("band", "pixel"))
        >>> split_dims(pixels, pixel={"y": 3, "x": 4}).dims
        ('band', 'y', 'x')
        >>> cube = DimArray(np.zeros((3, 4)), ("y", "x"), {"x": tuple("abcd")})
        >>> back = split_dims(merge_dims(cube, p=("y", "x")), p={"y": 3, "x": 4})
        >>> back.labels
        {'x': ('a', 'b', 'c', 'd')}
    """
    adapter = adapter_of(x, "split_dims")
    current = adapter.dims(x)
    axes_of(current, list(dims), "split_dims")
    parts = {name: dict(part) for name, part in dims.items()}
    data = adapter.data(x)
    sizes = dict(zip(current, data.shape, strict=True))
    for name, part in parts.items():
        if not part:
            raise ValueError(f"split_dims: {name}= lists no part.")
        product = math.prod(part.values())
        if product != sizes[name]:
            raise ValueError(
                f"split_dims: the sizes {part} multiply to {product}, but "
                f"{name!r} has size {sizes[name]}."
            )
    new_dims = tuple(p for d in current for p in parts.get(d, {d: 0}))
    if len(set(new_dims)) != len(new_dims):
        raise ValueError(f"split_dims: the new dims {new_dims} repeat a name.")
    fast = fast_path(adapter, "split_dims")
    if fast is not None:
        return fast(x, parts)
    xp = namespace_of(data)
    shape = tuple(
        n for d in current for n in (parts[d].values() if d in parts else (sizes[d],))
    )
    out = adapter.rewrap(x, xp.reshape(data, shape), new_dims)
    labels: dict[str, Labels] = {}
    for name, part in parts.items():
        recovered = split_labels(adapter.labels(x, name), tuple(part.values()))
        labels.update(zip(part, recovered, strict=True))
    return set_labels(adapter, out, labels, "split_dims")

squeeze(x, dim)

Drop named length-1 dims.

y_{…} = x_{…, 0, …}.

dim is required: squeezing every length-1 dim would also drop a batch of one (A12).

Parameters:

Name Type Description Default
x Any

A named carrier.

required
dim str | Sequence[str]

A dim name, or several; each must have size 1.

required

Raises:

Type Description
KeyError

A name is not a dim of x.

ValueError

A named dim's size is not 1.

Example

import numpy as np from pipekit_array import DimArray squeeze(DimArray(np.zeros((1, 2, 4)), ("time", "band", "x")), "time").dims ('band', 'x')

Source code in packages/pipekit-array/src/pipekit_array/named/_shape.py
def squeeze(x: Any, dim: str | Sequence[str], /) -> Any:
    """Drop named length-1 dims.

    y_{…} = x_{…, 0, …}.

    ``dim`` is required: squeezing every length-1 dim would also drop a
    batch of one (A12).

    Args:
        x: A named carrier.
        dim: A dim name, or several; each must have size 1.

    Raises:
        KeyError: A name is not a dim of ``x``.
        ValueError: A named dim's size is not 1.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> squeeze(DimArray(np.zeros((1, 2, 4)), ("time", "band", "x")), "time").dims
        ('band', 'x')
    """
    adapter = adapter_of(x, "squeeze")
    names = (dim,) if isinstance(dim, str) else tuple(dim)
    dims = adapter.dims(x)
    axes = axes_of(dims, names, "squeeze")
    shape = adapter.data(x).shape
    wrong = [
        f"{n!r} has size {shape[a]}"
        for n, a in zip(names, axes, strict=True)
        if shape[a] != 1
    ]
    if wrong:
        raise ValueError(f"squeeze: only length-1 dims drop; {', '.join(wrong)}.")
    fast = fast_path(adapter, "squeeze")
    if fast is not None:
        return fast(x, names)
    return adapter.isel(x, dict.fromkeys(names, 0))

stack(xs, dim)

A new leading dim dim from carriers with the same dims; never aligns.

Source code in packages/pipekit-array/src/pipekit_array/named/_join.py
def stack(xs: Sequence[Any], dim: str) -> Any:
    """A new leading dim ``dim`` from carriers with the same dims; never aligns."""
    if len(xs) == 0:
        raise ValueError("stack needs at least one carrier.")
    adapter = adapter_of(xs[0], "stack")
    fast = fast_path(adapter, "stack")
    if fast is not None:
        return fast(list(xs), dim)
    checked = adapter.check_compatible(*xs)
    dims = _same_dims(adapter, checked, "stack")
    if dim in dims:
        raise ValueError(f"stack: {dim!r} is already a dim of {dims}; use concat.")
    datas = join_backends([adapter.data(x) for x in checked], "stack")
    xp = namespace_of(datas[0])
    return adapter.rewrap(checked[0], xp.stack(datas), (dim, *dims))

stack_items(xs, dim)

Items along a new leading dim dim, each keeping its own geometry.

The inverse of the split map_over makes: where stack requires one shared grid (geoarray's stack never aligns two), stack_items lets each item carry its own per-item leaves (a patch's transform row, a frame's grid) and makes dim a batch dim. A carrier family with no per-item geometry (DimArray, xr.DataArray) has nothing to keep apart, so this is stack; geoarray's adapter provides the stack_items fast path.

Raises:

Type Description
ValueError

xs is empty, or the items' dims differ.

Example

import numpy as np from pipekit_array import DimArray items = [DimArray(np.ones((2, 3)), ("y", "x")) for _ in range(4)] stack_items(items, "sample").dims ('sample', 'y', 'x')

Source code in packages/pipekit-array/src/pipekit_array/named/_join.py
def stack_items(xs: Sequence[Any], dim: str) -> Any:
    """Items along a new leading dim ``dim``, each keeping its own geometry.

    The inverse of the split `map_over` makes: where `stack` requires one
    shared grid (geoarray's `stack` never aligns two), ``stack_items`` lets
    each item carry its own per-item leaves (a patch's transform row, a
    frame's grid) and makes ``dim`` a batch dim. A carrier family with no
    per-item geometry (`DimArray`, ``xr.DataArray``) has nothing to keep
    apart, so this is `stack`; geoarray's adapter provides the
    ``stack_items`` fast path.

    Raises:
        ValueError: ``xs`` is empty, or the items' dims differ.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> items = [DimArray(np.ones((2, 3)), ("y", "x")) for _ in range(4)]
        >>> stack_items(items, "sample").dims
        ('sample', 'y', 'x')
    """
    if len(xs) == 0:
        raise ValueError("stack_items needs at least one carrier.")
    adapter = adapter_of(xs[0], "stack_items")
    fast = fast_path(adapter, "stack_items")
    if fast is not None:
        return fast(list(xs), dim)
    return stack(xs, dim)

take_along(x, index, dim)

Gather one entry of dim per position, with an index carrier.

index has x's dims without dim (an argreduce result, in any dim order); the result has those dims too.

Example

import numpy as np from pipekit_array import DimArray cube = DimArray(np.arange(24.0).reshape(3, 2, 4), ("time", "band", "x")) best = DimArray(np.array([0, 2, 1, 0]), ("x",)) take_along(cube, best, "time").dims ('band', 'x')

Source code in packages/pipekit-array/src/pipekit_array/named/_select.py
def take_along(x: Any, index: Any, dim: str) -> Any:
    """Gather one entry of ``dim`` per position, with an index carrier.

    ``index`` has ``x``'s dims without ``dim`` (an `argreduce` result, in any
    dim order); the result has those dims too.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> cube = DimArray(np.arange(24.0).reshape(3, 2, 4), ("time", "band", "x"))
        >>> best = DimArray(np.array([0, 2, 1, 0]), ("x",))
        >>> take_along(cube, best, "time").dims
        ('band', 'x')
    """
    adapter = adapter_of(x, "take_along")
    fast = fast_path(adapter, "take_along")
    if fast is not None:
        return fast(x, index, dim)
    index_adapter = adapter_of(index, "take_along")
    dims = adapter.dims(x)
    (axis,) = axes_of(dims, (dim,), "take_along")
    rest = tuple(d for d in dims if d != dim)
    index_dims = index_adapter.dims(index)
    if not set(index_dims) <= set(rest):
        raise ValueError(f"take_along: index dims {index_dims} are not among {rest}.")
    # a JAX index next to NumPy data would be converted (or fail under jit)
    datas = [adapter.data(x), index_adapter.data(index)]
    data, idx = join_backends(datas, "take_along")
    xp = namespace_of(data)
    # index → x's dim order with dim as a size-1 axis; missing dims broadcast
    check_index_labels(adapter, x, index_adapter, index, index_dims)
    sizes = dict(zip(dims, data.shape, strict=True))
    for d, n in zip(index_dims, idx.shape, strict=True):
        if n != sizes[d]:
            raise ValueError(
                f"take_along: index dim {d!r} has size {n}, but {d!r} has size "
                f"{sizes[d]}; the index is never broadcast."
            )
    present = [d for d in rest if d in index_dims]
    idx = xp.permute_dims(idx, tuple(index_dims.index(d) for d in present))
    full = tuple(1 if d == dim or d not in index_dims else None for d in dims)
    for position, size in enumerate(full):
        if size == 1:
            idx = xp.expand_dims(idx, axis=position)
    shape = tuple(1 if d == dim else n for d, n in zip(dims, data.shape, strict=True))
    idx = xp.broadcast_to(idx, shape)
    gathered = xp.take_along_axis(data, idx, axis=axis)
    out = xp.squeeze(gathered, axis=axis)
    return adapter.rewrap(x, out, rest)

to_rows(x, sample_dims)

x as a (n_samples, n_features) table, and how to undo it.

Parameters:

Name Type Description Default
x Any

A named carrier.

required
sample_dims Sequence[str]

The dims whose entries are samples (rows); every other dim of x is a feature (column). At least one.

required

Returns:

Type Description
Any

The table, on x's backend, and the RowLayout for from_rows.

RowLayout

With no feature dims the table has one column.

Raises:

Type Description
TypeError

x is not a named carrier.

ValueError

No sample dims, a repeated one, or one x lacks.

Example

import numpy as np from pipekit_array import DimArray scene = DimArray(np.arange(24.0).reshape(2, 3, 4), ("band", "y", "x")) rows, layout = to_rows(scene, sample_dims=("y", "x")) rows.shape, layout.feature_dims ((12, 2), ('band',))

Source code in packages/pipekit-array/src/pipekit_array/named/_rows.py
def to_rows(x: Any, sample_dims: Sequence[str]) -> tuple[Any, RowLayout]:
    """``x`` as a ``(n_samples, n_features)`` table, and how to undo it.

    Args:
        x: A named carrier.
        sample_dims: The dims whose entries are samples (rows); every other
            dim of ``x`` is a feature (column). At least one.

    Returns:
        The table, on ``x``'s backend, and the `RowLayout` for `from_rows`.
        With no feature dims the table has one column.

    Raises:
        TypeError: ``x`` is not a named carrier.
        ValueError: No sample dims, a repeated one, or one ``x`` lacks.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> scene = DimArray(np.arange(24.0).reshape(2, 3, 4), ("band", "y", "x"))
        >>> rows, layout = to_rows(scene, sample_dims=("y", "x"))
        >>> rows.shape, layout.feature_dims
        ((12, 2), ('band',))
    """
    adapter = adapter_of(x, "to_rows")
    dims = tuple(adapter.dims(x))
    wanted = tuple(sample_dims)
    if not wanted:
        raise ValueError("to_rows needs at least one sample dim.")
    if len(set(wanted)) != len(wanted):
        raise ValueError(f"to_rows: sample dims {wanted} repeat a name.")
    missing = [d for d in wanted if d not in dims]
    if missing:
        raise ValueError(f"to_rows: sample dims {missing} are not in {dims}.")
    samples = tuple(d for d in dims if d in wanted)
    features = tuple(d for d in dims if d not in wanted)
    sizes = tuple(int(n) for n in adapter.data(x).shape)
    layout = RowLayout(dims, sizes, samples, features, template=x)
    data = adapter.data(transpose(x, (*samples, *features)))
    xp = namespace_of(data)
    rows = xp.reshape(data, (layout.n_samples, layout.n_features))
    return rows, layout

transpose(x, order)

Reorder dims by name; order names every dim once.

Source code in packages/pipekit-array/src/pipekit_array/named/_shape.py
def transpose(x: Any, order: Sequence[str]) -> Any:
    """Reorder dims by name; ``order`` names every dim once."""
    adapter = adapter_of(x, "transpose")
    fast = fast_path(adapter, "transpose")
    if fast is not None:
        return fast(x, tuple(order))
    dims = adapter.dims(x)
    if sorted(order) != sorted(dims):
        raise ValueError(
            f"transpose: order {tuple(order)} is not a permutation of {dims}."
        )
    data = adapter.data(x)
    xp = namespace_of(data)
    moved = xp.permute_dims(data, axes_of(dims, order, "transpose"))
    return adapter.rewrap(x, moved, tuple(order))

vmapped_dims(dims, shape)

Static dims matched to leaves jax.vmap added or removed an axis from.

vmap maps leaves but not static dims: inside, the data has lost its leading axis; outside, the results gained one. With the default in_axes=0 / out_axes=0 that axis is the first, so drop the first dims, or prepend "batch" ("batch_1", … if taken). A shape that already matches, or no shape (a tracing placeholder), leaves dims alone. Map over any other axis with map_over.

A carrier's pytree stores core_dims as its static dims and calls this when it is unflattened.

Source code in packages/pipekit-array/src/pipekit_array/named/_carrier.py
def vmapped_dims(dims: tuple[str, ...], shape: Any) -> tuple[str, ...]:
    """Static dims matched to leaves ``jax.vmap`` added or removed an axis from.

    ``vmap`` maps leaves but not static dims: inside, the data has lost
    its leading axis; outside, the results gained one. With the default
    ``in_axes=0`` / ``out_axes=0`` that axis is the first, so drop the
    first dims, or prepend ``"batch"`` (``"batch_1"``, … if taken). A
    shape that already matches, or no shape (a tracing placeholder),
    leaves ``dims`` alone. Map over any other axis with `map_over`.

    A carrier's pytree stores `core_dims` as its static dims and calls this
    when it is unflattened.
    """
    if shape is None or not hasattr(shape, "__len__") or len(shape) == len(dims):
        return dims
    if len(shape) < len(dims):
        return dims[len(dims) - len(shape) :]
    return (*_batch_names(len(shape) - len(dims), dims), *dims)

where(x, cond, /, other=None)

Keep x where cond is True, other elsewhere (xarray's where).

y = x where v, else the fill; True keeps (A16).

cond aligns by dim name, as reduce(where=) does: a named mask whose dims are among x's, so a time y x cloud mask applies to a time band y x cube; or a plain array covering x's leading dims. It is never right-broadcast.

Parameters:

Name Type Description Default
x Any

A named carrier.

required
cond Any

A bool mask v, named or plain.

required
other float | None

The fill where cond is False; None is NaN.

None

Raises:

Type Description
ValueError

A named cond has a dim x lacks or a size that differs, or a plain one does not match x's leading dims.

TypeError

Integer or bool data without other.

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> cube = DimArray(np.ones((2, 3, 2)), ("time", "band", "x"))
>>> valid = DimArray(np.array([[True, False], [True, True]]), ("time", "x"))
>>> clear = where(cube, valid)
>>> clear.dims, clear.data[0, :, 1]
(('time', 'band', 'x'), array([nan, nan, nan]))
>>> where(cube, valid, 0.0).data[0, 0]
array([1., 0.])
Source code in packages/pipekit-array/src/pipekit_array/named/_shift.py
def where(x: Any, cond: Any, /, other: float | None = None) -> Any:
    """Keep ``x`` where ``cond`` is True, ``other`` elsewhere (xarray's ``where``).

    y = x where v, else the fill; True keeps (A16).

    ``cond`` aligns by dim name, as ``reduce(where=)`` does: a named mask
    whose dims are among ``x``'s, so a ``time y x`` cloud mask applies to a
    ``time band y x`` cube; or a plain array covering ``x``'s leading dims.
    It is never right-broadcast.

    Args:
        x: A named carrier.
        cond: A bool mask v, named or plain.
        other: The fill where ``cond`` is False; None is NaN.

    Raises:
        ValueError: A named ``cond`` has a dim ``x`` lacks or a size that
            differs, or a plain one does not match ``x``'s leading dims.
        TypeError: Integer or bool data without ``other``.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> cube = DimArray(np.ones((2, 3, 2)), ("time", "band", "x"))
        >>> valid = DimArray(np.array([[True, False], [True, True]]), ("time", "x"))
        >>> clear = where(cube, valid)
        >>> clear.dims, clear.data[0, :, 1]
        (('time', 'band', 'x'), array([nan, nan, nan]))
        >>> where(cube, valid, 0.0).data[0, 0]
        array([1., 0.])
    """
    adapter = adapter_of(x, "where")
    dims = adapter.dims(x)
    data = adapter.data(x)
    mask = aligned_mask(cond, dims, data, "where")
    x, data, mask = join_mask(adapter, x, data, mask, "where")
    xp = namespace_of(data)
    if other is None and not _holds_nan(xp, data.dtype):
        raise _no_nan_fill("where", "other", data.dtype)
    fast = fast_path(adapter, "where")
    if fast is not None:
        return fast(x, mask, other)
    fill = xp.asarray(float("nan") if other is None else other, dtype=data.dtype)
    # y = x if v else the fill
    kept = xp.where(mask, data, fill)
    return adapter.rewrap(x, kept, dims)

window(x, /, *, window_dim='window', stride=1, **sizes)

Sliding windows along one dim; the window becomes a new last dim.

w_{…, n, …, r} = x_{…, n·s + r, …}, 0 ≤ r < S, 0 ≤ n < ⌊(N - S)/s⌋ + 1.

Full windows only: there is no NaN fill, so float32 stays float32 and integers work (A15). Window n carries the label of its last entry, position n·s + S - 1, as xarray's rolling(...).construct(window_dim).isel(dim=slice(S - 1, None, s)) does. For xarray's length-N result, pad the dim by S - 1 first.

Algorithm
  1. Count the full windows, n = ⌊(N - S)/s⌋ + 1.
  2. Take the S strided slices of the data, x_{r::s}, each n long.
  3. Stack them on a new last axis and rewrap on the slice of end positions, so the windowed dim keeps the labels of those.

The generic path copies the data S times; the xarray adapter's fast path is a view.

Parameters:

Name Type Description Default
x Any

A named carrier.

required
window_dim str

The new last dim, indexing the entries r of a window.

'window'
stride int

The step s ≥ 1 between window starts.

1
**sizes int

dim=S for the one dim to window, with 1 ≤ S ≤ N.

{}

Raises:

Type Description
KeyError

The dim is not a dim of x.

ValueError

sizes names no dim or several, S or s is not positive, S exceeds N, or window_dim is already a dim.

Examples:

>>> import numpy as np
>>> from pipekit_array import DimArray
>>> ndvi = DimArray(np.arange(12.0), ("time",), {"time": tuple(range(12))})
>>> windows = window(ndvi, time=3, stride=2)
>>> windows.dims, windows.shape
(('time', 'window'), (5, 3))
>>> windows.labels["time"]
(2, 4, 6, 8, 10)
>>> window(DimArray(np.arange(4), ("time",)), time=2).data.tolist()
[[0, 1], [1, 2], [2, 3]]
Source code in packages/pipekit-array/src/pipekit_array/named/_window.py
def window(
    x: Any, /, *, window_dim: str = "window", stride: int = 1, **sizes: int
) -> Any:
    """Sliding windows along one dim; the window becomes a new last dim.

    w_{…, n, …, r} = x_{…, n·s + r, …},  0 ≤ r < S,  0 ≤ n < ⌊(N - S)/s⌋ + 1.

    Full windows only: there is no NaN fill, so float32 stays float32 and
    integers work (A15). Window ``n`` carries the label of its last entry,
    position n·s + S - 1, as xarray's
    ``rolling(...).construct(window_dim).isel(dim=slice(S - 1, None, s))``
    does. For xarray's length-N result, `pad` the dim by S - 1 first.

    Algorithm:
        1. Count the full windows, n = ⌊(N - S)/s⌋ + 1.
        2. Take the S strided slices of the data, x_{r::s}, each n long.
        3. Stack them on a new last axis and rewrap on the slice of end
           positions, so the windowed dim keeps the labels of those.

    The generic path copies the data S times; the xarray adapter's fast
    path is a view.

    Args:
        x: A named carrier.
        window_dim: The new last dim, indexing the entries r of a window.
        stride: The step s ≥ 1 between window starts.
        **sizes: ``dim=S`` for the one dim to window, with 1 ≤ S ≤ N.

    Raises:
        KeyError: The dim is not a dim of ``x``.
        ValueError: ``sizes`` names no dim or several, S or s is not
            positive, S exceeds N, or ``window_dim`` is already a dim.

    Examples:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> ndvi = DimArray(np.arange(12.0), ("time",), {"time": tuple(range(12))})
        >>> windows = window(ndvi, time=3, stride=2)
        >>> windows.dims, windows.shape
        (('time', 'window'), (5, 3))
        >>> windows.labels["time"]
        (2, 4, 6, 8, 10)
        >>> window(DimArray(np.arange(4), ("time",)), time=2).data.tolist()
        [[0, 1], [1, 2], [2, 3]]
    """
    adapter = adapter_of(x, "window")
    if len(sizes) != 1:
        raise ValueError(f"window: give one dim=size, got {sizes}.")
    ((dim, size),) = sizes.items()
    dims = adapter.dims(x)
    (axis,) = axes_of(dims, (dim,), "window")
    if window_dim in dims:
        raise ValueError(f"window: {window_dim!r} is already a dim of {dims}.")
    if size < 1 or stride < 1:
        raise ValueError(
            f"window: size and stride must be positive, got {dim}={size}, "
            f"stride={stride}."
        )
    data = adapter.data(x)
    length = data.shape[axis]
    if size > length:
        raise ValueError(
            f"window: a window of {size} does not fit {dim!r} of size {length}."
        )
    fast = fast_path(adapter, "window")
    if fast is not None:
        return fast(x, dim, size, window_dim=window_dim, stride=stride)
    xp = namespace_of(data)
    # n = ⌊(N - S)/s⌋ + 1
    count = (length - size) // stride + 1
    # the last start is (n - 1)·s
    last_start = (count - 1) * stride
    leading = (slice(None),) * axis
    # x_{r::s}, n entries each, for 0 ≤ r < S
    strided = [
        data[(*leading, slice(r, r + last_start + 1, stride))] for r in range(size)
    ]
    # w_{…, n, …, r} = x_{…, n·s + r, …}
    stacked = xp.stack(strided, axis=-1)
    # window n carries the label of position n·s + S - 1
    ends = adapter.isel(x, {dim: slice(size - 1, size + last_start, stride)})
    out = adapter.rewrap(ends, stacked, (*dims, window_dim))
    return set_labels(adapter, out, {window_dim: None}, "window")

Kernels

pipekit_array.kernels runs one array function on raw arrays and on every carrier.

pipekit_array.kernels

One array function on every carrier: KernelSpec, KernelOp, elementwise.

A shapes-only kernel declares the axes it consumes and produces with a generalized-ufunc signature ("(c)->()", "(r,c),(c),(c,d)->(r)"), the convention NumPy (np.vectorize), JAX (jnp.vectorize) and xarray (apply_ufunc core dims) already share. KernelOp wraps the kernel once and runs it:

raw arrays        → the kernel, called as is
named carriers    → core dims moved last by name, the kernel on the
                    data, the result rewrapped by the adapter
                    (NumPy eagerly, JAX traced under jit)
an adapter with   → that adapter's own path (the xarray adapter:
``apply_kernel``    one ``apply_ufunc``, lazy on dask)

Kernel conventions:

  1. Consumed axes last. Core dims arrive as the trailing axes.
  2. Broadcast over leading axes, or set vectorize=True and pay for np.vectorize / jnp.vectorize.
  3. Non-carrier inputs are keyword parameters (a background mean, a threshold), passed whole and never mapped. Geometry derived from the carrier (geometry=) is passed the same way; geometry that varies per pixel has the carrier's dims and is a positional input instead.
  4. Write against the array namespace (xp.linalg.solve) to run on every backend. A kernel that needs a JAX-only library (lineax) declares backends=("jax",) (spike finding F3).

Rules from the spike (geotoolz#516):

  • Outputs keep the first input's dim order (F2): a gufunc puts core dims last, so each output is transposed back, with dims new to the output last.
  • Every output has the loop dims of all the inputs. An output that ignores an input is broadcast (#246).
  • A DataArray's name and attributes are dropped, because the kernel changes the quantity and its unit. Coordinates keep theirs, and other carriers keep what their adapter's rewrap keeps.
  • geometry= needs one geometry per call (F4): on a carrier whose adapter reports batch_dims (per-item transforms), KernelOp raises and names map_over.
  • An output-only core dim declares its size (F5): KernelSpec.sizes maps it to an input core dim, so a lazy backend can plan the output.

Carriers of different kinds combine by dim name and size (a geo carrier "band row col" with a DimArray "col band"); grid checks (check_compatible) run between carriers of the same kind.

elementwise is the decorator for a function of plain arrays that maps each element: it aligns labelled arrays by name and calls the function on their data. It needs pipekit-array[numpy,compat] and is imported on first use.

KernelOp

Bases: Operator

An L1 kernel wrapped once, run on raw arrays and on every named carrier.

Parameters:

Name Type Description Default
kernel Callable[..., Any]

The L1 function: arrays (core dims last) and keyword params in, arrays out.

required
spec KernelSpec | str

A KernelSpec, or its signature string.

required
core dict[str, str]

Each core letter of the spec → the carrier dim it stands for, e.g. {"c": "band"}; the same kernel serves "band" and "variable".

required
geometry dict[str, Callable[[Any], Any]] | None

Kernel keyword → a function of the first carrier returning arrays ({"spacing": spacing_of}), computed per call and passed whole.

None
params Mapping[str, Any] | None

Keyword parameters passed to every call, as a mapping: the way to pass one named core, geometry or params.

None
**kwargs Any

More keyword parameters passed to every call.

{}

Raises:

Type Description
ValueError

core misses or invents a letter of the spec, or a parameter has the name of a derived-geometry keyword.

TypeError

geometry is not a mapping of callables (pass a kernel parameter named geometry through params=).

Every output has the loop dims of all the inputs, in the first input's order. On DataArrays an output keeps the coordinates and has no name or attributes: a density is not in the pressure's units.

KernelOp holds a user function, so its config is a debug repr only (forbid_in_yaml, like pipekit_ops.Lambda); a named operator built on one by composition (geoops.NDVI) is what serialises.

Example

import numpy as np from pipekit_array import DimArray def nd(a, b, eps): ... return (a - b) / (a + b + eps) op = KernelOp(nd, "(),()->()", core={}, eps=1e-10) nir = DimArray(np.full((2, 3), 0.4), ("y", "x")) red = DimArray(np.full((2, 3), 0.1), ("y", "x")) op(nir, red).dims ('y', 'x')

Source code in packages/pipekit-array/src/pipekit_array/kernels/_op.py
@recorded("pipekit_array.kernel")
class KernelOp(Operator):
    """An L1 kernel wrapped once, run on raw arrays and on every named carrier.

    Args:
        kernel: The L1 function: arrays (core dims last) and keyword params in,
            arrays out.
        spec: A `KernelSpec`, or its signature string.
        core: Each core letter of the spec → the carrier dim it stands for,
            e.g. ``{"c": "band"}``; the same kernel serves ``"band"`` and
            ``"variable"``.
        geometry: Kernel keyword → a function of the first carrier returning
            arrays (``{"spacing": spacing_of}``), computed per call and passed
            whole.
        params: Keyword parameters passed to every call, as a mapping: the
            way to pass one named ``core``, ``geometry`` or ``params``.
        **kwargs: More keyword parameters passed to every call.

    Raises:
        ValueError: ``core`` misses or invents a letter of the spec, or a
            parameter has the name of a derived-geometry keyword.
        TypeError: ``geometry`` is not a mapping of callables (pass a kernel
            parameter named ``geometry`` through ``params=``).

    Every output has the loop dims of all the inputs, in the first input's
    order. On DataArrays an output keeps the coordinates and has no name or
    attributes: a density is not in the pressure's units.

    ``KernelOp`` holds a user function, so its config is a debug repr only
    (``forbid_in_yaml``, like `pipekit_ops.Lambda`); a named operator built on
    one by composition (``geoops.NDVI``) is what serialises.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> def nd(a, b, eps):
        ...     return (a - b) / (a + b + eps)
        >>> op = KernelOp(nd, "(),()->()", core={}, eps=1e-10)
        >>> nir = DimArray(np.full((2, 3), 0.4), ("y", "x"))
        >>> red = DimArray(np.full((2, 3), 0.1), ("y", "x"))
        >>> op(nir, red).dims
        ('y', 'x')
    """

    forbid_in_yaml: ClassVar[bool] = True

    def __init__(
        self,
        kernel: Callable[..., Any],
        spec: KernelSpec | str,
        /,
        core: dict[str, str],
        geometry: dict[str, Callable[[Any], Any]] | None = None,
        params: Mapping[str, Any] | None = None,
        **kwargs: Any,
    ) -> None:
        self.kernel = kernel
        self.spec = spec if isinstance(spec, KernelSpec) else KernelSpec(spec)
        letters = set(self.spec.letters)
        if set(core) != letters:
            raise ValueError(
                f"core must map exactly the spec's letters {sorted(letters)}, "
                f"got {sorted(core)}."
            )
        self.core = dict(core)
        if geometry is not None and not (
            isinstance(geometry, Mapping) and all(map(callable, geometry.values()))
        ):
            raise TypeError(
                "geometry= maps kernel keywords to functions of the first carrier; "
                "pass a kernel parameter named 'geometry' as params={'geometry': ...}."
            )
        self.geometry = dict(geometry or {})
        self.params = {**dict(params or {}), **kwargs}
        clash = sorted(set(self.params) & set(self.geometry))
        if clash:
            raise ValueError(
                f"{clash} are both parameters and derived-geometry keywords; "
                "the kernel can take each name once."
            )

    def _place_inputs(
        self,
        args: tuple[Any, ...],
        kwargs: dict[str, Any],
        backends: tuple[str, ...] | None = None,
    ) -> tuple[tuple[Any, ...], dict[str, Any], bool]:
        """The kernel's backends for NumPy and JAX inputs; other inputs pass on.

        ``run_kernel`` joins a mix the kernel takes, and checks dask and xarray.
        """
        if backends is None and self.backends is None:
            found = {backend_of(x) for x in args} - {None}
            runs_on = {*self.spec.backends}
            if not found <= {"numpy", "jax"} or found <= runs_on:
                return args, kwargs, False
            backends = self.spec.backends
        return super()._place_inputs(args, kwargs, backends)

    def _apply(self, *carriers: Any) -> Any:
        if len(carriers) != len(self.spec.inputs):
            raise TypeError(
                f"{self.spec.signature!r} takes {len(self.spec.inputs)} input(s), "
                f"got {len(carriers)}."
            )
        derived = derive_geometry(self.geometry, carriers[0])
        kwargs = self.params | derived
        return run_kernel(self.kernel, self.spec, self.core, carriers, kwargs)

    def get_config(self) -> dict[str, Any]:
        module = getattr(self.kernel, "__module__", "?")
        name = getattr(self.kernel, "__qualname__", repr(self.kernel))
        return {
            "kernel": f"{module}:{name}",
            "spec": self.spec.signature,
            "core": self.core,
            "geometry": sorted(self.geometry),
            "params": sorted(self.params),
        }

    def compute_output_signature(self, input_signature: Any) -> Any:
        """The output signature(s) for a one-input kernel; ``None`` otherwise.

        A two-output kernel gives a tuple of signatures. With several inputs
        the other inputs' dims are unknown here, so the answer is ``None``.
        The dtype is the one ``spec.dtypes`` declares (``"same"``: the
        input's), else unknown: a kernel may return a mask or an index.
        """
        return kernel_output_signature(self.spec, self.core, input_signature)

compute_output_signature(input_signature)

The output signature(s) for a one-input kernel; None otherwise.

A two-output kernel gives a tuple of signatures. With several inputs the other inputs' dims are unknown here, so the answer is None. The dtype is the one spec.dtypes declares ("same": the input's), else unknown: a kernel may return a mask or an index.

Source code in packages/pipekit-array/src/pipekit_array/kernels/_op.py
def compute_output_signature(self, input_signature: Any) -> Any:
    """The output signature(s) for a one-input kernel; ``None`` otherwise.

    A two-output kernel gives a tuple of signatures. With several inputs
    the other inputs' dims are unknown here, so the answer is ``None``.
    The dtype is the one ``spec.dtypes`` declares (``"same"``: the
    input's), else unknown: a kernel may return a mask or an index.
    """
    return kernel_output_signature(self.spec, self.core, input_signature)

KernelSpec dataclass

A generalized-ufunc signature over abstract core-dim letters.

Parameters:

Name Type Description Default
signature str

E.g. "(),()->()" (element-wise), "(c)->()" (consumes one axis), "(r,c)->(c),(c,d)" (two outputs).

required
backends tuple[str, ...]

The backends the kernel is written for, from "numpy", "jax", "dask", "xarray".

('numpy', 'jax', 'dask', 'xarray')
vectorize bool

The kernel handles only its core dims, and is mapped over the rest with np.vectorize / jnp.vectorize.

False
sizes tuple[tuple[str, str], ...]

Output-only core letters and the input letter whose size each has, e.g. (("d", "c"),). Pairs, not a dict, so the spec stays hashable.

()
dtypes tuple[str, ...]

Each output's dtype, for output signatures and lazy backends: a dtype name ("bool") or "same" for the first input's. Empty (the default) leaves it unknown in signatures; dask then finds it by calling the kernel once.

()

Raises:

Type Description
ValueError

The signature does not parse, sizes names a letter that is not output-only or an input letter that does not exist, or dtypes is not one entry per output.

Example

spec = KernelSpec("(r,c)->(c),(c,d)", sizes=(("d", "c"),)) spec.inputs, spec.outputs ((('r', 'c'),), (('c',), ('c', 'd')))

Source code in packages/pipekit-array/src/pipekit_array/kernels/_spec.py
@dataclasses.dataclass(frozen=True)
class KernelSpec:
    """A generalized-ufunc signature over abstract core-dim letters.

    Args:
        signature: E.g. ``"(),()->()"`` (element-wise), ``"(c)->()"``
            (consumes one axis), ``"(r,c)->(c),(c,d)"`` (two outputs).
        backends: The backends the kernel is written for, from ``"numpy"``,
            ``"jax"``, ``"dask"``, ``"xarray"``.
        vectorize: The kernel handles only its core dims, and is mapped over
            the rest with ``np.vectorize`` / ``jnp.vectorize``.
        sizes: Output-only core letters and the input letter whose size each
            has, e.g. ``(("d", "c"),)``. Pairs, not a dict, so the spec stays
            hashable.
        dtypes: Each output's dtype, for output signatures and lazy
            backends: a dtype name (``"bool"``) or ``"same"`` for the first
            input's. Empty (the default) leaves it unknown in signatures;
            dask then finds it by calling the kernel once.

    Raises:
        ValueError: The signature does not parse, ``sizes`` names a letter
            that is not output-only or an input letter that does not exist,
            or ``dtypes`` is not one entry per output.

    Example:
        >>> spec = KernelSpec("(r,c)->(c),(c,d)", sizes=(("d", "c"),))
        >>> spec.inputs, spec.outputs
        ((('r', 'c'),), (('c',), ('c', 'd')))
    """

    signature: str
    backends: tuple[str, ...] = ("numpy", "jax", "dask", "xarray")
    vectorize: bool = False
    sizes: tuple[tuple[str, str], ...] = ()
    dtypes: tuple[str, ...] = ()

    def __post_init__(self) -> None:
        if not _SIGNATURE.match(self.signature):
            raise ValueError(f"Not a gufunc signature: {self.signature!r}.")
        object.__setattr__(self, "backends", tuple(self.backends))
        object.__setattr__(self, "sizes", tuple(tuple(p) for p in self.sizes))
        object.__setattr__(self, "dtypes", tuple(map(str, self.dtypes)))
        if self.dtypes and len(self.dtypes) != len(self.outputs):
            raise ValueError(
                f"dtypes: {len(self.dtypes)} entries for {len(self.outputs)} output(s)."
            )
        in_letters = {c for group in self.inputs for c in group}
        out_only = {c for group in self.outputs for c in group} - in_letters
        declared = {out for out, _ in self.sizes}
        for out, source in self.sizes:
            if out not in out_only:
                raise ValueError(f"sizes: {out!r} is not an output-only letter.")
            if source not in in_letters:
                raise ValueError(f"sizes: {source!r} is not an input letter.")
        missing = sorted(out_only - declared)
        if missing:
            raise ValueError(
                f"Output-only core letter(s) {missing} need a size, e.g. "
                f"sizes=(({missing[0]!r}, '<input letter>'),)."
            )

    @functools.cached_property
    def inputs(self) -> tuple[tuple[str, ...], ...]:
        """Core letters of each input."""
        return _groups(self.signature.split("->")[0])

    @functools.cached_property
    def outputs(self) -> tuple[tuple[str, ...], ...]:
        """Core letters of each output."""
        return _groups(self.signature.split("->")[1])

    @functools.cached_property
    def letters(self) -> tuple[str, ...]:
        """Every core letter, in order of first appearance."""
        seen: dict[str, None] = {}
        for group in (*self.inputs, *self.outputs):
            for letter in group:
                seen.setdefault(letter, None)
        return tuple(seen)

inputs cached property

Core letters of each input.

letters cached property

Every core letter, in order of first appearance.

outputs cached property

Core letters of each output.

derive_geometry(geometry, first)

The kernel's derived-geometry keywords, computed from the first carrier.

Raises:

Type Description
ValueError

The carrier's adapter reports batch_dims (one geometry per item), but keyword inputs are passed whole (F4); the message names map_over.

Source code in packages/pipekit-array/src/pipekit_array/kernels/_run.py
def derive_geometry(
    geometry: dict[str, Callable[[Any], Any]], first: Any
) -> dict[str, Any]:
    """The kernel's derived-geometry keywords, computed from the first carrier.

    Raises:
        ValueError: The carrier's adapter reports ``batch_dims`` (one geometry
            per item), but keyword inputs are passed whole (F4); the message
            names ``map_over``.
    """
    if not geometry:
        return {}
    adapter = get_adapter(first)
    batch = getattr(adapter, "batch_dims", None)
    batched = batch(first) if batch is not None else ()
    if batched:
        raise ValueError(
            f"geometry= on a carrier batched over {batched} (one geometry per "
            "item), but keyword inputs are passed whole: use "
            f"map_over(x, op, {batched[0]!r}) so each item has its own."
        )
    return {name: fn(first) for name, fn in geometry.items()}

elementwise(*, n_outputs=1)

Let an elementwise function take and return labelled arrays.

Algorithm
  1. With no labelled array among the arguments, call the function as it is.
  2. Hand DataArrays to xarray.apply_ufunc, and other carriers with a registered adapter (DimArray, geoarray's GeoArray) to the path of KernelOp. Both align by dimension name and call the function on the data. The other arguments are passed through.
  3. Broadcast every result to the dimensions of the inputs together.
  4. Return labelled arrays with the inputs' coordinates. A DataArray has no name or attributes, because the quantity and its unit have changed; another carrier keeps what its adapter's rewrap keeps, and its missing elements stay missing (pipekit_ops.MissingValueAdapter).

An array without dimension names cannot be aligned with one that has them, so it is refused, unless it has no dimensions.

Parameters:

Name Type Description Default
n_outputs int

How many arrays the function returns; above 1, a tuple.

1

Returns:

Type Description
Callable[[F], F]

The decorator.

Raises:

Type Description
ValueError

(the decorated function) if a labelled array meets an array with dimensions but no names.

BackendError

(the decorated function) if a DataArray meets a carrier of another kind, or carriers hold arrays of several libraries other than NumPy and JAX.

Example

import numpy as np from pipekit_array import DimArray @elementwise() ... def scaled(x, slope): ... return slope * x scaled(np.asarray([1.0, 2.0]), 2.0).tolist() [2.0, 4.0] scaled(DimArray(np.asarray([1.0, 2.0]), ("x",)), 2.0).dims ('x',)

Source code in packages/pipekit-array/src/pipekit_array/kernels/_elementwise.py
def elementwise[F: Callable[..., Any]](*, n_outputs: int = 1) -> Callable[[F], F]:
    """Let an elementwise function take and return labelled arrays.

    Algorithm:
        1. With no labelled array among the arguments, call the function as
           it is.
        2. Hand DataArrays to ``xarray.apply_ufunc``, and other carriers with
           a registered adapter (`DimArray`, geoarray's ``GeoArray``) to the
           path of `KernelOp`. Both align by dimension name and call the
           function on the data. The other arguments are passed through.
        3. Broadcast every result to the dimensions of the inputs together.
        4. Return labelled arrays with the inputs' coordinates. A DataArray
           has no name or attributes, because the quantity and its unit have
           changed; another carrier keeps what its adapter's ``rewrap`` keeps,
           and its missing elements stay missing
           (`pipekit_ops.MissingValueAdapter`).

    An array without dimension names cannot be aligned with one that has
    them, so it is refused, unless it has no dimensions.

    Args:
        n_outputs: How many arrays the function returns; above 1, a tuple.

    Returns:
        The decorator.

    Raises:
        ValueError: (the decorated function) if a labelled array meets an
            array with dimensions but no names.
        BackendError: (the decorated function) if a DataArray meets a carrier
            of another kind, or carriers hold arrays of several libraries
            other than NumPy and JAX.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> @elementwise()
        ... def scaled(x, slope):
        ...     return slope * x
        >>> scaled(np.asarray([1.0, 2.0]), 2.0).tolist()
        [2.0, 4.0]
        >>> scaled(DimArray(np.asarray([1.0, 2.0]), ("x",)), 2.0).dims
        ('x',)
    """

    def decorate(function: F) -> F:
        signature = inspect.signature(function)

        @functools.wraps(function)
        def wrapper(*args: Any, **kwargs: Any) -> Any:
            given = [v for v in (*args, *kwargs.values()) if not is_host(v)]
            if any(map(is_dataarray, given)):
                apply = _apply_xarray
            elif any(map(_is_carrier, given)):
                apply = _apply_carriers
            else:
                return function(*args, **kwargs)
            bound = signature.bind(*args, **kwargs)
            values = {**dict(enumerate(bound.args)), **bound.kwargs}
            return apply(_Call(function, signature, values), n_outputs)

        return cast(Any, wrapper)

    return decorate

kernel_output_dims(spec, core, input_dims)

The dims of each output, from the inputs' dims, without running.

Loop dims are every input's dims minus its core dims, in order of first appearance. Output k has the loop dims plus its core dims, in the first input's order, with dims new to the output last (F2).

Source code in packages/pipekit-array/src/pipekit_array/kernels/_spec.py
def kernel_output_dims(
    spec: KernelSpec, core: dict[str, str], input_dims: Sequence[tuple[str, ...]]
) -> tuple[tuple[str, ...], ...]:
    """The dims of each output, from the inputs' dims, without running.

    Loop dims are every input's dims minus its core dims, in order of first
    appearance. Output k has the loop dims plus its core dims, in the first
    input's order, with dims new to the output last (F2).
    """
    loop: list[str] = []
    for dims, group in zip(input_dims, spec.inputs, strict=True):
        cores = {core[c] for c in group}
        for d in dims:
            if d not in cores and d not in loop:
                loop.append(d)
    _check_core_not_loop(loop, [tuple(core[c] for c in g) for g in spec.inputs])
    first = tuple(input_dims[0]) if input_dims else ()
    outputs = []
    for group in spec.outputs:
        dims = (*loop, *(core[c] for c in group))
        ordered = [d for d in first if d in dims] + [d for d in dims if d not in first]
        outputs.append(tuple(ordered))
    return tuple(outputs)

kernel_output_signature(spec, core, input_signature)

KernelOp.compute_output_signature for any operator running spec.

Shared with pipekit_jax.JaxKernelOp, so a converted operator reports the same dims, sizes, dtypes and output count.

Source code in packages/pipekit-array/src/pipekit_array/kernels/_spec.py
def kernel_output_signature(
    spec: KernelSpec, core: dict[str, str], input_signature: Any
) -> Any:
    """`KernelOp.compute_output_signature` for any operator running ``spec``.

    Shared with ``pipekit_jax.JaxKernelOp``, so a converted operator reports
    the same dims, sizes, dtypes and output count.
    """
    sig = signature_of(input_signature)
    if not isinstance(sig, Signature) or len(spec.inputs) != 1:
        return None
    sizes = dict(sig.dims)
    names = tuple(sizes)
    letter_size = {}
    for letter, dim in core.items():
        if dim in sizes:
            letter_size[letter] = sizes[dim]
    for out, source in spec.sizes:
        letter_size[out] = letter_size.get(source)
    dim_size = {core[k]: v for k, v in letter_size.items()} | {
        d: s for d, s in sizes.items()
    }
    dtypes = [sig.dtype if d == "same" else d for d in spec.dtypes] or [None] * len(
        spec.outputs
    )
    outs = tuple(
        Signature(dims=tuple((d, dim_size.get(d)) for d in dims), dtype=dtype)
        for dims, dtype in zip(
            kernel_output_dims(spec, core, [names]), dtypes, strict=True
        )
    )
    return outs if len(outs) > 1 else outs[0]

run_kernel(kernel, spec, core, carriers, kwargs)

Run kernel on raw arrays or named carriers; the dispatch behind KernelOp.

Raises:

Type Description
BackendError

Carriers on several backends other than NumPy and JAX, or a backend the spec does not list.

TypeError

A raw array next to named carriers.

ValueError

A core dim is missing from its input, or a dim has two sizes.

Source code in packages/pipekit-array/src/pipekit_array/kernels/_run.py
def run_kernel(
    kernel: Callable[..., Any],
    spec: KernelSpec,
    core: dict[str, str],
    carriers: Sequence[Any],
    kwargs: dict[str, Any],
) -> Any:
    """Run ``kernel`` on raw arrays or named carriers; the dispatch behind `KernelOp`.

    Raises:
        BackendError: Carriers on several backends other than NumPy and JAX,
            or a backend the spec does not list.
        TypeError: A raw array next to named carriers.
        ValueError: A core dim is missing from its input, or a dim has two
            sizes.
    """
    adapters = [get_adapter(x) for x in carriers]
    if all(a is None for a in adapters):
        arrays, backend = _one_backend(carriers, kernel, spec)
        return _call(kernel, spec, kwargs, backend)(*arrays)
    for i, adapter in enumerate(adapters):
        if adapter is None:
            raise TypeError(
                f"input {i} is a raw array next to named carriers; wrap it as a "
                "pipekit_array.DimArray with its dims."
            )
    named: list[NamedArrayAdapter] = [a for a in adapters if a is not None]
    # a class attribute check: isinstance against a runtime Protocol costs ~10 µs
    own = [cast(_RunsKernels, a) for a in named if hasattr(type(a), "apply_kernel")]
    if own:
        if len({type(a) for a in named}) > 1:
            raise BackendError(
                f"{type(own[0]).__name__} carriers cannot mix with others in one "
                "kernel call; convert them to one family first."
            )
        return own[0].apply_kernel(kernel, spec, core, kwargs, *carriers)
    return _run_named(kernel, spec, core, list(carriers), named, kwargs)

pipekit_array.kernels._elementwise

Let an elementwise function of plain arrays take and return labelled arrays.

elementwise(*, n_outputs=1)

Let an elementwise function take and return labelled arrays.

Algorithm
  1. With no labelled array among the arguments, call the function as it is.
  2. Hand DataArrays to xarray.apply_ufunc, and other carriers with a registered adapter (DimArray, geoarray's GeoArray) to the path of KernelOp. Both align by dimension name and call the function on the data. The other arguments are passed through.
  3. Broadcast every result to the dimensions of the inputs together.
  4. Return labelled arrays with the inputs' coordinates. A DataArray has no name or attributes, because the quantity and its unit have changed; another carrier keeps what its adapter's rewrap keeps, and its missing elements stay missing (pipekit_ops.MissingValueAdapter).

An array without dimension names cannot be aligned with one that has them, so it is refused, unless it has no dimensions.

Parameters:

Name Type Description Default
n_outputs int

How many arrays the function returns; above 1, a tuple.

1

Returns:

Type Description
Callable[[F], F]

The decorator.

Raises:

Type Description
ValueError

(the decorated function) if a labelled array meets an array with dimensions but no names.

BackendError

(the decorated function) if a DataArray meets a carrier of another kind, or carriers hold arrays of several libraries other than NumPy and JAX.

Example

import numpy as np from pipekit_array import DimArray @elementwise() ... def scaled(x, slope): ... return slope * x scaled(np.asarray([1.0, 2.0]), 2.0).tolist() [2.0, 4.0] scaled(DimArray(np.asarray([1.0, 2.0]), ("x",)), 2.0).dims ('x',)

Source code in packages/pipekit-array/src/pipekit_array/kernels/_elementwise.py
def elementwise[F: Callable[..., Any]](*, n_outputs: int = 1) -> Callable[[F], F]:
    """Let an elementwise function take and return labelled arrays.

    Algorithm:
        1. With no labelled array among the arguments, call the function as
           it is.
        2. Hand DataArrays to ``xarray.apply_ufunc``, and other carriers with
           a registered adapter (`DimArray`, geoarray's ``GeoArray``) to the
           path of `KernelOp`. Both align by dimension name and call the
           function on the data. The other arguments are passed through.
        3. Broadcast every result to the dimensions of the inputs together.
        4. Return labelled arrays with the inputs' coordinates. A DataArray
           has no name or attributes, because the quantity and its unit have
           changed; another carrier keeps what its adapter's ``rewrap`` keeps,
           and its missing elements stay missing
           (`pipekit_ops.MissingValueAdapter`).

    An array without dimension names cannot be aligned with one that has
    them, so it is refused, unless it has no dimensions.

    Args:
        n_outputs: How many arrays the function returns; above 1, a tuple.

    Returns:
        The decorator.

    Raises:
        ValueError: (the decorated function) if a labelled array meets an
            array with dimensions but no names.
        BackendError: (the decorated function) if a DataArray meets a carrier
            of another kind, or carriers hold arrays of several libraries
            other than NumPy and JAX.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> @elementwise()
        ... def scaled(x, slope):
        ...     return slope * x
        >>> scaled(np.asarray([1.0, 2.0]), 2.0).tolist()
        [2.0, 4.0]
        >>> scaled(DimArray(np.asarray([1.0, 2.0]), ("x",)), 2.0).dims
        ('x',)
    """

    def decorate(function: F) -> F:
        signature = inspect.signature(function)

        @functools.wraps(function)
        def wrapper(*args: Any, **kwargs: Any) -> Any:
            given = [v for v in (*args, *kwargs.values()) if not is_host(v)]
            if any(map(is_dataarray, given)):
                apply = _apply_xarray
            elif any(map(_is_carrier, given)):
                apply = _apply_carriers
            else:
                return function(*args, **kwargs)
            bound = signature.bind(*args, **kwargs)
            values = {**dict(enumerate(bound.args)), **bound.kwargs}
            return apply(_Call(function, signature, values), n_outputs)

        return cast(Any, wrapper)

    return decorate

Operators

pipekit_array.operators has the operator twin of each named function, and MeanScalar, StackAlong and ConcatenateAlong for raw arrays.

pipekit_array.operators

The functions of pipekit_array.named as Operators, for pipelines and config.

Each wraps one function and computes its output Signature without running. Operators that take only names and numbers round-trip through dumps / loads; those that hold a user function (MapOver, Combine) set forbid_in_yaml, like pipekit_ops.Lambda.

Reduce and ArgReduce take the reduction by its array-namespace name ("mean", "argmax") so they serialise; the function forms also take a callable. MeanScalar, StackAlong and ConcatenateAlong run on raw arrays.

A saved pipeline records an operator under pipekit_array, wherever its file is (ADR A30).

ApplyAlong

Bases: Operator

inner on each item along dim; the results stacked along dim, first.

map_over with an Operator instead of a function, so it serialises whenever inner does: inner is stored as a nested config, and ApplyAlong itself does not set forbid_in_yaml (a flagged inner is found by the serialiser's walk). Use MapOver for a plain function. Replaces the planned ApplyToBands (A15).

Parameters:

Name Type Description Default
inner Operator

The operator applied to each item, isel(x, dim=i); it returns named carriers with the same dims.

required
dim str

The dim to map over.

required
Example

import numpy as np from pipekit_array import DimArray cube = DimArray(np.ones((3, 2, 4)), ("time", "band", "x")) ApplyAlong(Reduce("sum", dim="band"), dim="time")(cube).dims ('time', 'x')

Source code in packages/pipekit-array/src/pipekit_array/operators/_map.py
@recorded("pipekit_array.dim_operators")
class ApplyAlong(Operator):
    """``inner`` on each item along ``dim``; the results stacked along ``dim``, first.

    `map_over` with an `Operator` instead of a function, so it serialises
    whenever ``inner`` does: ``inner`` is stored as a nested config, and
    `ApplyAlong` itself does not set ``forbid_in_yaml`` (a flagged
    ``inner`` is found by the serialiser's walk). Use `MapOver` for a
    plain function. Replaces the planned ``ApplyToBands`` (A15).

    Args:
        inner: The operator applied to each item, ``isel(x, dim=i)``; it
            returns named carriers with the same dims.
        dim: The dim to map over.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> cube = DimArray(np.ones((3, 2, 4)), ("time", "band", "x"))
        >>> ApplyAlong(Reduce("sum", dim="band"), dim="time")(cube).dims
        ('time', 'x')
    """

    def __init__(self, inner: Operator, dim: str) -> None:
        require_operator(inner, "ApplyAlong", "inner")
        self.inner = inner
        self.dim = dim

    def _apply(self, x: Any) -> Any:
        return _map.map_over(x, self.inner, self.dim)

    def get_config(self) -> dict[str, Any]:
        return {"inner": nested_config(self.inner), "dim": self.dim}

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        item = self.inner.compute_output_signature(sig.drop_dims((self.dim,)))
        if not isinstance(item, Signature):
            return None
        return Signature(
            dims=((self.dim, dict(sig.dims)[self.dim]), *item.dims), dtype=item.dtype
        )

ArgReduce

Bases: Operator

The index of a reduction along one dim ("argmax", "argmin").

The output signature's dtype is None: the integer width depends on the backend.

Source code in packages/pipekit-array/src/pipekit_array/operators/_reduce.py
@recorded("pipekit_array.dim_operators")
class ArgReduce(Operator):
    """The index of a reduction along one dim (``"argmax"``, ``"argmin"``).

    The output signature's dtype is ``None``: the integer width depends on
    the backend.
    """

    def __init__(self, fn: str, dim: str) -> None:
        if not isinstance(fn, str):
            raise TypeError("ArgReduce takes the reduction's name, e.g. 'argmax'.")
        self.fn = fn
        self.dim = dim

    def _apply(self, x: Any) -> Any:
        return _reduce.argreduce(x, self.fn, self.dim)

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        return Signature(dims=sig.drop_dims((self.dim,)).dims, dtype=None)

BatchAlong

Bases: Operator

inner on chunks of size along dim of one carrier, joined back.

y = concat([inner(x_{0:m}), inner(x_{m:2m}), …], dim), m = size.

Not pipekit_ops.BatchedMap, which takes an iterable and calls its operator on lists of items: BatchAlong splits one named carrier along a dim (A19, which supersedes A5). The use case is a GPU-bound model on a cube that does not fit in device memory at once.

inner must keep dim, one output entry per input entry (a per-item operator), for the "last" padding to be trimmed and for dim's labels to come back; the result then equals inner(x).

Algorithm
  1. Cut dim into chunks of size; the last may be short.
  2. Apply pad (ADR A10) to a short last chunk: "none" keeps it short; "last" pads it to size with copies of its last item, so a jitted inner compiles once, and trims the padding from its output; "drop" drops it.
  3. Join the outputs along dim and set dim's labels to the input's (an adapter's generic concat drops them).

Parameters:

Name Type Description Default
inner Operator

The operator applied to each chunk.

required
size int

Entries per chunk; must be positive.

required
dim str

The dim to cut.

required
pad PadPolicy

"none" (default), "last" or "drop".

'none'

Raises:

Type Description
TypeError

inner is not an Operator.

ValueError

size is not positive or pad is not a policy.

Example

import numpy as np from pipekit_array import DimArray, Reduce cube = DimArray(np.ones((10, 2, 4)), ("sample", "band", "x")) per_chunk = BatchAlong(Reduce("sum", "band"), size=4, dim="sample") per_chunk(cube).dims, per_chunk(cube).shape (('sample', 'x'), (10, 4)) BatchAlong(Reduce("sum", "band"), 4, "sample", pad="drop")(cube).shape (8, 4)

Source code in packages/pipekit-array/src/pipekit_array/operators/_map.py
@recorded("pipekit_array.batching")
class BatchAlong(Operator):
    """``inner`` on chunks of ``size`` along ``dim`` of one carrier, joined back.

    y = concat([inner(x_{0:m}), inner(x_{m:2m}), …], dim),  m = ``size``.

    Not `pipekit_ops.BatchedMap`, which takes an iterable and calls its
    operator on lists of items: `BatchAlong` splits *one* named carrier
    along a dim (A19, which supersedes A5). The use case is a GPU-bound
    model on a cube that does not fit in device memory at once.

    ``inner`` must keep ``dim``, one output entry per input entry (a
    per-item operator), for the ``"last"`` padding to be trimmed and for
    ``dim``'s labels to come back; the result then equals ``inner(x)``.

    Algorithm:
        1. Cut ``dim`` into chunks of ``size``; the last may be short.
        2. Apply ``pad`` (ADR A10) to a short last chunk: ``"none"`` keeps
           it short; ``"last"`` pads it to ``size`` with copies of its
           last item, so a jitted ``inner`` compiles once, and trims the
           padding from its output; ``"drop"`` drops it.
        3. Join the outputs along ``dim`` and set ``dim``'s labels to the
           input's (an adapter's generic ``concat`` drops them).

    Args:
        inner: The operator applied to each chunk.
        size: Entries per chunk; must be positive.
        dim: The dim to cut.
        pad: ``"none"`` (default), ``"last"`` or ``"drop"``.

    Raises:
        TypeError: ``inner`` is not an `Operator`.
        ValueError: ``size`` is not positive or ``pad`` is not a policy.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray, Reduce
        >>> cube = DimArray(np.ones((10, 2, 4)), ("sample", "band", "x"))
        >>> per_chunk = BatchAlong(Reduce("sum", "band"), size=4, dim="sample")
        >>> per_chunk(cube).dims, per_chunk(cube).shape
        (('sample', 'x'), (10, 4))
        >>> BatchAlong(Reduce("sum", "band"), 4, "sample", pad="drop")(cube).shape
        (8, 4)
    """

    def __init__(
        self, inner: Operator, size: int, dim: str, pad: PadPolicy = "none"
    ) -> None:
        require_operator(inner, "BatchAlong", "inner")
        if size <= 0:
            raise ValueError(f"BatchAlong: size must be positive, got {size}.")
        if pad not in PAD_POLICIES:
            raise ValueError(
                f"BatchAlong: pad must be one of {PAD_POLICIES}, got {pad!r}."
            )
        self.inner = inner
        self.size = size
        self.dim = dim
        self.pad = pad

    def _apply(self, x: Any) -> Any:
        adapter = adapter_of(x, "BatchAlong")
        (axis,) = axes_of(adapter.dims(x), (self.dim,), "BatchAlong")
        length = adapter.data(x).shape[axis]
        outputs = []
        covered = 0
        for start in range(0, length, self.size):
            stop = min(start + self.size, length)
            missing = self.size - (stop - start)
            if missing and self.pad == "drop":
                break
            covered = stop
            if missing and self.pad == "last":
                # copies of the chunk's last item fill it to size
                positions = [*range(start, stop), *([stop - 1] * missing)]
                padded = self.inner(adapter.isel(x, {self.dim: positions}))
                outputs.append(isel(padded, **{self.dim: slice(0, stop - start)}))
                continue
            outputs.append(self.inner(adapter.isel(x, {self.dim: slice(start, stop)})))
        if not outputs:
            raise ValueError(
                f"BatchAlong: pad='drop' drops every entry of {self.dim!r} "
                f"(size {length} < {self.size})."
            )
        out = concat(outputs, self.dim)
        labels = adapter.labels(x, self.dim)
        if labels is None:
            return out
        out_adapter = adapter_of(out, "BatchAlong")
        (out_axis,) = axes_of(out_adapter.dims(out), (self.dim,), "BatchAlong")
        if out_adapter.data(out).shape[out_axis] != covered:
            return out
        # a per-item inner: entry i of the output is entry i of the input
        return set_labels(out_adapter, out, {self.dim: labels[:covered]}, "BatchAlong")

    def get_config(self) -> dict[str, Any]:
        return {
            "inner": nested_config(self.inner),
            "size": self.size,
            "dim": self.dim,
            "pad": self.pad,
        }

    def compute_output_signature(self, input_signature: Any) -> Any:
        if not isinstance(input_signature, Signature):
            return None
        length = dict(input_signature.dims)[self.dim]
        if length is None:
            return None
        chunk = input_signature.replace_dims({self.dim: min(self.size, length)})
        out = self.inner.compute_output_signature(chunk)
        if not isinstance(out, Signature) or self.dim not in dict(out.dims):
            return None
        # a per-item inner: as many entries as the input keeps
        kept = length - length % self.size if self.pad == "drop" else length
        return out.replace_dims({self.dim: kept})

BroadcastLike

Bases: Operator

Add other's missing dims to x, in front: op(x, other).

compute_output_signature takes the pair (x's, other's): from x's alone the added dims are unknown, so it returns None.

Parameters:

Name Type Description Default
exclude Sequence[str]

Dims of other neither added nor checked.

()
Source code in packages/pipekit-array/src/pipekit_array/operators/_shape.py
@recorded("pipekit_array.dim_operators")
class BroadcastLike(Operator):
    """Add ``other``'s missing dims to ``x``, in front: ``op(x, other)``.

    `compute_output_signature` takes the pair ``(x's, other's)``: from
    ``x``'s alone the added dims are unknown, so it returns None.

    Args:
        exclude: Dims of ``other`` neither added nor checked.
    """

    def __init__(self, exclude: Sequence[str] = ()) -> None:
        self.exclude = tuple(exclude)

    def _apply(self, x: Any, other: Any) -> Any:
        return _shape.broadcast_like(x, other, exclude=self.exclude)

    def get_config(self) -> dict[str, Any]:
        return {"exclude": list(self.exclude)}

    def compute_output_signature(self, input_signature: Any) -> Any:
        if not isinstance(input_signature, tuple) or len(input_signature) != 2:
            return None
        sig, other = (_signature(s) for s in input_signature)
        if sig is None or other is None:
            return None
        names = {d for d, _ in sig.dims}
        added = tuple(
            (d, n) for d, n in other.dims if d not in names and d not in self.exclude
        )
        return Signature(dims=(*added, *sig.dims), dtype=sig.dtype)

Coarsen

Bases: Operator

Reduce non-overlapping blocks along named dims by a named reduction.

Parameters:

Name Type Description Default
fn str

The reduction's name in the array namespace: "mean", "nanmean", "max", …

required
boundary str

"exact" or "trim".

'exact'
**windows int

dim=k for each dim to coarsen.

{}
Example

import numpy as np from pipekit_array import DimArray Coarsen("mean", y=2, x=2)(DimArray(np.ones((4, 6)), ("y", "x"))).shape (2, 3)

Source code in packages/pipekit-array/src/pipekit_array/operators/_window.py
@recorded("pipekit_array.dim_operators")
class Coarsen(Operator):
    """Reduce non-overlapping blocks along named dims by a named reduction.

    Args:
        fn: The reduction's name in the array namespace: ``"mean"``,
            ``"nanmean"``, ``"max"``, …
        boundary: ``"exact"`` or ``"trim"``.
        **windows: ``dim=k`` for each dim to coarsen.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> Coarsen("mean", y=2, x=2)(DimArray(np.ones((4, 6)), ("y", "x"))).shape
        (2, 3)
    """

    def __init__(self, fn: str, boundary: str = "exact", **windows: int) -> None:
        if not isinstance(fn, str):
            raise TypeError(
                "Coarsen takes the reduction's name (e.g. 'mean') so it serialises; "
                "for a function, use pipekit_array.coarsen(x, fn, ...)."
            )
        self.fn = fn
        self.boundary = boundary
        self.windows = dict(windows)

    def _apply(self, x: Any) -> Any:
        return _window.coarsen(x, self.fn, boundary=self.boundary, **self.windows)

    def get_config(self) -> dict[str, Any]:
        return {"fn": self.fn, "boundary": self.boundary, **self.windows}

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        sizes = dict(sig.dims)
        coarse: dict[str, int | None] = {}
        for d, k in self.windows.items():
            length = sizes[d]
            # ⌊N/k⌋ blocks; "exact" raises at run time unless k divides N
            coarse[d] = None if length is None else length // k
        dims = sig.replace_dims(coarse).dims
        return Signature(dims=dims, dtype=_reduced_dtype(self.fn, sig.dtype))

Combine

Bases: Operator

An element-wise function of carriers with the same dims: op(a, b, …).

Holds a user function, so its config is a debug repr only.

Source code in packages/pipekit-array/src/pipekit_array/operators/_join.py
@recorded("pipekit_array.dim_operators")
class Combine(Operator):
    """An element-wise function of carriers with the same dims: ``op(a, b, …)``.

    Holds a user function, so its config is a debug repr only.
    """

    forbid_in_yaml: ClassVar[bool] = True

    def __init__(self, fn: Callable[..., Any]) -> None:
        self.fn = fn

    def _apply(self, *xs: Any) -> Any:
        return _join.combine(self.fn, *xs)

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        # same dims; fn may change the dtype
        return Signature(dims=sig.dims, dtype=None)

ConcatenateAlong

Bases: Operator

Concatenate a list-of-arrays along an existing axis via xp.concat.

xp.concat is the Array API standardised name; numpy 2.0+ exposes it as concat alongside the legacy concatenate alias. All input arrays must share shape (except along axis), dtype, and backend; NumPy arrays join JAX ones.

Parameters:

Name Type Description Default
axis int | None

Existing axis to concatenate along, for raw arrays. Default 0.

None
dim str | None

For named carriers instead: the dim to join along (pipekit_array.concat). axis and dim are mutually exclusive.

None

Raises:

Type Description
ValueError

From _apply when the input sequence is empty (also when the inputs mix backends other than NumPy and JAX).

Example::

out = ConcatenateAlong(axis=0)([arr_a, arr_b])  # shape (2*N, H, W)
Source code in packages/pipekit-array/src/pipekit_array/operators/_join.py
@recorded("pipekit_array.combinators")
class ConcatenateAlong(Operator):
    """Concatenate a list-of-arrays along an existing axis via `xp.concat`.

    `xp.concat` is the Array API standardised name; numpy 2.0+
    exposes it as `concat` alongside the legacy `concatenate` alias.
    All input arrays must share shape (except along `axis`), dtype,
    and backend; NumPy arrays join JAX ones.

    Args:
        axis: Existing axis to concatenate along, for raw arrays. Default 0.
        dim: For named carriers instead: the dim to join along
            (`pipekit_array.concat`). ``axis`` and ``dim`` are mutually
            exclusive.

    Raises:
        ValueError: From ``_apply`` when the input sequence is empty
            (also when the inputs mix backends other than NumPy and JAX).

    Example::

        out = ConcatenateAlong(axis=0)([arr_a, arr_b])  # shape (2*N, H, W)
    """

    def __init__(self, axis: int | None = None, dim: str | None = None) -> None:
        if axis is not None and dim is not None:
            raise ValueError(
                "ConcatenateAlong takes axis= (raw arrays) or dim=, not both."
            )
        self.axis = 0 if axis is None and dim is None else axis
        self.dim = dim

    def _apply(self, xs: Sequence[Any]) -> Any:
        if len(xs) == 0:
            raise ValueError("ConcatenateAlong requires at least one input array.")
        if self.dim is not None:
            return _join.concat(list(xs), self.dim)
        arrays = _dispatch.join_backends(xs, "ConcatenateAlong")
        xp = array_namespace(*arrays)
        return xp.concat(arrays, axis=self.axis)

    def get_config(self) -> dict[str, Any]:
        if self.dim is not None:
            return {"dim": self.dim}
        return {"axis": self.axis}

CumReduce

Bases: Operator

A cumulative function along dim, by its array-namespace name.

Parameters:

Name Type Description Default
fn str

"cumulative_sum", "cumulative_prod", "nancumsum", …

required
dim str

The dim to accumulate along.

required
Example

import numpy as np from pipekit_array import DimArray CumReduce("cumulative_sum", "time")(DimArray(np.ones(3), ("time",))).data array([1., 2., 3.])

Source code in packages/pipekit-array/src/pipekit_array/operators/_reduce.py
@recorded("pipekit_array.dim_operators")
class CumReduce(Operator):
    """A cumulative function along ``dim``, by its array-namespace name.

    Args:
        fn: ``"cumulative_sum"``, ``"cumulative_prod"``, ``"nancumsum"``, …
        dim: The dim to accumulate along.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> CumReduce("cumulative_sum", "time")(DimArray(np.ones(3), ("time",))).data
        array([1., 2., 3.])
    """

    def __init__(self, fn: str, dim: str) -> None:
        if not isinstance(fn, str):
            raise TypeError(
                "CumReduce takes the function's name (e.g. 'cumulative_sum') so it "
                "serialises; for a function, use pipekit_array.cumreduce(x, fn, dim)."
            )
        self.fn = fn
        self.dim = dim

    def _apply(self, x: Any) -> Any:
        return _reduce.cumreduce(x, self.fn, self.dim)

    def get_config(self) -> dict[str, Any]:
        return {"fn": self.fn, "dim": self.dim}

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        keeps = (
            self.fn in _CUMULATIVE_FLOAT_PRESERVING
            and sig.dtype is not None
            and "float" in sig.dtype
        )
        return Signature(dims=sig.dims, dtype=sig.dtype if keeps else None)

Diff

Bases: Operator

The n-th forward difference along dim: N entries become N - n.

Parameters:

Name Type Description Default
dim str

The dim to difference along.

required
n int

The order, n ≥ 0.

1
label str

The labels kept on dim: "upper" (the minuend's) or "lower" (the subtrahend's).

'upper'
Example

import numpy as np from pipekit_array import DimArray Diff("time")(DimArray(np.zeros((12, 4)), ("time", "x"))).shape (11, 4)

Source code in packages/pipekit-array/src/pipekit_array/operators/_shift.py
@recorded("pipekit_array.dim_operators")
class Diff(Operator):
    """The n-th forward difference along ``dim``: N entries become N - n.

    Args:
        dim: The dim to difference along.
        n: The order, n ≥ 0.
        label: The labels kept on ``dim``: ``"upper"`` (the minuend's) or
            ``"lower"`` (the subtrahend's).

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> Diff("time")(DimArray(np.zeros((12, 4)), ("time", "x"))).shape
        (11, 4)
    """

    def __init__(self, dim: str, n: int = 1, label: str = "upper") -> None:
        self.dim = dim
        self.n = n
        self.label = label

    def _apply(self, x: Any) -> Any:
        return _shift.diff(x, self.dim, self.n, label=self.label)

    def get_config(self) -> dict[str, Any]:
        return {"dim": self.dim, "n": self.n, "label": self.label}

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        size = dict(sig.dims)[self.dim]
        return sig.replace_dims(
            {self.dim: None if size is None else max(size - self.n, 0)}
        )

Dot

Bases: Operator

The sum of the product of carriers over named dims: op(*xs).

compute_output_signature takes the tuple of the inputs' signatures: from one signature the other inputs' dims are unknown, so it returns None.

Parameters:

Name Type Description Default
dim str | Sequence[str]

A dim name, or several, to sum over.

required
Example

import numpy as np from pipekit_array import DimArray x = DimArray(np.ones((12, 13)), ("time", "band")) w = DimArray(np.ones((13, 3)), ("band", "component")) Dot(dim="band")(x, w).dims ('time', 'component')

Source code in packages/pipekit-array/src/pipekit_array/operators/_reduce.py
@recorded("pipekit_array.dim_operators")
class Dot(Operator):
    """The sum of the product of carriers over named dims: ``op(*xs)``.

    `compute_output_signature` takes the tuple of the inputs' signatures:
    from one signature the other inputs' dims are unknown, so it returns
    None.

    Args:
        dim: A dim name, or several, to sum over.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> x = DimArray(np.ones((12, 13)), ("time", "band"))
        >>> w = DimArray(np.ones((13, 3)), ("band", "component"))
        >>> Dot(dim="band")(x, w).dims
        ('time', 'component')
    """

    def __init__(self, dim: str | Sequence[str]) -> None:
        self.dim = _names(dim)

    def _apply(self, *xs: Any) -> Any:
        return _reduce.dot(*xs, dim=self.dim)

    def get_config(self) -> dict[str, Any]:
        return {"dim": _names_config(self.dim)}

    def compute_output_signature(self, input_signature: Any) -> Any:
        if not isinstance(input_signature, tuple) or not input_signature:
            return None
        sigs = [s for s in map(_signature, input_signature) if s is not None]
        if len(sigs) != len(input_signature):
            return None
        names = (self.dim,) if isinstance(self.dim, str) else self.dim
        sizes: dict[str, int | None] = {}
        for sig in sigs:
            for d, n in sig.dims:
                # the first known size; check_compatible makes them agree
                if sizes.get(d) is None:
                    sizes[d] = n
        dims = tuple((d, n) for d, n in sizes.items() if d not in names)
        dtypes = {sig.dtype for sig in sigs}
        return Signature(dims=dims, dtype=dtypes.pop() if len(dtypes) == 1 else None)

ExpandDims

Bases: Operator

Insert length-1 dims at position axis of the result.

Parameters:

Name Type Description Default
dim str | Sequence[str]

A new dim name, or several.

required
axis int

Where the first new dim goes; negative counts from the end.

0
Example

import numpy as np from pipekit_array import DimArray ExpandDims("member")(DimArray(np.zeros((2, 4)), ("band", "x"))).dims ('member', 'band', 'x')

Source code in packages/pipekit-array/src/pipekit_array/operators/_shape.py
@recorded("pipekit_array.dim_operators")
class ExpandDims(Operator):
    """Insert length-1 dims at position ``axis`` of the result.

    Args:
        dim: A new dim name, or several.
        axis: Where the first new dim goes; negative counts from the end.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> ExpandDims("member")(DimArray(np.zeros((2, 4)), ("band", "x"))).dims
        ('member', 'band', 'x')
    """

    def __init__(self, dim: str | Sequence[str], axis: int = 0) -> None:
        self.dim = _names(dim)
        self.axis = axis

    def _apply(self, x: Any) -> Any:
        return _shape.expand_dims(x, self.dim, axis=self.axis)

    def get_config(self) -> dict[str, Any]:
        return {"dim": _names_config(self.dim), "axis": self.axis}

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        names = (self.dim,) if isinstance(self.dim, str) else self.dim
        start = self.axis if self.axis >= 0 else self.axis + len(sig.dims) + 1
        added = tuple((n, 1) for n in names)
        dims = (*sig.dims[:start], *added, *sig.dims[start:])
        return Signature(dims=dims, dtype=sig.dtype)

ISel

Bases: Operator

Select by position along one dim; an integer drops the dim.

Parameters:

Name Type Description Default
dim str

The dim to index.

required
index int | slice

An int, or a slice (stored in config as start/stop/step).

required
Example

import numpy as np from pipekit_array import DimArray ISel(dim="time", index=0)(DimArray(np.zeros((12, 4)), ("time", "x"))).dims ('x',)

Source code in packages/pipekit-array/src/pipekit_array/operators/_select.py
@recorded("pipekit_array.dim_operators")
class ISel(Operator):
    """Select by position along one dim; an integer drops the dim.

    Args:
        dim: The dim to index.
        index: An int, or a slice (stored in config as start/stop/step).

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> ISel(dim="time", index=0)(DimArray(np.zeros((12, 4)), ("time", "x"))).dims
        ('x',)
    """

    def __init__(self, dim: str, index: int | slice) -> None:
        self.dim = dim
        index = _slice_from_config(index)
        if type(index).__module__ == "numpy":
            # NumPy positions or a mask (array or scalar) as int / list, so
            # the config is JSON; numpy is optional, so no isinstance
            index = index.tolist()
        self.index = index

    def _apply(self, x: Any) -> Any:
        return _select.isel(x, **{self.dim: self.index})

    def get_config(self) -> dict[str, Any]:
        return {"dim": self.dim, "index": _slice_config(self.index)}

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        if isinstance(self.index, slice):
            size = dict(sig.dims)[self.dim]
            new = None if size is None else len(range(*self.index.indices(size)))
            return sig.replace_dims({self.dim: new})
        if getattr(self.index, "ndim", None) == 0 or isinstance(self.index, int):
            return sig.drop_dims((self.dim,))
        # a list or 1-D array of positions keeps the dim, one entry each; a
        # boolean mask keeps one entry per True
        return sig.replace_dims({self.dim: _selected_count(self.index)})

MapOver

Bases: Operator

fn on each item along dim; the results stacked along dim, first.

Holds a user function, so its config is a debug repr only.

Source code in packages/pipekit-array/src/pipekit_array/operators/_map.py
@recorded("pipekit_array.dim_operators")
class MapOver(Operator):
    """``fn`` on each item along ``dim``; the results stacked along ``dim``, first.

    Holds a user function, so its config is a debug repr only.
    """

    forbid_in_yaml: ClassVar[bool] = True

    def __init__(self, fn: Callable[[Any], Any], dim: str) -> None:
        self.fn = fn
        self.dim = dim

    def _apply(self, x: Any) -> Any:
        return _map.map_over(x, self.fn, self.dim)

    def compute_output_signature(self, input_signature: Any) -> Any:
        # fn is opaque: its output dims are unknown without running it
        return None

MeanScalar

Bases: Operator

Reduce an array to a scalar (or per-axis means) via xp.mean.

Defaults to a full reduction (axis=None) yielding a scalar of rank 0. Output dtype matches the input's floating dtype per the Array API mean contract.

Parameters:

Name Type Description Default
axis int | tuple[int, ...] | None

Axis or tuple of axes to reduce over. None reduces all axes.

None
keepdims bool

If True, retains the reduced axes with size 1.

False

Raises:

Type Description
ValueError

From compute_output_signature when axis is out of range for the input signature's rank (xp.mean would raise the backend-native equivalent at apply time).

Example::

out = MeanScalar()(image)               # scalar
out = MeanScalar(axis=(-2, -1))(image)  # per-band mean
out = MeanScalar(axis=0, keepdims=True)(image)  # shape (1, H, W)
Source code in packages/pipekit-array/src/pipekit_array/operators/_reduce.py
@recorded("pipekit_array.reduce", "pipekit_array.reductions")
class MeanScalar(Operator):
    """Reduce an array to a scalar (or per-axis means) via `xp.mean`.

    Defaults to a full reduction (`axis=None`) yielding a scalar of
    rank 0. Output dtype matches the input's floating dtype per the
    Array API `mean` contract.

    Args:
        axis: Axis or tuple of axes to reduce over. `None` reduces
            all axes.
        keepdims: If True, retains the reduced axes with size 1.

    Raises:
        ValueError: From `compute_output_signature` when ``axis`` is out
            of range for the input signature's rank (``xp.mean`` would
            raise the backend-native equivalent at apply time).

    Example::

        out = MeanScalar()(image)               # scalar
        out = MeanScalar(axis=(-2, -1))(image)  # per-band mean
        out = MeanScalar(axis=0, keepdims=True)(image)  # shape (1, H, W)
    """

    def __init__(
        self,
        axis: int | tuple[int, ...] | None = None,
        keepdims: bool = False,
    ) -> None:
        # a tuple of axes reloads from JSON as a list, which `xp.mean` rejects
        self.axis = tuple(axis) if isinstance(axis, list) else axis
        self.keepdims = keepdims

    def _apply(self, x: Any) -> Any:
        xp = array_namespace(x)
        return xp.mean(x, axis=self.axis, keepdims=self.keepdims)

    def get_config(self) -> dict[str, Any]:
        return {"axis": self.axis, "keepdims": self.keepdims}

    def compute_output_signature(self, input_signature: Any) -> Any:
        if input_signature is None:
            return None

        input_sig: Signature = input_signature
        n_dims = len(input_sig.dims)

        def _validate(axis: int) -> int:
            """Return positive axis index; raise on out-of-range."""
            if n_dims == 0 or axis < -n_dims or axis >= n_dims:
                raise ValueError(
                    f"MeanScalar.axis={axis!r} is out of range for input "
                    f"signature with {n_dims} dim(s); xp.mean would raise."
                )
            return axis % n_dims

        if self.axis is None:
            axes_to_reduce: tuple[int, ...] = tuple(range(n_dims))
        elif isinstance(self.axis, int):
            axes_to_reduce = (_validate(self.axis),)
        else:
            axes_to_reduce = tuple(_validate(a) for a in self.axis)

        if self.keepdims:
            new_dims = tuple(
                (name, 1) if i in axes_to_reduce else (name, size)
                for i, (name, size) in enumerate(input_sig.dims)
            )
        else:
            new_dims = tuple(
                (name, size)
                for i, (name, size) in enumerate(input_sig.dims)
                if i not in axes_to_reduce
            )
        return Signature(dims=new_dims, dtype=input_sig.dtype)

MergeDims

Bases: Operator

Merge the listed dims into one, placed last, in C order.

Its config lists the merges in order (JSON objects sort their keys).

Parameters:

Name Type Description Default
**dims Sequence[str]

new=(source, …) for each merged dim.

{}
Example

import numpy as np from pipekit_array import DimArray cube = DimArray(np.zeros((2, 3, 4)), ("band", "y", "x")) MergeDims(pixel=("y", "x"))(cube).shape (2, 12)

Source code in packages/pipekit-array/src/pipekit_array/operators/_shape.py
@recorded("pipekit_array.dim_operators")
class MergeDims(Operator):
    """Merge the listed dims into one, placed last, in C order.

    Its config lists the merges in order (JSON objects sort their keys).

    Args:
        **dims: ``new=(source, …)`` for each merged dim.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> cube = DimArray(np.zeros((2, 3, 4)), ("band", "y", "x"))
        >>> MergeDims(pixel=("y", "x"))(cube).shape
        (2, 12)
    """

    def __init__(self, **dims: Sequence[str]) -> None:
        self.dims = {name: _merge_group(name, group) for name, group in dims.items()}

    def _apply(self, x: Any) -> Any:
        return _shape.merge_dims(x, **self.dims)

    def get_config(self) -> dict[str, Any]:
        return {"dims": [[name, list(group)] for name, group in self.dims.items()]}

    @classmethod
    def from_config(cls, config: dict[str, Any]) -> Self:
        return cls(**{name: tuple(group) for name, group in config["dims"]})

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        sizes = dict(sig.dims)
        sources = [s for group in self.dims.values() for s in group]
        kept = sig.drop_dims(tuple(sources)).dims
        merged = tuple(
            (name, _product([sizes[s] for s in group]))
            for name, group in self.dims.items()
        )
        return Signature(dims=(*kept, *merged), dtype=sig.dtype)

Pad

Bases: Operator

Widen named dims at either end; a padded dim loses its labels.

Parameters:

Name Type Description Default
mode str

"constant", "edge", "reflect" or "wrap".

'constant'
constant_values float | None

The fill for mode="constant"; None is NaN.

None
**pad_width int | Sequence[int]

dim= a width for both ends, or (before, after).

{}
Example

import numpy as np from pipekit_array import DimArray Pad(time=(2, 0))(DimArray(np.zeros((12, 4)), ("time", "x"))).shape (14, 4)

Source code in packages/pipekit-array/src/pipekit_array/operators/_shift.py
@recorded("pipekit_array.dim_operators")
class Pad(Operator):
    """Widen named dims at either end; a padded dim loses its labels.

    Args:
        mode: ``"constant"``, ``"edge"``, ``"reflect"`` or ``"wrap"``.
        constant_values: The fill for ``mode="constant"``; None is NaN.
        **pad_width: ``dim=`` a width for both ends, or ``(before, after)``.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> Pad(time=(2, 0))(DimArray(np.zeros((12, 4)), ("time", "x"))).shape
        (14, 4)
    """

    def __init__(
        self,
        mode: str = "constant",
        constant_values: float | None = None,
        **pad_width: int | Sequence[int],
    ) -> None:
        self.mode = mode
        self.constant_values = constant_values
        # an int or a pair, as (before, after); JSON gives the pair as a list
        self.pad_width = {
            d: (w, w) if isinstance(w, int) else (w[0], w[1])
            for d, w in pad_width.items()
        }

    def _apply(self, x: Any) -> Any:
        return _shift.pad(
            x, mode=self.mode, constant_values=self.constant_values, **self.pad_width
        )

    def get_config(self) -> dict[str, Any]:
        widths = {d: list(w) for d, w in self.pad_width.items()}
        return {"mode": self.mode, "constant_values": self.constant_values, **widths}

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        sizes = dict(sig.dims)
        grown: dict[str, int | None] = {}
        for d, (before, after) in self.pad_width.items():
            size = sizes[d]
            grown[d] = None if size is None else size + before + after
        return sig.replace_dims(grown)

Quantile

Bases: Operator

Quantiles over named dims; a sequence q adds a leading quantile dim.

q is stored in config as a float or an ordered list.

Parameters:

Name Type Description Default
q float | Sequence[float]

A probability in [0, 1], or a sequence of them.

required
dim str | Sequence[str]

A dim name, or several, to reduce over.

required
method str

One of pipekit_array.named.METHODS.

'linear'
skipna bool

Ignore NaN.

False

Raises:

Type Description
ValueError

method is not supported, or a probability is outside [0, 1].

Example

import numpy as np from pipekit_array import DimArray cube = DimArray(np.ones((12, 2, 4)), ("time", "band", "x")) Quantile([0.1, 0.9], dim="time")(cube).dims ('quantile', 'band', 'x')

Source code in packages/pipekit-array/src/pipekit_array/operators/_reduce.py
@recorded("pipekit_array.dim_operators")
class Quantile(Operator):
    """Quantiles over named dims; a sequence ``q`` adds a leading ``quantile`` dim.

    ``q`` is stored in config as a float or an ordered list.

    Args:
        q: A probability in [0, 1], or a sequence of them.
        dim: A dim name, or several, to reduce over.
        method: One of `pipekit_array.named.METHODS`.
        skipna: Ignore NaN.

    Raises:
        ValueError: ``method`` is not supported, or a probability is
            outside [0, 1].

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> cube = DimArray(np.ones((12, 2, 4)), ("time", "band", "x"))
        >>> Quantile([0.1, 0.9], dim="time")(cube).dims
        ('quantile', 'band', 'x')
    """

    def __init__(
        self,
        q: float | Sequence[float],
        dim: str | Sequence[str],
        method: str = "linear",
        skipna: bool = False,
    ) -> None:
        _reduce.check_method(method, "Quantile")
        self.q = _reduce.probabilities(q, "Quantile")
        self.dim = _names(dim)
        self.method = method
        self.skipna = skipna

    def _apply(self, x: Any) -> Any:
        return _reduce.quantile(
            x, self.q, self.dim, method=self.method, skipna=self.skipna
        )

    def get_config(self) -> dict[str, Any]:
        q = list(self.q) if isinstance(self.q, tuple) else self.q
        return {
            "q": q,
            "dim": _names_config(self.dim),
            "method": self.method,
            "skipna": self.skipna,
        }

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        names = (self.dim,) if isinstance(self.dim, str) else self.dim
        kept = sig.drop_dims(names).dims
        # a float input keeps its dtype; an integer one promotes by backend
        dtype = sig.dtype if sig.dtype is not None and "float" in sig.dtype else None
        if isinstance(self.q, tuple):
            dims = (("quantile", len(self.q)), *kept)
            return Signature(dims=dims, dtype=dtype)
        return Signature(dims=kept, dtype=dtype)

Reduce

Bases: Operator

Reduce over named dims with an array-namespace reduction.

Parameters:

Name Type Description Default
fn str

The reduction's name in the array namespace: "mean", "max", "nanmedian", …

required
dim str | Sequence[str]

A dim name, or several.

required
Example

import numpy as np from pipekit_array import DimArray cube = DimArray(np.ones((12, 2, 4)), ("time", "band", "x")) Reduce("median", dim="time")(cube).dims ('band', 'x')

Source code in packages/pipekit-array/src/pipekit_array/operators/_reduce.py
@recorded("pipekit_array.dim_operators")
class Reduce(Operator):
    """Reduce over named dims with an array-namespace reduction.

    Args:
        fn: The reduction's name in the array namespace: ``"mean"``, ``"max"``,
            ``"nanmedian"``, …
        dim: A dim name, or several.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> cube = DimArray(np.ones((12, 2, 4)), ("time", "band", "x"))
        >>> Reduce("median", dim="time")(cube).dims
        ('band', 'x')
    """

    def __init__(self, fn: str, dim: str | Sequence[str]) -> None:
        if not isinstance(fn, str):
            raise TypeError(
                "Reduce takes the reduction's name (e.g. 'mean') so it serialises; "
                "for a function, use pipekit_array.reduce(x, fn, dim)."
            )
        self.fn = fn
        self.dim = dim if isinstance(dim, str) else tuple(dim)

    def _apply(self, x: Any) -> Any:
        return _reduce.reduce(x, self.fn, self.dim)

    def get_config(self) -> dict[str, Any]:
        dim = self.dim if isinstance(self.dim, str) else list(self.dim)
        return {"fn": self.fn, "dim": dim}

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        names = (self.dim,) if isinstance(self.dim, str) else self.dim
        dropped = sig.drop_dims(tuple(names))
        return Signature(dims=dropped.dims, dtype=_reduced_dtype(self.fn, sig.dtype))

Rename

Bases: Operator

New dim names; data, order and labels unchanged.

Parameters:

Name Type Description Default
**names str

old=new for each dim to rename.

{}
Example

import numpy as np from pipekit_array import DimArray Rename(band="channel")(DimArray(np.zeros((2, 4)), ("band", "x"))).dims ('channel', 'x')

Source code in packages/pipekit-array/src/pipekit_array/operators/_shape.py
@recorded("pipekit_array.dim_operators")
class Rename(Operator):
    """New dim names; data, order and labels unchanged.

    Args:
        **names: ``old=new`` for each dim to rename.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> Rename(band="channel")(DimArray(np.zeros((2, 4)), ("band", "x"))).dims
        ('channel', 'x')
    """

    def __init__(self, **names: str) -> None:
        self.names = dict(names)

    def _apply(self, x: Any) -> Any:
        return _shape.rename(x, **self.names)

    def get_config(self) -> dict[str, Any]:
        return dict(self.names)

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        dims = tuple((self.names.get(d, d), n) for d, n in sig.dims)
        return Signature(dims=dims, dtype=sig.dtype)

Roll

Bases: Operator

Shift data cyclically along named dims; labels stay.

Parameters:

Name Type Description Default
**shifts int

dim=k; a positive k moves data to higher positions.

{}
Example

import numpy as np from pipekit_array import DimArray Roll(time=1)(DimArray(np.arange(3.0), ("time",))).data array([2., 0., 1.])

Source code in packages/pipekit-array/src/pipekit_array/operators/_shift.py
@recorded("pipekit_array.dim_operators")
class Roll(Operator):
    """Shift data cyclically along named dims; labels stay.

    Args:
        **shifts: ``dim=k``; a positive k moves data to higher positions.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> Roll(time=1)(DimArray(np.arange(3.0), ("time",))).data
        array([2., 0., 1.])
    """

    def __init__(self, **shifts: int) -> None:
        self.shifts = dict(shifts)

    def _apply(self, x: Any) -> Any:
        return _shift.roll(x, **self.shifts)

    def get_config(self) -> dict[str, Any]:
        return dict(self.shifts)

    def compute_output_signature(self, input_signature: Any) -> Any:
        return _signature(input_signature)

Sel

Bases: Operator

Select by exact label along one dim; one label drops the dim.

Parameters:

Name Type Description Default
dim str

The dim to select on; it must have labels.

required
label Any

One label, or a list of labels (the dim is kept). A tuple is one label when the dim has tuple-valued labels, else several.

required
Source code in packages/pipekit-array/src/pipekit_array/operators/_select.py
@recorded("pipekit_array.dim_operators")
class Sel(Operator):
    """Select by exact label along one dim; one label drops the dim.

    Args:
        dim: The dim to select on; it must have labels.
        label: One label, or a list of labels (the dim is kept). A tuple is
            one label when the dim has tuple-valued labels, else several.
    """

    def __init__(self, dim: str, label: Any) -> None:
        self.dim = dim
        # a tuple that is itself a label (tuple-valued labels) stays one label;
        # a config from JSON spells it {"__tuple__": [...]}
        label = _decode_tuples(label)
        if getattr(label, "ndim", 0) == 1:
            # a 1-D array of labels is a list of labels: the dim is kept, and
            # the config stays JSON
            label = list(label.tolist())
        self.label = label

    def _apply(self, x: Any) -> Any:
        return _select.sel(x, **{self.dim: self.label})

    def get_config(self) -> dict[str, Any]:
        # JSON has no tuples: keep "one tuple label" apart from "a list of labels"
        return {"dim": self.dim, "label": _encode_tuples(self.label)}

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        if isinstance(self.label, tuple):
            return None  # one tuple-valued label or several: unknown without labels
        if isinstance(self.label, list):
            return sig.replace_dims({self.dim: len(self.label)})
        return sig.drop_dims((self.dim,))

Shift

Bases: Operator

Shift data along named dims, filling the vacated entries; labels stay.

Parameters:

Name Type Description Default
fill_value float | None

The value of the vacated entries; None is NaN.

None
**shifts int

dim=k; a positive k moves data to higher positions.

{}
Example

import numpy as np from pipekit_array import DimArray Shift(time=1)(DimArray(np.arange(3.0), ("time",))).data array([nan, 0., 1.])

Source code in packages/pipekit-array/src/pipekit_array/operators/_shift.py
@recorded("pipekit_array.dim_operators")
class Shift(Operator):
    """Shift data along named dims, filling the vacated entries; labels stay.

    Args:
        fill_value: The value of the vacated entries; None is NaN.
        **shifts: ``dim=k``; a positive k moves data to higher positions.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> Shift(time=1)(DimArray(np.arange(3.0), ("time",))).data
        array([nan,  0.,  1.])
    """

    def __init__(self, fill_value: float | None = None, **shifts: int) -> None:
        self.fill_value = fill_value
        self.shifts = dict(shifts)

    def _apply(self, x: Any) -> Any:
        return _shift.shift(x, fill_value=self.fill_value, **self.shifts)

    def get_config(self) -> dict[str, Any]:
        return {"fill_value": self.fill_value, **self.shifts}

    def compute_output_signature(self, input_signature: Any) -> Any:
        return _signature(input_signature)

SplitDims

Bases: Operator

Split one dim into several, in place, in C order.

Its config lists each split's parts in order (JSON objects sort their keys).

Parameters:

Name Type Description Default
**dims Mapping[str, int]

dim={part: size, …} for each dim to split.

{}
Example

import numpy as np from pipekit_array import DimArray pixels = DimArray(np.zeros((2, 12)), ("band", "pixel")) SplitDims(pixel={"y": 3, "x": 4})(pixels).dims ('band', 'y', 'x')

Source code in packages/pipekit-array/src/pipekit_array/operators/_shape.py
@recorded("pipekit_array.dim_operators")
class SplitDims(Operator):
    """Split one dim into several, in place, in C order.

    Its config lists each split's parts in order (JSON objects sort their
    keys).

    Args:
        **dims: ``dim={part: size, …}`` for each dim to split.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> pixels = DimArray(np.zeros((2, 12)), ("band", "pixel"))
        >>> SplitDims(pixel={"y": 3, "x": 4})(pixels).dims
        ('band', 'y', 'x')
    """

    def __init__(self, **dims: Mapping[str, int]) -> None:
        self.dims = {name: dict(part) for name, part in dims.items()}

    def _apply(self, x: Any) -> Any:
        return _shape.split_dims(x, **self.dims)

    def get_config(self) -> dict[str, Any]:
        return {
            "dims": [
                [name, [[p, n] for p, n in part.items()]]
                for name, part in self.dims.items()
            ]
        }

    @classmethod
    def from_config(cls, config: dict[str, Any]) -> Self:
        return cls(**{name: dict(part) for name, part in config["dims"]})

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        dims: list[tuple[str, int | None]] = []
        for d, n in sig.dims:
            dims.extend(self.dims[d].items() if d in self.dims else [(d, n)])
        return Signature(dims=tuple(dims), dtype=sig.dtype)

Squeeze

Bases: Operator

Drop named length-1 dims.

Parameters:

Name Type Description Default
dim str | Sequence[str]

A dim name, or several; each must have size 1.

required
Source code in packages/pipekit-array/src/pipekit_array/operators/_shape.py
@recorded("pipekit_array.dim_operators")
class Squeeze(Operator):
    """Drop named length-1 dims.

    Args:
        dim: A dim name, or several; each must have size 1.
    """

    def __init__(self, dim: str | Sequence[str]) -> None:
        self.dim = _names(dim)

    def _apply(self, x: Any) -> Any:
        return _shape.squeeze(x, self.dim)

    def get_config(self) -> dict[str, Any]:
        return {"dim": _names_config(self.dim)}

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        names = (self.dim,) if isinstance(self.dim, str) else self.dim
        return sig.drop_dims(names)

StackAlong

Bases: Operator

Stack a list-of-arrays along a new axis via xp.stack.

All input arrays must share the same shape, dtype, and backend; NumPy arrays join JAX ones.

Parameters:

Name Type Description Default
axis int | None

Position of the new axis in the output, for raw arrays. Default 0.

None
dim str | None

For named carriers instead: the new dim's name (pipekit_array.stack; it becomes the leading dim). axis and dim are mutually exclusive.

None

Raises:

Type Description
ValueError

From _apply when the input sequence is empty (also when the inputs mix backends other than NumPy and JAX).

Example::

out = StackAlong(axis=0)([arr_a, arr_b, arr_c])  # shape (3, H, W)
Source code in packages/pipekit-array/src/pipekit_array/operators/_join.py
@recorded("pipekit_array.combinators")
class StackAlong(Operator):
    """Stack a list-of-arrays along a new axis via `xp.stack`.

    All input arrays must share the same shape, dtype, and backend; NumPy
    arrays join JAX ones.

    Args:
        axis: Position of the new axis in the output, for raw arrays.
            Default 0.
        dim: For named carriers instead: the new dim's name
            (`pipekit_array.stack`; it becomes the leading dim). ``axis``
            and ``dim`` are mutually exclusive.

    Raises:
        ValueError: From ``_apply`` when the input sequence is empty
            (also when the inputs mix backends other than NumPy and JAX).

    Example::

        out = StackAlong(axis=0)([arr_a, arr_b, arr_c])  # shape (3, H, W)
    """

    def __init__(self, axis: int | None = None, dim: str | None = None) -> None:
        if axis is not None and dim is not None:
            raise ValueError("StackAlong takes axis= (raw arrays) or dim=, not both.")
        self.axis = 0 if axis is None and dim is None else axis
        self.dim = dim

    def _apply(self, xs: Sequence[Any]) -> Any:
        if len(xs) == 0:
            raise ValueError("StackAlong requires at least one input array.")
        if self.dim is not None:
            return _join.stack(list(xs), self.dim)
        arrays = _dispatch.join_backends(xs, "StackAlong")
        xp = array_namespace(*arrays)
        return xp.stack(arrays, axis=self.axis)

    def get_config(self) -> dict[str, Any]:
        if self.dim is not None:
            return {"dim": self.dim}
        return {"axis": self.axis}

TakeAlong

Bases: Operator

Gather one entry of dim per position: op(x, index).

index is a named carrier with x's dims without dim, such as an ArgReduce result.

Source code in packages/pipekit-array/src/pipekit_array/operators/_select.py
@recorded("pipekit_array.dim_operators")
class TakeAlong(Operator):
    """Gather one entry of ``dim`` per position: ``op(x, index)``.

    ``index`` is a named carrier with ``x``'s dims without ``dim``, such as
    an `ArgReduce` result.
    """

    def __init__(self, dim: str) -> None:
        self.dim = dim

    def _apply(self, x: Any, index: Any) -> Any:
        return _select.take_along(x, index, self.dim)

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        return sig.drop_dims((self.dim,))

Transpose

Bases: Operator

Reorder dims by name.

Parameters:

Name Type Description Default
order Sequence[str]

Every dim, once, in the new order.

required
Source code in packages/pipekit-array/src/pipekit_array/operators/_shape.py
@recorded("pipekit_array.dim_operators")
class Transpose(Operator):
    """Reorder dims by name.

    Args:
        order: Every dim, once, in the new order.
    """

    def __init__(self, order: Sequence[str]) -> None:
        self.order = tuple(order)

    def _apply(self, x: Any) -> Any:
        return _shape.transpose(x, self.order)

    def get_config(self) -> dict[str, Any]:
        return {"order": list(self.order)}

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        sizes = dict(sig.dims)
        return Signature(dims=tuple((d, sizes[d]) for d in self.order), dtype=sig.dtype)

Where

Bases: Operator

Keep x where cond is True, other elsewhere: op(x, cond).

compute_output_signature takes x's signature, or the pair (x's, cond's): the output has x's dims and dtype either way.

Parameters:

Name Type Description Default
other float | None

The fill where cond is False; None is NaN.

None
Example

import numpy as np from pipekit_array import DimArray valid = DimArray(np.array([True, False]), ("x",)) Where()(DimArray(np.ones(2), ("x",)), valid).data array([ 1., nan])

Source code in packages/pipekit-array/src/pipekit_array/operators/_shift.py
@recorded("pipekit_array.dim_operators")
class Where(Operator):
    """Keep ``x`` where ``cond`` is True, ``other`` elsewhere: ``op(x, cond)``.

    `compute_output_signature` takes ``x``'s signature, or the pair
    ``(x's, cond's)``: the output has ``x``'s dims and dtype either way.

    Args:
        other: The fill where ``cond`` is False; None is NaN.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> valid = DimArray(np.array([True, False]), ("x",))
        >>> Where()(DimArray(np.ones(2), ("x",)), valid).data
        array([ 1., nan])
    """

    def __init__(self, other: float | None = None) -> None:
        self.other = other

    def _apply(self, x: Any, cond: Any) -> Any:
        return _shift.where(x, cond, self.other)

    def get_config(self) -> dict[str, Any]:
        return {"other": self.other}

    def compute_output_signature(self, input_signature: Any) -> Any:
        if isinstance(input_signature, tuple) and len(input_signature) == 2:
            input_signature = input_signature[0]
        return _signature(input_signature)

Window

Bases: Operator

Sliding windows along one dim, as a new last dim; full windows only.

Parameters:

Name Type Description Default
window_dim str

The new last dim.

'window'
stride int

The step between window starts.

1
**sizes int

dim=S for the one dim to window.

{}
Example

import numpy as np from pipekit_array import DimArray Window(stride=2, time=3)(DimArray(np.zeros((12, 4)), ("time", "x"))).shape (5, 4, 3)

Source code in packages/pipekit-array/src/pipekit_array/operators/_window.py
@recorded("pipekit_array.dim_operators")
class Window(Operator):
    """Sliding windows along one dim, as a new last dim; full windows only.

    Args:
        window_dim: The new last dim.
        stride: The step between window starts.
        **sizes: ``dim=S`` for the one dim to window.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> Window(stride=2, time=3)(DimArray(np.zeros((12, 4)), ("time", "x"))).shape
        (5, 4, 3)
    """

    def __init__(
        self, window_dim: str = "window", stride: int = 1, **sizes: int
    ) -> None:
        if len(sizes) != 1:
            raise ValueError(f"Window: give one dim=size, got {sizes}.")
        self.window_dim = window_dim
        self.stride = stride
        self.sizes = dict(sizes)

    def _apply(self, x: Any) -> Any:
        return _window.window(
            x, window_dim=self.window_dim, stride=self.stride, **self.sizes
        )

    def get_config(self) -> dict[str, Any]:
        return {"window_dim": self.window_dim, "stride": self.stride, **self.sizes}

    def compute_output_signature(self, input_signature: Any) -> Any:
        sig = _signature(input_signature)
        if sig is None:
            return None
        ((dim, size),) = self.sizes.items()
        length = dict(sig.dims)[dim]
        # n = ⌊(N - S)/s⌋ + 1
        count = None if length is None else (length - size) // self.stride + 1
        windowed = sig.replace_dims({dim: count})
        return Signature(
            dims=(*windowed.dims, (self.window_dim, size)), dtype=sig.dtype
        )

Bridges

pipekit_array.bridges is for other libraries on named carriers.

xarray

The adapter of xr.DataArray loads the first time one arrives ([xarray]).

pipekit_array.bridges.xarray

The xr.DataArray adapter (pipekit-array[xarray]).

Registered lazily: importing pipekit-array never imports xarray; the adapter loads the first time a DataArray reaches pipekit_ops.get_adapter (pipekit_ops.register_lazy_adapter). This module is the only place pipekit-array imports xarray.

  • Exact coordinates. XarrayAdapter.check_compatible aligns with join="exact": two cubes whose x differ by 1e-7 raise instead of inner-joining to an empty result.
  • Lazy on dask. KernelOp runs as one xr.apply_ufunc call, dask="parallelized", one task per chunk; the dim-level fast paths (reduce, argreduce, take_along, stack, concat, combine, transpose, rename, window) are native xarray calls, so nothing computes until .compute().
  • DataArray only (PK-P5): Dataset and DataTree dispatch stays in xrops.

from_xarray turns a DataArray into a DimArray, keeping the labels of dims that have a 1-D index coordinate.

XarrayAdapter

The pipekit_ops.NamedArrayAdapter for xr.DataArray, with fast paths.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
class XarrayAdapter:
    """The `pipekit_ops.NamedArrayAdapter` for ``xr.DataArray``, with fast paths."""

    # -- the protocol ----------------------------------------------------

    def dims(self, x: xr.DataArray) -> tuple[str, ...]:
        """``x.dims``."""
        return tuple(str(d) for d in x.dims)

    def data(self, x: xr.DataArray) -> Any:
        """``x.data``: NumPy, dask or JAX, never computed."""
        return x.data

    def labels(self, x: xr.DataArray, dim: str) -> tuple[Any, ...] | None:
        """The values of ``dim``'s 1-D index coordinate, or None."""
        if dim in x.indexes and x.coords[dim].ndim == 1:
            values = x.coords[dim].values
            # tolist() turns datetime64[ns] into integers; keep the scalars
            if values.dtype.kind in "mM":
                return tuple(values)
            return tuple(values.tolist())
        return None

    def isel(self, x: xr.DataArray, indexers: dict[str, Any]) -> xr.DataArray:
        """``x.isel``; coordinates follow the data."""
        return x.isel(indexers)

    def rewrap(self, x: xr.DataArray, data: Any, dims: tuple[str, ...]) -> xr.DataArray:
        """A DataArray around new data, keeping coords and attrs of surviving dims.

        A coordinate is kept when all its dims survive with their sizes; a
        scalar one named after a new dim (left by ``isel``) is dropped, since
        xarray refuses a scalar variable named after a dim.
        """
        sizes = dict(zip(dims, data.shape, strict=True))
        coords = {
            name: coord
            for name, coord in x.coords.items()
            if all(sizes.get(d) == x.sizes[d] for d in coord.dims)
            and (name not in sizes or name in coord.dims)
        }
        out = xr.DataArray(data, dims=dims, coords=coords, attrs=x.attrs, name=x.name)
        # name=None lets xarray take a dask array's graph name; keep x's instead
        return out if out.name == x.name else out.rename(x.name)

    def check_compatible(self, *xs: xr.DataArray) -> tuple[xr.DataArray, ...]:
        """The inputs back if their shared coordinates are exactly equal.

        Raises:
            ValueError: Coordinates differ; xarray would otherwise inner-join.
        """
        try:
            xr.align(*xs, join="exact", copy=False)
        except ValueError as exc:
            raise ValueError(
                f"DataArray inputs are not aligned (join='exact'): {exc}"
            ) from exc
        return xs

    def signature(self, x: xr.DataArray) -> Signature:
        """Dims with their sizes, and the dtype's name."""
        dims = tuple((str(d), int(n)) for d, n in x.sizes.items())
        return Signature(dims=dims, dtype=str(x.dtype))

    def relabel(
        self, x: xr.DataArray, labels: Mapping[str, tuple[Any, ...] | None]
    ) -> xr.DataArray:
        """``assign_coords`` for a tuple, ``drop_vars`` for None (ADR A22).

        Tuple-valued labels (`pipekit_array.merge_dims`) become a 1-D object
        coordinate, one tuple per entry.

        Raises:
            ValueError: A dim is not in ``x.dims``, or a label tuple's
                length differs from its dim's size.
        """
        dims = self.dims(x)
        unknown = sorted(set(labels) - set(dims))
        if unknown:
            raise ValueError(f"relabel: unknown dim(s) {unknown}; dims are {dims}.")
        out = x.drop_vars([d for d in labels if d in x.coords])
        coords: dict[str, Any] = {}
        for dim, values in labels.items():
            if values is None:
                continue
            size = int(x.sizes[dim])
            if len(values) != size:
                raise ValueError(
                    f"{len(values)} labels for dim {dim!r} of size {size}."
                )
            coords[dim] = (dim, _label_array(values))
        return out.assign_coords(coords) if coords else out

    # -- dim-level fast paths --------------------------------------------

    def rename(self, x: xr.DataArray, names: dict[str, str]) -> xr.DataArray:
        """``x.rename``, which also renames the dim in non-index coordinates.

        The generic path (``rewrap`` then ``relabel``) would drop a
        coordinate such as ``quality(time, x)`` on a renamed dim (A22).
        """
        return x.rename(names)

    def transpose(self, x: xr.DataArray, order: tuple[str, ...]) -> xr.DataArray:
        """``x.transpose``."""
        return x.transpose(*order)

    def reduce(
        self,
        x: xr.DataArray,
        fn: str | Callable[..., Any],
        dims: tuple[str, ...],
        *,
        where: Any | None = None,
    ) -> xr.DataArray:
        """``x.reduce`` with the array-namespace function, lazy on dask.

        A name resolves to NumPy's function (``"nanmean"`` included), so NaN
        handling matches the generic path: DataArray's own ``mean`` would
        skip NaN by default. ``where`` arrives aligned to ``x``'s full shape
        (`pipekit_array.reduce` aligns it) and is passed to the function.
        """
        func = _numpy_function(fn, "reduce")
        if where is None:
            return x.reduce(func, dim=list(dims))
        return x.reduce(func, dim=list(dims), where=where)

    def argreduce(
        self, x: xr.DataArray, fn: str | Callable[..., Any], dim: str
    ) -> xr.DataArray:
        """``x.reduce`` with NumPy's ``argmax`` / ``argmin`` (or ``fn``)."""
        return x.reduce(_numpy_function(fn, "argreduce"), dim=dim)

    def take_along(self, x: xr.DataArray, index: Any, dim: str) -> xr.DataArray:
        """One entry of ``dim`` per position, lazily on dask, in ``x``'s dim order.

        An ``apply_ufunc`` over ``take_along_axis``, not a vectorized
        ``isel``: dask cannot index with a lazy indexer. Attributes are kept,
        auxiliary coordinates on ``dim`` (``quality(time, x)``) are gathered
        with the same index, and ``dim``'s own coordinate is dropped.
        """
        rest = [str(d) for d in x.dims if d != dim]
        idx = _as_dataarray(index)  # raises for an unnamed index
        index_dims = tuple(map(str, idx.dims))
        if not set(index_dims) <= set(rest):
            # the gather would broadcast over an unrelated index dim
            raise ValueError(
                f"take_along: index dims {index_dims} are not among {tuple(rest)}."
            )
        # the labels are checked on the index itself: idx drops its coordinates
        index_adapter = get_adapter(index)
        if index_adapter is not None:
            check_index_labels(self, x, index_adapter, index, index_adapter.dims(index))
        out = _gather(x, idx, dim)
        aux = {
            str(name): _gather(xr.DataArray(c.variable), idx, dim).variable
            for name, c in x.coords.items()
            if dim in c.dims and name != dim
        }
        if aux:
            out = out.assign_coords(aux)
        return out.transpose(*rest)

    def stack(self, xs: Sequence[xr.DataArray], dim: str) -> xr.DataArray:
        """A new leading dim; coordinates must be identical (never aligned)."""
        if dim in xs[0].dims:
            raise ValueError(
                f"stack: {dim!r} is already a dim of {tuple(xs[0].dims)}; use concat."
            )
        _require_same_dims(xs, "stack")
        checked = _joined(self.check_compatible(*xs), "stack")
        return xr.concat(
            checked, dim=dim, join="exact", coords="minimal", compat="identical"
        )

    def concat(self, xs: Sequence[xr.DataArray], dim: str) -> xr.DataArray:
        """Along an existing dim, joining its coordinate; others must be identical."""
        dims = _require_same_dims(xs, "concat")
        if dim not in dims:
            raise ValueError(f"concat: {dim!r} is not a dim of {dims}; use stack.")
        return xr.concat(
            _joined(xs, "concat"),
            dim=dim,
            join="exact",
            coords="minimal",
            compat="identical",
        )

    def combine(self, fn: Callable[..., Any], *xs: xr.DataArray) -> xr.DataArray:
        """``apply_ufunc(fn, …, join="exact")``, lazy on dask."""
        checked = _joined(self.check_compatible(*xs), "combine")
        dims = tuple(checked[0].dims)
        ordered = [x.transpose(*dims) for x in checked]
        lazy = any(_is_dask(x) for x in ordered)
        return xr.apply_ufunc(
            fn,
            *ordered,
            dask="parallelized",
            join="exact",
            # dask needs the result dtype up front: read it from a one-element call
            output_dtypes=[_probe_dtype(fn, ordered)] if lazy else None,
        )

    def window(
        self, x: xr.DataArray, dim: str, size: int, *, window_dim: str, stride: int
    ) -> xr.DataArray:
        """``rolling(...).construct(window_dim)`` from the first full window: a view.

        The fill is zero, not NaN, so integers keep their dtype; the
        ``isel`` drops every filled window.
        """
        constructed = x.rolling({dim: size}).construct(
            window_dim, fill_value=np.zeros((), x.dtype)[()]
        )
        return constructed.isel({dim: slice(size - 1, None, stride)})

    # -- KernelOp ---------------------------------------------------------

    def apply_kernel(
        self,
        kernel: Callable[..., Any],
        spec: KernelSpec,
        core: dict[str, str],
        kwargs: dict[str, Any],
        *xs: xr.DataArray,
    ) -> Any:
        """A `KernelOp` call as one ``xr.apply_ufunc``: lazy, exact, in input order.

        Core dims move last; ``kwargs`` (params and derived geometry) are
        passed whole; output-only core dims take their sizes from
        ``spec.sizes``; outputs come back with the loop dims of all the inputs,
        in the first input's dim order (F2), without a name or attributes.

        Raises:
            BackendError: The spec does not list ``"xarray"``.
        """
        if "xarray" not in spec.backends:
            raise BackendError(
                f"the kernel declares backends {spec.backends}, not 'xarray'."
            )
        checked = _joined(
            self.check_compatible(*xs), getattr(kernel, "__name__", "KernelOp")
        )
        in_dims = [[core[c] for c in group] for group in spec.inputs]
        out_dims = [[core[c] for c in group] for group in spec.outputs]
        letter_size: dict[str, int] = {}
        for x, group in zip(checked, spec.inputs, strict=True):
            for letter in group:
                letter_size[letter] = int(x.sizes[core[letter]])
        output_sizes = {core[out]: letter_size[source] for out, source in spec.sizes}
        result = xr.apply_ufunc(
            full_loop(kernel, spec),
            *checked,
            input_core_dims=in_dims,
            output_core_dims=out_dims,
            kwargs=kwargs,
            vectorize=spec.vectorize,
            dask="parallelized",
            join="exact",
            # for the coordinates: False would drop their attributes too
            keep_attrs="drop_conflicts",
            output_dtypes=_output_dtypes(kernel, spec, kwargs, checked, letter_size),
            dask_gufunc_kwargs={"allow_rechunk": True, "output_sizes": output_sizes},
        )
        targets = kernel_output_dims(
            spec, core, [tuple(map(str, x.dims)) for x in checked]
        )
        if len(out_dims) > 1:
            outs = zip(result, targets, strict=True)
            return tuple(anonymous(out.transpose(*dims)) for out, dims in outs)
        return anonymous(result.transpose(*targets[0]))

apply_kernel(kernel, spec, core, kwargs, *xs)

A KernelOp call as one xr.apply_ufunc: lazy, exact, in input order.

Core dims move last; kwargs (params and derived geometry) are passed whole; output-only core dims take their sizes from spec.sizes; outputs come back with the loop dims of all the inputs, in the first input's dim order (F2), without a name or attributes.

Raises:

Type Description
BackendError

The spec does not list "xarray".

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def apply_kernel(
    self,
    kernel: Callable[..., Any],
    spec: KernelSpec,
    core: dict[str, str],
    kwargs: dict[str, Any],
    *xs: xr.DataArray,
) -> Any:
    """A `KernelOp` call as one ``xr.apply_ufunc``: lazy, exact, in input order.

    Core dims move last; ``kwargs`` (params and derived geometry) are
    passed whole; output-only core dims take their sizes from
    ``spec.sizes``; outputs come back with the loop dims of all the inputs,
    in the first input's dim order (F2), without a name or attributes.

    Raises:
        BackendError: The spec does not list ``"xarray"``.
    """
    if "xarray" not in spec.backends:
        raise BackendError(
            f"the kernel declares backends {spec.backends}, not 'xarray'."
        )
    checked = _joined(
        self.check_compatible(*xs), getattr(kernel, "__name__", "KernelOp")
    )
    in_dims = [[core[c] for c in group] for group in spec.inputs]
    out_dims = [[core[c] for c in group] for group in spec.outputs]
    letter_size: dict[str, int] = {}
    for x, group in zip(checked, spec.inputs, strict=True):
        for letter in group:
            letter_size[letter] = int(x.sizes[core[letter]])
    output_sizes = {core[out]: letter_size[source] for out, source in spec.sizes}
    result = xr.apply_ufunc(
        full_loop(kernel, spec),
        *checked,
        input_core_dims=in_dims,
        output_core_dims=out_dims,
        kwargs=kwargs,
        vectorize=spec.vectorize,
        dask="parallelized",
        join="exact",
        # for the coordinates: False would drop their attributes too
        keep_attrs="drop_conflicts",
        output_dtypes=_output_dtypes(kernel, spec, kwargs, checked, letter_size),
        dask_gufunc_kwargs={"allow_rechunk": True, "output_sizes": output_sizes},
    )
    targets = kernel_output_dims(
        spec, core, [tuple(map(str, x.dims)) for x in checked]
    )
    if len(out_dims) > 1:
        outs = zip(result, targets, strict=True)
        return tuple(anonymous(out.transpose(*dims)) for out, dims in outs)
    return anonymous(result.transpose(*targets[0]))

argreduce(x, fn, dim)

x.reduce with NumPy's argmax / argmin (or fn).

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def argreduce(
    self, x: xr.DataArray, fn: str | Callable[..., Any], dim: str
) -> xr.DataArray:
    """``x.reduce`` with NumPy's ``argmax`` / ``argmin`` (or ``fn``)."""
    return x.reduce(_numpy_function(fn, "argreduce"), dim=dim)

check_compatible(*xs)

The inputs back if their shared coordinates are exactly equal.

Raises:

Type Description
ValueError

Coordinates differ; xarray would otherwise inner-join.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def check_compatible(self, *xs: xr.DataArray) -> tuple[xr.DataArray, ...]:
    """The inputs back if their shared coordinates are exactly equal.

    Raises:
        ValueError: Coordinates differ; xarray would otherwise inner-join.
    """
    try:
        xr.align(*xs, join="exact", copy=False)
    except ValueError as exc:
        raise ValueError(
            f"DataArray inputs are not aligned (join='exact'): {exc}"
        ) from exc
    return xs

combine(fn, *xs)

apply_ufunc(fn, …, join="exact"), lazy on dask.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def combine(self, fn: Callable[..., Any], *xs: xr.DataArray) -> xr.DataArray:
    """``apply_ufunc(fn, …, join="exact")``, lazy on dask."""
    checked = _joined(self.check_compatible(*xs), "combine")
    dims = tuple(checked[0].dims)
    ordered = [x.transpose(*dims) for x in checked]
    lazy = any(_is_dask(x) for x in ordered)
    return xr.apply_ufunc(
        fn,
        *ordered,
        dask="parallelized",
        join="exact",
        # dask needs the result dtype up front: read it from a one-element call
        output_dtypes=[_probe_dtype(fn, ordered)] if lazy else None,
    )

concat(xs, dim)

Along an existing dim, joining its coordinate; others must be identical.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def concat(self, xs: Sequence[xr.DataArray], dim: str) -> xr.DataArray:
    """Along an existing dim, joining its coordinate; others must be identical."""
    dims = _require_same_dims(xs, "concat")
    if dim not in dims:
        raise ValueError(f"concat: {dim!r} is not a dim of {dims}; use stack.")
    return xr.concat(
        _joined(xs, "concat"),
        dim=dim,
        join="exact",
        coords="minimal",
        compat="identical",
    )

data(x)

x.data: NumPy, dask or JAX, never computed.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def data(self, x: xr.DataArray) -> Any:
    """``x.data``: NumPy, dask or JAX, never computed."""
    return x.data

dims(x)

x.dims.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def dims(self, x: xr.DataArray) -> tuple[str, ...]:
    """``x.dims``."""
    return tuple(str(d) for d in x.dims)

isel(x, indexers)

x.isel; coordinates follow the data.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def isel(self, x: xr.DataArray, indexers: dict[str, Any]) -> xr.DataArray:
    """``x.isel``; coordinates follow the data."""
    return x.isel(indexers)

labels(x, dim)

The values of dim's 1-D index coordinate, or None.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def labels(self, x: xr.DataArray, dim: str) -> tuple[Any, ...] | None:
    """The values of ``dim``'s 1-D index coordinate, or None."""
    if dim in x.indexes and x.coords[dim].ndim == 1:
        values = x.coords[dim].values
        # tolist() turns datetime64[ns] into integers; keep the scalars
        if values.dtype.kind in "mM":
            return tuple(values)
        return tuple(values.tolist())
    return None

reduce(x, fn, dims, *, where=None)

x.reduce with the array-namespace function, lazy on dask.

A name resolves to NumPy's function ("nanmean" included), so NaN handling matches the generic path: DataArray's own mean would skip NaN by default. where arrives aligned to x's full shape (pipekit_array.reduce aligns it) and is passed to the function.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def reduce(
    self,
    x: xr.DataArray,
    fn: str | Callable[..., Any],
    dims: tuple[str, ...],
    *,
    where: Any | None = None,
) -> xr.DataArray:
    """``x.reduce`` with the array-namespace function, lazy on dask.

    A name resolves to NumPy's function (``"nanmean"`` included), so NaN
    handling matches the generic path: DataArray's own ``mean`` would
    skip NaN by default. ``where`` arrives aligned to ``x``'s full shape
    (`pipekit_array.reduce` aligns it) and is passed to the function.
    """
    func = _numpy_function(fn, "reduce")
    if where is None:
        return x.reduce(func, dim=list(dims))
    return x.reduce(func, dim=list(dims), where=where)

relabel(x, labels)

assign_coords for a tuple, drop_vars for None (ADR A22).

Tuple-valued labels (pipekit_array.merge_dims) become a 1-D object coordinate, one tuple per entry.

Raises:

Type Description
ValueError

A dim is not in x.dims, or a label tuple's length differs from its dim's size.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def relabel(
    self, x: xr.DataArray, labels: Mapping[str, tuple[Any, ...] | None]
) -> xr.DataArray:
    """``assign_coords`` for a tuple, ``drop_vars`` for None (ADR A22).

    Tuple-valued labels (`pipekit_array.merge_dims`) become a 1-D object
    coordinate, one tuple per entry.

    Raises:
        ValueError: A dim is not in ``x.dims``, or a label tuple's
            length differs from its dim's size.
    """
    dims = self.dims(x)
    unknown = sorted(set(labels) - set(dims))
    if unknown:
        raise ValueError(f"relabel: unknown dim(s) {unknown}; dims are {dims}.")
    out = x.drop_vars([d for d in labels if d in x.coords])
    coords: dict[str, Any] = {}
    for dim, values in labels.items():
        if values is None:
            continue
        size = int(x.sizes[dim])
        if len(values) != size:
            raise ValueError(
                f"{len(values)} labels for dim {dim!r} of size {size}."
            )
        coords[dim] = (dim, _label_array(values))
    return out.assign_coords(coords) if coords else out

rename(x, names)

x.rename, which also renames the dim in non-index coordinates.

The generic path (rewrap then relabel) would drop a coordinate such as quality(time, x) on a renamed dim (A22).

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def rename(self, x: xr.DataArray, names: dict[str, str]) -> xr.DataArray:
    """``x.rename``, which also renames the dim in non-index coordinates.

    The generic path (``rewrap`` then ``relabel``) would drop a
    coordinate such as ``quality(time, x)`` on a renamed dim (A22).
    """
    return x.rename(names)

rewrap(x, data, dims)

A DataArray around new data, keeping coords and attrs of surviving dims.

A coordinate is kept when all its dims survive with their sizes; a scalar one named after a new dim (left by isel) is dropped, since xarray refuses a scalar variable named after a dim.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def rewrap(self, x: xr.DataArray, data: Any, dims: tuple[str, ...]) -> xr.DataArray:
    """A DataArray around new data, keeping coords and attrs of surviving dims.

    A coordinate is kept when all its dims survive with their sizes; a
    scalar one named after a new dim (left by ``isel``) is dropped, since
    xarray refuses a scalar variable named after a dim.
    """
    sizes = dict(zip(dims, data.shape, strict=True))
    coords = {
        name: coord
        for name, coord in x.coords.items()
        if all(sizes.get(d) == x.sizes[d] for d in coord.dims)
        and (name not in sizes or name in coord.dims)
    }
    out = xr.DataArray(data, dims=dims, coords=coords, attrs=x.attrs, name=x.name)
    # name=None lets xarray take a dask array's graph name; keep x's instead
    return out if out.name == x.name else out.rename(x.name)

signature(x)

Dims with their sizes, and the dtype's name.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def signature(self, x: xr.DataArray) -> Signature:
    """Dims with their sizes, and the dtype's name."""
    dims = tuple((str(d), int(n)) for d, n in x.sizes.items())
    return Signature(dims=dims, dtype=str(x.dtype))

stack(xs, dim)

A new leading dim; coordinates must be identical (never aligned).

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def stack(self, xs: Sequence[xr.DataArray], dim: str) -> xr.DataArray:
    """A new leading dim; coordinates must be identical (never aligned)."""
    if dim in xs[0].dims:
        raise ValueError(
            f"stack: {dim!r} is already a dim of {tuple(xs[0].dims)}; use concat."
        )
    _require_same_dims(xs, "stack")
    checked = _joined(self.check_compatible(*xs), "stack")
    return xr.concat(
        checked, dim=dim, join="exact", coords="minimal", compat="identical"
    )

take_along(x, index, dim)

One entry of dim per position, lazily on dask, in x's dim order.

An apply_ufunc over take_along_axis, not a vectorized isel: dask cannot index with a lazy indexer. Attributes are kept, auxiliary coordinates on dim (quality(time, x)) are gathered with the same index, and dim's own coordinate is dropped.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def take_along(self, x: xr.DataArray, index: Any, dim: str) -> xr.DataArray:
    """One entry of ``dim`` per position, lazily on dask, in ``x``'s dim order.

    An ``apply_ufunc`` over ``take_along_axis``, not a vectorized
    ``isel``: dask cannot index with a lazy indexer. Attributes are kept,
    auxiliary coordinates on ``dim`` (``quality(time, x)``) are gathered
    with the same index, and ``dim``'s own coordinate is dropped.
    """
    rest = [str(d) for d in x.dims if d != dim]
    idx = _as_dataarray(index)  # raises for an unnamed index
    index_dims = tuple(map(str, idx.dims))
    if not set(index_dims) <= set(rest):
        # the gather would broadcast over an unrelated index dim
        raise ValueError(
            f"take_along: index dims {index_dims} are not among {tuple(rest)}."
        )
    # the labels are checked on the index itself: idx drops its coordinates
    index_adapter = get_adapter(index)
    if index_adapter is not None:
        check_index_labels(self, x, index_adapter, index, index_adapter.dims(index))
    out = _gather(x, idx, dim)
    aux = {
        str(name): _gather(xr.DataArray(c.variable), idx, dim).variable
        for name, c in x.coords.items()
        if dim in c.dims and name != dim
    }
    if aux:
        out = out.assign_coords(aux)
    return out.transpose(*rest)

transpose(x, order)

x.transpose.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def transpose(self, x: xr.DataArray, order: tuple[str, ...]) -> xr.DataArray:
    """``x.transpose``."""
    return x.transpose(*order)

window(x, dim, size, *, window_dim, stride)

rolling(...).construct(window_dim) from the first full window: a view.

The fill is zero, not NaN, so integers keep their dtype; the isel drops every filled window.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def window(
    self, x: xr.DataArray, dim: str, size: int, *, window_dim: str, stride: int
) -> xr.DataArray:
    """``rolling(...).construct(window_dim)`` from the first full window: a view.

    The fill is zero, not NaN, so integers keep their dtype; the
    ``isel`` drops every filled window.
    """
    constructed = x.rolling({dim: size}).construct(
        window_dim, fill_value=np.zeros((), x.dtype)[()]
    )
    return constructed.isel({dim: slice(size - 1, None, stride)})

from_xarray(da)

A DimArray with the same data and dims; 1-D index coordinates become labels.

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def from_xarray(da: xr.DataArray) -> DimArray:
    """A `DimArray` with the same data and dims; 1-D index coordinates become labels."""
    adapter = XarrayAdapter()
    dims = adapter.dims(da)
    labels = {d: adapter.labels(da, d) for d in dims}
    return DimArray(da.data, dims, {d: v for d, v in labels.items() if v is not None})

register()

Register XarrayAdapter for xr.DataArray (the lazy loader calls this).

Source code in packages/pipekit-array/src/pipekit_array/bridges/xarray.py
def register() -> None:
    """Register `XarrayAdapter` for ``xr.DataArray`` (the lazy loader calls this)."""
    register_adapter(xr.DataArray, XarrayAdapter())

einx

einx patterns whose axis names are the carrier's dim names ([einx], geostack OP-O2). A dask input raises BackendError naming .compute(), because einx has no dask backend.

pipekit_array.bridges.einx

A named-dims einx bridge on any carrier (geostack OP-O2).

einx <https://github.com/fferflo/einx>_ writes tensor algebra as patterns ("band y x -> y x band"). Here the pattern's axis names are the carrier's dim names: each input is matched to its slot by name, einx runs on the data's own backend (NumPy, JAX under jit), and the result is rewrapped through the first input's adapter with the output slot's dims, so it keeps the labels, coordinates and (for geoarray) the grid of the dims that survive:

rearrange("band y x -> y x band", scene)            # transpose by name
reduce("time band y x -> band y x", cube, op="mean") # over time
einsum("time band, band k -> time k", cube, w)       # contract band
repeat("y x -> member y x", field, member=8)         # broadcast

einsum, reduce and repeat match input slots to dims by name, in any order. rearrange also splits and merges dims ("(a b)"), so its input slot is read positionally: it lists the input's dims in order, a group standing for one dim that is split. A merged output group (a b) becomes one dim named a_b.

dask is not an einx backend: a dask-backed input raises pipekit_ops.BackendError naming .compute() rather than computing it silently. einx is imported lazily: install the [einx] extra.

Einx

Bases: Operator

One einx operation as an operator: Einx("reduce", pattern, op="mean").

Parameters:

Name Type Description Default
operation str

"rearrange", "reduce", "einsum" or "repeat".

required
pattern str

The pattern, with dim names as axis names.

required
op str | None

The reduction, for "reduce" (a name, so it serialises).

None
sizes dict[str, int] | None

Axis sizes forwarded to einx.

None
Example

import numpy as np from pipekit_array import DimArray cube = DimArray(np.ones((2, 3, 4)), ("time", "y", "x")) Einx("reduce", "time y x -> y x", op="mean")(cube).dims ('y', 'x')

Source code in packages/pipekit-array/src/pipekit_array/bridges/einx.py
class Einx(Operator):
    """One einx operation as an operator: ``Einx("reduce", pattern, op="mean")``.

    Args:
        operation: ``"rearrange"``, ``"reduce"``, ``"einsum"`` or ``"repeat"``.
        pattern: The pattern, with dim names as axis names.
        op: The reduction, for ``"reduce"`` (a name, so it serialises).
        sizes: Axis sizes forwarded to einx.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> cube = DimArray(np.ones((2, 3, 4)), ("time", "y", "x"))
        >>> Einx("reduce", "time y x -> y x", op="mean")(cube).dims
        ('y', 'x')
    """

    #: the module that states were saved under before
    __former_modules__: ClassVar[tuple[str, ...]] = ("pipekit_array.einx",)
    backends: ClassVar[tuple[str, ...]] = ("numpy", "jax")

    def __init__(
        self,
        operation: str,
        pattern: str,
        *,
        op: str | None = None,
        sizes: dict[str, int] | None = None,
    ) -> None:
        if operation not in _FUNCTIONS:
            raise ValueError(f"operation must be one of {tuple(_FUNCTIONS)}")
        if (operation == "reduce") != (op is not None):
            raise ValueError("op is given exactly when operation is 'reduce'")
        _parse(pattern)  # fail at construction, not at the first call
        self.operation = operation
        self.pattern = pattern
        self.op = op
        self.sizes = dict(sizes or {})

    def _apply(self, *xs: Any) -> Any:
        fn = _FUNCTIONS[self.operation]
        if self.operation == "reduce":
            return fn(self.pattern, *xs, op=self.op, **self.sizes)
        return fn(self.pattern, *xs, **self.sizes)

PatternError

Bases: ValueError

A pattern this bridge cannot read, or one that does not fit its inputs.

Source code in packages/pipekit-array/src/pipekit_array/bridges/einx.py
class PatternError(ValueError):
    """A pattern this bridge cannot read, or one that does not fit its inputs."""

einsum(pattern, *xs, **sizes)

Contract the dims named on the input slots and left off the output.

Parameters:

Name Type Description Default
pattern str

"time band, band k -> time k".

required
*xs Any

One named carrier per input slot; carriers of one family are checked to combine first (geoarray: the same grid).

()
**sizes int

Forwarded to einx.

{}

Returns:

Type Description
Any

A carrier with the output slot's dims, rewrapped by the first

Any

input's adapter.

Example

import numpy as np from pipekit_array import DimArray a = DimArray(np.ones((2, 3)), ("time", "band")) w = DimArray(np.ones((3, 4)), ("band", "k")) einsum("time band, band k -> time k", a, w).shape (2, 4)

Source code in packages/pipekit-array/src/pipekit_array/bridges/einx.py
def einsum(pattern: str, *xs: Any, **sizes: int) -> Any:
    """Contract the dims named on the input slots and left off the output.

    Args:
        pattern: ``"time band, band k -> time k"``.
        *xs: One named carrier per input slot; carriers of one family are
            checked to combine first (geoarray: the same grid).
        **sizes: Forwarded to einx.

    Returns:
        A carrier with the output slot's dims, rewrapped by the first
        input's adapter.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> a = DimArray(np.ones((2, 3)), ("time", "band"))
        >>> w = DimArray(np.ones((3, 4)), ("band", "k"))
        >>> einsum("time band, band k -> time k", a, w).shape
        (2, 4)
    """
    einx = _einx()
    parsed = _parse(pattern)
    prepared = _by_name(xs, parsed, "einsum")
    datas = _datas(prepared, "einsum")
    return _wrap(prepared[0], einx.dot(pattern, *datas, **sizes), parsed)

rearrange(pattern, x, **sizes)

Transpose, split or merge dims by name.

Parameters:

Name Type Description Default
pattern str

One input slot and one output slot. The input slot lists x's dims in order; (a b) stands for one dim split into a and b (give their sizes in sizes).

required
x Any

A named carrier.

required
**sizes int

Sizes of split dims einx cannot infer (a=2).

{}

Returns:

Type Description
Any

A carrier with the output slot's dims (a merged (a b) is

Any

a_b), rewrapped by x's adapter.

Raises:

Type Description
PatternError

The input slot does not describe x's dims.

Example

import numpy as np from pipekit_array import DimArray scene = DimArray(np.zeros((3, 4, 5)), ("band", "y", "x")) rearrange("band y x -> y x band", scene).dims ('y', 'x', 'band')

Source code in packages/pipekit-array/src/pipekit_array/bridges/einx.py
def rearrange(pattern: str, x: Any, **sizes: int) -> Any:
    """Transpose, split or merge dims by name.

    Args:
        pattern: One input slot and one output slot. The input slot lists
            ``x``'s dims in order; ``(a b)`` stands for one dim split into
            ``a`` and ``b`` (give their sizes in ``sizes``).
        x: A named carrier.
        **sizes: Sizes of split dims einx cannot infer (``a=2``).

    Returns:
        A carrier with the output slot's dims (a merged ``(a b)`` is
        ``a_b``), rewrapped by ``x``'s adapter.

    Raises:
        PatternError: The input slot does not describe ``x``'s dims.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> scene = DimArray(np.zeros((3, 4, 5)), ("band", "y", "x"))
        >>> rearrange("band y x -> y x band", scene).dims
        ('y', 'x', 'band')
    """
    einx = _einx()
    parsed = _parse(pattern)
    if len(parsed.inputs) != 1:
        raise PatternError("rearrange takes one input slot.")
    dims = tuple(adapter_of(x, "rearrange").dims(x))
    slot = parsed.inputs[0]
    names = [el for el in slot if isinstance(el, str)]
    if len(slot) != len(dims) or any(
        isinstance(el, str) and el != d for el, d in zip(slot, dims, strict=True)
    ):
        if not any(isinstance(el, tuple) for el in slot) and set(names) == set(dims):
            x = transpose(x, tuple(names))  # a flat slot in another order: by name
        else:
            raise PatternError(
                f"rearrange: the input slot {slot} does not describe dims {dims} "
                "in order"
            )
    (data,) = _datas([x], "rearrange")
    return _wrap(x, einx.id(pattern, data, **sizes), parsed)

reduce(pattern, x, *, op, **sizes)

Reduce over the dims on the input slot that the output slot leaves out.

Parameters:

Name Type Description Default
pattern str

"time band y x -> band y x".

required
x Any

A named carrier.

required
op str | Callable[..., Any]

"sum", "mean", "min", "max", "prod", "std", "var", "any", "all", or a callable taking axis= (applied on the data's backend).

required
**sizes int

Forwarded to einx.

{}

Returns:

Type Description
Any

A carrier with the output slot's dims.

Example

import numpy as np from pipekit_array import DimArray cube = DimArray(np.ones((2, 3, 4)), ("time", "y", "x")) reduce("time y x -> y x", cube, op="sum").data[0, 0] np.float64(2.0)

Source code in packages/pipekit-array/src/pipekit_array/bridges/einx.py
def reduce(pattern: str, x: Any, *, op: str | Callable[..., Any], **sizes: int) -> Any:
    """Reduce over the dims on the input slot that the output slot leaves out.

    Args:
        pattern: ``"time band y x -> band y x"``.
        x: A named carrier.
        op: ``"sum"``, ``"mean"``, ``"min"``, ``"max"``, ``"prod"``,
            ``"std"``, ``"var"``, ``"any"``, ``"all"``, or a callable taking
            ``axis=`` (applied on the data's backend).
        **sizes: Forwarded to einx.

    Returns:
        A carrier with the output slot's dims.

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> cube = DimArray(np.ones((2, 3, 4)), ("time", "y", "x"))
        >>> reduce("time y x -> y x", cube, op="sum").data[0, 0]
        np.float64(2.0)
    """
    einx = _einx()
    parsed = _parse(pattern)
    (x,) = _by_name([x], parsed, "reduce")
    (data,) = _datas([x], "reduce")
    if isinstance(op, str):
        if op not in _REDUCERS:
            raise PatternError(f"reduce: op must be one of {_REDUCERS} or a callable")
        result = getattr(einx, op)(pattern, data, **sizes)
    else:
        result = _backend(data).adapt_numpylike_reduce(op)(pattern, data, **sizes)
    return _wrap(x, result, parsed)

repeat(pattern, x, **sizes)

Broadcast x along new dims (sizes in sizes).

Example

import numpy as np from pipekit_array import DimArray field = DimArray(np.zeros((4, 5)), ("y", "x")) repeat("y x -> member y x", field, member=8).shape (8, 4, 5)

Source code in packages/pipekit-array/src/pipekit_array/bridges/einx.py
def repeat(pattern: str, x: Any, **sizes: int) -> Any:
    """Broadcast ``x`` along new dims (sizes in ``sizes``).

    Example:
        >>> import numpy as np
        >>> from pipekit_array import DimArray
        >>> field = DimArray(np.zeros((4, 5)), ("y", "x"))
        >>> repeat("y x -> member y x", field, member=8).shape
        (8, 4, 5)
    """
    einx = _einx()
    parsed = _parse(pattern)
    (x,) = _by_name([x], parsed, "repeat")
    (data,) = _datas([x], "repeat")
    return _wrap(x, einx.id(pattern, data, **sizes), parsed)

scikit-learn

Estimator fits and applies a scikit-learn-style estimator on any named carrier, through to_rows and from_rows ([sklearn], imported lazily; geostack OP-O3).

pipekit_array.bridges.sklearn

Estimator: one scikit-learn wrapper for every named carrier (geostack OP-O3).

A scikit-learn-style estimator learns from a (n_samples, n_features) table. Estimator chooses the samples by dim name, flattens the carrier with pipekit_array.to_rows, fits or applies the estimator, and rebuilds a carrier like the input with pipekit_array.from_rows:

cube (time, band, y, x) ── Estimator(PCA(3), sample_dims=("time", "y", "x"),
                                     feature_dim="component") ──>
    (time, component, y, x)

It satisfies pipekit_ops.FittableTransformer: fit returns the operator with estimator_ and layout_ set, and transform (and predict, predict_proba, inverse_transform) only read them. Fitting and applying run on the host: scikit-learn computes on NumPy, so a dask input is computed and a JAX input converted, eagerly; inside a JAX trace fit raises.

NaN policies. nan_policy decides what a non-finite entry costs:

  • "propagate" (default): the rows go to the estimator as they are;
  • "raise": any non-finite entry raises, naming the policy;
  • "mask_samples": rows with a non-finite entry are left out of fitting and applying, and come back as NaN in the output;
  • "mask_features": columns non-finite in every fitted row are dropped at fit time and at transform time, and come back as NaN from inverse_transform;
  • "mask": "mask_features" then "mask_samples" on the columns kept, for a land-masked field that also has gaps.

The scikit-learn import is lazy: install the [sklearn] extra.

Estimator

Bases: Operator

A scikit-learn-style estimator fitted and applied on a named carrier.

Parameters:

Name Type Description Default
estimator Any

Any object with scikit-learn's fit and the methods you call (transform, predict, …). It is cloned on fit; the original is never fitted.

required
sample_dims tuple[str, ...] | list[str]

The dims whose entries are samples; every other dim is a feature.

required
feature_dim str | None

The name of the output dim when the estimator's output columns are not the input's features (PCA components, class probabilities). Left None, outputs must have the input's feature count.

None
nan_policy NanPolicy

"propagate", "raise", "mask_samples", "mask_features" or "mask" (module docstring).

'propagate'
method str

What calling the operator runs once fitted: "transform", "predict" or "predict_proba".

'transform'

Raises:

Type Description
ValueError

An unknown nan_policy or method, or no sample dims.

Example

import numpy as np from sklearn.decomposition import PCA from pipekit_array import DimArray rng = np.random.default_rng(0) cube = DimArray(rng.normal(size=(5, 4, 3)), ("band", "y", "x")) pca = Estimator(PCA(2), sample_dims=("y", "x"), feature_dim="component") pca.fit(cube).transform(cube).dims ('component', 'y', 'x')

Source code in packages/pipekit-array/src/pipekit_array/bridges/sklearn.py
@recorded("pipekit_array.estimator")
class Estimator(Operator):
    """A scikit-learn-style estimator fitted and applied on a named carrier.

    Args:
        estimator: Any object with scikit-learn's ``fit`` and the methods
            you call (``transform``, ``predict``, …). It is cloned on
            ``fit``; the original is never fitted.
        sample_dims: The dims whose entries are samples; every other dim
            is a feature.
        feature_dim: The name of the output dim when the estimator's output
            columns are not the input's features (PCA components, class
            probabilities). Left ``None``, outputs must have the input's
            feature count.
        nan_policy: ``"propagate"``, ``"raise"``, ``"mask_samples"``,
            ``"mask_features"`` or ``"mask"`` (module docstring).
        method: What calling the operator runs once fitted:
            ``"transform"``, ``"predict"`` or ``"predict_proba"``.

    Raises:
        ValueError: An unknown ``nan_policy`` or ``method``, or no sample
            dims.

    Example:
        >>> import numpy as np
        >>> from sklearn.decomposition import PCA
        >>> from pipekit_array import DimArray
        >>> rng = np.random.default_rng(0)
        >>> cube = DimArray(rng.normal(size=(5, 4, 3)), ("band", "y", "x"))
        >>> pca = Estimator(PCA(2), sample_dims=("y", "x"), feature_dim="component")
        >>> pca.fit(cube).transform(cube).dims
        ('component', 'y', 'x')
    """

    forbid_in_yaml: ClassVar[bool] = True
    backends: ClassVar[tuple[str, ...]] = ("numpy", "dask")
    __config_exclude__: ClassVar[tuple[str, ...]] = (
        "estimator_",
        "layout_",
        "kept_features_",
    )

    def __init__(
        self,
        estimator: Any,
        *,
        sample_dims: tuple[str, ...] | list[str],
        feature_dim: str | None = None,
        nan_policy: NanPolicy = "propagate",
        method: str = "transform",
    ) -> None:
        if nan_policy not in _POLICIES:
            raise ValueError(
                f"nan_policy must be one of {_POLICIES}; got {nan_policy!r}"
            )
        if method not in _METHODS:
            raise ValueError(f"method must be one of {_METHODS}; got {method!r}")
        if not tuple(sample_dims):
            raise ValueError("Estimator needs at least one sample dim.")
        self.estimator = estimator
        self.sample_dims = tuple(sample_dims)
        self.feature_dim = feature_dim
        self.nan_policy = nan_policy
        self.method = method
        self.estimator_: Any = None
        self.layout_: RowLayout | None = None
        self.kept_features_: np.ndarray | None = None

    # ------------------------------------------------------------------ fit

    def fit(self, x: Any, y: Any = None) -> Self:
        """Learn the estimator from ``x`` (and ``y``, for a supervised one).

        Args:
            x: A named carrier with the sample dims.
            y: Targets: a carrier whose dims are the sample dims (plus at
                most one target dim), or ``None``.

        Returns:
            This operator, fitted.

        Raises:
            TypeError: ``x`` is a JAX tracer (fit eagerly, outside ``jit``).
            ValueError: ``nan_policy="raise"`` and ``x`` has a non-finite
                entry.
        """
        import numpy as np
        from sklearn.base import clone

        rows, layout = to_rows(x, self.sample_dims)
        table = _host(rows, "fit")
        self._check(table)
        if self.nan_policy in ("mask_features", "mask"):
            self.kept_features_ = np.asarray(np.isfinite(table).any(axis=0))
            table = table[:, self.kept_features_]
        keep = self._rows(table)
        targets = None if y is None else _targets(y, self.sample_dims)
        estimator = clone(self.estimator)
        if targets is None:
            estimator.fit(table[keep])
        else:
            estimator.fit(table[keep], targets[keep])
        self.estimator_ = estimator
        # the layout without its template: the training data is not kept alive
        self.layout_ = dataclasses.replace(layout, template=None)
        return self

    # ------------------------------------------------------------- apply

    def transform(self, x: Any) -> Any:
        """The estimator's ``transform`` of ``x``, as a carrier like ``x``."""
        return self._apply_method("transform", x)

    def predict(self, x: Any) -> Any:
        """One prediction per sample: a carrier with the sample dims only."""
        return self._apply_method("predict", x)

    def predict_proba(self, x: Any) -> Any:
        """Class probabilities per sample, along ``feature_dim`` (``"class"``)."""
        return self._apply_method("predict_proba", x)

    def inverse_transform(self, z: Any) -> Any:
        """Map ``transform``'s output back to the fitted features.

        Args:
            z: A carrier with the sample dims and the output dim (``feature_dim``).

        Returns:
            A carrier with the fitted feature dims and sizes in place of the
            output dim; dropped features (``"mask_features"``) are ``NaN``.
        """
        layout = self._fitted()
        rows, z_layout = to_rows(z, self.sample_dims)
        table = _host(rows, "inverse_transform")
        keep = _finite_rows(table) if self._masks_samples else _all(table)
        out_cols = self._restore_features(
            self.estimator_.inverse_transform(table[keep])
        )
        full = _fill_rows(out_cols, keep, layout.n_features)
        target = RowLayout(
            dims=layout.dims,
            sizes=tuple(
                _size(z_layout, d) if d in layout.sample_dims else _size(layout, d)
                for d in layout.dims
            ),
            sample_dims=layout.sample_dims,
            feature_dims=layout.feature_dims,
            template=z,
        )
        return from_rows(full, target)

    def _apply(self, x: Any) -> Any:
        return getattr(self, self.method)(x)

    # ----------------------------------------------------------- helpers

    @property
    def _masks_samples(self) -> bool:
        return self.nan_policy in ("mask_samples", "mask")

    def _fitted(self) -> RowLayout:
        if self.estimator_ is None or self.layout_ is None:
            raise ValueError("Estimator is not fitted: call fit(x) first.")
        return self.layout_

    def _apply_method(self, name: str, x: Any) -> Any:
        import numpy as np

        layout = self._fitted()
        rows, x_layout = to_rows(x, self.sample_dims)
        _require_same_features(x_layout, layout)
        table = _host(rows, name)
        self._check(table)
        if self.kept_features_ is not None:
            table = table[:, self.kept_features_]
        keep = self._rows(table)
        out = np.asarray(getattr(self.estimator_, name)(table[keep]))
        if out.ndim == 1:
            return from_rows(_fill_rows(out, keep, None), x_layout)
        full = _fill_rows(out, keep, out.shape[1])
        if self.feature_dim is None and out.shape[1] == x_layout.n_features:
            return from_rows(full, x_layout)
        dim = self.feature_dim or ("class" if name == "predict_proba" else None)
        if dim is None:
            raise ValueError(
                f"Estimator.{name} returned {out.shape[1]} columns for "
                f"{x_layout.n_features} features; pass feature_dim= to name them."
            )
        return from_rows(full, x_layout, feature_dim=dim)

    def _check(self, table: np.ndarray) -> None:
        """Raise on a non-finite entry under ``nan_policy="raise"``."""
        import numpy as np

        if self.nan_policy == "raise" and not np.isfinite(table).all():
            raise ValueError(
                "Estimator: the input has non-finite entries (nan_policy='raise'); "
                "use 'mask_samples', 'mask_features' or 'mask' to leave them out."
            )

    def _rows(self, table: np.ndarray) -> np.ndarray:
        """The rows to use: the finite ones when the policy masks samples."""
        return _finite_rows(table) if self._masks_samples else _all(table)

    def _restore_features(self, cols: np.ndarray) -> np.ndarray:
        """Re-insert features dropped at fit time as ``NaN`` columns."""
        import numpy as np

        if self.kept_features_ is None:
            return cols
        out = np.full((cols.shape[0], self.kept_features_.size), np.nan)
        out[:, self.kept_features_] = cols
        return out

fit(x, y=None)

Learn the estimator from x (and y, for a supervised one).

Parameters:

Name Type Description Default
x Any

A named carrier with the sample dims.

required
y Any

Targets: a carrier whose dims are the sample dims (plus at most one target dim), or None.

None

Returns:

Type Description
Self

This operator, fitted.

Raises:

Type Description
TypeError

x is a JAX tracer (fit eagerly, outside jit).

ValueError

nan_policy="raise" and x has a non-finite entry.

Source code in packages/pipekit-array/src/pipekit_array/bridges/sklearn.py
def fit(self, x: Any, y: Any = None) -> Self:
    """Learn the estimator from ``x`` (and ``y``, for a supervised one).

    Args:
        x: A named carrier with the sample dims.
        y: Targets: a carrier whose dims are the sample dims (plus at
            most one target dim), or ``None``.

    Returns:
        This operator, fitted.

    Raises:
        TypeError: ``x`` is a JAX tracer (fit eagerly, outside ``jit``).
        ValueError: ``nan_policy="raise"`` and ``x`` has a non-finite
            entry.
    """
    import numpy as np
    from sklearn.base import clone

    rows, layout = to_rows(x, self.sample_dims)
    table = _host(rows, "fit")
    self._check(table)
    if self.nan_policy in ("mask_features", "mask"):
        self.kept_features_ = np.asarray(np.isfinite(table).any(axis=0))
        table = table[:, self.kept_features_]
    keep = self._rows(table)
    targets = None if y is None else _targets(y, self.sample_dims)
    estimator = clone(self.estimator)
    if targets is None:
        estimator.fit(table[keep])
    else:
        estimator.fit(table[keep], targets[keep])
    self.estimator_ = estimator
    # the layout without its template: the training data is not kept alive
    self.layout_ = dataclasses.replace(layout, template=None)
    return self

inverse_transform(z)

Map transform's output back to the fitted features.

Parameters:

Name Type Description Default
z Any

A carrier with the sample dims and the output dim (feature_dim).

required

Returns:

Type Description
Any

A carrier with the fitted feature dims and sizes in place of the

Any

output dim; dropped features ("mask_features") are NaN.

Source code in packages/pipekit-array/src/pipekit_array/bridges/sklearn.py
def inverse_transform(self, z: Any) -> Any:
    """Map ``transform``'s output back to the fitted features.

    Args:
        z: A carrier with the sample dims and the output dim (``feature_dim``).

    Returns:
        A carrier with the fitted feature dims and sizes in place of the
        output dim; dropped features (``"mask_features"``) are ``NaN``.
    """
    layout = self._fitted()
    rows, z_layout = to_rows(z, self.sample_dims)
    table = _host(rows, "inverse_transform")
    keep = _finite_rows(table) if self._masks_samples else _all(table)
    out_cols = self._restore_features(
        self.estimator_.inverse_transform(table[keep])
    )
    full = _fill_rows(out_cols, keep, layout.n_features)
    target = RowLayout(
        dims=layout.dims,
        sizes=tuple(
            _size(z_layout, d) if d in layout.sample_dims else _size(layout, d)
            for d in layout.dims
        ),
        sample_dims=layout.sample_dims,
        feature_dims=layout.feature_dims,
        template=z,
    )
    return from_rows(full, target)

predict(x)

One prediction per sample: a carrier with the sample dims only.

Source code in packages/pipekit-array/src/pipekit_array/bridges/sklearn.py
def predict(self, x: Any) -> Any:
    """One prediction per sample: a carrier with the sample dims only."""
    return self._apply_method("predict", x)

predict_proba(x)

Class probabilities per sample, along feature_dim ("class").

Source code in packages/pipekit-array/src/pipekit_array/bridges/sklearn.py
def predict_proba(self, x: Any) -> Any:
    """Class probabilities per sample, along ``feature_dim`` (``"class"``)."""
    return self._apply_method("predict_proba", x)

transform(x)

The estimator's transform of x, as a carrier like x.

Source code in packages/pipekit-array/src/pipekit_array/bridges/sklearn.py
def transform(self, x: Any) -> Any:
    """The estimator's ``transform`` of ``x``, as a carrier like ``x``."""
    return self._apply_method("transform", x)

Test kit

The test kit ships in the wheel, for other libraries' own test suites. check_adapter and check_kernel_op are for carrier libraries (gates PK-K1 and PK-K4); pipekit's own tests run PK-K2, PK-K3 and, under -m slow, PK-K5. The others are for functions on any array library (ADR A29).

pipekit_array.testing

Checks for the test suites of the libraries built on pipekit-array.

They ship in the wheel, so that a library tests itself with one call instead of copying the check. Each raises AssertionError, saying where and by how much. A check of a number has a twin that returns what it measured, for a notebook or a run report: oracle_error, adjoint_error, equivariance_error, idempotence_error, grad_fd_error, invariant_ratio, taylor_slopes and observed_order.

  • Contracts of a carrier library (gates PK-K1 and PK-K4): check_adapter and check_kernel_op.
  • Array libraries: assert_backends_agree runs a function on each kind of array that is installed (array_kinds), and assert_refuses_dataarrays checks that labels are never lost silently.
  • Values: assert_matches_oracle, assert_adjoint, the dot-product test, and assert_order, the order of convergence of a scheme under refinement.
  • Structure: assert_equivariant (a function commutes with a transform) and assert_idempotent (a function is a projection), on any array library.
  • JAX: assert_traces, assert_grad_matches_fd, assert_finite_gradient, assert_taylor (the Taylor remainder along one direction falls as ε², for a state too large for a Jacobian) and assert_invariant (a field conserves a functional). They skip without JAX, and need its 64-bit mode.
  • Samples: assert_mean, assert_variance and assert_uniform, with thresholds from the sampling distribution.

Importing this module needs NumPy, and neither pytest nor another array library.

ArrayKind dataclass

One kind of array: how to make it, what comes back, and how to read it.

Attributes:

Name Type Description
name str

What it is called in a message, and in kinds=.

make Callable[[ndarray], Any]

An array of this kind from a NumPy one.

type Any

The class, or classes, that a result must have.

host Callable[[Any], ndarray]

The values of a result as NumPy; it checks the device.

Source code in packages/pipekit-array/src/pipekit_array/testing/_arrays.py
@dataclasses.dataclass(frozen=True, slots=True)
class ArrayKind:
    """One kind of array: how to make it, what comes back, and how to read it.

    Attributes:
        name: What it is called in a message, and in ``kinds=``.
        make: An array of this kind from a NumPy one.
        type: The class, or classes, that a result must have.
        host: The values of a result as NumPy; it checks the device.
    """

    name: str
    make: Callable[[np.ndarray], Any]
    type: Any
    host: Callable[[Any], np.ndarray] = np.asarray

    def read(self, array: Any) -> np.ndarray:
        """The array as NumPy, after checking that it is of this kind.

        Raises:
            AssertionError: If it is of another kind, or on another device.

        Example:
            >>> array_kinds()[0].read(np.ones(2))
            array([1., 1.])
        """
        if not isinstance(array, self.type):
            raise AssertionError(
                f"{self.name} in must give {self.name} out, got {type(array).__name__}"
            )
        return self.host(array)

read(array)

The array as NumPy, after checking that it is of this kind.

Raises:

Type Description
AssertionError

If it is of another kind, or on another device.

Example

array_kinds()[0].read(np.ones(2)) array([1., 1.])

Source code in packages/pipekit-array/src/pipekit_array/testing/_arrays.py
def read(self, array: Any) -> np.ndarray:
    """The array as NumPy, after checking that it is of this kind.

    Raises:
        AssertionError: If it is of another kind, or on another device.

    Example:
        >>> array_kinds()[0].read(np.ones(2))
        array([1., 1.])
    """
    if not isinstance(array, self.type):
        raise AssertionError(
            f"{self.name} in must give {self.name} out, got {type(array).__name__}"
        )
    return self.host(array)

adjoint_error(apply, apply_adjoint, n_source, n_target, *, seed=0)

The relative error of the dot-product test, on random NumPy x and r.

|⟨Hx, r⟩ - ⟨x, Gr⟩| / max(‖Hx‖ ‖r‖, ‖x‖ ‖Gr‖)

The measurement behind assert_adjoint. The larger product of norms is the scale, so that a G which is too large also shows. If both products are 0, the error is absolute.

Parameters:

Name Type Description Default
apply Callable[[Any], Any]

H, a linear map from ℝᴺ to ℝᴹ.

required
apply_adjoint Callable[[Any], Any]

G, the claimed adjoint Hᵀ, from ℝᴹ to ℝᴺ.

required
n_source int

N, the length of x.

required
n_target int

M, the length of Hx.

required
seed int

The seed of x and r.

0

Returns:

Type Description
float

The relative error, near the unit round-off for a true adjoint.

Raises:

Type Description
AssertionError

If an output has the wrong shape.

Example

import numpy as np H = np.array([[1.0, 2.0, 0.0], [0.0, 1.0, -1.0]]) adjoint_error(lambda x: H @ x, lambda r: H.T @ r, 3, 2) < 1e-15 True

Source code in packages/pipekit-array/src/pipekit_array/testing/_oracles.py
def adjoint_error(
    apply: Callable[[Any], Any],
    apply_adjoint: Callable[[Any], Any],
    n_source: int,
    n_target: int,
    *,
    seed: int = 0,
) -> float:
    """The relative error of the dot-product test, on random NumPy x and r.

        |⟨Hx, r⟩ - ⟨x, Gr⟩| / max(‖Hx‖ ‖r‖, ‖x‖ ‖Gr‖)

    The measurement behind `assert_adjoint`. The larger product of norms is the
    scale, so that a G which is too large also shows. If both products are 0,
    the error is absolute.

    Args:
        apply: H, a linear map from ℝᴺ to ℝᴹ.
        apply_adjoint: G, the claimed adjoint Hᵀ, from ℝᴹ to ℝᴺ.
        n_source: N, the length of x.
        n_target: M, the length of Hx.
        seed: The seed of x and r.

    Returns:
        The relative error, near the unit round-off for a true adjoint.

    Raises:
        AssertionError: If an output has the wrong shape.

    Example:
        >>> import numpy as np
        >>> H = np.array([[1.0, 2.0, 0.0], [0.0, 1.0, -1.0]])
        >>> adjoint_error(lambda x: H @ x, lambda r: H.T @ r, 3, 2) < 1e-15
        True
    """
    lhs, rhs, scale = _dot_products(apply, apply_adjoint, n_source, n_target, seed)
    return abs(lhs - rhs) if scale == 0.0 else abs(lhs - rhs) / scale

array_kinds() cached

The kinds of array whose library is installed.

The call imports those libraries. In this order: numpy; strict and second device, array_api_strict on its default device and on another one; jax; dask; torch; xarray.

Returns:

Type Description
tuple[ArrayKind, ...]

The kinds, for pytest.mark.parametrize and for kinds=.

Example

array_kinds()[0].name 'numpy'

Source code in packages/pipekit-array/src/pipekit_array/testing/_arrays.py
@functools.cache
def array_kinds() -> tuple[ArrayKind, ...]:
    """The kinds of array whose library is installed.

    The call imports those libraries. In this order: ``numpy``; ``strict`` and
    ``second device``, `array_api_strict` on its default device and on another
    one; ``jax``; ``dask``; ``torch``; ``xarray``.

    Returns:
        The kinds, for ``pytest.mark.parametrize`` and for ``kinds=``.

    Example:
        >>> array_kinds()[0].name
        'numpy'
    """
    kinds = [ArrayKind("numpy", np.asarray, (np.ndarray, np.generic))]
    if _installed("array_api_strict"):
        kinds += _strict_kinds()
    if _installed("jax"):
        import jax

        kinds.append(ArrayKind("jax", jax.numpy.asarray, jax.Array))
    if _installed("dask"):
        import dask.array as da

        kinds.append(ArrayKind("dask", da.from_array, da.Array))
    if _installed("torch"):
        # torch is in no dependency group, so a type checker does not see it
        torch = importlib.import_module("torch")

        def detached(array: Any) -> np.ndarray:
            return array.detach().cpu().numpy()

        kinds.append(ArrayKind("torch", torch.tensor, torch.Tensor, detached))
    if _installed("xarray"):
        import xarray

        kinds.append(ArrayKind("xarray", _labelled, xarray.DataArray))
    return tuple(kinds)

assert_adjoint(apply, apply_adjoint, n_source, n_target, *, seed=0, rtol=1e-12)

Assert the dot-product test ⟨Hx, r⟩ = ⟨x, Gr⟩ on random NumPy x and r.

Parameters:

Name Type Description Default
apply Callable[[Any], Any]

H, a linear map from ℝᴺ to ℝᴹ.

required
apply_adjoint Callable[[Any], Any]

G, the claimed adjoint Hᵀ, from ℝᴹ to ℝᴺ.

required
n_source int

N, the length of x.

required
n_target int

M, the length of Hx.

required
seed int

The seed of x and r.

0
rtol float

The tolerance, relative to max(‖Hx‖ ‖r‖, ‖x‖ ‖Gr‖).

1e-12

Raises:

Type Description
AssertionError

If an output has the wrong shape, or the relative error exceeds rtol.

Example

import numpy as np H = np.array([[1.0, 2.0, 0.0], [0.0, 1.0, -1.0]]) assert_adjoint(lambda x: H @ x, lambda r: H.T @ r, 3, 2)

Source code in packages/pipekit-array/src/pipekit_array/testing/_oracles.py
def assert_adjoint(
    apply: Callable[[Any], Any],
    apply_adjoint: Callable[[Any], Any],
    n_source: int,
    n_target: int,
    *,
    seed: int = 0,
    rtol: float = 1e-12,
) -> None:
    """Assert the dot-product test ⟨Hx, r⟩ = ⟨x, Gr⟩ on random NumPy x and r.

    Args:
        apply: H, a linear map from ℝᴺ to ℝᴹ.
        apply_adjoint: G, the claimed adjoint Hᵀ, from ℝᴹ to ℝᴺ.
        n_source: N, the length of x.
        n_target: M, the length of Hx.
        seed: The seed of x and r.
        rtol: The tolerance, relative to max(‖Hx‖ ‖r‖, ‖x‖ ‖Gr‖).

    Raises:
        AssertionError: If an output has the wrong shape, or the relative error
            exceeds ``rtol``.

    Example:
        >>> import numpy as np
        >>> H = np.array([[1.0, 2.0, 0.0], [0.0, 1.0, -1.0]])
        >>> assert_adjoint(lambda x: H @ x, lambda r: H.T @ r, 3, 2)
    """
    lhs, rhs, scale = _dot_products(apply, apply_adjoint, n_source, n_target, seed)
    if abs(lhs - rhs) > rtol * scale:
        raise AssertionError(
            f"dot-product test failed: <Hx, r> = {lhs!r}, <x, H^T r> = {rhs!r}, "
            f"relative error {abs(lhs - rhs) / scale:.3e} > rtol {rtol}"
        )

assert_backends_agree(fn, *args, rtol=1e-12, atol=0.0, rtol32=1e-05, atol32=0.0, kinds=None, labelled=False)

Assert that a function gives NumPy's numbers on every kind of array.

For each kind: the same kind comes back, on the same device; float64 values agree with NumPy's to rtol and atol; and float32 in gives float32 out, which agrees with NumPy's float64 to rtol32 and atol32. JAX is also checked under jit. Boolean and integer arguments keep their dtype, and boolean and integer results must match exactly.

Parameters:

Name Type Description Default
fn Callable[..., Any]

The function under test; it returns an array or a tuple of them.

required
*args Any

NumPy arrays to call it with; the floating ones are float64.

()
rtol float

Relative tolerance in float64.

1e-12
atol float

Absolute tolerance in float64, for results that cross zero.

0.0
rtol32 float

Relative tolerance of float32 against float64.

1e-05
atol32 float

Absolute tolerance of float32 against float64.

0.0
kinds Collection[str] | None

The names of the kinds to check; one that is not installed is left out. None checks every kind but xarray.

None
labelled bool

Also check DataArrays, for an elementwise function. Their dimensions are named from the right, so they broadcast as NumPy does.

False

Raises:

Type Description
AssertionError

If a check fails; the message names the kind, the dtype and the worst index. Also if JAX is checked without its 64-bit mode.

ValueError

If kinds holds a name that is no kind.

Example

from pipekit_array.arrays import floating_inputs def root(x): ... xp, (x,) = floating_inputs(x) ... return xp.sqrt(x) assert_backends_agree(root, np.arange(3.0), kinds=["numpy", "strict"])

Source code in packages/pipekit-array/src/pipekit_array/testing/_arrays.py
def assert_backends_agree(
    fn: Callable[..., Any],
    *args: Any,
    rtol: float = 1e-12,
    atol: float = 0.0,
    rtol32: float = 1e-5,
    atol32: float = 0.0,
    kinds: Collection[str] | None = None,
    labelled: bool = False,
) -> None:
    """Assert that a function gives NumPy's numbers on every kind of array.

    For each kind: the same kind comes back, on the same device; float64 values
    agree with NumPy's to ``rtol`` and ``atol``; and float32 in gives float32 out,
    which agrees with NumPy's float64 to ``rtol32`` and ``atol32``. JAX is also
    checked under ``jit``. Boolean and integer arguments keep their dtype, and
    boolean and integer results must match exactly.

    Args:
        fn: The function under test; it returns an array or a tuple of them.
        *args: NumPy arrays to call it with; the floating ones are float64.
        rtol: Relative tolerance in float64.
        atol: Absolute tolerance in float64, for results that cross zero.
        rtol32: Relative tolerance of float32 against float64.
        atol32: Absolute tolerance of float32 against float64.
        kinds: The names of the kinds to check; one that is not installed is left
            out. ``None`` checks every kind but ``xarray``.
        labelled: Also check DataArrays, for an elementwise function. Their
            dimensions are named from the right, so they broadcast as NumPy does.

    Raises:
        AssertionError: If a check fails; the message names the kind, the dtype
            and the worst index. Also if JAX is checked without its 64-bit mode.
        ValueError: If ``kinds`` holds a name that is no kind.

    Example:
        >>> from pipekit_array.arrays import floating_inputs
        >>> def root(x):
        ...     xp, (x,) = floating_inputs(x)
        ...     return xp.sqrt(x)
        >>> assert_backends_agree(root, np.arange(3.0), kinds=["numpy", "strict"])
    """
    unknown = set(kinds or ()) - set(_NAMES)
    if unknown:
        msg = f"no such kind of array: {', '.join(sorted(unknown))}"
        raise ValueError(msg)
    arrays = [np.asarray(arg) for arg in args]
    expected = _outputs(fn(*arrays))
    checks = ((np.float64, rtol, atol), (np.float32, rtol32, atol32))
    for kind in array_kinds():
        chosen = kind.name != "xarray" if kinds is None else kind.name in kinds
        if not chosen and not (labelled and kind.name == "xarray"):
            continue
        variants = {kind.name: fn}
        if kind.name == "jax":
            variants["jax.jit"] = require_jax().jit(fn)
        for name, variant in variants.items():
            for dtype, relative, absolute in checks:
                label = f"{name} {np.dtype(dtype).name}"
                results = _outputs(
                    variant(*(kind.make(_cast(array, dtype)) for array in arrays))
                )
                if len(results) != len(expected):
                    raise AssertionError(
                        f"{label}: {len(results)} results, {len(expected)} on NumPy"
                    )
                for got, want in zip(results, expected, strict=True):
                    _compare(kind, got, want, dtype, label, relative, absolute)

assert_equivariant(fn, transform, x, *, transform_out=None, rtol=1e-13)

Assert f(S x) = S' f(x): the function commutes with a transform.

The error max |f(S x) - S' f(x)| must not exceed rtol · max |f(x)|, the maxima taken over every entry of every leaf. Translation in a periodic box is S = S', a roll; a reflection of a vector field needs another S'. It runs on any array library, and on pytrees (dicts, lists and tuples, and any pytree of JAX once JAX is imported).

Pass a state that is random in every entry: a smooth or resting state hides a wrong stencil behind a small output.

Parameters:

Name Type Description Default
fn Callable[[Any], Any]

f, the function under test.

required
transform Callable[[Any], Any]

S, the transform of the input.

required
x Any

The point, an array or a pytree of them.

required
transform_out Callable[[Any], Any] | None

S', the transform of the output; None uses S.

None
rtol float

The tolerance, relative to max |f(x)|.

1e-13

Raises:

Type Description
AssertionError

If the outputs have different structures or shapes, or the relative error exceeds rtol or is not finite.

Example

import numpy as np def laplacian(x): ... return np.roll(x, 1) - 2.0 * x + np.roll(x, -1) x = np.random.default_rng(0).standard_normal(8) assert_equivariant(laplacian, lambda x: np.roll(x, 3), x)

Source code in packages/pipekit-array/src/pipekit_array/testing/_oracles.py
def assert_equivariant(
    fn: Callable[[Any], Any],
    transform: Callable[[Any], Any],
    x: Any,
    *,
    transform_out: Callable[[Any], Any] | None = None,
    rtol: float = 1e-13,
) -> None:
    """Assert f(S x) = S' f(x): the function commutes with a transform.

    The error max |f(S x) - S' f(x)| must not exceed ``rtol`` · max |f(x)|, the
    maxima taken over every entry of every leaf. Translation in a periodic box is
    S = S', a roll; a reflection of a vector field needs another S'. It runs on any
    array library, and on pytrees (dicts, lists and tuples, and any pytree of JAX
    once JAX is imported).

    Pass a state that is random in every entry: a smooth or resting state hides a
    wrong stencil behind a small output.

    Args:
        fn: f, the function under test.
        transform: S, the transform of the input.
        x: The point, an array or a pytree of them.
        transform_out: S', the transform of the output; ``None`` uses S.
        rtol: The tolerance, relative to max |f(x)|.

    Raises:
        AssertionError: If the outputs have different structures or shapes, or
            the relative error exceeds ``rtol`` or is not finite.

    Example:
        >>> import numpy as np
        >>> def laplacian(x):
        ...     return np.roll(x, 1) - 2.0 * x + np.roll(x, -1)
        >>> x = np.random.default_rng(0).standard_normal(8)
        >>> assert_equivariant(laplacian, lambda x: np.roll(x, 3), x)
    """
    error, scale = _equivariance(fn, transform, x, transform_out)
    if not error <= rtol * scale:
        raise AssertionError(
            f"equivariance test failed: max |f(S x) - S' f(x)| = {error:.3e} > "
            f"rtol {rtol} * max |f(x)| = {rtol * scale:.3e}"
        )

assert_finite_gradient(fn, x, *, where=None)

Assert the derivative of every output by each chosen input is finite.

It checks that an undefined or invalid cell does not spoil the others: a single where passes a test of the values and fails this one, because 0 times NaN is NaN in the backward pass. Skips without JAX.

Parameters:

Name Type Description Default
fn Callable[[Any], Any]

The function under test, which JAX can trace.

required
x Any

The point, a float64 array; its invalid cells may be NaN.

required
where Any

A boolean mask of the inputs to check, None for all.

None

Raises:

Type Description
AssertionError

If JAX's 64-bit mode is off, or a derivative is not finite; the message gives the first (output, input) index.

Example

assert_finite_gradient(lambda x: x**2, np.zeros(2)) # doctest: +SKIP

Source code in packages/pipekit-array/src/pipekit_array/testing/_transforms.py
def assert_finite_gradient(
    fn: Callable[[Any], Any], x: Any, *, where: Any = None
) -> None:
    """Assert the derivative of every output by each chosen input is finite.

    It checks that an undefined or invalid cell does not spoil the others: a
    single ``where`` passes a test of the values and fails this one, because 0
    times NaN is NaN in the backward pass. Skips without JAX.

    Args:
        fn: The function under test, which JAX can trace.
        x: The point, a float64 array; its invalid cells may be NaN.
        where: A boolean mask of the inputs to check, ``None`` for all.

    Raises:
        AssertionError: If JAX's 64-bit mode is off, or a derivative is not
            finite; the message gives the first (output, input) index.

    Example:
        >>> assert_finite_gradient(lambda x: x**2, np.zeros(2))  # doctest: +SKIP
    """
    jax = require_jax()
    flat_fn, point = _flat(jax, fn, x)
    jacobian = np.asarray(jax.jit(jax.jacrev(flat_fn))(point))
    chosen = True if where is None else np.ravel(np.broadcast_to(where, np.shape(x)))
    _require_finite("derivatives", jacobian, where=chosen)

assert_grad_matches_fd(fn, x, *, eps=1e-06, rtol=1e-06, indices=None)

Assert jax.jacrev(fn) agrees with central finite differences at x.

For each entry k of the input, (f(x + ε eₖ) - f(x - ε eₖ)) / 2ε approximates column k of the Jacobian. The tolerance is relative to its largest entry, since an entry near zero carries the O(ε²) truncation error at full size, and never below the rounding of f divided by ε. Skips without JAX.

Parameters:

Name Type Description Default
fn Callable[[Any], Any]

The function under test, which JAX can trace.

required
x Any

The point: a float64 array, or a pytree of them.

required
eps float

The step ε.

1e-06
rtol float

The relative tolerance.

1e-06
indices Sequence[int] | None

The entries of the flattened input to check, which keeps the check affordable on a long state; None checks every entry.

None

Raises:

Type Description
AssertionError

If JAX's 64-bit mode is off, if the Jacobians differ, or if an entry of one is not finite; the message gives the (output, input) index, the input counted within indices when given.

Example

assert_grad_matches_fd(lambda x: x**2, np.ones(2)) # doctest: +SKIP

Source code in packages/pipekit-array/src/pipekit_array/testing/_transforms.py
def assert_grad_matches_fd(
    fn: Callable[[Any], Any],
    x: Any,
    *,
    eps: float = 1e-6,
    rtol: float = 1e-6,
    indices: Sequence[int] | None = None,
) -> None:
    """Assert ``jax.jacrev(fn)`` agrees with central finite differences at x.

    For each entry k of the input, (f(x + ε eₖ) - f(x - ε eₖ)) / 2ε approximates
    column k of the Jacobian. The tolerance is relative to its largest entry,
    since an entry near zero carries the O(ε²) truncation error at full size, and
    never below the rounding of f divided by ε. Skips without JAX.

    Args:
        fn: The function under test, which JAX can trace.
        x: The point: a float64 array, or a pytree of them.
        eps: The step ε.
        rtol: The relative tolerance.
        indices: The entries of the flattened input to check, which keeps the
            check affordable on a long state; ``None`` checks every entry.

    Raises:
        AssertionError: If JAX's 64-bit mode is off, if the Jacobians differ, or
            if an entry of one is not finite; the message gives the (output,
            input) index, the input counted within ``indices`` when given.

    Example:
        >>> assert_grad_matches_fd(lambda x: x**2, np.ones(2))  # doctest: +SKIP
    """
    jacobian, expected, largest = _jacobians(fn, x, eps, indices)
    _require_finite("derivatives", jacobian)
    # a NaN scale would let every entry pass
    _require_finite("finite differences", expected)
    scale = float(np.max(np.abs(expected), initial=0.0))
    # the rounding of f, divided by the step: it is what is left at a minimum
    noise = float(np.finfo(expected.dtype).eps) * largest / eps
    assert_matches_oracle(jacobian, expected, rtol=0.0, atol=rtol * scale + noise)

assert_idempotent(fn, x, *, rtol=1e-14)

Assert B(B(x)) = B(x): the function is a projection.

The error max |B(B(x)) - B(x)| must not exceed rtol · max |B(x)|. A fill of ghost cells, or a projection onto a constraint, must pass. It runs on any array library, and on pytrees as assert_equivariant does.

Pass a state that is random in every entry, so that B has something to change.

Parameters:

Name Type Description Default
fn Callable[[Any], Any]

B, the function under test.

required
x Any

The point, an array or a pytree of them.

required
rtol float

The tolerance, relative to max |B(x)|.

1e-14

Raises:

Type Description
AssertionError

If the outputs have different structures or shapes, or the relative error exceeds rtol or is not finite.

Example

import numpy as np x = np.random.default_rng(0).standard_normal(8) assert_idempotent(lambda x: np.clip(x, -0.5, 0.5), x)

Source code in packages/pipekit-array/src/pipekit_array/testing/_oracles.py
def assert_idempotent(fn: Callable[[Any], Any], x: Any, *, rtol: float = 1e-14) -> None:
    """Assert B(B(x)) = B(x): the function is a projection.

    The error max |B(B(x)) - B(x)| must not exceed ``rtol`` · max |B(x)|. A fill of
    ghost cells, or a projection onto a constraint, must pass. It runs on any array
    library, and on pytrees as `assert_equivariant` does.

    Pass a state that is random in every entry, so that B has something to change.

    Args:
        fn: B, the function under test.
        x: The point, an array or a pytree of them.
        rtol: The tolerance, relative to max |B(x)|.

    Raises:
        AssertionError: If the outputs have different structures or shapes, or
            the relative error exceeds ``rtol`` or is not finite.

    Example:
        >>> import numpy as np
        >>> x = np.random.default_rng(0).standard_normal(8)
        >>> assert_idempotent(lambda x: np.clip(x, -0.5, 0.5), x)
    """
    error, scale = _idempotence(fn, x)
    if not error <= rtol * scale:
        raise AssertionError(
            f"idempotence test failed: max |B(B(x)) - B(x)| = {error:.3e} > "
            f"rtol {rtol} * max |B(x)| = {rtol * scale:.3e}"
        )

assert_invariant(functional, field, x, *, rtol=1e-14)

Assert a field conserves a functional: dC/dt = ⟨∇C(x), f(x)⟩ = 0.

With g = ∇C(x) from jax.grad and f = field(x), the conservation ratio

rho = |∑ᵢ gᵢ fᵢ| / ∑ᵢ |gᵢ fᵢ|

lies in [0, 1], has no units, and is 0 in exact arithmetic for an invariant. It must not exceed rtol; if every product is 0, the check passes. A sum of n terms of mixed sign leaves an error near √n times the unit round-off, so the default suits float64. A nonlinear invariant, such as an energy, needs nothing more, since jax.grad gives g. Skips without JAX.

Pass a state that is random in every entry: a smooth or resting state hides a wrong stencil behind a small tendency.

Parameters:

Name Type Description Default
functional Callable[[Any], Any]

C, a scalar function of the state, which JAX can trace.

required
field Callable[[Any], Any]

f, the tendency; it returns a tree of the state's shape.

required
x Any

The state: a float64 array, or a pytree of them.

required
rtol float

The tolerance of rho.

1e-14

Raises:

Type Description
AssertionError

If JAX's 64-bit mode is off, the field's size differs from the state's, or rho exceeds rtol or is not finite; the message gives rho.

Example

import jax.numpy as jnp def centred(x): # a centred difference in a periodic box ... return jnp.roll(x, -1) - jnp.roll(x, 1) x = np.random.default_rng(0).standard_normal(8) assert_invariant(jnp.sum, centred, x) # doctest: +SKIP

Source code in packages/pipekit-array/src/pipekit_array/testing/_transforms.py
def assert_invariant(
    functional: Callable[[Any], Any],
    field: Callable[[Any], Any],
    x: Any,
    *,
    rtol: float = 1e-14,
) -> None:
    """Assert a field conserves a functional: dC/dt = ⟨∇C(x), f(x)⟩ = 0.

    With g = ∇C(x) from ``jax.grad`` and f = field(x), the conservation ratio

        rho = |∑ᵢ gᵢ fᵢ| / ∑ᵢ |gᵢ fᵢ|

    lies in [0, 1], has no units, and is 0 in exact arithmetic for an invariant.
    It must not exceed ``rtol``; if every product is 0, the check passes. A sum
    of n terms of mixed sign leaves an error near √n times the unit round-off, so
    the default suits float64. A nonlinear invariant, such as an energy, needs
    nothing more, since ``jax.grad`` gives g. Skips without JAX.

    Pass a state that is random in every entry: a smooth or resting state hides a
    wrong stencil behind a small tendency.

    Args:
        functional: C, a scalar function of the state, which JAX can trace.
        field: f, the tendency; it returns a tree of the state's shape.
        x: The state: a float64 array, or a pytree of them.
        rtol: The tolerance of rho.

    Raises:
        AssertionError: If JAX's 64-bit mode is off, the field's size differs
            from the state's, or rho exceeds ``rtol`` or is not finite; the message
            gives rho.

    Example:
        >>> import jax.numpy as jnp
        >>> def centred(x):  # a centred difference in a periodic box
        ...     return jnp.roll(x, -1) - jnp.roll(x, 1)
        >>> x = np.random.default_rng(0).standard_normal(8)
        >>> assert_invariant(jnp.sum, centred, x)  # doctest: +SKIP
    """
    ratio = invariant_ratio(functional, field, x)
    if not ratio <= rtol:
        raise AssertionError(
            f"invariant test failed: conservation ratio "
            f"rho = |sum g f| / sum |g f| = {ratio:.3e} > rtol {rtol}"
        )

assert_matches_oracle(actual, expected, *, rtol, atol=0.0, where=None)

Assert |actual - expected| ≤ atol + rtol·|expected| wherever where holds.

NaNs match only NaNs at the same index. Boolean and integer arrays must match exactly, whatever the tolerances.

Parameters:

Name Type Description Default
actual Any

The values under test.

required
expected Any

The reference values, the same shape.

required
rtol float

The relative tolerance.

required
atol float

The absolute tolerance, for references that cross zero.

0.0
where Any

A boolean mask; only True entries are compared. None compares every entry.

None

Raises:

Type Description
AssertionError

If the shapes differ or any compared entry is out of tolerance. The message gives the number of failures, the worst index, both values there, the error and the tolerance.

Example

import numpy as np assert_matches_oracle( ... np.array([1.0, np.nan]), np.array([1.0, np.nan]), rtol=1e-12 ... )

Source code in packages/pipekit-array/src/pipekit_array/testing/_oracles.py
def assert_matches_oracle(
    actual: Any,
    expected: Any,
    *,
    rtol: float,
    atol: float = 0.0,
    where: Any = None,
) -> None:
    """Assert ``|actual - expected| ≤ atol + rtol·|expected|`` wherever ``where`` holds.

    NaNs match only NaNs at the same index. Boolean and integer arrays must
    match exactly, whatever the tolerances.

    Args:
        actual: The values under test.
        expected: The reference values, the same shape.
        rtol: The relative tolerance.
        atol: The absolute tolerance, for references that cross zero.
        where: A boolean mask; only ``True`` entries are compared. ``None``
            compares every entry.

    Raises:
        AssertionError: If the shapes differ or any compared entry is out of
            tolerance. The message gives the number of failures, the worst
            index, both values there, the error and the tolerance.

    Example:
        >>> import numpy as np
        >>> assert_matches_oracle(
        ...     np.array([1.0, np.nan]), np.array([1.0, np.nan]), rtol=1e-12
        ... )
    """
    actual_values = np.asarray(actual)
    expected_values = np.asarray(expected)
    if actual_values.shape != expected_values.shape:
        raise AssertionError(
            f"shape mismatch: actual {actual_values.shape}, "
            f"expected {expected_values.shape}"
        )
    if where is None:
        mask = np.ones(actual_values.shape, dtype=bool)
    else:
        mask = np.broadcast_to(np.asarray(where, dtype=bool), actual_values.shape)
    floating = np.issubdtype(actual_values.dtype, np.inexact) or np.issubdtype(
        expected_values.dtype, np.inexact
    )
    if not floating:
        # Booleans and integers (masks, indices) must match exactly.
        actual_values = actual_values.astype(np.float64)
        expected_values = expected_values.astype(np.float64)
        rtol, atol = 0.0, 0.0

    error = _entry_errors(actual_values, expected_values)
    # τ = atol + rtol·|b|; a NaN or infinite reference contributes no tolerance
    reference_scale = np.where(np.isfinite(expected_values), expected_values, 0.0)
    tolerance = atol + rtol * np.abs(reference_scale)
    failing = mask & (error > tolerance)
    if not failing.any():
        return

    # The worst entry is the one furthest past its own tolerance.
    excess = np.where(failing, error - tolerance, -np.inf)
    worst = np.unravel_index(np.argmax(excess), excess.shape)
    raise AssertionError(
        f"{int(failing.sum())} of {int(mask.sum())} compared entries out of "
        f"tolerance; worst at index {tuple(int(i) for i in worst)}: "
        f"actual {actual_values[worst]!r}, expected {expected_values[worst]!r}, "
        f"error {error[worst]:.3e} > tolerance {tolerance[worst]:.3e} "
        f"(rtol={rtol}, atol={atol})"
    )

assert_mean(sample, expected, *, n_sigma=4.0)

Assert that a sample's mean is the expected one, within its sampling error.

The bound is n_sigma standard errors s/√N of the sample itself: at 4 a right mean fails about once in 16,000 draws. The entries must be independent, so average over anything that shares a draw first.

Parameters:

Name Type Description Default
sample Any

Independent draws, of any shape.

required
expected float

The mean they should have.

required
n_sigma float

The bound, in standard errors.

4.0

Raises:

Type Description
AssertionError

If the mean is further away, if a draw is not finite, or if the sample is too small or has no spread to bound it by.

Example

import numpy as np assert_mean(np.random.default_rng(0).normal(3.0, 1.0, 1000), 3.0)

Source code in packages/pipekit-array/src/pipekit_array/testing/_samples.py
def assert_mean(sample: Any, expected: float, *, n_sigma: float = 4.0) -> None:
    """Assert that a sample's mean is the expected one, within its sampling error.

    The bound is ``n_sigma`` standard errors s/√N of the sample itself: at 4 a
    right mean fails about once in 16,000 draws. The entries must be
    independent, so average over anything that shares a draw first.

    Args:
        sample: Independent draws, of any shape.
        expected: The mean they should have.
        n_sigma: The bound, in standard errors.

    Raises:
        AssertionError: If the mean is further away, if a draw is not finite, or
            if the sample is too small or has no spread to bound it by.

    Example:
        >>> import numpy as np
        >>> assert_mean(np.random.default_rng(0).normal(3.0, 1.0, 1000), 3.0)
    """
    draws = _draws(sample)
    # s/√N
    standard_error = float(np.std(draws, ddof=1)) / np.sqrt(draws.size)
    if standard_error == 0.0:
        raise AssertionError("the sample has no spread, so compare its values exactly")
    distance = abs(float(np.mean(draws)) - expected) / standard_error
    if distance > n_sigma:
        raise AssertionError(
            f"mean {np.mean(draws):.6g} is {distance:.1f} standard errors "
            f"({standard_error:.3g} each) from {expected:.6g}, more than {n_sigma}"
        )

assert_order(errors, expected, *, ratio=2.0, atol=0.2, at_least=True)

Assert a scheme converges with order expected under refinement.

For a scheme of order p the error at spacing Δ is e(Δ) = C Δᵖ + O(Δᵖ⁺¹), so the observed order between levels l and l + 1, refined by ratio, is

pₗ = log(eₗ / eₗ₊₁) / log(ratio).

It approaches p from the coarse end and is spoiled by round-off at the fine end, so the check reads the last order and the caller chooses the levels. With at_least an order above expected passes, as it does for a scheme that is better than its nominal order on a smooth problem.

Parameters:

Name Type Description Default
errors Sequence[float]

The L errors, from the coarsest level to the finest.

required
expected float

The order p of the scheme.

required
ratio float

The refinement ratio r between neighbouring levels, above 1.

2.0
atol float

The tolerance of the last order.

0.2
at_least bool

Whether an order above expected + atol passes.

True

Raises:

Type Description
AssertionError

If there are fewer than two errors, an error is 0 or not finite (the message names its level), or the last order is out of tolerance; the message gives every order.

Example

assert_order([1e-2, 2.5e-3, 6.25e-4], expected=2)

Source code in packages/pipekit-array/src/pipekit_array/testing/_oracles.py
def assert_order(
    errors: Sequence[float],
    expected: float,
    *,
    ratio: float = 2.0,
    atol: float = 0.2,
    at_least: bool = True,
) -> None:
    """Assert a scheme converges with order ``expected`` under refinement.

    For a scheme of order p the error at spacing Δ is e(Δ) = C Δᵖ + O(Δᵖ⁺¹), so
    the observed order between levels l and l + 1, refined by ``ratio``, is

        pₗ = log(eₗ / eₗ₊₁) / log(ratio).

    It approaches p from the coarse end and is spoiled by round-off at the fine
    end, so the check reads the last order and the caller chooses the levels.
    With ``at_least`` an order above ``expected`` passes, as it does for a
    scheme that is better than its nominal order on a smooth problem.

    Args:
        errors: The L errors, from the coarsest level to the finest.
        expected: The order p of the scheme.
        ratio: The refinement ratio r between neighbouring levels, above 1.
        atol: The tolerance of the last order.
        at_least: Whether an order above ``expected + atol`` passes.

    Raises:
        AssertionError: If there are fewer than two errors, an error is 0 or
            not finite (the message names its level), or the last order is out
            of tolerance; the message gives every order.

    Example:
        >>> assert_order([1e-2, 2.5e-3, 6.25e-4], expected=2)
    """
    orders = _orders(errors, ratio)
    last = float(orders[-1])
    low = expected - atol
    if not (last >= low if at_least else abs(last - expected) <= atol):
        bound = f">= {low:g}" if at_least else f"{expected:g} +- {atol:g}"
        raise AssertionError(
            f"order test failed: the last observed order is {last:.3f}, not "
            f"{bound}; the orders are {np.array2string(orders, precision=3)}"
        )

assert_refuses_dataarrays(fn, *args)

Assert that a DataArray in any argument raises BackendError.

A function over a grid or an axis takes plain arrays, so that labels are never lost silently. Skips without xarray.

Parameters:

Name Type Description Default
fn Callable[..., Any]

The function under test.

required
*args Any

NumPy arrays to call it with; each is labelled in turn.

()

Raises:

Type Description
AssertionError

If a call is accepted; the message names the argument.

Example

from pipekit_array.arrays import floating_inputs def plain(x): ... _, (x,) = floating_inputs(x) ... return x assert_refuses_dataarrays(plain, np.ones(2))

Source code in packages/pipekit-array/src/pipekit_array/testing/_arrays.py
def assert_refuses_dataarrays(fn: Callable[..., Any], *args: Any) -> None:
    """Assert that a DataArray in any argument raises `BackendError`.

    A function over a grid or an axis takes plain arrays, so that labels are never
    lost silently. Skips without xarray.

    Args:
        fn: The function under test.
        *args: NumPy arrays to call it with; each is labelled in turn.

    Raises:
        AssertionError: If a call is accepted; the message names the argument.

    Example:
        >>> from pipekit_array.arrays import floating_inputs
        >>> def plain(x):
        ...     _, (x,) = floating_inputs(x)
        ...     return x
        >>> assert_refuses_dataarrays(plain, np.ones(2))
    """
    if not _installed("xarray"):
        raise unittest.SkipTest("xarray is not installed")
    for position, arg in enumerate(args):
        inputs = list(args)
        inputs[position] = _labelled(np.asarray(arg))
        try:
            fn(*inputs)
        except BackendError:
            continue
        raise AssertionError(f"argument {position} accepted a DataArray")

assert_taylor(fn, x, *, min_slope=1.9, steps=_STEPS, seed=0)

Assert the Taylor remainder of fn at x falls as ε², along one direction.

With v a random direction of unit norm and J(x) v from one jax.jvp, the remainder

r(ε) = ‖f(x + ε v) - f(x) - ε J(x) v‖

is O(ε²) when the derivative is right, and O(ε) when it is wrong, so the slope of log r against log ε between neighbouring steps is 2 or 1. Every slope must be at least min_slope; a scheme that switches on a sign, such as an upwind flux, needs about 1.7. If r is below the rounding of f at every step, √D·u·(‖f(x)‖ + ε‖J(x) v‖) with u the unit round-off and D the size of x, f is linear and the check passes. Unlike assert_grad_matches_fd, it never forms the Jacobian: it costs one evaluation of fn per step and one jax.jvp, whatever the size of x. Skips without JAX.

Parameters:

Name Type Description Default
fn Callable[[Any], Any]

The function under test, which JAX can trace; it returns a scalar or a pytree, whose flattened norm is taken.

required
x Any

The point: a float64 array, or a pytree of them.

required
min_slope float

The smallest slope that passes.

1.9
steps Sequence[float]

The steps ε, relative to the norm of x (or absolute at x = 0), from the largest down.

_STEPS
seed int

The seed of the direction v.

0

Raises:

Type Description
AssertionError

If JAX's 64-bit mode is off, or a slope is below min_slope or not finite; the message gives every slope and remainder.

Example

import jax.numpy as jnp assert_taylor(lambda x: jnp.sin(x).sum(), np.ones(3)) # doctest: +SKIP

Source code in packages/pipekit-array/src/pipekit_array/testing/_transforms.py
def assert_taylor(
    fn: Callable[[Any], Any],
    x: Any,
    *,
    min_slope: float = 1.9,
    steps: Sequence[float] = _STEPS,
    seed: int = 0,
) -> None:
    """Assert the Taylor remainder of fn at x falls as ε², along one direction.

    With v a random direction of unit norm and J(x) v from one ``jax.jvp``, the
    remainder

        r(ε) = ‖f(x + ε v) - f(x) - ε J(x) v‖

    is O(ε²) when the derivative is right, and O(ε) when it is wrong, so the
    slope of log r against log ε between neighbouring steps is 2 or 1. Every
    slope must be at least ``min_slope``; a scheme that switches on a sign, such
    as an upwind flux, needs about 1.7. If r is below the rounding of f at every
    step, √D·u·(‖f(x)‖ + ε‖J(x) v‖) with u the unit round-off and D the size of
    x, f is linear and the check passes.
    Unlike `assert_grad_matches_fd`, it never forms the Jacobian: it costs one
    evaluation of fn per step and one ``jax.jvp``, whatever the size of x. Skips
    without JAX.

    Args:
        fn: The function under test, which JAX can trace; it returns a scalar
            or a pytree, whose flattened norm is taken.
        x: The point: a float64 array, or a pytree of them.
        min_slope: The smallest slope that passes.
        steps: The steps ε, relative to the norm of x (or absolute at x = 0),
            from the largest down.
        seed: The seed of the direction v.

    Raises:
        AssertionError: If JAX's 64-bit mode is off, or a slope is below
            ``min_slope`` or not finite; the message gives every slope and
            remainder.

    Example:
        >>> import jax.numpy as jnp
        >>> assert_taylor(lambda x: jnp.sin(x).sum(), np.ones(3))  # doctest: +SKIP
    """
    remainders, floor = _taylor(fn, x, steps, seed)
    if np.all(remainders <= floor):
        return
    slopes = _slopes(steps, remainders)
    if not np.all(slopes >= min_slope):
        raise AssertionError(
            f"Taylor test failed: the slopes of log r(eps) are "
            f"{np.array2string(slopes, precision=3)}, below min_slope {min_slope}, "
            f"for r = {np.array2string(remainders, precision=3)} at the steps "
            f"{list(steps)}"
        )

assert_traces(fn, *args, rtol=1e-12)

Assert jit(fn), vmap(fn) and an eager loop agree over a batch.

Every array of args has a leading batch axis of the same length B. The reference is the eager loop over it, stacked; jit(fn) on each slice, vmap(fn) and jit(vmap(fn)) on the batch must match it. Skips without JAX.

Parameters:

Name Type Description Default
fn Callable[..., Any]

The function under test; it may take and return pytrees.

required
*args Any

The batched arguments; a NumPy array goes to JAX.

()
rtol float

The relative tolerance.

1e-12

Raises:

Type Description
AssertionError

If JAX's 64-bit mode is off, the batch axes differ, or a variant disagrees; the message names the variant.

Example

assert_traces(lambda x: 2.0 * x, np.arange(4.0)) # doctest: +SKIP

Source code in packages/pipekit-array/src/pipekit_array/testing/_transforms.py
def assert_traces(fn: Callable[..., Any], *args: Any, rtol: float = 1e-12) -> None:
    """Assert ``jit(fn)``, ``vmap(fn)`` and an eager loop agree over a batch.

    Every array of ``args`` has a leading batch axis of the same length B. The
    reference is the eager loop over it, stacked; ``jit(fn)`` on each slice,
    ``vmap(fn)`` and ``jit(vmap(fn))`` on the batch must match it. Skips without
    JAX.

    Args:
        fn: The function under test; it may take and return pytrees.
        *args: The batched arguments; a NumPy array goes to JAX.
        rtol: The relative tolerance.

    Raises:
        AssertionError: If JAX's 64-bit mode is off, the batch axes differ, or a
            variant disagrees; the message names the variant.

    Example:
        >>> assert_traces(lambda x: 2.0 * x, np.arange(4.0))  # doctest: +SKIP
    """
    jax = require_jax()
    args = _on_jax(jax, args)
    shapes = [np.shape(leaf) for leaf in jax.tree.leaves(args)]
    if len({shape[:1] for shape in shapes}) != 1 or () in shapes:
        raise AssertionError(f"every array needs the same leading batch axis: {shapes}")

    def stack(outputs: list[Any]) -> Any:
        return jax.tree.map(lambda *leaves: jax.numpy.stack(leaves), *outputs)

    slices = [
        jax.tree.map(lambda leaf, index=index: leaf[index], args)
        for index in range(shapes[0][0])
    ]
    reference = stack([fn(*one) for one in slices])
    jitted = jax.jit(fn)
    variants = {
        "jit(fn)": lambda: stack([jitted(*one) for one in slices]),
        "vmap(fn)": lambda: jax.vmap(fn)(*args),
        "jit(vmap(fn))": lambda: jax.jit(jax.vmap(fn))(*args),
    }
    for name, variant in variants.items():
        got = variant()
        structures = jax.tree.structure(got), jax.tree.structure(reference)
        if structures[0] != structures[1]:
            raise AssertionError(f"{name}: tree {structures[0]} != {structures[1]}")
        pairs = zip(jax.tree.leaves(got), jax.tree.leaves(reference), strict=True)
        for index, (actual, expected) in enumerate(pairs):
            try:
                assert_matches_oracle(actual, expected, rtol=rtol)
            except AssertionError as err:
                raise AssertionError(f"{name}, output {index}: {err}") from err

assert_uniform(sample, low, high, *, level=0.001)

Assert that independent draws are uniform on [low, high].

Parameters:

Name Type Description Default
sample Any

Independent draws, of any shape.

required
low float

The lower end of the interval.

required
high float

Its upper end.

required
level float

The probability that a uniform sample fails.

0.001

Raises:

Type Description
AssertionError

If a draw is outside the interval or not finite, if there are fewer than 30, or if the Kolmogorov-Smirnov statistic exceeds its critical value at N.

Example

import numpy as np assert_uniform( ... np.random.default_rng(0).uniform(2.0, 5.0, 1000), 2.0, 5.0 ... )

Source code in packages/pipekit-array/src/pipekit_array/testing/_samples.py
def assert_uniform(
    sample: Any, low: float, high: float, *, level: float = 1e-3
) -> None:
    """Assert that independent draws are uniform on ``[low, high]``.

    Args:
        sample: Independent draws, of any shape.
        low: The lower end of the interval.
        high: Its upper end.
        level: The probability that a uniform sample fails.

    Raises:
        AssertionError: If a draw is outside the interval or not finite, if
            there are fewer than 30, or if the Kolmogorov-Smirnov statistic
            exceeds its critical value at N.

    Example:
        >>> import numpy as np
        >>> assert_uniform(
        ...     np.random.default_rng(0).uniform(2.0, 5.0, 1000), 2.0, 5.0
        ... )
    """
    draws = _draws(sample)
    outside = int(np.sum((draws < low) | (draws > high)))
    if outside:
        raise AssertionError(f"{outside} of {draws.size} draws outside [{low}, {high}]")
    statistic = ks_statistic(draws, low, high)
    critical = ks_critical_value(draws.size, level)
    if statistic > critical:
        raise AssertionError(
            f"not uniform on [{low}, {high}]: KS statistic {statistic:.4g} exceeds "
            f"{critical:.4g}, its critical value at N = {draws.size}, level {level}"
        )

assert_variance(sample, expected, *, n_sigma=4.0)

Assert that Gaussian draws have the expected variance.

The variance of N Gaussian draws has a relative standard error (2/N)^½, so the bound on s²/σ² - 1 is n_sigma of it. A bias of the scheme counts as an error, so the step must be short enough for it to be smaller.

Parameters:

Name Type Description Default
sample Any

Independent Gaussian draws, of any shape.

required
expected float

The variance they should have.

required
n_sigma float

The bound, in standard errors.

4.0

Raises:

Type Description
AssertionError

If the variance is further away, or if the sample is too small or holds a draw that is not finite.

Example

import numpy as np assert_variance(np.random.default_rng(0).normal(0.0, 2.0, 1000), 4.0)

Source code in packages/pipekit-array/src/pipekit_array/testing/_samples.py
def assert_variance(sample: Any, expected: float, *, n_sigma: float = 4.0) -> None:
    """Assert that Gaussian draws have the expected variance.

    The variance of N Gaussian draws has a relative standard error (2/N)^½, so
    the bound on s²/σ² - 1 is ``n_sigma`` of it. A bias of the scheme counts as
    an error, so the step must be short enough for it to be smaller.

    Args:
        sample: Independent Gaussian draws, of any shape.
        expected: The variance they should have.
        n_sigma: The bound, in standard errors.

    Raises:
        AssertionError: If the variance is further away, or if the sample is too
            small or holds a draw that is not finite.

    Example:
        >>> import numpy as np
        >>> assert_variance(np.random.default_rng(0).normal(0.0, 2.0, 1000), 4.0)
    """
    draws = _draws(sample)
    ratio = float(np.var(draws, ddof=1)) / expected
    # (2/N)^½
    standard_error = np.sqrt(2.0 / draws.size)
    distance = abs(ratio - 1.0) / standard_error
    if distance > n_sigma:
        raise AssertionError(
            f"variance is {ratio:.4f} of {expected:.6g}, {distance:.1f} standard "
            f"errors ({standard_error:.3g} each) from 1, more than {n_sigma}"
        )

check_adapter(adapter, make_carrier, *, rtol=1e-12, seed=0)

Assert that adapter and its fast paths agree with the reference adapter.

Parameters:

Name Type Description Default
adapter NamedArrayAdapter

The adapter under test; it must be the one registered for the carriers make_carrier builds.

required
make_carrier MakeCarrier

(data, dims, labels) -> carrier, building a carrier in the DIMS layout (time band y x, band labels LABELS) from a float64 NumPy array of shape (3, 3, 4, 5). Put the data on the backend under test inside it (jnp.asarray, a dask array).

required
rtol float

Tolerance for reductions; selections compare exactly.

1e-12
seed int

For the random data.

0

Raises:

Type Description
AssertionError

On the first mismatch, naming the function and the carrier type.

Source code in packages/pipekit-array/src/pipekit_array/testing/_carriers.py
def check_adapter(
    adapter: NamedArrayAdapter,
    make_carrier: MakeCarrier,
    *,
    rtol: float = 1e-12,
    seed: int = 0,
) -> None:
    """Assert that ``adapter`` and its fast paths agree with the reference adapter.

    Args:
        adapter: The adapter under test; it must be the one registered for
            the carriers ``make_carrier`` builds.
        make_carrier: ``(data, dims, labels) -> carrier``, building a carrier
            in the `DIMS` layout (``time band y x``, band labels `LABELS`)
            from a float64 NumPy array of shape (3, 3, 4, 5). Put the data on
            the backend under test inside it (``jnp.asarray``, a dask array).
        rtol: Tolerance for reductions; selections compare exactly.
        seed: For the random data.

    Raises:
        AssertionError: On the first mismatch, naming the function and the
            carrier type.
    """
    rng = np.random.default_rng(seed)
    data = rng.random((3, 3, 4, 5))
    carrier = make_carrier(data, DIMS, dict(LABELS))
    reference = DimArray(data, DIMS, dict(LABELS))
    name = type(carrier).__name__
    registered = get_adapter(carrier)
    # the same type: the functions dispatch through the registered instance
    assert type(registered) is type(adapter), (
        f"{name}: get_adapter returns {type(registered).__name__}, "
        f"not the adapter under test {type(adapter).__name__}"
    )
    _check_protocol(adapter, carrier, data, name)
    _check_stack_structure(carrier, name)
    lazy = backend_name(adapter.data(carrier)) == "dask"
    labelled = getattr(adapter, "relabel", None) is not None
    shape_cases = _dim_shape_cases(labelled)
    shift_cases = [*_dim_shift_cases(), *_dim_window_cases()]
    algebra_cases = _dim_algebra_cases(labelled)
    shape_labels = {label for label, _ in [*shape_cases, *shift_cases, *algebra_cases]}
    for label, fn in [*_cases(), *shape_cases, *shift_cases, *algebra_cases]:
        if lazy and label.endswith("where)"):
            continue  # dask's reductions take no where= (reduce raises TypeError)
        expected = fn(reference)
        actual = fn(carrier)
        got = _dims_of(actual)
        want = _dims_of(expected)
        assert got == want, f"{label} on {name}: dims {got}, expected {want}"
        exact = not label.startswith(
            ("reduce", "map_over", "cumreduce", "coarsen", "quantile", "dot")
        )
        if label in shape_labels:
            _check_labels(actual, expected, label, name)
        a, e = values_of(actual), values_of(expected)
        if exact:
            np.testing.assert_array_equal(a, e, err_msg=f"{label} on {name}")
        else:
            np.testing.assert_allclose(a, e, rtol=rtol, err_msg=f"{label} on {name}")

check_kernel_op(op, calls, *, rtol=1e-06)

Assert that one KernelOp agrees across carrier families (PK-K1, PK-K4).

Parameters:

Name Type Description Default
op KernelOp

The operator under test.

required
calls Sequence[Any]

The inputs of each call, one entry per carrier family: a carrier for a one-input kernel, or a tuple of inputs. The first entry is the reference.

required
rtol float

Relative tolerance on the outputs' values.

1e-06

Raises:

Type Description
AssertionError

Outputs differ in dims or values, or a one-input kernel's output dims differ from op.compute_output_signature.

Source code in packages/pipekit-array/src/pipekit_array/testing/_carriers.py
def check_kernel_op(op: KernelOp, calls: Sequence[Any], *, rtol: float = 1e-6) -> None:
    """Assert that one `KernelOp` agrees across carrier families (PK-K1, PK-K4).

    Args:
        op: The operator under test.
        calls: The inputs of each call, one entry per carrier family: a
            carrier for a one-input kernel, or a tuple of inputs. The first
            entry is the reference.
        rtol: Relative tolerance on the outputs' values.

    Raises:
        AssertionError: Outputs differ in dims or values, or a one-input
            kernel's output dims differ from ``op.compute_output_signature``.
    """
    if not calls:
        raise ValueError("check_kernel_op needs at least one call.")
    results = []
    for entry in calls:
        inputs = entry if isinstance(entry, tuple) else (entry,)
        out = op(*inputs)
        outs = out if isinstance(out, tuple) else (out,)
        results.append((inputs, outs))
    _, ref_outs = results[0]
    for inputs, outs in results:
        name = "/".join(type(x).__name__ for x in inputs)
        assert len(outs) == len(ref_outs), (
            f"{name}: {len(outs)} outputs, not {len(ref_outs)}"
        )
        for k, (o, r) in enumerate(zip(outs, ref_outs, strict=True)):
            got, want = _dims_of(o), _dims_of(r)
            assert got == want, f"output {k} on {name}: dims {got}, expected {want}"
            np.testing.assert_allclose(
                values_of(o), values_of(r), rtol=rtol, err_msg=f"output {k} on {name}"
            )
        if len(inputs) == 1:
            _check_signature(op, inputs[0], outs, name)

equivariance_error(fn, transform, x, *, transform_out=None)

The relative error max |f(S x) - S' f(x)| / max |f(x)|.

The measurement behind assert_equivariant, over every entry of every leaf. If f(x) is 0 everywhere, the error is absolute.

Parameters:

Name Type Description Default
fn Callable[[Any], Any]

f, the function under test.

required
transform Callable[[Any], Any]

S, the transform of the input.

required
x Any

The point, an array or a pytree of them.

required
transform_out Callable[[Any], Any] | None

S', the transform of the output; None uses S.

None

Returns:

Type Description
float

The relative error, 0 for an equivariant f; NaN if a value is NaN.

Raises:

Type Description
AssertionError

If the outputs have different structures or shapes.

Example

import numpy as np x = np.array([1.0, 2.0, 4.0]) equivariance_error(lambda x: x**2, lambda x: np.roll(x, 1), x) 0.0

Source code in packages/pipekit-array/src/pipekit_array/testing/_oracles.py
def equivariance_error(
    fn: Callable[[Any], Any],
    transform: Callable[[Any], Any],
    x: Any,
    *,
    transform_out: Callable[[Any], Any] | None = None,
) -> float:
    """The relative error max |f(S x) - S' f(x)| / max |f(x)|.

    The measurement behind `assert_equivariant`, over every entry of every leaf.
    If f(x) is 0 everywhere, the error is absolute.

    Args:
        fn: f, the function under test.
        transform: S, the transform of the input.
        x: The point, an array or a pytree of them.
        transform_out: S', the transform of the output; ``None`` uses S.

    Returns:
        The relative error, 0 for an equivariant f; NaN if a value is NaN.

    Raises:
        AssertionError: If the outputs have different structures or shapes.

    Example:
        >>> import numpy as np
        >>> x = np.array([1.0, 2.0, 4.0])
        >>> equivariance_error(lambda x: x**2, lambda x: np.roll(x, 1), x)
        0.0
    """
    return _relative(*_equivariance(fn, transform, x, transform_out))

grad_fd_error(fn, x, *, eps=1e-06, indices=None)

The relative error max |J - J_fd| / max |J_fd| of JAX's Jacobian at x.

The measurement behind assert_grad_matches_fd: J from jax.jacrev (or jax.jvp for the columns in indices) and J_fd from central finite differences with step ε. It is about ε² for a smooth function, and near 1 for a gradient that is cut. If J_fd is 0 everywhere, the error is absolute. Skips without JAX.

Parameters:

Name Type Description Default
fn Callable[[Any], Any]

The function under test, which JAX can trace.

required
x Any

The point: a float64 array, or a pytree of them.

required
eps float

The step ε.

1e-06
indices Sequence[int] | None

The entries of the flattened input to check; None checks every entry.

None

Returns:

Type Description
float

The relative error; NaN or infinite if a derivative is not finite.

Raises:

Type Description
AssertionError

If JAX's 64-bit mode is off.

Example

grad_fd_error(lambda x: x**2, np.ones(2)) < 1e-8 # doctest: +SKIP True

Source code in packages/pipekit-array/src/pipekit_array/testing/_transforms.py
def grad_fd_error(
    fn: Callable[[Any], Any],
    x: Any,
    *,
    eps: float = 1e-6,
    indices: Sequence[int] | None = None,
) -> float:
    """The relative error max |J - J_fd| / max |J_fd| of JAX's Jacobian at x.

    The measurement behind `assert_grad_matches_fd`: J from ``jax.jacrev`` (or
    ``jax.jvp`` for the columns in ``indices``) and J_fd from central finite
    differences with step ε. It is about ε² for a smooth function, and near 1 for
    a gradient that is cut. If J_fd is 0 everywhere, the error is absolute.
    Skips without JAX.

    Args:
        fn: The function under test, which JAX can trace.
        x: The point: a float64 array, or a pytree of them.
        eps: The step ε.
        indices: The entries of the flattened input to check; ``None`` checks
            every entry.

    Returns:
        The relative error; NaN or infinite if a derivative is not finite.

    Raises:
        AssertionError: If JAX's 64-bit mode is off.

    Example:
        >>> grad_fd_error(lambda x: x**2, np.ones(2)) < 1e-8  # doctest: +SKIP
        True
    """
    jacobian, expected, _ = _jacobians(fn, x, eps, indices)
    error = float(np.max(np.abs(jacobian - expected), initial=0.0))
    scale = float(np.max(np.abs(expected), initial=0.0))
    return error if scale == 0.0 else error / scale

idempotence_error(fn, x)

The relative error max |B(B(x)) - B(x)| / max |B(x)|.

The measurement behind assert_idempotent, over every entry of every leaf. If B(x) is 0 everywhere, the error is absolute.

Parameters:

Name Type Description Default
fn Callable[[Any], Any]

B, the function under test.

required
x Any

The point, an array or a pytree of them.

required

Returns:

Type Description
float

The relative error, 0 for a projection; NaN if a value is NaN.

Raises:

Type Description
AssertionError

If the outputs have different structures or shapes.

Example

import numpy as np idempotence_error(lambda x: 0.5 * x, np.array([2.0, -4.0])) 0.5

Source code in packages/pipekit-array/src/pipekit_array/testing/_oracles.py
def idempotence_error(fn: Callable[[Any], Any], x: Any) -> float:
    """The relative error max |B(B(x)) - B(x)| / max |B(x)|.

    The measurement behind `assert_idempotent`, over every entry of every leaf.
    If B(x) is 0 everywhere, the error is absolute.

    Args:
        fn: B, the function under test.
        x: The point, an array or a pytree of them.

    Returns:
        The relative error, 0 for a projection; NaN if a value is NaN.

    Raises:
        AssertionError: If the outputs have different structures or shapes.

    Example:
        >>> import numpy as np
        >>> idempotence_error(lambda x: 0.5 * x, np.array([2.0, -4.0]))
        0.5
    """
    return _relative(*_idempotence(fn, x))

invariant_ratio(functional, field, x)

The conservation ratio rho = |∑ᵢ gᵢ fᵢ| / ∑ᵢ |gᵢ fᵢ|, g = ∇C(x), f = field(x).

The measurement behind assert_invariant. It lies in [0, 1], has no units, and is 0 in exact arithmetic when the field conserves the functional; it is 0 when every product is 0. Skips without JAX.

Parameters:

Name Type Description Default
functional Callable[[Any], Any]

C, a scalar function of the state, which JAX can trace.

required
field Callable[[Any], Any]

f, the tendency; it returns a tree of the state's shape.

required
x Any

The state: a float64 array, or a pytree of them.

required

Returns:

Type Description
float

rho; NaN if a product is not finite.

Raises:

Type Description
AssertionError

If JAX's 64-bit mode is off, or the field's size differs from the state's.

Example

import jax.numpy as jnp x = np.array([1.0, 2.0]) invariant_ratio(jnp.sum, jnp.ones_like, x) # doctest: +SKIP 1.0

Source code in packages/pipekit-array/src/pipekit_array/testing/_transforms.py
def invariant_ratio(
    functional: Callable[[Any], Any], field: Callable[[Any], Any], x: Any
) -> float:
    """The conservation ratio rho = |∑ᵢ gᵢ fᵢ| / ∑ᵢ |gᵢ fᵢ|, g = ∇C(x), f = field(x).

    The measurement behind `assert_invariant`. It lies in [0, 1], has no units,
    and is 0 in exact arithmetic when the field conserves the functional; it is
    0 when every product is 0. Skips without JAX.

    Args:
        functional: C, a scalar function of the state, which JAX can trace.
        field: f, the tendency; it returns a tree of the state's shape.
        x: The state: a float64 array, or a pytree of them.

    Returns:
        rho; NaN if a product is not finite.

    Raises:
        AssertionError: If JAX's 64-bit mode is off, or the field's size
            differs from the state's.

    Example:
        >>> import jax.numpy as jnp
        >>> x = np.array([1.0, 2.0])
        >>> invariant_ratio(jnp.sum, jnp.ones_like, x)  # doctest: +SKIP
        1.0
    """
    jax = require_jax()
    point, unravel = jax.flatten_util.ravel_pytree(_on_jax(jax, x))
    gradient = np.asarray(jax.grad(lambda flat: functional(unravel(flat)))(point))
    tendency = np.asarray(jax.flatten_util.ravel_pytree(field(unravel(point)))[0])
    if tendency.shape != gradient.shape:
        raise AssertionError(
            f"the field has {tendency.size} entries, the state {gradient.size}"
        )
    products = gradient * tendency
    total = float(np.sum(np.abs(products)))
    return 0.0 if total == 0.0 else abs(float(np.sum(products))) / total

ks_critical_value(size, level)

The large-sample critical value of the Kolmogorov-Smirnov statistic.

c = √(-½ ln(level / 2)) / √N, which is 1.95 / √N at a level of 10⁻³.

Parameters:

Name Type Description Default
size int

N, how many draws.

required
level float

The probability that a sample of the law exceeds it.

required

Returns:

Type Description
float

c.

Example

round(ks_critical_value(100, 1e-3), 3) 0.195

Source code in packages/pipekit-array/src/pipekit_array/testing/_samples.py
def ks_critical_value(size: int, level: float) -> float:
    """The large-sample critical value of the Kolmogorov-Smirnov statistic.

    c = √(-½ ln(level / 2)) / √N, which is 1.95 / √N at a level of 10⁻³.

    Args:
        size: N, how many draws.
        level: The probability that a sample of the law exceeds it.

    Returns:
        c.

    Example:
        >>> round(ks_critical_value(100, 1e-3), 3)
        0.195
    """
    return float(np.sqrt(-0.5 * np.log(level / 2.0)) / np.sqrt(size))

ks_statistic(sample, low, high)

The Kolmogorov-Smirnov distance of a sample from the uniform law.

D = sup_x |F_N(x) - (x - low) / (high - low)|.

Parameters:

Name Type Description Default
sample Any

Independent draws, of any shape.

required
low float

The lower end of the interval.

required
high float

Its upper end.

required

Returns:

Type Description
float

D.

Raises:

Type Description
AssertionError

If there are fewer than 30 draws, or one is not finite.

Example

import numpy as np round(ks_statistic((np.arange(50) + 0.5) / 50, 0.0, 1.0), 12) 0.01

Source code in packages/pipekit-array/src/pipekit_array/testing/_samples.py
def ks_statistic(sample: Any, low: float, high: float) -> float:
    """The Kolmogorov-Smirnov distance of a sample from the uniform law.

    D = sup_x |F_N(x) - (x - low) / (high - low)|.

    Args:
        sample: Independent draws, of any shape.
        low: The lower end of the interval.
        high: Its upper end.

    Returns:
        D.

    Raises:
        AssertionError: If there are fewer than 30 draws, or one is not finite.

    Example:
        >>> import numpy as np
        >>> round(ks_statistic((np.arange(50) + 0.5) / 50, 0.0, 1.0), 12)
        0.01
    """
    draws = np.sort(_draws(sample))
    size = draws.size
    # F(x) = (x - low) / (high - low)
    cdf = (draws - low) / (high - low)
    ranks = np.arange(1, size + 1)
    # D⁺ = max(i/N - F), D⁻ = max(F - (i - 1)/N)
    above = np.max(ranks / size - cdf)
    below = np.max(cdf - (ranks - 1) / size)
    return float(max(above, below))

observed_order(errors, *, ratio=2.0)

The observed orders pₗ = log(eₗ / eₗ₊₁) / log(ratio) of a refinement.

The measurement behind assert_order: for L errors at spacings Δ, Δ/r, …, the L - 1 orders between neighbouring levels.

Parameters:

Name Type Description Default
errors Sequence[float]

The L errors, from the coarsest level to the finest.

required
ratio float

The refinement ratio r between neighbouring levels, above 1.

2.0

Returns:

Type Description
ndarray

The L - 1 observed orders.

Raises:

Type Description
AssertionError

If there are fewer than two errors, or an error is 0 or not finite; the message names its level.

Example

observed_order([1e-2, 2.5e-3, 6.25e-4]) array([2., 2.])

Source code in packages/pipekit-array/src/pipekit_array/testing/_oracles.py
def observed_order(errors: Sequence[float], *, ratio: float = 2.0) -> np.ndarray:
    """The observed orders pₗ = log(eₗ / eₗ₊₁) / log(ratio) of a refinement.

    The measurement behind `assert_order`: for L errors at spacings Δ, Δ/r, …,
    the L - 1 orders between neighbouring levels.

    Args:
        errors: The L errors, from the coarsest level to the finest.
        ratio: The refinement ratio r between neighbouring levels, above 1.

    Returns:
        The L - 1 observed orders.

    Raises:
        AssertionError: If there are fewer than two errors, or an error is 0 or
            not finite; the message names its level.

    Example:
        >>> observed_order([1e-2, 2.5e-3, 6.25e-4])
        array([2., 2.])
    """
    return _orders(errors, ratio)

oracle_error(actual, expected)

The relative error max |actual - expected| / max |expected|.

The measurement behind assert_matches_oracle, which compares each entry with its own tolerance; this gives one number for a table or a run report. NaNs match only NaNs at the same index, and an infinity only itself; any other NaN or infinity makes the error infinite. If max |expected| is 0, or no entry of it is finite, the error is absolute.

Parameters:

Name Type Description Default
actual Any

The values under test.

required
expected Any

The reference values, the same shape.

required

Returns:

Type Description
float

The relative error, 0 when the two agree exactly.

Raises:

Type Description
AssertionError

If the shapes differ.

Example

import numpy as np oracle_error(np.array([1.0, 2.2]), np.array([1.0, 2.0])) 0.10000000000000009

Source code in packages/pipekit-array/src/pipekit_array/testing/_oracles.py
def oracle_error(actual: Any, expected: Any) -> float:
    """The relative error max |actual - expected| / max |expected|.

    The measurement behind `assert_matches_oracle`, which compares each entry
    with its own tolerance; this gives one number for a table or a run report.
    NaNs match only NaNs at the same index, and an infinity only itself; any
    other NaN or infinity makes the error infinite. If max |expected| is 0, or
    no entry of it is finite, the error is absolute.

    Args:
        actual: The values under test.
        expected: The reference values, the same shape.

    Returns:
        The relative error, 0 when the two agree exactly.

    Raises:
        AssertionError: If the shapes differ.

    Example:
        >>> import numpy as np
        >>> oracle_error(np.array([1.0, 2.2]), np.array([1.0, 2.0]))
        0.10000000000000009
    """
    actual_values = np.asarray(actual, dtype=np.float64)
    expected_values = np.asarray(expected, dtype=np.float64)
    if actual_values.shape != expected_values.shape:
        raise AssertionError(
            f"shape mismatch: actual {actual_values.shape}, "
            f"expected {expected_values.shape}"
        )
    error = float(np.max(_entry_errors(actual_values, expected_values), initial=0.0))
    finite = np.where(np.isfinite(expected_values), expected_values, 0.0)
    scale = float(np.max(np.abs(finite), initial=0.0))
    return error if scale == 0.0 else error / scale

taylor_slopes(fn, x, *, steps=_STEPS, seed=0)

The slopes of log r(ε) against log ε between neighbouring steps.

The measurement behind assert_taylor, with the remainder

r(ε) = ‖f(x + ε v) - f(x) - ε J(x) v‖

along a random direction v of unit norm. The slope between steps i and i + 1 is log(rᵢ / rᵢ₊₁) / log(εᵢ / εᵢ₊₁): 2 for a right derivative, 1 for a wrong one, and a fall at the small end shows round-off. Skips without JAX.

Parameters:

Name Type Description Default
fn Callable[[Any], Any]

The function under test, which JAX can trace; it returns a scalar or a pytree, whose flattened norm is taken.

required
x Any

The point: a float64 array, or a pytree of them.

required
steps Sequence[float]

The E steps ε, relative to the norm of x (or absolute at x = 0).

_STEPS
seed int

The seed of the direction v.

0

Returns:

Type Description
ndarray

The E - 1 slopes; NaN where a remainder is 0, as for a linear fn.

Raises:

Type Description
AssertionError

If JAX's 64-bit mode is off.

Example

import jax.numpy as jnp taylor_slopes(lambda x: jnp.sum(x**2), np.ones(3)) # doctest: +SKIP array([2., 2., 2.])

Source code in packages/pipekit-array/src/pipekit_array/testing/_transforms.py
def taylor_slopes(
    fn: Callable[[Any], Any],
    x: Any,
    *,
    steps: Sequence[float] = _STEPS,
    seed: int = 0,
) -> np.ndarray:
    """The slopes of log r(ε) against log ε between neighbouring steps.

    The measurement behind `assert_taylor`, with the remainder

        r(ε) = ‖f(x + ε v) - f(x) - ε J(x) v‖

    along a random direction v of unit norm. The slope between steps i and i + 1
    is log(rᵢ / rᵢ₊₁) / log(εᵢ / εᵢ₊₁): 2 for a right derivative, 1 for a wrong
    one, and a fall at the small end shows round-off. Skips without JAX.

    Args:
        fn: The function under test, which JAX can trace; it returns a scalar
            or a pytree, whose flattened norm is taken.
        x: The point: a float64 array, or a pytree of them.
        steps: The E steps ε, relative to the norm of x (or absolute at x = 0).
        seed: The seed of the direction v.

    Returns:
        The E - 1 slopes; NaN where a remainder is 0, as for a linear fn.

    Raises:
        AssertionError: If JAX's 64-bit mode is off.

    Example:
        >>> import jax.numpy as jnp
        >>> taylor_slopes(lambda x: jnp.sum(x**2), np.ones(3))  # doctest: +SKIP
        array([2., 2., 2.])
    """
    remainders, _ = _taylor(fn, x, steps, seed)
    return _slopes(steps, remainders)

values_of(x)

A named carrier's data on the host, computing it if lazy.

Source code in packages/pipekit-array/src/pipekit_array/testing/_carriers.py
def values_of(x: Any) -> np.ndarray:
    """A named carrier's data on the host, computing it if lazy."""
    adapter = get_adapter(x)
    data = x if adapter is None else adapter.data(x)
    compute = getattr(data, "compute", None)
    if callable(compute) and type(data).__module__.startswith("dask."):
        data = compute()
    return np.asarray(data)