Skip to content

Make AxesImage.contains account for transforms #12052

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 3 commits into from

Conversation

ilopata1
Copy link

@ilopata1 ilopata1 commented Sep 6, 2018

Corrects contains() method for AxesImages with transforms applied See. #12042

PR Summary

As described in #12042

Proposed change creates a bounding box from the image extents, applies the current transform to the bounding box and then tests whether the mouse event occurred within the transformed bounding box.

Corrects contains for AxesImages with transforms applied See. matplotlib#12042
bbox = Bbox(np.array([[xmin, ymin], [xmax, ymax]]))
transformed_bbox = TransformedBbox(bbox, trans)

inside = transformed_bbox.contains(mouseevent.x,mouseevent.y)
Copy link
Member

@jklymak jklymak Sep 7, 2018

Choose a reason for hiding this comment

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

Thanks a lot @ilopata1! No criticism to you because I think you talked this through w/ @tacaswell, but wrt #12031 (excessive transforms being created) I think this is the kind of code that'll do that. I think this transformed_bbox should be saved so that a new bbox is not created on every call to contains.

Copy link
Author

Choose a reason for hiding this comment

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

@jklymak Sounds very reasonable. Understanding the class well enough to find places at which the extents and transforms are modified is more than I have the capacity to take on at present, so regretfully, much as I would like to help further I am going to have to leave this hanging out there and hope that others might have the time and interest to carry to completion.

Copy link
Member

Choose a reason for hiding this comment

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

@ilopata1 This solution may be the best we can do for now. But it was a bit of an a-ha moment for why so many transforms get created even if a plot is not changing.

Does contains get called all the time, or just after a mouse click? If just after a mouse click, its probably not at all a performance problem for this change to be put in.

Copy link
Author

Choose a reason for hiding this comment

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

I can't speak to when/whether it may get called internally, but from an application perspective it is most likely to get called on mouse events. This may include mouse moves, which can be generated in high volume.

I just ran a test and on an Intel i5 processor, over 18 executions of this code fragment the average execution time was 0.00022 seconds. To put that another way, contains would need to be called 4,500 times on my machine for the execution time to be one second.

@tacaswell tacaswell added this to the v3.1 milestone Sep 7, 2018
anntzer
anntzer previously requested changes Sep 7, 2018
Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

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

This doesn't actually work because TransformedBbox.contains is basically broken for non-axis-aligned rectangles (it inherits contains from Bbox, which checks x and y separately).

This can be seen by using a 45° rotation instead -- with this PR, the whole axis-aligned rectangle that contains the transformed AxesImage will be considered as "contained".

I think(?) you could apply the inverse transform to the event coordinates and check whether that's in the untransformed bbox? That'll work around the issue above, and avoid the cost of creating a TransformedBbox.

@ilopata1
Copy link
Author

ilopata1 commented Sep 7, 2018

@anntzer Good catch. I have tested your hypothesis and using the reverse transform on the event cordinates works. Being a noob, I don't know if I am supposed to update this PR or create a new one. Can you give me a pointer?

@WeatherGod
Copy link
Member

WeatherGod commented Sep 7, 2018 via email

Changed approach to apply inverse transform to event coordinates.  This a) addresses an issue with TransformedBbox only working with coordinate aligned axes; and b) avoids creating a new transform.
@QuLogic QuLogic changed the title Update image.py Make AxesImage.contains account for transforms Sep 7, 2018
@QuLogic
Copy link
Member

QuLogic commented Sep 7, 2018

When editing on GitHub, please try to give your commits descriptive messages.

To whomever merges, please squash-merge and update the commit message.

@ilopata1
Copy link
Author

Someone give me a pointer on what I need to do, if anything, to resolve the codecov failure? Thanks

@jklymak
Copy link
Member

jklymak commented Sep 13, 2018

We can prob let the codecov slide, but I think you can fake a mouse event and add a test for this. Ideally you could grep for mouse event in the test subdirectory, and then make a test.

@anntzer anntzer dismissed their stale review September 13, 2018 19:40

no longer relevant review

@jklymak
Copy link
Member

jklymak commented Oct 2, 2018

@ilopata1 where is this one at? Are you done? Be sure to ping us if you need a review....

@ilopata1
Copy link
Author

ilopata1 commented Oct 2, 2018

@jklymak I spent a long time trying to write a test case that would validate the code but had much more trouble writing a standalone test that worked than I did preparing the original patch. I can say that the patch fixes my case in my application, but I am hesitant to recommend the patch without being able to confirm it more authoritatively. So to answer your question more directly, I am stalled.

@jklymak jklymak added the stale label Feb 26, 2019
@jklymak jklymak modified the milestones: v3.1.0, v3.2.0 Feb 26, 2019
@jklymak
Copy link
Member

jklymak commented Jul 16, 2019

Thanks a lot for the contribution. I'm going to close this as having had no action for quite a while. OTOH, if there is still a community that wants this, please feel free to re-open, with my apologies!

@jklymak jklymak closed this Jul 16, 2019
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.

6 participants