How to use the climpred.exceptions.VariableError 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 / classes.py View on Github external
error_str = f'Cannot use {type(self)} {OPERATOR_STR[operator]} {type(other)}'

        # catch undefined types for other
        if not isinstance(other, tuple(ALLOWED_TYPES_FOR_MATH_OPERATORS)):
            raise TypeError(
                f'{error_str} because type {type(other)} not supported. '
                f'Please choose from {ALLOWED_TYPES_FOR_MATH_OPERATORS}.'
            )
        # catch other dimensions in other
        if isinstance(other, tuple([xr.Dataset, xr.DataArray])):
            if not set(other.dims).issubset(self._datasets['initialized'].dims):
                raise DimensionError(f'{error_str} containing new dimensions.')
        # catch xr.Dataset with different data_vars
        if isinstance(other, xr.Dataset):
            if list(other.data_vars) != list(self._datasets['initialized'].data_vars):
                raise VariableError(
                    f'{error_str} with new `data_vars`. Please use {type(self)} '
                    f'{operator} {type(other)} only with same `data_vars`. Found '
                    f'initialized.data_vars = '
                    f' {list(self._datasets["initialized"].data_vars)} vs. '
                    f'other.data_vars = { list(other.data_vars)}.'
                )

        operator = eval(operator)

        # Create temporary copy to modify to avoid inplace operation.
        datasets = self._datasets.copy()
        for dataset in datasets:
            if isinstance(other, PredictionEnsemble):
                other_dataset = other._datasets[dataset]
            else:
                other_dataset = other
github bradyrx / climpred / climpred / checks.py View on Github external
def match_initialized_vars(init, verif):
    """Checks that a new verification dataset has at least one variable
    in common with the initialized dataset.

    This ensures that they can be compared pairwise.

    Args:
        init (xarray object): Initialized forecast ensemble.
        verif (xarray object): Product to be added.
    """
    init_vars = init.data_vars
    verif_vars = verif.data_vars
    # https://stackoverflow.com/questions/10668282/
    # one-liner-to-check-if-at-least-one-item-in-list-exists-in-another-list
    if set(init_vars).isdisjoint(verif_vars):
        raise VariableError(
            'Please provide a Dataset/DataArray with at least '
            'one matching variable to the initialized prediction ensemble; '
            f'got {init_vars} for init and {verif_vars} for verif.'
        )
    return True