Status: not built (phase C of
boundaries.md§4).pipekit_array.qcdoes not exist in pipekit-array 0.3.1, so every operator on this page isproposed. Until it lands,pipekit_ops.operators.AssertThat(an operator as the predicate) andAssertCallable(a function) cover one-off numeric checks.
Four pass-through assertions that look inside the array.
They sit above the QC operators of pipekit_ops.operators, which stay carrier-agnostic (their module docstring defers numeric checks to pipekit-array), and below geoops.qc / xr_toolz.qa, which would subclass these with carrier-specific defaults.
All four follow the same pass-through shape:
on success, return the carrier unchanged;
on failure, raise
QCError(defined inpipekit_ops.operators) or, for ops that takeon_fail="warn", emit aUserWarningand return the carrier unchanged.
The on_fail argument is the same across the family:
| Value | Behaviour |
|---|---|
"raise" | Raise QCError with a message describing the violation. |
"warn" | warnings.warn(...), then return the carrier unchanged. |
Per ADR A6, NaN edge cases are documented per operator; we don’t unify them.
AssertClose(reference: Array, atol: float = 1e-6, rtol: float = 0.0, on_fail: str = "raise")¶
Renamed from Diff by ADR A21: Diff is the operator twin of the dim-level diff (Dim-level functions: the missing ones), and AssertClose matches its Assert* siblings.
Diff ships today (#205); AssertClose is not built yet.
Compare the input to a stored reference array, and raise (or warn) if the per-element absolute difference exceeds atol + rtol * |reference|.
gold = np.load("reference_output.npy")
# proposed (phase C)
checked = AssertClose(gold, atol=1e-5)
guarded = pipe | checked # raises if pipe(x) drifts from goldContract:
_apply(x):xp = array_namespace(x), to dispatch on the input’s backend. The storedself.referenceis always rehydrated as a NumPy array (seeget_config()below), and it is adapted to the input’s backend on every call viaref = xp.asarray(self.reference). This handles the asymmetry where the operator is YAML-loaded with a NumPy reference but applied to JAX, torch or CuPy inputs.delta = xp.abs(x - ref).tol = self.atol + self.rtol * xp.abs(ref).If
xp.any(delta > tol), dispatch peron_fail.Return
xunchanged.
get_config():
{
# The reference is always a NumPy-shaped artifact in the
# serialised form, regardless of what backend produced it at
# capture time. We base64-encode the array's bytes plus shape /
# dtype so YAML round-trips. At load time the reference is
# reconstructed as a NumPy array; `_apply` then converts it to
# the input's backend via `xp.asarray` on every call.
"reference": {"data": b64, "shape": list(ref.shape), "dtype": str(ref.dtype)},
"atol": self.atol,
"rtol": self.rtol,
"on_fail": self.on_fail,
}The cross-backend conversion via xp.asarray(numpy_ref) is expected to be cheap: JAX, torch and CuPy all accept NumPy in asarray, and avoid a copy when the dtypes match and the data is host-resident.
Use case: regression tests for pipelines.
The reference is captured once (during the trusted run), and later runs compare against it.
The atol / rtol combination matches the contract of xp.allclose.
Per-backend note: all five backends support xp.abs and xp.any per the standard.
AssertValueRange(min_val: float | None = None, max_val: float | None = None, on_fail: str = "raise")¶
Pass-through; raise or warn if the input has values outside [min_val, max_val].
Either bound may be None for a one-sided check.
# proposed (phase C)
checked = AssertValueRange(0.0, 1.0) # reflectance bounds
out = checked(arr) # raises if any value < 0 or > 1Contract:
_apply(x):xp = array_namespace(x).oob = xp.zeros_like(x, dtype=bool).If
min_val is not None:oob = oob | (x < self.min_val).If
max_val is not None:oob = oob | (x > self.max_val).If
xp.any(oob), dispatch peron_fail, with a message that gives the count of offending values and (on the raise path) a few examples.Return
xunchanged.
get_config():
{
"min_val": self.min_val, # float | None
"max_val": self.max_val,
"on_fail": self.on_fail,
}NaN handling: NaN values are silently excluded from the comparison (in NumPy, nan < n is False, and the same holds in JAX).
To assert there is no NaN, compose with AssertNoNaN().
Sister-library override pattern (Report 3 §1.5):
# geotoolz/qc.py
# proposed (phase C)
from pipekit_array.qc import AssertValueRange as _AssertValueRange
class AssertValueRange(_AssertValueRange):
"""GeoTensor-aware: reads .fill_value as default bounds."""
def __init__(self, min_val=None, max_val=None, on_fail="raise") -> None:
# If GeoTensor with fill_value, default bounds from it.
super().__init__(min_val=min_val, max_val=max_val, on_fail=on_fail)AssertNoNaN(on_fail: str = "raise")¶
Pass-through; raise or warn if the input contains any NaN.
# proposed (phase C)
checked = AssertNoNaN()
out = checked(arr) # raises if any NaNContract:
_apply(x):xp = array_namespace(x).If
xp.any(xp.isnan(x)), dispatch peron_fail, with a message that gives the NaN count and (for arrays that are not tiny) the fraction of NaN entries.Return
xunchanged.
get_config(): {"on_fail": self.on_fail}.
Per-backend note: xp.isnan is in the Array API standard, and NumPy, JAX, CuPy and PyTorch agree on the result.
Integer dtypes never contain NaN, so the op is a no-op there (fast path: skip if not xp.isdtype(x.dtype, "real floating")).
AssertValidFraction(min_valid: float = 0.5, on_fail: str = "raise")¶
Pass-through; raise or warn if the fraction of non-NaN entries is below min_valid.
It is the “is this data usable” gate.
# proposed (phase C)
checked = AssertValidFraction(min_valid=0.5) # need at least 50% valid
out = checked(arr) # raises if too many NaNsWith the number of entries of the input and its -th entry, the check is
where is min_valid and is the valid fraction.
Contract:
_apply(x):xp = array_namespace(x).If
not xp.isdtype(x.dtype, "real floating"), short-circuit (no NaN is possible, so the valid fraction is 1.0).valid_count = xp.sum(~xp.isnan(x)).total = x.size.valid_fraction = float(valid_count) / total.If
valid_fraction < self.min_valid, dispatch peron_fail, with a message that gives the actual valid fraction and the threshold.Return
xunchanged.
get_config(): {"min_valid": self.min_valid, "on_fail": self.on_fail}.
Distinction from AssertNoNaN: AssertNoNaN requires no NaN (strict), while AssertValidFraction allows up to 1 - min_valid NaN (tolerant of partial cloud cover, sensor gaps and the like).
The two compose: use AssertValidFraction(0.5) upstream of AssertNoNaN(), to guard quality first and then guard the downstream operations that tolerate no NaN.
Per-backend note: xp.sum over a boolean array is in the standard, and all five backends agree on the cast-to-int-then-sum semantics.
Failure messages¶
All four operators raise QCError (a subclass of AssertionError, defined in pipekit_ops.operators) on failure.
The message format is operator-specific but follows a common shape:
QCError: AssertValueRange: 13 / 1024 values out of [0.0, 1.0].
Examples: [1.04, -0.02, 1.71, ...]
QCError: AssertNoNaN: 47 NaN values found (4.6% of 1024).
QCError: AssertValidFraction: only 0.32 of values are valid;
required >= 0.50.
QCError: AssertClose: 3 elements exceed atol=1e-06.
Max abs diff: 2.3e-04. Reference: refs/output_v3.npyThe implementation phase pulls these into pipekit_array.qc._messages, to keep the per-operator code lean.