Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into fix-cf-temperature
Browse files Browse the repository at this point in the history
# Conflicts:
#	environment-dev.yml
#	environment.yml
#	pyproject.toml
  • Loading branch information
RondeauG committed Nov 4, 2024
2 parents 79996f9 + 211816f commit 20f65f7
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .cruft.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"project_slug": "xscen",
"project_short_description": "A climate change scenario-building analysis framework, built with xclim/xarray.",
"pypi_username": "RondeauG",
"version": "0.10.1-dev.4",
"version": "0.10.1-dev.5",
"use_pytest": "y",
"use_black": "y",
"use_conda": "y",
Expand Down
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`).

v0.10.0 (2024-09-30)
--------------------
Expand Down
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 @@ -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 Expand Up @@ -133,7 +133,7 @@ target-version = [
]

[tool.bumpversion]
current_version = "0.10.1-dev.4"
current_version = "0.10.1-dev.5"
commit = true
commit_args = "--no-verify"
tag = false
Expand Down
2 changes: 1 addition & 1 deletion src/xscen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

__author__ = """Gabriel Rondeau-Genesse"""
__email__ = "[email protected]"
__version__ = "0.10.1-dev.4"
__version__ = "0.10.1-dev.5"


def warning_on_one_line(
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 20f65f7

Please sign in to comment.