Like :py:func:`numpy.testing.assert_allclose`, but for xarray objects. Raises an AssertionError if two objects are not equal up to desired tolerance. Parameters ---------- a : xarray.Dataset, xarray.DataArray or xarray.Variable The first object to compare. b : xarra
(
a, b, rtol=1e-05, atol=1e-08, decode_bytes=True, check_dim_order: bool = True
)
| 198 | |
| 199 | @ensure_warnings |
| 200 | def assert_allclose( |
| 201 | a, b, rtol=1e-05, atol=1e-08, decode_bytes=True, check_dim_order: bool = True |
| 202 | ): |
| 203 | """Like :py:func:`numpy.testing.assert_allclose`, but for xarray objects. |
| 204 | |
| 205 | Raises an AssertionError if two objects are not equal up to desired |
| 206 | tolerance. |
| 207 | |
| 208 | Parameters |
| 209 | ---------- |
| 210 | a : xarray.Dataset, xarray.DataArray or xarray.Variable |
| 211 | The first object to compare. |
| 212 | b : xarray.Dataset, xarray.DataArray or xarray.Variable |
| 213 | The second object to compare. |
| 214 | rtol : float, optional |
| 215 | Relative tolerance. |
| 216 | atol : float, optional |
| 217 | Absolute tolerance. |
| 218 | decode_bytes : bool, optional |
| 219 | Whether byte dtypes should be decoded to strings as UTF-8 or not. |
| 220 | This is useful for testing serialization methods on Python 3 that |
| 221 | return saved strings as bytes. |
| 222 | check_dim_order : bool, optional, default is True |
| 223 | Whether dimensions must be in the same order. |
| 224 | |
| 225 | See Also |
| 226 | -------- |
| 227 | assert_identical, assert_equal, numpy.testing.assert_allclose |
| 228 | """ |
| 229 | __tracebackhide__ = True |
| 230 | assert type(a) is type(b) |
| 231 | b = maybe_transpose_dims(a, b, check_dim_order) |
| 232 | |
| 233 | equiv = functools.partial( |
| 234 | _data_allclose_or_equiv, rtol=rtol, atol=atol, decode_bytes=decode_bytes |
| 235 | ) |
| 236 | equiv.__name__ = "allclose" # type: ignore[attr-defined] |
| 237 | |
| 238 | def compat_variable(a, b): |
| 239 | a = getattr(a, "variable", a) |
| 240 | b = getattr(b, "variable", b) |
| 241 | return a.dims == b.dims and (a._data is b._data or equiv(a.data, b.data)) |
| 242 | |
| 243 | def compat_node(a, b): |
| 244 | return a.ds._coord_names == b.ds._coord_names and utils.dict_equiv( |
| 245 | a.variables, b.variables, compat=compat_variable |
| 246 | ) |
| 247 | |
| 248 | if isinstance(a, Variable): |
| 249 | allclose = compat_variable(a, b) |
| 250 | assert allclose, formatting.diff_array_repr(a, b, compat=equiv) |
| 251 | elif isinstance(a, DataArray): |
| 252 | allclose = utils.dict_equiv( |
| 253 | a.coords, b.coords, compat=compat_variable |
| 254 | ) and compat_variable(a.variable, b.variable) |
| 255 | assert allclose, formatting.diff_array_repr(a, b, compat=equiv) |
| 256 | elif isinstance(a, Dataset): |
| 257 | allclose = a._coord_names == b._coord_names and utils.dict_equiv( |
searching dependent graphs…