Skip to content

Adding test for all NANs in contour #25330

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

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,3 +715,12 @@ def test_bool_autolevel():
assert plt.tricontour(x, y, z).levels.tolist() == [.5]
assert plt.tricontourf(x, y, z.tolist()).levels.tolist() == [0, .5, 1]
assert plt.tricontourf(x, y, z).levels.tolist() == [0, .5, 1]


def test_contour_all_nan():
# Smoke test for gh#14124
x = np.array([[np.nan, np.nan], [np.nan, np.nan]])
with pytest.warns(UserWarning) as w:
Copy link
Contributor

@greglucas greglucas Feb 26, 2023

Choose a reason for hiding this comment

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

You should add the match string here rather than asserting on private message lists. https://docs.pytest.org/en/7.1.x/how-to/capture-warnings.html#warns

edit: also, the warning text seems a bit misleading here since the user didn't pass in any masked elements.

Copy link
Member

@oscargus oscargus Feb 26, 2023

Choose a reason for hiding this comment

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

There are two warnings, so this should be the way to go.
https://docs.pytest.org/en/7.1.x/how-to/capture-warnings.html#recwarn

Edit: I think that those comes from numpy.

Oscar@Beavis MINGW64 ~/matplotlib (main)
$ git grep "Warning: converting a masked element"

Oscar@Beavis MINGW64 ~/matplotlib (main)
$ git grep "converting a masked element"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As @oscargus mentioned, There are two warnings, so I manually checked the warning messages for both.

Yes, the warning message is coming from numpy.

/usr/lib/python3/dist-packages/numpy/ma > grep "Warning: converting a masked" core.py   
            warnings.warn("Warning: converting a masked element to nan.", stacklevel=2)

Copy link
Contributor

Choose a reason for hiding this comment

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

The warning definitely comes from numpy. It is converting the floats for zmin/zmax float(np.ma.masked)

Ahh, now I see that (maybe unfortunately?) the levels actually have values now, so we only have the two warnings (there used to be other level related warnings on earlier versions):
[-1.0e-13 -7.5e-14 -5.0e-14 -2.5e-14 0.0e+00 2.5e-14 5.0e-14 7.5e-14 1.0e-13]

I guess these warnings don't really help much for the root of the problem, so I'd actually suggest getting rid of them altogether by converting the typecasts to: z.min().astype(float), and think about instead giving a more helpful error message somewhere else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, should we close this PR and add z.min().astype(float) & z.max().astype(float) to a new one?
I'm getting the correct result using astype

Copy link
Contributor

Choose a reason for hiding this comment

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

That seems like a reasonable fix to me and seems to line up with the plt.plot([np.nan], [np.nan]) case not issuing any warnings either.

plt.contour(x)
assert str(w._list[0].message) == "Warning: converting a masked element to nan."
assert str(w._list[1].message) == "Warning: converting a masked element to nan."