Skip to content

Commit

Permalink
Normalized taylordiagram (#1764)
Browse files Browse the repository at this point in the history
### What kind of change does this PR introduce?

* Add a `bool` option `normalize` and divide `ref_std, sim_std` by
`ref_std` if True

### Does this PR introduce a breaking change?

No.

### Other information:

I was planning a new plot option in `figanos`, but I was told we don't
want to do any computation in `figanos`, it would be handy that `sdba`
can manage this by itself.
  • Loading branch information
Zeitsperre authored Jun 11, 2024
2 parents 64bacb6 + 0353bdc commit d291681
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
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
out.attrs["units"] = ""

return out


Expand Down

0 comments on commit d291681

Please sign in to comment.