Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix imshow to work with subclasses of ndarray. #18286

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 3 commits into from
Aug 22, 2020
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
2 changes: 1 addition & 1 deletion 2 lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
# of over numbers.
self.norm.autoscale_None(A)
dv = np.float64(self.norm.vmax) - np.float64(self.norm.vmin)
vmid = self.norm.vmin + dv / 2
vmid = np.float64(self.norm.vmin) + dv / 2
fact = 1e7 if scaled_dtype == np.float64 else 1e4
newmin = vmid - dv * fact
if newmin < a_min:
Expand Down
80 changes: 80 additions & 0 deletions 80 lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,3 +1124,83 @@ def test_exact_vmin():
@pytest.mark.flaky
def test_https_imread_smoketest():
v = mimage.imread('https://matplotlib.org/1.5.0/_static/logo2.png')


# A basic ndarray subclass that implements a quantity
# It does not implement an entire unit system or all quantity math.
# There is just enough implemented to test handling of ndarray
# subclasses.
class QuantityND(np.ndarray):
def __new__(cls, input_array, units):
obj = np.asarray(input_array).view(cls)
obj.units = units
return obj

def __array_finalize__(self, obj):
self.units = getattr(obj, "units", None)

def __getitem__(self, item):
units = getattr(self, "units", None)
ret = super(QuantityND, self).__getitem__(item)
if isinstance(ret, QuantityND) or units is not None:
ret = QuantityND(ret, units)
return ret

def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
func = getattr(ufunc, method)
if "out" in kwargs:
raise NotImplementedError
if len(inputs) == 1:
i0 = inputs[0]
unit = getattr(i0, "units", "dimensionless")
out_arr = func(np.asarray(i0), **kwargs)
elif len(inputs) == 2:
i0 = inputs[0]
i1 = inputs[1]
u0 = getattr(i0, "units", "dimensionless")
u1 = getattr(i1, "units", "dimensionless")
u0 = u1 if u0 is None else u0
u1 = u0 if u1 is None else u1
if ufunc in [np.add, np.subtract]:
if u0 != u1:
raise ValueError
unit = u0
elif ufunc == np.multiply:
unit = f"{u0}*{u1}"
elif ufunc == np.divide:
unit = f"{u0}/({u1})"
else:
raise NotImplementedError
out_arr = func(i0.view(np.ndarray), i1.view(np.ndarray), **kwargs)
else:
raise NotImplementedError
if unit is None:
out_arr = np.array(out_arr)
else:
out_arr = QuantityND(out_arr, unit)
return out_arr

@property
def v(self):
return self.view(np.ndarray)


def test_quantitynd():
q = QuantityND([1, 2], "m")
q0, q1 = q[:]
assert np.all(q.v == np.asarray([1, 2]))
assert q.units == "m"
assert np.all((q0 + q1).v == np.asarray([3]))
assert (q0 * q1).units == "m*m"
assert (q1 / q0).units == "m/(m)"
with pytest.raises(ValueError):
q0 + QuantityND(1, "s")


def test_imshow_quantitynd():
# generate a dummy ndarray subclass
arr = QuantityND(np.ones((2, 2)), "m")
fig, ax = plt.subplots()
ax.imshow(arr)
# executing the draw should not raise an exception
fig.canvas.draw()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.