Skip to content

Commit

Permalink
Merge branch 'main' into prep0.10.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeitsperre authored Nov 4, 2024
2 parents bb7a173 + 0fecccd commit b7ae920
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 85 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ New features and enhancements
Breaking changes
^^^^^^^^^^^^^^^^
* ``xs.get_warming_level`` has been renamed to ``xs.get_period_from_warming_level``. Its argument `return_horizon` was reversed and renamed `return_central_year` (:pull:`474`).
* Removed support for the deprecated `xclim` function `change_significance` in `ensemble_stats`. (:pull:`482`).

Bug fixes
^^^^^^^^^
* ``xs.io.save_to_table`` now correctly handles the case where the input is a `DataArray` or a `Dataset` with a single variable. (:pull:`473`).
* Fixed a bug in ``xs.utils.change_units`` where the original dataset was also getting modified. (:pull:`482`).

Internal changes
^^^^^^^^^^^^^^^^
* Bumped the version of `xclim` to 0.53.2. (:pull:`482`).

Internal changes
^^^^^^^^^^^^^^^^
Expand Down
12 changes: 6 additions & 6 deletions CI/requirements_ci.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
build==1.2.2
bump-my-version==0.26.1
build==1.2.2.post1
bump-my-version==0.28.1
coveralls==4.0.1
pip==24.2.0
setuptools==75.1.0
pip==24.3.1
setuptools==75.3.0
setuptools-scm==8.1.0
tox==4.21.0
tox-gh==1.4.1
tox==4.23.2
tox-gh==1.4.4
36 changes: 18 additions & 18 deletions CI/requirements_ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ bracex==2.4 \
--hash=sha256:a27eaf1df42cf561fed58b7a8f3fdf129d1ea16a81e1fadd1d17989bc6384beb \
--hash=sha256:efdc71eff95eaff5e0f8cfebe7d01adf2c8637c8c92edaf63ef348c241a82418
# via wcmatch
build==1.2.2 \
--hash=sha256:119b2fb462adef986483438377a13b2f42064a2a3a4161f24a0cca698a07ac8c \
--hash=sha256:277ccc71619d98afdd841a0e96ac9fe1593b823af481d3b0cea748e8894e0613
build==1.2.2.post1 \
--hash=sha256:1d61c0887fa860c01971625baae8bdd338e517b836a2f70dd1f7aa3a6b2fc5b5 \
--hash=sha256:b36993e92ca9375a219c99e606a122ff365a760a2d4bba0caa09bd5278b608b7
# via -r CI/requirements_ci.in
bump-my-version==0.26.1 \
--hash=sha256:af1cada726cf6f9a723d18941c68c325d5196453a180b3a42f8e0b38567d734d \
--hash=sha256:be09c48111eeba56f8c870b69718013f52b5b6c6e65bc3bda5bc928181901c48
bump-my-version==0.28.1 \
--hash=sha256:df7fdb02a1b43c122a6714df6d1fe4efc7a1220b5638ca5a0eb3018813c1b222 \
--hash=sha256:e608def5191baf505b6cde88bd679a0a95fc4cfeace4247adb60ac0f8a7e57ee
# via -r CI/requirements_ci.in
cachetools==5.5.0 \
--hash=sha256:02134e8439cdc2ffb62023ce1debca2944c3f289d66bb17ead3ab3dede74b292 \
Expand Down Expand Up @@ -391,15 +391,15 @@ tomlkit==0.13.0 \
--hash=sha256:08ad192699734149f5b97b45f1f18dad7eb1b6d16bc72ad0c2335772650d7b72 \
--hash=sha256:7075d3042d03b80f603482d69bf0c8f345c2b30e41699fd8883227f89972b264
# via bump-my-version
tox==4.21.0 \
--hash=sha256:693ac51378255d34ad7aab6dd2ce9ab6a1cf1924eb930183fde850ad503b681d \
--hash=sha256:e64dd9847ff3a7ec90368be412d7efe61a39caf043222ffbe9ad638ea435f6f6
tox==4.23.2 \
--hash=sha256:452bc32bb031f2282881a2118923176445bac783ab97c874b8770ab4c3b76c38 \
--hash=sha256:86075e00e555df6e82e74cfc333917f91ecb47ffbc868dcafbd2672e332f4a2c
# via
# -r CI/requirements_ci.in
# tox-gh
tox-gh==1.4.1 \
--hash=sha256:005b33d16eef1bd1dae9f7d8b3cef53374af7d475f9c9c33ef098247741fb694 \
--hash=sha256:da422beccbdc5ad5994fe8faf6c193f2d794e957628b052ba23e7fcf9e2e340f
tox-gh==1.4.4 \
--hash=sha256:4ea585f66585b90f5826b1677cfc9453747792a0f9ff83d468603bc17556e07b \
--hash=sha256:b962e0f8c4619e98d11c2a135939876691e148b843b7dac4cff7de1dc4f7c215
# via -r CI/requirements_ci.in
typing-extensions==4.12.2 \
--hash=sha256:04e5ca0351e0f3f85c6853954072df659d0d13fac324d0072316b67d7794700d \
Expand Down Expand Up @@ -427,13 +427,13 @@ wcwidth==0.2.13 \
# via prompt-toolkit

# The following packages are considered to be unsafe in a requirements file:
pip==24.2 \
--hash=sha256:2cd581cf58ab7fcfca4ce8efa6dcacd0de5bf8d0a3eb9ec927e07405f4d9e2a2 \
--hash=sha256:5b5e490b5e9cb275c879595064adce9ebd31b854e3e803740b72f9ccf34a45b8
pip==24.3.1 \
--hash=sha256:3790624780082365f47549d032f3770eeb2b1e8bd1f7b2e02dace1afa361b4ed \
--hash=sha256:ebcb60557f2aefabc2e0f918751cd24ea0d56d8ec5445fe1807f1d2109660b99
# via -r CI/requirements_ci.in
setuptools==75.1.0 \
--hash=sha256:35ab7fd3bcd95e6b7fd704e4a1539513edad446c097797f2985e0e4b960772f2 \
--hash=sha256:d59a21b17a275fb872a9c3dae73963160ae079f1049ed956880cd7c09b120538
setuptools==75.3.0 \
--hash=sha256:f2504966861356aa38616760c0f66568e535562374995367b4e69c7143cf6bcd \
--hash=sha256:fba5dd4d766e97be1b1681d98712680ae8f2f26d7881245f2ce9e40714f1a686
# via
# -r CI/requirements_ci.in
# setuptools-scm
15 changes: 11 additions & 4 deletions docs/notebooks/4_ensembles.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@
"}\n",
"\n",
"for d in datasets:\n",
" ds = open_dataset(datasets[d], branch=\"v2023.12.14\").isel(\n",
" lon=slice(0, 4), lat=slice(0, 4)\n",
" )\n",
" ds = open_dataset(datasets[d]).isel(lon=slice(0, 4), lat=slice(0, 4))\n",
" ds = xs.climatological_op(\n",
" ds,\n",
" op=\"mean\",\n",
Expand All @@ -66,6 +64,15 @@
" datasets[d] = xs.compute_deltas(ds, reference_horizon=\"1981-2010\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"datasets"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -263,7 +270,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
8 changes: 4 additions & 4 deletions environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies:
- sparse
- toolz
- xarray >=2023.11.0, !=2024.6.0
- xclim >=0.53.1, <0.54
- xclim >=0.53.2, <0.54
- xesmf >=0.7
- zarr >=2.13
# Opt
Expand All @@ -40,13 +40,13 @@ dependencies:
- babel
- black ==24.8.0
- blackdoc ==0.3.9
- bump-my-version >=0.25.1
- bump-my-version >=0.26.8
- coverage>=7.5.0
- coveralls>=4.0.1
- flake8 >=6.1.0
- flake8 >=7.1.0
- flake8-rst-docstrings>=0.3.0
- ipykernel
- ipython
- ipython >=8.5.0
- isort ==5.13.2
- jupyter_client
- nbsphinx
Expand Down
4 changes: 2 additions & 2 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies:
- sparse
- toolz
- xarray >=2023.11.0, !=2024.6.0
- xclim >=0.53.1, <0.54
- xclim >=0.53.2, <0.54
- xesmf >=0.7
- zarr >=2.13
# To install from source
Expand All @@ -39,4 +39,4 @@ dependencies:
# Opt
- nc-time-axis >=1.3.1
- pyarrow >=10.0.1
- pip
- pip >=24.2.0
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ dependencies = [
"sparse",
"toolz",
"xarray >=2023.11.0, !=2024.6.0",
"xclim >=0.53.1, <0.54",
"xclim >=0.53.2, <0.54",
"zarr >=2.13"
]

Expand All @@ -74,7 +74,7 @@ dev = [
# Dev tools and testing
"pip >=24.2.0",
"babel",
"black[jupyter] ==24.8.0",
"black[jupyter] ==24.10.0",
"blackdoc ==0.3.9",
"bump-my-version >=0.26.0",
"coverage >=7.5.0",
Expand All @@ -84,7 +84,7 @@ dev = [
"isort ==5.13.2",
"mypy",
"numpydoc >=1.8.0",
"pooch",
"pooch >=1.8.0",
"pre-commit >=3.3.2",
"pytest-cov >=5.0.0",
"pytest >=8.3.2",
Expand All @@ -97,7 +97,7 @@ dev = [
docs = [
# Documentation and examples
"ipykernel",
"ipython",
"ipython >=8.5.0",
"jupyter_client",
"nbsphinx",
"nbval",
Expand Down
29 changes: 2 additions & 27 deletions src/xscen/ensembles.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def ensemble_stats( # noqa: C901
# if input files are .zarr, change the engine automatically
if isinstance(datasets, list) and isinstance(datasets[0], str | os.PathLike):
path = Path(datasets[0])
if path.suffix == ".zarr":
if path.suffix in [".zarr", ".zip"]:
create_kwargs.setdefault("engine", "zarr")

if not isinstance(datasets, xr.Dataset):
Expand Down Expand Up @@ -137,6 +137,7 @@ def ensemble_stats( # noqa: C901

# Workaround for robustness_categories
real_stat = None
categories_kwargs = {}
if stat == "robustness_categories":
real_stat = "robustness_categories"
stat = "robustness_fractions"
Expand All @@ -155,20 +156,10 @@ def ensemble_stats( # noqa: C901
f"Weighting is not supported for '{stat}'. The results may be incorrect."
)

# FIXME: change_significance is deprecated and will be removed in xclim 0.49.
if stat in [
"change_significance",
"robustness_fractions",
"robustness_categories",
]:
# 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_to_compute) > 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:
with xr.set_options(keep_attrs=True):
Expand All @@ -188,22 +179,6 @@ def ensemble_stats( # noqa: C901
# Call the function
tmp = getattr(ensembles, stat)(ens_v, **stats_kwargs)

# Manage the multiple outputs of change_significance
# FIXME: change_significance is deprecated and will be removed in xclim 0.49.
if (
stat == "change_significance"
and stats_kwargs.get("p_vals", False) is False
):
ens_stats[f"{v}_change_frac"], ens_stats[f"{v}_pos_frac"] = tmp
elif stat == "change_significance" and stats_kwargs.get(
"p_vals", False
):
(
ens_stats[f"{v}_change_frac"],
ens_stats[f"{v}_pos_frac"],
ens_stats[f"{v}_p_vals"],
) = tmp

# Robustness categories
if real_stat == "robustness_categories":
categories = ensembles.robustness_categories(
Expand Down
14 changes: 10 additions & 4 deletions src/xscen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,20 +812,26 @@ def change_units(ds: xr.Dataset, variables_and_units: dict) -> xr.Dataset:
).dimensionality.get("[time]")

if time_in_ds == time_in_out:
ds[v] = units.convert_units_to(ds[v], variables_and_units[v])
ds = ds.assign(
{v: units.convert_units_to(ds[v], variables_and_units[v])}
)
elif time_in_ds - time_in_out == 1:
# ds is an amount
ds[v] = units.amount2rate(ds[v], out_units=variables_and_units[v])
ds = ds.assign(
{v: units.amount2rate(ds[v], out_units=variables_and_units[v])}
)
elif time_in_ds - time_in_out == -1:
# ds is a rate
ds[v] = units.rate2amount(ds[v], out_units=variables_and_units[v])
ds = ds.assign(
{v: units.rate2amount(ds[v], out_units=variables_and_units[v])}
)
else:
raise ValueError(
f"No known transformation between {ds[v].units} and {variables_and_units[v]} (temporal dimensionality mismatch)."
)
elif (v in ds) and (ds[v].units != variables_and_units[v]):
# update unit name if physical units are equal but not their name (ex. degC vs °C)
ds[v] = ds[v].assign_attrs(units=variables_and_units[v])
ds = ds.assign({v: ds[v].assign_attrs(units=variables_and_units[v])})

return ds

Expand Down
16 changes: 0 additions & 16 deletions tests/test_ensembles.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,22 +258,6 @@ 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

0 comments on commit b7ae920

Please sign in to comment.