diff --git a/CHANGES.rst b/CHANGES.rst index b89eb7d07..1680d3857 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,7 @@ Changelog v0.49.0 (unreleased) -------------------- -Contributors to this version: Trevor James Smith (:user:`Zeitsperre`), Pascal Bourgault (:user:`aulemahal`), Juliette Lavoie (:user:`juliettelavoie`), David Huard (:user:`huard`). +Contributors to this version: Trevor James Smith (:user:`Zeitsperre`), Pascal Bourgault (:user:`aulemahal`), Juliette Lavoie (:user:`juliettelavoie`), David Huard (:user:`huard`), Gabriel Rondeau-Genesse (:user:`RondeauG`). Announcements ^^^^^^^^^^^^^ @@ -27,6 +27,7 @@ Bug fixes * Fixed bug with loess smoothing for an array full of NaNs. (:pull:`1699`). * Fixed and adapted ``time_bnds`` to the newest xarray. (:pull:`1700`). * Fixed "agreement fraction" in ``robustness_fractions`` to distinguish between negative change and no change. Added "negative" and "changed negative" fractions (:issue:`1690`, :pull:`1711`). +* ``make_criteria`` now skips columns with NaNs across all realizations. (:pull:`1713`). Internal changes ^^^^^^^^^^^^^^^^ diff --git a/tests/test_ensembles.py b/tests/test_ensembles.py index 63d62e6d4..91fafb92e 100644 --- a/tests/test_ensembles.py +++ b/tests/test_ensembles.py @@ -566,6 +566,15 @@ def test_make_criteria(self, tas_series): uncrit = crit.unstack("criteria") assert set(uncrit.dims) == {"realization", "lat", "time"} + crit = ensembles.make_criteria(ds.where(ds.var_a > 0)) + assert crit.dims == ("realization", "criteria") + assert crit.criteria.size == 12 + np.testing.assert_array_equal(crit.isnull().sum(), 0) + np.testing.assert_array_equal(crit.min(), 1) + uncrit = crit.unstack("criteria").to_dataset("variables") + assert set(uncrit.dims) == {"realization", "lat", "time"} + assert uncrit.time.size == 3 + # ## Tests for Robustness ## @pytest.fixture diff --git a/xclim/ensembles/_reduce.py b/xclim/ensembles/_reduce.py index cdcc3599d..404188fd2 100644 --- a/xclim/ensembles/_reduce.py +++ b/xclim/ensembles/_reduce.py @@ -57,6 +57,8 @@ def make_criteria(ds: xarray.Dataset | xarray.DataArray): `ds2` will have all variables with the same dimensions, so if the original dataset had variables with different dimensions, the added dimensions are filled with NaNs. + Also, note that criteria that are all NaN (such as lat/lon coordinates with no data) are dropped from `crit` to avoid issues with + the clustering algorithms, so the original dataset might not be able to be fully reconstructed. The `to_dataset` part can be skipped if the original input was a DataArray. """ @@ -113,6 +115,9 @@ def _make_crit(da): else: # Easy peasy, skip all the convoluted stuff crit = _make_crit(ds) + + # drop criteria that are all NaN + crit = crit.dropna(dim="criteria", how="all") return crit.rename("criteria")