-
Notifications
You must be signed in to change notification settings - Fork 115
Closed
Description
Using the Reader's .read method on a geotiff gives back ImageData. I expected to be able to run np.nansum on it, but I get ValueError: The truth value of an array with more than one element is ambiguous. .
Running np.sum works fine.
Reproducing script:
$ uv run --with numpy,rio-tiler python demo-rio-tiler-error.py# demo-rio-tiler-error.py
import rasterio
import numpy as np
from rasterio.transform import Affine
from rio_tiler.io import Reader
# Create a 32x32 numpy array with some data
data = np.ones((32, 32), dtype=np.float32)
# Add some random values
data = data * np.random.rand(32, 32) * 100
# Add some NaN values in a pattern
data[5:10, 5:10] = np.nan # Small square of NaNs
# Define the transformation (here using a simple identity transform)
transform = Affine(1.0, 0.0, 0.0,
0.0, 1.0, 0.0)
# Create a new GeoTIFF file
output_path = "test_with_nans.tiff"
# Open a new file for writing
with rasterio.open(
output_path,
'w',
driver='GTiff',
height=data.shape[0],
width=data.shape[1],
count=1,
dtype=data.dtype,
crs="epsg:4326",
transform=transform,
nodata=np.nan
) as dst:
dst.write(data, 1)
with Reader(output_path) as src:
data = src.read(1)
np.nansum(data)Traceback
File ~/.cache/uv/archive-v0/0bGl3mVbNZ8ByG2ZVow-P/lib/python3.13/site-packages/numpy/lib/_nanfunctions_impl.py:726, in nansum(a, axis, dtype, out, keepdims, initial, where)
634 @array_function_dispatch(_nansum_dispatcher)
635 def nansum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue,
636 initial=np._NoValue, where=np._NoValue):
637 """
638 Return the sum of array elements over a given axis treating Not a
639 Numbers (NaNs) as zero.
(...) 724
725 """
--> 726 a, mask = _replace_nan(a, 0)
727 return np.sum(a, axis=axis, dtype=dtype, out=out, keepdims=keepdims,
728 initial=initial, where=where)
File ~/.cache/uv/archive-v0/0bGl3mVbNZ8ByG2ZVow-P/lib/python3.13/site-packages/numpy/lib/_nanfunctions_impl.py:102, in _replace_nan(a, val)
98 a = np.asanyarray(a)
100 if a.dtype == np.object_:
101 # object arrays do not support `isnan` (gh-9009), so make a guess
--> 102 mask = np.not_equal(a, a, dtype=bool)
103 elif issubclass(a.dtype.type, np.inexact):
104 mask = np.isnan(a)
File ~/.cache/uv/archive-v0/0bGl3mVbNZ8ByG2ZVow-P/lib/python3.13/site-packages/attr/_make.py:1647, in __ne__(self, other)
1642 def __ne__(self, other):
1643 """
1644 Check equality and either forward a NotImplemented or
1645 return the result negated.
1646 """
-> 1647 result = self.__eq__(other)
1648 if result is NotImplemented:
1649 return NotImplemented
File <attrs generated methods rio_tiler.models.ImageData>:20, in __eq__(self, other)
17 if other.__class__ is not self.__class__:
18 return NotImplemented
19 return (
---> 20 self.array == other.array and
21 self.assets == other.assets and
22 self.bounds == other.bounds and
23 self.crs == other.crs and
24 self.metadata == other.metadata and
25 self.band_names == other.band_names and
26 self.dataset_statistics == other.dataset_statistics and
27 self.cutline_mask == other.cutline_mask
28 )
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels