Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Reference:
* Chapter 8 (Short-Term Climate Prediction) in Van den Dool, Huug.
Empirical methods in short-term climate prediction.
Oxford University Press, 2007.
"""
# Check that init is int, cftime, or datetime; convert ints or cftime to datetime.
hind = convert_time_index(hind, 'init', 'hind[init]')
verif = convert_time_index(verif, 'time', 'verif[time]')
# Put this after `convert_time_index` since it assigns 'years' attribute if the
# `init` dimension is a `float` or `int`.
has_valid_lead_units(hind)
# get metric/comparison function name, not the alias
metric = METRIC_ALIASES.get(metric, metric)
# get class metric(Metric)
metric = get_metric_class(metric, DETERMINISTIC_HINDCAST_METRICS)
if metric.probabilistic:
raise ValueError(
'probabilistic metric ',
metric.name,
'cannot compute persistence forecast.',
)
# If lead 0, need to make modifications to get proper persistence, since persistence
# at lead 0 is == 1.
if [0] in hind.lead.values:
hind = hind.copy()
with xr.set_options(keep_attrs=True): # keeps lead.attrs['units']
hind['lead'] = hind['lead'] + 1
should be based on the same set of verification dates.
add_attrs (bool): write climpred compute args to attrs. default: True
** metric_kwargs (dict): additional keywords to be passed to metric
Returns:
u (xarray object): Results from comparison at the first lag.
"""
# Check that init is int, cftime, or datetime; convert ints or cftime to datetime.
hind = convert_time_index(hind, 'init', 'hind[init]')
uninit = convert_time_index(uninit, 'time', 'uninit[time]')
verif = convert_time_index(verif, 'time', 'verif[time]')
has_valid_lead_units(hind)
# get metric/comparison function name, not the alias
metric = METRIC_ALIASES.get(metric, metric)
comparison = COMPARISON_ALIASES.get(comparison, comparison)
comparison = get_comparison_class(comparison, HINDCAST_COMPARISONS)
metric = get_metric_class(metric, DETERMINISTIC_HINDCAST_METRICS)
forecast, verif = comparison.function(uninit, verif, metric=metric)
hind = hind.rename({'init': 'time'})
_, verif_dates = return_inits_and_verif_dates(hind, verif, alignment=alignment)
plag = []
# TODO: Refactor this, getting rid of `compute_uninitialized` completely.
# `same_verifs` does not need to go through the loop, since it's a fixed
# skill over all leads.
for i in hind['lead'].values:
# Ensure that the uninitialized reference has all of the
* compute_perfect_model()
* compute_hindcast()
Args:
metric (str): name of metric.
list_ (list): check whether metric in list
Returns:
metric (Metric): class object of the metric.
"""
if isinstance(metric, metrics.Metric):
return metric
elif isinstance(metric, str):
# check if metric allowed
is_in_list(metric, list_, 'metric')
metric = METRIC_ALIASES.get(metric, metric)
return getattr(metrics, '__' + metric)
else:
raise ValueError(
f'Please provide metric as str or Metric class, found {type(metric)}'
)
comparison (Comparison): comparison class.
dim (list of str or str): corrected dimension to apply metric to.
"""
# check kind allowed
is_in_list(kind, ['hindcast', 'PM'], 'kind')
# set default dim
if dim is None:
dim = 'init' if kind == 'hindcast' else ['init', 'member']
# check allowed dims
if kind == 'hindcast':
is_in_list(dim, ['member', 'init'], 'dim')
elif kind == 'PM':
is_in_list(dim, ['member', 'init', ['init', 'member']], 'dim')
# get metric and comparison strings incorporating alias
metric = METRIC_ALIASES.get(metric, metric)
comparison = COMPARISON_ALIASES.get(comparison, comparison)
METRICS = HINDCAST_METRICS if kind == 'hindcast' else PM_METRICS
COMPARISONS = HINDCAST_COMPARISONS if kind == 'hindcast' else PM_COMPARISONS
metric = get_metric_class(metric, METRICS)
comparison = get_comparison_class(comparison, COMPARISONS)
# check whether combination of metric and comparison works
PROBABILISTIC_COMPARISONS = (
PROBABILISTIC_HINDCAST_COMPARISONS
if kind == 'hindcast'
else PROBABILISTIC_PM_COMPARISONS
)
if metric.probabilistic:
if not comparison.probabilistic:
raise ValueError(