Add make_palette and make_palette_from_data for palette generation#581
Merged
Add make_palette and make_palette_from_data for palette generation#581
Conversation
) Add two public functions for generating categorical color palettes: - `make_palette(n)`: produce n colors from a source palette, optionally reordered for maximum perceptual contrast or colorblind accessibility. - `make_palette_from_data(sdata, element, color)`: like make_palette but derives categories from a SpatialData element and supports spatially- aware assignment (spaco-inspired) that maximizes contrast between spatially interleaved categories. Both functions share the same method vocabulary: "default", "contrast", "colorblind", "protanopia", "deuteranopia", "tritanopia" (non-spatial), plus "spaco", "spaco_colorblind", "spaco_protanopia", etc. (spatial, only in make_palette_from_data). The palette parameter accepts None (scanpy defaults), named palettes ("okabe_ito"), matplotlib colormap names ("tab10"), or explicit color lists. Also adds dict[str, str] palette support to render_shapes, render_points, and render_labels, enabling reuse of generated palettes across multiple render calls. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Fix test_plot_dict_palette_hex_labels to use a hand-built dict instead of make_palette_from_data, since labels elements don't have extractable coordinates. Add 5 CI-generated reference images for palette visual tests. The hex_labels reference image will be generated on the next CI run. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Vectorize _spatial_interlacement with numpy (eliminates Python double-loop over cells × neighbors) - Select only needed columns before .compute() on dask points (avoids materializing entire dataframe) - Fix ListedColormap sampling: use integer indices for qualitative colormaps like tab10 instead of linspace (which wraps and duplicates) - Fix categorical dtype check: use isinstance(dtype, CategoricalDtype) instead of fragile hasattr(series, "cat") - Add n==2 early exit in optimizer (only 2 permutations to try) - Refactor tests: module-scoped fixtures, parametrize across methods and palette sources, merge redundant test classes Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The stash application accidentally reverted the resolved_dpi logic from commit 303140c, causing user figure DPI to be silently overwritten with rcParams defaults. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #581 +/- ##
==========================================
+ Coverage 75.63% 76.28% +0.64%
==========================================
Files 10 11 +1
Lines 2996 3259 +263
Branches 702 756 +54
==========================================
+ Hits 2266 2486 +220
- Misses 444 468 +24
- Partials 286 305 +19
🚀 New features to boost your workflow:
|
- _get_labels_from_table now joins on instance keys to guarantee coord-label alignment (was returning table rows in table order, silently misaligning with element coordinates) - Error when multiple tables annotate the same element; accept table_name= parameter to disambiguate - Dict palette path in _get_categorical_color_mapping now applies groups filtering (was silently ignoring groups= with dict palettes) - Validate dict palette color values with is_color_like() in _type_check_params (was passing invalid colors through to matplotlib) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Summary
Closes #210.
make_palette(n)— produce n colors from a source palette, optionally reordered for maximum perceptual contrast or colorblind accessibility.make_palette_from_data(sdata, element, color)— derives categories from a SpatialData element. Supports allmake_palettemethods plus spatially-aware assignment (spaco-inspired) that maximizes contrast between spatially interleaved categories.dict[str, str]palette support torender_shapes,render_points, andrender_labels, enabling reuse of generated palettes across multiple render calls.API overview
Methods
make_palettemake_palette_from_data"default""contrast""colorblind""protanopia""deuteranopia""tritanopia""spaco""spaco_colorblind""spaco_*"Implementation details
palette="okabe_ito".palette=(e.g.,"tab10","Set2").