def test_detrend_1D( array_dims, array_shape, detrend_dim, chunks, detrend_type, trend_amplitude, linear_error, ): da_original = noise(array_dims, array_shape) da_trend = da_original + trend_amplitude * da_original[detrend_dim] if chunks: da_trend = da_trend.chunk(chunks) # bail out if we are expecting an error if detrend_type == "linear" and linear_error: with pytest.raises(linear_error): detrend(da_trend, detrend_dim, detrend_type=detrend_type) return detrended = detrend(da_trend, detrend_dim, detrend_type=detrend_type) assert detrended.chunks == da_trend.chunks if detrend_type is None: xrt.assert_equal(detrended, da_trend) elif detrend_type == "constant": xrt.assert_allclose(detrended, da_trend - da_trend.mean(dim=detrend_dim)) elif detrend_type == "linear": xrt.assert_allclose(detrended, da_original)
def test_dim_format(dim, detrend_type): """ Check that detrend can deal with dim in various formats""" data = xr.DataArray( np.random.random([10]), dims=[dim], coords={dim: range(10)}, ) dt = detrend(data, dim=dim, detrend_type=detrend_type) dt = detrend(data, dim=[dim], detrend_type=detrend_type)
def test_detrend_2D(array_dims, array_shape, chunks, detrend_type, trend_amplitude): da_original = noise(array_dims, array_shape) da_trend = (da_original + trend_amplitude["x"] * da_original["x"] + trend_amplitude["y"] * da_original["y"]) if chunks: da_trend = da_trend.chunk(chunks) detrend_dim = ["y", "x"] detrended = detrend(da_trend, detrend_dim, detrend_type=detrend_type) assert detrended.chunks == da_trend.chunks if detrend_type is None: xrt.assert_equal(detrended, da_trend) elif detrend_type == "constant": xrt.assert_allclose(detrended, da_trend - da_trend.mean(dim=detrend_dim)) elif detrend_type == "linear": xrt.assert_allclose(detrended, da_original)