feat: colors for +inf, -inf, NaN, and masked values - #151
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #151 +/- ##
==========================================
+ Coverage 95.72% 95.78% +0.06%
==========================================
Files 168 168
Lines 2197 2231 +34
==========================================
+ Hits 2103 2137 +34
Misses 94 94 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@matthiasschabel I'm ok with this if it enables you to do things that regularly pop up in your work. My only objection would be the cognitive burden on the more casual user who doesn't particularly care to discriminate between "over" and "positive infinity". So, as long as a user not providing these values has behavior that just degrades to the old behavior, and as long as they are documented well (they are), then I'm fine with this. 👍 I think i'm most concerned with now having A couple questions. (some of these might be broken before this PR, but now is as good a time as any to address them). Do all of these over/under/inf/neg-inf values get preserved in:
(it occurs to me that some of that might not be tested) |
Thanks for the close read and for being agreeable! I do think this proposal addresses legitimate use cases and closes the circle on handling special/exceptional values in cmap. The implementation should be purely additive and takes care to degrade in the predictable/expected backward-compatible path, so transparent to current users and hopefully the cognitive overhead is minimal. When new classes are unused the routing appends fallback-resolved rows to a call-local copy of the LUT, so a class with no color of its own lands on exactly the row it lands on today.
Here's my argument in favor (in addition to the completeness argument):
None of those four channels preserve the existing
I think we should conform to matplotlib wherever it has a position:
Would you prefer that these get broken up into more granular, separate PRs or lumped in with this one? |
|
Thank you for the thorough response! Yeah I thought that might be the case (that it was broken before this PR). And I agree that we should mirror mpl where there is prior art. I suppose we should go ahead and split that fix out into a new PR. And it needn't hold this one up either. I would like to get a quick opinion from @jni on the API addition, but then this is all good by me. Thanks again! |
cmap colors three exceptional classes: under, over, and bad. Floating point data has more. Negative and positive infinity are indistinguishable from ordinary out-of-range values, and NaN is indistinguishable from a masked entry. Adds neg_inf, pos_inf, nan, and masked. Each falls back to the color its class uses now: neg_inf to under, pos_inf to over, nan and masked to bad. bad is kept as the joint fallback for both of its children, so code that sets it is unaffected and either child may be set alone. Routing appends four fallback-resolved rows to a call-local copy of the over/under LUT, so a class with no color of its own lands on exactly the row it lands on now. Colormap.lut() is unchanged. The infinity masks are taken before the input is scaled by N: that multiply overflows large finite values to infinity (float16 65504 does it), and those are out of range rather than infinite. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Reviewed-By: Codex (gpt-5.6-sol, reasoning effort xhigh)
0587c89 to
9380611
Compare
Closes #144.
Adds four optional colors so exceptional float values can be told apart:
neg_inf,pos_inf,nan, andmasked. Today-infis indistinguishable from any other under-rangevalue, and NaN is indistinguishable from a masked entry. Log transformed signal data and
saturated logistic regression both produce infinities worth marking rather than blending
into the ends of the scale.
Each new color falls back to the one its class uses now, so nothing changes for an existing
colormap:
neg_infunder, then the first ramp colorpos_infover, then the last ramp colornanbad, then transparentmaskedbad, then transparentbadstays the joint fallback fornanandmaskedrather than being replaced by them, socode that sets it is unaffected and either child can be set alone. A masked entry takes the
masked color whatever value it hides, as it does now.
Two things the diff does not show:
xa *= N. That multiply overflows large finite valuesto infinity (
float1665504 does at N=256), and those are out of range, not infinite.There is a test for it, because classifying after the multiply looks right and is not.
to_naparinow prefersnan_coloroverbad_colorfor napari'snan_color, since it isthe one converter target that represents the class. matplotlib's
badcovers NaN and maskedtogether, so
badis still what goes there.Depends on #150, which this branches from. Only the last commit is mine; the first two are
#150's.