Skip to content

Fix calls to np.ma.masked_where #20511

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 2 commits into from
Jul 5, 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
4 changes: 2 additions & 2 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1545,11 +1545,11 @@ class LogNorm(Normalize):

def autoscale(self, A):
# docstring inherited.
super().autoscale(np.ma.masked_less_equal(A, 0, copy=False))
super().autoscale(np.ma.array(A, mask=(A <= 0)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this save the copy step? It seems like this is creating a new array anyways, so I'm wondering if it is gaining anything relative to just removing the copy=False?

Copy link
Member Author

Choose a reason for hiding this comment

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

The default for np.ma.array is copy=False, keep_mask=True. The former will not copy the data, and the latter will use the existing mask (if any) with the new mask, which will make a copy. Removing copy=False from masked_less_equal makes a copy of both.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for clarifying! I was curious how to test if this actually holds and learned about np.shares_memory today. I did verify this assertion with that.

Copy link
Member Author

Choose a reason for hiding this comment

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

You can also check, if you have b = np.ma.array(a), that b.base is a (if a is originally a plain array), or b.base is a.base (if a is originally a masked array).


def autoscale_None(self, A):
# docstring inherited.
super().autoscale_None(np.ma.masked_less_equal(A, 0, copy=False))
super().autoscale_None(np.ma.array(A, mask=(A <= 0)))


@_make_norm_from_scale(
Expand Down
8 changes: 5 additions & 3 deletions lib/matplotlib/quiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1158,8 +1158,10 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
return barb_list

def set_UVC(self, U, V, C=None):
self.u = ma.masked_invalid(U, copy=False).ravel()
self.v = ma.masked_invalid(V, copy=False).ravel()
# We need to ensure we have a copy, not a reference to an array that
# might change before draw().
self.u = ma.masked_invalid(U, copy=True).ravel()
self.v = ma.masked_invalid(V, copy=True).ravel()

# Flip needs to have the same number of entries as everything else.
# Use broadcast_to to avoid a bloated array of identical values.
Expand All @@ -1170,7 +1172,7 @@ def set_UVC(self, U, V, C=None):
flip = self.flip

if C is not None:
c = ma.masked_invalid(C, copy=False).ravel()
c = ma.masked_invalid(C, copy=True).ravel()
x, y, u, v, c, flip = cbook.delete_masked_points(
self.x.ravel(), self.y.ravel(), self.u, self.v, c,
flip.ravel())
Expand Down
28 changes: 28 additions & 0 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,34 @@ def test_imshow_quantitynd():
fig.canvas.draw()


@check_figures_equal(extensions=['png'])
def test_norm_change(fig_test, fig_ref):
# LogNorm should not mask anything invalid permanently.
data = np.full((5, 5), 1, dtype=np.float64)
data[0:2, :] = -1

masked_data = np.ma.array(data, mask=False)
masked_data.mask[0:2, 0:2] = True

cmap = plt.get_cmap('viridis').with_extremes(under='w')

ax = fig_test.subplots()
im = ax.imshow(data, norm=colors.LogNorm(vmin=0.5, vmax=1),
extent=(0, 5, 0, 5), interpolation='nearest', cmap=cmap)
im.set_norm(colors.Normalize(vmin=-2, vmax=2))
im = ax.imshow(masked_data, norm=colors.LogNorm(vmin=0.5, vmax=1),
extent=(5, 10, 5, 10), interpolation='nearest', cmap=cmap)
im.set_norm(colors.Normalize(vmin=-2, vmax=2))
ax.set(xlim=(0, 10), ylim=(0, 10))

ax = fig_ref.subplots()
ax.imshow(data, norm=colors.Normalize(vmin=-2, vmax=2),
extent=(0, 5, 0, 5), interpolation='nearest', cmap=cmap)
ax.imshow(masked_data, norm=colors.Normalize(vmin=-2, vmax=2),
extent=(5, 10, 5, 10), interpolation='nearest', cmap=cmap)
ax.set(xlim=(0, 10), ylim=(0, 10))


@pytest.mark.parametrize('x', [-1, 1])
@check_figures_equal(extensions=['png'])
def test_huge_range_log(fig_test, fig_ref, x):
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_quiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,17 @@ def test_barbs_flip():
flip_barb=Y < 0)


def test_barb_copy():
fig, ax = plt.subplots()
u = np.array([1.1])
v = np.array([2.2])
b0 = ax.barbs([1], [1], u, v)
u[0] = 0
assert b0.u[0] == 1.1
v[0] = 0
assert b0.v[0] == 2.2


def test_bad_masked_sizes():
"""Test error handling when given differing sized masked arrays."""
x = np.arange(3)
Expand Down