Skip to content

Commit

Permalink
Compatibility with Numpy 2.0 (#373)
Browse files Browse the repository at this point in the history
* NaN -> nan

* upd changes
  • Loading branch information
aulemahal committed Jun 26, 2024
1 parent e4b7362 commit 8f28424
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
What's new
==========

0.8.6 (unreleased)
------------------
* Compatibility with Numpy 2.0 (NaN vs nan) (:pull:`373`) By `Pascal Bourgault <https://github.com/aulemahal>`_.

0.8.5 (2024-04-11)
------------------
* Reverted to the chunking behaviour of xESMF 0.7 for cases where the spatial dimensions are not chunked on the source data. (:pull:`348`) By `Pascal Bourgault <https://github.com/aulemahal>`_.
Expand Down
12 changes: 6 additions & 6 deletions xesmf/smm.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ def apply_weights(weights, indata, shape_in, shape_out):


def add_nans_to_weights(weights):
"""Add NaN in empty rows of the regridding weights sparse matrix.
"""Add nan in empty rows of the regridding weights sparse matrix.
By default, empty rows in the weights sparse matrix are interpreted as zeroes. This can become problematic
when the field being interpreted has legitimate null values. This function inserts NaN values in each row to
make sure empty weights are propagated as NaNs instead of zeros.
when the field being interpreted has legitimate null values. This function inserts nan values in each row to
make sure empty weights are propagated as nans instead of zeros.
Parameters
----------
Expand All @@ -222,11 +222,11 @@ def add_nans_to_weights(weights):
# Taken from @trondkr and adapted by @raphaeldussin to use `lil`.
# lil matrix is better than CSR when changing sparsity
m = weights.data.to_scipy_sparse().tolil()
# replace empty rows by one NaN value at element 0 (arbitrary)
# so that remapped element become NaN instead of zero
# replace empty rows by one nan value at element 0 (arbitrary)
# so that remapped element become nan instead of zero
for krow in range(len(m.rows)):
m.rows[krow] = [0] if m.rows[krow] == [] else m.rows[krow]
m.data[krow] = [np.NaN] if m.data[krow] == [] else m.data[krow]
m.data[krow] = [np.nan] if m.data[krow] == [] else m.data[krow]
# update regridder weights (in COO)
weights = weights.copy(data=sps.COO.from_scipy_sparse(m))
return weights
Expand Down

0 comments on commit 8f28424

Please sign in to comment.