Skip to content

Fix trajectory plot coordinates and styling#159

Draft
C-Achard wants to merge 8 commits intomainfrom
cy/fix-traj-plot-y-axis
Draft

Fix trajectory plot coordinates and styling#159
C-Achard wants to merge 8 commits intomainfrom
cy/fix-traj-plot-y-axis

Conversation

@C-Achard
Copy link
Copy Markdown
Collaborator

@C-Achard C-Achard commented Jan 22, 2026

A small update to the trajectory plotting utility, mostly for consistency and performance.

  • Fixed Y plot coordinates not matching viewer coordinates
  • Fixed orientation of trajectories (was upside down)
  • Fixed broken styling changes introduced by mistake, ensuring axes and labels are color based on current napari theme.
  • Avoids always updating the plot even when not shown for performance

Also updated API for finding layers in code, centralizing and ensuring consistency.


Before After
image image

Before: light blue track shown on top instead of bottom (flipped Y), incorrect vline color, and inconsistent axis with viewer coordinates.
After: trajectories match viewer coordinates/orientation, and styling correctly matches the current napari theme


Several plotting and UI behaviors are improved for robustness and better integration with napari's theming.
This pull request also refactors the way layers are found and filtered throughout the napari_deeplabcut codebase by introducing new utility functions (find_layer, find_layers, and find_layers_of_types) in misc.py. These new functions replace repetitive layer-searching code in _widgets.py, resulting in cleaner, more maintainable, and less error-prone logic.

The most important changes are:

Layer Selection Refactor:

  • Added find_layer, find_layers, and find_layers_of_types utility functions to misc.py for robust and reusable layer searching/filtering by type and predicate.
  • Replaced all manual layer iteration and type-checking in _widgets.py with calls to the new utility functions, simplifying code in methods such as _load_dataframe, _update_slider_max, load_superkeypoints_diagram, _map_keypoints, _extract_single_frame, _store_crop_coordinates, and color mode/colormap updates. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]

Plotting and UI Improvements:

  • Improved matplotlib plot initialization and update logic: now robustly resets axes, applies napari theme colors to axes/titles, and correctly handles y-axis orientation based on image stack shape. [1] [2] [3]
  • The plot update method (update_plot_range) now supports a force parameter to allow updates even when the widget is hidden, improving reliability when toggling the plot canvas. [1] [2]
  • Ensured correct slider maximum calculation by using the new layer search utilities and robustly handling 3D image stacks.

Maintenance and Readability:

  • Removed duplicated or error-prone code blocks for layer lookup, reducing code complexity and the chance of future bugs. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

@C-Achard C-Achard self-assigned this Jan 22, 2026
@C-Achard C-Achard added enhancement New feature or request bug fix Fixes an issue or a bug labels Jan 22, 2026
@C-Achard C-Achard requested a review from Copilot January 22, 2026 14:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes trajectory plot coordinate system issues and improves code maintainability by centralizing layer search logic. The trajectory plot now correctly matches napari's coordinate system (Y-axis orientation and coordinate values), applies theme-based styling consistently, and avoids unnecessary updates when hidden.

Changes:

  • Fixed trajectory plot Y-axis to match napari viewer coordinates and orientation
  • Centralized layer searching with new utility functions (find_layer, find_layers, find_layers_of_types)
  • Improved plot performance by skipping updates when canvas is hidden

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/napari_deeplabcut/misc.py Added three utility functions for finding layers by type and optional predicate
src/napari_deeplabcut/_widgets.py Replaced manual layer iteration with utility functions; fixed plot axis orientation and theme styling; added force parameter to plot updates
src/napari_deeplabcut/_tests/test_widgets.py Updated test to verify plot doesn't update when hidden unless forced

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@C-Achard C-Achard requested a review from Copilot January 22, 2026 14:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 4 out of 4 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

C-Achard and others added 6 commits January 23, 2026 09:28
Introduced find_layer, find_layers, and find_layers_of_types utilities in misc.py to simplify and standardize layer selection logic throughout _widgets.py. Replaced multiple for-loops and isinstance checks with these new functions, improving code readability and maintainability.
Added a visibility check to KeypointMatplotlibCanvas.update_plot_range to prevent unnecessary updates when the widget is hidden, unless forced. Ensures plot updates only occur when the canvas is visible or explicitly requested, improving performance.
Improves robustness by handling cases where the points layer or its data attribute may be None or empty. Also corrects logical checks for found neighbors and simplifies color scheme updates.
Replaces np.issubdtype with pandas' is_numeric_dtype to more robustly check if image_paths is numeric in read_hdf. This improves compatibility with different pandas index types.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@C-Achard C-Achard force-pushed the cy/fix-traj-plot-y-axis branch from 23b113c to 708a455 Compare January 23, 2026 08:28
@C-Achard C-Achard requested a review from Copilot January 23, 2026 13:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 4 out of 4 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Enhanced the handling of keypoint trajectory extraction in KeypointMatplotlibCanvas to ensure only 1D trajectories are plotted and to skip empty selections. Updated data presence checks in KeypointControls to use .size for better reliability. Improved dtype handling in read_hdf for image_paths, and made shape_type checks more robust for Shapes layers.
Refactored the trajectory plotting code in KeypointMatplotlibCanvas to remove unnecessary checks and conversions, streamlining the extraction and plotting of keypoint data.
@C-Achard C-Achard marked this pull request as ready for review January 23, 2026 16:14
@C-Achard C-Achard requested a review from deruyter92 January 23, 2026 16:14
@C-Achard C-Achard marked this pull request as draft February 19, 2026 13:18
@C-Achard C-Achard marked this pull request as draft February 19, 2026 13:18
@C-Achard C-Achard mentioned this pull request Mar 19, 2026
18 tasks
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 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants