Skip to content

Commit d85382b

Browse files
committed
Fix: Update handling of filling values in MaskedImageMixin to ensure compatibility with future NumPy versions
1 parent c0c932a commit d85382b

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

plotpy/items/image/masked.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
from typing import TYPE_CHECKING
66

7-
import guidata.io
87
import numpy as np
98
import numpy.ma as ma
109
from qtpy import QtCore as QC
1110

11+
import guidata.io
1212
from plotpy import io
1313
from plotpy._scaler import INTERP_NEAREST, _scale_rect, _scale_xy
1414
from plotpy.config import _
@@ -23,11 +23,11 @@
2323
from plotpy.styles.image import MaskedImageParam, MaskedXYImageParam
2424

2525
if TYPE_CHECKING:
26-
import guidata.io
2726
import qwt.scale_map
2827
from qtpy.QtCore import QRectF
2928
from qtpy.QtGui import QPainter
3029

30+
import guidata.io
3131
from plotpy.plot import BasePlot
3232

3333

@@ -150,10 +150,6 @@ def deserialize(
150150
def update_mask(self) -> None:
151151
"""Update mask"""
152152
if isinstance(self.data, np.ma.MaskedArray):
153-
# Casting filling_value to data dtype, otherwise this may raise an error
154-
# in future versions of NumPy (at the time of writing, this raises a
155-
# DeprecationWarning "NumPy will stop allowing conversion of out-of-bound
156-
# Python integers to integer arrays.")
157153
filling_value = self.param.filling_value
158154
if filling_value is None or (
159155
isinstance(filling_value, float) and np.isnan(filling_value)
@@ -164,8 +160,15 @@ def update_mask(self) -> None:
164160
else:
165161
val = np.array(np.nan, dtype=self.data.dtype)
166162
else:
163+
# If filling_value is out of bounds for data dtype, get the default
164+
# fill value for that dtype
165+
if np.issubdtype(self.data.dtype, np.integer):
166+
info = np.iinfo(self.data.dtype)
167+
if filling_value < info.min or filling_value > info.max:
168+
# Using the default fill value thanks to the get_fill_value()
169+
# method of MaskedArray:
170+
filling_value = self.data.get_fill_value()
167171
val = np.array(filling_value).astype(self.data.dtype)
168-
169172
self.data.set_fill_value(val)
170173

171174
def set_mask(self, mask: ma.MaskedArray) -> None:

0 commit comments

Comments
 (0)