From 2c85ad6e51d4fbd41065692d6eccb23910e29ec8 Mon Sep 17 00:00:00 2001 From: Pascal Bourgault Date: Tue, 7 May 2024 13:55:40 -0400 Subject: [PATCH 1/4] Fixes for xclim 0.50 - Better defaults for opening --- pyproject.toml | 8 +++++--- tests/test_ensembles.py | 15 --------------- xscen/catalog.py | 4 ++++ xscen/extract.py | 11 +++++------ xscen/indicators.py | 2 +- xscen/utils.py | 14 ++++++++++++++ 6 files changed, 29 insertions(+), 25 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index dc4826e1..0e0229a4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -202,6 +202,11 @@ exclude = [ "build", "docs" ] + +[tool.ruff.format] +line-ending = "auto" + +[tool.ruff.lint] ignore = [ "D205", "D400", @@ -215,9 +220,6 @@ select = [ "W" ] -[tool.ruff.format] -line-ending = "auto" - [tool.ruff.lint.flake8-bandit] check-typed-exception = true diff --git a/tests/test_ensembles.py b/tests/test_ensembles.py index cb85d497..6cbe9d58 100644 --- a/tests/test_ensembles.py +++ b/tests/test_ensembles.py @@ -119,21 +119,6 @@ def test_weights(self, weights, to_level): decimal=2, ) - # FIXME: This function is deprecated, so this test should be removed eventually. - @pytest.mark.parametrize("p_vals", [True, False]) - def test_change_significance(self, p_vals): - ens = self.make_ensemble(10) - with pytest.warns( - FutureWarning, - match="Function change_significance is deprecated as of xclim 0.47", - ): - out = xs.ensemble_stats( - ens, - statistics={"change_significance": {"test": None, "p_vals": p_vals}}, - ) - - assert len(out.data_vars) == 3 if p_vals else 2 - @pytest.mark.parametrize("fractions", ["only", "both", "nested", "missing"]) def test_robustness_input(self, fractions): ens = self.make_ensemble(10) diff --git a/xscen/catalog.py b/xscen/catalog.py index 82ba671d..71877bd2 100644 --- a/xscen/catalog.py +++ b/xscen/catalog.py @@ -23,6 +23,7 @@ from .config import CONFIG, args_as_str, recursive_update from .utils import ( + _xarray_defaults, date_parser, ensure_correct_time, ensure_new_xrfreq, @@ -529,6 +530,9 @@ def preprocess(ds): raise ValueError( f"Expected exactly one dataset, received {N} instead : {cat.keys()}" ) + + kwargs = _xarray_defaults(**kwargs) + ds = cat.to_dask(**kwargs) return ds diff --git a/xscen/extract.py b/xscen/extract.py index b057637d..79b19cce 100644 --- a/xscen/extract.py +++ b/xscen/extract.py @@ -29,7 +29,7 @@ from .config import parse_config from .indicators import load_xclim_module, registry_from_module from .spatial import subset -from .utils import CV +from .utils import CV, _xarray_kwargs from .utils import ensure_correct_time as _ensure_correct_time from .utils import get_cat_attrs, natural_sort, standardize_periods, xrfreq_to_timedelta @@ -161,17 +161,16 @@ def extract_dataset( # noqa: C901 } # Default arguments to send xarray - xr_open_kwargs = xr_open_kwargs or {} - xr_combine_kwargs = xr_combine_kwargs or {} - xr_combine_kwargs.setdefault("data_vars", "minimal") + xr_kwargs = _xarray_kwargs( + xr_open_kwargs=xr_open_kwargs or {}, xr_combine_kwargs=xr_combine_kwargs or {} + ) # Open the catalog ds_dict = catalog.to_dataset_dict( - xarray_open_kwargs=xr_open_kwargs, - xarray_combine_by_coords_kwargs=xr_combine_kwargs, preprocess=preprocess, # Only print a progress bar when it is minimally useful progressbar=(len(catalog.keys()) > 1), + **xr_kwargs, ) out_dict = {} diff --git a/xscen/indicators.py b/xscen/indicators.py index 68e43df5..f47b2b12 100644 --- a/xscen/indicators.py +++ b/xscen/indicators.py @@ -134,7 +134,7 @@ def _infer_freq_from_meta(ind): ind.injected_parameters["freq"] if "freq" in ind.injected_parameters else ( - ind.parameters["freq"]["default"] + ind.parameters["freq"].default if "freq" in ind.parameters else ind.src_freq ) diff --git a/xscen/utils.py b/xscen/utils.py index 7fe938a1..c354e9ec 100644 --- a/xscen/utils.py +++ b/xscen/utils.py @@ -1402,3 +1402,17 @@ def ensure_new_xrfreq(freq: str) -> str: # noqa: C901 freq = freq.replace("U", "us") return freq + + +def _xarray_defaults(**kwargs): + """Translate from xscen's extract names to intake-esm names and put better defaults.""" + if "xr_open_kwargs" in kwargs: + kwargs["xarray_open_kwargs"] = kwargs.pop("xr_open_kwargs") + if "xr_combine_kwargs" in kwargs: + kwargs["xarray_combine_by_coords_kwargs"] = kwargs.pop("xr_combine_kwargs") + + kwargs.setdefault("xarray_open_kwargs", {}).setdefault("chunks", {}) + kwargs.setdefault("xarray_combine_by_coords_kwargs", {}).setdefault( + "data_vars", "minimal" + ) + return kwargs From dd77d5fcc23939534d0d7e5c340503cae0b24479 Mon Sep 17 00:00:00 2001 From: Pascal Bourgault Date: Tue, 7 May 2024 13:59:57 -0400 Subject: [PATCH 2/4] upd changes --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index 775b8688..3f30ad77 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -31,6 +31,7 @@ Internal changes * An `encoding` argument was added to ``xs.config.load_config``. (:pull:`370`). * Various small fixes to the code to address FutureWarnings. (:pull:`380`). * ``xs.spatial.subset`` will try to guess CF coordinate if it can't find "latitude" or "longitude" in ``ds.cf``. (:pull:`384`). +* ``xs.extract_dataset`` and ``xs.DataCatalog.to_dataset`` will now default to opening datasets with option ``chunks={}``, which tries to respect chunking on disk. (:pull:`398`, :issue:`368`). Bug fixes ^^^^^^^^^ From 718e88a237b967320d82eacf994618975bf1ce6f Mon Sep 17 00:00:00 2001 From: Pascal Bourgault Date: Tue, 7 May 2024 14:12:15 -0400 Subject: [PATCH 3/4] woups --- xscen/extract.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xscen/extract.py b/xscen/extract.py index 79b19cce..f5abc8ef 100644 --- a/xscen/extract.py +++ b/xscen/extract.py @@ -29,7 +29,7 @@ from .config import parse_config from .indicators import load_xclim_module, registry_from_module from .spatial import subset -from .utils import CV, _xarray_kwargs +from .utils import CV, _xarray_defaults from .utils import ensure_correct_time as _ensure_correct_time from .utils import get_cat_attrs, natural_sort, standardize_periods, xrfreq_to_timedelta @@ -161,7 +161,7 @@ def extract_dataset( # noqa: C901 } # Default arguments to send xarray - xr_kwargs = _xarray_kwargs( + xr_kwargs = _xarray_defaults( xr_open_kwargs=xr_open_kwargs or {}, xr_combine_kwargs=xr_combine_kwargs or {} ) From 7fb388b763eaaf9bc7e836e8fa1a0abfd818ab13 Mon Sep 17 00:00:00 2001 From: "bumpversion[bot]" Date: Tue, 7 May 2024 18:33:19 +0000 Subject: [PATCH 4/4] =?UTF-8?q?Bump=20version:=200.8.4-dev.14=20=E2=86=92?= =?UTF-8?q?=200.8.4-dev.15?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .cruft.json | 2 +- pyproject.toml | 2 +- xscen/__init__.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.cruft.json b/.cruft.json index a3206e4d..610708a6 100644 --- a/.cruft.json +++ b/.cruft.json @@ -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.8.4-dev.14", + "version": "0.8.4-dev.15", "use_pytest": "y", "use_black": "y", "use_conda": "y", diff --git a/pyproject.toml b/pyproject.toml index 0e0229a4..ae1800ea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -127,7 +127,7 @@ target-version = [ ] [tool.bumpversion] -current_version = "0.8.4-dev.14" +current_version = "0.8.4-dev.15" commit = true commit_args = "--no-verify" tag = false diff --git a/xscen/__init__.py b/xscen/__init__.py index 4cd288bd..0292a56f 100644 --- a/xscen/__init__.py +++ b/xscen/__init__.py @@ -51,7 +51,7 @@ __author__ = """Gabriel Rondeau-Genesse""" __email__ = "rondeau-genesse.gabriel@ouranos.ca" -__version__ = "0.8.4-dev.14" +__version__ = "0.8.4-dev.15" def warning_on_one_line(