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

Handle exceptionally rare linear algebra error #2015

Merged
merged 2 commits into from
Dec 10, 2024
Merged

Conversation

Zeitsperre
Copy link
Collaborator

@Zeitsperre Zeitsperre commented Dec 10, 2024

Pull Request Checklist:

  • This PR addresses an already opened issue (for bug fixes / features)
    • This PR fixes #xyz
  • Tests for the changes have been added (for bug fixes / features)
    • (If applicable) Documentation has been added / updated (for bug fixes / features)
  • CHANGELOG.rst has been updated (with summary of main changes)
    • Link to issue (:issue:number) and pull request (:pull:number) has been added

What kind of change does this PR introduce?

  • Adds handling for a rare but mathematically possible error that has happened at least once in xclim

Does this PR introduce a breaking change?

No.

Other information:

  _______________________ test_loess_smoothing_nan[False] ________________________
  [gw2] darwin -- Python 3.11.9 /Users/runner/work/xclim/xclim/.tox/py311-coverage-extras/bin/python
  
  use_dask = False
  
      @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()
  
  tests/test_sdba/test_loess.py:87: 
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
  .tox/py311-coverage-extras/lib/python3.11/site-packages/xarray/core/dataarray.py:814: in values
      return self.variable.values
  .tox/py311-coverage-extras/lib/python3.11/site-packages/xarray/core/variable.py:566: in values
      return _as_array_or_item(self._data)
  .tox/py311-coverage-extras/lib/python3.11/site-packages/xarray/core/variable.py:363: in _as_array_or_item
      data = np.asarray(data)
  .tox/py311-coverage-extras/lib/python3.11/site-packages/dask/array/core.py:1709: in __array__
      x = self.compute()
  .tox/py311-coverage-extras/lib/python3.11/site-packages/dask/base.py:372: in compute
      (result,) = compute(self, traverse=False, **kwargs)
  .tox/py311-coverage-extras/lib/python3.11/site-packages/dask/base.py:660: in compute
      results = schedule(dsk, keys, **kwargs)
  .tox/py311-coverage-extras/lib/python3.11/site-packages/numpy/lib/_function_base_impl.py:2397: in __call__
      return self._call_as_normal(*args, **kwargs)
  .tox/py311-coverage-extras/lib/python3.11/site-packages/numpy/lib/_function_base_impl.py:2390: in _call_as_normal
      return self._vectorize_call(func=func, args=vargs)
  .tox/py311-coverage-extras/lib/python3.11/site-packages/numpy/lib/_function_base_impl.py:2471: in _vectorize_call
      res = self._vectorize_call_with_signature(func, args)
  .tox/py311-coverage-extras/lib/python3.11/site-packages/numpy/lib/_function_base_impl.py:2511: in _vectorize_call_with_signature
      results = func(*(arg[index] for arg in args))
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
  
  >   raise np.linalg.LinAlgError(
          "Matrix is singular to machine precision.")
  E   numpy.linalg.LinAlgError: Matrix is singular to machine precision.
  
  .tox/py311-coverage-extras/lib/python3.11/site-packages/numba/np/linalg.py:899: LinAlgError

@Zeitsperre Zeitsperre added this to the v0.54.0 milestone Dec 10, 2024
@Zeitsperre Zeitsperre self-assigned this Dec 10, 2024
@github-actions github-actions bot added the sdba Issues concerning the sdba submodule. label Dec 10, 2024
Copy link
Contributor

@SarahG-579462 SarahG-579462 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol. LGTM

@github-actions github-actions bot added the approved Approved for additional tests label Dec 10, 2024
@Zeitsperre Zeitsperre added the priority Immediate priority label Dec 10, 2024
@Zeitsperre Zeitsperre changed the title handle exceptionally rare linear algebra error Handle exceptionally rare linear algebra error Dec 10, 2024
@Zeitsperre Zeitsperre merged commit c1bde75 into main Dec 10, 2024
21 checks passed
@Zeitsperre Zeitsperre deleted the winner-winner branch December 10, 2024 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Approved for additional tests priority Immediate priority sdba Issues concerning the sdba submodule.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants