Skip to content

Commit

Permalink
Augment test_fit error tolerance, add more tests for coverage (#1402)
Browse files Browse the repository at this point in the history
### What kind of change does this PR introduce?

* Relaxes the error tolerance for `test_fit` so that we experience fewer
failures
* Adds a few tests for uncovered moving window functions

### Does this PR introduce a breaking change?

No.
  • Loading branch information
Zeitsperre committed Jun 28, 2023
2 parents 23c1aa7 + 8b5672d commit 0f2eb42
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
Changelog
=========

v0.45.0 (unreleased)
--------------------
Contributors to this version: Trevor James Smith (:user:`Zeitsperre`).

Internal changes
^^^^^^^^^^^^^^^^
* Tolerance thresholds for error in ``test_stats::test_fit`` have been relaxed to allow for more variation in the results. Previously untested ``*_moving_yearly_window`` functions are now tested. (:issue:`1400`, :pull:`1402`).

v0.44.0 (2023-06-23)
--------------------
Contributors to this version: Éric Dupuis (:user:`coxipi`), Trevor James Smith (:user:`Zeitsperre`), Pascal Bourgault (:user:`aulemahal`), Ludwig Lierhammer (:user:`ludwiglierhammer`), David Huard (:user:`huard`).
Expand Down
13 changes: 13 additions & 0 deletions tests/test_analog.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ def test_spatial_analogs(method, open_dataset):
np.testing.assert_allclose(diss[method], out, rtol=1e-3, atol=1e-3)


def test_unsupported_spatial_analog_method(open_dataset):
method = "KonMari"

data = open_dataset("SpatialAnalogs/indicators")
target = data.sel(lat=46.1875, lon=-72.1875, time=slice("1970", "1990"))
candidates = data.sel(time=slice("1970", "1990"))

match_statement = f"Method `KonMari` is not implemented. Available methods are: {','.join(xca.metrics.keys())}"

with pytest.raises(ValueError, match=match_statement):
xca.spatial_analogs(target, candidates, method=method)


def test_spatial_analogs_multi_index(open_dataset):
# Test multi-indexes
diss = open_dataset("SpatialAnalogs/dissimilarity")
Expand Down
44 changes: 44 additions & 0 deletions tests/test_sdba/test_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from xclim.sdba.base import Grouper
from xclim.sdba.processing import (
adapt_freq,
construct_moving_yearly_window,
escore,
from_additive_space,
jitter,
Expand All @@ -20,6 +21,7 @@
stack_variables,
standardize,
to_additive_space,
unpack_moving_yearly_window,
unstack_variables,
unstandardize,
)
Expand Down Expand Up @@ -280,3 +282,45 @@ def test_stack_variables(open_dataset):
ds1p = unstack_variables(da1)

xr.testing.assert_equal(ds1, ds1p)


@pytest.mark.parametrize(
"window,step,lengths",
[
(1, 1, 151),
(5, 5, 30),
(10, 10, 15),
(25, 25, 6),
(50, 50, 3),
(None, None, 131),
],
)
def test_construct_moving_yearly_window(open_dataset, window, step, lengths):
ds = open_dataset("sdba/CanESM2_1950-2100.nc")

calls = {k: v for k, v in dict(window=window, step=step).items() if v is not None}
da_windowed = construct_moving_yearly_window(ds.tasmax, **calls)

assert len(da_windowed) == lengths


def test_construct_moving_yearly_window_standard_calendar(tasmin_series):
tasmin = tasmin_series(np.zeros(365 * 30), start="1997-01-01", units="degC")

with pytest.raises(ValueError):
construct_moving_yearly_window(tasmin)


@pytest.mark.parametrize("append_ends", [True, False])
def test_unpack_moving_yearly_window(open_dataset, append_ends):
tasmax = open_dataset("sdba/ahccd_1950-2013.nc").tasmax

tasmax_windowed = construct_moving_yearly_window(tasmax)

tx_deconstructed = unpack_moving_yearly_window(
tasmax_windowed, append_ends=append_ends
)
if append_ends:
np.testing.assert_array_equal(tasmax, tx_deconstructed)
else:
assert len(tx_deconstructed.time) < len(tasmax.time)
2 changes: 1 addition & 1 deletion tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_fit(self, fitda):
# Test with MM
pm = stats.fit(fitda, "lognorm", method="MM")
mm, mv = lognorm(*pm.values).stats()
np.testing.assert_allclose(np.exp(2 + 1 / 2), mm, rtol=0.5)
np.testing.assert_allclose(np.exp(2 + 1 / 2), mm, rtol=0.65)


def test_weibull_min_fit(weibull_min):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,25 @@

import numpy as np
import pytest
from xarray import Dataset

import xclim.testing.utils as utilities
from xclim import __version__ as __xclim_version__
from xclim.testing.helpers import test_timeseries as timeseries


class TestFixtures:
def test_timeseries_made_up_variable(self):
ds = timeseries(
np.zeros(31),
"luminiferous_aether_flux",
units="W K mol A-1 m-2 s-1",
as_dataset=True,
)

assert isinstance(ds, Dataset)
assert ds.luminiferous_aether_flux.attrs["units"] == "W K mol A-1 m-2 s-1"
assert "standard_name" not in ds.luminiferous_aether_flux.attrs


class TestFileRequests:
Expand Down
6 changes: 3 additions & 3 deletions xclim/analog.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ def spatial_analogs(
)

try:
metric = metrics[method]
metric_func = metrics[method]
except KeyError as e:
raise ValueError(
f"Method {method} is not implemented. Available methods are : {','.join(metrics.keys())}."
f"Method `{method}` is not implemented. Available methods are: {','.join(metrics.keys())}."
) from e

if candidates.chunks is not None:
Expand All @@ -91,7 +91,7 @@ def spatial_analogs(

# Compute dissimilarity
diss = xr.apply_ufunc(
metric,
metric_func,
target,
candidates,
input_core_dims=[(dist_dim, "_indices"), ("_dist_dim", "_indices")],
Expand Down

0 comments on commit 0f2eb42

Please sign in to comment.