Skip to content

Fix datashader failing with single-category Categorical#539

Open
timtreis wants to merge 5 commits intomainfrom
fix/datashader-single-category
Open

Fix datashader failing with single-category Categorical#539
timtreis wants to merge 5 commits intomainfrom
fix/datashader-single-category

Conversation

@timtreis
Copy link
Member

@timtreis timtreis commented Mar 2, 2026

Summary

  • Fixes render_labels with DataShader fails when coloring by a pd.Categorical with only 1 category #483: datashader raises ValueError when rendering shapes/points with a pd.Categorical column that has only 1 category
  • Changed color_key guard from > 1 to > 0 so a single-category Categorical still produces a valid color key for datashader
  • Changed groups guard from > 1 to >= 1 so a single group uses categorical aggregation instead of falling back to count-based heatmap
  • Used _hex_no_alpha in points color_key construction for consistency with the shapes path

Test plan

  • Added test_plot_datashader_single_category for shapes
  • Added test_plot_datashader_single_category_points for points
  • Verified existing non-image-comparison tests still pass
  • Pre-existing image comparison failures are unrelated (also fail on main)

🤖 Generated with Claude Code

timtreis and others added 2 commits March 3, 2026 00:54
When a pd.Categorical column has only 1 category, datashader's ds.by()
still produces a 3D DataArray requiring a color_key. The >1 guard was
causing color_key to be None and groups to fall back to count-based
aggregation, both leading to errors or incorrect output.

Changes:
- Change color_key guard from >1 to >0 for both shapes and points
- Change groups guard from >1 to >=1 for both shapes and points
- Use _hex_no_alpha in points color_key construction for consistency

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
color_vector.categories.values can contain named colors like
"lightgreen", not just hex strings. Only apply _hex_no_alpha when the
value starts with "#".

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@timtreis timtreis marked this pull request as ready for review March 3, 2026 00:06
timtreis and others added 3 commits March 3, 2026 01:10
… test

The test_datashader_single_group_named_color test didn't reproduce a
failure on main (it only caught an intermediate regression within this
PR), so remove it. Add the issue URL and detailed docstrings to the two
tests that do reproduce the #483 bug on main.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The condition `groups is None or len(groups) >= 1` was always true
since groups is either None or a non-empty list. Simplify to just
`col_for_color is not None`, which makes the intent clearer: use
categorical/reduction aggregation when there's a color column,
otherwise fall back to plain count aggregation.

Also note: render_labels does not use datashader, so despite the #483
issue title mentioning labels, this bug only affects shapes and points.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The color_key construction logic was duplicated between _render_shapes
and _render_points. Extract into a shared helper that also caches the
categories lookup and keeps the hex-vs-named-color guard in one place.

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

codecov-commenter commented Mar 3, 2026

Codecov Report

❌ Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 84.00%. Comparing base (1554eb2) to head (3539fc7).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/spatialdata_plot/pl/render.py 90.90% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #539      +/-   ##
==========================================
+ Coverage   83.83%   84.00%   +0.16%     
==========================================
  Files           8        9       +1     
  Lines        2568     2600      +32     
==========================================
+ Hits         2153     2184      +31     
- Misses        415      416       +1     
Files with missing lines Coverage Δ
src/spatialdata_plot/pl/render.py 88.90% <90.90%> (+0.10%) ⬆️

... and 5 files with indirect coverage changes

🚀 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.

render_labels with DataShader fails when coloring by a pd.Categorical with only 1 category

2 participants