Skip to content

ENH: support alpha arrays in collections #6268

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 2 commits into from
Sep 12, 2020

Conversation

efiring
Copy link
Member

@efiring efiring commented Apr 4, 2016

This started by supporting alpha arrays in collections with color mapping but it now also works when a single color is provided, or when a list of color specifications is given.

Tests are included.
There is an entry in next_whats_new.

@efiring efiring added this to the 2.1 (next point release) milestone Apr 4, 2016
@WeatherGod
Copy link
Member

This doesn't relate to the idea of 2D colormaps, right? (The second
dimension being alpha, typically indicating level of confidence in the data)

On Sun, Apr 3, 2016 at 10:24 PM, Eric Firing notifications@github.com
wrote:

This is a possible alternative to #6250
#6250. It applies the
alpha array in Colormap.call. It probably needs quite a bit more work
for adequate documentation and argument checking at higher levels. And

tests, of course.

You can view, comment on, or merge this pull request online at:

#6268
Commit Summary

  • ENH: support alpha arrays when color mapping

File Changes

Patch Links:


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#6268

@efiring
Copy link
Member Author

efiring commented Apr 5, 2016

@WeatherGod, this could be used for that purpose; it is up to the user to generate the alpha array on a 0-1 scale based on whatever measure of level of confidence, or anything else, the user wishes to encode via transparency. The alpha is applied here after the colormapping, so it can be based on a different set of data values and a different norm; it is not done by modifying alpha in the colormap LUT. (That can also be done, but it is a completely independent and rather different operation.)

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Sep 24, 2017
@jklymak
Copy link
Member

jklymak commented May 2, 2018

I'm not sure how obsolete this one is....

@efiring efiring modified the milestones: needs sorting, v3.0 Jun 11, 2018
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 looks to need examples and tests?

@jklymak jklymak modified the milestones: v3.0, v3.1 Jul 9, 2018
@jklymak jklymak modified the milestones: v3.1.0, v3.2.0 Feb 7, 2019
@timhoffm timhoffm modified the milestones: v3.2.0, v3.3.0 Aug 16, 2019
@jklymak jklymak added the stale label May 26, 2020
@jklymak jklymak modified the milestones: v3.3.0, v3.4.0 May 26, 2020
@jklymak
Copy link
Member

jklymak commented Jul 14, 2020

@efiring, I would like this to go in. Any appetite for resurrecting?

@efiring
Copy link
Member Author

efiring commented Jul 16, 2020

OK, I will have a look.

@efiring efiring marked this pull request as ready for review September 6, 2020 23:30
@efiring efiring changed the title ENH: support alpha arrays when color mapping ENH: support alpha arrays in collections Sep 7, 2020
@efiring
Copy link
Member Author

efiring commented Sep 7, 2020

I think this is ready for review now. It actually does more than I originally intended.
codecov/project/tests is unhappy; do I need to do something about that?

y = np.arange(4)
z = np.arange(9).reshape((3, 3))
alpha = z / z.max()
alpha_flat = alpha.ravel()
Copy link
Member

Choose a reason for hiding this comment

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

So at somepoint someone will put some masked or NaN into alpha. What will happen, and what do you recommend?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fail on nan; ignore mask. I don't think it is worthwhile for us to try to propagate nans and masked arrays from the alpha input.

@jklymak jklymak self-requested a review September 8, 2020 05:00
@efiring efiring force-pushed the alpha_array branch 2 times, most recently from 9c24406 to 20b894b Compare September 8, 2020 20:51
@efiring
Copy link
Member Author

efiring commented Sep 8, 2020

Squashed, and passing tests; it's ready, as far as I can see.

self.pchanged()
self.stale = True
martist.Artist._set_alpha_for_array(self, alpha)
if np.ndim(alpha) not in (0, 2):
Copy link
Member

Choose a reason for hiding this comment

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

Do you not want to check this before calling the setter?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, I think it is better to have the more basic argument-checking from the setter before checking the number of dimensions.

Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't that leave self.alpha in an inconsistent state?

Copy link
Member Author

Choose a reason for hiding this comment

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

Why would that matter? An exception is being raised either way. Look at _ImageBase.set_data. All of the validation is being done after the attribute assignment:

        self._A = cbook.safe_masked_invalid(A, copy=True)

It looks like the set_data code would be slightly more efficient and readable if the attribute assignment were done at the end, but that's not the issue here. For set_alpha, both efficiency and logical order of validation checks are better with the ndim check after set_alpha_for_array.

Copy link
Member

Choose a reason for hiding this comment

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

To resolve this, can we easily write a test that shows how it works?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't see anything to resolve. I have already included a test for this case, raising an exception if the wrong number of dimensions is supplied for an image.

Copy link
Member

Choose a reason for hiding this comment

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

FWIW, if we already have cases of inconsistent state on invalid input, then I'm slightly less concerned about adding one more.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is impossible to do all validation before setting attributes, given independent setters, because valid alpha depends on data, and vice-versa. That's why one of the tests I added calls update_scalarmappable; that's where the validation has to be, because that is where we have both alpha and data and can cross-check them.

@efiring
Copy link
Member Author

efiring commented Sep 9, 2020

@QuLogic Thank you. I think I have addressed everything you have found so far.

Previously this was supported only for images.  Now it works for
collections, and when directly calling a colormap or to_rgba_array.
@efiring
Copy link
Member Author

efiring commented Sep 11, 2020

I have rebased again because the Travis merge test seemed to have gotten stuck (I think it actually ran and passed, but failed to report that back correctly), and a bunch of stuff was merged recently.

@efiring
Copy link
Member Author

efiring commented Sep 12, 2020

Now, after a simple rebase, testing is throwing up what I think are unrelated errors--something about jupyter on windows?

lut[:, -1] = alpha
# If the bad value is set to have a color, then we
# override its alpha just as for any other value.
alpha = (alpha * 255).astype(np.uint8)
Copy link
Member

Choose a reason for hiding this comment

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

Stall a couple of code paths not tested? (L625 and 632)

Copy link
Member

@QuLogic QuLogic left a comment

Choose a reason for hiding this comment

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

Modulo tests that @jklymak requested.

@efiring
Copy link
Member Author

efiring commented Sep 12, 2020 via email

Fixup and clarification in colors; more tests

Tweak whats_new
@jklymak jklymak merged commit 1b1afe1 into matplotlib:master Sep 12, 2020
@jklymak
Copy link
Member

jklymak commented Sep 12, 2020

Thanks @efiring This will be pretty helpful in creating graphics with two-dimensions in Z.

@efiring efiring deleted the alpha_array branch September 12, 2020 19:21
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.

7 participants