SpatialImage#

class voxel.SpatialImage(tensor, geometry=None, labels=None)#

Base class of multi-channel image data sampled on a regular in-plane lattice with a world-space geometry, i.e. a Volume or SliceStack. It implements the tensor operations that do not depend on the geometry.

property tensor: Tensor#

The image feature tensor, always 4D with a leading channel dimension.

property labels: LabelLookup | None#

The label lookup table annotating the integer values of this image, or None if the image has no associated labels.

property shape: Size#

The 4D shape of the image, including channel dimension.

property baseshape: Size#

The 3D shape of the image, excluding channel dimension.

property num_channels: int#

The number of feature channels (the first image dimension size).

property device: device#

Device of the image tensor.

property dtype: dtype#

Datatype of the image tensor.

new(tensor, geometry=None, keep_labels=True) → SpatialImage#

Construct a new image instance with the provided features tensor, while preserving any unchanged properties of the original image.

Parameters:
  • tensor (Tensor) – The new image tensor replacement.

  • geometry (VolumeGeometry or SliceStackGeometry, optional) – The new geometry. If None, the current geometry will be propagated.

  • keep_labels (bool, optional) – Whether to propagate the current label lookup table to the new image. Should be False for operations that no longer produce an integer label map.

copy() → SpatialImage#

Copy the image instance. Only the data tensor is copied, not the underlying geometry.

apply(func) → SpatialImage#

Apply a function to the image tensor and return a new instance.

Parameters:

func (callable) – The function to apply.

Returns:

SpatialImage – A new image instance.

detach() → SpatialImage#

Detach the image tensor from the current computational graph.

Returns:

SpatialImage – A new image instance with the detached tensor.

to(device) → SpatialImage#

Move the image tensor to a device.

Parameters:

device (device) – The target device.

Returns:

SpatialImage – A new image instance with the tensor on the target device.

cuda() → SpatialImage#

Move the image tensor to the GPU.

Returns:

SpatialImage – A new image instance with the tensor on the GPU.

cpu() → SpatialImage#

Move the image tensor to the CPU.

Returns:

SpatialImage – A new image instance with the tensor on the CPU.

type(dtype) → SpatialImage#

Convert the image tensor to a specified data type.

Parameters:

dtype (torch.dtype) – The target data type.

Returns:

SpatialImage – A new image instance.

float() → SpatialImage#

Convert the image tensor to float data type.

Returns:

SpatialImage – A new float image instance.

half() → SpatialImage#

Convert the image tensor to half-precision float data type.

Returns:

SpatialImage – A new half-precision float image instance.

int() → SpatialImage#

Convert the image tensor to integer data type.

Returns:

SpatialImage – A new integer image instance.

bool() → SpatialImage#

Convert the image tensor to boolean data type.

Returns:

SpatialImage – A new boolean image instance.

max(dim=None) → SpatialImage | Tensor#

Get the maximum value in the image tensor.

Parameters:

dim (int, optional) – The dimension or dimensions to reduce. If None, all dimensions are reduced. If the dimension is 0 (channel axis), a single-channel image is returned.

Returns:

Tensor or SpatialImage – The maximum value(s) or image.

min(dim=None) → SpatialImage | Tensor#

Get the minimum value in the image features.

Parameters:

dim (int, optional) – The dimension or dimensions to reduce. If None, all dimensions are reduced. If the dimension is 0 (channel axis), a single-channel image is returned.

Returns:

Tensor or SpatialImage – The mininum value(s) or image.

sum(dim=None) → SpatialImage | Tensor#

Compute the sum of all voxels.

Parameters:

dim (int, optional) – The dimension or dimensions to reduce. If None, all dimensions are reduced. If the dimension is 0 (channel axis), a single-channel image is returned.

Returns:

Tensor or SpatialImage – The summed value(s) or image.

mean(dim=None) → SpatialImage | Tensor#

Compute the mean of all voxels.

Parameters:

dim (int, optional) – The dimension or dimensions to reduce. If None, all dimensions are reduced. If the dimension is 0 (channel axis), a single-channel image is returned.

Returns:

Tensor or SpatialImage – The mean value(s) or image.

floor() → SpatialImage#

Apply the floor operation to the image features.

Returns:

SpatialImage – A new floored image instance.

ceil() → SpatialImage#

Apply the ceil operation to the image features.

Returns:

SpatialImage – A new ceiled image instance.

abs() → SpatialImage#

Compute absolute values of the image features.

Returns:

SpatialImage – A new image instance.

exp() → SpatialImage#

Compute exponential of the elements in the image features.

Returns:

SpatialImage – A new exponentiated image instance.

log() → SpatialImage#

Compute the natural logarithm of the image features.

Returns:

SpatialImage – A new log-transformed image instance.

sqrt() → SpatialImage#

Compute the square root of the image features.

Returns:

SpatialImage – A new square-rooted image instance.

square() → SpatialImage#

Compute the square of the image features.

Returns:

SpatialImage – A new squared image instance.

pow(exponent) → SpatialImage#

Compute the power of the image features.

Parameters:

exponent (float) – The exponent value.

Returns:

SpatialImage – A new powered image instance.

isnan() → SpatialImage#

Compute a mask of NaN values in the image.

Returns:

SpatialImage – A new image mask instance.

clamp(min=None, max=None, inplace=False) → SpatialImage#

Clamp the values in the image tensor.

Parameters:
  • min (float, optional) – Minimum value to clamp to.

  • max (float, optional) – Maximum value to clamp to.

  • inplace (bool) – Whether to perform the operation in-place.

Returns:

SpatialImage – A new (if not in-place) clamped image instance.

maximum(other) → SpatialImage#

Computes the element-wise maximum between two images.

Parameters:

other (SpatialImage) – The input image to compare against.

Returns:

SpatialImage – A maximized image instance.

minimum(other) → SpatialImage#

Computes the element-wise minimum between two images.

Parameters:

other (SpatialImage) – The input image to compare against.

Returns:

SpatialImage – A minimized image instance.

all(dim=None) → SpatialImage | Tensor#

Check if all elements in the image are True.

Parameters:

dim (int, optional) – The dimension or dimensions to reduce. If None, all dimensions are reduced. If the dimension is 0 (channel axis), a single-channel image is returned.

Returns:

Tensor or SpatialImage – The all-True value(s) or image.

any(dim=None) → SpatialImage | Tensor#

Check if any elements in the image are True.

Parameters:

dim (int, optional) – The dimension or dimensions to reduce. If None, all dimensions are reduced. If the dimension is 0 (channel axis), a single-channel image is returned.

Returns:

Tensor or SpatialImage – The any-True value(s) or image.

zeros_like(channels=None, dtype=None) → SpatialImage#

Create an image of zeros with the same geometry and device as the current instance.

Parameters:
  • channels (int, optional) – Number of channels in the new image. If None, will default to the existing number.

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

Returns:

SpatialImage – A new image instance filled with zeros.

ones_like(channels=None, dtype=None) → SpatialImage#

Create an image of ones with the same geometry and device as the current instance.

Parameters:
  • channels (int, optional) – Number of channels in the new image. If None, will default to the existing number.

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

Returns:

SpatialImage – A new image instance filled with ones.

full_like(fill, channels=None, dtype=None) → SpatialImage#

Create an image filled with a specific value and with the same geometry and device as the current instance.

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

  • channels (int, optional) – Number of channels in the new image. If None, will default to the existing number.

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

Returns:

SpatialImage – A new filled image instance.

rand_like(channels=None, dtype=None) → SpatialImage#

Create an image of random values with the same geometry and device as the current instance. Values are sampled from a uniform distribution on the interval [0, 1).

Parameters:
  • channels (int, optional) – Number of channels in the new image. If None, will default to the existing number.

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

Returns:

SpatialImage – A new random image instance.

randn_like(channels=None, dtype=None) → SpatialImage#

Create an image of random values with the same geometry and device as the current instance. Values are sampled from a normal distribution with mean 0 and variance 1

Parameters:
  • channels (int, optional) – Number of channels in the new image. If None, will default to the existing number.

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

Returns:

SpatialImage – A new random image instance.

isin(elements) → SpatialImage#

Tests if each element of elements is in the image.

Parameters:

elements (Tensor or Scalar) – Values against which to test each voxel.

Returns:

SpatialImage –

A boolean image that is True when a voxel value is

in elements and False otherwise.

unique(**kwargs) → Tensor#

Compute the unique elements of image.

Parameters:

**kwargs (Any) – Additional arguments passed to the underlying call to torch.unique().

Returns:

Tensor – The output list of unique scalar elements.

quantile(q) → Tensor#

Compute the q-th quantile of the voxel data.

Parameters:

q (float) – A scalar quantile in the range [0, 1].

Returns:

Tensor – The quantile scalar value.

softmax(dim=0) → SpatialImage | Tensor#

Apply a softmax along a dimension of the image tensor.

Parameters:

dim (int, optional) – The dimension along which to apply the softmax. Defaults to the channel axis (0), in which case an image is returned.

Returns:

Tensor or SpatialImage – Softmaxed probabilities.

argmax(dim=None) → SpatialImage | Tensor#

Get the maximum index in the image tensor.

Parameters:

dim (int, optional) – The dimension or dimensions to reduce. If None, all dimensions are reduced. If the dimension is 0 (channel axis), a single-channel image is returned.

Returns:

Tensor or SpatialImage – The maximum indices or image.

recode(mapping, reverse=False, background=False) → SpatialImage#

Remap the integer label values of the image via a lookup.

In the forward direction, the image is treated as an index map and each voxel value i is replaced with mapping[i]. In the reverse direction, each voxel value is replaced with its position in mapping (the inverse operation).

A LabelLookup is treated as its ordered integer indices. When the forward direction is used with a LabelLookup, the recoded voxel values are the real label values it describes, so the lookup is attached to the returned image as its labels.

Parameters:
  • mapping (LabelLookup or Tensor or list) – The ordered values to map index positions to.

  • reverse (bool, optional) – Map values back to their index positions.

  • background (bool, optional) – Prepend the background label 0 to mapping (unless already present) so it occupies the first index.

Returns:

SpatialImage – A new label-map image with remapped values.

onehot(labels=-1, background=False) → SpatialImage#

One hot encode a label image, with one channel per class.

Parameters:
  • labels (int or Tensor or LabelLookup, optional) – The classes to encode. If an integer, it is the total number of classes (with -1 inferring one greater than the largest voxel value). If a tensor or lookup of label values, the image is first recoded so those values map to the one-hot channels, in order.

  • background (bool, optional) – When labels is a tensor or lookup, reserve the first channel for the background label 0 (unless already present).

Returns:

SpatialImage – The one-hot encoded image.

collapse(labels=None, background=False) → SpatialImage#

Collapse a multi-channel (one-hot or probabilistic) image into a single channel label map. This is the inverse of onehot.

The channel axis is reduced with an argmax, and the resulting per-voxel channel index is optionally recoded into label values.

Parameters:
  • labels (Tensor or LabelLookup, optional) – The label values that the channels correspond to. If None, the channel indices are returned directly. If a tensor or lookup, the channel index is recoded into the corresponding label value.

  • background (bool, optional) – When labels is provided, treat the first channel as the background label 0 (unless already present).

Returns:

SpatialImage – A single-channel label map image.