Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Bridge modules — shared overview

The three new submodules (einx, linalg, prob) are independent bridges but share the same shape, the same dim-bookkeeping convention, and the same place in the xrtoolz layer stack. This doc fixes the shared model so the per-module docs only need to say what’s different.

Why these three

xrtoolz today is strong on Earth-system diagnostics (operators that read a Dataset and return a Dataset of derived quantities). The three things diagnostic pipelines reach for that don’t fit that mold — and don’t have an xarray-native home — are:

  1. Named-tensor algebra. Multi-axis einsums, rearranges, reductions over the dims a DataArray already carries. Today this means da.values → numpy → einops / np.einsum → wrap back. The round-trip drops coords and dim names that the user already wrote down.

  2. Structured linear algebra. Covariance operators, Kalman steps, GP solves, low-rank updates over spatiotemporal grids. gaussx handles the math elegantly, but the user manually flattens (time, lat, lon) → (N,), calls gaussx.solve(K, y_flat), and reshapes the result. The labeled metadata is lost in the middle.

  3. Probabilistic modeling. Loc/scale parameters whose values are DataArrays (e.g. a Normal(loc=climatology, scale=stddev) where both are (month, lat, lon)-shaped). numpyro plates implement the right semantics but require careful manual indexing.

Each module adds the missing label-preserving layer between xarray and its respective backend. None of them reimplement the backend.

Place in the stack

┌──────────────────────────────────────────────────────────────────┐
│  Layer 2 — Graph                                                  │
│  pipekit_ops.Graph DAGs, ModelOp inference wrappers                   │
├──────────────────────────────────────────────────────────────────┤
│  Layer 1 — Operators                                              │
│  xrtoolz.Operator subclasses; Sequential chains                  │
│  ┌─ existing ──────────────┐  ┌─ new ────────────────────────┐   │
│  │ geo, ocn, atm, rs, ice  │  │ einx, linalg, prob ops       │   │
│  └─────────────────────────┘  └──────────────────────────────┘   │
├──────────────────────────────────────────────────────────────────┤
│  Layer 0 — Primitives                                             │
│  Pure functions over DataArray (and Dataset adapters)            │
│  ┌─ existing ──────────────┐  ┌─ new ────────────────────────┐   │
│  │ regrid, detrend, masks  │  │ einsum, solve, sample, …     │   │
│  └─────────────────────────┘  └──────────────────────────────┘   │
└──────────────────────────────────────────────────────────────────┘
                       │
       ┌───────────────┼─────────────────────────┐
       ▼               ▼                         ▼
   ┌─────────┐    ┌─────────┐               ┌─────────┐
   │  einx   │    │ gaussx  │ ── lineax ──▶ │ numpyro │
   └─────────┘    └─────────┘    matfree    └─────────┘
                                            └─ pyrox ─

The new modules sit beside the existing domain submodules in Layer 0/1 and inherit DataTree dispatch, Sequential, Graph, and Augment behaviour from the existing core unchanged.

The contract

Every Layer-0 function in the three bridges obeys:

def f(*arrays: DataArray, **named_args) -> DataArray:
    ...

Layer-1 wrappers

For every Layer-0 function xrtoolz.<module>.foo(da, ...) there is an Operator subclass xrtoolz.<module>.Foo whose _apply(da) calls the function. Operators are how the bridges enter Sequential / Graph / Augment. The convention follows the existing xrtoolz.geo pattern:

from xrtoolz import Sequential, Augment
from xrtoolz.einx import Rearrange
from xrtoolz.linalg import Solve
from xrtoolz.prob import Sample

pipeline = Sequential([
    Rearrange("time lat lon -> (lat lon) time"),
    Solve(operator=K),                    # gaussx structured op
    Sample(prior, key=key, sample_shape=()),
])

Augment / ApplyToEach from xrtoolz.combinators continue to work, since these operators inherit xrtoolz.Operator.

Dependencies and install

BridgeNew runtime depsExtra
einxeinx (and its numpy/jax/torch backend choice)core dep (no extra; see note)
linalggaussx, lineax, jaxpip install xrtoolz[linalg]
probnumpyro, pyrox, jaxpip install xrtoolz[prob]

einx is a core dependency (v0.2.0). Unlike linalg / prob, einx is installed with base xrtoolz rather than behind an extra, because core kernels across the package adopt it. This reverses the einx decision D9. import xrtoolz still stays light — the xrtoolz.einx submodule is not imported at package init and imports einx lazily inside its functions.

All three sit behind lazy imports (xrtoolz D4 pattern: see src/xrtoolz/inference/__init__.py). import xrtoolz never imports JAX, einx, numpyro, or anything heavy. Importing xrtoolz.linalg.solve imports gaussx the first time the function is called, not at module load.

Backend-array policy

xarray is array-library-agnostic — a DataArray can wrap numpy, dask, jax, cupy, or any duck array. The bridges keep that property:

Non-goals (shared)

Open shared questions

  1. Naming. linalg collides with numpy.linalg mental model; prob is short but generic. Alternatives considered: xrtoolz.{gauss,ops,stats}. Sticking with {einx, linalg, prob} for now — see per-module decisions docs.

  2. Coord realignment. Should einx.einsum(...) reindex on shared dims with mismatched coords (a la xarray’s default), or raise (a la numpy semantics)? Default raise; opt-in align=True flag.

  3. DataTree story. Inheriting leaf-wise dispatch is free; what we don’t have is cross-leaf ops (a Kronecker product whose factors live on different leaves). Out of scope for v1.