Skip to content

Commit f2d4956

Browse files
authored
Merge pull request #459 from jo-mueller/support-shape-selection
add conditional support for interactive shape selection
2 parents 6e2e114 + 1e7d0c9 commit f2d4956

4 files changed

Lines changed: 22 additions & 9 deletions

File tree

docs/usage/overview.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ The drawing of the plotted data is triggered whenever the data in the `layer.fea
4444
In case you ever found yourself in the situation that you wanted to process or inspect an individual object from a layer separately from all the other objects without tedious image processing to pick out *just the right object*, you can now do this in the clusters plotter. Simply draw a selection in the canvas (using the `MANUAL_CLUSTER_ID`) as hue column or select any other categorical feature. Then use the cluster selector on the top of the canvas to select the object class you'd want to export and click the `Add current class as new layer button`!
4545

4646
![Add selected objects to new layer](./imgs/copy_export_objects_function.gif)
47+
48+
## Bi-directional object selection
49+
50+
*Geez, what's the matter with this cell*? Ever hoped you could just select an object on the napari canvas and introspect its properties? Just as it is possible to select objects in the napari-clusters-plotter interface and highlight them in the napari viewer, it is possible to do it the other way around. For `Labels`, `Points` and `Shapes` (Shapes: napari version >=0.6.5), you can select objects in the napari UI. A new categorical feature (`SELECTED_LAYER_CLUSTER_ID`) will be added to the `hue` dropdown that highlights all selected objects.

src/napari_clusters_plotter/_new_plotter_widget.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,12 @@ def _on_update_layer_selection(
570570
# connect selection event
571571
if _is_selectable_layer(layer):
572572
selection_event = _get_selection_event(layer)
573-
selection_event.connect(self._update_selected_object_feature)
573+
574+
# not all napari versions support all selection events
575+
if selection_event is not None:
576+
selection_event.connect(
577+
self._update_selected_object_feature
578+
)
574579

575580
def _update_selected_object_feature(self) -> None:
576581
"""

src/napari_clusters_plotter/_tests/test_plotter.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,11 @@ def test_cluster_visibility_toggle(make_napari_viewer, create_sample_layers):
753753

754754
@pytest.mark.parametrize(
755755
"create_sample_layers",
756-
[create_multi_point_layer, create_multi_labels_layer],
756+
[
757+
create_multi_point_layer,
758+
create_multi_labels_layer,
759+
create_multi_shapes_layers,
760+
],
757761
)
758762
def test_selected_data_point_layer(make_napari_viewer, create_sample_layers):
759763
from napari_clusters_plotter import PlotterWidget
@@ -771,7 +775,7 @@ def test_selected_data_point_layer(make_napari_viewer, create_sample_layers):
771775
viewer.layers.selection.active = layer2
772776

773777
event = _get_selection_event(layer2)
774-
if isinstance(layer2, Points):
778+
if type(layer2) in [Points, Shapes]:
775779
selection = [1, 2]
776780
layer2.selected_data = selection
777781
event.emit()

src/napari_clusters_plotter/_utilities.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
import numpy as np
55
from napari.layers import Image, Labels, Layer, Points, Shapes
66
from napari.utils.events import Event
7+
from packaging.version import Version
78

8-
_selectable_layers = [
9-
Labels,
10-
Points,
11-
# TODO: Shapes support is planned for future implementation; currently excluded from selectable layers due to incomplete support.
12-
]
9+
_selectable_layers = [Labels, Points, Shapes]
1310

1411

1512
def _get_unique_values(layer: Union[Image, Labels]) -> np.ndarray:
@@ -77,4 +74,7 @@ def _get_selection_event(layer: Layer) -> Event:
7774
elif isinstance(layer, Labels):
7875
return layer.events.selected_label
7976
elif isinstance(layer, Shapes):
80-
return layer.events.highlight
77+
from napari import __version__
78+
79+
if Version(__version__) >= Version("0.6.5"):
80+
return layer.selected_data.events.items_changed

0 commit comments

Comments
 (0)