Skip to content

Commit

Permalink
Merge branch 'master' into fix-mrt
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeitsperre authored Oct 23, 2023
2 parents 37ce657 + 4652182 commit 5d96c50
Show file tree
Hide file tree
Showing 9 changed files with 551 additions and 213 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ New features and enhancements
* Indicator ``generic.stats`` now accepts any frequency (previously only daily). (:pull:`1498`).
* Added argument ``out_units`` to ``select_resample_op`` to bypass limitations of ``to_agg_units`` in custom indicators. Add "var" to supported operations in ``to_agg_units``. (:pull:`1498`).
* `adapt_freq_thresh` argument added `sdba` training functions, allowing to perform frequency adaptation appropriately in each map block. (:pull:`1407`).
* Standardized indices (``xclim.indices.standardized_precipitation_index`` and ``xclim.indices.standardized_precipitation_evapotranspiration_index``) (:issue:`1270`, :issue:`1416`, :issue:`1474`, :pull:`1311`) were changed:
* Optimized and noticeably faster calculation.
* Can be computed in two steps: First compute fit parameters with ``xclim.indices.stats.standardized_index_fit_params``, then use the output in the standardized indices functions.
* The standardized index values are now clipped to ±8.21. This reflects the ``float64`` precision of the computation when cumulative distributed function values are inverted to a normal distribution and avoids returning infinite values.
* An offset parameter is now available to account for negative water balance values``xclim.indices.standardized_precipitation_evapotranspiration_index``.

Bug fixes
^^^^^^^^^
Expand All @@ -34,6 +39,7 @@ Bug fixes
* Fixed ``xclim.indices.run_length.lazy_indexing`` which would sometimes trigger the loading of auxiliary coordinates. (:issue:`1483`, :pull:`1484`).
* Indicators ``snd_season_length`` and ``snw_season_length`` will return 0 instead of NaN if all inputs have a (non-NaN) zero snow depth (or water-equivalent thickness). (:pull:`1492`, :issue:`1491`)
* Fixed a bug in the `pytest` configuration that could prevent testing data caching from occurring in systems where the platform-dependent cache directory is not found in the user's home. (:issue:`1468`, :pull:`1473`).
* Fix ``xclim.core.dataflags.data_flags`` variable name generation (:pull:`1507`).
* Remove nonsensical `stat='average'` option for ``mean_radiant_temperature``. (:issue:`1496`, :pull:`1501`).

Breaking changes
Expand All @@ -45,6 +51,7 @@ Breaking changes
* Default threshold in ``xclim.indices.snw_season_{start|length|end}`` changed form `20 kg m-2` to `4 kg m-2`. (:pull:`1505`).
* `xclim` development dependencies now include `ruff`. `pycodestyle` and `pydocstyle` have been replaced by `ruff` and removed from the `dev` installation recipe. (:pull:`1504`).
* The `mf_file` call signature found in ``xclim.ensembles.create_ensemble`` (and ``xclim.ensembles._ens_align_dataset``) has been removed (deprecated since `xclim` v0.43.0). (:pull:`1506`).
* ``xclim.indices.standardized_precipitation_index`` and ``xclim.indices.standardized_precipitation_evapotranspiration_index`` will no longer accept two datasets (data and calibration data). Instead, a single dataset covering both the calibration and evaluation periods is expected. (:issue:`1270`, :pull:`1311`).

Internal changes
^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.45.20-beta
current_version = 0.45.22-beta
commit = True
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+).(?P<patch>\d+)(\-(?P<release>[a-z]+))?
Expand Down
17 changes: 17 additions & 0 deletions tests/test_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,20 @@ def test_era5_ecad_qc_flag(self, open_dataset):

df_flagged = df.ecad_compliant(bad_ds)
np.testing.assert_array_equal(df_flagged.ecad_qc_flag, False)

def test_names(self, pr_series):
pr = pr_series(np.zeros(365), start="1971-01-01")
flgs = df.data_flags(
pr,
flags={
"values_op_thresh_repeating_for_n_or_more_days": {
"op": "==",
"n": 5,
"thresh": "-5.1 mm d-1",
}
},
)
assert (
list(flgs.data_vars.keys())[0]
== "values_eq_minus5point1_repeating_for_5_or_more_days"
)
63 changes: 57 additions & 6 deletions tests/test_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,10 @@ def test_standardized_precipitation_index(
ds = open_dataset("sdba/CanESM2_1950-2100.nc").isel(location=1)
pr = ds.pr.sel(time=slice("1998", "2000"))
pr_cal = ds.pr.sel(time=slice("1950", "1980"))
spi = xci.standardized_precipitation_index(
pr, pr_cal, freq, window, dist, method
params = xci.stats.standardized_index_fit_params(
pr_cal, freq=freq, window=window, dist=dist, method=method
)

spi = xci.standardized_precipitation_index(pr, params=params)
# Only a few moments before year 2000 are tested
spi = spi.isel(time=slice(-11, -1, 2))

Expand Down Expand Up @@ -610,11 +610,17 @@ def test_standardized_precipitation_evapotranspiration_index(
tas = tasmax - 2.5
tasmin = tasmax - 5
wb = xci.water_budget(pr, None, tasmin, tasmax, tas)
wb_cal = wb.sel(time=slice("1950", "1980"))
wb = wb.sel(time=slice("1998", "2000"))

params = xci.stats.standardized_index_fit_params(
wb.sel(time=slice("1950", "1980")),
freq=freq,
window=window,
dist=dist,
method=method,
offset="1 mm/d",
)
spei = xci.standardized_precipitation_evapotranspiration_index(
wb, wb_cal, freq, window, dist, method
wb.sel(time=slice("1998", "2000")), params=params
)

# Only a few moments before year 2000 are tested
Expand All @@ -625,6 +631,51 @@ def test_standardized_precipitation_evapotranspiration_index(

np.testing.assert_allclose(spei.values, values, rtol=0, atol=diff_tol)

def test_standardized_index_modularity(self, open_dataset):
freq, window, dist, method = "MS", 6, "gamma", "APP"
ds = (
open_dataset("sdba/CanESM2_1950-2100.nc")
.isel(location=1)
.sel(time=slice("1950", "2000"))
)
pr = ds.pr
# generate water budget
with xr.set_options(keep_attrs=True):
tasmax = ds.tasmax
tas = tasmax - 2.5
tasmin = tasmax - 5
wb = xci.water_budget(pr, None, tasmin, tasmax, tas)

params = xci.stats.standardized_index_fit_params(
wb.sel(time=slice("1950", "1980")),
freq=freq,
window=window,
dist=dist,
method=method,
offset="1 mm/d",
)
spei1 = xci.standardized_precipitation_evapotranspiration_index(
wb.sel(time=slice("1998", "2000")), params=params
)

spei2 = xci.standardized_precipitation_evapotranspiration_index(
wb,
freq=freq,
window=window,
dist=dist,
method=method,
offset="1 mm/d",
cal_start="1950",
cal_end="1980",
).sel(time=slice("1998", "2000"))

# In the previous computation, the first {window-1} values are NaN because the rolling is performed on the period [1998,2000].
# Here, the computation is performed on the period [1950,2000], *then* subsetted to [1998,2000], so it doesn't have NaNs
# for the first values
spei2[{"time": slice(0, window - 1)}] = np.nan

np.testing.assert_allclose(spei1.values, spei2.values, rtol=0, atol=1e-4)


class TestDailyFreezeThawCycles:
@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion xclim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

__author__ = """Travis Logan"""
__email__ = "[email protected]"
__version__ = "0.45.20-beta"
__version__ = "0.45.22-beta"


_module_data = _files("xclim.data")
Expand Down
Loading

0 comments on commit 5d96c50

Please sign in to comment.