pycif.plugins.obsoperators.onnx — API reference#
Configuration reference: onnx plugin
- pycif.plugins.obsoperators.onnx.obsoper.obsoper(self, controlvect, obsvect, mode, run_id=0, datei=datetime.datetime(1979, 1, 1, 0, 0), datef=datetime.datetime(2100, 1, 1, 0, 0), workdir='./', reload_results=False, check_transforms=False, ignore_exceptions=False, force_fetch_results=False, **kwargs)[source]#
Apply the ONNX surrogate observation operator.
Forward (
mode='fwd'):\[\mathbf{y}_\text{sim} = \mathcal{N}(\mathbf{x})\]Sets
obsvect.ysimand returns obsvect.Tangent-linear (
mode='tl'):\[\delta\mathbf{y} \approx \frac{\mathcal{N}(\mathbf{x}+\varepsilon\,\delta\mathbf{x}) - \mathcal{N}(\mathbf{x}-\varepsilon\,\delta\mathbf{x})} {2\varepsilon}\]Requires 2 forward passes (central finite differences). Sets both
obsvect.ysimandobsvect.dy; returns obsvect.Adjoint (
mode='adj'):\[\delta\mathbf{x} \approx \mathbf{J}(\mathbf{x})^\top\,\delta\mathbf{y}\]where \(\mathbf{J}\) is built column-by-column via central finite differences (\(2 n_\text{ctl}\) forward passes). The Jacobian is cached and reused for repeated adjoint calls at the same \(\mathbf{x}\).
- Parameters:
self – Obs-operator plugin instance. Must have
_ort_operatorset by a prior call toini_data.controlvect – Control-vector object.
controlvect.xis read infwd/tlmodes;controlvect.dxis read intlmode and written inadjmode.obsvect – Observation-vector object.
obsvect.ysimis written infwd/tlmodes;obsvect.dyis read inadjmode and written intlmode.mode – One of
'fwd','tl', or'adj'.run_id – Run identifier (unused; present for interface compatibility).
datei – Simulation start date (unused).
datef – Simulation end date (unused).
workdir – Working directory (unused).
reload_results – Reload pre-computed results (unused).
check_transforms – Validate transform consistency (unused).
ignore_exceptions – Swallow non-fatal errors (unused).
force_fetch_results – If
True, raiseCifIOErrorimmediately.**kwargs – Additional keyword arguments (ignored).
- Returns:
obsvect in
'fwd'and'tl'modes; controlvect in'adj'.- Raises:
CifIOError – if
force_fetch_resultsisTrue.
Zero-dependency ONNX inference wrapper.
Loads a serialised model from an ONNX file and exposes a forward method
that maps a 1-D state vector to a 1-D observation vector. The only required
dependency is onnxruntime — no PyTorch, JAX, or other ML framework needed.
Example:
from pycif.plugins.obsoperators.onnx.onnx_operator import OnnxSurrogateOperator
op = OnnxSurrogateOperator("surrogate.onnx")
y = op.forward(x) # x: np.ndarray float64, y: np.ndarray float64
- class pycif.plugins.obsoperators.onnx.onnx_operator.OnnxSurrogateOperator(onnx_path: str)[source]#
Bases:
objectDrop-in replacement for a CIF obs operator backed by ONNX Runtime.
- Parameters:
onnx_path – Path to the
.onnxmodel file.
- forward(x: ndarray) ndarray[source]#
Run inference.
- Parameters:
x – Input array of shape
(N_state,)for a single sample or(batch, N_state)for a batch. A 1-D input is temporarily promoted to batch size 1 and squeezed back on output.- Returns:
float64 output array — shape
(N_obs,)for a 1-D input,(batch, N_obs)for batched input.