Skip to content

Commit 4d43841

Browse files
manugvsoxofaan
authored andcommitted
Added pixel tolerance to work along with absolute tolerance
1 parent 4e90851 commit 4d43841

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

openeo/testing/results.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ def _compare_xarray_dataarray_xy(
190190
return issues
191191

192192

193+
def _check_pixel_tolerance(
194+
actual: xarray.DataArray, expected: xarray.DataArray, pixel_tolerance: float = _DEFAULT_PIXELTOL
195+
):
196+
"""check if the percentage of pixels that are different is below the threshold"""
197+
assert (
198+
actual != expected
199+
).mean().item() * 100 <= pixel_tolerance, "Percentage number of pixels that are different is above the threshold"
200+
201+
193202
def _compare_xarray_dataarray(
194203
actual: Union[xarray.DataArray, str, Path],
195204
expected: Union[xarray.DataArray, str, Path],
@@ -232,20 +241,24 @@ def _compare_xarray_dataarray(
232241
if actual.shape != expected.shape:
233242
issues.append(f"Shape mismatch: {actual.shape} != {expected.shape}")
234243
compatible = len(issues) == 0
244+
_pixel_tolerance_satisfied = True # flag for pixel tolerance test
235245
try:
236-
if pixel_tolerance > _DEFAULT_PIXELTOL:
237-
assert (
238-
actual != expected
239-
).mean().item() <= pixel_tolerance, "Percentage number of pixels that are different is above the threshold"
240-
else:
241-
xarray.testing.assert_allclose(a=actual, b=expected, rtol=rtol, atol=atol)
246+
xarray.testing.assert_allclose(a=actual, b=expected, rtol=rtol, atol=atol)
242247
except AssertionError as e:
243-
# TODO: message of `assert_allclose` is typically multiline, split it again or make it one line?
244-
issues.append(str(e).strip())
245-
if compatible and {"x", "y"} <= set(expected.dims):
246-
issues.extend(
247-
_compare_xarray_dataarray_xy(actual=actual, expected=expected, rtol=rtol, atol=atol, name=name)
248-
)
248+
if (pixel_tolerance > _DEFAULT_PIXELTOL) and compatible: # skip if there are issues
249+
try:
250+
_check_pixel_tolerance(actual, expected, pixel_tolerance)
251+
except AssertionError as e1:
252+
issues.append(str(e1))
253+
# Used to add the assertion error from assert_allclose to issues
254+
_pixel_tolerance_satisfied = False
255+
elif not _pixel_tolerance_satisfied:
256+
# TODO: message of `assert_allclose` is typically multiline, split it again or make it one line?
257+
issues.append(str(e).strip())
258+
if compatible and {"x", "y"} <= set(expected.dims):
259+
issues.extend(
260+
_compare_xarray_dataarray_xy(actual=actual, expected=expected, rtol=rtol, atol=atol, name=name)
261+
)
249262
return issues
250263

251264

0 commit comments

Comments
 (0)