Tiling¶
plakakia.tiling ¶
Core, dependency-light tiling primitives.
Everything in this module is pure NumPy: no file I/O and no global state, so the
functions can be used online (e.g. inside a data loader) as well as by the
offline pipeline in :mod:plakakia.pipeline.
:func:tile_array, :func:tile_image and :func:iter_tiles also accept PyTorch tensors
and CuPy arrays, and then return tiles from the same library on the same device (a GPU
tensor stays on the GPU). Tile coordinates are always NumPy. See :mod:plakakia.backend.
Conventions¶
- Arrays can have any number of dimensions and any dtype:
(H, W)grayscale,(H, W, C)colour,(H, W, B)or(B, H, W)hyperspectral cubes, volumes... - :func:
tile_arraytiles along any subset of axes, with a tile size and step per axis. :func:tile_imageis the 2-D special case that returns box-style coordinates. - Boxes and tile coordinates are
(x1, y1, x2, y2)in pixels, withx2/y2exclusive, matching NumPy slicing:image[y1:y2, x1:x2]. - Tiles are ordered row-major (the first tiled axis varies slowest).
SizeLike
module-attribute
¶
SizeLike = int | Sequence[int]
A single size applied to every axis, or one size per axis.
normalize_axes ¶
normalize_axes(
axes: int | Sequence[int], ndim: int
) -> tuple[int, ...]
Resolve negative axes and check that axes are valid and unique.
normalize_axes((-2, -1), 3) (1, 2)
tile_starts ¶
tile_starts(
length: int,
tile_size: int,
step_size: int,
*,
cover_edges: bool = False,
) -> npt.NDArray[np.intp]
Return the start offsets of tiles along one axis of size length.
With cover_edges=True an extra tile aligned to the end of the axis is added
when the regular grid would otherwise leave a remainder uncovered.
tile_starts(10, 4, 3).tolist() [0, 3, 6] tile_starts(11, 4, 3, cover_edges=True).tolist() [0, 3, 6, 7]
tile_grid ¶
tile_grid(
shape: Sequence[int],
tile_shape: SizeLike,
step: SizeLike | None = None,
*,
cover_edges: bool = False,
) -> npt.NDArray[np.intp]
Start offsets of every tile of an N-D grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
Sequence[int]
|
Sizes of the tiled axes. |
required |
tile_shape
|
SizeLike
|
Tile size per axis (or one size for all). |
required |
step
|
SizeLike | None
|
Stride per axis (or one for all). Defaults to |
None
|
cover_edges
|
bool
|
See :func: |
False
|
Returns:
| Type | Description |
|---|---|
NDArray[intp]
|
|
tile_grid((4, 6), (2, 3)).tolist() [[0, 0], [0, 3], [2, 0], [2, 3]]
window_slices ¶
window_slices(
start: Sequence[int],
tile_shape: Sequence[int],
axes: Sequence[int],
ndim: int,
) -> tuple[slice, ...]
Slices selecting the tile at start (one value per tiled axis) from an array.
window_slices((10, 20), (5, 5), (1, 2), 3) (slice(None, None, None), slice(10, 15, None), slice(20, 25, None))
tile_array ¶
tile_array(
array: ndarray,
tile_shape: SizeLike,
step: SizeLike | None = ...,
*,
axes: int | Sequence[int] | None = ...,
cover_edges: bool = ...,
) -> tuple[np.ndarray, npt.NDArray[np.intp]]
tile_array(
array: object,
tile_shape: SizeLike,
step: SizeLike | None = ...,
*,
axes: int | Sequence[int] | None = ...,
cover_edges: bool = ...,
) -> tuple[Any, npt.NDArray[np.intp]]
tile_array(
array: object,
tile_shape: SizeLike,
step: SizeLike | None = None,
*,
axes: int | Sequence[int] | None = None,
cover_edges: bool = False,
) -> tuple[Any, npt.NDArray[np.intp]]
Tile an N-D array along any axes.
Axes that are not tiled are kept whole, in their original position, so that
tiles[i] equals array[window_slices(starts[i], tile_shape, axes, array.ndim)].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
object
|
Any array, e.g. a |
required |
tile_shape
|
SizeLike
|
Tile size per tiled axis (or one size for all of them). |
required |
step
|
SizeLike | None
|
Stride per tiled axis. Defaults to |
None
|
axes
|
int | Sequence[int] | None
|
Axes to tile, in any order. Defaults to the first |
None
|
cover_edges
|
bool
|
Add border-aligned tiles so the whole extent of each axis is covered. |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
|
NDArray[intp]
|
input's device for PyTorch/CuPy input) and |
tuple[Any, NDArray[intp]]
|
NumPy array of tile start offsets, one column per entry of |
Example
Tile a channels-first cube spatially and spectrally:
cube = np.zeros((200, 145, 145), dtype=np.float32) # (bands, H, W) tiles, starts = tile_array(cube, (64, 64, 50), axes=(1, 2, 0)) tiles.shape # (T, bands, H, W) with tiled sizes (16, 50, 64, 64) starts[:2].tolist() # (y, x, band) per tile [[0, 0, 0], [0, 0, 50]]
iter_tiles ¶
iter_tiles(
array: ndarray,
tile_shape: SizeLike,
step: SizeLike | None = ...,
*,
axes: int | Sequence[int] | None = ...,
cover_edges: bool = ...,
) -> Iterator[tuple[tuple[int, ...], np.ndarray]]
iter_tiles(
array: Any,
tile_shape: SizeLike,
step: SizeLike | None = ...,
*,
axes: int | Sequence[int] | None = ...,
cover_edges: bool = ...,
) -> Iterator[tuple[tuple[int, ...], Any]]
iter_tiles(
array: Any,
tile_shape: SizeLike,
step: SizeLike | None = None,
*,
axes: int | Sequence[int] | None = None,
cover_edges: bool = False,
) -> Iterator[tuple[tuple[int, ...], Any]]
Lazily yield (start, view) pairs without copying any data.
Same arguments as :func:tile_array. Prefer this for arrays that do not fit in
memory (e.g. memory-mapped hyperspectral cubes): only the tile you touch is read.
PyTorch tensors and CuPy arrays yield views on their own device.
arr = np.arange(12).reshape(3, 4) for start, view in iter_tiles(arr, (3, 2)): ... print(start, view.ravel()) (0, 0) [0 1 4 5 8 9] (0, 2) [ 2 3 6 7 10 11]
tile_coordinates ¶
tile_coordinates(
height: int,
width: int,
tile_size: SizeLike,
step_size: SizeLike,
*,
cover_edges: bool = False,
) -> npt.NDArray[np.intp]
Return (T, 4) tile coordinates (x1, y1, x2, y2) for an image of the given size.
tile_size and step_size are an int (square) or (height, width).
tile_image ¶
tile_image(
image: ndarray,
tile_size: SizeLike = ...,
step_size: SizeLike = ...,
*,
axes: Sequence[int] = ...,
cover_edges: bool = ...,
) -> tuple[np.ndarray, npt.NDArray[np.intp]]
tile_image(
image: object,
tile_size: SizeLike = ...,
step_size: SizeLike = ...,
*,
axes: Sequence[int] = ...,
cover_edges: bool = ...,
) -> tuple[Any, npt.NDArray[np.intp]]
tile_image(
image: object,
tile_size: SizeLike = 250,
step_size: SizeLike = 50,
*,
axes: Sequence[int] = (0, 1),
cover_edges: bool = False,
) -> tuple[Any, npt.NDArray[np.intp]]
Split an image into (possibly overlapping) tiles along its two spatial axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
object
|
Array with at least 2 dimensions: |
required |
tile_size
|
SizeLike
|
Tile side in pixels, or |
250
|
step_size
|
SizeLike
|
Stride in pixels, or |
50
|
axes
|
Sequence[int]
|
The |
(0, 1)
|
cover_edges
|
bool
|
Add tiles aligned to the right/bottom borders so that every pixel
is covered, even when |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
|
NDArray[intp]
|
spatial axes replaced by the tile size, prefixed by the tile index (a contiguous |
tuple[Any, NDArray[intp]]
|
copy, safe to modify), and |
tuple[Any, NDArray[intp]]
|
Tiles are empty ( |
Example
img = np.zeros((512, 512, 3), dtype=np.uint8) tiles, coords = tile_image(img, tile_size=128, step_size=64) tiles.shape, coords.shape ((49, 128, 128, 3), (49, 4)) cube = np.zeros((200, 145, 145), dtype=np.float32) # (bands, H, W) tile_image(cube, 64, 64, axes=(1, 2))[0].shape (4, 200, 64, 64)
box_visibility ¶
box_visibility(
boxes: ArrayLike, tiles: ArrayLike
) -> npt.NDArray[np.float64]
Fraction of each box's area that falls inside each tile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boxes
|
ArrayLike
|
|
required |
tiles
|
ArrayLike
|
|
required |
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
|
NDArray[float64]
|
|
assign_boxes_to_tiles ¶
assign_boxes_to_tiles(
boxes: ArrayLike,
tiles: ArrayLike,
*,
min_visibility: float = 1.0,
) -> npt.NDArray[np.bool_]
Decide which boxes belong to which tile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boxes
|
ArrayLike
|
|
required |
tiles
|
ArrayLike
|
|
required |
min_visibility
|
float
|
Minimum fraction of a box's area that must lie inside a tile for
the box to be assigned to it. |
1.0
|
Returns:
| Type | Description |
|---|---|
NDArray[bool_]
|
|
clip_boxes_to_tile ¶
clip_boxes_to_tile(
boxes: ArrayLike, tile: ArrayLike
) -> np.ndarray
Translate boxes into a tile's local frame and clip them to its bounds.
select_unique_tiles ¶
select_unique_tiles(
assignment: ArrayLike,
) -> npt.NDArray[np.bool_]
Greedily drop tiles whose boxes are all already covered by earlier tiles.
Tiles are visited in order; a tile is kept if it contains at least one box that no previously kept tile contains. Tiles without boxes are never selected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assignment
|
ArrayLike
|
|
required |
Returns:
| Type | Description |
|---|---|
NDArray[bool_]
|
|