napari-topograph/__init__.py
def build_plugin_(viewer: Viewer) -> None:
    """
    This part of the code sort of ended up as the controller for the plugin.
    """
    # validate state
    image_layers = [l for l in viewer.layers if isinstance(l, Image)]
    if len(image_layers) < 1:
        return
    layer: Image = image_layers[0]  # the image to visualize is the first
    image: np.ndarray = layer.data
    if image.ndim != 3:
        return
    if image.shape[2] <= 1:
        return

    n_channels = image.shape[2]

    # set up dock widgets
    topograph_pixels = _TopographPixels(n_channels)
    topograph_cells = TopographCells()

    viewer.window.add_dock_widget(
        topograph_pixels.view, name="TopographPixels", area="right"
    )
    # viewer.window.add_dock_widget(topograph_cells, name='TopographCells', area='right')

    init_rect = np.asarray([[0, 0], [0, 500], [500, 500], [500, 0]])

    shapes_layer: Shapes = viewer.add_shapes(
        [init_rect],
        shape_type="rectangle",
        name="Annotations",
        ndim=2,
        edge_color=[(1, 1, 1, 1)],  # black
        face_color=[(0, 0, 0, 0)],  # clear
    )

    _shape: Optional[np.ndarray] = None

    def update_plots():
        # at least one rectangle
        shapes: List[np.ndarray] = shapes_layer.data
        shapes: List[np.ndarray] = [s for s in shapes if is_rectangle(s)]
        if len(shapes) == 0:
            return

        # check if it's changed at all
        shape: np.ndarray = shapes[0]
        nonlocal _shape
        if (shape == _shape).all():
            return
        _shape = np.copy(shape)

        # get shape extents
        ymin, ymax = shape[:, 0].min(), shape[:, 0].max()
        ymin, ymax = int(round(ymin)), int(round(ymax))
        # ymin, ymax = int(round(ymin)), int(round(ymax)) + 1

        xmin, xmax = shape[:, 1].min(), shape[:, 1].max()
        xmin, xmax = int(round(xmin)), int(round(xmax))
        # xmin, xmax = int(round(xmin)), int(round(xmax)) + 1

        # clip extents
        ymin = min(max(ymin, 0), image.shape[0])
        ymax = max(min(ymax, image.shape[0]), 0)

        xmin = min(max(xmin, 0), image.shape[1])
        xmax = max(min(xmax, image.shape[1]), 0)

        # get desired region of image
        region = image[ymin:ymax, xmin:xmax, :]
        topograph_pixels.plotLayout.updatePlots(region)

    def on_change(event: Event) -> None:
        event_type = getattr(event, "type", None)
        if event_type != "set_data":
            return

        update_plots()

    shapes_layer.events.connect(on_change)
    update_plots()

    This part of the code sort of ended up as the controller for the plugin.
    
This part of the code sort of ended up as the controller for the plugin.
This part of the code sort of ended up as the controller for the plugin .
def build_plugin_(viewer: Viewer) -> None:
    image_layers = [l for l in viewer.layers if isinstance(l, Image)]
    if len(image_layers) < 1:
        return
    layer: Image = image_layers[0]  
    image: np.ndarray = layer.data
    if image.ndim != 3:
        return
    if image.shape[2] <= 1:
        return
    n_channels = image.shape[2]
    topograph_pixels = _TopographPixels(n_channels)
    topograph_cells = TopographCells()
    viewer.window.add_dock_widget(
        topograph_pixels.view, name="TopographPixels", area="right"
    )
    init_rect = np.asarray([[0, 0], [0, 500], [500, 500], [500, 0]])
    shapes_layer: Shapes = viewer.add_shapes(
        [init_rect],
        shape_type="rectangle",
        name="Annotations",
        ndim=2,
        edge_color=[(1, 1, 1, 1)],  
        face_color=[(0, 0, 0, 0)],  
    )
    _shape: Optional[np.ndarray] = None
    def update_plots():
        shapes: List[np.ndarray] = shapes_layer.data
        shapes: List[np.ndarray] = [s for s in shapes if is_rectangle(s)]
        if len(shapes) == 0:
            return
        shape: np.ndarray = shapes[0]
        nonlocal _shape
        if (shape == _shape).all():
            return
        _shape = np.copy(shape)
        ymin, ymax = shape[:, 0].min(), shape[:, 0].max()
        ymin, ymax = int(round(ymin)), int(round(ymax))
        xmin, xmax = shape[:, 1].min(), shape[:, 1].max()
        xmin, xmax = int(round(xmin)), int(round(xmax))
        ymin = min(max(ymin, 0), image.shape[0])
        ymax = max(min(ymax, image.shape[0]), 0)
        xmin = min(max(xmin, 0), image.shape[1])
        xmax = max(min(xmax, image.shape[1]), 0)
        region = image[ymin:ymax, xmin:xmax, :]
        topograph_pixels.plotLayout.updatePlots(region)
    def on_change(event: Event) -> None:
        event_type = getattr(event, "type", None)
        if event_type != "set_data":
            return
        update_plots()
    shapes_layer.events.connect(on_change)
    update_plots()
def build_plugin_ ( viewer : Viewer ) -> None : image_layers = [ l for l in viewer . layers if isinstance ( l , Image ) ] if len ( image_layers ) < 1 : return layer : Image = image_layers [ 0 ] image : np . ndarray = layer . data if image . ndim != 3 : return if image . shape [ 2 ] <= 1 : return n_channels = image . shape [ 2 ] topograph_pixels = _TopographPixels ( n_channels ) topograph_cells = TopographCells ( ) viewer . window . add_dock_widget ( topograph_pixels . view , name = "TopographPixels" , area = "right" ) init_rect = np . asarray ( [ [ 0 , 0 ] , [ 0 , 500 ] , [ 500 , 500 ] , [ 500 , 0 ] ] ) shapes_layer : Shapes = viewer . add_shapes ( [ init_rect ] , shape_type = "rectangle" , name = "Annotations" , ndim = 2 , edge_color = [ ( 1 , 1 , 1 , 1 ) ] , face_color = [ ( 0 , 0 , 0 , 0 ) ] , ) _shape : Optional [ np . ndarray ] = None def update_plots ( ) : shapes : List [ np . ndarray ] = shapes_layer . data shapes : List [ np . ndarray ] = [ s for s in shapes if is_rectangle ( s ) ] if len ( shapes ) == 0 : return shape : np . ndarray = shapes [ 0 ] nonlocal _shape if ( shape == _shape ) . all ( ) : return _shape = np . copy ( shape ) ymin , ymax = shape [ : , 0 ] . min ( ) , shape [ : , 0 ] . max ( ) ymin , ymax = int ( round ( ymin ) ) , int ( round ( ymax ) ) xmin , xmax = shape [ : , 1 ] . min ( ) , shape [ : , 1 ] . max ( ) xmin , xmax = int ( round ( xmin ) ) , int ( round ( xmax ) ) ymin = min ( max ( ymin , 0 ) , image . shape [ 0 ] ) ymax = max ( min ( ymax , image . shape [ 0 ] ) , 0 ) xmin = min ( max ( xmin , 0 ) , image . shape [ 1 ] ) xmax = max ( min ( xmax , image . shape [ 1 ] ) , 0 ) region = image [ ymin : ymax , xmin : xmax , : ] topograph_pixels . plotLayout . updatePlots ( region ) def on_change ( event : Event ) -> None : event_type = getattr ( event , "type" , None ) if event_type != "set_data" : return update_plots ( ) shapes_layer . events . connect ( on_change ) update_plots ( )
This part of the code sort of ended up as the controller for the plugin.
This part of the code sort of ended up as the controller for the plugin .
"""
    This part of the code sort of ended up as the controller for the plugin.
    """ # validate state # the image to visualize is the first # set up dock widgets # viewer.window.add_dock_widget(topograph_cells, name='TopographCells', area='right') # black # clear # at least one rectangle # check if it's changed at all # get shape extents # ymin, ymax = int(round(ymin)), int(round(ymax)) + 1 # xmin, xmax = int(round(xmin)), int(round(xmax)) + 1 # clip extents # get desired region of image
viewer
Viewer