Skip to content

Reshape Array

Source

Utilities for reshaping arrays between original and transformed space.

AxesTransform dataclass

Transformation between original and transformed space axes.

All transformation decisions are derived from the original axes string, shape.

Attributes:

Name Type Description
original_axes str

User defined attribute. Axes string of the input data (e.g. "YXC", "STCZYX").

original_shape tuple[int, ...]

User defined attribute. Shape corresponding to original_axes.

sample_dims list[str]

Computed property. Original dimensions merged into S. Spatial (Y, X and Z), as well as channels, are never considered sample dimensions.

c_added bool

Computed property. Whether C is added as a singleton.

has_z bool

Computed property. Whether original data contains a Z axis.

dim_sizes dict[str, int]

Computed property. Map from axis name to original size.

transformed_axes str

Computed property. Transformed axes string: "SC(Z)YX".

transformed_shape tuple[int, ...]

Computed property. Expected shape after forward transformation.

order_permutation list[int]

Computed property. Permutation to reorder original axes to STCZYX reference order in SC(Z)YX space.

c_added property

C is added as a singleton dimension.

Returns:

Type Description
bool

True if C is not in original axes, False otherwise.

dim_sizes property

Map from axis name to original size.

Returns:

Type Description
dict[str, int]

Dictionary mapping axis name to its size in the original shape.

has_z property

Original data contains a Z axis.

Returns:

Type Description
bool

True if Z is in original axes, False otherwise.

order_permutation property

Permutation to reorder original axes to STCZYX reference order.

Returns:

Type Description
list[int]

List of indices representing the permutation to reorder original axes to STCZYX reference order. Only includes axes present in the original axes.

sample_dims property

Original dimensions merged into S.

Spatial (Y, X and Z), as well as channels, are never considered sample dimensions.

Returns:

Type Description
list[str]

List of original axes that are considered sample dimensions and merged into S. Will be in the order they appear in the reference STCZYX.

transformed_axes property

Transformed axes string, SC(Z)YX or SCYX.

Returns:

Type Description
str

Transformed axes string. Will be SCZYX if original data has Z axis, otherwise SCYX.

transformed_shape property

Expected shape in transformed space.

Returns:

Type Description
tuple[int, ...]

Expected shape after forward transformation, in the order of transformed_axes.

get_original_stitch_slices(original_axes, original_shape, sample_idx, stitch_coords, crop_size)

Get slices to stitch tile back into original array.

sample_idx andstitch_coords` are expressed with respect to the transformed space (SCZYX or SCYX). The returned slices will index into the original array for stitching the tile back in place.

Parameters:

Name Type Description Default
original_axes str

Original axes string of the full data.

required
original_shape Sequence[int]

Original shape of the full data.

required
sample_idx int

Index of the sample in transformed space (S axis) to stitch back.

required
stitch_coords Sequence[int]

Starting coordinates of the tile in the original spatial axes (Y, X and Z if present).

required
crop_size Sequence[int]

Size of the tile in the original spatial axes (Y, X and Z if present).

required

Returns:

Type Description
tuple[slice | int, ...]

Slices to index into the original array for stitching the tile back in place.

reshape_array(array, original_axes)

Reshape array from arbitrary axes order to SC(Z)YX.

Parameters:

Name Type Description Default
array ndarray

Input array.

required
original_axes str

Original axes string describing current dimension order (e.g. YXC).

required

Returns:

Type Description
ndarray

Array reshaped to SC(Z)YX.

restore_array(array, original_axes, original_shape)

Restore array from SC(Z)YX space back to original axes and shape.

Parameters:

Name Type Description Default
array ndarray

Array in SC(Z)YX format.

required
original_axes str

Original axes string (e.g. YXC).

required
original_shape Sequence[int]

Original shape of the data.

required

Returns:

Type Description
ndarray

Array with original axes order and shape restored.

Raises:

Type Description
ValueError

If input array is not 4D (SCYX) or 5D (SCZYX), or if restoring shape is not supported for the given original axes (e.g. T as Z with CZI format).

restore_tile(tile, original_axes, original_shape)

Restore single tile from C(Z)YX space back to original axes and shape.

Parameters:

Name Type Description Default
tile ndarray

Tile in C(Z)YX format.

required
original_axes str

Original axes string of the full data.

required
original_shape Sequence[int]

Original shape of the full data.

required

Returns:

Type Description
ndarray

Tile with original spatial axes order restored.