Skip to content

Let imshow handle float128 data. #8447

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
Apr 14, 2017
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
7 changes: 6 additions & 1 deletion lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def _to_rgba_no_colorcycle(c, alpha=None):
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
# tuple color.
c = np.array(c)
if not np.can_cast(c.dtype, float) or c.ndim != 1:
if not np.can_cast(c.dtype, float, "same_kind") or c.ndim != 1:
# Test the dtype explicitly as `map(float, ...)`, `np.array(...,
# float)` and `np.array(...).astype(float)` all convert "0.5" to 0.5.
# Test dimensionality to reject single floats.
Expand Down Expand Up @@ -934,6 +934,11 @@ def __call__(self, value, clip=None):
resdat -= vmin
resdat /= (vmax - vmin)
result = np.ma.array(resdat, mask=result.mask, copy=False)
# Agg cannot handle float128. We actually only need 32-bit of
# precision, but on Windows, `np.dtype(np.longdouble) == np.float64`,
# so casting to float32 would lose precision on float64s as well.
if result.dtype == np.longdouble:
result = result.astype(np.float64)
if is_scalar:
result = result[0]
return result
Expand Down
16 changes: 8 additions & 8 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
space.
"""
if A is None:
raise RuntimeError('You must first set the image'
' array or the image attribute')
if any(s == 0 for s in A.shape):
raise RuntimeError('You must first set the image '
'array or the image attribute')
if A.size == 0:
raise RuntimeError("_make_image must get a non-empty image. "
"Your Artist's draw method must filter before "
"this method is called.")
Expand Down Expand Up @@ -359,7 +359,7 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
created_rgba_mask = False

if A.ndim not in (2, 3):
raise ValueError("Invalid dimensions, got %s" % (A.shape,))
raise ValueError("Invalid dimensions, got {}".format(A.shape))

if A.ndim == 2:
A = self.norm(A)
Expand Down Expand Up @@ -589,11 +589,11 @@ def set_data(self, A):
self._A = cbook.safe_masked_invalid(A, copy=True)

if (self._A.dtype != np.uint8 and
not np.can_cast(self._A.dtype, float)):
raise TypeError("Image data can not convert to float")
not np.can_cast(self._A.dtype, float, "same_kind")):
raise TypeError("Image data cannot be converted to float")

if (self._A.ndim not in (2, 3) or
(self._A.ndim == 3 and self._A.shape[-1] not in (3, 4))):
if not (self._A.ndim == 2
or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
Copy link
Member

Choose a reason for hiding this comment

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

I think that the not should not have been dropped?

I am partial towards leaving in (), even if they are not strictly needed when they help clarity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the boolean structure looks correct to me
i think the newline already helps with priority

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I missed the leading not when I read this before 🐑

raise TypeError("Invalid dimensions for image data")

self._imcache = None
Expand Down
21 changes: 13 additions & 8 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,9 @@ def test_jpeg_alpha():
def test_nonuniformimage_setdata():
ax = plt.gca()
im = NonUniformImage(ax)
x = np.arange(3, dtype=np.float64)
y = np.arange(4, dtype=np.float64)
z = np.arange(12, dtype=np.float64).reshape((4, 3))
x = np.arange(3, dtype=float)
y = np.arange(4, dtype=float)
z = np.arange(12, dtype=float).reshape((4, 3))
im.set_data(x, y, z)
x[0] = y[0] = z[0, 0] = 9.9
assert im._A[0, 0] == im._Ax[0] == im._Ay[0] == 0, 'value changed'
Expand All @@ -516,7 +516,7 @@ def test_nonuniformimage_setdata():
def test_axesimage_setdata():
ax = plt.gca()
im = AxesImage(ax)
z = np.arange(12, dtype=np.float64).reshape((4, 3))
z = np.arange(12, dtype=float).reshape((4, 3))
im.set_data(z)
z[0, 0] = 9.9
assert im._A[0, 0] == 0, 'value changed'
Expand All @@ -525,7 +525,7 @@ def test_axesimage_setdata():
def test_figureimage_setdata():
fig = plt.gcf()
im = FigureImage(fig)
z = np.arange(12, dtype=np.float64).reshape((4, 3))
z = np.arange(12, dtype=float).reshape((4, 3))
im.set_data(z)
z[0, 0] = 9.9
assert im._A[0, 0] == 0, 'value changed'
Expand All @@ -534,9 +534,9 @@ def test_figureimage_setdata():
def test_pcolorimage_setdata():
ax = plt.gca()
im = PcolorImage(ax)
x = np.arange(3, dtype=np.float64)
y = np.arange(4, dtype=np.float64)
z = np.arange(6, dtype=np.float64).reshape((3, 2))
x = np.arange(3, dtype=float)
y = np.arange(4, dtype=float)
z = np.arange(6, dtype=float).reshape((3, 2))
im.set_data(x, y, z)
x[0] = y[0] = z[0, 0] = 9.9
assert im._A[0, 0] == im._Ax[0] == im._Ay[0] == 0, 'value changed'
Expand Down Expand Up @@ -778,3 +778,8 @@ def test_empty_imshow():

with pytest.raises(RuntimeError):
im.make_image(fig._cachedRenderer)


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