diff --git a/src/napari_spatialdata/_viewer.py b/src/napari_spatialdata/_viewer.py index a714975..6bb0dda 100644 --- a/src/napari_spatialdata/_viewer.py +++ b/src/napari_spatialdata/_viewer.py @@ -13,7 +13,8 @@ from qtpy.QtCore import QObject, Signal from shapely import Polygon from spatialdata.models import PointsModel, ShapesModel -from spatialdata.transformations import Identity +from spatialdata.transformations import Affine, Identity +from spatialdata.transformations._utils import scale_radii from napari_spatialdata.utils._utils import ( _adjust_channels_order, @@ -307,7 +308,7 @@ def add_sdata_circles(self, sdata: SpatialData, key: str, selected_cs: str, mult radii = df.radius.to_numpy() adata = _get_metadata_adata(sdata, original_name) - self.viewer.add_points( + layer = self.viewer.add_points( xy, name=key, affine=affine, @@ -324,6 +325,8 @@ def add_sdata_circles(self, sdata: SpatialData, key: str, selected_cs: str, mult "indices": df.index.to_list(), }, ) + assert affine is not None + self._adjust_radii_of_points_layer(layer=layer, affine=affine) def add_sdata_shapes(self, sdata: SpatialData, key: str, selected_cs: str, multi: bool) -> None: original_name = key @@ -405,7 +408,7 @@ def add_sdata_points(self, sdata: SpatialData, key: str, selected_cs: str, multi xy = points[["y", "x"]].values[subsample] np.fliplr(xy) - self.viewer.add_points( + layer = self.viewer.add_points( xy, name=key, size=20, @@ -421,6 +424,23 @@ def add_sdata_points(self, sdata: SpatialData, key: str, selected_cs: str, multi "indices": subsample.tolist(), }, ) + assert affine is not None + self._adjust_radii_of_points_layer(layer=layer, affine=affine) + + def _adjust_radii_of_points_layer(self, layer: Layer, affine: npt.ArrayLike) -> None: + assert isinstance(affine, np.ndarray) + axes: tuple[str, ...] + if affine.shape == (3, 3): + axes = ("y", "x") + elif affine.shape == (4, 4): + axes = ("z", "y", "x") + else: + raise ValueError(f"Invalid affine shape: {affine.shape}") + affine_transformation = Affine(affine, input_axes=axes, output_axes=axes) + metadata = layer.metadata + radii = metadata["sdata"][metadata["name"]].radius.to_numpy() + new_radii = scale_radii(radii=radii, affine=affine_transformation, axes=axes) + layer.size = new_radii * 2 def _affine_transform_layers(self, coordinate_system: str) -> None: for layer in self.viewer.layers: @@ -432,3 +452,5 @@ def _affine_transform_layers(self, coordinate_system: str) -> None: affine = _get_transform(element_data, coordinate_system) if affine is not None: layer.affine = affine + if layer._type_string == "points": + self._adjust_radii_of_points_layer(layer, affine)