From f346b80b3b429e7342b5cc842720e8d169d8723a Mon Sep 17 00:00:00 2001 From: saschahofmann Date: Thu, 18 Apr 2024 15:57:22 +0200 Subject: [PATCH 1/7] Add failing test for QDM with seasonal group --- tests/test_sdba/test_adjustment.py | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/test_sdba/test_adjustment.py b/tests/test_sdba/test_adjustment.py index 7543ae9d7..9776c6285 100644 --- a/tests/test_sdba/test_adjustment.py +++ b/tests/test_sdba/test_adjustment.py @@ -378,6 +378,37 @@ def test_mon_U( # Test predict np.testing.assert_allclose(p, ref.transpose(*p.dims), rtol=0.1, atol=0.2) + def test_seasonal(self, series, random): + u = random.random(10000) + kind = '+' + name = 'tas' + # Define distributions + xd = uniform(loc=1, scale=1) + yd = uniform(loc=2, scale=4) + + # Generate random numbers with u so we get exact results for comparison + x = xd.ppf(u) + y = yd.ppf(u) + + # Test train + hist = sim = series(x, name) + ref = series(y, name) + + QDM = QuantileDeltaMapping.train( + ref.astype("float32"), + hist.astype("float32"), + kind=kind, + group="time.season", + nquantiles=10, + ) + p = QDM.adjust(sim.astype("float32"), interp="linear") + + # Test predict + # Accept discrepancies near extremes + middle = (u > 1e-2) * (u < 0.99) + np.testing.assert_array_almost_equal(p[middle], ref[middle], 1) + + def test_cannon_and_diagnostics(self, cannon_2015_dist, cannon_2015_rvs): ref, hist, sim = cannon_2015_rvs(15000, random=False) From 8e81861fa789b746048c3e11165494bf8d87e504 Mon Sep 17 00:00:00 2001 From: saschahofmann Date: Thu, 18 Apr 2024 16:39:09 +0200 Subject: [PATCH 2/7] Map seasons to integers in interp_on_quanitles --- tests/test_sdba/test_adjustment.py | 5 ++--- xclim/sdba/base.py | 2 ++ xclim/sdba/utils.py | 12 +++++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/test_sdba/test_adjustment.py b/tests/test_sdba/test_adjustment.py index 9776c6285..3cdc86ff9 100644 --- a/tests/test_sdba/test_adjustment.py +++ b/tests/test_sdba/test_adjustment.py @@ -380,8 +380,8 @@ def test_mon_U( def test_seasonal(self, series, random): u = random.random(10000) - kind = '+' - name = 'tas' + kind = "+" + name = "tas" # Define distributions xd = uniform(loc=1, scale=1) yd = uniform(loc=2, scale=4) @@ -408,7 +408,6 @@ def test_seasonal(self, series, random): middle = (u > 1e-2) * (u < 0.99) np.testing.assert_array_almost_equal(p[middle], ref[middle], 1) - def test_cannon_and_diagnostics(self, cannon_2015_dist, cannon_2015_rvs): ref, hist, sim = cannon_2015_rvs(15000, random=False) diff --git a/xclim/sdba/base.py b/xclim/sdba/base.py index 0d40e716e..6590601ba 100644 --- a/xclim/sdba/base.py +++ b/xclim/sdba/base.py @@ -295,6 +295,8 @@ def get_index( ind = da.indexes[self.dim] if self.prop == "week": i = da[self.dim].copy(data=ind.isocalendar().week).astype(int) + elif self.prop == "season": + i = da[self.dim].copy(data=ind.month % 12 // 3) else: i = getattr(ind, self.prop) diff --git a/xclim/sdba/utils.py b/xclim/sdba/utils.py index c36cb247a..09a3129af 100644 --- a/xclim/sdba/utils.py +++ b/xclim/sdba/utils.py @@ -360,6 +360,11 @@ def _interp_on_quantiles_2D(newx, newg, oldx, oldy, oldg, method, extrap): # no return out +SEASON_MAP = {"DJF": 0, "MAM": 1, "JJA": 2, "SON": 3} + +map_season_to_int = np.vectorize(SEASON_MAP.get) + + @parse_group def interp_on_quantiles( newx: xr.DataArray, @@ -429,12 +434,17 @@ def interp_on_quantiles( ) return out - # else: if prop not in xq.dims: xq = xq.expand_dims({prop: group.get_coordinate()}) if prop not in yq.dims: yq = yq.expand_dims({prop: group.get_coordinate()}) + # Adding the cyclic bounds fails for string coordinates like seasons + # That's why we map the seasons to integers + if prop == "season": + xq = xq.assign_coords(season=map_season_to_int(xq.season)) + yq = yq.assign_coords(season=map_season_to_int(yq.season)) + xq = add_cyclic_bounds(xq, prop, cyclic_coords=False) yq = add_cyclic_bounds(yq, prop, cyclic_coords=False) newg = group.get_index(newx, interp=method != "nearest") From 6c840a282a0aaaeb19610449544920d178637a13 Mon Sep 17 00:00:00 2001 From: saschahofmann Date: Thu, 18 Apr 2024 16:47:10 +0200 Subject: [PATCH 3/7] Update CHANGE.rst --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index 5f953b2b7..817d8f0e8 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -17,6 +17,7 @@ Bug fixes * Fixed bug with loess smoothing for an array full of NaNs. (:pull:`1699`). * Fixed and adapted ``time_bnds`` to the newest xarray. (:pull:`1700`). * Fixed "agreement fraction" in ``robustness_fractions`` to distinguish between negative change and no change. Added "negative" and "changed negative" fractions (:issue:`1690`, :pull:`1711`). +* Fixed bug QuantileDeltaMapping adjustment not working for seasonal grouping (:issue:`1704`, :pull:`1716`). Internal changes ^^^^^^^^^^^^^^^^ From b50071c9027cccaa756f7a91e379c8f6a1176bfc Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 19 Apr 2024 12:19:47 +0000 Subject: [PATCH 4/7] Bump actions/upload-artifact from 4.3.1 to 4.3.2 Bumps [actions/upload-artifact](https://github.com/actions/upload-artifact) from 4.3.1 to 4.3.2. - [Release notes](https://github.com/actions/upload-artifact/releases) - [Commits](https://github.com/actions/upload-artifact/compare/5d5d22a31266ced268874388b861e4b58bb5c2f3...1746f4ab65b179e0ea60a494b83293b640dd5bba) --- updated-dependencies: - dependency-name: actions/upload-artifact dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- .github/workflows/scorecard.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml index 74ddb8c10..1cfd46e42 100644 --- a/.github/workflows/scorecard.yml +++ b/.github/workflows/scorecard.yml @@ -59,7 +59,7 @@ jobs: # Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF # format to the repository Actions tab. - name: "Upload artifact" - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 + uses: actions/upload-artifact@1746f4ab65b179e0ea60a494b83293b640dd5bba with: name: SARIF file path: results.sarif From c64a10ce021286122bd8b08ade3e064941fb069b Mon Sep 17 00:00:00 2001 From: "bumpversion[bot]" Date: Fri, 19 Apr 2024 15:00:37 +0000 Subject: [PATCH 5/7] =?UTF-8?q?Bump=20version:=200.48.3-dev.11=20=E2=86=92?= =?UTF-8?q?=200.48.3-dev.12?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 2 +- xclim/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2a8d70c08..d6ecfaf2c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -125,7 +125,7 @@ target-version = [ ] [tool.bumpversion] -current_version = "0.48.3-dev.11" +current_version = "0.48.3-dev.12" commit = true commit_args = "--no-verify" tag = false diff --git a/xclim/__init__.py b/xclim/__init__.py index f03cbb35e..49ebec6e5 100644 --- a/xclim/__init__.py +++ b/xclim/__init__.py @@ -16,7 +16,7 @@ __author__ = """Travis Logan""" __email__ = "logan.travis@ouranos.ca" -__version__ = "0.48.3-dev.11" +__version__ = "0.48.3-dev.12" _module_data = _files("xclim.data") From 9c131dacd53866cf3a563787538932a53c6d43a4 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 22 Apr 2024 12:53:47 +0000 Subject: [PATCH 6/7] Bump chuhlomin/render-template from 1.9 to 1.10 Bumps [chuhlomin/render-template](https://github.com/chuhlomin/render-template) from 1.9 to 1.10. - [Release notes](https://github.com/chuhlomin/render-template/releases) - [Commits](https://github.com/chuhlomin/render-template/compare/a473db625a96c98e519d188812dc22bcaf54ffba...807354a04d9300c9c2ac177c0aa41556c92b3f75) --- updated-dependencies: - dependency-name: chuhlomin/render-template dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- .github/workflows/publish-mastodon.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/publish-mastodon.yml b/.github/workflows/publish-mastodon.yml index 108cdbdd0..97e9f999b 100644 --- a/.github/workflows/publish-mastodon.yml +++ b/.github/workflows/publish-mastodon.yml @@ -68,7 +68,7 @@ jobs: - name: Prepare Message id: render_template - uses: chuhlomin/render-template@a473db625a96c98e519d188812dc22bcaf54ffba # v1.9 + uses: chuhlomin/render-template@807354a04d9300c9c2ac177c0aa41556c92b3f75 # v1.10 with: template: .github/publish-mastodon.template.md vars: | From 1da3a1e948d9eaad7fc52e55a0767720e5be2346 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 22 Apr 2024 13:44:40 +0000 Subject: [PATCH 7/7] Bump actions/checkout from 4.1.2 to 4.1.3 Bumps [actions/checkout](https://github.com/actions/checkout) from 4.1.2 to 4.1.3. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/9bb56186c3b09b4f86b1c65136769dd318469633...1d96c772d19495a3b5c517cd2bc0cb401ea0529f) --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- .github/workflows/bump-version.yml | 2 +- .github/workflows/cache-cleaner.yml | 2 +- .github/workflows/codeql-analysis.yml | 2 +- .github/workflows/dependency-review.yml | 2 +- .github/workflows/main.yml | 8 ++++---- .github/workflows/publish-mastodon.yml | 2 +- .github/workflows/publish-pypi.yml | 2 +- .github/workflows/scorecard.yml | 2 +- .github/workflows/tag-testpypi.yml | 2 +- .github/workflows/testdata-version.yml | 2 +- .github/workflows/upstream.yml | 2 +- 11 files changed, 14 insertions(+), 14 deletions(-) diff --git a/.github/workflows/bump-version.yml b/.github/workflows/bump-version.yml index f1f87e84c..e9e2e6cb4 100644 --- a/.github/workflows/bump-version.yml +++ b/.github/workflows/bump-version.yml @@ -43,7 +43,7 @@ jobs: files.pythonhosted.org:443 github.com:443 pypi.org:443 - - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + - uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3 with: persist-credentials: false - uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0 diff --git a/.github/workflows/cache-cleaner.yml b/.github/workflows/cache-cleaner.yml index 8c1432546..6319ac0bd 100644 --- a/.github/workflows/cache-cleaner.yml +++ b/.github/workflows/cache-cleaner.yml @@ -23,7 +23,7 @@ jobs: objects.githubusercontent.com:443 - name: Check out code - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3 - name: Cleanup run: | diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index a7f4a8f0d..2e984356a 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -49,7 +49,7 @@ jobs: pypi.org:443 uploads.github.com:443 - name: Checkout repository - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL uses: github/codeql-action/init@1245696032ecf7d39f87d54daa406e22ddf769a8 diff --git a/.github/workflows/dependency-review.yml b/.github/workflows/dependency-review.yml index 5ae49b592..5ebdd7f0b 100644 --- a/.github/workflows/dependency-review.yml +++ b/.github/workflows/dependency-review.yml @@ -26,7 +26,7 @@ jobs: github.com:443 - name: 'Checkout Repository' - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3 - name: 'Dependency Review' uses: actions/dependency-review-action@5bbc3ba658137598168acb2ab73b21c432dd411b diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 92b406b3d..eafc31b61 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -52,7 +52,7 @@ jobs: files.pythonhosted.org:443 github.com:443 pypi.org:443 - - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + - uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3 - name: Set up Python${{ matrix.python-version }} uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0 with: @@ -91,7 +91,7 @@ jobs: github.com:443 pypi.org:443 raw.githubusercontent.com:443 - - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + - uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3 - name: Set up Python${{ matrix.python-version }} uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0 with: @@ -169,7 +169,7 @@ jobs: ppa.launchpadcontent.net:443 pypi.org:443 raw.githubusercontent.com:443 - - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + - uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3 - name: Install Eigen3 if: contains(matrix.tox-env, 'sbck') run: | @@ -223,7 +223,7 @@ jobs: pypi.org:443 raw.githubusercontent.com:443 repo.anaconda.com:443 - - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + - uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3 - name: Setup Conda (Micromamba) with Python${{ matrix.python-version }} uses: mamba-org/setup-micromamba@422500192359a097648154e8db4e39bdb6c6eed7 # v1.8.1 with: diff --git a/.github/workflows/publish-mastodon.yml b/.github/workflows/publish-mastodon.yml index 97e9f999b..0a8fba551 100644 --- a/.github/workflows/publish-mastodon.yml +++ b/.github/workflows/publish-mastodon.yml @@ -35,7 +35,7 @@ jobs: github.com:443 - name: Checkout - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3 - name: Current Version if: ${{ !github.event.inputs.version-tag }} diff --git a/.github/workflows/publish-pypi.yml b/.github/workflows/publish-pypi.yml index 3a14bf29d..bfd917fd6 100644 --- a/.github/workflows/publish-pypi.yml +++ b/.github/workflows/publish-pypi.yml @@ -27,7 +27,7 @@ jobs: github.com:443 pypi.org:443 upload.pypi.org:443 - - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + - uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3 - name: Set up Python3 uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0 with: diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml index 1cfd46e42..91b24214d 100644 --- a/.github/workflows/scorecard.yml +++ b/.github/workflows/scorecard.yml @@ -35,7 +35,7 @@ jobs: egress-policy: audit - name: "Checkout code" - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 + uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f with: persist-credentials: false diff --git a/.github/workflows/tag-testpypi.yml b/.github/workflows/tag-testpypi.yml index b679cf7d6..6840367e2 100644 --- a/.github/workflows/tag-testpypi.yml +++ b/.github/workflows/tag-testpypi.yml @@ -27,7 +27,7 @@ jobs: github.com:443 pypi.org:443 test.pypi.org:443 - - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + - uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3 - name: Set up Python3 uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0 with: diff --git a/.github/workflows/testdata-version.yml b/.github/workflows/testdata-version.yml index 60452f569..0bd4c1186 100644 --- a/.github/workflows/testdata-version.yml +++ b/.github/workflows/testdata-version.yml @@ -30,7 +30,7 @@ jobs: allowed-endpoints: > api.github.com:443 github.com:443 - - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + - uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3 - name: Find xclim-testdata Tag and CI Testing Branch run: | XCLIM_TESTDATA_TAG="$( \ diff --git a/.github/workflows/upstream.yml b/.github/workflows/upstream.yml index 96b086ae4..4ebdb7e80 100644 --- a/.github/workflows/upstream.yml +++ b/.github/workflows/upstream.yml @@ -54,7 +54,7 @@ jobs: pypi.org:443 raw.githubusercontent.com:443 repo.anaconda.com:443 - - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + - uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3 with: fetch-depth: 0 # Fetch all history for all branches and tags. - name: Setup Conda (Micromamba) with Python${{ matrix.python-version }}