Skip to content

Commit 1376d00

Browse files
committed
fix: handle constant data in scale_data_to_dtype to avoid NaN values
1 parent 351512e commit 1376d00

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

plotpy/io.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ def scale_data_to_dtype(data: np.ndarray, dtype: np.dtype) -> np.ndarray:
6464
info = np.iinfo(dtype)
6565
dmin = data.min()
6666
dmax = data.max()
67+
if dmax == dmin:
68+
# Constant data: avoid 0/0 division (which would yield NaN). Map every
69+
# sample to the dtype range minimum so downstream image rendering keeps
70+
# working instead of seeing NaNs.
71+
data = np.full_like(data, float(info.min))
72+
return np.array(data, dtype)
6773
data -= dmin
6874
data = data * float(info.max - info.min) / (dmax - dmin)
6975
data = data + float(info.min)

0 commit comments

Comments
 (0)