Skip to content

Commit

Permalink
Updated docstring, pre-commit passed
Browse files Browse the repository at this point in the history
  • Loading branch information
vindelico committed Nov 19, 2023
1 parent a6e5440 commit 9e1497e
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 100 deletions.
147 changes: 103 additions & 44 deletions tests/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
class TestClimatologicalMean:
def _format(self, s):
import xclim
op_format = (dict.fromkeys(("mean", "std", "var", "sum"), "adj") |
dict.fromkeys(("max", "min"), "noun"))

op_format = dict.fromkeys(("mean", "std", "var", "sum"), "adj") | dict.fromkeys(
("max", "min"), "noun"
)
return xclim.core.formatting.default_formatter.format_field(s, op_format[s])

def test_daily(self):
Expand Down Expand Up @@ -566,8 +568,10 @@ def test_global(self, lonstart, method, exp):
class TestClimatologicalOp:
def _format(self, s):
import xclim
op_format = (dict.fromkeys(("mean", "std", "var", "sum"), "adj") |
dict.fromkeys(("max", "min"), "noun"))

op_format = dict.fromkeys(("mean", "std", "var", "sum"), "adj") | dict.fromkeys(
("max", "min"), "noun"
)
return xclim.core.formatting.default_formatter.format_field(s, op_format[s])

def test_daily(self):
Expand All @@ -579,10 +583,12 @@ def test_daily(self):
as_dataset=True,
)
with pytest.raises(NotImplementedError):
xs.climatological_op(ds, op='mean')
xs.climatological_op(ds, op="mean")

@pytest.mark.parametrize("xrfreq", ["MS", "AS-JAN"])
@pytest.mark.parametrize("op", ["max", "mean", "median", "min", "std", "sum", "var", "linregress"])
@pytest.mark.parametrize(
"op", ["max", "mean", "median", "min", "std", "sum", "var", "linregress"]
)
def test_all_default(self, xrfreq, op):
o = 12 if xrfreq == "MS" else 1

Expand All @@ -594,25 +600,33 @@ def test_all_default(self, xrfreq, op):
as_dataset=True,
)
out = xs.climatological_op(ds, op=op)
expected = (dict.fromkeys(("max", "mean", "median", "min"), np.arange(1, o + 1)) |
dict.fromkeys(("std", "var"), np.zeros(o)) |
dict({"sum": np.arange(1, o + 1) * 30}) |
dict({"linregress": np.array([
np.zeros(o),
np.arange(1, o + 1),
np.zeros(o),
np.ones(o),
np.zeros(o),
np.zeros(o)]).T})
)
expected = (
dict.fromkeys(("max", "mean", "median", "min"), np.arange(1, o + 1))
| dict.fromkeys(("std", "var"), np.zeros(o))
| dict({"sum": np.arange(1, o + 1) * 30})
| dict(
{
"linregress": np.array(
[
np.zeros(o),
np.arange(1, o + 1),
np.zeros(o),
np.ones(o),
np.zeros(o),
np.zeros(o),
]
).T
}
)
)
# Test output variable name, values, length, horizon
assert list(out.data_vars.keys()) == [f"tas_clim_{op}"]
np.testing.assert_array_equal(out[f"tas_clim_{op}"], expected[op])
assert len(out.time) == (o * len(np.unique(out.horizon.values)))
np.testing.assert_array_equal(out.time[0], ds.time[0])
assert (out.horizon == "2001-2030").all()
# Test metadata
operation = self._format(op) if op not in ['median', 'linregress'] else op
operation = self._format(op) if op not in ["median", "linregress"] else op
assert (
out[f"tas_clim_{op}"].attrs["description"]
== f"Climatological 30-year {operation} of {ds.tas.attrs['description']}"
Expand All @@ -624,7 +638,9 @@ def test_all_default(self, xrfreq, op):
assert out.attrs["cat:processing_level"] == "climatology"

@pytest.mark.parametrize("xrfreq", ["MS", "AS-JAN"])
@pytest.mark.parametrize("op", ['max', 'mean', 'median', 'min', 'std', 'sum', 'var', 'linregress'])
@pytest.mark.parametrize(
"op", ["max", "mean", "median", "min", "std", "sum", "var", "linregress"]
)
def test_options(self, xrfreq, op):
o = 12 if xrfreq == "MS" else 1

Expand All @@ -635,20 +651,42 @@ def test_options(self, xrfreq, op):
freq=xrfreq,
as_dataset=True,
)
out = xs.climatological_op(ds, op=op, window=15, stride=5, to_level="for_testing")
expected = (dict.fromkeys(("max", "mean", "median", "min"),
np.tile(np.arange(1, o + 1), len(np.unique(out.horizon.values)))) |
dict.fromkeys(("std", "var"),
np.tile(np.zeros(o), len(np.unique(out.horizon.values)))) |
dict({"sum": np.tile(np.arange(1, o + 1) * 15, len(np.unique(out.horizon.values)))}) |
dict({"linregress": np.tile(np.array([
np.zeros(o),
np.arange(1, o + 1),
np.zeros(o),
np.ones(o),
np.zeros(o),
np.zeros(o)]), len(np.unique(out.horizon.values))).T})
out = xs.climatological_op(
ds, op=op, window=15, stride=5, to_level="for_testing"
)
expected = (
dict.fromkeys(
("max", "mean", "median", "min"),
np.tile(np.arange(1, o + 1), len(np.unique(out.horizon.values))),
)
| dict.fromkeys(
("std", "var"), np.tile(np.zeros(o), len(np.unique(out.horizon.values)))
)
| dict(
{
"sum": np.tile(
np.arange(1, o + 1) * 15, len(np.unique(out.horizon.values))
)
}
)
| dict(
{
"linregress": np.tile(
np.array(
[
np.zeros(o),
np.arange(1, o + 1),
np.zeros(o),
np.ones(o),
np.zeros(o),
np.zeros(o),
]
),
len(np.unique(out.horizon.values)),
).T
}
)
)
# Test output values
np.testing.assert_array_equal(
out[f"tas_clim_{op}"],
Expand All @@ -660,7 +698,7 @@ def test_options(self, xrfreq, op):
out.horizon.values
)
# Test metadata
operation = self._format(op) if op not in ['median', 'linregress'] else op
operation = self._format(op) if op not in ["median", "linregress"] else op
assert (
out[f"tas_clim_{op}"].attrs["description"]
== f"Climatological 15-year {operation} of {ds.tas.attrs['description']}"
Expand All @@ -682,7 +720,7 @@ def test_minperiods(self, op):
)
ds = ds.where(ds["time"].dt.strftime("%Y-%m-%d") != "2030-12-01")

op = 'mean'
op = "mean"
out = xs.climatological_op(ds, op=op, window=30)
assert all(np.isreal(out[f"tas_clim_{op}"]))
assert len(out.time) == 4
Expand All @@ -693,7 +731,7 @@ def test_minperiods(self, op):
assert np.sum(np.isnan(out[f"tas_clim_{op}"])) == 1

# min_periods as float
out = xs.climatological_op(ds, op=op, window=30, min_periods=.5)
out = xs.climatological_op(ds, op=op, window=30, min_periods=0.5)
assert "minimum of 15 years of data" in out[f"tas_clim_{op}"].attrs["history"]
assert np.sum(np.isnan(out[f"tas_clim_{op}"])) == 0

Expand Down Expand Up @@ -721,7 +759,9 @@ def test_periods(self, op):
with pytest.raises(ValueError):
xs.climatological_op(ds, op=op)

out = xs.climatological_op(ds, op='mean', periods=[["2001", "2010"], ["2021", "2030"]])
out = xs.climatological_op(
ds, op="mean", periods=[["2001", "2010"], ["2021", "2030"]]
)
assert len(out.time) == 2
assert {"2001-2010", "2021-2030"}.issubset(out.horizon.values)

Expand All @@ -735,7 +775,7 @@ def test_calendars(self, cal):
as_dataset=True,
)

out = xs.climatological_op(ds.convert_calendar(cal, align_on="date"), op='mean')
out = xs.climatological_op(ds.convert_calendar(cal, align_on="date"), op="mean")
assert out.time.dt.calendar == cal

def test_periods_as_dim(self):
Expand All @@ -746,11 +786,30 @@ def test_periods_as_dim(self):
freq="MS",
as_dataset=True,
)
out = xs.climatological_op(ds, op="mean", window=10, stride=5, periods_as_dim=True)
out = xs.climatological_op(
ds, op="mean", window=10, stride=5, periods_as_dim=True
)
assert (out.tas_clim_mean.values == np.tile(np.arange(1, 13), (5, 1))).all()
assert out.dims == {'period': 5, 'month': 12}
assert out.time.dims == ('period', 'month')
assert (out.period.values == ['2001-2010', '2006-2015', '2011-2020', '2016-2025', '2021-2030']).all()
assert (out.month.values ==
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']).all()
assert out.dims == {"period": 5, "month": 12}
assert out.time.dims == ("period", "month")
assert (
out.period.values
== ["2001-2010", "2006-2015", "2011-2020", "2016-2025", "2021-2030"]
).all()
assert (
out.month.values
== [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
]
).all()
Loading

0 comments on commit 9e1497e

Please sign in to comment.