Skip to content

Fix channel str support #221

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 11 commits into from
May 17, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ __pycache__/
/.pytest_cache/
/.cache/
/data/
*failed-diff.png

# docs
/docs/generated/
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ and this project adheres to [Semantic Versioning][].
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
[semantic versioning]: https://semver.org/spec/v2.0.0.html

## [0.2.3] - tbd

### Fixed

- Fix color assignment for NaN values (#257)
- Fix channel str support #221

## [0.2.2] - 2024-05-02

### Fixed

- Fixed `fill_alpha` ignoring `alpha` channel from custom cmap
- Fix channel str support (#221)

## [0.2.1] - 2024-03-26

Expand Down
2 changes: 1 addition & 1 deletion src/spatialdata_plot/pl/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def _render_images(

# 1) Image has only 1 channel
if n_channels == 1 and not isinstance(render_params.cmap_params, list):
layer = img.sel(c=channels).squeeze()
layer = img.sel(c=channels[0]).squeeze()

if render_params.quantiles_for_norm != (None, None):
layer = _normalize(
Expand Down
2 changes: 1 addition & 1 deletion src/spatialdata_plot/pl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ def _multiscale_to_spatial_image(
data_var_keys = list(multiscale_image[optimal_scale].data_vars)
image = multiscale_image[optimal_scale][data_var_keys[0]]

return Labels2DModel.parse(image) if is_label else Image2DModel.parse(image)
return Labels2DModel.parse(image) if is_label else Image2DModel.parse(image, c_coords=image.coords["c"].values)


def _get_elements_to_be_rendered(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/_images/Labels_label_categorical_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def sdata_blobs() -> SpatialData:
return blobs()


@pytest.fixture()
def sdata_blobs_str() -> SpatialData:
return blobs(n_channels=5, c_coords=["c1", "c2", "c3", "c4", "c5"])


@pytest.fixture()
def sdata_raccoon() -> SpatialData:
return raccoon()
Expand Down
21 changes: 21 additions & 0 deletions tests/pl/test_render_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,30 @@ def test_plot_can_pass_cmap_list(self, sdata_blobs: SpatialData):
def test_plot_can_render_a_single_channel_from_image(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_images(elements="blobs_image", channel=0).pl.show()

def test_plot_can_render_a_single_channel_from_multiscale_image(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_images(elements="blobs_multiscale_image", channel=0).pl.show()

def test_plot_can_render_a_single_channel_from_image_no_el(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_images(channel=0).pl.show()

def test_plot_can_render_a_single_channel_str_from_image(self, sdata_blobs_str: SpatialData):
sdata_blobs_str.pl.render_images(elements="blobs_image", channel="c1").pl.show()

def test_plot_can_render_a_single_channel_str_from_multiscale_image(self, sdata_blobs_str: SpatialData):
sdata_blobs_str.pl.render_images(elements="blobs_multiscale_image", channel="c1").pl.show()

def test_plot_can_render_two_channels_from_image(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_images(elements="blobs_image", channel=[0, 1]).pl.show()

def test_plot_can_render_two_channels_from_multiscale_image(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_images(elements="blobs_multiscale_image", channel=[0, 1]).pl.show()

def test_plot_can_render_two_channels_str_from_image(self, sdata_blobs_str: SpatialData):
sdata_blobs_str.pl.render_images(elements="blobs_image", channel=["c1", "c2"]).pl.show()

def test_plot_can_render_two_channels_str_from_multiscale_image(self, sdata_blobs_str: SpatialData):
sdata_blobs_str.pl.render_images(elements="blobs_multiscale_image", channel=["c1", "c2"]).pl.show()

def test_plot_can_pass_color_to_single_channel(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_images(elements="blobs_image", channel=1, palette="red").pl.show()

Expand Down
49 changes: 22 additions & 27 deletions tests/pl/test_render_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import matplotlib
import numpy as np
import pandas as pd
import pytest
import scanpy as sc
import spatialdata_plot # noqa: F401
from anndata import AnnData
Expand Down Expand Up @@ -100,39 +101,33 @@ def test_can_plot_with_one_element_color_table(self, sdata_blobs: SpatialData):
color=["channel_0_sum", "channel_1_sum"], table_name=["table", "multi_table"]
).pl.show()

def test_plot_label_categorical_color(self, sdata_blobs: SpatialData):
n_obs = max(_get_unique_label_values_as_index(sdata_blobs["blobs_labels"]))
@pytest.mark.parametrize(
"label",
[
"blobs_labels",
"blobs_multiscale_labels",
],
)
def test_plot_label_categorical_color(self, sdata_blobs: SpatialData, label: str):
self._make_tablemodel_with_categorical_labels(sdata_blobs, label)

def _make_tablemodel_with_categorical_labels(self, sdata_blobs, label):
n_obs = max(_get_unique_label_values_as_index(sdata_blobs[label]))
adata = AnnData(
RNG.normal(size=(n_obs, 10)), obs=pd.DataFrame(RNG.normal(size=(n_obs, 3)), columns=["a", "b", "c"])
RNG.normal(size=(n_obs, 10)),
obs=pd.DataFrame(RNG.normal(size=(n_obs, 3)), columns=["a", "b", "c"]),
)
adata.obs["instance_id"] = np.arange(adata.n_obs)
adata.obs["category"] = RNG.choice(["a", "b", "c"], size=adata.n_obs)
adata.obs["category"][:3] = ["a", "b", "c"]
adata.obs["instance_id"] = list(range(adata.n_obs))
adata.obs["region"] = "blobs_labels"
table = TableModel.parse(adata=adata, region_key="region", instance_key="instance_id", region="blobs_labels")
sdata_blobs["other_table"] = table

# with pytest.raises(ValueError, match="could not convert string"):
# sdata_blobs.pl.render_labels('blobs_labels', color='category').pl.show()
sdata_blobs["other_table"].obs["category"] = sdata_blobs["other_table"].obs["category"].astype("category")
sdata_blobs.pl.render_labels("blobs_labels", color="category").pl.show()

def test_plot_multiscale_label_categorical_color(self, sdata_blobs: SpatialData):
# recreate RNG to get same plot acorss 3.9 and 3.10 workers
RNG = np.random.default_rng(seed=42)

n_obs = max(_get_unique_label_values_as_index(sdata_blobs["blobs_multiscale_labels"]))
adata = AnnData(
RNG.normal(size=(n_obs, 10)), obs=pd.DataFrame(RNG.normal(size=(n_obs, 3)), columns=["a", "b", "c"])
)
adata.obs["instance_id"] = np.arange(adata.n_obs)
adata.obs["category"] = RNG.choice(["a", "b", "c"], size=adata.n_obs)
adata.obs["instance_id"] = list(range(adata.n_obs))
adata.obs["region"] = "blobs_multiscale_labels"
adata.obs["region"] = label
table = TableModel.parse(
adata=adata, region_key="region", instance_key="instance_id", region="blobs_multiscale_labels"
adata=adata,
region_key="region",
instance_key="instance_id",
region=label,
)
sdata_blobs["other_table"] = table

sdata_blobs["other_table"].obs["category"] = sdata_blobs["other_table"].obs["category"].astype("category")
sdata_blobs.pl.render_labels("blobs_multiscale_labels", color="category").pl.show()
sdata_blobs.pl.render_labels(label, color="category").pl.show()