Skip to content

Fix categorical color mapping across coordinate systems#547

Merged
timtreis merged 1 commit intomainfrom
fix/425-categorical-color-mapping-across-cs
Mar 12, 2026
Merged

Fix categorical color mapping across coordinate systems#547
timtreis merged 1 commit intomainfrom
fix/425-categorical-color-mapping-across-cs

Conversation

@timtreis
Copy link
Member

Summary

  • Fixes ValueError: zip() argument 2 is shorter than argument 1 when plotting shapes colored by a categorical column across multiple coordinate systems with different category counts
  • Calls remove_unused_categories() early in _generate_base_categorial_color_mapping and _extract_colors_from_table_uns so that categories and stored .uns colors stay aligned
  • Uses category position in the full table (not the subset) for color lookup, ensuring consistent coloring across coordinate systems

Fixes #425

Test plan

  • Verify plotting shapes with color="leiden" across multiple coordinate systems where category counts differ no longer crashes
  • Verify colors remain consistent (same category gets same color) regardless of which coordinate system is being rendered
  • Verify no regression when all categories are present in every coordinate system

🤖 Generated with Claude Code

…rdinate systems

When plotting shapes colored by a categorical column across multiple
coordinate systems, the color_source_vector could carry unused categories
from other coordinate systems. This caused a length mismatch between
categories and stored colors in adata.uns, leading to a ValueError in
strict zip calls.

The fix removes unused categories early in both _generate_base_categorial_color_mapping
and _extract_colors_from_table_uns, and maps colors by the category's position in the
full table (not the subset) to ensure consistent coloring across coordinate systems.

Fixes #425

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov-commenter
Copy link

codecov-commenter commented Mar 10, 2026

Codecov Report

❌ Patch coverage is 42.85714% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.86%. Comparing base (a2bb56b) to head (ec09641).

Files with missing lines Patch % Lines
src/spatialdata_plot/pl/utils.py 42.85% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #547      +/-   ##
==========================================
- Coverage   84.00%   83.86%   -0.15%     
==========================================
  Files           9        9              
  Lines        2601     2609       +8     
==========================================
+ Hits         2185     2188       +3     
- Misses        416      421       +5     
Files with missing lines Coverage Δ
src/spatialdata_plot/pl/utils.py 79.08% <42.85%> (-0.23%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@timtreis timtreis merged commit f2bfa3f into main Mar 12, 2026
4 checks passed
timtreis added a commit that referenced this pull request Mar 12, 2026
- _coerce_categorical_source: remove unreachable dd.Series, np.ndarray,
  and pd.Categorical branches (callers always pass pd.Series). 27→4 lines.
- _build_datashader_color_key: replace O(n*k) flatnonzero scans with a
  single pass over codes. Also handles NaN sentinel via the same fallback.
- _build_alignment_dtype_hint: remove overly broad exception catching
  (TypeError, AttributeError), redundant hasattr checks, and the
  color-index comparison that duplicates the element-index check. 32→12 lines.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Render shapes fails for categoricals of different length across coordinate systems

2 participants