Skip to content

Shoudn't assert_allclose transpose datasets? #5733

Closed
@jbusecke

Description

@jbusecke

I am trying to compare two datasets, one of which has possibly transposed dimensions on a data variable.

import xarray as xr
import numpy as np
data = np.random.rand(4,6)
da = xr.DataArray(data, dims=['x','y'])
ds1 = xr.Dataset({'data':da})
ds2 = xr.Dataset({'data':da}).transpose('y','x')

What happened:
In my mind this should pass

xr.testing.assert_allclose(ds1, ds2)

but instead it fails

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-7-58cd53174a1e> in <module>
----> 1 xr.testing.assert_allclose(ds1, ds2)

    [... skipping hidden 1 frame]

/srv/conda/envs/notebook/lib/python3.8/site-packages/xarray/testing.py in assert_allclose(a, b, rtol, atol, decode_bytes)
    169             a.variables, b.variables, compat=compat_variable
    170         )
--> 171         assert allclose, formatting.diff_dataset_repr(a, b, compat=equiv)
    172     else:
    173         raise TypeError("{} not supported by assertion comparison".format(type(a)))

AssertionError: Left and right Dataset objects are not close


Differing data variables:
L   data     (x, y) float64 0.8589 0.09264 0.0264 ... 0.1039 0.3685 0.3983
R   data     (y, x) float64 0.8589 0.8792 0.8433 0.6952 ... 0.3664 0.2214 0.3983

Simply transposing ds2 to the same dimensions of ds1 fixes this (since the data is the same after all)

xr.testing.assert_allclose(ds1, ds2.transpose('x','y'))

Since most of the other xarray operations are 'transpose-safe' ((ds1+ds2) = (ds1 + ds2.transpose('x','y')), shouldnt this one be too?

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.8.8 | packaged by conda-forge | (default, Feb 20 2021, 16:22:27)
[GCC 9.3.0]
python-bits: 64
OS: Linux
OS-release: 5.4.109+
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: C.UTF-8
LANG: C.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.10.6
libnetcdf: 4.7.4

xarray: 0.19.0
pandas: 1.2.4
numpy: 1.20.2
scipy: 1.6.2
netCDF4: 1.5.6
pydap: installed
h5netcdf: 0.11.0
h5py: 3.2.1
Nio: None
zarr: 2.7.1
cftime: 1.4.1
nc_time_axis: 1.2.0
PseudoNetCDF: None
rasterio: 1.2.2
cfgrib: 0.9.9.0
iris: None
bottleneck: 1.3.2
dask: 2021.04.1
distributed: 2021.04.1
matplotlib: 3.4.1
cartopy: 0.19.0
seaborn: None
numbagg: None
pint: 0.17
setuptools: 49.6.0.post20210108
pip: 20.3.4
conda: None
pytest: None
IPython: 7.22.0
sphinx: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions