Skip to content

Commit

Permalink
Fix bug adata in model not being reset (#317)
Browse files Browse the repository at this point in the history
* fix bug adata in model not being reset

* changelog
  • Loading branch information
LucaMarconato authored Sep 25, 2024
1 parent 4385a20 commit 3dc4e5b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning][].

## [0.x.x] - 2024-xx-xx

### Fixed

- Bug table was not reset after an element without table was added #317

## [0.5.2] - 2024-08-16

### Minor
Expand Down
37 changes: 24 additions & 13 deletions src/napari_spatialdata/_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,13 @@ def _update_adata(self) -> None:
)
layer.metadata["adata"] = table

if layer is not None and "adata" in layer.metadata:
with self.model.events.adata.blocker():
self.model.adata = layer.metadata["adata"]
if layer is not None:
if "adata" in layer.metadata:
with self.model.events.adata.blocker():
self.model.adata = layer.metadata["adata"]
else:
with self.model.events.adata.blocker():
self.model.adata = None

if self.model.adata.shape == (0, 0):
return
Expand Down Expand Up @@ -186,8 +190,8 @@ def _select_layer(self) -> None:
self.color_widget.clear()
return

if layer is not None and "adata" in layer.metadata:
self.model.adata = layer.metadata["adata"]
if layer is not None:
self.model.adata = layer.metadata.get("adata", None)

def screenshot(self) -> Any:
return QImg2array(self.grab().toImage())
Expand Down Expand Up @@ -384,10 +388,11 @@ def _select_layer(self) -> None:
if isinstance(layer, (Points, Shapes)) and (cols_df := layer.metadata.get("_columns_df")) is not None:
self.dataframe_columns_widget.addItems(map(str, cols_df.columns))
self.model.system_name = layer.metadata.get("name", None)
self.model.adata = None
return

if layer is not None and "adata" in layer.metadata:
self.model.adata = layer.metadata["adata"]
if layer is not None:
self.model.adata = layer.metadata.get("adata", None)

if self.model.adata.shape == (0, 0):
return
Expand Down Expand Up @@ -418,9 +423,13 @@ def _update_adata(self) -> None:
)
layer.metadata["adata"] = table

if layer is not None and "adata" in layer.metadata:
with self.model.events.adata.blocker():
self.model.adata = layer.metadata["adata"]
if layer is not None:
if "adata" in layer.metadata:
with self.model.events.adata.blocker():
self.model.adata = layer.metadata["adata"]
else:
with self.model.events.adata.blocker():
self.model.adata = None

if self.model.adata.shape == (0, 0):
return
Expand All @@ -440,10 +449,12 @@ def _update_adata(self) -> None:
return

def _get_adata_layer(self) -> Sequence[str | None]:
if self.model.adata is None:
return [None]
adata_layers = list(self.model.adata.layers.keys())
if len(adata_layers):
return adata_layers
return [None]
if len(adata_layers) == 0:
return [None]
return adata_layers

def _change_color_by(self) -> None:
self.color_by.setText(f"Color by: {self.model.color_by}")
Expand Down
5 changes: 2 additions & 3 deletions src/napari_spatialdata/_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from shapely import Polygon
from spatialdata import get_element_annotators, get_element_instances
from spatialdata._core.query.relational_query import _left_join_spatialelement_table
from spatialdata._types import ArrayLike
from spatialdata.models import PointsModel, ShapesModel, TableModel, force_2d, get_channels
from spatialdata.transformations import Affine, Identity
from spatialdata.transformations._utils import scale_radii
Expand Down Expand Up @@ -254,15 +255,13 @@ def _save_shapes_to_sdata(
for shape in layer_to_save._data_view.shapes
]

def _fix_coords(coords: np.ndarray) -> np.ndarray:
def _fix_coords(coords: ArrayLike) -> ArrayLike:
remove_z = coords.shape[1] == 3
first_index = 1 if remove_z else 0
coords = coords[:, first_index::]
return np.fliplr(coords)

polygons: list[Polygon] = [Polygon(_fix_coords(p)) 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
2 changes: 1 addition & 1 deletion src/napari_spatialdata/_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def _(self, vec: pd.Series, **kwargs: Any) -> dict[str, Any]:
f"The {vec_color_name} column must have unique values for the each {vec.name} level. Found:\n"
f"{unique_colors}"
)
color_dict = unique_colors.to_dict()["genes_color"]
color_dict = unique_colors.to_dict()[f"{vec.name}_color"]

if self.model.instance_key is not None and self.model.instance_key == vec.index.name:
merge_df = pd.merge(
Expand Down
4 changes: 3 additions & 1 deletion src/napari_spatialdata/utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def get_itemindex_by_text(
return widget_item


def _get_init_table_list(layer: Layer) -> Sequence[str | None] | None:
def _get_init_table_list(layer: Layer | None) -> Sequence[str | None] | None:
"""
Get the table names annotating the SpatialElement upon creating the napari layer.
Expand All @@ -432,6 +432,8 @@ def _get_init_table_list(layer: Layer) -> Sequence[str | None] | None:
------
The list of table names annotating the SpatialElement if any.
"""
if layer is None:
return None
table_names: Sequence[str | None] | None
if table_names := layer.metadata.get("table_names"):
return table_names # type: ignore[no-any-return]
Expand Down

0 comments on commit 3dc4e5b

Please sign in to comment.