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

Conversation

JonKing93
Copy link
Contributor

Summary

This pull request makes NoData type checking compatible with numpy 2+

Details

Currently, most of the package fails upon a fresh install. This is because the Raster class uses numpy.can_cast to type check NoData values, but the function's API changed following the release of numpy 2.0.

As an example, try running the following code:

import numpy as np
from pysheds.sview import ViewFinder, Raster

values = np.arange(100, dtype='int8').reshape(10,10)
view = ViewFinder(shape=values.shape, nodata=0)
raster = Raster(values, view)

This should work fine, but if you are using numpy 2+, it instead results in the error:

Traceback (most recent call last):
  File "...\pysheds\sview.py", line 85, in __new__
    assert np.can_cast(viewfinder.nodata, obj.dtype, casting='safe')
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: can_cast() does not support Python ints, floats, and complex because the result used to depend on the value.
This change was part of adopting NEP 50, we may explicitly allow them again in the future.       

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "...\pysheds\sview.py", line 87, in __new__
    raise TypeError('`nodata` value not representable in dtype of array.')
TypeError: `nodata` value not representable in dtype of array.

This pull request addresses this issue by updating the NoData type checking logic such that it should be compatible with both numpy 1 and 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant