Skip to content

Add RuntimeWarning guard around division-by-zero #22628

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
Mar 31, 2022

Conversation

oscargus
Copy link
Member

@oscargus oscargus commented Mar 9, 2022

PR Summary

Closes #22624

Avoid a RuntimeWarning otherwise plotted at the first divide by zero.

/local/data1/matplotlib/lib/mpl_toolkits/mplot3d/proj3d.py:25: RuntimeWarning: invalid value encountered in double_scalars
  u = (x01*x21 + y01*y21) / (x21**2 + y21**2)

Not sure if this can be tested properly. Directly calling the function is one way, but I do not have any other ideas really.

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

@oscargus oscargus added this to the v3.6.0 milestone Mar 9, 2022
@oscargus oscargus closed this Mar 9, 2022
@oscargus oscargus reopened this Mar 9, 2022
@tacaswell
Copy link
Member

This comes up in the degenerate case when p1 == p2?

@tacaswell
Copy link
Member

I would go with a test of the code from #22624. The thing we care about is that that code should not warn.

@oscargus
Copy link
Member Author

This comes up in the degenerate case when p1 == p2?

When I debugged it I noticed that x21 and y21 became zero quite often, but you are right, looking back in the code that is what happens. I'll modify it.

@oscargus
Copy link
Member Author

The thing we care about is that that code should not warn.

As I get it, one will need to move the mouse and it is when the mouse enters the window that the first warning is emitted. We can of course test the _line2d_seg_dist function directly though.

@oscargus oscargus force-pushed the proj3derrorstate branch 3 times, most recently from 19d7c4a to 4cb1dbd Compare March 10, 2022 10:25
@oscargus oscargus modified the milestones: v3.6.0, v3.5.2 Mar 10, 2022
@oscargus
Copy link
Member Author

@anntzer had a relevant comment in #22624 (comment)

Maybe one should simply not plot the mouse pointer position when in 3D mode?

(Still, if that is not decided and implemented, this can cover in the meanwhile.)

tacaswell
tacaswell previously approved these changes Mar 10, 2022
@tacaswell tacaswell dismissed their stale review March 10, 2022 14:24

Saw an issue right after approving :(

@QuLogic QuLogic changed the title Add RuntimeWarning guard. Add RuntimeWarning guard around division-by-zero Mar 16, 2022
@@ -17,11 +17,13 @@ def _line2d_seg_dist(p1, p2, p0):
and intersection point lies within segment if u is between 0 and 1
"""
Copy link
Member

@timhoffm timhoffm Mar 18, 2022

Choose a reason for hiding this comment

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

Your solution is technically correct. However, the issue is more a logical one rather than a technical division-by-zero problem. Therefore, I'd go with the following and leave all the original code unchanged in place.

Suggested change
"""
If p1 and p2 are identical, the distance between them and p0 is returned.
"""
if np.all(p1 == p2):
return np.hypot(p1[0] - p0[0], p1[1] - p0[1])

Copy link
Member Author

Choose a reason for hiding this comment

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

Added the docs and removed the comment. (Kept the solution with intermediate results.)

Copy link
Member

Choose a reason for hiding this comment

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

Kept the solution with intermediate results.

@oscargus: What is the motivation for keeping this? It would be nice to give a reason if you dismiss a suggestion. I find the separation of the special case and using explicit values there more clear, but can be convinced otherwise.

Copy link
Member Author

Choose a reason for hiding this comment

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

Just wanted to avoid duplicate code (and was a bit unsure what the effect of np.asarray was).

But I see your point about separation as well. And will try to motivate better in the future.

@@ -1002,6 +1002,16 @@ def test_lines_dists():
ax.set_ylim(0, 300)


def test_lines_dists_nowarning():
# Smoke test to see that no RuntimeWarning is emitted when two first
Copy link
Member

Choose a reason for hiding this comment

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

would it be preferable to test a user-facing API rather than the private methods?

Copy link
Member

Choose a reason for hiding this comment

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

This is mostly called through mouse callbacks so it is annoying to get public API tests that exercise this.

@timhoffm timhoffm dismissed their stale review March 31, 2022 20:06

Where to bail out for equal points is somewhat a stylistic choice. Not blocking over the current state.

@timhoffm timhoffm merged commit e6e20e3 into matplotlib:main Mar 31, 2022
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Mar 31, 2022
@oscargus oscargus deleted the proj3derrorstate branch March 31, 2022 20:58
QuLogic added a commit that referenced this pull request Apr 4, 2022
…628-on-v3.5.x

Backport PR #22628 on branch v3.5.x (Add RuntimeWarning guard around division-by-zero)
@timhoffm timhoffm mentioned this pull request Jul 18, 2022
2 tasks
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.

[Bug]: invalid value encountered with 'ortho' projection mode
5 participants