FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

FIX: Image scaling for large dynamic range ints by jklymak · Pull Request #10133 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .png  (1) .py  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
27 changes: 19 additions & 8 deletions lib/matplotlib/image.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,25 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,

# TODO slice input array first
inp_dtype = A.dtype
if inp_dtype.kind == 'f':
scaled_dtype = A.dtype
else:
scaled_dtype = np.float32

a_min = A.min()
a_max = A.max()
# figure out the type we should scale to. For floats,
# leave as is. For integers cast to an appropriate-sized
# float. Small integers get smaller floats in an attempt
# to keep the memory footprint reasonable.
if a_min is np.ma.masked:
a_min, a_max = 0, 1 # all masked, so values don't matter
# all masked, so values don't matter
a_min, a_max = np.int32(0), np.int32(1)
if inp_dtype.kind == 'f':
scaled_dtype = A.dtype
else:
a_min = a_min.astype(scaled_dtype)
a_max = A.max().astype(scaled_dtype)
# probably an integer of some type.
da = a_max.astype(np.float64) - a_min.astype(np.float64)
if da > 1e8:
# give more breathing room if a big dynamic range
scaled_dtype = np.float64
else:
scaled_dtype = np.float32

# scale the input data to [.1, .9]. The Agg
# interpolators clip to [0, 1] internally, use a
Expand All @@ -386,6 +394,9 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
A_scaled = np.empty(A.shape, dtype=scaled_dtype)
A_scaled[:] = A
A_scaled -= a_min
a_min = a_min.astype(scaled_dtype)
a_max = a_max.astype(scaled_dtype)

if a_min != a_max:
A_scaled /= ((a_max - a_min) / 0.8)
A_scaled += 0.1
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Viewer requires iframe.
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_image.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,19 @@ def test_imshow_flatfield():
im.set_clim(.5, 1.5)


@image_comparison(baseline_images=['imshow_bignumbers'],
remove_text=True, style='mpl20',
extensions=['png'])
def test_imshow_bignumbers():
# putting a big number in an array of integers shouldn't
# ruin the dynamic range of the resolved bits.
fig, ax = plt.subplots()
img = np.array([[1, 2, 1e12],[3, 1, 4]], dtype=np.uint64)
pc = ax.imshow(img)
pc.set_clim(0, 5)
plt.show()


@pytest.mark.parametrize(
"make_norm",
[colors.Normalize,
Expand Down

Back | FazBrowse Home | New Git URL