fix: preserve colormap state through reversed(), with_extremes(), pickle, and as_dict() - #155
Open
matthiasschabel wants to merge 3 commits into
Open
fix: preserve colormap state through reversed(), with_extremes(), pickle, and as_dict()#155matthiasschabel wants to merge 3 commits into
matthiasschabel wants to merge 3 commits into
Conversation
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)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #155 +/- ##
==========================================
+ Coverage 95.72% 95.95% +0.22%
==========================================
Files 168 168
Lines 2197 2247 +50
==========================================
+ Hits 2103 2156 +53
+ Misses 94 91 -3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
matthiasschabel
marked this pull request as ready for review
August 14, 2026 19:39
Keep the proposal scoped to negative infinity, NaN, and positive infinity in addition to the existing under and over behavior. Masked arrays continue to use the existing bad color, including when they hide an infinity or NaN.
…kle, and as_dict()
None of the four channels carried the extreme colors, and reversed() and
with_extremes() dropped the interpolation mode as well. Following matplotlib
where it has a position: reversed() swaps the directional colors and keeps the
rest, with_extremes() keeps anything not passed, __reduce__ carries the
constructor state alongside the stops, and as_dict() gains optional keys that
are written only when set, so existing payloads are unchanged.
The pydantic serializer emitted a catalog colormap's qualified name
unconditionally, which discarded any added extremes and turned "viridis_r"
back into plain viridis. It now emits the name only when the name alone
rebuilds the same colormap.
Colormap("x_r") swaps the catalog record's under and over, so it agrees with
Colormap("x").reversed().
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Reviewed-By: Codex (gpt-5.6-sol, reasoning effort xhigh)
matthiasschabel
force-pushed
the
fix/state-preservation
branch
from
August 25, 2026 21:57
4302481 to
313a7d9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to the four preservation questions on #151. Depends on #151: the base commits here are
that PR's, and the diff shrinks to the preservation commit alone once it lands.
None of the four channels preserved the extreme colors, and two of them dropped the interpolation
mode as well. Verified on
mainbefore this change:reversed()passes only stops, name, and category, so anearestcolormap comes backlinearwith no extremes.
with_extremes()clears anything not repeated in the call.__reduce__carries onlycolor_stops, sopickle.loads(pickle.dumps(cm)) == cmis alreadyFalse for any colormap with
badset.as_dict()has no keys for interpolation or the extremes, and it is what both the pydanticserializer and
_json_encodeemit.Following matplotlib 3.11 where it has a position:
reversed()swapsunder/overandneg_inf/pos_inf, which name the ends they extend, andpreserves the rest.
with_extremes()preserves anything not passed. Clearing a single color now means constructinga new
Colormap, the same limitation matplotlib has. This is the one backward-incompatiblechange here.
__reduce__carries the full constructor state.as_dict()gains optional keys, written only when they hold non-default state, so existingpayloads are unchanged.
Two changes that are more than preservation:
Colormap("x_r")now swaps the catalog record'sunder/over, so it agrees withColormap("x").reversed().napari:HiLois the only affected entry. An explicitunder=/over=argument still lands on the end the caller named.Colormap("viridis", under="red")came back as plain viridis, andColormap("viridis_r")cameback unreversed because
infois looked up under the stripped name. It now emits the name onlywhen the name alone rebuilds the same
as_dict().__eq__is not usable as that guard: itignores name, identifier, and category, and
ColorStops.__eq__compares withnp.allclose, soa
cmap_kwargs={"start": 0.500001}cubehelix compares equal to the default one.What I left alone, and why:
color_stopsas the object rather thanas_dict()'s samples, so a colormapbacked by a lut function keeps the function. Routing pickle through
as_dict()shiftedlut(17, gamma=2)of a parametrized cubehelix by 2e-5.info, which is set only from a string oranother
Colormap, so an unpickled catalog colormap serializes as a dict rather than as itsname. Fixing that means putting the qualified name in
value, which would change existingpayloads for every catalog colormap.
identifierfollows the name:reversed()andshifted()rename, so it re-derives;with_extremes()does not, so it is preserved. One consequence:cm.reversed().reversed()restores the name but not an explicitly supplied identifier. Say if you would rather it were
carried through.