From 8ff4e3ca9c2042ecbf0387403ac41fa6c93a916a Mon Sep 17 00:00:00 2001 From: Matti Eskelinen Date: Fri, 3 Nov 2017 16:56:00 +0200 Subject: [PATCH 1/9] Add coordinate name to test failure output --- xarray/testing.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xarray/testing.py b/xarray/testing.py index f51e474405f..af989366a29 100644 --- a/xarray/testing.py +++ b/xarray/testing.py @@ -129,7 +129,8 @@ def assert_allclose(a, b, rtol=1e-05, atol=1e-08, decode_bytes=True): # DataArray, so call into _data_allclose_or_equiv directly allclose = _data_allclose_or_equiv(a.coords[v].values, b.coords[v].values, **kwargs) - assert allclose, '{}\n{}'.format(a.coords[v].values, + assert allclose, 'coord {}:{}\n{}'.format(a.coords[v].name, + a.coords[v].values, b.coords[v].values) elif isinstance(a, xr.Dataset): assert set(a.data_vars) == set(b.data_vars) From b3e4cddb05359bf3cfebaf3826447984fa89b7d7 Mon Sep 17 00:00:00 2001 From: Matti Eskelinen Date: Fri, 3 Nov 2017 17:31:47 +0200 Subject: [PATCH 2/9] Add change to whats-new.rst --- doc/whats-new.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 05ebb3e96e3..38b9aa280a8 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -25,6 +25,11 @@ Bug fixes of ``.T`` attributes. (:issue:`1675`). By `Keisuke Fujii `_ +Testing +~~~~~~~ + +- Add coordinate names to failure printout for :py:func:`assert_allclose`. + By `Matti Eskelinen `_ v0.10.0 rc1 (30 October 2017) ----------------------------- From 65cffc4ba30b662fffb1efc344064bcd82a0b1cd Mon Sep 17 00:00:00 2001 From: Matti Eskelinen Date: Fri, 3 Nov 2017 17:53:00 +0200 Subject: [PATCH 3/9] Fix whitespace --- xarray/testing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/testing.py b/xarray/testing.py index af989366a29..46eb7e1d390 100644 --- a/xarray/testing.py +++ b/xarray/testing.py @@ -130,8 +130,8 @@ def assert_allclose(a, b, rtol=1e-05, atol=1e-08, decode_bytes=True): allclose = _data_allclose_or_equiv(a.coords[v].values, b.coords[v].values, **kwargs) assert allclose, 'coord {}:{}\n{}'.format(a.coords[v].name, - a.coords[v].values, - b.coords[v].values) + a.coords[v].values, + b.coords[v].values) elif isinstance(a, xr.Dataset): assert set(a.data_vars) == set(b.data_vars) assert set(a.coords) == set(b.coords) From d156cb945411976a6065d7d3f1c98d87b7649400 Mon Sep 17 00:00:00 2001 From: Matti Eskelinen Date: Fri, 3 Nov 2017 18:41:18 +0200 Subject: [PATCH 4/9] Better reference --- doc/whats-new.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 38b9aa280a8..5f83b766f84 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -28,7 +28,8 @@ Bug fixes Testing ~~~~~~~ -- Add coordinate names to failure printout for :py:func:`assert_allclose`. +- Add coordinate names to failure printout for + :py:func:`xarray.testing.assert_allclose`. By `Matti Eskelinen `_ v0.10.0 rc1 (30 October 2017) From 502e24bd13e6cdf5132010ce5dd6fb4fe507b406 Mon Sep 17 00:00:00 2001 From: Matti Eskelinen Date: Mon, 6 Nov 2017 14:46:01 +0200 Subject: [PATCH 5/9] Add optional name parameter for test failure identification --- xarray/testing.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/xarray/testing.py b/xarray/testing.py index 46eb7e1d390..fd0b4ee0eee 100644 --- a/xarray/testing.py +++ b/xarray/testing.py @@ -27,7 +27,7 @@ def _data_allclose_or_equiv(arr1, arr2, rtol=1e-05, atol=1e-08, arr1, arr2, rtol=rtol, atol=atol) -def assert_equal(a, b): +def assert_equal(a, b, name=''): """Like :py:func:`numpy.testing.assert_array_equal`, but for xarray objects. @@ -42,6 +42,8 @@ def assert_equal(a, b): The first object to compare. b : xarray.Dataset, xarray.DataArray or xarray.Variable The second object to compare. + name : str, optional + Descriptive name to be shown in test failures. See also -------- @@ -52,13 +54,14 @@ def assert_equal(a, b): __tracebackhide__ = True # noqa: F841 assert type(a) == type(b) if isinstance(a, (xr.Variable, xr.DataArray, xr.Dataset)): - assert a.equals(b), '{}\n{}'.format(a, b) + assert a.equals(b), \ + '{}:{}\n{}'.format(name, a, b) if name else '{}\n{}'.format(a, b) else: raise TypeError('{} not supported by assertion comparison' .format(type(a))) -def assert_identical(a, b): +def assert_identical(a, b, name=''): """Like :py:func:`xarray.testing.assert_equal`, but also matches the objects' names and attributes. @@ -70,6 +73,8 @@ def assert_identical(a, b): The first object to compare. b : xarray.Dataset, xarray.DataArray or xarray.Variable The second object to compare. + name : str, optional + Descriptive name to be shown in test failures. See also -------- @@ -80,15 +85,16 @@ def assert_identical(a, b): assert type(a) == type(b) if isinstance(a, xr.DataArray): assert a.name == b.name - assert_identical(a._to_temp_dataset(), b._to_temp_dataset()) + assert_identical(a._to_temp_dataset(), b._to_temp_dataset(), name=name) elif isinstance(a, (xr.Dataset, xr.Variable)): - assert a.identical(b), '{}\n{}'.format(a, b) + assert a.identical(b), \ + '{}:{}\n{}'.format(name, a, b) if name else '{}\n{}'.format(a, b) else: raise TypeError('{} not supported by assertion comparison' .format(type(a))) -def assert_allclose(a, b, rtol=1e-05, atol=1e-08, decode_bytes=True): +def assert_allclose(a, b, rtol=1e-05, atol=1e-08, decode_bytes=True, name=''): """Like :py:func:`numpy.testing.assert_allclose`, but for xarray objects. Raises an AssertionError if two objects are not equal up to desired @@ -108,6 +114,8 @@ def assert_allclose(a, b, rtol=1e-05, atol=1e-08, decode_bytes=True): Whether byte dtypes should be decoded to strings as UTF-8 or not. This is useful for testing serialization methods on Python 3 that return saved strings as bytes. + name : str, optional + Descriptive name to be shown in test failures. See also -------- @@ -120,7 +128,7 @@ def assert_allclose(a, b, rtol=1e-05, atol=1e-08, decode_bytes=True): if isinstance(a, xr.Variable): assert a.dims == b.dims allclose = _data_allclose_or_equiv(a.values, b.values, **kwargs) - assert allclose, '{}\n{}'.format(a.values, b.values) + assert allclose, '{}:{}\n{}'.format(name, a.values, b.values) elif isinstance(a, xr.DataArray): assert_allclose(a.variable, b.variable, **kwargs) assert set(a.coords) == set(b.coords) @@ -129,13 +137,15 @@ def assert_allclose(a, b, rtol=1e-05, atol=1e-08, decode_bytes=True): # DataArray, so call into _data_allclose_or_equiv directly allclose = _data_allclose_or_equiv(a.coords[v].values, b.coords[v].values, **kwargs) - assert allclose, 'coord {}:{}\n{}'.format(a.coords[v].name, - a.coords[v].values, - b.coords[v].values) + assert allclose, '{}:{}\n{}'.format( + '{} in {}'.format(v, name) if name else v, + a.coords[v].values, + b.coords[v].values) elif isinstance(a, xr.Dataset): assert set(a.data_vars) == set(b.data_vars) assert set(a.coords) == set(b.coords) for k in list(a.variables) + list(a.coords): + kwargs['name'] = '{} in {}'.format(k, name) if name else k assert_allclose(a[k], b[k], **kwargs) else: From 17a2491a08b2aa4345672919bdd4b943e65038e2 Mon Sep 17 00:00:00 2001 From: Matti Eskelinen Date: Mon, 6 Nov 2017 15:10:53 +0200 Subject: [PATCH 6/9] Add name passthrough to recursive call --- xarray/testing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/testing.py b/xarray/testing.py index fd0b4ee0eee..c67be5ef852 100644 --- a/xarray/testing.py +++ b/xarray/testing.py @@ -130,7 +130,7 @@ def assert_allclose(a, b, rtol=1e-05, atol=1e-08, decode_bytes=True, name=''): allclose = _data_allclose_or_equiv(a.values, b.values, **kwargs) assert allclose, '{}:{}\n{}'.format(name, a.values, b.values) elif isinstance(a, xr.DataArray): - assert_allclose(a.variable, b.variable, **kwargs) + assert_allclose(a.variable, b.variable, name=name, **kwargs) assert set(a.coords) == set(b.coords) for v in a.coords.variables: # can't recurse with this function as coord is sometimes a From c1018908f83319826beed110c57b60a57d4828fb Mon Sep 17 00:00:00 2001 From: Matti Eskelinen Date: Mon, 6 Nov 2017 15:13:13 +0200 Subject: [PATCH 7/9] Fix indentation --- xarray/testing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/testing.py b/xarray/testing.py index c67be5ef852..2c47235bf77 100644 --- a/xarray/testing.py +++ b/xarray/testing.py @@ -55,7 +55,7 @@ def assert_equal(a, b, name=''): assert type(a) == type(b) if isinstance(a, (xr.Variable, xr.DataArray, xr.Dataset)): assert a.equals(b), \ - '{}:{}\n{}'.format(name, a, b) if name else '{}\n{}'.format(a, b) + '{}:{}\n{}'.format(name, a, b) if name else '{}\n{}'.format(a, b) else: raise TypeError('{} not supported by assertion comparison' .format(type(a))) @@ -88,7 +88,7 @@ def assert_identical(a, b, name=''): assert_identical(a._to_temp_dataset(), b._to_temp_dataset(), name=name) elif isinstance(a, (xr.Dataset, xr.Variable)): assert a.identical(b), \ - '{}:{}\n{}'.format(name, a, b) if name else '{}\n{}'.format(a, b) + '{}:{}\n{}'.format(name, a, b) if name else '{}\n{}'.format(a, b) else: raise TypeError('{} not supported by assertion comparison' .format(type(a))) From 821ac6919f1a17f601e424bf962ebcefcc03b4e5 Mon Sep 17 00:00:00 2001 From: Matti Eskelinen Date: Mon, 6 Nov 2017 15:34:17 +0200 Subject: [PATCH 8/9] Update whats-new.rst --- doc/whats-new.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 5f83b766f84..c1ba34711d2 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -28,8 +28,10 @@ Bug fixes Testing ~~~~~~~ -- Add coordinate names to failure printout for - :py:func:`xarray.testing.assert_allclose`. +- Add name parameter and extra formatting to + :py:func:`xarray.testing.assert_allclose`, + :py:func:`xarray.testing.assert_identical` and + :py:func:`xarray.testing.assert_equal` for nicer failure printouts. By `Matti Eskelinen `_ v0.10.0 rc1 (30 October 2017) From b3e2b6db85504aecf5e186989d4abb4164cd73f8 Mon Sep 17 00:00:00 2001 From: Matti Eskelinen Date: Tue, 7 Nov 2017 16:44:44 +0200 Subject: [PATCH 9/9] Switch default value of name to None --- xarray/testing.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/testing.py b/xarray/testing.py index 2c47235bf77..8343e26863b 100644 --- a/xarray/testing.py +++ b/xarray/testing.py @@ -27,7 +27,7 @@ def _data_allclose_or_equiv(arr1, arr2, rtol=1e-05, atol=1e-08, arr1, arr2, rtol=rtol, atol=atol) -def assert_equal(a, b, name=''): +def assert_equal(a, b, name=None): """Like :py:func:`numpy.testing.assert_array_equal`, but for xarray objects. @@ -61,7 +61,7 @@ def assert_equal(a, b, name=''): .format(type(a))) -def assert_identical(a, b, name=''): +def assert_identical(a, b, name=None): """Like :py:func:`xarray.testing.assert_equal`, but also matches the objects' names and attributes. @@ -94,7 +94,7 @@ def assert_identical(a, b, name=''): .format(type(a))) -def assert_allclose(a, b, rtol=1e-05, atol=1e-08, decode_bytes=True, name=''): +def assert_allclose(a, b, rtol=1e-05, atol=1e-08, decode_bytes=True, name=None): """Like :py:func:`numpy.testing.assert_allclose`, but for xarray objects. Raises an AssertionError if two objects are not equal up to desired