def test_detrend(): N = 16 x = np.arange(N + 1) y = np.arange(N - 1) t = np.linspace(-int(N / 2), int(N / 2), N - 6) z = np.arange(int(N / 2)) d4d = (t[:, np.newaxis, np.newaxis, np.newaxis] + z[np.newaxis, :, np.newaxis, np.newaxis] + y[np.newaxis, np.newaxis, :, np.newaxis] + x[np.newaxis, np.newaxis, np.newaxis, :]) da4d = xr.DataArray(d4d, dims=['time', 'z', 'y', 'x'], coords={ 'time': range(len(t)), 'z': range(len(z)), 'y': range(len(y)), 'x': range(len(x)) }) func = xrft.detrend_wrap(xrft.detrendn) ######### # Chunk along the `time` axis ######### da = da4d.chunk({'time': 1}) with pytest.raises(ValueError): func(da.data, axes=[0]).compute with pytest.raises(ValueError): func(da.data, axes=[0, 1, 2, 3]).compute() da_prime = func(da.data, axes=[2]).compute() npt.assert_allclose(da_prime[0, 0], sps.detrend(d4d[0, 0], axis=0)) da_prime = func(da.data, axes=[1, 2, 3]).compute() npt.assert_allclose(da_prime[0], xrft.detrendn(d4d[0], axes=[0, 1, 2])) ######### # Chunk along the `time` and `z` axes ######### da = da4d.chunk({'time': 1, 'z': 1}) with pytest.raises(ValueError): func(da.data, axes=[1, 2]).compute() with pytest.raises(ValueError): func(da.data, axes=[2, 2]).compute() da_prime = func(da.data, axes=[2, 3]).compute() npt.assert_allclose(da_prime[0, 0], xrft.detrendn(d4d[0, 0], axes=[0, 1])) s = np.arange(2) d5d = d4d[np.newaxis, :, :, :, :] + s[:, np.newaxis, np.newaxis, np.newaxis, np.newaxis] da5d = xr.DataArray(d5d, dims=['s', 'time', 'z', 'y', 'x'], coords={ 's': range(len(s)), 'time': range(len(t)), 'z': range(len(z)), 'y': range(len(y)), 'x': range(len(x)) }) da = da5d.chunk({'time': 1}) with pytest.raises(ValueError): func(da.data).compute()
def test_detrend(): N = 16 x = np.arange(N+1) y = np.arange(N-1) t = np.linspace(-int(N/2), int(N/2), N-6) z = np.arange(int(N/2)) d4d = (t[:,np.newaxis,np.newaxis,np.newaxis] + z[np.newaxis,:,np.newaxis,np.newaxis] + y[np.newaxis,np.newaxis,:,np.newaxis] + x[np.newaxis,np.newaxis,np.newaxis,:] ) da4d = xr.DataArray(d4d, dims=['time','z','y','x'], coords={'time':range(len(t)),'z':range(len(z)),'y':range(len(y)), 'x':range(len(x))} ) func = xrft.detrend_wrap(xrft.detrendn) ######### # Chunk along the `time` axis ######### da = da4d.chunk({'time': 1}) with pytest.raises(ValueError): func(da.data, axes=[0]).compute with pytest.raises(ValueError): func(da.data, axes=[0,1,2,3]).compute() da_prime = func(da.data, axes=[2]).compute() npt.assert_allclose(da_prime[0,0], sps.detrend(d4d[0,0], axis=0)) da_prime = func(da.data, axes=[1,2,3]).compute() npt.assert_allclose(da_prime[0], xrft.detrendn(d4d[0], axes=[0,1,2])) ######### # Chunk along the `time` and `z` axes ######### da = da4d.chunk({'time':1, 'z':1}) with pytest.raises(ValueError): func(da.data, axes=[1,2]).compute() with pytest.raises(ValueError): func(da.data, axes=[2,2]).compute() da_prime = func(da.data, axes=[2,3]).compute() npt.assert_allclose(da_prime[0,0], xrft.detrendn(d4d[0,0], axes=[0,1]))
def _apply_detrend(da, axis_num): """Wrapper function for applying detrending""" ''' copied from xrft.py -- not sure why but the functions in xrft.py starting with '_' cannot be called directly! So I just copied here directly -- look for the reason later!!!! ''' if da.chunks: func = xrft.detrend_wrap(xrft.detrendn) da = xr.DataArray(func(da.data, axes=axis_num), dims=da.dims, coords=da.coords) else: if da.ndim == 1: da = xr.DataArray(sps.detrend(da), dims=da.dims, coords=da.coords) else: da = xrft.detrendn(da, axes=axis_num) # else: # raise ValueError("Data should be dask array.") return da