Skip to content

LVAE Tiled Patching

Source

Functions to reimplement the tiling in the Disentangle repository.

compute_padding(data_shape, tile_size, overlaps)

Calculate padding to ensure stitched data comes from the center of a tile.

Padding is added to an array with shape data_shape so that when tiles are stitched together, the data used always comes from the center of a tile, even for tiles at the boundaries of the array.

Parameters:

  • data_shape (1D numpy.array of int) –

    The shape of the data to be tiled and stitched together, (S, C, (Z), Y, X).

  • tile_size (1D numpy.array of int) –

    The tile size in each dimension, ((Z), Y, X).

  • overlaps (1D numpy.array of int) –

    The tile overlap in each dimension, ((Z), Y, X).

Returns:

  • tuple of (int, int)

    A tuple specifying the padding to add in each dimension, each element is a two element tuple specifying the padding to add before and after the data. This can be used as the pad_width argument to numpy.pad.

compute_tile_grid_shape(data_shape, tile_size, overlaps)

Calculate the number of tiles in each dimension.

This can be thought of as a grid of tiles.

Parameters:

  • data_shape (1D numpy.array of int) –

    The shape of the data to be tiled and stitched together, (S, C, (Z), Y, X).

  • tile_size (1D numpy.array of int) –

    The tile size in each dimension, ((Z), Y, X).

  • overlaps (1D numpy.array of int) –

    The tile overlap in each dimension, ((Z), Y, X).

Returns:

  • tuple of int

    The number of tiles in each direction, ((Z, Y, X)).

compute_tile_info(tile_grid_indices, data_shape, tile_size, overlaps, sample_id=0)

Compute the tile information for a tile with the coordinates tile_grid_indices.

Parameters:

  • tile_grid_indices (1D np.array of int) –

    The coordinates of the tile within the tile grid, ((Z), Y, X), i.e. for 2D tiling the coordinates for the second tile in the first row of tiles would be (0, 1).

  • data_shape (1D np.array of int) –

    The shape of the data, should be (C, (Z), Y, X) where Z is optional.

  • tile_size (1D np.array of int) –

    Tile sizes in each dimension, of length 2 or 3.

  • overlaps (1D np.array of int) –

    Overlap values in each dimension, of length 2 or 3.

  • sample_id (int, default: 0 ) –

    An ID to identify which sample a tile belongs to.

Returns:

  • TileInformation

    Information that describes how to crop and stitch a tile to create a full image.

compute_tile_info_legacy(grid_index_manager, index)

Compute the tile information for a tile at a given dataset index.

Parameters:

  • grid_index_manager (GridIndexManager) –

    The grid index manager that keeps track of tile locations.

  • index (int) –

    The dataset index.

Returns:

  • TileInformation

    Information that describes how to crop and stitch a tile to create a full image.

Raises:

  • ValueError

    If grid_index_manager.data_shape does not have 4 or 5 dimensions.

extract_tiles(arr, tile_size, overlaps, padding_kwargs=None)

Generate tiles from the input array with specified overlap.

The tiles cover the whole array; which will be additionally padded, to ensure that the section of the tile that contributes to the final image comes from the center of the tile.

The method returns a generator that yields tuples of array and tile information, the latter includes whether the tile is the last one, the coordinates of the overlap crop, and the coordinates of the stitched tile.

Input array should have shape SC(Z)YX, while the returned tiles have shape C(Z)YX, where C can be a singleton.

Parameters:

  • arr (ndarray) –

    Array of shape (S, C, (Z), Y, X).

  • tile_size (1D numpy.ndarray of tuple) –

    Tile sizes in each dimension, of length 2 or 3.

  • overlaps (1D numpy.ndarray of tuple) –

    Overlap values in each dimension, of length 2 or 3.

  • padding_kwargs (dict, default: None ) –

    The arguments of np.pad after the first two arguments, array and pad_width. If not specified the default will be {"mode": "reflect"}. See numpy.pad docs: https://numpy.org/doc/stable/reference/generated/numpy.pad.html.

Yields:

  • Generator[Tuple[ndarray, TileInformation], None, None]

    Tile generator, yields the tile and additional information.

n_tiles_1d(axis_size, tile_size, overlap)

Calculate the number of tiles in a specific dimension.

Parameters:

  • axis_size (int) –

    The length of the data for in a specific dimension.

  • tile_size (int) –

    The length of the tiles in a specific dimension.

  • overlap (int) –

    The tile overlap in a specific dimension.

Returns:

  • int

    The number of tiles that fit in one dimension given the arguments.

total_n_tiles(data_shape, tile_size, overlaps)

Calculate The total number of tiles over all dimensions.

Parameters:

  • data_shape (1D numpy.array of int) –

    The shape of the data to be tiled and stitched together, (S, C, (Z), Y, X).

  • tile_size (1D numpy.array of int) –

    The tile size in each dimension, ((Z), Y, X).

  • overlaps (1D numpy.array of int) –

    The tile overlap in each dimension, ((Z), Y, X).

Returns:

  • int

    The total number of tiles over all dimensions.