Coverage for src/scores/probability/checks.py: 100%
17 statements
« prev ^ index » next coverage.py v7.3.2, created at 2024-02-28 12:51 +1100
« prev ^ index » next coverage.py v7.3.2, created at 2024-02-28 12:51 +1100
1"""
2This module contains methods which make assertions at runtime about the state of various data
3structures and values
4"""
6import numpy as np
7import xarray as xr
10def coords_increasing(da: xr.DataArray, dim: str):
11 """Checks if coordinates in a given DataArray are increasing.
13 Note: No in-built raise if `dim` is not a dimension of `da`.
15 Args:
16 da (xr.DataArray): Input data
17 dim (str): Dimension to check if increasing
18 Returns:
19 (bool): Returns True if coordinates along `dim` dimension of
20 `da` are increasing, False otherwise.
21 """
22 result = (da[dim].diff(dim) > 0).all()
23 return result
26def cdf_values_within_bounds(cdf: xr.DataArray) -> bool:
27 """Checks that 0 <= cdf <= 1. Ignores NaNs.
29 Args:
30 cdf (xr.DataArray): array of CDF values
32 Returns:
33 (bool): `True` if `cdf` values are all between 0 and 1 whenever values are not NaN,
34 or if all values are NaN; and `False` otherwise.
35 """
36 return cdf.count() == 0 or ((cdf.min() >= 0) & (cdf.max() <= 1))
39def check_nan_decreasing_inputs(cdf, threshold_dim, tolerance):
40 """Checks inputs to `nan_decreasing_cdfs` and `_decreasing_cdfs`."""
42 if threshold_dim not in cdf.dims:
43 raise ValueError(f"'{threshold_dim}' is not a dimension of `cdf`")
45 if tolerance < 0:
46 raise ValueError("`tolerance` must be nonnegative.")
48 if not coords_increasing(cdf, threshold_dim):
49 raise ValueError(f"Coordinates along '{threshold_dim}' dimension should be increasing.")
51 all_nan_or_no_nan = np.isnan(cdf).all(threshold_dim) | (~np.isnan(cdf)).all(threshold_dim)
52 if not all_nan_or_no_nan.all():
53 raise ValueError("CDFs should have no NaNs or be all NaN along `threshold_dim`")