Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Improve reprs of transforms. #9421

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 7 commits into from
Dec 1, 2017
Merged

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Oct 15, 2017

This patch should make debugging the transform a bit stack easier, by
changing, for example, the repr of the transform of a line created with
plot([1, 2]) from

CompositeGenericTransform(TransformWrapper(BlendedAffine2D(IdentityTransform(),IdentityTransform())), CompositeGenericTransform(BboxTransformFrom(TransformedBbox(Bbox([[-0.05, 0.95], [1.05, 2.05]]), TransformWrapper(BlendedAffine2D(IdentityTransform(),IdentityTransform())))), BboxTransformTo(TransformedBbox(Bbox([[0.125, 0.10999999999999999], [0.9, 0.88]]), BboxTransformTo(TransformedBbox(Bbox([[0.0, 0.0], [6.4, 4.8]]), Affine2D(array([[ 100.,    0.,    0.],
    [   0.,  100.,    0.],
    [   0.,    0.,    1.]]))))))))

to

CompositeGenericTransform(
    TransformWrapper(
        BlendedAffine2D(
            IdentityTransform(),
            IdentityTransform())),
    CompositeGenericTransform(
        BboxTransformFrom(
            TransformedBbox(
                Bbox(x0=-0.05, y0=0.95, x1=1.05, y1=2.05),
                TransformWrapper(
                    BlendedAffine2D(
                        IdentityTransform(),
                        IdentityTransform())))),
        BboxTransformTo(
            TransformedBbox(
                Bbox(x0=0.125, y0=0.10999999999999999, x1=0.9, y1=0.88),
                BboxTransformTo(
                    TransformedBbox(
                        Bbox(x0=0.0, y0=0.0, x1=6.4, y1=4.8),
                        Affine2D(
                            [[ 100.    0.    0.]
                             [   0.  100.    0.]
                             [   0.    0.    1.]])))))))

This will, of course, invalidate code that relied on the exact strs, but
what can you do.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 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

@anntzer anntzer force-pushed the transforms-repr branch 2 times, most recently from 3603921 to 6960534 Compare October 16, 2017 06:15
@QuLogic
Copy link
Member

QuLogic commented Oct 27, 2017

Instead of re-indenting in every parent, you could define __format__ which somehow accepts an indentation level to produce the string with the right indent from the beginning. I'm not sure if this is any better.

@anntzer
Copy link
Contributor Author

anntzer commented Oct 27, 2017

I actually had an original implementation that did that, but I think it's better not to do so, because I'd prefer keeping the format string available for (... some day) pass it down to float formatting (à la numpy/numpy#5543), rather than overloading it for what is mostly internal use.

TBH I don't think the performance of reindenting every parent is a real issue...

@QuLogic
Copy link
Member

QuLogic commented Oct 28, 2017

I added the same sort of fancy string formats to the transforms in other projections.

@@ -268,6 +267,11 @@ def __init__(self, resolution):
Transform.__init__(self)
self._resolution = resolution

def __str__(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Couldn't these be provided by a mixin class? transform_path_non_affine also looks repeated across, haven't checked if other methods are too

Copy link
Member

Choose a reason for hiding this comment

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

Possibly; I wasn't really looking for things to refactor, but I'm sure there's a lot of repetition in there.

I was also wondering if maybe we should def __repr__(self): return str(self) in some parent class instead of __repr__ = __str__ in every subclass?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point. Want to push it?

def __str__(self):
return ("{}(\n"
"{},\n"
" use_rmin={},\n"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have a slightly preference for this string not to be indented, or remove the explicit spaces and call indent_str for use_rmin too (either way). The idea was to make the source layout "look like" the final result. Same comment throughout.

@tacaswell tacaswell added this to the v2.2 milestone Oct 29, 2017
@jklymak
Copy link
Member

jklymak commented Nov 19, 2017

Where is this at? I was just looking at some Transforms, and their __repr__ sure is ugly!

@anntzer
Copy link
Contributor Author

anntzer commented Nov 19, 2017

I pushed the whitespace fix, the __repr__ = __str__-to-base-class fix, and the refactoring of common code in GeoTransforms mentioned above.
I think this is good to go (if I didn't break anything just now).

@jklymak
Copy link
Member

jklymak commented Nov 19, 2017

I tested the __repr__ and it is much better, though I'm sure I didn't test everything. All I did was

import numpy as np
import matplotlib.pyplot as plt

fig, axs = plt.subplots()
axs.plot(np.arange(10))
la = axs.set_xlabel('Test')
print(la.get_transform())

and I got:

BlendedAffine2D(
    BboxTransformTo(
        TransformedBbox(
            Bbox(x0=0.125, y0=0.10999999999999999, x1=0.9, y1=0.88),
            BboxTransformTo(
                TransformedBbox(
                    Bbox(x0=0.0, y0=0.0, x1=6.4, y1=4.8),
                    Affine2D(
                        [[ 200.    0.    0.]
                         [   0.  200.    0.]
                         [   0.    0.    1.]]))))),
    IdentityTransform())

Are there enough tests of the geo-transform code to make sure this refactor is OK?

@anntzer
Copy link
Contributor Author

anntzer commented Nov 19, 2017

I'd assume yes (and it's really just moving common code to a base class), if not that means more tests are needed anyways...

Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

Can you also add a whats_new for this?

@tacaswell
Copy link
Member

tacaswell commented Nov 19, 2017

Is there a straight forward way to at least smoke-test the reprs?

@anntzer
Copy link
Contributor Author

anntzer commented Nov 19, 2017

done (the whatsnew)

I think numpy is going to (finally) change their floating point reprs (for the better) numpy/numpy#9941 so unless we construct exactly the right transform to only use integer values we'll (soon) have some trouble getting stable outputs.

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

This is very helpful

@jklymak
Copy link
Member

jklymak commented Nov 19, 2017

code-block needs an argument... We just went through this, but I forget what the solution was!

@anntzer
Copy link
Contributor Author

anntzer commented Nov 19, 2017

Hey, I'm supposed to be the rst police :)
fixed

anntzer and others added 7 commits November 19, 2017 10:33
This patch should make debugging the transform a bit stack easier, by
changing, for example, the repr of the transform of a line created with
`plot([1, 2])` from

    CompositeGenericTransform(TransformWrapper(BlendedAffine2D(IdentityTransform(),IdentityTransform())), CompositeGenericTransform(BboxTransformFrom(TransformedBbox(Bbox([[-0.05, 0.95], [1.05, 2.05]]), TransformWrapper(BlendedAffine2D(IdentityTransform(),IdentityTransform())))), BboxTransformTo(TransformedBbox(Bbox([[0.125, 0.10999999999999999], [0.9, 0.88]]), BboxTransformTo(TransformedBbox(Bbox([[0.0, 0.0], [6.4, 4.8]]), Affine2D(array([[ 100.,    0.,    0.],
        [   0.,  100.,    0.],
        [   0.,    0.,    1.]]))))))))

to

    CompositeGenericTransform(
        TransformWrapper(
            BlendedAffine2D(
                IdentityTransform(),
                IdentityTransform())),
        CompositeGenericTransform(
            BboxTransformFrom(
                TransformedBbox(
                    Bbox(x0=-0.05, y0=0.95, x1=1.05, y1=2.05),
                    TransformWrapper(
                        BlendedAffine2D(
                            IdentityTransform(),
                            IdentityTransform())))),
            BboxTransformTo(
                TransformedBbox(
                    Bbox(x0=0.125, y0=0.10999999999999999, x1=0.9, y1=0.88),
                    BboxTransformTo(
                        TransformedBbox(
                            Bbox(x0=0.0, y0=0.0, x1=6.4, y1=4.8),
                            Affine2D(
                                [[ 100.    0.    0.]
                                [   0.  100.    0.]
                                [   0.    0.    1.]])))))))

This will, of course, invalidate code that relied on the exact strs, but
what can you do.
@jklymak
Copy link
Member

jklymak commented Nov 19, 2017

I was just saving you clicking into the test failure - you are still definitely the rst enforcer!

@QuLogic QuLogic merged commit 07d2e94 into matplotlib:master Dec 1, 2017
@anntzer anntzer deleted the transforms-repr branch December 1, 2017 08:39
@QuLogic QuLogic modified the milestones: needs sorting, v2.2.0 Feb 12, 2018
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.

4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.