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

Conversation

xtanion
Copy link
Contributor

@xtanion xtanion commented Feb 26, 2023

PR Summary

This PR adds checks/tests for the all NANs test in plt.contour as mentioned in issue #14124.

Closes #14124.

PR Checklist

Documentation and Tests

  • Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • New plotting related features are documented with examples.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@oscargus
Copy link
Member

Thanks! I added a line in your original post to automatically close the issue when merged.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

plt.contour with all NaNs fails assertion in _contour.cpp
3 participants