Skip to content

Fix unused cmap in render_points #432

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 3 commits into from
Mar 22, 2025
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
2 changes: 2 additions & 0 deletions src/spatialdata_plot/pl/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,9 @@ def _render_points(
palette=palette,
na_color=default_color,
cmap_params=render_params.cmap_params,
alpha=render_params.alpha,
table_name=table_name,
render_type="points",
)

# color_source_vector is None when the values aren't categorical
Expand Down
20 changes: 19 additions & 1 deletion src/spatialdata_plot/pl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,10 @@ def _set_color_source_vec(
groups: list[str] | str | None = None,
palette: list[str] | str | None = None,
cmap_params: CmapParams | None = None,
alpha: float = 1.0,
table_name: str | None = None,
table_layer: str | None = None,
render_type: Literal["points"] | None = None,
) -> tuple[ArrayLike | pd.Series | None, ArrayLike, bool]:
if value_to_plot is None and element is not None:
color = np.full(len(element), na_color)
Expand Down Expand Up @@ -757,9 +759,12 @@ def _set_color_source_vec(
adata=sdata.table,
cluster_key=value_to_plot,
color_source_vector=color_source_vector,
cmap_params=cmap_params,
alpha=alpha,
groups=groups,
palette=palette,
na_color=na_color,
render_type=render_type,
)

color_source_vector = color_source_vector.set_categories(color_mapping.keys())
Expand Down Expand Up @@ -912,15 +917,28 @@ def _get_categorical_color_mapping(
na_color: ColorLike,
cluster_key: str | None = None,
color_source_vector: ArrayLike | pd.Series[CategoricalDtype] | None = None,
cmap_params: CmapParams | None = None,
alpha: float = 1,
groups: list[str] | str | None = None,
palette: list[str] | str | None = None,
render_type: Literal["points"] | None = None,
) -> Mapping[str, str]:
if not isinstance(color_source_vector, Categorical):
raise TypeError(f"Expected `categories` to be a `Categorical`, but got {type(color_source_vector).__name__}")

if isinstance(groups, str):
groups = [groups]

if not palette and render_type == "points" and cmap_params is not None and not cmap_params.cmap_is_default:
palette = cmap_params.cmap

color_idx = color_idx = np.linspace(0, 1, len(color_source_vector.categories))
if isinstance(palette, ListedColormap):
palette = [to_hex(x) for x in palette(color_idx, alpha=alpha)]
elif isinstance(palette, LinearSegmentedColormap):
palette = [to_hex(palette(x, alpha=alpha)) for x in color_idx] # type: ignore[attr-defined]
return dict(zip(color_source_vector.categories, palette, strict=True))

if isinstance(palette, str):
palette = [palette]

Expand Down Expand Up @@ -2011,7 +2029,7 @@ def _is_coercable_to_float(series: pd.Series) -> bool:


def _ax_show_and_transform(
array: MaskedArray[tuple[int, ...], Any],
array: MaskedArray[tuple[int, ...], Any] | npt.NDArray[Any],
trans_data: CompositeGenericTransform,
ax: Axes,
alpha: float | None = None,
Expand Down
6 changes: 3 additions & 3 deletions tests/pl/test_render_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def test_plot_can_color_labels_by_continuous_variable(self, sdata_blobs: Spatial
sdata_blobs.pl.render_labels("blobs_labels", color="channel_0_sum").pl.show()

def test_plot_can_color_labels_by_categorical_variable(self, sdata_blobs: SpatialData):
max_col = sdata_blobs.table.to_df().idxmax(axis=1)
max_col = pd.Categorical(max_col, categories=sdata_blobs.table.to_df().columns, ordered=True)
sdata_blobs.table.obs["which_max"] = max_col
max_col = sdata_blobs["table"].to_df().idxmax(axis=1)
max_col = pd.Categorical(max_col, categories=sdata_blobs["table"].to_df().columns, ordered=True)
sdata_blobs["table"].obs["which_max"] = max_col

sdata_blobs.pl.render_labels("blobs_labels", color="which_max").pl.show()

Expand Down
Loading