napari-topograph/plugin.py
def update_plots(self, xmin_w: int, xmax_w: int, ymin_w: int, ymax_w: int) -> None:
        """
        We have to be careful not to mix up world and pixel coords. World coordinates
        are explicitly indicated as such.

        TODO: there is a substantial bug here where any overlapping regions between
        dask arrays are doubly counted.
        """

        min_w = np.asarray([ymin_w, xmin_w])
        max_w = np.asarray([ymax_w, xmax_w])

        histograms: Dict[Tuple[int, int], np.ndarray] = {}

        regions: List[da.Array] = []

        image_layers = [layer for layer in self.layers if isinstance(layer, Image)]
        for layer in image_layers:
            mpp: np.ndarray = layer.scale[1:]
            translate_w: np.ndarray = layer.translate[1:]

            # translate rect bounds from global to image
            upper_left_w = np.subtract(min_w, translate_w)
            lower_right_w = np.subtract(max_w, translate_w)

            # scale rect bounds from world to pixel coordinates
            upper_left = np.divide(upper_left_w, mpp)
            lower_right = np.divide(lower_right_w, mpp)

            # make them integers
            upper_left = np.round(upper_left).astype(int)
            lower_right = np.round(lower_right).astype(int)
            lower_right += 1

            # clip to image bounds
            image: da.Array = layer.data[0]
            upper_left = np.minimum(np.maximum(upper_left, 0), image.shape[1:])
            lower_right = np.minimum(np.maximum(lower_right, 0), image.shape[1:])

            # stop early
            if np.any(upper_left == lower_right):
                continue
            # if (
            #     np.all(self._upper_left == upper_left)
            #     and np.all(self._lower_right == lower_right)
            # ) or np.any(upper_left == lower_right):
            #     continue
            # self._upper_left = upper_left
            # self._lower_right = lower_right

            # crop
            region = image[
                :,
                upper_left[0] : lower_right[0],
                upper_left[1] : lower_right[1],
            ]
            regions.append(region)

        hist1d: Delayed = da.sum(
            da.stack(
                [
                    da.stack(
                        [
                            dask_hist1d(region[i, ...], range=(0, 256), bins=256)[0]
                            for i in range(self.n_channels)
                        ]
                    )
                    for region in regions
                ]
            ),
            axis=0,
        )

        indices = [
            (row, col)
            for row in range(self.n_channels)
            for col in range(self.n_channels)
            if row > col
        ]
        hist2d: Delayed = da.sum(
            da.stack(
                [
                    da.stack(
                        [
                            dask_hist2d(
                                x=region[col, ...],
                                y=region[row, ...],
                                range=[(0, 256), (0, 256)],
                                bins=256,
                            )
                            for row, col in indices
                        ]
                    )
                    for region in regions
                ]
            ),
            axis=0,
        )

        hist1d, hist2d = da.compute(hist1d, hist2d)
        hist1d: np.ndarray
        hist2d: np.ndarray
        hist1d = np.log10(hist1d)
        hist1d[~np.isfinite(hist1d)] = 0
        hist2d = np.log10(hist2d)
        hist2d[~np.isfinite(hist2d)] = 0

        for row in range(self.n_channels):
            histograms[(row, row)] = hist1d[row, ...]

        for i, (row, col) in enumerate(indices):
            histograms[(row, col)] = hist2d[i, ...]

        self.plotLayout.updatePlots(histograms)

        We have to be careful not to mix up world and pixel coords. World coordinates
        are explicitly indicated as such.

        TODO: there is a substantial bug here where any overlapping regions between
        dask arrays are doubly counted.
        
We have to be careful not to mix up world and pixel coords. World coordinates
are explicitly indicated as such.

there is a substantial bug here where any overlapping regions between
dask arrays are doubly counted.
We have to be careful not to mix up world and pixel coords . World coordinates are explicitly indicated as such . there is a substantial bug here where any overlapping regions between dask arrays are doubly counted .
def update_plots(self, xmin_w: int, xmax_w: int, ymin_w: int, ymax_w: int) -> None:
        min_w = np.asarray([ymin_w, xmin_w])
        max_w = np.asarray([ymax_w, xmax_w])
        histograms: Dict[Tuple[int, int], np.ndarray] = {}
        regions: List[da.Array] = []
        image_layers = [layer for layer in self.layers if isinstance(layer, Image)]
        for layer in image_layers:
            mpp: np.ndarray = layer.scale[1:]
            translate_w: np.ndarray = layer.translate[1:]
            upper_left_w = np.subtract(min_w, translate_w)
            lower_right_w = np.subtract(max_w, translate_w)
            upper_left = np.divide(upper_left_w, mpp)
            lower_right = np.divide(lower_right_w, mpp)
            upper_left = np.round(upper_left).astype(int)
            lower_right = np.round(lower_right).astype(int)
            lower_right += 1
            image: da.Array = layer.data[0]
            upper_left = np.minimum(np.maximum(upper_left, 0), image.shape[1:])
            lower_right = np.minimum(np.maximum(lower_right, 0), image.shape[1:])
            if np.any(upper_left == lower_right):
                continue
            region = image[
                :,
                upper_left[0] : lower_right[0],
                upper_left[1] : lower_right[1],
            ]
            regions.append(region)
        hist1d: Delayed = da.sum(
            da.stack(
                [
                    da.stack(
                        [
                            dask_hist1d(region[i, ...], range=(0, 256), bins=256)[0]
                            for i in range(self.n_channels)
                        ]
                    )
                    for region in regions
                ]
            ),
            axis=0,
        )
        indices = [
            (row, col)
            for row in range(self.n_channels)
            for col in range(self.n_channels)
            if row > col
        ]
        hist2d: Delayed = da.sum(
            da.stack(
                [
                    da.stack(
                        [
                            dask_hist2d(
                                x=region[col, ...],
                                y=region[row, ...],
                                range=[(0, 256), (0, 256)],
                                bins=256,
                            )
                            for row, col in indices
                        ]
                    )
                    for region in regions
                ]
            ),
            axis=0,
        )
        hist1d, hist2d = da.compute(hist1d, hist2d)
        hist1d: np.ndarray
        hist2d: np.ndarray
        hist1d = np.log10(hist1d)
        hist1d[~np.isfinite(hist1d)] = 0
        hist2d = np.log10(hist2d)
        hist2d[~np.isfinite(hist2d)] = 0
        for row in range(self.n_channels):
            histograms[(row, row)] = hist1d[row, ...]
        for i, (row, col) in enumerate(indices):
            histograms[(row, col)] = hist2d[i, ...]
        self.plotLayout.updatePlots(histograms)
def update_plots ( self , xmin_w : int , xmax_w : int , ymin_w : int , ymax_w : int ) -> None : min_w = np . asarray ( [ ymin_w , xmin_w ] ) max_w = np . asarray ( [ ymax_w , xmax_w ] ) histograms : Dict [ Tuple [ int , int ] , np . ndarray ] = { } regions : List [ da . Array ] = [ ] image_layers = [ layer for layer in self . layers if isinstance ( layer , Image ) ] for layer in image_layers : mpp : np . ndarray = layer . scale [ 1 : ] translate_w : np . ndarray = layer . translate [ 1 : ] upper_left_w = np . subtract ( min_w , translate_w ) lower_right_w = np . subtract ( max_w , translate_w ) upper_left = np . divide ( upper_left_w , mpp ) lower_right = np . divide ( lower_right_w , mpp ) upper_left = np . round ( upper_left ) . astype ( int ) lower_right = np . round ( lower_right ) . astype ( int ) lower_right += 1 image : da . Array = layer . data [ 0 ] upper_left = np . minimum ( np . maximum ( upper_left , 0 ) , image . shape [ 1 : ] ) lower_right = np . minimum ( np . maximum ( lower_right , 0 ) , image . shape [ 1 : ] ) if np . any ( upper_left == lower_right ) : continue region = image [ : , upper_left [ 0 ] : lower_right [ 0 ] , upper_left [ 1 ] : lower_right [ 1 ] , ] regions . append ( region ) hist1d : Delayed = da . sum ( da . stack ( [ da . stack ( [ dask_hist1d ( region [ i , ... ] , range = ( 0 , 256 ) , bins = 256 ) [ 0 ] for i in range ( self . n_channels ) ] ) for region in regions ] ) , axis = 0 , ) indices = [ ( row , col ) for row in range ( self . n_channels ) for col in range ( self . n_channels ) if row > col ] hist2d : Delayed = da . sum ( da . stack ( [ da . stack ( [ dask_hist2d ( x = region [ col , ... ] , y = region [ row , ... ] , range = [ ( 0 , 256 ) , ( 0 , 256 ) ] , bins = 256 , ) for row , col in indices ] ) for region in regions ] ) , axis = 0 , ) hist1d , hist2d = da . compute ( hist1d , hist2d ) hist1d : np . ndarray hist2d : np . ndarray hist1d = np . log10 ( hist1d ) hist1d [ ~ np . isfinite ( hist1d ) ] = 0 hist2d = np . log10 ( hist2d ) hist2d [ ~ np . isfinite ( hist2d ) ] = 0 for row in range ( self . n_channels ) : histograms [ ( row , row ) ] = hist1d [ row , ... ] for i , ( row , col ) in enumerate ( indices ) : histograms [ ( row , col ) ] = hist2d [ i , ... ] self . plotLayout . updatePlots ( histograms )
We have to be careful not to mix up world and pixel coords.
We have to be careful not to mix up world and pixel coords .
"""
        We have to be careful not to mix up world and pixel coords. World coordinates
        are explicitly indicated as such.

        TODO: there is a substantial bug here where any overlapping regions between
        dask arrays are doubly counted.
        """ # translate rect bounds from global to image # scale rect bounds from world to pixel coordinates # make them integers # clip to image bounds # stop early # if ( #     np.all(self._upper_left == upper_left) #     and np.all(self._lower_right == lower_right) # ) or np.any(upper_left == lower_right): #     continue # self._upper_left = upper_left # self._lower_right = lower_right # crop
self
xmin_w
int
xmax_w
int
ymin_w
int
ymax_w
int