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

fix wrong point size when affine specified #193

Merged
merged 1 commit into from
Mar 14, 2024
Merged
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
28 changes: 25 additions & 3 deletions src/napari_spatialdata/_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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)
Loading