Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'sub': '-',
'mul': '*',
'div': '/',
}
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:
"""Checks that the verification data dimensions match appropriate initialized
dimensions.
If uninitialized, ignore ``member``. Otherwise, ignore ``lead`` and ``member``.
"""
# Since verification data won't have the ``init``` dimension, temporarily rename to
# time.
init = init.rename({'init': 'time'})
init_dims = list(init.dims)
if 'lead' in init_dims:
init_dims.remove('lead')
if ('member' in init_dims) and not uninitialized:
init_dims.remove('member')
if not (set(verif.dims) == set(init_dims)):
unmatch_dims = set(verif.dims) ^ set(init_dims)
raise DimensionError(
'Dimensions must match initialized prediction ensemble '
f'dimensions; these dimensions do not match: {unmatch_dims}.'
)
return True
Returns:
ds (xarray object): xr.Dataset/xr.DataArray with less members.
Raises:
DimensionError: if list items are not all in ds.member
"""
if removed_member is None:
removed_member = [0]
if all(m in ds.member.values for m in removed_member):
member_list = list(ds.member.values)
for ens in removed_member:
member_list.remove(ens)
else:
raise DimensionError('select available members only')
return ds.sel(member=member_list)
Reference:
* https://stackoverflow.com/questions/1528237/
how-to-handle-exceptions-in-a-list-comprehensions
"""
try:
return getattr(v, name)(*args, **kwargs)
# ValueError : Cases such as .sum(dim='time'). This doesn't apply
# it to the given dataset if the dimension doesn't exist.
# KeyError : Cases where a function calls the index of a Dataset. Such
# as ds[dim] and the dim doesn't exist as a key.
# DimensionError: This accounts for our custom error when applying
# some stats functions.
# NOTE: Remove the esmtools version once you remove those errors from
# esmtools.
except (ValueError, KeyError, DimensionError):
return v