Skip to content

FIX: bypass inverse in collection #17206

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 3 commits into from
Apr 27, 2020

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Apr 22, 2020

PR Summary

I think this fixes #17203. If anyone has a dev environment where #17203 is triggered can you test this? It bypasses transforming the offsets if the offset transform and the data transform are the same.

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

@jklymak jklymak force-pushed the fix-bypass-inverse-collection branch from 5f878c1 to 20ab0fa Compare April 22, 2020 00:43
@jklymak jklymak force-pushed the fix-bypass-inverse-collection branch from 20ab0fa to 229b49e Compare April 22, 2020 01:10
@jklymak jklymak force-pushed the fix-bypass-inverse-collection branch from 229b49e to a53833e Compare April 22, 2020 01:13
@QuLogic
Copy link
Member

QuLogic commented Apr 22, 2020

This works for me, though it appears tests are not happy about it. Never mind, seems you've pushed a fix.

Copy link
Member

@dstansby dstansby left a comment

Choose a reason for hiding this comment

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

👍 works for me, thanks for the quick investigation and fix!

@jklymak jklymak added this to the v3.2.2 milestone Apr 22, 2020
@anntzer
Copy link
Contributor

anntzer commented Apr 22, 2020

Untested, but I think something along the lines of the following may work too, be simpler, and cover more cases?

diff --git i/lib/matplotlib/collections.py w/lib/matplotlib/collections.py
index 181e46b45..55a681000 100644
--- i/lib/matplotlib/collections.py
+++ w/lib/matplotlib/collections.py
@@ -211,8 +211,6 @@ class Collection(artist.Artist, cm.ScalarMappable):
             # we may have transform.contains_branch(transData) but not
             # transforms.get_affine().contains_branch(transData).  But later,
             # be careful to only apply the affine part that remains.
-        if not transOffset.is_affine:
-            offsets = transOffset.transform_non_affine(offsets)
 
         if isinstance(offsets, np.ma.MaskedArray):
             offsets = offsets.filled(np.nan)
@@ -226,7 +224,8 @@ class Collection(artist.Artist, cm.ScalarMappable):
                 # also use this algorithm (like streamplot).
                 result = mpath.get_path_collection_extents(
                     transform.get_affine(), paths, self.get_transforms(),
-                    offsets, transOffset.get_affine().frozen())
+                    transOffset.transform_non_affine(offsets),
+                    transOffset.get_affine().frozen())
                 return result.transformed(transData.inverted())
             if not self._offsetsNone:
                 # this is for collections that have their paths (shapes)
@@ -234,14 +233,13 @@ class Collection(artist.Artist, cm.ScalarMappable):
                 # (i.e. like scatter). We can't uniquely set limits based on
                 # those shapes, so we just set the limits based on their
                 # location.
-                # Finish the transform:
-                offsets = (transOffset.get_affine() +
-                           transData.inverted()).transform(offsets)
+                offsets = (transOffset - transData).transform(offsets)
                 offsets = np.ma.masked_invalid(offsets)
                 if not offsets.mask.all():
                     points = np.row_stack((offsets.min(axis=0),
                                            offsets.max(axis=0)))
                     return transforms.Bbox(points)
+
         return transforms.Bbox.null()
 
     def get_window_extent(self, renderer):

The point is that transOffset - transData will check whether transOffset and transData contain the same subtransform and if so will cancel them out exactly (that's how Transform.__sub__ is implemented). So this would work even if e.g. transOffset = someothertransform + transData or whatnot.

@jklymak
Copy link
Member Author

jklymak commented Apr 22, 2020

I really don't understand why transforms were implemented with add and minus to really mean multiplication and multiplication by the inverse. Worse, these operators are, as far as I can tell, completely undocumented. So I'm not in favour of adding them to the code until this situation improves.

@dstansby
Copy link
Member

I really don't understand why transforms were implemented with add and minus to really mean multiplication and multiplication by the inverse. Worse, these operators are, as far as I can tell, completely undocumented. So I'm not in favour of adding them to the code until this situation improves.

Woah, that is confusing! I was looking at your changelog and got really confused with what was happening because it never crossed my mind that + could ever mean multiplying two transformations together.

@jklymak
Copy link
Member Author

jklymak commented Apr 22, 2020

I should stipulate that __sub__ and __add__ are documented, but as far as I can tell they don't come up in the webpage docs.

@anntzer
Copy link
Contributor

anntzer commented Apr 22, 2020

There was one transform operator in the code before (an addition: transOffset.get_affine() + transData.inverted()); there's still one in my proposed patch (transOffset - transData), so in that sense it doesn't make things worse. And my proposed patch avoids splitting the affine and non-affine parts of transOffset unless we really need to do it.

As to why multiplication and division are implemented as addition and subtraction: I didn't make that choice so I can't tell, but it's not a completely silly choice either. In particular, if you follow the matrix multiplication semantics, you'd have A + B [current] === B * A [matrix], i.e. apply B then A. But then you simply don't have a good way to write A - B (apply A then the inverse of B) because there's no leftdivide in python. Sure you could write (1/B) * A but then it becomes much more awkward to get the "enforced cancelling" when A = B (unless 1/B returns a special object which knows its inverse), and even then (1/B) * A (or B**-1 * A) doesn't read that nicely either.

@jklymak
Copy link
Member Author

jklymak commented Apr 22, 2020

There was one transform operator in the code before

Ha ha, guess why?

I've implement it with the minus sign and put a comment. The extra code in transform to handle the inverse properly does seem quite helpful.

@jklymak
Copy link
Member Author

jklymak commented Apr 22, 2020

... added a simple test as well..

@jklymak jklymak added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Apr 27, 2020
@anntzer anntzer merged commit f091a4d into matplotlib:master Apr 27, 2020
@lumberbot-app
Copy link

lumberbot-app bot commented Apr 27, 2020

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
$ git checkout v3.2.x
$ git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
$ git cherry-pick -m1 f091a4d92e0a73e702e15c172c884d35c9a478a2
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
$ git commit -am 'Backport PR #17206: FIX: bypass inverse in collection'
  1. Push to a named branch :
git push YOURFORK v3.2.x:auto-backport-of-pr-17206-on-v3.2.x
  1. Create a PR against branch v3.2.x, I would have named this PR:

"Backport PR #17206 on branch v3.2.x"

And apply the correct labels and milestones.

Congratulation you did some good work ! Hopefully your backport PR will be tested by the continuous integration and merged soon!

If these instruction are inaccurate, feel free to suggest an improvement.

@jklymak
Copy link
Member Author

jklymak commented Apr 27, 2020

OK, I have real troubles manually backporting:

$ git checkout v3.2.x
error: pathspec 'v3.2.x' did not match any file(s) known to git

Like maybe this PR isn't worth back porting, but the instructions used to work

@kannes
Copy link

kannes commented Apr 27, 2020

OK, I have real troubles manually backporting:

$ git checkout v3.2.x
error: pathspec 'v3.2.x' did not match any file(s) known to git

Like maybe this PR isn't worth back porting, but the instructions used to work

Works fine for me on a fresh git clone, maybe you need to fetch the remote branch to your repo?

QuLogic added a commit that referenced this pull request Apr 27, 2020
…3.2.x

Merge pull request #17206 from jklymak/fix-bypass-inverse-collection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Subplots using bad axis limits in 3.2
5 participants