8000 MNT: do the interpolation in the input dtype · matplotlib/matplotlib@bcabef7 · GitHub
[go: up one dir, main page]

Skip to content

Commit bcabef7

Browse files
committed
MNT: do the interpolation in the input dtype
down-casting input data before interpolation was causing issues with numerical precision.
1 parent 2db0d6a commit bcabef7

File tree

4 files changed

+76
-75
lines changed

4 files changed

+76
-75
lines changed

lib/matplotlib/image.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,11 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
367367
# need to
368368

369369
# TODO slice input array first
370-
371-
# make a working array up here, re-use twice to save memory
372-
working_array = np.empty(A.shape, dtype=np.float32)
373-
370+
inp_dtype = A.dtype
371+
if inp_dtype.kind == 'f':
372+
scaled_dtype = A.dtype
373+
else:
374+
scaled_dtype = np.float32
374375
a_min = np.nanmin(A)
375376
a_max = np.nanmax(A)
376377
# scale the input data to [.1, .9]. The Agg
@@ -380,7 +381,7 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
380381
# over / under.
381382
# This may introduce numeric instabilities in very broadly
382383
# scaled data
383-
A_scaled = working_array
384+
A_scaled = np.empty(A.shape, dtype=scaled_dtype)
384385
A_scaled[:] = A
385386
A_scaled -= a_min
386387
A_scaled /= ((a_max - a_min) / 0.8)
@@ -410,7 +411,7 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
410411
if isinstance(self.norm, mcolors.NoNorm):
411412
A_resampled = A_resampled.astype(A.dtype)
412413

413-
mask = working_array
414+
mask = np.empty(A.shape, dtype=np.float32)
414415
if A.mask.shape == A.shape:
415416
# this is the case of a nontrivial mask
416417
mask[:] = np.where(A.mask, np.float32(np.nan),
Binary file not shown.
Loading

0 commit comments

Comments
 (0)
0