def test_linregress_ufunc(): y = np.random.random(20) x = np.arange(len(y)) fit = np.array(stats.linregress(x, y)) assert np.allclose(fit, _linregress_ufunc(x, y)) # test with nans y[0] = np.nan fit = np.array(stats.linregress(x[1:], y[1:])) assert np.isnan(_linregress_ufunc(x, y)).all() assert np.allclose(fit, _linregress_ufunc(x, y, nanmask=True)) # test with all nansum y[:] = np.nan assert np.isnan(_linregress_ufunc(x, y, nanmask=True)).all()
def test_xr_linregress(chunks, variant, dtype, nans): a = xr.DataArray(np.random.rand(3, 13, 5), dims=["x", "time", "y"]) b = xr.DataArray(np.random.rand(3, 5, 13), dims=["x", "y", "time"]) if nans: # add nans at random positions a.data[np.unravel_index(np.random.randint(0, 2 * 4 * 12, 10), a.shape)] = np.nan b.data[np.unravel_index(np.random.randint(0, 2 * 4 * 12, 10), b.shape)] = np.nan if chunks is not None: if variant == 0: a = a.chunk(chunks) elif variant == 1: b = b.chunk(chunks) elif variant == 2: a = a.chunk(chunks) b = b.chunk(chunks) reg = xr_linregress(a, b, dtype=dtype) for xx in range(len(a.x)): for yy in range(len(a.y)): pos = dict(x=xx, y=yy) expected = _linregress_ufunc(a.isel(**pos), b.isel(**pos)) reg_sub = reg.isel(**pos) for ni, nn in enumerate( ["slope", "intercept", "r_value", "p_value", "std_err"]): np.testing.assert_allclose(reg_sub[nn].data, expected[ni])
def test_lin_trend_full_legacy(): # This is meant to be a test if the old ufunc for the trend produces # identical output y = np.random.random(20) x = np.arange(len(y)) fit = _linregress_ufunc(x, y)[0:2] fit_legacy = _lin_trend_legacy(y) assert np.allclose(fit, fit_legacy)
def test_linregress_ufunc(): y = np.random.random(20) x = np.arange(len(y)) fit = np.array(stats.linregress(x, y)) assert np.allclose(fit, _linregress_ufunc(x, y))