Skip to content

Commit

Permalink
Fix numpy 1.24 warnings (#1218)
Browse files Browse the repository at this point in the history
* Avoid numpy warning when converting float array containing NaNs to int

* Avoid numpy warning about all-nan slice

* Fix raster fill_type to be integer if dtype is integer
  • Loading branch information
ianthomas23 authored May 22, 2023
1 parent 7a97e97 commit 4823087
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions datashader/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,8 @@ def raster(self,
mask = array==nan_value
array = np.ma.masked_array(array, mask=mask, fill_value=nan_value)
fill_value = nan_value
elif np.issubdtype(source.dtype, np.integer):
fill_value = 0
else:
fill_value = np.NaN

Expand Down
6 changes: 5 additions & 1 deletion datashader/reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ def apply(self, df):
values = df[self.column].to_numpy()
nan_values = np.isnan(values)

index = ((values - self.bin0) / self.binsize).astype(int)
index_float = (values - self.bin0) / self.binsize
# NaN values are corrected below, so set them to zero to avoid warnings when
# converting from float to int.
index_float[nan_values] = 0
index = index_float.astype(int)
index[index < 0] = self.bin_under
index[index >= self.nbins] = self.bin_over
index[nan_values] = self.nbins
Expand Down
4 changes: 4 additions & 0 deletions datashader/tests/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
except ImportError:
rioxarray = None

from dask.context import config

config.set(scheduler='synchronous')

open_rasterio_available = pytest.mark.skipif(rioxarray is None and rasterio is None, reason="requires rioxarray or rasterio")

from os import path
Expand Down
20 changes: 12 additions & 8 deletions datashader/transfer_functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ def _interpolate(agg, cmap, how, alpha, span, min_alpha, name, rescale_discrete_
if isinstance(cmap, list):
rspan, gspan, bspan = np.array(list(zip(*map(rgb, cmap))))
span = np.linspace(span[0], span[1], len(cmap))
r = interp(data, span, rspan, left=255).astype(np.uint8)
g = interp(data, span, gspan, left=255).astype(np.uint8)
b = interp(data, span, bspan, left=255).astype(np.uint8)
r = np.nan_to_num(interp(data, span, rspan, left=255), copy=False).astype(np.uint8)
g = np.nan_to_num(interp(data, span, gspan, left=255), copy=False).astype(np.uint8)
b = np.nan_to_num(interp(data, span, bspan, left=255), copy=False).astype(np.uint8)
a = np.where(np.isnan(data), 0, alpha).astype(np.uint8)
rgba = np.dstack([r, g, b, a])
elif isinstance(cmap, str) or isinstance(cmap, tuple):
Expand All @@ -319,7 +319,7 @@ def _interpolate(agg, cmap, how, alpha, span, min_alpha, name, rescale_discrete_
r = np.full(data.shape, color[0], dtype=np.uint8)
g = np.full(data.shape, color[1], dtype=np.uint8)
b = np.full(data.shape, color[2], dtype=np.uint8)
a = interp(data, span, aspan, left=0, right=255).astype(np.uint8)
a = np.nan_to_num(interp(data, span, aspan, left=0, right=255), copy=False).astype(np.uint8)
rgba = np.dstack([r, g, b, a])
elif callable(cmap):
# Assume callable is matplotlib colormap
Expand Down Expand Up @@ -379,7 +379,9 @@ def _colorize(agg, color_key, how, alpha, span, min_alpha, name, color_baseline,
color_data = data.copy()

# subtract color_baseline if needed
baseline = np.nanmin(color_data) if color_baseline is None else color_baseline
with warnings.catch_warnings():
warnings.filterwarnings('ignore', r'All-NaN slice encountered')
baseline = np.nanmin(color_data) if color_baseline is None else color_baseline
with np.errstate(invalid='ignore'):
if baseline > 0:
color_data -= baseline
Expand Down Expand Up @@ -444,7 +446,9 @@ def _interpolate_alpha(data, total, mask, how, alpha, span, min_alpha, rescale_d
# if span is provided, use it, otherwise produce a span based off the
# min/max of the data
if span is None:
offset = np.nanmin(total)
with warnings.catch_warnings():
warnings.filterwarnings('ignore', r'All-NaN slice encountered')
offset = np.nanmin(total)
if total.dtype.kind == 'u' and offset == 0:
mask = mask | (total == 0)
# If at least one element is not masked, use the minimum as the offset
Expand Down Expand Up @@ -494,8 +498,8 @@ def _interpolate_alpha(data, total, mask, how, alpha, span, min_alpha, rescale_d
norm_span = array_module.hstack(norm_span)

# Interpolate the alpha values
a = interp(a_scaled, norm_span, array_module.array([min_alpha, alpha]),
left=0, right=255).astype(np.uint8)
a_float = interp(a_scaled, norm_span, array_module.array([min_alpha, alpha]), left=0, right=255)
a = np.nan_to_num(a_float, copy=False).astype(np.uint8)
return a


Expand Down

0 comments on commit 4823087

Please sign in to comment.