Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support xclim 0.53.1 #482

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 modified. (:pull:`482`).

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

v0.10.0 (2024-09-30)
--------------------
Expand Down
2 changes: 1 addition & 1 deletion 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.52.2, <0.53
- xclim >=0.53.1, <0.54
- xesmf >=0.7
- zarr >=2.13
# Opt
Expand Down
2 changes: 1 addition & 1 deletion 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.52.2, <0.53
- xclim >=0.53.1, <0.54
- xesmf >=0.7
- zarr >=2.13
# To install from source
Expand Down
2 changes: 1 addition & 1 deletion 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.52.2, <0.53",
"xclim >=0.53.1, <0.54",
"zarr >=2.13"
]

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
Loading