Skip to content

Commit

Permalink
fix nodata type checking following numpy 2+
Browse files Browse the repository at this point in the history
  • Loading branch information
JonKing93 authored Nov 15, 2024
1 parent 4030e60 commit d7e999b
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions pysheds/sview.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ def __new__(cls, input_array, viewfinder=None, metadata={}):
assert not np.issubdtype(obj.dtype, np.flexible)
except:
raise TypeError('`object` and `flexible` dtypes not allowed.')
try:
assert np.can_cast(viewfinder.nodata, obj.dtype, casting='safe')
except:
raise TypeError('`nodata` value not representable in dtype of array.')
cls._validate_nodata(viewfinder.nodata, obj.dtype)

# Don't allow original viewfinder and metadata to be modified
viewfinder = viewfinder.copy()
metadata = metadata.copy()
Expand Down Expand Up @@ -117,6 +115,16 @@ def _handle_raster_input(cls, input_array, viewfinder, metadata):
new_metadata=metadata)
return input_array, viewfinder, metadata

@staticmethod
def _validate_nodata(nodata, dtype):
"Checks the NoData value is preserved when cast to the raster dtype"
nodata = np.array(nodata)
casted = nodata.astype(dtype, casting='unsafe')
try:
assert (nodata == casted) or np.can_cast(nodata, dtype, casting='safe')
except:
raise TypeError('`nodata` value not representable in dtype of array.')

@property
def viewfinder(self):
return self._viewfinder
Expand Down Expand Up @@ -287,10 +295,7 @@ def __new__(cls, input_array, viewfinder=None, metadata={}):
assert not np.issubdtype(obj.dtype, np.flexible)
except:
raise TypeError('`object` and `flexible` dtypes not allowed.')
try:
assert np.can_cast(viewfinder.nodata, obj.dtype, casting='safe')
except:
raise TypeError('`nodata` value not representable in dtype of array.')
cls._validate_nodata(viewfinder.nodata, obj.dtype)
# Don't allow original viewfinder and metadata to be modified
viewfinder = viewfinder.copy()
metadata = metadata.copy()
Expand Down

0 comments on commit d7e999b

Please sign in to comment.