Skip to content

Commit dbf9307

Browse files
shoyerJoe Hamman
authored andcommitted
Fix a bug in assert_allclose where rtol and atol were ignored (#1488)
* Fix a bug in assert_allclose where rtol and atol were ignored * Add regression test
1 parent 96e6e8f commit dbf9307

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ Bug fixes
4343
case). See (:issue:`1477`) for details.
4444
By `Prakhar Goel <https://github.com/newt0311>`_.
4545

46+
- Fix :py:func:`xarray.testing.assert_allclose` to actually use ``atol`` and
47+
``rtol`` arguments when called on ``DataArray`` objects.
48+
By `Stephan Hoyer <http://github.com/shoyer>`_.
49+
4650
.. _whats-new.0.9.6:
4751

4852
v0.9.6 (8 June 2017)

xarray/testing.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,26 @@ def assert_allclose(a, b, rtol=1e-05, atol=1e-08, decode_bytes=True):
116116
import xarray as xr
117117
___tracebackhide__ = True # noqa: F841
118118
assert type(a) == type(b)
119+
kwargs = dict(rtol=rtol, atol=atol, decode_bytes=decode_bytes)
119120
if isinstance(a, xr.Variable):
120121
assert a.dims == b.dims
121-
allclose = _data_allclose_or_equiv(a.values, b.values,
122-
rtol=rtol, atol=atol,
123-
decode_bytes=decode_bytes)
122+
allclose = _data_allclose_or_equiv(a.values, b.values, **kwargs)
124123
assert allclose, '{}\n{}'.format(a.values, b.values)
125124
elif isinstance(a, xr.DataArray):
126-
assert_allclose(a.variable, b.variable)
125+
assert_allclose(a.variable, b.variable, **kwargs)
127126
assert set(a.coords) == set(b.coords)
128127
for v in a.coords.variables:
129128
# can't recurse with this function as coord is sometimes a
130129
# DataArray, so call into _data_allclose_or_equiv directly
131130
allclose = _data_allclose_or_equiv(a.coords[v].values,
132-
b.coords[v].values,
133-
rtol=rtol, atol=atol,
134-
decode_bytes=decode_bytes)
131+
b.coords[v].values, **kwargs)
135132
assert allclose, '{}\n{}'.format(a.coords[v].values,
136133
b.coords[v].values)
137134
elif isinstance(a, xr.Dataset):
138135
assert set(a) == set(b)
139136
assert set(a.coords) == set(b.coords)
140137
for k in list(a.variables) + list(a.coords):
141-
assert_allclose(a[k], b[k], rtol=rtol, atol=atol,
142-
decode_bytes=decode_bytes)
138+
assert_allclose(a[k], b[k], **kwargs)
143139

144140
else:
145141
raise TypeError('{} not supported by assertion comparison'

xarray/tests/test_testing.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
5+
import xarray as xr
6+
7+
8+
def test_allclose_regression():
9+
x = xr.DataArray(1.01)
10+
y = xr.DataArray(1.02)
11+
xr.testing.assert_allclose(x, y, atol=0.01)

0 commit comments

Comments
 (0)