Skip to content

Fix: cannot add point label after selecting a keypoint - #234

Merged
C-Achard merged 6 commits into
mainfrom
cy/fix-selected-label-switch
Aug 12, 2026
Merged

Fix: cannot add point label after selecting a keypoint#234
C-Achard merged 6 commits into
mainfrom
cy/fix-selected-label-switch

Conversation

@C-Achard

Copy link
Copy Markdown
Collaborator

This PR fixes keypoint switching and out-of-order annotation when Napari events occur during an add.
Closes #233

Fix

  • Update next point properties inside napari's block_update_properties() so selected points are not relabeled and remain selected
  • Captures the requested (bodypart, individual) pair once at the start of add(), such that duplicate detection, property assignment, and QUICK-mode lookup use a consistent keypoint
  • Resolves the backing layer once per operation without rebinding it

Why

Previously, add() read current_keypoint multiple times. Updating layer data could trigger events that changed the current properties mid-operation, causing the new point to receive an incorrect pair or leaving the store in an inconsistent sequence state.

Tests

Regression tests added:

  • Switching the complete (bodypart, individual) pair does not relabel or deselect an existing point
  • Changing only the individual preserves the bodypart and existing annotations
  • Switching to an already annotated keypoint does not modify point data or properties
  • Changing the current keypoint does not affect another independently managed Points layer

Add a dedicated helper to update `layer.current_properties` for the next keypoint while preserving the current selection via `block_update_properties()`. This fixes selected points being unintentionally edited when changing current label/id defaults, and consolidates duplicated setter logic for `current_keypoint`, `current_label`, and `current_id`.
Extend keypoints store tests to cover selection-safety when changing `current_keypoint` and `current_id`. The new cases assert that switching keypoints does not relabel selected points, does not mutate point data/properties when selecting an existing keypoint, and does not affect non-active layers.
Refactors keypoint default updates to capture the requested keypoint before mutating layer properties, then compares it against the actual layer state and emits a warning when they diverge. Also snapshots the current keypoint and annotated set before append logic, so annotation decisions and appended properties use a consistent keypoint view.
@C-Achard C-Achard self-assigned this Aug 10, 2026
@C-Achard C-Achard added the bug fix Fixes an issue or a bug label Aug 10, 2026
@C-Achard
C-Achard requested a balanced review from Copilot August 10, 2026 13:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes keypoint switching while points are selected and stabilizes annotation properties during point addition.

Changes:

  • Updates current properties without relabeling selected points.
  • Snapshots the requested keypoint during annotation.
  • Adds regression tests for selection and layer isolation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/napari_deeplabcut/core/keypoints.py Revises keypoint property updates and add behavior.
src/napari_deeplabcut/_tests/core/test_keypoints.py Adds keypoint-switching regression tests.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/napari_deeplabcut/core/keypoints.py
Add two core keypoint store tests for sequential labeling behavior. One verifies adding keypoints out of header order preserves the explicitly selected label/id for each added point. The other verifies that when a non-first keypoint is manually selected, add() uses that pair for insertion and then advances to the next keypoint in sequence.
Make `KeypointStore.add()` advance from the keypoint requested at the start of the call, even if layer property events mutate `current_keypoint` during the operation. This also fixes QUICK mode updates to index from the captured requested keypoint instead of the potentially changed current one. Added `_next_keypoint_after()` to centralize safe next-keypoint lookup (with a warning for out-of-sequence keypoints) and added a regression test covering the event-callback race case.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (1)

src/napari_deeplabcut/core/keypoints.py:330

  • prev_keypoint() still calls self._keypoints.index(self.current_keypoint) without handling ValueError. Since next_keypoint() now explicitly handles the case where current_keypoint is outside the configured sequence, prev_keypoint() can still crash in the same scenario (e.g., if events or malformed current_properties produce a keypoint not present in self._keypoints). Consider mirroring the defensive behavior here as well.
    def prev_keypoint(self, *args):
        ind = self._keypoints.index(self.current_keypoint) - 1
        if ind >= 0:
            self.current_keypoint = self._keypoints[ind]

@C-Achard
C-Achard marked this pull request as ready for review August 10, 2026 14:37
@C-Achard
C-Achard requested a review from deruyter92 August 10, 2026 14:37

@deruyter92 deruyter92 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good fix!

@C-Achard
C-Achard merged commit d50015d into main Aug 12, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug fix Fixes an issue or a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keypoint cannot be switched (dropdown / next / prev) while a point is selected — labels silently go to the wrong bodypart

3 participants