Skip to content
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

Add functions to interactive #315

Merged
merged 15 commits into from
Oct 9, 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: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ 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.5.5] - 2024-10-07

### Added

- New function to grab layer by name #315 @minhtien-trinh
- New annotation function to add text to polygons #315 @minhtien-trinh

## [0.5.4] - 2024-xx-xx

### Fixed
Expand Down
25 changes: 25 additions & 0 deletions src/napari_spatialdata/_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import TYPE_CHECKING, Any

import napari
from napari.layers import Image, Labels, Points, Shapes
from napari.utils.events import EventedList
from spatialdata._types import ArrayLike

Expand Down Expand Up @@ -100,3 +101,27 @@
def screenshot(self) -> ArrayLike | Any:
"""Take a screenshot of the viewer in its current state."""
return self._viewer.screenshot(canvas_only=False)

def get_layer(self, layer_name: str) -> Image | Labels | Points | Shapes | None:
"""Get a layer by name."""
try:
return self._viewer.layers[layer_name]
except KeyError:
return None

def add_text_to_polygons(self, layer_name: str, text_annotations: list[str]) -> None:
"""Add text annotations to a polygon layer."""
polygon_layer = self.get_layer(layer_name)
if not polygon_layer:
raise ValueError(f"Polygon layer '{layer_name}' not found.")

Check warning on line 116 in src/napari_spatialdata/_interactive.py

View check run for this annotation

Codecov / codecov/patch

src/napari_spatialdata/_interactive.py#L116

Added line #L116 was not covered by tests
if len(text_annotations) != len(polygon_layer.data):
raise ValueError(

Check warning on line 118 in src/napari_spatialdata/_interactive.py

View check run for this annotation

Codecov / codecov/patch

src/napari_spatialdata/_interactive.py#L118

Added line #L118 was not covered by tests
f"The number of text annotations must match the number of polygons. "
f"Polygons: {len(polygon_layer.data)}, Text: {len(text_annotations)}."
)
polygon_layer.text = {
"string": text_annotations,
"size": 10,
"color": "red",
"anchor": "center",
}
29 changes: 29 additions & 0 deletions tests/test_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,32 @@ def test_plot_can_add_element_switch_cs(sdata_blobs: SpatialData):
assert i._sdata_widget.coordinate_system_widget._system == "global"
assert i._viewer.layers[-1].visible
i._viewer.close()


class TestInteractive(PlotTester, metaclass=PlotTesterMeta):
def test_get_layer_existing(self, sdata_blobs: SpatialData):
i = Interactive(sdata=sdata_blobs, headless=True)
i.add_element(element="blobs_image", element_coordinate_system="global")
layer = i.get_layer("blobs_image")
assert layer is not None, "Expected to retrieve the blobs_image layer, but got None"
assert layer.name == "blobs_image", f"Expected layer name 'blobs_image', got {layer.name}"
i._viewer.close()

def test_get_layer_non_existing(self, sdata_blobs: SpatialData):
i = Interactive(sdata=sdata_blobs, headless=True)
layer = i.get_layer("non_existing_layer")
assert layer is None, "Expected None for a non-existing layer, but got a layer"
i._viewer.close()

def test_add_text_to_polygons(self, sdata_blobs: SpatialData):
i = Interactive(sdata=sdata_blobs, headless=True)
i.add_element(element="blobs_polygons", element_coordinate_system="global")

# Mock polygon layer with some polygon data
text_annotations = ["Label 1", "Label 2", "Label 3", "Label 4", "Label 5"]
polygon_layer = i.get_layer("blobs_polygons")

# Verify that text is added
i.add_text_to_polygons(layer_name="blobs_polygons", text_annotations=text_annotations)
assert polygon_layer.text is not None, "Text annotations were not added to the polygon layer"
i._viewer.close()
Loading