Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/climatebenchpress/compressor/tests/error_bound.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ def __call__(self, x: xr.DataArray, y: xr.DataArray) -> tuple[bool, float]:
assert x.dtype.kind == "f", f"Expected x to be float, got {x.dtype}"
assert y.dtype.kind == "f", f"Expected y to be float, got {y.dtype}"

abs_error = np.abs(x - y)
relative_error = abs_error / abs(x)

error_to_check = abs_error if self.error_type == "abs_error" else relative_error
satisfied = error_to_check <= self.threshold
if self.error_type == "abs_error":
# absolute error: |x-y| <= threshold
satisfied = np.abs(x - y) <= self.threshold
else:
# relative error: |x-y| / |x| <= threshold
# to avoid dividing by zero, multiply both sides by |x|
satisfied = np.abs(x - y) <= np.abs(x) * self.threshold

# The comparison does not work for NaN values, as `np.nan < threshold` is False.
# This check ensures that if x contains a NaN then y must also contain a NaN at
Expand Down
Loading