Skip to content

Conversation

melonora
Copy link
Owner

@melonora melonora commented May 5, 2025

References and relevant issues

Description

Summary by Sourcery

Implement a new multichannel grid canvas feature for displaying layers in a configurable grid layout

New Features:

  • Add MultiChannelGridCanvas class to support advanced grid display of layers
  • Introduce grid configuration options like stride, shape, and enable/disable functionality

Enhancements:

  • Extend ViewerModel to support multichannel grid canvas
  • Modify VispyCanvas to dynamically create and manage grid views
  • Add flexible grid positioning and layout calculations

Copy link

sourcery-ai bot commented May 5, 2025

Reviewer's Guide

This pull request implements a multi-channel grid view feature. It introduces a new MultiChannelGridCanvas model to manage the grid state and logic. The VispyCanvas is updated to dynamically create a Vispy Grid layout with multiple linked ViewBoxes when this mode is enabled, assigning different layers to different views. When disabled, it reverts to the standard single view.

File-Level Changes

Change Details Files
Introduced MultiChannelGridCanvas model and integrated it into ViewerModel.
  • Added the MultiChannelGridCanvas class to define multi-channel grid state (enabled, stride, shape) and layout logic.
  • Added multi_channel_gridcanvas field to ViewerModel.
  • Initialized multi_channel_gridcanvas properties from application settings in ViewerModel.
napari/components/grid.py
napari/components/viewer_model.py
Implemented dynamic Vispy grid layout switching based on MultiChannelGridCanvas state.
  • Connected VispyCanvas to MultiChannelGridCanvas events.
  • Added _on_grid_change method to handle grid activation/deactivation.
  • Dynamically created/removed Vispy Grid and ViewBox elements.
  • Re-parented layer visual nodes between the single view and multiple grid views.
  • Linked cameras of multiple view boxes in grid mode.
  • Managed creation/deletion of associated VispyCamera objects for grid views.
napari/_vispy/canvas.py
Adapted coordinate mapping for the new grid view.
  • Modified _map_canvas2world to use the transform from the primary grid view when multi-channel grid mode is active.
napari/_vispy/canvas.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

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @melonora - I've reviewed your changes - here's some feedback:

  • Consider refactoring MultiChannelGridCanvas to reuse logic from the existing GridCanvas to reduce code duplication.
  • Clarify the distinction and relationship between the existing GridCanvas and the new MultiChannelGridCanvas.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

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 on lines +713 to +715
for y in range(grid_shape[0])
for x in range(grid_shape[1])
if x * y < n_gridboxes
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Review grid view index calculation in the list comprehension.

Use (y * grid_shape[1] + x) instead of x * y to compute the grid index and preserve row-major ordering.

Suggested change
for y in range(grid_shape[0])
for x in range(grid_shape[1])
if x * y < n_gridboxes
for y in range(grid_shape[0])
for x in range(grid_shape[1])
if y * grid_shape[1] + x < n_gridboxes

"""Enable playing of animation. False if awaiting a draw event"""
self.viewer.dims._play_ready = True

def _on_grid_change(self):
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider refactoring the _on_grid_change method by extracting the enabling/disabling logic into separate helper functions.

Refactor the method by offloading the enabling/disabling logic to helper functions. This will keep the functionality intact while reducing the inline branching and list comprehensions. For example:

def _enable_grid_mode(self):
    grid_shape, n_gridboxes = self.viewer.multi_channel_gridcanvas.actual_shape(
        len(self.layer_to_visual)
    )
    self.grid = self.central_widget.add_grid()
    camera = self.camera._view.camera
    self.grid_views = [
        self.grid.add_view(
            row=y,
            col=x,
            camera=camera if y == 0 and x == 0 else None
        )
        for y in range(grid_shape[0])
        for x in range(grid_shape[1])
        if x * y < n_gridboxes
    ]
    self.camera._view = self.grid_views[0]
    self.central_widget.remove_widget(self.view)
    self.grid_cameras = [
        VispyCamera(self.grid_views[i], self.viewer.camera, self.viewer.dims)
        for i in range(len(self.grid_views[1:]))
    ]
    for ind, layer in enumerate(self.layer_to_visual.values()):
        if ind != 0:
            self.grid_views[ind].camera = self.grid_cameras[ind - 1]._view.camera
            self.grid_views[ind].camera.link(self.grid_views[0].camera)
        layer.node.parent = self.grid_views[ind].scene

def _disable_grid_mode(self):
    for layer in self.layer_to_visual.values():
        layer.node.parent = self.view.scene
    self.central_widget.remove_widget(self.grid)
    self.central_widget.add_widget(self.view)
    self.camera._view = self.view
    # TODO: properly disconnect grid events and delete all viewboxes
    del self.grid
    for camera in self.grid_cameras:
        camera.disconnect()
        del camera
    # TODO: respect 3D camera if enabled
    self.camera._view.camera = self.camera._2D_camera

def _on_grid_change(self):
    """Change grid view"""
    if self.viewer.multi_channel_gridcanvas.enabled:
        self._enable_grid_mode()
    else:
        self._disable_grid_mode()

Actionable Steps:

  1. Create two focused helper functions: _enable_grid_mode and _disable_grid_mode.
  2. Move the corresponding code from _on_grid_change into these helper functions.
  3. In _on_grid_change, simply check the condition and call the appropriate helper.

This refactoring decouples responsibilities, making the control flow easier to follow and maintain.

def _on_grid_change(self):
"""Change grid view"""
if self.viewer.multi_channel_gridcanvas.enabled:
grid_shape, n_gridboxes = (
Copy link

Choose a reason for hiding this comment

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

issue (code-quality): Extract code out into method (extract-method)

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