Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix floating comparison in path.arc #26991

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

stevezhang1999
Copy link
Contributor

@stevezhang1999 stevezhang1999 commented Oct 4, 2023

PR summary

Closes #26972. When drawing the spine for the radial axis, we call Path.arc to draw. In Path.arc, we are using == for floating point comparison which may results in wrong results. The solution is to compare with a tolerance.

Tests on Path.arc is added to ensure that it's robust about the input change within [-1e-6, 1e-6]

The solution is to replace this with np.isclose() np.isclose(0, 1e-6) will give false, which is not we want.

However, a large number of tests will break and I'm checking whether the difference of results will be noticed by human eyes. Failing locally, we think this is irrelevant

PR checklist

@oscargus
Copy link
Member

oscargus commented Oct 4, 2023

Seems like all tests were OK?

Maybe add a test like one of the examples in #26972 though?

@stevezhang1999
Copy link
Contributor Author

stevezhang1999 commented Oct 4, 2023

Seems like all tests were OK?

Maybe add a test like one of the examples in #26972 though?

That's weird, numerous tests are failing on my machine test_pathes, test_path, test_axes... They are all image comparison tests complaining "image not close" but the difference are all black (according to human eyes) Are we doing image comparison tests differently on CI?

mpl version is 3.9.0.dev356+g19c0e07558

@stevezhang1999
Copy link
Contributor Author

Should the test be an image comparison test that with a baseline image? I haven't think about a way that we don't need to add a image

@oscargus
Copy link
Member

oscargus commented Oct 4, 2023

Does the tests pass on main for you? A common reason for failing tests is font issues (one reason that MPL pin freetype to an old version is subtle differences in the exact rendering). A consequence is that it may be hard to generate test images on such an install (as they will maybe fail on the CI, but worth trying, it is actually possible to download the CI-generated image from the artifact and use that if it is a problem).

Another reason is that a different processor architecture is used, which may lead to floating-point differences. Usually we compensate for that in the tests, e.g.

@image_comparison(
['contour_addlines.png'], remove_text=True, style='mpl20',
tol=0.15 if platform.machine() in ('aarch64', 'ppc64le', 's390x')
else 0.03)

But considering your multiple failures I'd guess freetype.

@stevezhang1999
Copy link
Contributor Author

Does the tests pass on main for you? A common reason for failing tests is font issues (one reason that MPL pin freetype to an old version is subtle differences in the exact rendering). A consequence is that it may be hard to generate test images on such an install (as they will maybe fail on the CI, but worth trying, it is actually possible to download the CI-generated image from the artifact and use that if it is a problem).

Another reason is that a different processor architecture is used, which may lead to floating-point differences. Usually we compensate for that in the tests, e.g.

@image_comparison(
['contour_addlines.png'], remove_text=True, style='mpl20',
tol=0.15 if platform.machine() in ('aarch64', 'ppc64le', 's390x')
else 0.03)

But considering your multiple failures I'd guess freetype.

Thanks for the help! There still test failures on main (I just assumed that they will work...) I setup the dev environment with conda following "Setting up Matplotlib for development", so I have no ida about where the font issues come from?

@stevezhang1999 stevezhang1999 marked this pull request as ready for review October 5, 2023 03:39
@tacaswell
Copy link
Member

My understanding of the problem is that:

  • we always try to draw an arc less than or equal to 360 in the clockwise direction
  • to sort this out we subtract the extra multiples of 360 off of the second angle
  • if the second angle is now less than the first, assume they are "equal" and add 360 back to the second so we go all the way around
  • in some cases when they are "equal" the floating point error falls the other way and it looks like the user asked for (0, 360 + delta) which we want to normalize to (0, delta) when the user wanted a full circle.

I don't think that this is quite the right fix. The behavior we want is:

  • if theta1 == theta2 -> do nothing
  • if theta1 - theta2 < 360 -> do nothing
  • if theta2 == theta1 + 360 + non-epsilon -> normalize to the shortest way around
  • if theta2 == theta1 + 360*n + epsilon -> draw a complete circle

so I think the test is:

  • do we have a "winding"
    • if so, is it "close" to an integer number -> then draw a full circle
    • if not, make no changes

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]: set_theta_offset removes grid outline
3 participants