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

warn many shapes as points #224

Merged
merged 5 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/napari_spatialdata/_constants/config.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
POLYGON_THRESHOLD = 100
POINT_THRESHOLD = 100000
N_CIRCLES_WARNING_THRESHOLD = 100000
N_SHAPES_WARNING_THRESHOLD = 10000
37 changes: 36 additions & 1 deletion src/napari_spatialdata/_sdata_widgets.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,68 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, Iterable

import shapely
from napari.utils.events import EventedList
from qtpy.QtGui import QIcon
from qtpy.QtWidgets import QLabel, QListWidget, QListWidgetItem, QVBoxLayout, QWidget
from spatialdata import SpatialData

from napari_spatialdata._constants.config import N_CIRCLES_WARNING_THRESHOLD, N_SHAPES_WARNING_THRESHOLD
from napari_spatialdata._viewer import SpatialDataViewer
from napari_spatialdata.utils._utils import _get_sdata_key, get_duplicate_element_names, get_elements_meta_mapping

if TYPE_CHECKING:
from napari import Viewer
from napari.utils.events.event import Event

icon_path = Path(__file__).parent / "resources/exclamation.png"


class ElementWidget(QListWidget):
def __init__(self, sdata: EventedList):
super().__init__()
self._icon = QIcon(str(icon_path))
self._sdata = sdata
self._duplicate_element_names, _ = get_duplicate_element_names(self._sdata)
self._elements: None | dict[str, dict[str, str | int]] = None

def _onItemChange(self, selected_coordinate_system: QListWidgetItem | int | Iterable[str]) -> None:
self.clear()
elements, _ = get_elements_meta_mapping(self._sdata, selected_coordinate_system, self._duplicate_element_names)
self.addItems(elements.keys())
self._set_element_widget_items(elements)
self._elements = elements

def _set_element_widget_items(self, elements: dict[str, dict[str, str | int]]) -> None:
for key, dict_val in elements.items():
sdata = self._sdata[dict_val["sdata_index"]]
element_type = dict_val["element_type"]
element_name = dict_val["original_name"]
item = QListWidgetItem(key)
if element_type == "shapes":
if (
type((element := sdata.shapes[element_name]).iloc[0].geometry) == shapely.Point
and len(element) > N_CIRCLES_WARNING_THRESHOLD
):
item.setIcon(self._icon)
item.setToolTip(
"Visualizing this many circles is currently slow in napari. Consider whether you want to "
"visualize."
)
self.addItem(item)
elif (
type((element := sdata.shapes[element_name]).iloc[0].geometry)
in [shapely.Polygon, shapely.MultiPolygon]
and len(element) > N_SHAPES_WARNING_THRESHOLD
):
item.setIcon(self._icon)
item.setToolTip(
"Visualizing this many shapes is currently slow in napari. Consider whether you want to "
"visualize."
)
self.addItem(item)


class CoordinateSystemWidget(QListWidget):
def __init__(self, sdata: EventedList):
Expand Down
Binary file added src/napari_spatialdata/resources/exclamation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading