Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diff formatting for assert_allclose #3617

Closed
keewis opened this issue Dec 12, 2019 · 0 comments · Fixed by #3847
Closed

diff formatting for assert_allclose #3617

keewis opened this issue Dec 12, 2019 · 0 comments · Fixed by #3847

Comments

@keewis
Copy link
Collaborator

keewis commented Dec 12, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant