Skip to content

Corrected behavior of 2D alpha array - #1543

Merged
greglucas merged 2 commits into
SciTools:masterfrom
kdpenner:fix-alpha-array
May 20, 2020
Merged

greglucas merged 2 commits into
SciTools:masterfrom
kdpenner:fix-alpha-array

Conversation

@kdpenner

Copy link
Copy Markdown
Contributor

Rationale

imshow() does not warp a 2D alpha array; it passes the unwarped alpha array to matplotlib's imshow(), which results in the display of an unwarped image.

Implications

With these changes imshow() passes a warped 2D alpha array.

@greglucas greglucas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Overall I think this update makes sense. Do you have a test case you can add to show that this does what you are expecting with 2d alpha?

Comment thread lib/cartopy/mpl/geoaxes.py Resolved
Comment thread lib/cartopy/mpl/geoaxes.py Resolved
@kdpenner

Copy link
Copy Markdown
Contributor Author

Overall I think this update makes sense. Do you have a test case you can add to show that this does what you are expecting with 2d alpha?

Yes. Here is a test with no warping of alpha:

t

Here is a test with warping of alpha:

t1

@greglucas

Copy link
Copy Markdown
Contributor

Yep, I know what you were getting at and the improvement :) I meant to add that under the tests/ directory. Can you add something like that to make sure that your code is working and someone doesn't break it in the future?

@kdpenner

Copy link
Copy Markdown
Contributor Author

Sure, I'll make a test for the next commit.

@kdpenner

kdpenner commented May 4, 2020

Copy link
Copy Markdown
Contributor Author

Writing a "good" test is harder than I expected. (As always.)

My thought was to test select alphas from the output of imshow. The AxesImage class has a method, make_image, which returns an RGBA array. Its values are integers between 0 and 255. Alpha arrays should have floats between 0 and 1. And the transformation between image value and RGBA value requires both a normalization and a colormap. I think I can't assert that the A in the RGBA output is the alpha from the input. Ideas?

@greglucas

Copy link
Copy Markdown
Contributor

You could get the data out of the returned object like:

image = ax.imshow(...)
arr = image.get_array()
alpha = image.get_alpha()

assert arr.shape == alpha.shape

Then you can make sure that the regridded data is the shape expected and that alpha is the same shape as that, which is the real issue here I think. You probably don't need an explicit image if you just want to throw in some fake data and some fake alphas that you could test this against too.

@kdpenner

kdpenner commented May 5, 2020

Copy link
Copy Markdown
Contributor Author

I agree that the shape of the alpha array is the issue---get_array and get_alpha are undocumented for an AxesImage object, so I overlooked them. Your proposed test will work, I'll write it up.

@greglucas

Copy link
Copy Markdown
Contributor

I didn't realize those methods were undocumented! It looks like other people have run into the issue with a lack of documentation on that class as well: matplotlib/matplotlib#15503

Comment thread lib/cartopy/tests/mpl/test_alpha.py Resolved
Comment thread lib/cartopy/tests/mpl/test_alpha.py Resolved
Comment thread lib/cartopy/tests/mpl/test_alpha.py Resolved
Comment thread lib/cartopy/tests/mpl/test_alpha.py Resolved
Comment thread lib/cartopy/tests/mpl/test_alpha.py Resolved
Comment thread lib/cartopy/tests/mpl/test_alpha.py Resolved
Comment thread lib/cartopy/tests/mpl/test_alpha.py Resolved
Comment thread lib/cartopy/tests/mpl/test_alpha.py Resolved
Comment thread lib/cartopy/tests/mpl/test_alpha.py Resolved
Comment thread lib/cartopy/tests/mpl/test_alpha.py Resolved
Comment thread lib/cartopy/tests/mpl/test_alpha.py Resolved

@greglucas greglucas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like CI is failing because the new file you added doesn't have a header. The test should be under a current file and then you won't run into that license failure. Probably under:
https://github.com/SciTools/cartopy/blob/master/lib/cartopy/tests/mpl/test_img_transform.py
or
https://github.com/SciTools/cartopy/blob/master/lib/cartopy/tests/mpl/test_images.py

and then call it test_alpha_2d_reshape or something along those lines so we know what it is trying to test.

Can you squash your commits into a few good commit messages too?

@kdpenner

Copy link
Copy Markdown
Contributor Author

I think the rebase worked. (I haven't done it before.)

@greglucas greglucas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like you got it all done properly, nice work. Looks good to me, thanks @kdpenner!

@greglucas
greglucas merged commit e34e015 into SciTools:master May 20, 2020
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.

4 participants