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

Normalized taylordiagram #1764

Merged
merged 12 commits into from
Jun 11, 2024
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Contributors to this version: Trevor James Smith (:user:`Zeitsperre`), Éric Dup
New features and enhancements
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* New properties: Bivariate Spell Length (``xclim.sdba.properties.bivariate_spell_length``), generalized spell lengths with an argument for `window`, and specific spell lengths with `window` fixed to 1 (``xclim.sdba.propertiies.threshold_count``, ``xclim.sdba.propertiies.bivariate_threshold_count``). (:pull:`1758`).
* New option `normalize` in ``sdba.measures.taylordiagram`` to obtain normalized Taylor diagrams (divide standard deviations by standard deviation of the reference). (:pull:`1764`).

Breaking changes
^^^^^^^^^^^^^^^^
Expand Down
8 changes: 8 additions & 0 deletions tests/test_sdba/test_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
17 changes: 17 additions & 0 deletions xclim/sdba/measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ def _taylordiagram(
ref: xr.DataArray,
dim: str = "time",
group: str | Grouper = "time",
normalize: bool = False,
) -> xr.DataArray:
"""Taylor diagram.

Expand All @@ -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`, divide the standard deviations by the standard deviation of the reference.
Default is `False`.


Returns
Expand Down Expand Up @@ -496,6 +500,19 @@ 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."
)
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
coxipi marked this conversation as resolved.
Show resolved Hide resolved
out.attrs["units"] = ""

return out


Expand Down