Skip to content

Fix error when a color palette is shorter than the category list - #409

Draft
AlbertoFabbri93 wants to merge 1 commit into
scverse:mainfrom
AlbertoFabbri93:fix/categorical-labels-cached-colors-length-mismatch
Draft

AlbertoFabbri93 wants to merge 1 commit into
scverse:mainfrom
AlbertoFabbri93:fix/categorical-labels-cached-colors-length-mismatch

Conversation

@AlbertoFabbri93

Copy link
Copy Markdown

Coloring a layer by a categorical obs column paired the palette in uns["<column>_colors"] with the column's categories using a strict zip. When the table rows annotating an element do not use every category, the colors of the unused categories have already been dropped from the palette while the column still declares all of them, so the zip raised:

ValueError: zip() argument 2 is shorter than argument 1

Pair the palette with the used categories when its length matches theirs, so each category keeps its color. If the palette matches neither the full nor the used category list, raise an error naming the palette, its number of colors and the number of categories.

Add a test that colors a labels layer by a column with four categories, two of them used, and a two-color palette.

Coloring a layer by a categorical obs column paired the palette in
uns["<column>_colors"] with the column's categories using a strict zip.
When the table rows annotating an element do not use every category,
the colors of the unused categories have already been dropped from the
palette while the column still declares all of them, so the zip raised:

    ValueError: zip() argument 2 is shorter than argument 1

Pair the palette with the used categories when its length matches
theirs, so each category keeps its color. If the palette matches
neither the full nor the used category list, raise an error naming the
palette, its number of colors and the number of categories.

Add a test that colors a labels layer by a column with four categories,
two of them used, and a two-color palette.
@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.17%. Comparing base (69105e0) to head (1e466a0).
⚠️ Report is 73 commits behind head on main.

Files with missing lines Patch % Lines
src/napari_spatialdata/_widgets.py 66.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #409      +/-   ##
==========================================
+ Coverage   63.08%   72.17%   +9.09%     
==========================================
  Files          19       19              
  Lines        2636     3404     +768     
==========================================
+ Hits         1663     2457     +794     
+ Misses        973      947      -26     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant