Example #1
0
    def get_image(self, item: str, aligned_group: int = 0,
                  x_slice: Optional[Union[int, slice]] = None,
                  y_slice: Optional[Union[int, slice]] = None,
                  ) -> ImageStack:
        """
        Load into memory the Imagestack representation of an aligned image group. If crop parameters
        provided, first crop the TileSet.

        Parameters
        ----------
        item: str
            The name of the tileset ex. 'primary' or 'nuclei'
        aligned_group: int
            The aligned subgroup, default 0
        x_slice: int or slice
            The cropping parameters for the x axis
        y_slice:
            The cropping parameters for the y axis

        Returns
        -------
        ImageStack
            The instantiated image stack
        """
        crop_params = copy.copy((self.aligned_coordinate_groups[item][aligned_group]))
        crop_params._x_slice = x_slice
        crop_params._y_slice = y_slice
        return ImageStack.from_tileset(self._images[item], crop_parameters=crop_params)
Example #2
0
def imagestack_factory(
        fetched_tile_cls: Type[LocationAwareFetchedTile],
        round_labels: Sequence[int],
        ch_labels: Sequence[int],
        zplane_labels: Sequence[int],
        tile_height: int,
        tile_width: int,
        xrange: Tuple[Number, Number],
        yrange: Tuple[Number, Number],
        zrange: Tuple[Number, Number],
        crop_parameters: Optional[CropParameters] = None) -> ImageStack:
    """Given a type that implements the :py:class:`LocationAwareFetchedTile` contract, produce an
    imagestack with those tiles, and apply coordinates such that the 5D tensor has coordinates
    that range from `xrange[0]:xrange[1]`, `yrange[0]:yrange[1]`, `zrange[0]:zrange[1]`.

    Parameters
    ----------
    fetched_tile_cls : Type[LocationAwareFetchedTile]
        The class of the FetchedTile.
    round_labels : Sequence[int]
        Labels for the rounds.
    ch_labels : Sequence[int]
        Labels for the channels.
    zplane_labels : Sequence[int]
        Labels for the zplanes.
    tile_height : int
        Height of each tile, in pixels.
    tile_width : int
        Width of each tile, in pixels.
    xrange : Tuple[Number, Number]
        The starting and ending x physical coordinates for the tile.
    yrange : Tuple[Number, Number]
        The starting and ending y physical coordinates for the tile.
    zrange : Tuple[Number, Number]
        The starting and ending z physical coordinates for the tile.
    crop_parameters : Optional[CropParameters]
        The crop parameters to apply during ImageStack construction.
    """
    original_tile_fetcher = tile_fetcher_factory(
        fetched_tile_cls,
        True,
        round_labels,
        ch_labels,
        zplane_labels,
        tile_height,
        tile_width,
    )
    modified_tile_fetcher = _apply_coords_range_fetcher(
        original_tile_fetcher, zplane_labels, xrange, yrange, zrange)

    collection = build_image(
        range(1),
        round_labels,
        ch_labels,
        zplane_labels,
        modified_tile_fetcher,
    )
    tileset = list(collection.all_tilesets())[0][1]

    return ImageStack.from_tileset(tileset, crop_parameters)
Example #3
0
def unique_tiles_imagestack(
        round_labels: Sequence[int],
        ch_labels: Sequence[int],
        z_labels: Sequence[int],
        tile_height: int,
        tile_width: int,
        crop_parameters: Optional[CropParameters] = None) -> ImageStack:
    """Build an imagestack with unique values per tile.
    """
    collection = build_image(
        range(1),
        round_labels,
        ch_labels,
        z_labels,
        tile_fetcher_factory(
            UniqueTiles,
            True,
            len(round_labels),
            len(ch_labels),
            len(z_labels),
            tile_height,
            tile_width,
        ),
    )
    tileset = list(collection.all_tilesets())[0][1]

    return ImageStack.from_tileset(tileset, crop_parameters)
Example #4
0
def synthetic_stack(
    num_round: int = 4,
    num_ch: int = 4,
    num_z: int = 12,
    tile_height: int = 50,
    tile_width: int = 40,
    tile_fetcher: TileFetcher = None,
) -> ImageStack:
    """generate a synthetic ImageStack

    Returns
    -------
    ImageStack :
        imagestack containing a tensor whose default shape is (2, 3, 4, 30, 20)
        and whose default values are all 1.

    """
    if tile_fetcher is None:
        tile_fetcher = tile_fetcher_factory(
            OnesTile,
            False,
            {
                Axes.Y: tile_height,
                Axes.X: tile_width
            },
        )

    collection = build_image(
        range(1),
        range(num_round),
        range(num_ch),
        range(num_z),
        tile_fetcher,
    )
    tileset = list(collection.all_tilesets())[0][1]

    return ImageStack.from_tileset(tileset)
Example #5
0
 def __next__(self) -> ImageStack:
     aligned_group = next(self.aligned_groups)
     stack = ImageStack.from_tileset(self.tileset, aligned_group)
     return stack
Example #6
0
def imagestack_factory(
        fetched_tile_cls: Type[LocationAwareFetchedTile],
        round_labels: Sequence[int],
        ch_labels: Sequence[int],
        zplane_labels: Sequence[int],
        tile_height: int,
        tile_width: int,
        xrange: Tuple[Number, Number],
        yrange: Tuple[Number, Number],
        zrange: Tuple[Number, Number],
        crop_parameters: Optional[CropParameters] = None) -> ImageStack:
    """Given a type that implements the :py:class:`LocationAwareFetchedTile` contract, produce an
    imagestack with those tiles, and apply coordinates such that the 5D tensor has coordinates
    that range from `xrange[0]:xrange[1]`, `yrange[0]:yrange[1]`, `zrange[0]:zrange[1]`.

    Parameters
    ----------
    fetched_tile_cls : Type[LocationAwareFetchedTile]
        The class of the FetchedTile.
    round_labels : Sequence[int]
        Labels for the rounds.
    ch_labels : Sequence[int]
        Labels for the channels.
    zplane_labels : Sequence[int]
        Labels for the zplanes.
    tile_height : int
        Height of each tile, in pixels.
    tile_width : int
        Width of each tile, in pixels.
    xrange : Tuple[Number, Number]
        The starting and ending x physical coordinates for the tile.
    yrange : Tuple[Number, Number]
        The starting and ending y physical coordinates for the tile.
    zrange : Tuple[Number, Number]
        The starting and ending z physical coordinates for the tile.
    crop_parameters : Optional[CropParameters]
        The crop parameters to apply during ImageStack construction.
    """
    tile_identifiers: Collection[TileIdentifier] = [
        TileIdentifier(0, round_label, ch_label, zplane_label)
        for round_label in round_labels for ch_label in ch_labels
        for zplane_label in zplane_labels
    ]

    def make_tile_coordinate_callback(
        all_zplane_labels: Sequence[int]
    ) -> Callable[[TileIdentifier], Mapping[Coordinates, CoordinateValue]]:
        def tile_coordinate_callback(
            tile_identifier: TileIdentifier
        ) -> Mapping[Coordinates, CoordinateValue]:
            zplane_offset = all_zplane_labels.index(
                tile_identifier.zplane_label)
            return {
                Coordinates.X: xrange,
                Coordinates.Y: yrange,
                Coordinates.Z: zrange[zplane_offset],
            }

        return tile_coordinate_callback

    collection = collection_factory(
        fetched_tile_cls,
        tile_identifiers,
        make_tile_coordinate_callback(
            sorted(
                set(tile_identifier.zplane_label
                    for tile_identifier in tile_identifiers))),
        tile_height,
        tile_width,
    )
    tileset = list(collection.all_tilesets())[0][1]

    return ImageStack.from_tileset(tileset, crop_parameters)