fix: treat unmasked nans as bad when the input is a masked array - #148
Open
matthiasschabel wants to merge 1 commit into
Open
fix: treat unmasked nans as bad when the input is a masked array#148matthiasschabel wants to merge 1 commit into
matthiasschabel wants to merge 1 commit into
Conversation
The mask and the nan test were exclusive, so a masked array's unmasked nans were never found. Such a value survived to the int cast, where it became INT_MIN, and take(..., mode="clip") clipped it to index 0: nan rendered as the colormap's first ramp color instead of `bad`, silently and plausibly. The same values in a plain ndarray render correctly, so the two input types disagreed about identical data. `bad` is documented as the color for values that are "NaN or masked" (docs/faq.md), so this restores the stated contract. The nan test is applied only to float dtypes on the masked path, keeping object-dtype masked arrays working, and the mask is combined with `|` rather than `|=` so the caller's own mask is left alone. 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 #148 +/- ##
=======================================
Coverage 95.56% 95.57%
=======================================
Files 168 168
Lines 2186 2190 +4
=======================================
+ Hits 2089 2093 +4
Misses 97 97 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
matthiasschabel
marked this pull request as ready for review
August 9, 2026 22:00
This was referenced Aug 9, 2026
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.
src/cmap/_colormap.py:409picks the mask or the nan test, never both:So a masked array's unmasked nans are never found. Such a value survives to
xa.astype(int), becomesINT_MIN, andlut.take(..., mode="clip")clips it to index 0. The nan renders as the colormap's first ramp color, silently, and the result looks like ordinary data.Colormap("viridis", bad="red")applied tonp.ma.masked_array([0.0, 0.25, nan, 1.0], mask=[True, False, False, False]):The same four values as a plain ndarray render the nan red, so the two input types disagree about identical data. The docs say
badis the color "when values are NaN or masked" (docs/faq.md), so this restores the documented behavior.Both conditions are needed to hit it:
np.ma.is_maskedreturns False for an all-false mask, so those arrays already take the nan path and are fine.Two details in the fix worth flagging:
xa.dtype.kind == "f". A numeric object-dtype masked array works today precisely because it never reachesnp.isnan, which raises on object dtype; the guard keeps that working.|, not|=, so the caller's own mask array is not written to. The test asserts that.matplotlib 3.11 has the same line in
Colormap._get_rgba_and_mask, comment included, so this is a divergence. It seems worth taking: the current output is not a decision about masked arrays, it is an integer-cast overflow clipped to index 0, and it contradicts cmap's own documentation ofbad.🤖 Generated with Claude Code