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
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
10 changes: 7 additions & 3 deletions lib/mpl_toolkits/mplot3d/proj3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ def _line2d_seg_dist(p1, p2, p0):
p0[1] = y(s)

intersection point p = p1 + u*(p2-p1)
and intersection point lies within segment if u is between 0 and 1
and intersection point lies within segment if u is between 0 and 1.

If p1 and p2 are identical, the distance between them and p0 is returned.
"""
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.


x21 = p2[0] - p1[0]
y21 = p2[1] - p1[1]
x01 = np.asarray(p0[0]) - p1[0]
y01 = np.asarray(p0[1]) - p1[1]
if np.all(p1 == p2):
return np.hypot(x01, y01)

x21 = p2[0] - p1[0]
y21 = p2[1] - p1[1]
u = (x01*x21 + y01*y21) / (x21**2 + y21**2)
u = np.clip(u, 0, 1)
d = np.hypot(x01 - u*x21, y01 - u*y21)
Expand Down
10 changes: 10 additions & 0 deletions lib/mpl_toolkits/tests/test_mplot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

# arguments are the same, see GH#22624
p0 = (10, 30)
p1 = (20, 150)
proj3d._line2d_seg_dist(p0, p0, p1)
p0 = np.array(p0)
proj3d._line2d_seg_dist(p0, p0, p1)


def test_autoscale():
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.margins(x=0, y=.1, z=.2)
Expand Down