Skip to content

String column names in [0, 1] are now no longer interpreted as colors #331

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

44 changes: 35 additions & 9 deletions src/spatialdata_plot/pl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@
ColorLike = Union[tuple[float, ...], str]


def _is_color_like(color: Any) -> bool:
"""Check if a value is a valid color, returns False for pseudo-bools.

For discussion, see: https://github.com/scverse/spatialdata-plot/issues/327.
matplotlib accepts strings in [0, 1] as grey-scale values - therefore,
"0" and "1" are considered valid colors. However, we won't do that
so we're filtering these out.
"""
if isinstance(color, bool):
return False
if isinstance(color, str):
try:
num_value = float(color)
if 0 <= num_value <= 1:
return False
except ValueError:
# we're not dealing with what matplotlib considers greyscale
pass

return bool(colors.is_color_like(color))


def _prepare_params_plot(
# this param is inferred when `pl.show`` is called
num_panels: int,
Expand Down Expand Up @@ -532,7 +554,15 @@ def _normalize(

perc = np.percentile(img, [pmin, pmax])

norm = (img - perc[0]) / (perc[1] - perc[0] + eps)
# Ensure perc is an array of two elements
if np.isscalar(perc):
logger.warning(
"Percentile range is too small, using the same percentile for both min "
"and max. Consider using a larger percentile range."
)
perc = np.array([perc, perc])

norm = (img - perc[0]) / (perc[1] - perc[0] + eps) # type: ignore

if clip:
norm = np.clip(norm, 0, 1)
Expand Down Expand Up @@ -727,7 +757,7 @@ def _map_color_seg(
val_im = np.squeeze(val_im, axis=0)
if "#" in str(color_vector[0]):
# we have hex colors
assert all(colors.is_color_like(c) for c in color_vector), "Not all values are color-like."
assert all(_is_color_like(c) for c in color_vector), "Not all values are color-like."
cols = colors.to_rgba_array(color_vector)
else:
cols = cmap_params.cmap(cmap_params.norm(color_vector))
Expand Down Expand Up @@ -1557,15 +1587,11 @@ def _type_check_params(param_dict: dict[str, Any], element_type: str) -> dict[st
if (contour_px := param_dict.get("contour_px")) and not isinstance(contour_px, int):
raise TypeError("Parameter 'contour_px' must be an integer.")

if (color := param_dict.get("color")) and element_type in {
"shapes",
"points",
"labels",
}:
if (color := param_dict.get("color")) and element_type in {"shapes", "points", "labels"}:
if not isinstance(color, str):
raise TypeError("Parameter 'color' must be a string.")
if element_type in {"shapes", "points"}:
if colors.is_color_like(color):
if _is_color_like(color):
logger.info("Value for parameter 'color' appears to be a color, using it as such.")
param_dict["col_for_color"] = None
else:
Expand Down Expand Up @@ -1645,7 +1671,7 @@ def _type_check_params(param_dict: dict[str, Any], element_type: str) -> dict[st
raise TypeError("Parameter 'cmap' must be a string, a Colormap, or a list of these types.")

if (na_color := param_dict.get("na_color")) != "default" and (
na_color is not None and not colors.is_color_like(na_color)
na_color is not None and not _is_color_like(na_color)
):
raise ValueError("Parameter 'na_color' must be color-like.")

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/pl/test_render_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import spatialdata_plot # noqa: F401
from anndata import AnnData
from shapely.geometry import MultiPolygon, Point, Polygon
from spatialdata import SpatialData
from spatialdata import SpatialData, deepcopy
from spatialdata.models import ShapesModel, TableModel

from tests.conftest import DPI, PlotTester, PlotTesterMeta
Expand Down Expand Up @@ -94,7 +94,7 @@ def _make_multi():
sdata.pl.render_shapes(color="val", outline=True, fill_alpha=0.3).pl.show()

def test_plot_can_color_from_geodataframe(self, sdata_blobs: SpatialData):
blob = sdata_blobs
blob = deepcopy(sdata_blobs)
blob["table"].obs["region"] = ["blobs_polygons"] * sdata_blobs["table"].n_obs
blob["table"].uns["spatialdata_attrs"]["region"] = "blobs_polygons"
blob.shapes["blobs_polygons"]["value"] = [1, 10, 1, 20, 1]
Expand Down
36 changes: 36 additions & 0 deletions tests/pl/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -23,6 +25,11 @@
# the comp. function can be accessed as `self.compare(<your_filename>, tolerance=<your_tolerance>)`
# ".png" is appended to <your_filename>, no need to set it

# replace with
# from spatialdata._types import ColorLike
# once https://github.com/scverse/spatialdata/pull/689/ is in a release
ColorLike = Union[tuple[float, ...], str]


class TestUtils(PlotTester, metaclass=PlotTesterMeta):
@pytest.mark.parametrize(
Expand All @@ -35,6 +42,18 @@ class TestUtils(PlotTester, metaclass=PlotTesterMeta):
def test_plot_set_outline_accepts_str_or_float_or_list_thereof(self, sdata_blobs: SpatialData, outline_color):
sdata_blobs.pl.render_shapes(element="blobs_polygons", outline=True, outline_color=outline_color).pl.show()

@pytest.mark.parametrize(
"colname",
["0", "0.5", "1"],
)
def test_plot_colnames_that_are_valid_matplotlib_greyscale_colors_are_not_evaluated_as_colors(
self, sdata_blobs: SpatialData, colname: str
):
sdata_blobs["table"].obs["region"] = ["blobs_polygons"] * sdata_blobs["table"].n_obs
sdata_blobs["table"].uns["spatialdata_attrs"]["region"] = "blobs_polygons"
sdata_blobs.shapes["blobs_polygons"][colname] = [1, 2, 3, 5, 20]
sdata_blobs.pl.render_shapes("blobs_polygons", color=colname).pl.show()

def test_plot_can_set_zero_in_cmap_to_transparent(self, sdata_blobs: SpatialData):
from spatialdata_plot.pl.utils import set_zero_in_cmap_to_transparent

Expand All @@ -60,6 +79,23 @@ def test_plot_can_set_zero_in_cmap_to_transparent(self, sdata_blobs: SpatialData
).pl.show(ax=axs[1], colorbar=False)


@pytest.mark.parametrize(
"color_result",
[
("0", False),
("0.5", False),
("1", False),
("#00ff00", True),
((0.0, 1.0, 0.0, 1.0), True),
],
)
def test_is_color_like(color_result: tuple[ColorLike, bool]):

color, result = color_result

assert spatialdata_plot.pl.utils._is_color_like(color) == result


@pytest.mark.parametrize(
"input_output",
[
Expand Down
Loading