Example #1
0
def test_pearson_r_xr_dask(a_dask, b_dask, dim, weight, weights_ones_dask,
                           weights_latitude_dask):
    # Generates subsetted weights to pass in as arg to main function and for the numpy testing.
    weights_arg, weights_np = adjust_weights(weight, dim, weights_ones_dask,
                                             weights_latitude_dask)

    actual = pearson_r(a_dask, b_dask, dim, weights=weights_arg)
    assert actual.chunks is not None

    dim, _ = _preprocess_dims(dim)
    if len(dim) > 1:
        new_dim = '_'.join(dim)
        _a_dask = a_dask.stack(**{new_dim: dim})
        _b_dask = b_dask.stack(**{new_dim: dim})
        _weights_np = weights_np.stack(**{new_dim: dim})
    else:
        new_dim = dim[0]
        _a_dask = a_dask
        _b_dask = b_dask
        _weights_np = weights_np
    _weights_np = _preprocess_weights(_a_dask, dim, new_dim, _weights_np)

    axis = _a_dask.dims.index(new_dim)
    res = _pearson_r(_a_dask.values, _b_dask.values, _weights_np.values, axis)
    expected = actual.copy()
    expected.values = res
    assert_allclose(actual, expected)
Example #2
0
def test_pearson_r_accessor(ds_dask):
    ds = ds_dask.load()

    dim = 'time'
    actual = pearson_r(ds['a'], ds['b'], dim)
    expected = ds.xs.pearson_r('a', 'b', dim)
    assert_allclose(actual, expected)
Example #3
0
def test_pearsonr_same_as_scipy(a, b):
    """Tests that pearson r correlation and pvalue is same as computed from
    scipy."""
    xs_corr = pearson_r(a, b, 'time')
    xs_p = pearson_r_p_value(a, b, 'time')
    scipy_corr, scipy_p = pearsonr(a, b)
    assert np.allclose(xs_corr, scipy_corr)
    assert np.allclose(xs_p, scipy_p)
Example #4
0
def test_keep_attrs(a, b, metrics, keep_attrs):
    """Test keep_attrs for all metrics."""
    metric, _metric = metrics
    # ths tests only copying attrs from a
    res = metric(a, b, "time", keep_attrs=keep_attrs)
    if keep_attrs:
        assert res.attrs == a.attrs
    else:
        assert res.attrs == {}
    da = xr.DataArray([0, 1, 2], dims=["time"])
    assert pearson_r(da, da, dim="time") == 1
Example #5
0
def test_pearson_r_xr_dask(a_dask, b_dask, dim):
    actual = pearson_r(a_dask, b_dask, dim)

    dim, _ = _preprocess(dim)
    if len(dim) > 1:
        new_dim = '_'.join(dim)
        _a_dask = a_dask.stack(**{new_dim: dim})
        _b_dask = b_dask.stack(**{new_dim: dim})
    else:
        new_dim = dim[0]
        _a_dask = a_dask
        _b_dask = b_dask

    axis = _a_dask.dims.index(new_dim)
    res = _pearson_r(_a_dask.values, _b_dask.values, axis)
    expected = actual.copy()
    expected.values = res
    assert_allclose(actual, expected)
Example #6
0
def test_pearson_r_xr(a, b, dim):
    actual = pearson_r(a, b, dim)

    dim, _ = _preprocess(dim)
    if len(dim) > 1:
        new_dim = '_'.join(dim)
        _a = a.stack(**{new_dim: dim})
        _b = b.stack(**{new_dim: dim})
    else:
        new_dim = dim[0]
        _a = a
        _b = b

    axis = _a.dims.index(new_dim)
    res = _pearson_r(_a.values, _b.values, axis)
    expected = actual.copy()
    expected.values = res
    assert_allclose(actual, expected)
Example #7
0
def test_nan_skipna(a, b):
    # Randomly add some nans to a
    a = a.where(np.random.random(a.shape) < 0.5)
    with raise_if_dask_computes():
        pearson_r(a, b, dim="lat", skipna=True)
def test_pearson_r_integer():
    """Test whether arrays as integers work."""
    da = xr.DataArray([0, 1, 2], dims=['time'])
    assert pearson_r(da, da, dim='time') == 1
Example #9
0
def test_nan_skipna(a, b):
    # Randomly add some nans to a
    a = a.where(np.random.random(a.shape) < 0.5)
    pearson_r(a, b, dim="lat", skipna=True)