Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
improve readability

Co-authored-by: Grzegorz Bokota <[email protected]>
  • Loading branch information
minhtien-trinh and Czaki authored Oct 8, 2024
1 parent 0a5f121 commit 325460d
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/napari_spatialdata/_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,26 @@ def screenshot(self) -> ArrayLike | Any:

def get_layer(self, layer_name: str) -> Image | Labels | Points | Shapes | None:
"""Get a layer by name."""
for layer in self._viewer.layers:
if layer.name == layer_name:
return layer
return None
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 polygon_layer:
if len(text_annotations) != len(polygon_layer.data):
raise ValueError(
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",
}
else:
if not polygon_layer:
raise ValueError(f"Polygon layer '{layer_name}' not found.")
if len(text_annotations) != len(polygon_layer.data):
raise ValueError(
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",
}


0 comments on commit 325460d

Please sign in to comment.