|
26 | 26 | from xarray.core.pycompat import dask_version |
27 | 27 | from xarray.core.types import T_Xarray |
28 | 28 |
|
29 | | -from . import has_dask, raise_if_dask_computes, requires_dask |
| 29 | +from . import ( |
| 30 | + has_cftime, |
| 31 | + has_dask, |
| 32 | + raise_if_dask_computes, |
| 33 | + requires_cftime, |
| 34 | + requires_dask, |
| 35 | +) |
30 | 36 |
|
31 | 37 |
|
32 | 38 | def assert_identical(a, b): |
@@ -1936,7 +1942,9 @@ def test_where_attrs() -> None: |
1936 | 1942 | assert actual.attrs == {} |
1937 | 1943 |
|
1938 | 1944 |
|
1939 | | -@pytest.mark.parametrize("use_dask", [False, True]) |
| 1945 | +@pytest.mark.parametrize( |
| 1946 | + "use_dask", [pytest.param(False, id="nodask"), pytest.param(True, id="dask")] |
| 1947 | +) |
1940 | 1948 | @pytest.mark.parametrize( |
1941 | 1949 | ["x", "coeffs", "expected"], |
1942 | 1950 | [ |
@@ -2031,20 +2039,99 @@ def test_polyval( |
2031 | 2039 | pytest.skip("requires dask") |
2032 | 2040 | coeffs = coeffs.chunk({"degree": 2}) |
2033 | 2041 | x = x.chunk({"x": 2}) |
| 2042 | + |
2034 | 2043 | with raise_if_dask_computes(): |
2035 | | - actual = xr.polyval(coord=x, coeffs=coeffs) # type: ignore |
| 2044 | + actual = xr.polyval(coord=x, coeffs=coeffs) |
| 2045 | + |
| 2046 | + xr.testing.assert_allclose(actual, expected) |
| 2047 | + |
| 2048 | + |
| 2049 | +@requires_cftime |
| 2050 | +@pytest.mark.parametrize( |
| 2051 | + "use_dask", [pytest.param(False, id="nodask"), pytest.param(True, id="dask")] |
| 2052 | +) |
| 2053 | +@pytest.mark.parametrize("date", ["1970-01-01", "0753-04-21"]) |
| 2054 | +def test_polyval_cftime(use_dask: bool, date: str) -> None: |
| 2055 | + import cftime |
| 2056 | + |
| 2057 | + x = xr.DataArray( |
| 2058 | + xr.date_range(date, freq="1S", periods=3, use_cftime=True), |
| 2059 | + dims="x", |
| 2060 | + ) |
| 2061 | + coeffs = xr.DataArray([0, 1], dims="degree", coords={"degree": [0, 1]}) |
| 2062 | + |
| 2063 | + if use_dask: |
| 2064 | + if not has_dask: |
| 2065 | + pytest.skip("requires dask") |
| 2066 | + coeffs = coeffs.chunk({"degree": 2}) |
| 2067 | + x = x.chunk({"x": 2}) |
| 2068 | + |
| 2069 | + with raise_if_dask_computes(max_computes=1): |
| 2070 | + actual = xr.polyval(coord=x, coeffs=coeffs) |
| 2071 | + |
| 2072 | + t0 = xr.date_range(date, periods=1)[0] |
| 2073 | + offset = (t0 - cftime.DatetimeGregorian(1970, 1, 1)).total_seconds() * 1e9 |
| 2074 | + expected = ( |
| 2075 | + xr.DataArray( |
| 2076 | + [0, 1e9, 2e9], |
| 2077 | + dims="x", |
| 2078 | + coords={"x": xr.date_range(date, freq="1S", periods=3, use_cftime=True)}, |
| 2079 | + ) |
| 2080 | + + offset |
| 2081 | + ) |
2036 | 2082 | xr.testing.assert_allclose(actual, expected) |
2037 | 2083 |
|
2038 | 2084 |
|
2039 | | -def test_polyval_degree_dim_checks(): |
2040 | | - x = (xr.DataArray([1, 2, 3], dims="x"),) |
| 2085 | +def test_polyval_degree_dim_checks() -> None: |
| 2086 | + x = xr.DataArray([1, 2, 3], dims="x") |
2041 | 2087 | coeffs = xr.DataArray([2, 3, 4], dims="degree", coords={"degree": [0, 1, 2]}) |
2042 | 2088 | with pytest.raises(ValueError): |
2043 | 2089 | xr.polyval(x, coeffs.drop_vars("degree")) |
2044 | 2090 | with pytest.raises(ValueError): |
2045 | 2091 | xr.polyval(x, coeffs.assign_coords(degree=coeffs.degree.astype(float))) |
2046 | 2092 |
|
2047 | 2093 |
|
| 2094 | +@pytest.mark.parametrize( |
| 2095 | + "use_dask", [pytest.param(False, id="nodask"), pytest.param(True, id="dask")] |
| 2096 | +) |
| 2097 | +@pytest.mark.parametrize( |
| 2098 | + "x", |
| 2099 | + [ |
| 2100 | + pytest.param(xr.DataArray([0, 1, 2], dims="x"), id="simple"), |
| 2101 | + pytest.param( |
| 2102 | + xr.DataArray(pd.date_range("1970-01-01", freq="ns", periods=3), dims="x"), |
| 2103 | + id="datetime", |
| 2104 | + ), |
| 2105 | + pytest.param( |
| 2106 | + xr.DataArray(np.array([0, 1, 2], dtype="timedelta64[ns]"), dims="x"), |
| 2107 | + id="timedelta", |
| 2108 | + ), |
| 2109 | + ], |
| 2110 | +) |
| 2111 | +@pytest.mark.parametrize( |
| 2112 | + "y", |
| 2113 | + [ |
| 2114 | + pytest.param(xr.DataArray([1, 6, 17], dims="x"), id="1D"), |
| 2115 | + pytest.param( |
| 2116 | + xr.DataArray([[1, 6, 17], [34, 57, 86]], dims=("y", "x")), id="2D" |
| 2117 | + ), |
| 2118 | + ], |
| 2119 | +) |
| 2120 | +def test_polyfit_polyval_integration( |
| 2121 | + use_dask: bool, x: xr.DataArray, y: xr.DataArray |
| 2122 | +) -> None: |
| 2123 | + y.coords["x"] = x |
| 2124 | + if use_dask: |
| 2125 | + if not has_dask: |
| 2126 | + pytest.skip("requires dask") |
| 2127 | + y = y.chunk({"x": 2}) |
| 2128 | + |
| 2129 | + fit = y.polyfit(dim="x", deg=2) |
| 2130 | + evaluated = xr.polyval(y.x, fit.polyfit_coefficients) |
| 2131 | + expected = y.transpose(*evaluated.dims) |
| 2132 | + xr.testing.assert_allclose(evaluated.variable, expected.variable) |
| 2133 | + |
| 2134 | + |
2048 | 2135 | @pytest.mark.parametrize("use_dask", [False, True]) |
2049 | 2136 | @pytest.mark.parametrize( |
2050 | 2137 | "a, b, ae, be, dim, axis", |
|
0 commit comments