Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support imshow(<float16 array>). #20290

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

Merged
merged 1 commit into from
May 26, 2021
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
14 changes: 6 additions & 8 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,12 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
# 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
# Cast to float64
if A.dtype not in (np.float32, np.float16):
if A.dtype != np.float64:
_api.warn_external(
f"Casting input data from '{A.dtype}' to "
f"'float64' for imshow")
scaled_dtype = np.float64
scaled_dtype = np.dtype(
np.float64 if A.dtype.itemsize > 4 else np.float32)
if scaled_dtype.itemsize < A.dtype.itemsize:
_api.warn_external(
f"Casting input data from {A.dtype} to "
f"{scaled_dtype} for imshow")
else:
# probably an integer of some type.
da = a_max.astype(np.float64) - a_min.astype(np.float64)
Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,13 @@ def test_empty_imshow(make_norm):
im.make_image(fig._cachedRenderer)


def test_imshow_float16():
fig, ax = plt.subplots()
ax.imshow(np.zeros((3, 3), dtype=np.float16))
Copy link
Member

Choose a reason for hiding this comment

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

This warns too? Should you check for the warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this doesn't warn because it's an upcast (just like we don't warn for int->float).
(Unchecked warnings normally cause tests to fail, so this also tests that no warning is emitted.)

# Ensure that drawing doesn't cause crash.
fig.canvas.draw()


def test_imshow_float128():
fig, ax = plt.subplots()
ax.imshow(np.zeros((3, 3), dtype=np.longdouble))
Expand Down