def assert_identical(a, b): """Like :py:func:`xarray.testing.assert_equal`, but also matches the objects' names and attributes. Raises an AssertionError if two objects are not identical. Parameters ---------- a : xarray.Dataset, xarray.DataArray or xarray.Variable The first object to compare. b : xarray.Dataset, xarray.DataArray or xarray.Variable The second object to compare. See Also -------- assert_equal, assert_allclose, Dataset.equals, DataArray.equals """ __tracebackhide__ = True assert type(a) == type(b) if isinstance(a, Variable): assert a.identical(b), formatting.diff_array_repr(a, b, "identical") elif isinstance(a, DataArray): assert a.name == b.name assert a.identical(b), formatting.diff_array_repr(a, b, "identical") elif isinstance(a, (Dataset, Variable)): assert a.identical(b), formatting.diff_dataset_repr(a, b, "identical") else: raise TypeError(f"{type(a)} not supported by assertion comparison")
def assert_identical(a, b): """Like :py:func:`xarray.testing.assert_equal`, but also matches the objects' names and attributes. Raises an AssertionError if two objects are not identical. Parameters ---------- a : xarray.Dataset, xarray.DataArray or xarray.Variable The first object to compare. b : xarray.Dataset, xarray.DataArray or xarray.Variable The second object to compare. See also -------- assert_equal, assert_allclose, Dataset.equals, DataArray.equals """ import xarray as xr __tracebackhide__ = True # noqa: F841 assert type(a) == type(b) # noqa if isinstance(a, xr.Variable): assert a.identical(b), formatting.diff_array_repr(a, b, 'identical') elif isinstance(a, xr.DataArray): assert a.name == b.name assert a.identical(b), formatting.diff_array_repr(a, b, 'identical') elif isinstance(a, (xr.Dataset, xr.Variable)): assert a.identical(b), formatting.diff_dataset_repr(a, b, 'identical') 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): """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 : xarray.Dataset, xarray.DataArray or xarray.Variable The second object to compare. rtol : float, optional Relative tolerance. atol : float, optional Absolute tolerance. decode_bytes : bool, optional 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. See also -------- assert_identical, assert_equal, numpy.testing.assert_allclose """ __tracebackhide__ = True assert type(a) == type(b) equiv = functools.partial(_data_allclose_or_equiv, rtol=rtol, atol=atol, decode_bytes=decode_bytes) equiv.__name__ = "allclose" def compat_variable(a, b): a = getattr(a, "variable", a) b = getattr(b, "variable", b) return a.dims == b.dims and (a._data is b._data or equiv(a.data, b.data)) if isinstance(a, Variable): allclose = compat_variable(a, b) assert allclose, formatting.diff_array_repr(a, b, compat=equiv) elif isinstance(a, DataArray): allclose = utils.dict_equiv( a.coords, b.coords, compat=compat_variable) and compat_variable( a.variable, b.variable) assert allclose, formatting.diff_array_repr(a, b, compat=equiv) elif isinstance(a, Dataset): allclose = a._coord_names == b._coord_names and utils.dict_equiv( a.variables, b.variables, compat=compat_variable) assert allclose, formatting.diff_dataset_repr(a, b, compat=equiv) else: raise TypeError("{} not supported by assertion comparison".format( type(a)))
def assert_equal(a, b): """Like :py:func:`numpy.testing.assert_array_equal`, but for xarray objects. Raises an AssertionError if two objects are not equal. This will match data values, dimensions and coordinates, but not names or attributes (except for Dataset objects for which the variable names must match). Arrays with NaN in the same location are considered equal. Parameters ---------- a : xarray.Dataset, xarray.DataArray or xarray.Variable The first object to compare. b : xarray.Dataset, xarray.DataArray or xarray.Variable The second object to compare. See Also -------- assert_identical, assert_allclose, Dataset.equals, DataArray.equals numpy.testing.assert_array_equal """ __tracebackhide__ = True assert type(a) == type(b) if isinstance(a, (Variable, DataArray)): assert a.equals(b), formatting.diff_array_repr(a, b, "equals") elif isinstance(a, Dataset): assert a.equals(b), formatting.diff_dataset_repr(a, b, "equals") else: raise TypeError(f"{type(a)} not supported by assertion comparison")
def assert_equal(a, b): """Like :py:func:`numpy.testing.assert_array_equal`, but for xarray objects. Raises an AssertionError if two objects are not equal. This will match data values, dimensions and coordinates, but not names or attributes (except for Dataset objects for which the variable names must match). Arrays with NaN in the same location are considered equal. Parameters ---------- a : xarray.Dataset, xarray.DataArray or xarray.Variable The first object to compare. b : xarray.Dataset, xarray.DataArray or xarray.Variable The second object to compare. See also -------- assert_identical, assert_allclose, Dataset.equals, DataArray.equals, numpy.testing.assert_array_equal """ import xarray as xr __tracebackhide__ = True # noqa: F841 assert type(a) == type(b) # noqa if isinstance(a, (xr.Variable, xr.DataArray)): assert a.equals(b), formatting.diff_array_repr(a, b, 'equals') elif isinstance(a, xr.Dataset): assert a.equals(b), formatting.diff_dataset_repr(a, b, 'equals') else: raise TypeError('{} not supported by assertion comparison' .format(type(a)))
def test_diff_array_repr(self): da_a = xr.DataArray(np.array([[1, 2, 3], [4, 5, 6]], dtype='int64'), dims=('x', 'y'), coords={ 'x': np.array(['a', 'b'], dtype='U1'), 'y': np.array([1, 2, 3], dtype='int64') }, attrs={ 'units': 'm', 'description': 'desc' }) da_b = xr.DataArray(np.array([1, 2], dtype='int64'), dims='x', coords={ 'x': np.array(['a', 'c'], dtype='U1'), 'label': ('x', np.array([1, 2], dtype='int64')) }, attrs={'units': 'kg'}) byteorder = '<' if sys.byteorder == 'little' else '>' expected = dedent("""\ Left and right DataArray objects are not identical Differing dimensions: (x: 2, y: 3) != (x: 2) Differing values: L array([[1, 2, 3], [4, 5, 6]], dtype=int64) R array([1, 2], dtype=int64) Differing coordinates: L * x (x) %cU1 'a' 'b' R * x (x) %cU1 'a' 'c' Coordinates only on the left object: * y (y) int64 1 2 3 Coordinates only on the right object: label (x) int64 1 2 Differing attributes: L units: m R units: kg Attributes only on the left object: description: desc""" % (byteorder, byteorder)) actual = formatting.diff_array_repr(da_a, da_b, 'identical') try: assert actual == expected except AssertionError: # depending on platform, dtype may not be shown in numpy array repr assert actual == expected.replace(", dtype=int64", "") va = xr.Variable('x', np.array([1, 2, 3], dtype='int64'), {'title': 'test Variable'}) vb = xr.Variable(('x', 'y'), np.array([[1, 2, 3], [4, 5, 6]], dtype='int64')) expected = dedent("""\ Left and right Variable objects are not equal Differing dimensions: (x: 3) != (x: 2, y: 3) Differing values: L array([1, 2, 3], dtype=int64) R array([[1, 2, 3], [4, 5, 6]], dtype=int64)""") actual = formatting.diff_array_repr(va, vb, 'equals') try: assert actual == expected except AssertionError: assert actual == expected.replace(", dtype=int64", "")
def test_diff_array_repr(self): da_a = xr.DataArray( np.array([[1, 2, 3], [4, 5, 6]], dtype="int64"), dims=("x", "y"), coords={ "x": np.array(["a", "b"], dtype="U1"), "y": np.array([1, 2, 3], dtype="int64"), }, attrs={ "units": "m", "description": "desc" }, ) da_b = xr.DataArray( np.array([1, 2], dtype="int64"), dims="x", coords={ "x": np.array(["a", "c"], dtype="U1"), "label": ("x", np.array([1, 2], dtype="int64")), }, attrs={"units": "kg"}, ) byteorder = "<" if sys.byteorder == "little" else ">" expected = dedent("""\ Left and right DataArray objects are not identical Differing dimensions: (x: 2, y: 3) != (x: 2) Differing values: L array([[1, 2, 3], [4, 5, 6]], dtype=int64) R array([1, 2], dtype=int64) Differing coordinates: L * x (x) %cU1 'a' 'b' R * x (x) %cU1 'a' 'c' Coordinates only on the left object: * y (y) int64 1 2 3 Coordinates only on the right object: label (x) int64 1 2 Differing attributes: L units: m R units: kg Attributes only on the left object: description: desc""" % (byteorder, byteorder)) actual = formatting.diff_array_repr(da_a, da_b, "identical") try: assert actual == expected except AssertionError: # depending on platform, dtype may not be shown in numpy array repr assert actual == expected.replace(", dtype=int64", "") va = xr.Variable("x", np.array([1, 2, 3], dtype="int64"), {"title": "test Variable"}) vb = xr.Variable(("x", "y"), np.array([[1, 2, 3], [4, 5, 6]], dtype="int64")) expected = dedent("""\ Left and right Variable objects are not equal Differing dimensions: (x: 3) != (x: 2, y: 3) Differing values: L array([1, 2, 3], dtype=int64) R array([[1, 2, 3], [4, 5, 6]], dtype=int64)""") actual = formatting.diff_array_repr(va, vb, "equals") try: assert actual == expected except AssertionError: assert actual == expected.replace(", dtype=int64", "")
def test_diff_array_repr(self): da_a = xr.DataArray( np.array([[1, 2, 3], [4, 5, 6]], dtype='int64'), dims=('x', 'y'), coords={'x': np.array(['a', 'b'], dtype='U1'), 'y': np.array([1, 2, 3], dtype='int64')}, attrs={'units': 'm', 'description': 'desc'}) da_b = xr.DataArray( np.array([1, 2], dtype='int64'), dims='x', coords={'x': np.array(['a', 'c'], dtype='U1'), 'label': ('x', np.array([1, 2], dtype='int64'))}, attrs={'units': 'kg'}) expected = dedent("""\ Left and right DataArray objects are not identical Differing dimensions: (x: 2, y: 3) != (x: 2) Differing values: L array([[1, 2, 3], [4, 5, 6]], dtype=int64) R array([1, 2], dtype=int64) Differing coordinates: L * x (x) <U1 'a' 'b' R * x (x) <U1 'a' 'c' Coordinates only on the left object: * y (y) int64 1 2 3 Coordinates only on the right object: label (x) int64 1 2 Differing attributes: L units: m R units: kg Attributes only on the left object: description: desc""") actual = formatting.diff_array_repr(da_a, da_b, 'identical') try: assert actual == expected except AssertionError: # depending on platform, dtype may not be shown in numpy array repr assert actual == expected.replace(", dtype=int64", "") va = xr.Variable('x', np.array([1, 2, 3], dtype='int64'), {'title': 'test Variable'}) vb = xr.Variable(('x', 'y'), np.array([[1, 2, 3], [4, 5, 6]], dtype='int64')) expected = dedent("""\ Left and right Variable objects are not equal Differing dimensions: (x: 3) != (x: 2, y: 3) Differing values: L array([1, 2, 3], dtype=int64) R array([[1, 2, 3], [4, 5, 6]], dtype=int64)""") actual = formatting.diff_array_repr(va, vb, 'equals') try: assert actual == expected except AssertionError: assert actual == expected.replace(", dtype=int64", "")