Skip to content

BUG: Fix implementation of _is_closed_polygon #17801

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
Jun 30, 2020

Conversation

dopplershift
Copy link
Contributor

PR Summary

It's a bad idea to use floating point equality to check if two points are equal. While not a huge difference, especially in the case of contour analysis, this was causing testing issues downstream for MetPy. Specifically, I had one test that yielded different results on macOS vs. Linux/Windows, and this was the root cause. This change then slightly adjusted where a seemingly closed contour was labelled.

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@dopplershift
Copy link
Contributor Author

Waiting to see what kind of destruction this wreaks upon our suite of image tests...

@dopplershift
Copy link
Contributor Author

And for the historians, the previous implementation came in with 15a35dc and is the original, predating matplotlib 0.98.4.

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

Np.allclose?

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

Np.allclose?

@tacaswell tacaswell added this to the v3.3.0 milestone Jun 30, 2020
@tacaswell tacaswell requested a review from QuLogic June 30, 2020 00:23
@QuLogic
Copy link
Member

QuLogic commented Jun 30, 2020

In Cartopy, we used np.isclose(path_verts[0, :], path_verts[1:, :], rtol=1e-10, atol=1e-13) because those tolerances match Matplotlib's path tolerances ffaf302; we probably want to use that here too, for consistency.

@anntzer
Copy link
Contributor

anntzer commented Jun 30, 2020

Can we instead fix the contour generator to make it output exactly matching endpoints for contour lines? (AFAICT this is not intended as a general purpose function, but only to detect whether contour lines are closed)
Edit: Indeed, I would like to see a case where the contour generator does does not generate an exactly closed curve.

@dopplershift
Copy link
Contributor Author

@anntzer This issue has held up a bugfix release for me for over a month (trying to get passing tests), so I'm not inclined to search for the pure solution right now, nor am I likely to have cycles any time soon to do so. Then again, I've now already monkey-patched this (internal 😬) function in my MetPy tests, so maybe it doesn't matter.

For reference, here's a sample where it makes a difference--stock matplotlib:
image

With my change:
image

The change is in the closed 268 contour a third-ways from the right side of the image centered vertically, over Kansas and Oklahoma.

I'll also note that this really only matters in practicality because the inline labelling algorithm takes a different approach for closed vs. open contours. In fact, that function is only used in the process of generating labels, nowhere else.

It's a bad idea to use floating point equality to check if two points
are equal. While not a huge difference, especially in the case of
contour analysis, this was causing testing issues downstream. In one
case this yielded different results for the same test on different OSes.
@dopplershift
Copy link
Contributor Author

Updated to use tighter tolerances as suggested by @QuLogic. Also now using np.allclose as suggested by @jklymak and @timhoffm .

@anntzer
Copy link
Contributor

anntzer commented Jun 30, 2020

fair enough

@tacaswell tacaswell merged commit 6a66f76 into matplotlib:master Jun 30, 2020
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Jun 30, 2020
tacaswell added a commit that referenced this pull request Jul 1, 2020
…801-on-v3.3.x

Backport PR #17801 on branch v3.3.x (BUG: Fix implementation of _is_closed_polygon)
@dopplershift dopplershift deleted the fix-is-closed branch July 1, 2020 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants