Skip to content

Commit

Permalink
hotfix annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaMarconato committed Sep 11, 2024
1 parent 885b5fa commit c3cd50a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/napari_spatialdata/_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ def _write_element_to_disk(
element: tuple[DaskDataFrame | GeoDataFrame | AnnData],
overwrite: bool,
) -> None:
if sdata.is_backed:
self._delete_from_disk(sdata, element_name, overwrite)
sdata[element_name] = element
sdata.write_element(element_name)
else:
sdata[element_name] = element
logger.warning("Spatialdata object is not stored on disk, could only add element in memory.")
# if sdata.is_backed:
# self._delete_from_disk(sdata, element_name, overwrite)
# sdata[element_name] = element
# sdata.write_element(element_name)
# else:
sdata[element_name] = element
logger.warning("Annotations only added in memory, please manually save to disk.")

def _save_points_to_sdata(
self, layer_to_save: Points, spatial_element_name: str | None, overwrite: bool
Expand All @@ -187,6 +187,8 @@ def _save_points_to_sdata(
swap_data = swap_data[:, :2]
parsed = PointsModel.parse(swap_data, transformations=transformation)

# saving to disk of points temporarily disabled until the interface update that will unify the view widget,
# annotation widget and scatterplot widget
self._write_element_to_disk(sdata, spatial_element_name, parsed, overwrite)

return parsed, coordinate_system
Expand Down Expand Up @@ -247,7 +249,15 @@ def _save_shapes_to_sdata(
if len(layer_to_save.data) == 0:
raise ValueError("Cannot export a shapes element with no shapes")

polygons: list[Polygon] = [Polygon(i) for i in _transform_coordinates(layer_to_save.data, f=lambda x: x[::-1])]
coords = [
np.array([layer_to_save.data_to_world(xy) for xy in shape._data])
for shape in layer_to_save._data_view.shapes
]
remove_z = coords[0].shape[1] == 3
first_index = 1 if remove_z else 0
polygons: list[Polygon] = [Polygon(p[::-1, first_index:]) for p in coords]
# polygons: list[Polygon] = [Polygon(i) for i in _transform_coordinates(coords, f=lambda x: x[::-1])]
# polygons: list[Polygon] = [Polygon(i) for i in _transform_coordinates(layer_to_save.data, f=lambda x: x[::-1])]
gdf = GeoDataFrame({"geometry": polygons})

force_2d(gdf)
Expand Down Expand Up @@ -573,6 +583,7 @@ def add_sdata_shapes(self, sdata: SpatialData, key: str, selected_cs: str, multi
name=key,
affine=affine,
shape_type="polygon",
face_color="#00000000",
metadata={
"sdata": sdata,
"adata": adata,
Expand Down
1 change: 1 addition & 0 deletions src/napari_spatialdata/_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def _onAction(self, items: Iterable[str]) -> None:
self.model.layer.text = None # needed because of the text-feature order of updates
# self.model.layer.features = properties.get("features", None)
self.model.layer.face_color = properties["face_color"]
# self.model.layer.edge_color = properties["face_color"]
self.model.layer.text = properties["text"]
elif isinstance(self.model.layer, Labels):
version = get_napari_version()
Expand Down

0 comments on commit c3cd50a

Please sign in to comment.