Skip to content

Commit

Permalink
add error in case of improper inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
RondeauG committed Dec 6, 2023
1 parent e49db06 commit ecb8701
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/test_ensembles.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ def test_common_attrs_only(self, common_attrs_only):

def test_errors(self):
ens = self.make_ensemble(10)

# Warning if the statistic does not support weighting
weights = xr.DataArray([0] * 5 + [1] * 5, dims="realization")
with pytest.warns(UserWarning, match="Weighting is not supported"):
with pytest.raises(
Expand All @@ -231,6 +233,8 @@ def test_errors(self):
xs.ensemble_stats(
ens, statistics={"kkz_reduce_ensemble": None}, weights=weights
)

# Error if you try to use a relative delta with a reference dataset
for e in ens:
e["tg_mean"].attrs["delta_kind"] = "rel."
ref = weights
Expand All @@ -248,6 +252,22 @@ def test_errors(self):
},
)

# Error if you try to use a robustness_fractions with a reference dataset, but also specify other statistics
with pytest.raises(
ValueError, match="The input requirements for 'robustness_fractions'"
):
xs.ensemble_stats(
ens,
statistics={
"robustness_fractions": {
"test": "threshold",
"abs_thresh": 2.5,
"ref": ref,
},
"ensemble_mean_std_max_min": None,
},
)


class TestGenerateWeights:
@staticmethod
Expand Down
4 changes: 4 additions & 0 deletions xscen/ensembles.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ def ensemble_stats( # noqa: C901
# FIXME: This can be removed once change_significance is removed.
# It's here because the 'ref' default was removed for change_significance in xclim 0.47.
stats_kwargs.setdefault("ref", None)
if (stats_kwargs.get("ref") is not None) and len(statistics.keys()) > 1:
raise ValueError(
f"The input requirements for '{stat}' when 'ref' is specified are not compatible with other statistics."
)

# These statistics only work on DataArrays
for v in ens.data_vars:
Expand Down

0 comments on commit ecb8701

Please sign in to comment.