Skip to content

Commit

Permalink
handle all nan case for loess smoothing (#1699)
Browse files Browse the repository at this point in the history
<!--Please ensure the PR fulfills the following requirements! -->
<!-- If this is your first PR, make sure to add your details to the
AUTHORS.rst! -->
### Pull Request Checklist:
- [ ] This PR addresses an already opened issue (for bug fixes /
features)
    - This PR fixes #xyz
- [x] Tests for the changes have been added (for bug fixes / features)
- [ ] (If applicable) Documentation has been added / updated (for bug
fixes / features)
- [x] CHANGES.rst has been updated (with summary of main changes)
- [x] Link to issue (:issue:`number`) and pull request (:pull:`number`)
has been added

### What kind of change does this PR introduce?

* Trying to fix https://stackoverflowteams.com/c/ouranos/questions/103
* When the axis is only nan and skipna=True, don't even try to calculate
a loess (because it fails), just return nan.

### Does this PR introduce a breaking change?
before the all nan case would fail.

### Other information:
  • Loading branch information
juliettelavoie committed Apr 5, 2024
2 parents c6c10cb + db819b3 commit 534b7c8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changelog

v0.49.0 (unreleased)
--------------------
Contributors to this version: Trevor James Smith (:user:`Zeitsperre`), Pascal Bourgault (:user:`aulemahal`).
Contributors to this version: Trevor James Smith (:user:`Zeitsperre`), Pascal Bourgault (:user:`aulemahal`), Juliette Lavoie (:user:`juliettelavoie`).

Announcements
^^^^^^^^^^^^^
Expand All @@ -14,6 +14,7 @@ Bug fixes
^^^^^^^^^
* Fixed an bug in sdba's ``map_groups`` that prevented passing DataArrays with cftime coordinates if the ``sdba_encode_cf`` option was True. (:issue:`1673`, :pull:`1674`).
* Fixed bug (:issue:`1678`, :pull:`1679`) in sdba where a loaded training dataset could not be used for adjustment
* Fixed bug with loess smoothing for an array full of NaNs. (:pull:`1699`).
* Fixed and adapted ``time_bnds`` to the newest xarray. (:pull:`1700`)

Internal changes
Expand Down
21 changes: 21 additions & 0 deletions tests/test_sdba/test_loess.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import numpy as np
import pandas as pd
import pytest
import xarray as xr

from xclim.sdba.loess import _constant_regression # noqa
from xclim.sdba.loess import _gaussian_weighting # noqa
Expand Down Expand Up @@ -62,3 +64,22 @@ def test_loess_smoothing(use_dask, open_dataset):
# Same but we force not to use the optimization
tasmooth3 = loess_smoothing(tas, f=0.1, equal_spacing=False)
np.testing.assert_allclose(tasmooth, tasmooth3, rtol=1e-3, atol=1e-3)


@pytest.mark.slow
@pytest.mark.parametrize("use_dask", [True, False])
def test_loess_smoothing_nan(use_dask):
# create data with one axis full of nan
data = np.random.randn(2, 2, 10)
data[0, 0] = [np.nan] * 10
da = xr.DataArray(
data,
dims=["scenario", "model", "time"],
coords={"time": pd.date_range("2000-01-01", periods=10, freq="YS")},
).chunk({"time": -1})

out = loess_smoothing(da)

assert out.dims == da.dims
# check that the output is all nan on the axis with nan in the input
assert np.isnan(out.values[0, 0]).all()
2 changes: 2 additions & 0 deletions xclim/sdba/loess.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def _loess_nb(
out = np.full(x.size, np.NaN)
y = y[~nan]
x = x[~nan]
if x.size == 0:
return out

n = x.size
yest = np.zeros(n)
Expand Down

0 comments on commit 534b7c8

Please sign in to comment.