Skip to content

Commit e62e952

Browse files
committed
fix: tolerate scanpy 1.13 relocation of _tools/_utils plotting helpers
Follow-up to #759 (which shimmed the palettes). scanpy 1.13 also moved the private plotting helpers from scanpy.plotting.{_tools,_utils} to scanpy.plotting.legacy.*, so `import spatialdata_plot` still fails on pre-release scanpy at `_add_categorical_legend` / `add_colors_for_categorical_sample_annotation`. Rename the palettes shim to _scanpy_compat and extend it to import all three helper groups from the new path with a fallback to the old, then route every call site (render.py, utils.py, _color.py, _palette.py) through it. Behaviour is unchanged; the reliance on scanpy internals now lives in one version-tolerant module.
1 parent c6054fb commit e62e952

6 files changed

Lines changed: 40 additions & 20 deletions

File tree

src/spatialdata_plot/pl/_color.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from numpy.random import default_rng
2929
from pandas.api.types import CategoricalDtype, is_bool_dtype, is_numeric_dtype, is_string_dtype
3030
from pandas.core.arrays.categorical import Categorical
31-
from scanpy.plotting._utils import add_colors_for_categorical_sample_annotation
3231
from skimage.color import label2rgb
3332
from skimage.morphology import erosion, footprint_rectangle
3433
from skimage.util import map_array
@@ -42,7 +41,12 @@
4241
)
4342

4443
from spatialdata_plot._logging import logger
45-
from spatialdata_plot.pl._scanpy_palettes import default_20, default_28, default_102
44+
from spatialdata_plot.pl._scanpy_compat import (
45+
add_colors_for_categorical_sample_annotation,
46+
default_20,
47+
default_28,
48+
default_102,
49+
)
4650
from spatialdata_plot.pl.render_params import (
4751
CmapParams,
4852
Color,

src/spatialdata_plot/pl/_palette.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from matplotlib.colors import ListedColormap, to_hex, to_rgb
2222
from matplotlib.pyplot import colormaps as mpl_colormaps
2323

24-
from spatialdata_plot.pl._scanpy_palettes import default_20, default_28, default_102
24+
from spatialdata_plot.pl._scanpy_compat import default_20, default_28, default_102
2525

2626
if TYPE_CHECKING:
2727
import spatialdata as sd
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Version-tolerant access to scanpy internals used by spatialdata-plot.
2+
3+
scanpy 1.13 relocated the default palettes and several private plotting helpers from
4+
``scanpy.plotting.{palettes,_tools,_utils}`` to ``scanpy.plotting.legacy.*``. The values and
5+
behaviour are unchanged, so we import from whichever path the installed scanpy exposes and
6+
re-export from a single place. This keeps spatialdata-plot working on scanpy both < and >= 1.13
7+
and confines the reliance on scanpy internals to one module.
8+
"""
9+
10+
try: # scanpy >= 1.13
11+
from scanpy.plotting.legacy.palettes import default_20, default_28, default_102
12+
except ImportError: # scanpy < 1.13
13+
from scanpy.plotting.palettes import default_20, default_28, default_102
14+
15+
try: # scanpy >= 1.13
16+
from scanpy.plotting.legacy._tools.scatterplots import _add_categorical_legend
17+
except ImportError: # scanpy < 1.13
18+
from scanpy.plotting._tools.scatterplots import _add_categorical_legend
19+
20+
try: # scanpy >= 1.13
21+
from scanpy.plotting.legacy._utils import add_colors_for_categorical_sample_annotation
22+
except ImportError: # scanpy < 1.13
23+
from scanpy.plotting._utils import add_colors_for_categorical_sample_annotation
24+
25+
__all__ = [
26+
"_add_categorical_legend",
27+
"add_colors_for_categorical_sample_annotation",
28+
"default_20",
29+
"default_28",
30+
"default_102",
31+
]

src/spatialdata_plot/pl/_scanpy_palettes.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/spatialdata_plot/pl/render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from matplotlib.cm import ScalarMappable
2222
from matplotlib.colors import BoundaryNorm, Colormap, ListedColormap, Normalize, to_rgba_array
2323
from scanpy._settings import settings as sc_settings
24-
from scanpy.plotting._tools.scatterplots import _add_categorical_legend
2524
from spatialdata import get_extent, get_values
2625
from spatialdata.models import PointsModel, ShapesModel, get_table_keys
2726
from spatialdata.transformations import set_transformation
@@ -60,6 +59,7 @@
6059
_scale_geometries,
6160
_validate_polygons,
6261
)
62+
from spatialdata_plot.pl._scanpy_compat import _add_categorical_legend
6363
from spatialdata_plot.pl._validate import (
6464
_check_obs_var_shadow,
6565
)

src/spatialdata_plot/pl/utils.py

Lines changed: 1 addition & 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._tools.scatterplots import _add_categorical_legend
3534
from spatialdata import (
3635
SpatialData,
3736
get_element_annotators,
@@ -56,7 +55,7 @@
5655
from xarray import DataArray, DataTree
5756

5857
from spatialdata_plot._logging import logger
59-
from spatialdata_plot.pl._scanpy_palettes import default_102
58+
from spatialdata_plot.pl._scanpy_compat import _add_categorical_legend, default_102
6059
from spatialdata_plot.pl.render_params import (
6160
Color,
6261
ColorbarSpec,

0 commit comments

Comments
 (0)