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
Fix data cursor for images with additional transform #18737
Fix data cursor for images with additional transform #18737
Conversation
| array_extent = Bbox([[0, 0], arr.shape[:2]]) | ||
| trans = BboxTransform(boxin=data_extent, boxout=array_extent) | ||
| point = trans.transform([event.ydata, event.xdata]) | ||
| data_extent = Bbox([[xmin, ymin], [xmax, ymax]]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused - was the previous version just a huge bug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you referring to the switched xdata and ydata coordinates in the original version? I think this was fine originally (this code was covered quite well by tests in test_cursor_data()).
The first index of the array runs along the y-axis, while the second index runs along the x-axis. This means that a flip has to happen somewhere.
Originally, the flip was in the line point = trans.transform([event.ydata, event.xdata]). However, since we now want to apply a different transform first, we have to input the points in the right order (first x, then y). The flip still has to happen though, so we instead flip arr.shape and change i, j to j, i when we get the result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got it - I didn't trace it all through so was confused...
|
Can you provide a self-contained example in the description to make it easier for reviewers to test themselves? |
|
Example added |
| @@ -661,7 +661,8 @@ def contains(self, mouseevent): | |||
| # collection on nonlinear transformed coordinates. | |||
| # TODO: consider returning image coordinates (shouldn't | |||
| # be too difficult given that the image is rectilinear | |||
| x, y = mouseevent.xdata, mouseevent.ydata | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you aren't using xdata/ydata any more, does that mean the comment about about figimage is fixed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it should be fine to remove the self.axes is not mouseevent.inaxes check now and contains() should also work for FigureImage. However, FigureImage doesn't have a get_cursor_data() yet, so I think the only place where contains would be used for a FigureImage is for the pick events? I'm not sure about that though, maybe @anntzer can help since he wrote the original comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's likely that this fixes some of the problems I had noticed when I put in the comment, but I haven't reviewed this carefully enough to see whether all problems are indeed fixed.
|
Looking into this more, it seems like the current |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, sorry this sat for so long!
PR Summary
When an image artist gets an additional transform supplied via
set_transformor thetransform=...argument, the data cursor will give values at the wrong coordinates. Example:This happens because the current
AxesImage.get_cursor_datauses thexdataandydataattributes in the MouseEvent, which only undo the axis transform, but not the additional artist transform.This PR adds a test and fixes the issue by applying the inversion of the image artists transform directly to the MouseEvents
xandyattributes.PR Checklist
pytestpasses).flake8on changed files to check).flake8-docstringsandpydocstyle<4and runflake8 --docstring-convention=all).doc/users/next_whats_new/(follow instructions in README.rst there).doc/api/next_api_changes/(follow instructions in README.rst there).