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

Proper fix for loess linalg error - non-random tests #2020

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Changes from all 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
31 changes: 19 additions & 12 deletions tests/test_sdba/test_loess.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# ruff: noqa: E241
from __future__ import annotations

import logging

import numpy as np
import pandas as pd
import pytest
Expand All @@ -13,7 +12,7 @@
_linear_regression, # noqa
_loess_nb, # noqa
_tricube_weighting, # noqa
loess_smoothing,
loess_smoothing, # noqa
)


Expand Down Expand Up @@ -74,7 +73,22 @@ def test_loess_smoothing(use_dask, open_dataset):
@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)
# (random array taken from np.random.randn)
# fmt: off
data = np.array(
[
-0.993, -0.980, -0.452, -0.076, 0.447,
0.389, 2.408, 0.966, -0.793, 0.090,
-0.173, 1.713, -1.579, 0.454, -0.272,
-0.005, -0.926, -2.022, -1.661, -0.493,
-0.643, 0.891, 0.194, 0.086, 0.983,
-1.048, 2.032, 1.174, -0.441, -0.204,
-1.126, 0.933, 1.987, 0.638, 0.789,
0.767, 0.676, -1.028, 1.422, 0.453,
]
)
# fmt: on
data = data.reshape(2, 2, 10) # pylint: disable=too-many-function-args
data[0, 0] = [np.nan] * 10
da = xr.DataArray(
data,
Expand All @@ -86,11 +100,4 @@ def test_loess_smoothing_nan(use_dask):

assert out.dims == da.dims
# check that the output is all nan on the axis with nan in the input
try:
assert np.isnan(out.values[0, 0]).all()
except np.linalg.LinAlgError:
msg = (
"This has roughly a 1/50,000,000 chance of occurring. Buy a lottery ticket!"
)
logging.error(msg)
pass
assert np.isnan(out.values[0, 0]).all()
Loading