SliceStackGeometry#

class voxel.SliceStackGeometry(baseshape, matrices=None, device=None)#

Geometry representing the linear relationship between the slice coordinates of a slice stack and a three-dimensional world space. Every slice carries its own matrix, so the slices need not form a uniform grid.

property tensor: Tensor#

Slice-to-world matrices of shape \((N, 4, 4)\).

property device: device#

Device of the matrix tensor.

property baseshape: Size#

The \((N, X, Y)\) shape of the slice stack.

property num_slices: int#

The number of slices.

property normals: Tensor#

Unit normals of the slice planes, of shape \((N, 3)\), pointing along the slice thickness.

property thickness: Tensor#

Thickness of each slice along its normal, of shape \((N,)\).

property centers: Tensor#

World coordinates of the slice centers, of shape \((N, 3)\).

detach() → SliceStackGeometry#

Detach the matrix tensor from the current computational graph.

to(device) → SliceStackGeometry#

Move the matrix tensor to a device.

cuda() → SliceStackGeometry#

Move the matrix tensor to the GPU.

cpu() → SliceStackGeometry#

Move the matrix tensor to the CPU.

transform(affine) → SliceStackGeometry#

Apply a world-space affine transform to every slice.

Parameters:

affine (AffineMatrix) – Transform to apply.

Returns:

SliceStackGeometry – The transformed geometry.

property step: Tensor#

Mean step between the origins of neighboring slices, of shape \((3,)\). For a single slice, this is its thickness vector.

is_uniform(tol=1e-4) → bool#

Determine if the slices form a regular grid in order, sharing their in-plane axes with evenly spaced origins. Such a slice stack is equivalent to a volume with step as its slice axis, whatever the slice thickness.

Parameters:

tol (float, optional) – Absolute tolerance of the matrix comparison.

Returns:

bool – True if the slices form a regular grid.

blocks(angle=1.0, gap=2.0, coverage=True) → list[Tensor]#

Group the slices into uniform blocks that can each be treated as a volume. A block holds slices on parallel planes with no large gap between neighbors. Slices are grouped regardless of their order in the slice stack.

Parameters:
  • angle (float, optional) – Maximum angle in degrees between the normal of a slice and the normal of the first slice of its block. If None, slices of any orientation are grouped.

  • gap (float, optional) – A block is cut between neighboring slices whose distance exceeds 1 + gap times a reference length. If None, blocks are never cut.

  • coverage (bool, optional) – If True, the reference length is the slice thickness, so gap measures the space left uncovered between slices in units of thickness. Otherwise it is the median slice distance.

Returns:

list of Tensor –

Slice indices of each block, sorted by position along

the block normal.

as_volume(spacing='min', preserve_tilt=False, angle=1.0, gap=2.0, coverage=True) → VolumeGeometry#

Compute a volume geometry that covers the slice stack. The grid is aligned with the largest block of slices (see blocks) and fit to the pixel centers of every slice.

Parameters:
  • spacing (str or float, optional) – Slice spacing of the grid, either a value or the ‘min’, ‘median’, or ‘max’ slice distance of the block.

  • preserve_tilt (bool, optional) – If True, the slice axis of the grid follows the slice origins of the block instead of its normal, so a tilted block is kept as a sheared grid.

  • angle (float, optional) – Maximum normal angle within a block.

  • gap (float, optional) – Maximum relative gap within a block.

  • coverage (bool, optional) – Whether the gap is relative to slice thickness.

Returns:

VolumeGeometry – Geometry of baseshape \((X, Y, N)\).

zeros_like(channels=1, dtype=None) → SliceStack#

Create a slice stack of zeros.

Parameters:
  • channels (int, optional) – Number of channels for the new slice stack.

  • dtype (torch.dtype, optional) – Target data type.

Returns:

SliceStack – A new slice stack instance filled with zeros.

ones_like(channels=1, dtype=None) → SliceStack#

Create a slice stack of ones.

Parameters:
  • channels (int, optional) – Number of channels for the new slice stack.

  • dtype (torch.dtype, optional) – Target data type.

Returns:

SliceStack – A new slice stack instance filled with ones.

full_like(fill, channels=1, dtype=None) → SliceStack#

Create a slice stack filled with a specific value.

Parameters:
  • fill (float) – The fill value.

  • channels (int, optional) – Number of channels for the new slice stack.

  • dtype (torch.dtype, optional) – Target data type.

Returns:

SliceStack – A new filled slice stack instance.

rand_like(channels=1, dtype=None) → SliceStack#

Create a slice stack of random values. Values are sampled from a uniform distribution on the interval [0, 1).

Parameters:
  • channels (int, optional) – Number of channels for the new slice stack.

  • dtype (torch.dtype, optional) – Target data type.

Returns:

SliceStack – A new random slice stack instance.

randn_like(channels=1, dtype=None) → SliceStack#

Create a slice stack of random values. Values are sampled from a normal distribution with mean 0 and variance 1.

Parameters:
  • channels (int, optional) – Number of channels for the new slice stack.

  • dtype (torch.dtype, optional) – Target data type.

Returns:

SliceStack – A new random slice stack instance.