From 361afab056be266e3c42e7c6ef24c2413243562f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Dupuis?= Date: Wed, 29 May 2024 17:52:57 -0400 Subject: [PATCH 1/6] normalize option in taylordiagram --- CHANGES.rst | 6 +++++- xclim/sdba/measures.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 6eafde626..e4a8d2e6d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,11 @@ Changelog v0.50.0 (unreleased) -------------------- -Contributors to this version: Trevor James Smith (:user:`Zeitsperre`). +Contributors to this version: Trevor James Smith (:user:`Zeitsperre`), Éric Dupuis (:user:`coxipi`) + +New features and enhancements +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +* New option `normalize` in `sdba.measures.taylordiagram` to obtain normalized Taylor diagrams (divide standard deviations by standard deviation of the reference). Internal changes ^^^^^^^^^^^^^^^^ diff --git a/xclim/sdba/measures.py b/xclim/sdba/measures.py index e6957f434..4000acd79 100644 --- a/xclim/sdba/measures.py +++ b/xclim/sdba/measures.py @@ -451,6 +451,7 @@ def _taylordiagram( ref: xr.DataArray, dim: str = "time", group: str | Grouper = "time", + normalize: bool = False, ) -> xr.DataArray: """Taylor diagram. @@ -468,6 +469,9 @@ def _taylordiagram( group : str Compute the property and measure for each temporal groups individually. Currently not implemented. + normalize : bool + If `True`, Normalize the data by dividing by the standard deviation of the reference. + Default is `False`. Returns @@ -496,6 +500,13 @@ def _taylordiagram( } ) + # Normalize the standard deviations byt the standard deviation of the reference. + if normalize: + out[{"taylor_param": [0, 1]}] = ( + out[{"taylor_param": [0, 1]}] / out[{"taylor_param": 0}] + ) + out.attrs["normalized"] = True + return out From 1d57e1a111acb340817d5de3efb30168a5b3b833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Dupuis?= Date: Wed, 29 May 2024 17:58:08 -0400 Subject: [PATCH 2/6] add test for normalize option --- tests/test_sdba/test_measures.py | 8 ++++++++ xclim/sdba/measures.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_sdba/test_measures.py b/tests/test_sdba/test_measures.py index 2410c63e3..2235251d6 100644 --- a/tests/test_sdba/test_measures.py +++ b/tests/test_sdba/test_measures.py @@ -92,3 +92,11 @@ def test_taylordiagram(open_dataset): ) test = sdba.measures.taylordiagram(sim, ref).values np.testing.assert_array_almost_equal(test, [13.12244701, 6.76166582, 0.73230199], 4) + + # test normalization option + test_normalize = sdba.measures.taylordiagram(sim, ref, normalize=True).values + np.testing.assert_array_almost_equal( + test_normalize, + [13.12244701 / 13.12244701, 6.76166582 / 13.12244701, 0.73230199], + 4, + ) diff --git a/xclim/sdba/measures.py b/xclim/sdba/measures.py index 4000acd79..8457440d5 100644 --- a/xclim/sdba/measures.py +++ b/xclim/sdba/measures.py @@ -470,7 +470,7 @@ def _taylordiagram( Compute the property and measure for each temporal groups individually. Currently not implemented. normalize : bool - If `True`, Normalize the data by dividing by the standard deviation of the reference. + If `True`, normalize the data by dividing by the standard deviation of the reference. Default is `False`. From 6bce434da8f14a919a807128402230c92a90b101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Dupuis?= Date: Wed, 29 May 2024 18:02:04 -0400 Subject: [PATCH 3/6] add PR number --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index e4a8d2e6d..72790c84a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,7 +8,7 @@ Contributors to this version: Trevor James Smith (:user:`Zeitsperre`), Éric Dup New features and enhancements ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -* New option `normalize` in `sdba.measures.taylordiagram` to obtain normalized Taylor diagrams (divide standard deviations by standard deviation of the reference). +* New option `normalize` in `sdba.measures.taylordiagram` to obtain normalized Taylor diagrams (divide standard deviations by standard deviation of the reference). (:pull:`1764`). Internal changes ^^^^^^^^^^^^^^^^ From 302a1880e08be8d4c05411f0cace71084df75ff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Dupuis?= Date: Wed, 29 May 2024 18:03:22 -0400 Subject: [PATCH 4/6] better doc --- xclim/sdba/measures.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xclim/sdba/measures.py b/xclim/sdba/measures.py index 8457440d5..93d4860b1 100644 --- a/xclim/sdba/measures.py +++ b/xclim/sdba/measures.py @@ -470,7 +470,7 @@ def _taylordiagram( Compute the property and measure for each temporal groups individually. Currently not implemented. normalize : bool - If `True`, normalize the data by dividing by the standard deviation of the reference. + If `True`, divide the standard deviations by the standard deviation of the reference. Default is `False`. From 07e97455a39c1e6d0aff88f449b55430b5e2d5d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Dupuis?= Date: Thu, 30 May 2024 10:56:58 -0400 Subject: [PATCH 5/6] raise error if `ref_std=0` and `normalize=True` --- xclim/sdba/measures.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/xclim/sdba/measures.py b/xclim/sdba/measures.py index 93d4860b1..402c18257 100644 --- a/xclim/sdba/measures.py +++ b/xclim/sdba/measures.py @@ -502,6 +502,10 @@ def _taylordiagram( # Normalize the standard deviations byt the standard deviation of the reference. if normalize: + if (out[{"taylor_param": 0}] == 0).any(): + raise ValueError( + "`ref_std =0` (homogeneous field) obtained, normalization is not possible." + ) out[{"taylor_param": [0, 1]}] = ( out[{"taylor_param": [0, 1]}] / out[{"taylor_param": 0}] ) From df67f613c7a1c8b3ca7a3eddd98944aa628159a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Dupuis?= Date: Tue, 11 Jun 2024 11:39:35 -0400 Subject: [PATCH 6/6] keep attrs and set units to "" --- xclim/sdba/measures.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/xclim/sdba/measures.py b/xclim/sdba/measures.py index 402c18257..43432d5ee 100644 --- a/xclim/sdba/measures.py +++ b/xclim/sdba/measures.py @@ -506,10 +506,12 @@ def _taylordiagram( raise ValueError( "`ref_std =0` (homogeneous field) obtained, normalization is not possible." ) - out[{"taylor_param": [0, 1]}] = ( - out[{"taylor_param": [0, 1]}] / out[{"taylor_param": 0}] - ) + with xr.set_options(keep_attrs=True): + out[{"taylor_param": [0, 1]}] = ( + out[{"taylor_param": [0, 1]}] / out[{"taylor_param": 0}] + ) out.attrs["normalized"] = True + out.attrs["units"] = "" return out