-
Notifications
You must be signed in to change notification settings - Fork 10
add LAYER_SELECTED_DATA_CLUSTER_ID to cluster from layer space #366
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -308,6 +308,24 @@ def _on_update_layer_selection( | |
for layer in self.layers: | ||
layer.events.features.connect(self._update_feature_selection) | ||
|
||
# if layer is a Point Layer | ||
if type(layer) is napari.layers.Points: | ||
layer.selected_data.events.items_changed.connect( | ||
lambda selected_data: self._update_layer_selected_data_feature( | ||
layer, selected_data | ||
) | ||
) | ||
|
||
def _update_layer_selected_data_feature( | ||
self, layer: napari.layers.Points, selected_data: np.ndarray | ||
) -> None: | ||
""" | ||
Update the layer selected_data to feature. | ||
""" | ||
cluster = np.zeros(layer.data.shape[0]) | ||
cluster[list(selected_data)] = 1 | ||
layer.features["LAYER_SELECTED_DATA_CLUSTER_ID"] = cluster | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would change the name of the column to something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the review @jo-mueller !
|
||
|
||
def _update_feature_selection( | ||
self, event: napari.utils.events.Event | ||
) -> None: | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -108,3 +108,27 @@ def test_cluster_memorization(make_napari_viewer, n_samples: int = 100): | |||||||||
plotter_widget.plotting_widget.active_artist.color_indices | ||||||||||
== cluster_indeces | ||||||||||
) | ||||||||||
|
||||||||||
|
||||||||||
def test_layer_selection(make_napari_viewer, n_samples: int = 100): | ||||||||||
from napari_clusters_plotter import PlotterWidget | ||||||||||
|
||||||||||
viewer = make_napari_viewer() | ||||||||||
_, layer2 = create_multi_point_layer(n_samples=n_samples) | ||||||||||
|
||||||||||
# add layers to viewer | ||||||||||
viewer.add_layer(layer2) | ||||||||||
plotter_widget = PlotterWidget(viewer) | ||||||||||
viewer.window.add_dock_widget(plotter_widget, area="right") | ||||||||||
|
||||||||||
# select last layer and create a random selection on the layer | ||||||||||
viewer.layers.selection.active = layer2 | ||||||||||
selection = np.random.randint(0, 1, len(layer2.data)) | ||||||||||
layer2.selected_data = selection | ||||||||||
|
||||||||||
assert "LAYER_SELECTED_DATA_CLUSTER_ID" in layer2.features.columns | ||||||||||
|
||||||||||
# make sure that the cluster selection is the same | ||||||||||
cluster = np.zeros(layer2.data.shape[0]) | ||||||||||
cluster[list(selection)] = 1 | ||||||||||
assert np.all(layer2.features["LAYER_SELECTED_DATA_CLUSTER_ID"] == cluster) | ||||||||||
Comment on lines
+113
to
+134
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe one suggestion for a clean way to check whether the selection from the viewer is forwarded to the plotter widget: The plotter widget has an item that contains the actual plotter/canvas/stuff where you'll find a reference to the ids of each object's currently selected cluster
which comes from here. Essentially, you could write the test so that it checks whether this property has actually been updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the review @jo-mueller, I agree that the test should check if the selection can update From my understanding the current implementation only allows napari-clusters-plotter/src/napari_clusters_plotter/_new_plotter_widget.py Lines 161 to 164 in 73192f5
Should this PR wait for a method Should I manually update in the test the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe other layer type are eligible?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Labels layers have
layer.selected_label
, that could work, too. Maybe we could add a propertyis_selectable(self)
to the plotter widget that will returnTrue
orFalse
if the layer supports some sort of interactive selection. We could then hide theif/else
check for the kind of layer in that property/helper function.It could look something like this:
At this point we could then simply add a check
and then pass it on to the
_update_selected_layer...
function, where we would have to add the same check for the kind of layer. That would make it a bit easier extensible down the road.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice this a neat solution! Do you want to commit on the branch or do you want me to add your snippet?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm too dumb right now to send a PR to your branch but I'd basically have it ready to a degree. If you want I can also paste it here and you just add me as a contributor to the commit 🤷
You'd basically need this in the class definition:
and these convenience functions:
the respective code in the
_on_update_layer_selection
function would then look like this: