Skip to content

points_in_path can fail with NaN data #3759

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
Nov 7, 2014
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
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def test_path_clipping():
xy, facecolor='none', edgecolor='red', closed=True))


def test_point_in_path_nan():
box = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]])
p = Path(box)
test = np.array([[np.nan, 0.5]])
contains = p.contains_points(test)
assert len(contains) == 1
Copy link
Member

Choose a reason for hiding this comment

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

why isn't the length 2?

Copy link
Member

Choose a reason for hiding this comment

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

Because "test" is length 1

On Fri, Nov 7, 2014 at 9:05 AM, Thomas A Caswell notifications@github.com
wrote:

In lib/matplotlib/tests/test_path.py:

@@ -61,6 +61,15 @@ def test_path_clipping():
xy, facecolor='none', edgecolor='red', closed=True))

+def test_point_in_path_nan():

  • box = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]])
  • p = Path(box)
  • test = np.array([[np.nan, 0.5]])
  • contains = p.contains_points(test)
  • assert len(contains) == 1

why isn't the length 2?


Reply to this email directly or view it on GitHub
https://github.com/matplotlib/matplotlib/pull/3759/files#r20011300.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah -- a single point (which is an x, y pair) is being tested.

Copy link
Member

Choose a reason for hiding this comment

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

Right, sorry for the brains-bar comment.

On Fri Nov 07 2014 at 11:23:55 AM Michael Droettboom <
notifications@github.com> wrote:

In lib/matplotlib/tests/test_path.py:

@@ -61,6 +61,15 @@ def test_path_clipping():
xy, facecolor='none', edgecolor='red', closed=True))

+def test_point_in_path_nan():

  • box = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]])
  • p = Path(box)
  • test = np.array([[np.nan, 0.5]])
  • contains = p.contains_points(test)
  • assert len(contains) == 1

Yeah -- a single point (which is an x, y pair) is being tested.


Reply to this email directly or view it on GitHub
https://github.com/matplotlib/matplotlib/pull/3759/files#r20019732.

assert not contains[0]


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
16 changes: 13 additions & 3 deletions src/_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,12 @@ point_in_path_impl(const void* const points_, const size_t s0,
for (i = 0; i < n; ++i) {
ty = *(double *)(points + s0 * i + s1);

// get test bit for above/below X axis
yflag0[i] = (vty0 >= ty);
if (MPL_isfinite64(ty)) {
// get test bit for above/below X axis
yflag0[i] = (vty0 >= ty);

subpath_flag[i] = 0;
subpath_flag[i] = 0;
}
}

do
Expand All @@ -191,6 +193,10 @@ point_in_path_impl(const void* const points_, const size_t s0,
tx = *(double *)(points + s0 * i);
ty = *(double *)(points + s0 * i + s1);

if (MPL_notisfinite64(tx) || MPL_notisfinite64(ty)) {
continue;
}

yflag1 = (vty1 >= ty);
// Check if endpoints straddle (are on opposite sides) of
// X axis (i.e. the Y's differ); if so, +X ray could
Expand Down Expand Up @@ -237,6 +243,10 @@ point_in_path_impl(const void* const points_, const size_t s0,
tx = *(double *)(points + s0 * i);
ty = *(double *)(points + s0 * i + s1);

if (MPL_notisfinite64(tx) || MPL_notisfinite64(ty)) {
continue;
}

yflag1 = (vty1 >= ty);
if (yflag0[i] != yflag1) {
if (((vty1 - ty) * (vtx0 - vtx1) >=
Expand Down