Skip to content

Commit ab869bb

Browse files
committed
Fix translation handling & detection of clicks when translation is present
1 parent 751634f commit ab869bb

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/napari_feature_classifier/annotator_widget.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,22 @@ def toggle_label(self, labels_layer, event):
215215
Callback for when a label is clicked. It then updates the color of that
216216
label in the annotation layer.
217217
"""
218-
# Need to scale position that event.position returns by the
218+
# Need to translate & scale position that event.position returns by the
219219
# label_layer scale.
220220
# If scale is (1, 1, 1), nothing changes
221+
# If translate is (0, 0, 0)
221222
# If scale is anything else, this makes the click still match the
222223
# correct label
224+
# translate before scale
223225
scaled_position = tuple(
224-
pos / scale for pos, scale in zip(event.position, labels_layer.scale)
226+
(pos - trans) / scale
227+
for pos, trans, scale in zip(
228+
event.position, labels_layer.translate, labels_layer.scale
229+
)
225230
)
226231
label = labels_layer.get_value(scaled_position)
227232
if label == 0 or not label:
228-
napari_info("No label clicked.")
233+
napari_info(f"No label clicked on the {labels_layer} label layer.")
229234
return
230235

231236
# Left click: add annotation
@@ -269,6 +274,8 @@ def _init_annotation(self, label_layer: napari.layers.Labels):
269274
# label_layer.opacity = 0.4
270275
self._annotations_layer.data = label_layer.data
271276
self._annotations_layer.scale = label_layer.scale
277+
self._annotations_layer.translate = label_layer.translate
278+
272279
reset_display_colormaps(
273280
label_layer,
274281
feature_col="annotations",

src/napari_feature_classifier/classifier_widget.py

+1
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ def _init_prediction_layer(self, label_layer: napari.layers.Labels):
424424
# Update the label data in the prediction layer
425425
self._prediction_layer.data = label_layer.data
426426
self._prediction_layer.scale = label_layer.scale
427+
self._prediction_layer.translate = label_layer.translate
427428

428429
# Update the colormap of the prediction layer
429430
reset_display_colormaps(

0 commit comments

Comments
 (0)