Exemplo n.º 1
0
def test_rle(ufunc, use_dask, index):
    if use_dask and ufunc:
        pytest.xfail("rle_1d is not implemented for dask arrays.")

    values = np.zeros((10, 365, 4, 4))
    time = pd.date_range("2000-01-01", periods=365, freq="D")
    values[:, 1:11, ...] = 1
    da = xr.DataArray(values,
                      coords={"time": time},
                      dims=("a", "time", "b", "c"))

    if ufunc:
        da = da[0, :, 0, 0]
        v, l, p = rl.rle_1d(da != 0)
        np.testing.assert_array_equal(v, [False, True, False])
        np.testing.assert_array_equal(l, [1, 10, 354])
        np.testing.assert_array_equal(p, [0, 1, 11])
    else:
        if use_dask:
            da = da.chunk({"a": 1, "b": 2})

        out = rl.rle(da != 0, index=index).mean(["a", "b", "c"])
        if index == "last":
            expected = np.zeros(365)
            expected[1:10] = np.nan
            expected[10] = 10
        else:
            expected = np.zeros(365)
            expected[1] = 10
            expected[2:11] = np.nan
        np.testing.assert_array_equal(out, expected)
Exemplo n.º 2
0
    def test_dataarray(self):
        values = np.zeros(365)
        time = pd.date_range("7/1/2000",
                             periods=len(values),
                             freq=pd.DateOffset(days=1))
        values[1:11] = 1
        da = xr.DataArray(values, coords={"time": time}, dims="time")

        v, l, p = rl.rle_1d(da != 0)
        np.testing.assert_array_equal(v, [False, True, False])
        np.testing.assert_array_equal(l, [1, 10, 354])
        np.testing.assert_array_equal(p, [0, 1, 11])