def test_crps_quadrature_dim(o, dim):
    """Check that crps_ensemble reduces only dim."""
    # to speed things up
    o = o.isel(time=0, drop=True)
    cdf_or_dist = norm
    actual = crps_quadrature(o, cdf_or_dist, dim=dim)
    assert_only_dim_reduced(dim, actual, o)
def test_crps_quadrature_args(o_dask, f_prob_dask, keep_attrs):
    # to speed things up
    o_dask = o_dask.isel(time=0, drop=True)
    f_prob_dask = f_prob_dask.isel(time=0, drop=True)
    xmin, xmax, tol = -10, 10, 1e-6
    cdf_or_dist = norm
    actual = crps_quadrature(o_dask,
                             cdf_or_dist,
                             xmin,
                             xmax,
                             tol,
                             keep_attrs=keep_attrs)
    expected = properscoring.crps_quadrature(o_dask, cdf_or_dist, xmin, xmax,
                                             tol)
    expected = xr.DataArray(expected, coords=o_dask.coords).mean()
    # test for numerical identity of xskillscore crps and crps
    assert_allclose(actual, expected)
    # test that xskillscore crps_ensemble returns chunks
    assert actual.chunks is not None
    # show that properscoring crps_ensemble returns no chunks
    assert expected.chunks is None
    if keep_attrs:
        assert actual.attrs == o_dask.attrs
    else:
        assert actual.attrs == {}
def test_crps_quadrature_accessor(o):
    # to speed things up
    o = o.isel(time=0, drop=True)
    cdf_or_dist = norm
    actual = crps_quadrature(o, cdf_or_dist)
    ds = xr.Dataset()
    ds["o"] = o
    expected = ds.xs.crps_quadrature("o", cdf_or_dist)
    assert_allclose(actual, expected)
def test_crps_quadrature_dim(o, dim, keep_attrs):
    # to speed things up
    o = o.isel(time=0, drop=True)
    cdf_or_dist = norm
    actual = crps_quadrature(o, cdf_or_dist, dim=dim, keep_attrs=keep_attrs)
    assert_only_dim_reduced(dim, actual, o)
    if keep_attrs:
        assert actual.attrs == o.attrs
    else:
        assert actual.attrs == {}
def test_crps_quadrature_accessor(o, dask_bool, outer_bool):
    cdf_or_dist = norm
    if dask_bool:
        o = o.chunk()
    actual = crps_quadrature(o, cdf_or_dist)

    ds = xr.Dataset()
    ds['o'] = o
    ds['cdf_or_dist'] = cdf_or_dist
    if outer_bool:
        ds = ds.drop_vars('cdf_or_dist')
        expected = ds.xs.crps_quadrature('o', cdf_or_dist)
    else:
        expected = ds.xs.crps_quadrature('o', 'cdf_or_dist')
    assert_allclose(actual, expected)
def test_crps_quadrature_api_and_inputs(o, f_prob, keep_attrs, input_type, chunk_bool):
    """Test that crps_quadrature keeps attributes, chunking, input types and equals
    properscoring.crps_quadrature."""
    o, f_prob = modify_inputs(o, f_prob, input_type, chunk_bool)
    # to speed things up
    o = o.isel(time=0, drop=True)
    cdf_or_dist = norm
    actual = crps_quadrature(o, cdf_or_dist, keep_attrs=keep_attrs)
    if input_type == "DataArray":  # properscoring allows only DataArrays
        expected = properscoring.crps_quadrature(o, cdf_or_dist)
        expected = xr.DataArray(expected, coords=o.coords).mean()
        # test for numerical identity of xskillscore crps and properscoring crps
        assert_allclose(actual, expected)
    # test that returns chunks
    assert_chunk(actual, chunk_bool)
    # test that attributes are kept
    assert_keep_attrs(actual, o, keep_attrs)
    # test that input types equal output types
    assign_type_input_output(actual, o)