mdbartos / pysheds

:earth_americas: Simple and fast watershed delineation in python.
GNU General Public License v3.0
702 stars 188 forks source link

Type checking of nodata values #228

Closed JonKing93 closed 7 months ago

JonKing93 commented 10 months ago

This pull request replaces the call to np.min_scalar_type with np.can_cast in the nodata check that occurs when creating a Raster or MultiRaster.

Currently, the use of np.min_scalar_type causes this check to fail unexpectedly in a few cases. For example, try creating an int8 raster with a nodata value of 0:

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)

Even though 0 is representable in int8, this code results in the following error:

TypeError: `nodata` value not representable in dtype of array.

This occurs because np.min_scalar_type(0) returns uint8, which is not <= int8. Replacing this with np.can_cast instead checks that the nodata value is representable in the raster's dtype, which I believe is the intended behavior.

groutr commented 7 months ago

ping @mdbartos Could this PR be reviewed and merged?

mdbartos commented 7 months ago

I think I got held up on this because the automated tests didn't run. I'll run them locally and then if they pass, pull in.