Skip to content

diff formatting for assert_allclose #3617

Closed
@keewis

Description

@keewis

At the moment, the output of a failing assert_allclose is not really useful, it simply prints the values it compared:

In [1]: import numpy as np
   ...: import xarray as xr
   ...: from xarray.testing import assert_allclose, assert_identical

In [2]: da1 = xr.DataArray(data=[2, 5, 8], coords={"x": [0, 1, 2]}, dims="x")
   ...: da2 = xr.DataArray(data=[1, 5, 8], coords={"x": [0, 1, 3]}, dims="x")

In [3]: assert_allclose(da1, da2)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-6-55cbcc1eeb58> in <module>
----> 1 assert_allclose(da1, da2)

~/Documents/Programming/xarray/xarray/testing.py in assert_allclose(a, b, rtol, atol, decode_bytes)
    123         assert allclose, f"{a.values}\n{b.values}"
    124     elif isinstance(a, DataArray):
--> 125         assert_allclose(a.variable, b.variable, **kwargs)
    126         assert set(a.coords) == set(b.coords)
    127         for v in a.coords.variables:

~/Documents/Programming/xarray/xarray/testing.py in assert_allclose(a, b, rtol, atol, decode_bytes)
    121         assert a.dims == b.dims
    122         allclose = _data_allclose_or_equiv(a.values, b.values, **kwargs)
--> 123         assert allclose, f"{a.values}\n{b.values}"
    124     elif isinstance(a, DataArray):
    125         assert_allclose(a.variable, b.variable, **kwargs)

AssertionError: [2 5 8]
[1 5 8]

In [4]: assert_identical(da1, da2)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-7-127e2b1ca0dc> in <module>
----> 1 assert_identical(da1, da2)

~/Documents/Programming/xarray/xarray/testing.py in assert_identical(a, b)
     83     elif isinstance(a, DataArray):
     84         assert a.name == b.name
---> 85         assert a.identical(b), formatting.diff_array_repr(a, b, "identical")
     86     elif isinstance(a, (Dataset, Variable)):
     87         assert a.identical(b), formatting.diff_dataset_repr(a, b, "identical")

AssertionError: Left and right DataArray objects are not identical

Differing values:
L
    array([2, 5, 8])
R
    array([1, 5, 8])
Differing coordinates:
L * x        (x) int64 0 1 2
R * x        (x) int64 0 1 3

It would be good to somehow extend formatting.diff_array_repr and formatting.diff_dataset_repr and to then use those to print a diff for assert_allclose.

I found a reference to this: #1690 (comment), but it's not being actively worked on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions