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.

Sklearn patch-wise Recipe

Open In Colab

This recipe shows how XarrayEstimator composes with patch-wise xarray workflows: fit local sklearn estimators independently on each spatial patch, or fit one global estimator and run memory-bounded inference on patches that preserve the fitted feature axis.

What you’ll learn:

  1. How per-patch fitting works when each spatial tile has its own local statistics.

  2. Why global-fit / patch-infer workflows must patch along the sample axis so every patch keeps the same feature layout seen during fitting.

  3. Where XRDAPatcher slots into the loop without any additional xrtoolz operator.

Python implementation: CPython
Python version       : 3.13.12
IPython version      : 9.16.1

numpy  : 2.5.2
xarray : 2026.7.0
sklearn: 1.9.0
xrops  : 0.4.0

Compiler    : Clang 21.1.4 
OS          : Linux
Release     : 6.8.0-1044-azure
Machine     : x86_64
Processor   : x86_64
CPU cores   : 16
Architecture: 64bit

Synthetic sea-surface-height cube

The examples use a tiny time × lat × lon DataArray so the patch shapes are visible. Real workflows replace the hand-written isel slices below with XRDAPatcher(da=ssh, patches=..., strides=...).

Loading...

Pattern 1 — per-patch local fit

Use this pattern when each patch should learn its own local statistics, such as local scaling before a regional EOF/PCA. With XRDAPatcher, the loop shape is:

from xrpatcher import XRDAPatcher

patcher = XRDAPatcher(da=ssh, patches={"lat": 64, "lon": 64}, strides={"lat": 64, "lon": 64})
scaled = [
    XarrayEstimator(StandardScaler(), sample_dims="time").fit_transform(patcher[i])
    for i in range(len(patcher))
]
out = patcher.reconstruct(scaled)
Loading...

Pattern 2 — global fit, patch-wise inference

A fitted sklearn estimator expects the same feature count during inference that it saw during fitting. For global-fit / patch-infer workflows, patch along the sample axis and keep the feature axis intact. Here the grid is stacked to point × time, PCA is fit globally over all points, and inference runs over point patches that all keep the same time features.

Loading...

The equivalent XRDAPatcher recipe patches the stacked sample dimension and reconstructs the component scores:

from xrpatcher import XRDAPatcher

point_series = ssh.stack(point=("lat", "lon")).transpose("point", "time")
pca = XarrayEstimator(PCA(n_components=10), sample_dims="point", feature_dim="component").fit(point_series)
patcher = XRDAPatcher(da=point_series, patches={"point": 4096}, strides={"point": 4096})
score_patches = [pca.transform(patcher[i]) for i in range(len(patcher))]
scores = patcher.reconstruct(score_patches)

The important invariant is that each patch still has the fitted feature axis (time here). No new xrtoolz operator is needed because XRDAPatcher yields DataArray patches and XarrayEstimator consumes DataArray inputs directly.

Where this lands once OB-1.1 ships

The patch loops above use hand-written isel slices to keep this notebook executable without extra dependencies. The actual XRDAPatcher integration (operator wrappers, overlap-blended reconstruct, runtime caching) is tracked under issue OB-1.1 (#151). After that lands, the loops below shorten to:

from xrtoolz.patcher import PatchDataset, PatchedInference

Sequential([
    PatchDataset(patches={"lat": 64, "lon": 64}, var="ssh"),
    PatchedInference(model=lambda da: scaler.fit_transform(da)),
])(ds)

Until then, the manual isel patterns shown here capture the same compositional shape on xr.DataArray only — no patcher dependency.