Skip to content

Commit 9cfca21

Browse files
authored
Merge pull request #772 from Open-EO/761-apex-reference-check-needs-better-representation-ascii-art-diff-diff-image-statistics
761-apex-reference-check-needs-better-representation-ascii-art-diff-diff-image-statistics
2 parents 8bdedb9 + e210660 commit 9cfca21

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

openeo/testing/results.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,25 @@ def _compare_xarray_dataarray_xy(
134134
:return: list of issues (empty if no issues)
135135
"""
136136
issues = []
137-
threshold = abs(expected * rtol) + atol
138-
diff_exact = abs(expected - actual)
137+
138+
actual_as_float = actual.astype(dtype=float)
139+
expected_as_float = expected.astype(dtype=float)
140+
141+
threshold = abs(expected_as_float * rtol) + atol
142+
diff_exact = abs(expected_as_float - actual_as_float)
139143
diff_mask = diff_exact > threshold
140144
diff_lenient = diff_exact.where(diff_mask)
141145

142-
non_x_y_dims = list(set(expected.dims) - {"x", "y"})
143-
value_mapping = dict(map(lambda d: (d, expected[d].data), non_x_y_dims))
146+
non_x_y_dims = list(set(expected_as_float.dims) - {"x", "y"})
147+
value_mapping = dict(map(lambda d: (d, expected_as_float[d].data), non_x_y_dims))
144148
shape = tuple([len(value_mapping[x]) for x in non_x_y_dims])
145149

146150
for shape_index, v in np.ndenumerate(np.ndarray(shape)):
147151
indexers = {}
148152
for index, value_index in enumerate(shape_index):
149153
indexers[non_x_y_dims[index]] = value_mapping[non_x_y_dims[index]][value_index]
150154
diff_data = diff_lenient.sel(indexers=indexers)
151-
total_pixel_count = expected.sel(indexers).count().item()
155+
total_pixel_count = expected_as_float.sel(indexers).count().item()
152156
diff_pixel_count = diff_data.count().item()
153157

154158
if diff_pixel_count > 0:
@@ -230,8 +234,10 @@ def _compare_xarray_dataarray(
230234
except AssertionError as e:
231235
# TODO: message of `assert_allclose` is typically multiline, split it again or make it one line?
232236
issues.append(str(e).strip())
233-
if compatible and {"x", "y"} <= set(expected.dims):
234-
issues.extend(_compare_xarray_dataarray_xy(actual=actual, expected=expected, rtol=rtol, atol=atol, name=name))
237+
if compatible and {"x", "y"} <= set(expected.dims):
238+
issues.extend(
239+
_compare_xarray_dataarray_xy(actual=actual, expected=expected, rtol=rtol, atol=atol, name=name)
240+
)
235241
return issues
236242

237243

tests/testing/test_results.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def test_allclose_xy_success(self, tmp_path, actual_dir, expected_dir):
377377
actual_ds.to_netcdf(actual_dir / "data.nc")
378378
assert_job_results_allclose(actual=actual_dir, expected=expected_dir, tmp_path=tmp_path, rtol=1)
379379

380-
def test_allclose_minimal_xy_different(self, tmp_path, actual_dir, expected_dir):
380+
def test_allclose_xy_different(self, tmp_path, actual_dir, expected_dir):
381381
expected_ds = xarray.Dataset(
382382
{
383383
"b1": xarray.Variable(dims=["t", "x", "y"], data=2 * numpy.ones((3, 4, 5))),
@@ -415,6 +415,71 @@ def test_allclose_minimal_xy_different(self, tmp_path, actual_dir, expected_dir)
415415
):
416416
assert_job_results_allclose(actual=actual_dir, expected=expected_dir, tmp_path=tmp_path)
417417

418+
def test_allclose_bytes_minimal_xy_success(self, tmp_path, actual_dir, expected_dir):
419+
expected_ds = xarray.Dataset(
420+
{
421+
"b1": xarray.Variable(dims=["t", "x", "y"], data=(2 * numpy.ones((3, 4, 5)))),
422+
"b2": xarray.Variable(dims=["t", "x", "y"], data=(3 * numpy.ones((3, 4, 5)))),
423+
},
424+
coords={
425+
"t": range(0, 3),
426+
"x": range(4, 8),
427+
"y": range(5, 10),
428+
},
429+
)
430+
expected_ds.astype(dtype=numpy.dtype("B")).to_netcdf(expected_dir / "data.nc")
431+
actual_ds = xarray.Dataset(
432+
{
433+
"b1": xarray.Variable(dims=["t", "x", "y"], data=(1 * numpy.ones((3, 4, 5)))),
434+
"b2": xarray.Variable(dims=["t", "x", "y"], data=(3 * numpy.ones((3, 4, 5)))),
435+
},
436+
coords={
437+
"t": range(0, 3),
438+
"x": range(4, 8),
439+
"y": range(5, 10),
440+
},
441+
)
442+
actual_ds.astype(dtype=numpy.dtype("B")).to_netcdf(actual_dir / "data.nc")
443+
assert_job_results_allclose(actual=actual_dir, expected=expected_dir, tmp_path=tmp_path, atol=2)
444+
445+
def test_allclose_bytes_minimal_xy_different(self, tmp_path, actual_dir, expected_dir):
446+
expected_ds = xarray.Dataset(
447+
{
448+
"b1": xarray.Variable(dims=["t", "x", "y"], data=(2 * numpy.ones((3, 4, 5)))),
449+
"b2": xarray.Variable(dims=["t", "x", "y"], data=(3 * numpy.ones((3, 4, 5)))),
450+
},
451+
coords={
452+
"t": range(0, 3),
453+
"x": range(4, 8),
454+
"y": range(5, 10),
455+
},
456+
)
457+
expected_ds.astype(dtype=numpy.dtype("B")).to_netcdf(expected_dir / "data.nc")
458+
actual_ds = xarray.Dataset(
459+
{
460+
"b1": xarray.Variable(dims=["t", "x", "y"], data=(1 * numpy.ones((3, 4, 5)))),
461+
"b2": xarray.Variable(dims=["t", "x", "y"], data=(3 * numpy.ones((3, 4, 5)))),
462+
},
463+
coords={
464+
"t": range(0, 3),
465+
"x": range(4, 8),
466+
"y": range(5, 10),
467+
},
468+
)
469+
actual_ds.astype(dtype=numpy.dtype("B")).to_netcdf(actual_dir / "data.nc")
470+
with raises_assertion_error_or_not(
471+
r"Issues for file 'data.nc'.*"
472+
r"Issues for variable 'b1'.*"
473+
r"Left and right DataArray objects are not close.*Differing values:.*"
474+
r"t 0: value difference exceeds tolerance \(rtol 1e-06, atol 0.2\), min:1.0, max: 1.0, mean: 1.0, var: 0.0.*"
475+
r"t 0: differing pixels: 20/20 \(100.0%\), bbox \(\(4, 5\), \(7, 9\)\) - 100.0% of the area.*"
476+
r"t 1: value difference exceeds tolerance \(rtol 1e-06, atol 0.2\), min:1.0, max: 1.0, mean: 1.0, var: 0.0.*"
477+
r"t 1: differing pixels: 20/20 \(100.0%\), bbox \(\(4, 5\), \(7, 9\)\) - 100.0% of the area.*"
478+
r"t 2: value difference exceeds tolerance \(rtol 1e-06, atol 0.2\), min:1.0, max: 1.0, mean: 1.0, var: 0.0.*"
479+
r"t 2: differing pixels: 20/20 \(100.0%\), bbox \(\(4, 5\), \(7, 9\)\) - 100.0% of the area"
480+
):
481+
assert_job_results_allclose(actual=actual_dir, expected=expected_dir, tmp_path=tmp_path, atol=0.2)
482+
418483
def test_allclose_minimal_xy_different_small_area(self, tmp_path, actual_dir, expected_dir):
419484
expected_ds = xarray.Dataset(
420485
{

0 commit comments

Comments
 (0)