Skip to content

fix: Fix scale bar migration for canvas change in napari 0.8.1 - #1439

Merged
Czaki merged 3 commits into
developfrom
fix_scale_bar
Jul 29, 2026
Merged

fix: Fix scale bar migration for canvas change in napari 0.8.1#1439
Czaki merged 3 commits into
developfrom
fix_scale_bar

Conversation

@Czaki

@Czaki Czaki commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Summary by Sourcery

Update napari image viewer integration to support API changes introduced in napari 0.8.1 while simplifying version-dependent rendering logic.

Bug Fixes:

  • Fix scale bar tick configuration for napari versions above 0.8.0 by using the new canvas overlay API.

Enhancements:

  • Simplify rendering and view access by removing legacy napari version branching and unifying on the scene canvas implementation.

Summary by CodeRabbit

  • Bug Fixes
    • Improved scale-bar tick configuration so tick display is consistent across supported Napari versions.
    • Fixed rendering and view wiring to behave consistently in newer Napari environments.
    • Reduced version-dependent differences in image display behavior for a more stable viewing experience.

@Czaki Czaki added this to the 0.17.1 milestone Jul 28, 2026
@sourcery-ai

sourcery-ai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates napari version handling and scale bar access to be compatible with napari 0.8.1’s canvas/overlay changes, while simplifying the render/view accessors to always use the scene canvas path.

Sequence diagram for updated scale bar tick handling across napari versions

sequenceDiagram
    participant Settings
    participant NapariImageView
    participant NapariViewer
    participant Canvas
    participant ScaleBar

    Settings->>NapariImageView: _update_scale_bar_ticks()
    NapariImageView->>Settings: get_from_profile(scale_bar_ticks, True)
    Settings-->>NapariImageView: ticks_value

    alt _napari_gt_8_0
        NapariImageView->>NapariViewer: canvas
        NapariViewer-->>NapariImageView: Canvas
        NapariImageView->>Canvas: overlays.scale_bar.ticks = ticks_value
    else not _napari_gt_8_0
        NapariImageView->>NapariViewer: scale_bar
        NapariViewer-->>NapariImageView: ScaleBar
        NapariImageView->>ScaleBar: ticks = ticks_value
    end
Loading

File-Level Changes

Change Details Files
Add version gate for napari > 0.8.0 and adjust scale bar tick access accordingly.
  • Introduce a new boolean flag comparing napari version to 0.8.0 to detect newer APIs.
  • Update scale bar tick update logic to use viewer.canvas.overlays.scale_bar.ticks for napari > 0.8.0.
  • Preserve legacy behavior by falling back to viewer.scale_bar.ticks for older napari versions.
package/PartSeg/common_gui/napari_image_view.py
Simplify rendering and view access to always use the scene canvas for napari >=0.5.
  • Remove conditional render logic and unconditionally call self.canvas._scene_canvas.render() in _render.
  • Hoist the view property definition out of the version guard so it is always available and consistently returns self.canvas.view.
  • Drop support for the older self.canvas.render() call path that was used for napari <0.5.
package/PartSeg/common_gui/napari_image_view.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 862af0fc-9cb6-44d4-a462-1cf2d6e3ccd3

📥 Commits

Reviewing files that changed from the base of the PR and between 4b0728b and 9922c6b.

📒 Files selected for processing (1)
  • package/PartSeg/common_gui/napari_image_view.py
💤 Files with no reviewable changes (1)
  • package/PartSeg/common_gui/napari_image_view.py

📝 Walkthrough

Walkthrough

Napari compatibility handling now branches scale-bar tick updates by version, while NapariQtViewer always uses the canvas scene renderer and view.

Changes

Napari compatibility

Layer / File(s) Summary
Napari API compatibility wiring
package/PartSeg/common_gui/napari_image_view.py
Adds a Napari version gate for scale-bar tick configuration and makes viewer rendering and view access use the canvas APIs unconditionally.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adapting scale bar handling to Napari 0.8.1 canvas API changes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix_scale_bar

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sourcery-ai sourcery-ai Bot 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.

Hey - I've found 1 issue, and left some high level feedback:

  • The _napari_ge_5 and _napari_le_7_0 version flags appear to no longer be used after the _render simplification; consider removing them (and any related dead code) to keep the version handling clear and minimal.
  • If older napari versions (<0.5) are still supported, the change to always call self.canvas._scene_canvas.render() in _render may break those environments; either restore a version guard or explicitly drop support for those versions and remove related compatibility code.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `_napari_ge_5` and `_napari_le_7_0` version flags appear to no longer be used after the `_render` simplification; consider removing them (and any related dead code) to keep the version handling clear and minimal.
- If older napari versions (<0.5) are still supported, the change to always call `self.canvas._scene_canvas.render()` in `_render` may break those environments; either restore a version guard or explicitly drop support for those versions and remove related compatibility code.

## Individual Comments

### Comment 1
<location path="package/PartSeg/common_gui/napari_image_view.py" line_range="235-239" />
<code_context>

     def _update_scale_bar_ticks(self):
-        self.viewer.scale_bar.ticks = self.settings.get_from_profile("scale_bar_ticks", True)
+        if _napari_gt_8_0:
+            self.viewer.canvas.overlays.scale_bar.ticks = self.settings.get_from_profile("scale_bar_ticks", True)
+        else:
+            self.viewer.scale_bar.ticks = self.settings.get_from_profile("scale_bar_ticks", True)

     def toggle_points_visibility(self):
</code_context>
<issue_to_address>
**suggestion:** Avoid recomputing the profile value twice in `_update_scale_bar_ticks`.

`self.settings.get_from_profile("scale_bar_ticks", True)` is called in both branches. Assign it to a local variable before the `if` (e.g. `ticks = ...`) and reuse it to avoid duplication and simplify maintenance.

```suggestion
    def _update_scale_bar_ticks(self):
        ticks = self.settings.get_from_profile("scale_bar_ticks", True)
        if _napari_gt_8_0:
            self.viewer.canvas.overlays.scale_bar.ticks = ticks
        else:
            self.viewer.scale_bar.ticks = ticks
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread package/PartSeg/common_gui/napari_image_view.py Outdated

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
package/PartSeg/common_gui/napari_image_view.py (1)

235-239: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add regression coverage for both scale-bar API branches.

The existing package/tests/test_PartSeg/test_common_gui.py:857-865 test verifies only the profile value; it would not catch a failure while assigning ticks to the newer canvas path. Test both the legacy viewer.scale_bar branch and the Napari 0.8.1 Qt-canvas branch.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@package/PartSeg/common_gui/napari_image_view.py` around lines 235 - 239,
Extend the tests for _update_scale_bar_ticks to cover both _napari_gt_8_0
branches: verify the legacy viewer.scale_bar.ticks assignment and the newer
viewer.canvas.overlays.scale_bar.ticks assignment, using the configured profile
value in each case.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@package/PartSeg/common_gui/napari_image_view.py`:
- Around line 235-239: Update the newer-Napari branch in _update_scale_bar_ticks
to access the canvas through the QtViewer stored in self.viewer_widget, rather
than self.viewer; preserve the existing scale_bar_ticks setting and legacy
self.viewer.scale_bar path.

---

Nitpick comments:
In `@package/PartSeg/common_gui/napari_image_view.py`:
- Around line 235-239: Extend the tests for _update_scale_bar_ticks to cover
both _napari_gt_8_0 branches: verify the legacy viewer.scale_bar.ticks
assignment and the newer viewer.canvas.overlays.scale_bar.ticks assignment,
using the configured profile value in each case.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 270a2d75-53fc-400d-98ef-bce7eb66c2f8

📥 Commits

Reviewing files that changed from the base of the PR and between 46d8d79 and c68464d.

📒 Files selected for processing (1)
  • package/PartSeg/common_gui/napari_image_view.py

Comment thread package/PartSeg/common_gui/napari_image_view.py Outdated
@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.56%. Comparing base (46d8d79) to head (9922c6b).

Files with missing lines Patch % Lines
package/PartSeg/common_gui/napari_image_view.py 88.88% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1439      +/-   ##
===========================================
- Coverage    92.58%   92.56%   -0.02%     
===========================================
  Files          211      211              
  Lines        33224    33224              
===========================================
- Hits         30760    30755       -5     
- Misses        2464     2469       +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sonarqubecloud

Copy link
Copy Markdown

@Czaki
Czaki merged commit 3df1223 into develop Jul 29, 2026
55 of 57 checks passed
@Czaki
Czaki deleted the fix_scale_bar branch July 29, 2026 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant