Skip to content

[WIP] Add check in text for finite values of x and y #9281

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

Closed
wants to merge 1 commit into from

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Oct 4, 2017

PR Summary

If we pass non-finite x or y to ax.text the code fails during draw rather than on instantiation or set_x and set_y. This just adds a np.isfinite check to x and y and calls the internal method at instantiation.

See #9267

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant

Add check for finite values of x and y: allow None

Add check for finite values of x and y: allow None
@@ -1165,7 +1166,10 @@ def set_x(self, x):

ACCEPTS: float
"""
self._x = x
if x is None or np.isfinite(x):
Copy link
Contributor

Choose a reason for hiding this comment

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

... do we accept None? what would that even mean? (in any case this is inconsistent with the ACCEPTS: float just above).

@anntzer
Copy link
Contributor

anntzer commented Oct 5, 2017

I am not convinced that the Text class should catch this case. IMO this is fairly similar to how we allow nans and infs when plotting data, and simply silently ignore such elements.

The true issue is with the webagg backend silently dropping draw exceptions. The relevant chunk is in backend_webagg_core, in

    def draw(self):
        renderer = self.get_renderer(cleared=True)

        self._png_is_old = True

        backend_agg.RendererAgg.lock.acquire()
        try:
            self.figure.draw(renderer)
        finally:
            backend_agg.RendererAgg.lock.release()
            # Swap the frames
            self.manager.refresh_all()

    def draw_idle(self):
        self.send_event("draw")

We send a draw event, and the draw method raises an exception asynchronously (as can be seen by adding an except clause and printing the exception to a file (stdout is not available because of how jupyter handles things)), but there's no one to actually catch that exception, so it is silently dropped. This should not be the case.

OTOH coming back to the proposed fix I have been known to be very much on the "let the user shoot himself in the foot if he wants to" camp :-) and it is true that in this specific case I can't really think of a compelling reason to allow such inputs.

@jklymak
Copy link
Member Author

jklymak commented Oct 5, 2017

@anntzer So, first, yeah, test_skew.py somehow had a None position. The code wasn't readily accessible to me, so I'm not sure why it did that, but I can only assume it set it to non-None at some point later in the script. With these changes the documentation is still failing to build, so I suspect other errors of this sort. Not clear to me if text should be changed to make these errors OK, or if the documentation examples should be changed. The Sphinx errors are quite sphinx-like.

WRT this issue. 1) I completely agree that the fundamental problem is that webagg is failing silently. That is beyond my ability to fix.

  1. I'd still argue that the error message thrown out is pretty cryptic:
ValueError: posx and posy should be finite values

Given that there are no posx and posy in the call toax.text I think it would be nice to catch this error earlier (or to let it pass through without an error, and just not render any text if either x or y are not finite floats.

@anntzer
Copy link
Contributor

anntzer commented Oct 5, 2017

Given the place from where the exception is thrown, it looks totally reasonable (and an improvement) to change the message to something like "Text object position must be finite" (or whatever sounds reasonable to you), because that's exactly what the error is.

Actually, if it was really up to me I would just let nonfinite x and y pass through silently... (in any case, I think the behavior you notice is indeed that some cases require delaying the definition of x and y, which means that catching at set_xy time is never going to cover all cases, so again let's just check things where we can actually cover all cases).

@tacaswell
Copy link
Member

#4066
#4235
and #4535 are the source of this exception. Given my comment in #4535 I think a simpler fix is to just replace the exception with a short-circuit return is better.

Should probably also move it to below the transformation.

@jklymak jklymak changed the title Add check for finite values of x and y Add check in text for finite values of x and y Oct 5, 2017
@jklymak
Copy link
Member Author

jklymak commented Oct 5, 2017

OK, so the plan is to pass any invalid through, and when it comes time to actually render just have it not render because the position is invalid?

I'm OK w/ that and it was the behaviour I was expecting when I (accidentally) called text with NaN arguments. I could see that someone who put text into a transformed axis with "valid" x,y would be a bit confused as to why their text doesn't show up. Would a warning be too messy here?

@jklymak jklymak changed the title Add check in text for finite values of x and y [WIP] Add check in text for finite values of x and y Oct 5, 2017
@jklymak jklymak closed this Oct 6, 2017
@jklymak jklymak deleted the checktextpos branch March 5, 2019 16:08
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.

3 participants