1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
import numpy as np
import pytest
import xarray as xr
from xarray.core.npcompat import IS_NEP18_ACTIVE
from . import has_dask
try:
from dask.array import from_array as dask_from_array
except ImportError:
dask_from_array = lambda x: x
try:
import pint
unit_registry = pint.UnitRegistry(force_ndarray_like=True)
def quantity(x):
return unit_registry.Quantity(x, "m")
has_pint = True
except ImportError:
def quantity(x):
return x
has_pint = False
def test_allclose_regression():
x = xr.DataArray(1.01)
y = xr.DataArray(1.02)
xr.testing.assert_allclose(x, y, atol=0.01)
@pytest.mark.parametrize(
"obj1,obj2",
(
pytest.param(
xr.Variable("x", [1e-17, 2]), xr.Variable("x", [0, 3]), id="Variable"
),
pytest.param(
xr.DataArray([1e-17, 2], dims="x"),
xr.DataArray([0, 3], dims="x"),
id="DataArray",
),
pytest.param(
xr.Dataset({"a": ("x", [1e-17, 2]), "b": ("y", [-2e-18, 2])}),
xr.Dataset({"a": ("x", [0, 2]), "b": ("y", [0, 1])}),
id="Dataset",
),
),
)
def test_assert_allclose(obj1, obj2):
with pytest.raises(AssertionError):
xr.testing.assert_allclose(obj1, obj2)
@pytest.mark.filterwarnings("error")
@pytest.mark.parametrize(
"duckarray",
(
pytest.param(np.array, id="numpy"),
pytest.param(
dask_from_array,
id="dask",
marks=pytest.mark.skipif(not has_dask, reason="requires dask"),
),
pytest.param(
quantity,
id="pint",
marks=pytest.mark.skipif(not has_pint, reason="requires pint"),
),
),
)
@pytest.mark.parametrize(
["obj1", "obj2"],
(
pytest.param([1e-10, 2], [0.0, 2.0], id="both arrays"),
pytest.param([1e-17, 2], 0.0, id="second scalar"),
pytest.param(0.0, [1e-17, 2], id="first scalar"),
),
)
def test_assert_duckarray_equal_failing(duckarray, obj1, obj2):
# TODO: actually check the repr
a = duckarray(obj1)
b = duckarray(obj2)
with pytest.raises(AssertionError):
xr.testing.assert_duckarray_equal(a, b)
@pytest.mark.filterwarnings("error")
@pytest.mark.parametrize(
"duckarray",
(
pytest.param(
np.array,
id="numpy",
marks=pytest.mark.skipif(
not IS_NEP18_ACTIVE,
reason="NUMPY_EXPERIMENTAL_ARRAY_FUNCTION is not enabled",
),
),
pytest.param(
dask_from_array,
id="dask",
marks=pytest.mark.skipif(not has_dask, reason="requires dask"),
),
pytest.param(
quantity,
id="pint",
marks=pytest.mark.skipif(not has_pint, reason="requires pint"),
),
),
)
@pytest.mark.parametrize(
["obj1", "obj2"],
(
pytest.param([0, 2], [0.0, 2.0], id="both arrays"),
pytest.param([0, 0], 0.0, id="second scalar"),
pytest.param(0.0, [0, 0], id="first scalar"),
),
)
def test_assert_duckarray_equal(duckarray, obj1, obj2):
a = duckarray(obj1)
b = duckarray(obj2)
xr.testing.assert_duckarray_equal(a, b)
|