Skip to content

Fix palette and groups #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/spatialdata_plot/pl/basic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import sys
import warnings
from collections import OrderedDict
from copy import deepcopy
from pathlib import Path
Expand Down Expand Up @@ -762,7 +763,9 @@ def show(
# go through tree

for i, cs in enumerate(coordinate_systems):
sdata = self._copy()
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
sdata = self._copy()
_, has_images, has_labels, has_points, has_shapes = (
cs_contents.query(f"cs == '{cs}'").iloc[0, :].values.tolist()
)
Expand Down Expand Up @@ -851,7 +854,7 @@ def show(
source=sdata[table],
target=sdata[table],
key=params_copy.color[index],
palette=params_copy.palette,
palette=params_copy.palette[index],
)

rasterize = (params_copy.scale is None) or (
Expand Down
6 changes: 3 additions & 3 deletions src/spatialdata_plot/pl/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,8 @@ def _render_labels(
element_index=i,
element_name=e,
value_to_plot=cast(list[str], render_params.color)[i],
groups=render_params.groups,
palette=render_params.palette,
groups=render_params.groups[i],
palette=render_params.palette[i],
na_color=render_params.cmap_params.na_color,
cmap_params=render_params.cmap_params,
table_name=cast(str, table_name),
Expand Down Expand Up @@ -686,7 +686,7 @@ def _render_labels(
adata=table,
value_to_plot=cast(list[str], render_params.color)[i],
color_source_vector=color_source_vector,
palette=render_params.palette,
palette=render_params.palette[i],
alpha=render_params.fill_alpha,
na_color=render_params.cmap_params.na_color,
legend_fontsize=legend_params.legend_fontsize,
Expand Down
7 changes: 5 additions & 2 deletions src/spatialdata_plot/pl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,11 +628,11 @@ def _set_color_source_vec(
color_source_vector = pd.Categorical(color_source_vector) # convert, e.g., `pd.Series`
categories = color_source_vector.categories

if groups is not None:
if groups is not None and groups[0] is not None:
color_source_vector = color_source_vector.remove_categories(categories.difference(groups))
categories = groups

if groups is not None:
if groups is not None and groups[0] is not None:
if isinstance(palette, list):
palette_input = palette[0] if palette[0] is None else palette
elif palette is not None and isinstance(palette, list):
Expand Down Expand Up @@ -717,6 +717,7 @@ def _get_palette(
palette: ListedColormap | str | list[str] | None = None,
alpha: float = 1.0,
) -> Mapping[str, str] | None:
palette = None if isinstance(palette, list) and palette[0] is None else palette
if adata is not None and palette is None:
try:
palette = adata.uns[f"{cluster_key}_colors"] # type: ignore[arg-type]
Expand Down Expand Up @@ -772,6 +773,7 @@ def _maybe_set_colors(
palette = ListedColormap([palette])
if isinstance(palette, ListedColormap): # `scanpy` requires it
palette = cycler(color=palette.colors)
palette = None
add_colors_for_categorical_sample_annotation(target, key=key, force_update_colors=True, palette=palette)


Expand Down Expand Up @@ -809,6 +811,7 @@ def _decorate_axs(
# order of clusters should agree to palette order
clusters = color_source_vector.unique()
clusters = clusters[~clusters.isnull()]
palette = None if isinstance(palette, list) and palette[0] else palette
palette = _get_palette(
adata=adata, cluster_key=value_to_plot, categories=clusters, palette=palette, alpha=alpha
)
Expand Down