Esempio n. 1
0
def test_distance_metrics_xr(a, b, dim, weight_bool, weights, metrics):
    """Test whether distance-based metric for xarray functions (from
     deterministic.py) give save numerical results as for numpy functions from
     np_deterministic.py)."""
    # unpack metrics
    metric, _metric = metrics
    # Generates subsetted weights to pass in as arg to main function and for
    # the numpy testing.
    weights = adjust_weights(dim, weight_bool, weights)
    # median absolute error has no weights argument
    if metric is median_absolute_error:
        actual = metric(a, b, dim)
    else:
        actual = metric(a, b, dim, weights=weights)
    assert actual.chunks is None

    dim, axis = _preprocess_dims(dim)
    _a = a
    _b = b
    _weights = _preprocess_weights(_a, dim, dim, weights)
    axis = tuple(a.dims.index(d) for d in dim)
    if metric is median_absolute_error:
        res = _metric(_a.values, _b.values, axis, skipna=False)
    else:
        # ensure _weights.values or None
        _weights = None if _weights is None else _weights.values
        res = _metric(_a.values, _b.values, _weights, axis, skipna=False)
    expected = actual.copy()
    expected.values = res
    assert_allclose(actual, expected)
Esempio n. 2
0
def test_correlation_metrics_xr(a, b, dim, weight_bool, weights, metrics):
    """Test whether correlation metric for xarray functions (from
     deterministic.py) give save numerical results as for numpy functions from
     np_deterministic.py)."""
    # unpack metrics
    metric, _metric = metrics
    # Generates subsetted weights to pass in as arg to main function and for
    # the numpy testing.
    _weights = adjust_weights(dim, weight_bool, weights)

    actual = metric(a, b, dim, weights=_weights)
    # check that no chunks for no chunk inputs
    assert actual.chunks is None

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

    axis = _a.dims.index(new_dim)
    res = _metric(_a.values, _b.values, _weights.values, axis, skipna=False)
    expected = actual.copy()
    expected.values = res
    assert_allclose(actual, expected)
Esempio n. 3
0
def test_pearson_r_p_value_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_p_value(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_p_value(_a_dask.values, _b_dask.values,
                             _weights_np.values, axis)
    expected = actual.copy()
    expected.values = res
    assert_allclose(actual, expected)
def test_correlation_metrics_ufunc_same_np(a, b, dim, weight_bool,
                                           weights_cos_lat, metrics, skipna,
                                           has_nan):
    """Test whether correlation metric for xarray functions (from
    deterministic.py) give save numerical results as for numpy functions (from
    np_deterministic.py)."""
    if has_nan:
        a[0] = np.nan
    # unpack metrics
    metric, _metric = metrics
    # Only apply over time dimension for effective p value.
    if (dim != "time") and (metric in temporal_only_metrics):
        dim = "time"
    # Generates subsetted weights to pass in as arg to main function and for
    # the numpy testing.
    _weights = adjust_weights(dim, weight_bool, weights_cos_lat)
    if metric in temporal_only_metrics:
        actual = metric(a, b, dim, skipna=skipna)
    else:
        actual = metric(a, b, dim, weights=_weights, skipna=skipna)
    # check that no chunks for no chunk inputs
    assert actual.chunks is None

    dim, _ = _preprocess_dims(dim, a)
    if len(dim) > 1:
        new_dim = "_".join(dim)
        _a = a.stack(**{new_dim: dim})
        _b = b.stack(**{new_dim: dim})
        if weight_bool:
            _weights = _weights.stack(**{new_dim: dim})
    else:
        new_dim = dim[0]
        _a = a
        _b = b
    _weights = _preprocess_weights(_a, dim, new_dim, _weights)

    # ensure _weights.values or None
    _weights = None if _weights is None else _weights.values

    axis = _a.dims.index(new_dim)
    if metric in temporal_only_metrics:
        res = _metric(_a.values, _b.values, axis, skipna=skipna)
    else:
        res = _metric(_a.values, _b.values, _weights, axis, skipna=skipna)
    expected = actual.copy()
    expected.values = res
    assert_allclose(actual, expected)
Esempio n. 5
0
def test_mae_r_xr(a, b, dim, weight, weights_ones, weights_latitude):
    # 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,
                                             weights_latitude)

    actual = mae(a, b, dim, weights=weights_arg)
    assert actual.chunks is None

    dim, axis = _preprocess_dims(dim)
    _a = a
    _b = b
    _weights_np = _preprocess_weights(_a, dim, dim, weights_np)
    axis = tuple(a.dims.index(d) for d in dim)
    res = _mae(_a.values, _b.values, _weights_np.values, axis)
    expected = actual.copy()
    expected.values = res
    assert_allclose(actual, expected)
Esempio n. 6
0
def test_mse_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 = mse(a_dask, b_dask, dim, weights=weights_arg)
    assert actual.chunks is not None

    dim, axis = _preprocess_dims(dim)
    _a_dask = a_dask
    _b_dask = b_dask
    _weights_np = _preprocess_weights(_a_dask, dim, dim, weights_np)
    axis = tuple(a_dask.dims.index(d) for d in dim)
    res = _mse(_a_dask.values, _b_dask.values, _weights_np.values, axis)
    expected = actual.copy()
    expected.values = res
    assert_allclose(actual, expected)