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

fix nodata type checking following numpy 2+ #261

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
Loading