Skip to content

Commit c6054fb

Browse files
authored
fix: import scanpy default palettes across scanpy versions (1.13 compat) (#759)
1 parent fddb0d7 commit c6054fb

4 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/spatialdata_plot/pl/_color.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from pandas.api.types import CategoricalDtype, is_bool_dtype, is_numeric_dtype, is_string_dtype
3030
from pandas.core.arrays.categorical import Categorical
3131
from scanpy.plotting._utils import add_colors_for_categorical_sample_annotation
32-
from scanpy.plotting.palettes import default_20, default_28, default_102
3332
from skimage.color import label2rgb
3433
from skimage.morphology import erosion, footprint_rectangle
3534
from skimage.util import map_array
@@ -43,6 +42,7 @@
4342
)
4443

4544
from spatialdata_plot._logging import logger
45+
from spatialdata_plot.pl._scanpy_palettes import default_20, default_28, default_102
4646
from spatialdata_plot.pl.render_params import (
4747
CmapParams,
4848
Color,

src/spatialdata_plot/pl/_palette.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
import pandas as pd
2121
from matplotlib.colors import ListedColormap, to_hex, to_rgb
2222
from matplotlib.pyplot import colormaps as mpl_colormaps
23-
from scanpy.plotting.palettes import default_20, default_28, default_102
23+
24+
from spatialdata_plot.pl._scanpy_palettes import default_20, default_28, default_102
2425

2526
if TYPE_CHECKING:
2627
import spatialdata as sd
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Access scanpy's built-in categorical palettes across scanpy versions.
2+
3+
scanpy relocated the ``default_20`` / ``default_28`` / ``default_102`` palettes from
4+
``scanpy.plotting.palettes`` to ``scanpy.plotting.legacy.palettes`` in 1.13. The values are
5+
frozen (identical across versions), so we import from whichever path the installed scanpy
6+
exposes and re-export them from a single place for the rest of the package.
7+
"""
8+
9+
try: # scanpy >= 1.13
10+
from scanpy.plotting.legacy.palettes import default_20, default_28, default_102
11+
except ImportError: # scanpy < 1.13
12+
from scanpy.plotting.palettes import default_20, default_28, default_102
13+
14+
__all__ = ["default_20", "default_28", "default_102"]

src/spatialdata_plot/pl/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
from pandas.api.types import CategoricalDtype, is_numeric_dtype
3232
from pandas.core.arrays.categorical import Categorical
3333
from scanpy import settings
34-
from scanpy.plotting import palettes
3534
from scanpy.plotting._tools.scatterplots import _add_categorical_legend
3635
from spatialdata import (
3736
SpatialData,
@@ -57,6 +56,7 @@
5756
from xarray import DataArray, DataTree
5857

5958
from spatialdata_plot._logging import logger
59+
from spatialdata_plot.pl._scanpy_palettes import default_102
6060
from spatialdata_plot.pl.render_params import (
6161
Color,
6262
ColorbarSpec,
@@ -451,7 +451,7 @@ def _stack_categorical_legend(
451451
# A per-entry legend past this many categories is unreadable, and scanpy builds it in O(categories^2)
452452
# (one autoscaling artist each), dominating the render — so skip it with a warning. Tied to scanpy's
453453
# default_102 palette, beyond which its *default* colors also stop being distinguishable (uniform grey).
454-
_MAX_LEGEND_CATEGORIES = len(palettes.default_102)
454+
_MAX_LEGEND_CATEGORIES = len(default_102)
455455

456456

457457
def _first_color_per_category(source: pd.Categorical, color_vector: Any) -> dict[Any, Any]:

0 commit comments

Comments
 (0)