How to use the climpred.comparisons.Comparison function in climpred

To help you get started, we’ve selected a few climpred examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bradyrx / climpred / climpred / comparisons.py View on Github external
Returns:
        xr.object: forecast, verif.
    """
    # check that this contains more than one member
    has_dims(hind, 'member', 'decadal prediction ensemble')
    has_min_len(hind['member'], 1, 'decadal prediction ensemble member')
    forecast, verif = _broadcast_non_CLIMPRED_DIMS_from_forecast_to_verif(hind, verif)
    if not metric.probabilistic and 'member' not in verif.dims:
        verif = verif.expand_dims('member')
        nMember = forecast.member.size
        verif = verif.isel(member=[0] * nMember)
        verif['member'] = forecast['member']
    return forecast, verif


__m2o = Comparison(
    name='m2o',
    function=_m2o,
    hindcast=True,
    probabilistic=True,
    long_name='Verify each individual forecast member against the verification data.',
    aliases=['m2r'],
)


__ALL_COMPARISONS__ = [__m2m, __m2e, __m2c, __e2c, __e2o, __m2o]

COMPARISON_ALIASES = dict()
for c in __ALL_COMPARISONS__:
    if c.aliases is not None:
        for a in c.aliases:
            COMPARISON_ALIASES[a] = c.name
github bradyrx / climpred / climpred / comparisons.py View on Github external
for m in ds.member.values:
        forecast = _drop_members(ds, removed_member=[m]).mean('member')
        reference = ds.sel(member=m).squeeze()
        forecast_list.append(forecast)
        reference_list.append(reference)
    reference = xr.concat(reference_list, M2E_COMPARISON_DIM)
    forecast = xr.concat(forecast_list, M2E_COMPARISON_DIM)
    forecast[M2E_COMPARISON_DIM] = np.arange(forecast[M2E_COMPARISON_DIM].size)
    reference[M2E_COMPARISON_DIM] = np.arange(reference[M2E_COMPARISON_DIM].size)
    if dask.is_dask_collection(forecast):
        forecast = _transpose_and_rechunk_to(forecast, ds)
        reference = _transpose_and_rechunk_to(reference, ds)
    return forecast, reference


__m2e = Comparison(
    name='m2e',
    function=_m2e,
    hindcast=False,
    probabilistic=False,
    long_name='Comparison of all members as verification vs. the ensemble mean'
    'forecast',
)


def _m2c(ds, control_member=None, metric=None):
    """
    Compare all other members forecasts to control member verification.

    Args:
        ds (xarray object): xr.Dataset/xr.DataArray with member and ensemble
                            dimension.
github bradyrx / climpred / climpred / comparisons.py View on Github external
# set incrementing members to avoid nans from broadcasting
        forecast['member'] = np.arange(1, 1 + forecast.member.size)
        reference = ds.sel(member=m).squeeze()
        # Tiles the singular "reference" member to compare directly to all other members
        if not metric.probabilistic:
            forecast, reference = xr.broadcast(forecast, reference)
        reference_list.append(reference)
        forecast_list.append(forecast)
    reference = xr.concat(reference_list, M2M_MEMBER_DIM)
    forecast = xr.concat(forecast_list, M2M_MEMBER_DIM)
    reference[M2M_MEMBER_DIM] = np.arange(reference[M2M_MEMBER_DIM].size)
    forecast[M2M_MEMBER_DIM] = np.arange(forecast[M2M_MEMBER_DIM].size)
    return forecast, reference


__m2m = Comparison(
    name='m2m',
    function=_m2m,
    hindcast=False,
    probabilistic=True,
    long_name='Comparison of all forecasts vs. all other members as verification',
)


def _m2e(ds, metric=None):
    """
    Compare all members to ensemble mean while leaving out the reference in
     ensemble mean.

    Args:
        ds (xarray object): xr.Dataset/xr.DataArray with member and ensemble
                            dimension.
github bradyrx / climpred / climpred / comparisons.py View on Github external
but expected by internal API.

    Returns:
        xr.object: forecast, verif.
    """
    if 'member' in hind.dims:
        forecast = hind.mean('member')
    else:
        forecast = hind
    forecast, verif = _broadcast_non_CLIMPRED_DIMS_from_forecast_to_verif(
        forecast, verif
    )
    return forecast, verif


__e2o = Comparison(
    name='e2o',
    function=_e2o,
    hindcast=True,
    probabilistic=False,
    long_name='Verify the ensemble mean against the verification data',
    aliases=['e2r'],
)


def _m2o(hind, verif, metric=None):
    """Compares each ensemble member individually to the verification data for a
    ``HindcastEnsemble`` setup.

    Args:
        hind (xarray object): Hindcast with ``member`` dimension.
        verif (xarray object): Verification data.
github bradyrx / climpred / climpred / comparisons.py View on Github external
Returns:
        xr.object: forecast, reference.
    """
    if control_member is None:
        control_member = [0]
    reference = ds.isel(member=control_member).squeeze()
    # drop the member being reference
    forecast = _drop_members(ds, removed_member=ds.member.values[control_member])
    if not metric.probabilistic:
        forecast, reference = xr.broadcast(forecast, reference)
    elif 'member' in reference.coords:
        del reference['member']
    return forecast, reference


__m2c = Comparison(
    name='m2c',
    function=_m2c,
    hindcast=False,
    probabilistic=True,
    long_name='Comparison of multiple forecasts vs. control verification',
)


def _e2c(ds, control_member=None, metric=None):
    """
    Compare ensemble mean forecast to control member verification.

    Args:
        ds (xarray object): xr.Dataset/xr.DataArray with member and ensemble
                            dimension.
        control_member: list of the one integer member serving as
github bradyrx / climpred / climpred / utils.py View on Github external
* m2e: Compare all members to the ensemble mean.
        * e2c: Compare the ensemble mean to the control.

    Hindcast:

        * e2o: Compare the ensemble mean to the verification data.
        * m2o: Compare each ensemble member to the verification data.

    Args:
        comparison (str): name of comparison.

    Returns:
        comparison (Comparison): comparison class.

    """
    if isinstance(comparison, comparisons.Comparison):
        return comparison
    elif isinstance(comparison, str):
        # check if comparison allowed
        is_in_list(comparison, list_, 'comparison')
        comparison = COMPARISON_ALIASES.get(comparison, comparison)
        return getattr(comparisons, '__' + comparison)
    else:
        is_in_list(comparison, list_, 'comparison')
        return getattr(comparisons, '__' + comparison)
github bradyrx / climpred / climpred / comparisons.py View on Github external
Returns:
        xr.object: forecast, reference.
    """
    # stack_dim irrelevant
    if control_member is None:
        control_member = [0]
    reference = ds.isel(member=control_member).squeeze()
    if 'member' in reference.coords:
        del reference['member']
    ds = _drop_members(ds, removed_member=[ds.member.values[control_member]])
    forecast = ds.mean('member')
    return forecast, reference


__e2c = Comparison(
    name='e2c',
    function=_e2c,
    hindcast=False,
    probabilistic=False,
    long_name='Comparison of the ensemble mean forecast vs. control as verification',
)


# --------------------------------------------#
# HINDCAST COMPARISONS
# --------------------------------------------#
def _e2o(hind, verif, metric=None):
    """Compare the ensemble mean forecast to the verification data for a
    ``HindcastEnsemble`` setup.

    Args: