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 numpy 1.24 warnings #1218

Merged
merged 3 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
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
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')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is consistent with what is in test_dask.py.


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