|
1 | 1 | import matplotlib |
| 2 | +import numpy as np |
2 | 3 | import scanpy as sc |
3 | 4 | from spatialdata import SpatialData |
| 5 | +from spatialdata.models import Image2DModel, Labels2DModel |
4 | 6 |
|
5 | 7 | import spatialdata_plot # noqa: F401 |
6 | 8 | from tests.conftest import DPI, PlotTester, PlotTesterMeta |
|
19 | 21 |
|
20 | 22 |
|
21 | 23 | class TestColorbarControls(PlotTester, metaclass=PlotTesterMeta): |
| 24 | + def _make_multi_image_single_channel_sdata(self) -> SpatialData: |
| 25 | + img1 = Image2DModel.parse(np.linspace(0, 1, 2500, dtype=float).reshape(1, 50, 50), dims=("c", "y", "x")) |
| 26 | + img2 = Image2DModel.parse(np.linspace(2, 0, 2500, dtype=float).reshape(1, 50, 50), dims=("c", "y", "x")) |
| 27 | + labels = Labels2DModel.parse(np.arange(2500, dtype=int).reshape(50, 50), dims=("y", "x")) |
| 28 | + return SpatialData(images={"img1": img1, "img2": img2}, labels={"lab": labels}) |
| 29 | + |
22 | 30 | def test_plot_image_auto_colorbar_for_single_channel(self, sdata_blobs: SpatialData): |
23 | | - sdata_blobs.pl.render_images(element="img").pl.show() |
| 31 | + sdata_blobs.pl.render_images("blobs_image", element="img").pl.show() |
24 | 32 |
|
25 | 33 | def test_plot_colorbar_img_default_location(self, sdata_blobs: SpatialData): |
26 | | - (sdata_blobs.pl.render_images(channel=0, cmap="Reds").pl.show()) |
| 34 | + (sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds").pl.show()) |
27 | 35 |
|
28 | 36 | def test_plot_colorbar_img_bottom(self, sdata_blobs: SpatialData): |
29 | | - (sdata_blobs.pl.render_images(channel=0, cmap="Reds", colorbar_params={"loc": "bottom"}).pl.show()) |
| 37 | + ( |
| 38 | + sdata_blobs.pl.render_images( |
| 39 | + "blobs_image", channel=0, cmap="Reds", colorbar_params={"loc": "bottom"} |
| 40 | + ).pl.show() |
| 41 | + ) |
30 | 42 |
|
31 | 43 | def test_plot_colorbar_img_left(self, sdata_blobs: SpatialData): |
32 | | - (sdata_blobs.pl.render_images(channel=0, cmap="Reds", colorbar_params={"loc": "left"}).pl.show()) |
| 44 | + (sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"loc": "left"}).pl.show()) |
33 | 45 |
|
34 | 46 | def test_plot_colorbar_img_top(self, sdata_blobs: SpatialData): |
35 | | - (sdata_blobs.pl.render_images(channel=0, cmap="Reds", colorbar_params={"loc": "top"}).pl.show()) |
| 47 | + (sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"loc": "top"}).pl.show()) |
36 | 48 |
|
37 | 49 | def test_plot_colorbar_can_adjust_width(self, sdata_blobs: SpatialData): |
38 | | - (sdata_blobs.pl.render_images(channel=0, cmap="Reds", colorbar_params={"width": 0.4}).pl.show()) |
| 50 | + (sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"width": 0.4}).pl.show()) |
39 | 51 |
|
40 | 52 | def test_plot_colorbar_can_adjust_title(self, sdata_blobs: SpatialData): |
41 | | - (sdata_blobs.pl.render_images(channel=0, cmap="Reds", colorbar_params={"label": "Intensity"}).pl.show()) |
| 53 | + ( |
| 54 | + sdata_blobs.pl.render_images( |
| 55 | + "blobs_image", channel=0, cmap="Reds", colorbar_params={"label": "Intensity"} |
| 56 | + ).pl.show() |
| 57 | + ) |
42 | 58 |
|
43 | 59 | def test_plot_colorbar_can_adjust_pad(self, sdata_blobs: SpatialData): |
44 | | - (sdata_blobs.pl.render_images(channel=0, cmap="Reds", colorbar_params={"pad": 0.4}).pl.show()) |
| 60 | + (sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"pad": 0.4}).pl.show()) |
45 | 61 |
|
46 | 62 | def test_plot_colorbar_can_have_colorbars_on_different_sides(self, sdata_blobs: SpatialData): |
47 | 63 | ( |
48 | | - sdata_blobs.pl.render_images(channel=0, cmap="Reds", colorbar_params={"loc": "top"}) |
49 | | - .pl.render_labels(element="blobs_labels", color="instance_id", colorbar_params={"loc": "bottom"}) |
| 64 | + sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"loc": "top"}) |
| 65 | + .pl.render_labels("blobs_labels", color="instance_id", colorbar_params={"loc": "bottom"}) |
50 | 66 | .pl.show() |
51 | 67 | ) |
52 | 68 |
|
53 | 69 | def test_plot_colorbar_can_have_two_colorbars_on_same_side(self, sdata_blobs: SpatialData): |
54 | 70 | ( |
55 | | - sdata_blobs.pl.render_images(channel=0, cmap="Reds") |
56 | | - .pl.render_labels(element="blobs_labels", color="instance_id") |
| 71 | + sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds") |
| 72 | + .pl.render_labels("blobs_labels", color="instance_id") |
57 | 73 | .pl.show() |
58 | 74 | ) |
59 | 75 |
|
60 | 76 | def test_plot_colorbar_can_have_colorbars_on_all_sides(self, sdata_blobs: SpatialData): |
61 | 77 | # primarily shows that spacing is correct between colorbars and plot elements |
| 78 | + shared_params = { |
| 79 | + "element": "blobs_image", |
| 80 | + "channel": 0, |
| 81 | + "cmap": "Reds", |
| 82 | + } |
62 | 83 | ( |
63 | | - sdata_blobs.pl.render_images(channel=0, cmap="Reds", colorbar_params={"loc": "top", "label": "top_1"}) |
64 | | - .pl.render_images(channel=0, cmap="Reds", colorbar_params={"loc": "right", "label": "right_1"}) |
65 | | - .pl.render_images(channel=0, cmap="Reds", colorbar_params={"loc": "left", "label": "left_1"}) |
66 | | - .pl.render_images(channel=0, cmap="Reds", colorbar_params={"loc": "bottom", "label": "bottom_1"}) |
67 | | - .pl.render_images(channel=0, cmap="Reds", colorbar_params={"loc": "top", "label": "top_2"}) |
68 | | - .pl.render_images(channel=0, cmap="Reds", colorbar_params={"loc": "right", "label": "right_2"}) |
69 | | - .pl.render_images(channel=0, cmap="Reds", colorbar_params={"loc": "left", "label": "left_2"}) |
70 | | - .pl.render_images(channel=0, cmap="Reds", colorbar_params={"loc": "bottom", "label": "bottom_2"}) |
| 84 | + sdata_blobs.pl.render_images(**shared_params, colorbar_params={"loc": "top", "label": "top_1"}) |
| 85 | + .pl.render_images(**shared_params, colorbar_params={"loc": "right", "label": "right_1"}) |
| 86 | + .pl.render_images(**shared_params, colorbar_params={"loc": "left", "label": "left_1"}) |
| 87 | + .pl.render_images(**shared_params, colorbar_params={"loc": "bottom", "label": "bottom_1"}) |
| 88 | + .pl.render_images(**shared_params, colorbar_params={"loc": "top", "label": "top_2"}) |
| 89 | + .pl.render_images(**shared_params, colorbar_params={"loc": "right", "label": "right_2"}) |
| 90 | + .pl.render_images(**shared_params, colorbar_params={"loc": "left", "label": "left_2"}) |
| 91 | + .pl.render_images(**shared_params, colorbar_params={"loc": "bottom", "label": "bottom_2"}) |
71 | 92 | .pl.show() |
72 | 93 | ) |
| 94 | + |
| 95 | + def test_plot_multiple_images_in_one_cs_result_in_multiple_colorbars(self): |
| 96 | + sdata = self._make_multi_image_single_channel_sdata() |
| 97 | + sdata.pl.render_images(channel=0, cmap="Reds").pl.show() |
| 98 | + |
| 99 | + def test_plot_can_globally_turn_off_colorbars(self): |
| 100 | + # adresses https://github.com/scverse/spatialdata-plot/issues/431 |
| 101 | + sdata = self._make_multi_image_single_channel_sdata() |
| 102 | + sdata.pl.render_images(channel=0, cmap="Reds").pl.show(colorbar=False) |
0 commit comments