Skip to content

Stratified Patching

Source

Stratified patching strategy with the option to exclude coordinates.

StratifiedPatching

Stratified patching strategy allowing patches on a grid to be excluded.

Patches will be sampled from sampling regions that are two times the patch size in each dimension. Some sampling regions may be smaller than this because they are on the edge of an image or because a nearby patch has been excluded.

If the same index is used twice to sample a patch with the method get_patch_spec there will be a high probability that it will come from the same sampling region, but not necessarily 100%. Smaller sampling regions may be binned together into a single index. The mean of all the expected values that each pixel will be selected in a patch per epoch is 1.

The number of patches is determined from the number of selectable patch coordinates.

Parameters:

  • data_shapes (sequence of (sequence of int)) –

    Shapes of the underlying data (axes SC(Z)YX).

  • patch_size (sequence of int) –

    Patch size per spatial dimension (length 2 or 3).

  • seed (int or None, default: None ) –

    Seed for reproducibility.

n_patches property

The number of patches that this patching strategy will return.

It also determines the maximum index that can be given to get_patch_spec.

Returns:

  • int

    Number of patches.

__init__(data_shapes, patch_size, seed=None)

A patching strategy for sampling stratified patches.

Parameters:

  • data_shapes (sequence of (sequence of int)) –

    The shapes of the underlying data. Each element is the dimension of the axes SC(Z)YX.

  • patch_size (sequence of int) –

    The size of the patch. The sequence will have length 2 or 3, for 2D and 3D data respectively.

  • seed (int, default: None ) –

    An optional seed to ensure the reproducibility of the random patches.

exclude_patches(data_idx, sample_idx, grid_coords)

Exclude patches from being sampled.

Excluded patches must lie on a grid which starts at (0, 0) and has a spacing of the given patch_size.

After calling this method the number of patches will be recalculated and the excluded patches will never be returned by get_patch_spec.

Parameters:

  • data_idx (int) –

    The index of the "image stack" that the patches will be excluded from.

  • sample_idx (int) –

    An index that corresponds to the sample in the "image stack" that the patches will be excluded from.

  • grid_coords (Sequence[tuple[int, ...]]) –

    A sequence of 2D or 3D tuples. Each tuple corresponds to a grid coordinate that will be excluded from sampling. The grid starts at (0, 0) and has a spacing of the given patch_size.

get_all_grid_coords()

Get all the grid coordinates for sampling regions in the patching strategy.

Returns:

  • dict[tuple[int, int], list[tuple, ...]]

    Dictionary with keys being (data_idx, sample_idx) and values corresponding to the grid coords.

get_included_grid_coords()

Get all grid coordinates included in the patching strategy.

If a grid coordinate is not included, a patch can never be selected from the region [grid_coord*patch_size, (grid_coord+1)*patch_size].

Returns:

  • dict[tuple[int, int], list[tuple, ...]]

    Dictionary with keys being (data_idx, sample_idx) and values corresponding to the grid coords.

get_patch_indices(data_idx)

Return the patch indices for a specific image_stack.

The image_stack corresponds to the given data_idx.

Parameters:

  • data_idx (int) –

    An index that corresponds to a given image_stack.

Returns:

  • sequence of int

    A sequence of patch indices, used to index the CAREamicsDataset to return a patch that comes from the image_stack corresponding to the given data_idx.

get_patch_spec(index)

Return the patch specs for a given index.

Parameters:

  • index (int) –

    A patch index.

Returns:

  • PatchSpecs

    A dictionary that specifies a single patch in a series of ImageStacks.

set_region_probs(data_idx, sample_idx, probs)

Set the probability that regions will be sampled from at each epoch.

Parameters:

  • data_idx (int) –

    The index of the "image stack" that the patches will be excluded from.

  • sample_idx (int) –

    An index that corresponds to the sample in the "image stack" that the patches will be excluded from.

  • probs (dict[tuple[int, ...], float]) –

    The probabilities for each region. The keys of the dictionary correspond to the grid coordinates of the regions. The values of the dictionary are the probabilities.