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

Add missing formatting words and operations for generic stats #1672

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions tests/test_generic_indicators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import numpy as np
import pytest

from xclim import generic, set_options

Expand Down Expand Up @@ -84,10 +85,14 @@ def test_empty(self, ndq_series):
class TestStats:
"""See other tests in test_land::TestStats"""

def test_simple(self, pr_series, random):
@pytest.mark.parametrize(
"op,word",
[("min", "Minimum"), ("integral", "Integral"), ("doymin", "Day of minimum")],
)
def test_simple(self, pr_series, random, op, word):
pr = pr_series(random.random(400))
out = generic.stats(pr, freq="YS", op="min", season="MAM")
assert out.units == pr.units
out = generic.stats(pr, freq="YS", op=op)
assert out.long_name == f"{word} of variable"

def test_ndq(self, ndq_series):
out = generic.stats(ndq_series, freq="YS", op="min", season="MAM")
Expand Down
6 changes: 5 additions & 1 deletion xclim/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ def _match_value(self, value):
"m10": ["october"],
"m11": ["november"],
"m12": ["december"],
# Arguments to "op / reducer / stat"
# Arguments to "op / reducer / stat" (for example for generic.stats)
"integral": ["integrated", "integral"],
"count": ["count"],
"doymin": ["day of minimum"],
"doymax": ["day of maximum"],
"mean": ["average"],
"max": ["maximal", "maximum"],
"min": ["minimal", "minimum"],
Expand Down
16 changes: 16 additions & 0 deletions xclim/data/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@
"m12": [
"decembre"
],
"integral": [
"intégré",
"intégrée",
"intégrés",
"intégrées",
"integrale"
aulemahal marked this conversation as resolved.
Show resolved Hide resolved
],
"count": [
"décompte"
],
"doymin": [
"jour du minimum"
],
"doymax": [
"jour du maximum"
],
"mean": [
"moyen",
"moyenne",
Expand Down
5 changes: 5 additions & 0 deletions xclim/indices/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def select_resample_op(
"""
da = select_time(da, **indexer)
r = da.resample(time=freq)
if op in _xclim_ops:
op = _xclim_ops[op]
if isinstance(op, str):
out = getattr(r, op.replace("integral", "sum"))(dim="time", keep_attrs=True)
else:
Expand Down Expand Up @@ -164,6 +166,9 @@ def doymin(da: xr.DataArray) -> xr.DataArray:
return to_agg_units(out, da, "doymin")


_xclim_ops = {"doymin": doymin, "doymax": doymax}


def default_freq(**indexer) -> str:
"""Return the default frequency."""
freq = "YS-JAN"
Expand Down
Loading