E2S-09: Earth2Studio Statistics and Metrics¶
Purpose¶
Statistics reduce a single field; metrics compare a forecast against an
observation. Both operate on Xarray DataArrays, matching the diagnostic model (DX)
interface.
Contract¶
Protocol¶
class Statistic(Protocol):
@property
def reduction_dimensions(self) -> list[str]: ...
def output_coords(self, input_coords: CoordinateSystem) -> CoordinateSystem: ...
def __call__(self, x: xr.DataArray) -> xr.DataArray: ...
class Metric(Protocol):
@property
def reduction_dimensions(self) -> list[str]: ...
def output_coords(self, input_coords: CoordinateSystem) -> CoordinateSystem: ...
def __call__(self, x: xr.DataArray, y: xr.DataArray) -> xr.DataArray: ...
xis the forecast,ythe observation (Metriconly).CoordinateSystemis an allocation-freeDataArraysignature (seeearth2studio.utils.coords).output_coords(x)has the same dims, shape, and coordinates as the result of__call__.- There is no
coordsargument and no tuple return. Tensor/CoordSystemcalls are unsupported.
Dimensions and Alignment¶
- Reduction dimensions are located by name in
x.dims. A missing dimension raisesValueErrorin__call__andKeyErrorinoutput_coords. - Reduced dimensions are removed; surviving dimensions keep their order and
coordinates. Added dimensions (
threshold,window_size,wavenumber) are appended;metric,histogram_data, andbinlead. ymust have the same dimensions asx(minusensemble_dimensionwhen given; the ensemble dimension must exist inxand not iny).yis transposed toxorder and coordinates are compared by value. Mismatches raiseValueError.- Dimension names reserved by a method (
threshold,window_size) may not appear in the inputs (ValueError).
Weights¶
Weights are constructor arguments (torch.Tensor or np.ndarray, for example from
lat_weight). They have ndim == len(reduction_dimensions) (ValueError) and a
shape equal to the sizes of the reduction dimensions in x order (AssertionError).
None means uniform weights.
Backends¶
- Inputs may be NumPy, CuPy, or Torch backed. All numerical computation uses
PyTorch: inputs are converted with
x.e2s.to_torch()and results are returned withfrom_torch. - Outputs follow the configured e2s backend policy. The default
autopolicy returns NumPy on CPU and CuPy on CUDA; thetorchpolicy preserves tensors and their autograd history. - Weights are placed directly on the input tensor's device. Uniform weights use float32, and float16/bfloat16 weights are promoted to float32 for reductions.
crps,rank_histogram, andlog_spectral_distancerequire thestatisticsextra (PhysicsNeMo).
Methods¶
mean,std,variance:batch_updatekeeps running sums across calls.meanwith no reduction dims is the identity;varianceandstdrequire at least one.acc: requirestimeandvariable. The climatology is aDataSource(fetched withfetch_datawhenlead_timeis present) orNonefor zero.brier_score: takesthresholdsand an optionalensemble_dimension; adds a trailingthresholddim.crps:ensemble_dimension(str) with optionalreduction_dimensionsandweights;fairselects the unbiased estimator.rank_histogram: leadinghistogram_data(bin_centers,bin_counts) andbindims. Bins default to ensemble size + 1;randomize_tiesbreaks ties.energy_score:ensemble_dimensionand non-emptymultivariate_dimensions.fairneeds at least 2 members (ValueError).fss:window_sizesandthresholds.spatial_dimensionsdefault to the last two and must be reduced; spatial coordinates are cropped by the window margin. Adds trailingthresholdandwindow_sizedims.log_spectral_distance:spatial_dimensionsdefault to the last two; addswavenumberbefore reduction; optionalwavenumber_cutoff.rmse,mae: optionalensemble_dimension(ensemble mean is taken first).spread_skill_ratio,skill_spread:skill_spreadhas a leadingmetricdim (mse,variance).
Custom Statistics¶
Any object with the members above satisfies the protocol. A Statistic used by
workflows (for example the seasonal statistics example) is called as stat(x) and
its output_coords(x) describes the result without computing it.