pipekit-cycle: protocols & primitives for variational (4DVar) and nudging (BFN) data assimilation
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- python
- Domain
- backend-api-design, data
Research direction
Start with packages/pipekit-cycle/src/pipekit_cycle/protocols.py and review the existing forward.py and obs.py patterns. Then map the proposed protocols and operators across innovation.py, window.py, nudging.py, cycle.py, variational.py, and kernels.py. Done means the contracts and thin composition primitives are added to pipekit-cycle while remaining pure-Python, dependency-free, and algorithm-agnostic.
Written by the indexing model from the issue text.
Description
TL;DR
pipekit-cycle cleanly models the filtering / ensemble family of data assimilation (DACycle, EnsembleDACycle, SmootherCycle). It has no vocabulary for the two other major DA families: variational (3D/4D-Var) and nudging (back-and-forth nudging, BFN).
An audit of the MASSH sea-surface-height mapping codebase (leguillf/MASSH@VarDyn) — which ships a full 4DVar-SW and BFN-QG stack — surfaced a coherent set of missing protocols and composition primitives. This issue proposes adding them while keeping pipekit-cycle algorithm-agnostic: the numerics (minimizer internals, filter math) stay in downstream algorithm libraries (filterx, vardax, …); pipekit ships only the contracts and thin composition operators that those libraries satisfy and compose.
Scope of this issue: protocols + primitives/operators only. The concrete 4DVar/BFN/EnKF algorithms are explicitly out of scope (a follow-up round).
Background & motivation
Classical DA decomposes into three families, and pipekit-cycle currently only spans one:
| Family | Update mechanism | pipekit-cycle today |
|---|---|---|
| Sequential / ensemble | analysis = forecast + K·(obs − H·forecast); Kalman gain from covariances |
✅ DACycle, EnsembleDACycle, SmootherCycle, ObservationOperator, AnalysisStep |
| Variational (3D/4D-Var) | minimize J(x) = ½‖x−x_b‖²_B⁻¹ + ½‖H(x)−y‖²_R⁻¹ over a window, using the adjoint of the forward model and obs operator to get ∇J |
❌ nothing |
| Nudging (BFN) | integrate the model forward and backward, relaxing the state toward observations with a gain term; iterate to convergence | ❌ nothing |
The current protocols (packages/pipekit-cycle/src/pipekit_cycle/protocols.py) encode only the sequential contract:
ForwardModelexposesstep(state, dt)/dt/state_signature— no tangent-linear, no adjoint (protocols.py:35-41).ObservationOperatorexposes__call__+linearize— no adjoint/transpose application (protocols.py:60-62).AnalysisStepis(forecast, obs, *, obs_op, obs_err_cov) → analysis— no notion of a differentiable costJand its gradient (protocols.py:79-86).obs_err_covis threaded as an opaqueAny(DAState.obs_err_cov) — no covariance contract with the√/inv-√operations preconditioning needs.
MASSH demonstrates exactly what's missing. Verbatim from MASSH@VarDyn:
- Model adjoint/TLM are first-class: every dynamical model carries
def step(...),def step_tgl(...),def step_adj(...)—Qgm(models/model_qg1l/jqgm.py),SW(models/model_qgsw/sw.py),CSWm(models/model_sw1l/jswm.py). - Obs operator carries its adjoint: every
Obsop_*hasdef misfit(self, t, State)(forward innovation) anddef adj(self, t, adState, R)(src/obsop.py). - Cost function is explicit:
class Variational→cost,grad,cost_and_grad, withJ = np.float64(0.5 * (Jo + Jb)),Jo += _m.dot(self.R.inv(misfit)),Jb = X0.dot(X0)/np.dot(X0, self.B.inv(X0))(src/tools_4Dvar.py). - Covariance with √ / inv-√:
class Cov→inv(1/sigma**2 * X),sqr(sigma * X),invsqr(1/sigma * X) (src/tools_4Dvar.py). - Reduced control-vector transform: 8
Basis_*classes, each exposingoperg(control→state) andoperg_transpose(its adjoint), driven fromVariationalviaself.basis.operg(...)/operg_transpose(...)(src/basis.py). - Minimizer driver:
Inv_4Dvar→opt.minimize(wrapper, ..., method=config.INV.opt_method, jac=wrapper.jac, callback=callback)wrapped byclass Wrapper, withConvergenceReached/CrazyGradientretry control (src/inv.py). - BFN nudging:
class bfn_qg1l→compute_nudging_term,convergence(path_forth, path_back),update_parameter; plusbfn_nudge_smoothing,bfn_select_obs_temporal_window(src/tools_bfn.py). - Localization kernel:
def gaspari_cohn(r, c)(models/model_qg1l/jqgm.py), reused throughout the basis and nudging tapers.
User stories
- As a variational-DA researcher, I want to express a 4D-Var problem as a pipekit graph — forward model + obs operator + their adjoints + B/R covariances + a reduced control vector — so I can plug in any minimizer and any forward model that satisfies the protocols, without rewriting the outer loop.
- As an algorithm-library author (vardax), I want runtime-checkable protocols for
CostFunction,Covariance, andControlVectorTransformso my classes interop with pipekit cycles without importing pipekit-cycle, exactly as filter classes do today. - As an SSH-mapping practitioner, I want a ready-made
ForwardBackwardCycle+NudgingStepso I can build a BFN-QG mapper by supplying only the QG model and a nudging gain. - As a hybrid-ML modeler, I want to wrap a differentiable (JAX/PyTorch) emulator as a
ForwardModeland have itstangent_linear/adjointcome "for free" from autodiff, then drop it straight into a 4D-Var driver. - As a maintainer, I want these additions to stay pure-Python, dependency-free, and algorithm-agnostic, consistent with the existing protocol-driven design.
API proposal
All additions live in pipekit-cycle. Protocols extend pipekit_cycle.protocols; operators get new modules. Nothing here imports a numerical backend.
1. Adjoint / tangent-linear contracts
# pipekit_cycle/protocols.py (extend)
from typing import Any, Protocol, runtime_checkable
@runtime_checkable
class TangentLinearModel(Protocol):
"""A ForwardModel that also exposes its tangent-linear and adjoint."""
def step(self, state: Any, dt: float) -> Any: ...
def tangent_linear(self, dstate: Any, state: Any, dt: float) -> Any: ...
"""Propagate a perturbation `dstate` linearised about `state`."""
def adjoint(self, adstate: Any, state: Any, dt: float) -> Any: ...
"""Apply the adjoint (transpose) of the tangent-linear step."""
@runtime_checkable
class AdjointObservationOperator(Protocol):
"""An ObservationOperator that can apply H and Hᵀ."""
def __call__(self, state: Any) -> Any: ... # H(x)
def linearize(self, state: Any) -> Any: ... # tangent-linear H
def adjoint(self, dobs: Any, state: Any) -> Any: ... # Hᵀ · dobs
ForwardModel and ObservationOperator stay as-is (backward compatible); the new protocols are the differentiable refinements. A @runtime_checkable isinstance(model, TangentLinearModel) lets a driver detect adjoint support and fall back to autodiff otherwise.
2. Cost function (variational objective)
# pipekit_cycle/protocols.py
@runtime_checkable
class CostFunction(Protocol):
"""A differentiable objective J(x) over a control/state vector."""
def value(self, x: Any) -> float: ...
def grad(self, x: Any) -> Any: ...
def value_and_grad(self, x: Any) -> tuple[float, Any]: ...
3. Covariance (B and R) with sqrt / inverse machinery
# pipekit_cycle/protocols.py
@runtime_checkable
class Covariance(Protocol):
"""Symmetric positive-definite operator with the ops DA needs.
Mirrors MASSH `Cov`: apply, inverse-apply, and the square-root /
inverse-square-root used for preconditioning the control vector.
"""
def apply(self, x: Any) -> Any: ... # C · x
def inv(self, x: Any) -> Any: ... # C⁻¹ · x
def sqrt(self, x: Any) -> Any: ... # C^{1/2} · x
def inv_sqrt(self, x: Any) -> Any: ... # C^{-1/2} · x
4. Control-vector transform (change of variable / B^{1/2})
# pipekit_cycle/protocols.py
@runtime_checkable
class ControlVectorTransform(Protocol):
"""Map a reduced control vector to state space and back (its adjoint).
Mirrors MASSH `Basis.operg` / `Basis.operg_transpose`.
"""
def to_state(self, x_ctrl: Any, t: float | None = None) -> Any: ...
def to_control_adjoint(self, adstate: Any, t: float | None = None) -> Any: ...
5. Minimizer contract
# pipekit_cycle/protocols.py
@runtime_checkable
class Minimizer(Protocol):
"""Drives a CostFunction to a (local) minimum. Adapter over scipy/optax/etc."""
def minimize(self, cost: "CostFunction", x0: Any) -> Any: ...
6. New composition operators
# pipekit_cycle/innovation.py
class Innovation(Operator):
"""Compute the (optionally R-weighted) innovation d = obs − H(forecast)."""
def __init__(self, obs_op: ObservationOperator,
obs_err_cov: Covariance | None = None): ...
# pipekit_cycle/window.py
class ObservationWindow(Operator):
"""Select observations active within [t0, t1] (temporal binning),
mirroring MASSH `bfn_select_obs_temporal_window`."""
def __init__(self, obs_source: Operator, window: float, stride: float): ...
# pipekit_cycle/nudging.py
class NudgingStep(Operator):
"""One relaxation step: x ← x + K · taper · (obs − H(x)).
`gain` (the nudging coefficient K) and `taper` (e.g. Gaspari-Cohn in
space/time) are supplied by the caller. Mirrors `bfn_qg1l.compute_nudging_term`.
"""
def __init__(self, obs_op, gain, taper=None): ...
# pipekit_cycle/cycle.py (new wrapper)
class ForwardBackwardCycle(StatefulOperator):
"""Back-and-forth nudging: integrate forward with NudgingStep, then
backward, iterate until `converged(forth, back) < tol` or `max_iters`.
Mirrors MASSH BFN outer loop (`bfn_qg1l.convergence`)."""
def __init__(self, forward_model, nudging_step, *,
max_iters: int = 10, tol: float = 1e-3): ...
# pipekit_cycle/variational.py (driver; algorithm-agnostic)
class FourDVar(StatefulOperator):
"""Assemble a windowed 4D-Var cost from (model, obs_op, B, R, control)
and hand it to a Minimizer. Computes ∇J via the model+obs adjoints when
available, else via autodiff. Mirrors MASSH `Inv_4Dvar`."""
def __init__(self, forward_model, obs_op, *, background, B, R,
control: ControlVectorTransform | None = None,
minimizer: Minimizer, n_steps: int, precondition: bool = True): ...
7. Correlation-kernel utility
# pipekit_cycle/kernels.py (pure functions; no numpy dependency — duck-typed xp)
def gaspari_cohn(r, c, *, xp=math): ... # compact-support 5th-order piecewise poly
def gaussian_kernel(r, length_scale, *, xp=math): ...
def matern_kernel(r, length_scale, nu=1.5, *, xp=math): ...
Design note. Following the existing pattern (
forward.py,obs.py), each concrete operator is apipekit.Operatorthat also satisfies the relevant protocol structurally, so libraries can supply their own implementations without subclassing.
API examples
4D-Var (sketch). Forward model + obs operator with adjoints, B/R covariances, a wavelet control vector, and a scipy L-BFGS minimizer:
from pipekit_cycle import FourDVar, Innovation
dvar = FourDVar(
forward_model=qg_model, # satisfies TangentLinearModel (has tangent_linear/adjoint)
obs_op=altimetry_obs, # satisfies AdjointObservationOperator
background=x_b,
B=BackgroundCov(sigma=0.1), # satisfies Covariance (apply/inv/sqrt/inv_sqrt)
R=DiagObsCov(sigma=0.03),
control=WaveletControl(...), # satisfies ControlVectorTransform (to_state/to_control_adjoint)
minimizer=ScipyMinimizer(method="L-BFGS-B", maxiter=200),
n_steps=window_steps,
precondition=True, # optimize in B^{1/2} space
)
analysis = dvar(forecast0, DAState(t=0.0, obs_err_cov=R))
BFN-QG (sketch). Back-and-forth nudging into a QG model with a Gaspari-Cohn taper:
from pipekit_cycle import ForwardBackwardCycle, NudgingStep
from pipekit_cycle.kernels import gaspari_cohn
nudge = NudgingStep(
obs_op=ssh_obs,
gain=0.5,
taper=lambda r: gaspari_cohn(r, c=3.0),
)
bfn = ForwardBackwardCycle(forward_model=qg_model, nudging_step=nudge,
max_iters=10, tol=1e-3)
mapped = bfn(first_guess, DAState(t=t0))
Autodiff fallback. A JAX emulator that doesn't hand-code an adjoint still works:
# NeuralForward gains tangent_linear/adjoint via jax.jvp / jax.vjp in an adapter,
# so isinstance(model, TangentLinearModel) is True and FourDVar uses it directly.
TODO
- Add
TangentLinearModel,AdjointObservationOperator,CostFunction,Covariance,ControlVectorTransform,Minimizerprotocols toprotocols.py(+__all__, runtime-checkable). -
kernels.py:gaspari_cohn,gaussian_kernel,matern_kernel(duck-typedxp, pure-Python default). -
innovation.py:Innovationoperator (+ optional R-weighting). -
window.py:ObservationWindowselector. -
nudging.py:NudgingStepoperator. -
cycle.py:ForwardBackwardCyclewrapper (+ convergence callback hook). -
variational.py:FourDVardriver (adjoint-from-protocol with autodiff fallback; preconditioned + unpreconditioned control; checkpointing hook for the adjoint sweep). - Adjoint checkpointing primitive for the trajectory (stride + recompute), so the backward sweep doesn't need the whole forward trajectory in memory (MASSH uses
jax.checkpoint). - Re-export all new symbols from
pipekit_cycle/__init__.py. - Tests:
gaspari_cohncompact-support + partition-of-unity; adjoint/tangent dot-product test helper (<M dx, y> == <dx, Mᵀ y>, mirroring MASSHadjoint_test);FourDVargradient check via finite differences (mirroring MASSHgrad_test); BFN convergence on a linear toy model. - Docs notebook: 4D-Var and BFN on a 1-D toy model (Burgers or linear advection) end-to-end.
- Update
CLAUDE.mdworkspace map + master-plan Report 10 cross-reference.
Open questions
- Should
tangent_linear/adjointbe separate protocols (as proposed) or optional methods onForwardModel/ObservationOperator? Separate protocols keep the base contract minimal and let drivers detect capability viaisinstance. - Does the adjoint-checkpointing primitive belong in
pipekit-cycleor in corepipekit.state? It's reusable beyond DA. Minimizeradapters (scipy, optax, jaxopt) — ship thin adapters here, or leave entirely to downstream libs and only define the protocol?
References
- MASSH (SSH mapping; 4DVar-SW + BFN-QG): https://github.com/leguillf/MASSH/tree/VarDyn
src/tools_4Dvar.py—Cov,Variational(cost/grad/cost_and_grad, Jb/Jo, preconditioning)src/inv.py—Inv_4Dvar,Wrapper,scipy.optimize.minimizedriversrc/tools_bfn.py—bfn_qg1l(nudging term, forward/backward convergence)src/basis.py—Basis_*reduced control vectors (operg/operg_transpose)src/obsop.py—Obsop_*(misfit/adj)models/model_qg1l/jqgm.py—step_tgl/step_adj,gaspari_cohn
- Current pipekit-cycle contracts:
packages/pipekit-cycle/src/pipekit_cycle/protocols.py,forward.py,obs.py,da.py - Master plan Report 10 (pipekit-cycle), §2.3 (protocols), §2.5–2.6 (obs/forward)
- Background:
- Le Dimet & Talagrand (1986), Variational algorithms for analysis and assimilation of meteorological observations. Tellus A.
- Auroux & Blum (2008), A nudging-based data assimilation method: the Back and Forth Nudging algorithm. Nonlin. Processes Geophys.
- Gaspari & Cohn (1999), Construction of correlation functions in two and three dimensions. QJRMS.
- Le Guillou et al. (2021), Mapping altimetry in the forthcoming SWOT era by back-and-forth nudging a one-layer QG model. JTECH.
- Dominant language
- Python
- Stars
- 0
- Forks
- 0
- Avg merge
- 3h 50m
- Merged PRs (30d)
- 143
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from jejjohnson/pipekit
-
area:code layer:0-primitives priority:p2 type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
jejjohnson/pipekit#296 ·
Maintainers usually reply within 1 day
-
area:code type:feature
Difficulty 3/5 1-2 days Newbie friendliness 57/100
jejjohnson/pipekit#411 ·
Maintainers usually reply within 1 day
-
area:code type:feature
Difficulty 4/5 3-5 days Newbie friendliness 48/100
jejjohnson/pipekit#410 ·
Maintainers usually reply within 1 day
-
area:code type:feature
Difficulty 5/5 Over a week Newbie friendliness 38/100
jejjohnson/pipekit#409 ·
Maintainers usually reply within 1 day
-
area:code type:feature
Difficulty 3/5 1-2 days Newbie friendliness 75/100
jejjohnson/pipekit#408 ·
Maintainers usually reply within 1 day
All issues in jejjohnson/pipekit
Similar issues
-
bug(operator): VolterraOperator built inside jax.jit with a concrete decay raises TracerBoolConversionErrorPossibly taken @jejjohnson claimed this today. Opentype:bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
jejjohnson/integrix#144 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
finos/ai-governance-framework#459 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
autowarefoundation/autoware_carla_scenario#2963 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
freelawproject/litigant-portal#1307 ·
Maintainers usually reply within 2 days
-
type/bug
Difficulty 1/5 Under an hour Newbie friendliness 86/100
h2oai/h2o-llmstudio#1163 ·
Maintainers usually reply within 1 day