Transforms

Generic Interfaces

A collection of generic interfaces for MONAI transforms.

Transform

class Transform[source]

An abstract class of a Transform. A transform is callable that processes data.

It could be stateful and may modify data in place, the implementation should be aware of:

  1. thread safety when mutating its own states. When used from a multi-process context, transform’s instance variables are read-only.

  2. data content unused by this transform may still be used in the subsequent transforms in a composed transform.

  3. storing too much information in data may not scale.

See Also

abstract __call__(data, *args, **kwargs)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

MapTransform

class MapTransform(keys)[source]

A subclass of monai.transforms.compose.Transform with an assumption that the data input of self.__call__ is a MutableMapping such as dict.

The keys parameter will be used to get and set the actual data item to transform. That is, the callable of this transform should follow the pattern:

def __call__(self, data):
    for key in self.keys:
        if key in data:
            # update output data with some_transform_function(data[key]).
        else:
            # do nothing or some exceptions handling.
    return data
abstract __call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

Randomizable

class Randomizable[source]

An interface for handling local numpy random state. this is mainly for randomized data augmentation transforms.

abstract randomize(*args, **kwargs)[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

set_random_state(seed=None, state=None)[source]

Set the random state locally, to control the randomness, the derived classes should use self.R instead of np.random to introduce random factors.

Parameters
  • seed (int) – set the random state with an integer seed.

  • state (np.random.RandomState) – set the random state with a np.random.RandomState object.

Returns

a Randomizable instance.

Compose

class Compose(transforms=None)[source]

Compose provides the ability to chain a series of calls together in a sequence. Each transform in the sequence must take a single argument and return a single value, so that the transforms can be called in a chain.

Compose can be used in two ways:

  1. With a series of transforms that accept and return a single ndarray / tensor / tensor-like parameter.

  2. With a series of transforms that accept and return a dictionary that contains one or more parameters. Such transforms must have pass-through semantics; unused values in the dictionary must be copied to the return dictionary. It is required that the dictionary is copied between input and output of each transform.

If some transform generates a list batch of data in the transform chain, every item in the list is still a dictionary, and all the following transforms will apply to every item of the list, for example:

  1. transformA normalizes the intensity of ‘img’ field in the dict data.

  2. transformB crops out a list batch of images on ‘img’ and ‘seg’ field. And constructs a list of dict data, other fields are copied:

    {                          [{                   {
        'img': [1, 2],              'img': [1],         'img': [2],
        'seg': [1, 2],              'seg': [1],         'seg': [2],
        'extra': 123,    -->        'extra': 123,       'extra': 123,
        'shape': 'CHWD'             'shape': 'CHWD'     'shape': 'CHWD'
    }                           },                  }]
    
  3. transformC then randomly rotates or flips ‘img’ and ‘seg’ fields of every dictionary item in the list.

When using the pass-through dictionary operation, you can make use of monai.transforms.adaptors.adaptor to wrap transforms that don’t conform to the requirements. This approach allows you to use transforms from otherwise incompatible libraries with minimal additional work.

Note

In many cases, Compose is not the best way to create pre-processing pipelines. Pre-processing is often not a strictly sequential series of operations, and much of the complexity arises when a not-sequential set of functions must be called as if it were a sequence.

Example: images and labels Images typically require some kind of normalisation that labels do not. Both are then typically augmented through the use of random rotations, flips, and deformations. Compose can be used with a series of transforms that take a dictionary that contains ‘image’ and ‘label’ entries. This might require wrapping torchvision transforms before passing them to compose. Alternatively, one can create a class with a __call__ function that calls your pre-processing functions taking into account that not all of them are called on the labels.

__call__(input_)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

set_random_state(seed=None, state=None)[source]

Set the random state locally, to control the randomness, the derived classes should use self.R instead of np.random to introduce random factors.

Parameters
  • seed (int) – set the random state with an integer seed.

  • state (np.random.RandomState) – set the random state with a np.random.RandomState object.

Returns

a Randomizable instance.

Vanilla Transforms

A collection of “vanilla” transforms https://github.com/Project-MONAI/MONAI/wiki/MONAI_Design

Spacing

class Spacing(pixdim, diagonal=False, mode='constant', cval=0, dtype=None)[source]

Resample input image into the specified pixdim.

Parameters
  • pixdim (sequence of floats) – output voxel spacing.

  • diagonal (bool) –

    whether to resample the input to have a diagonal affine matrix. If True, the input data is resampled to the following affine:

    np.diag((pixdim_0, pixdim_1, ..., pixdim_n, 1))
    

    This effectively resets the volume to the world coordinate system (RAS+ in nibabel). The original orientation, rotation, shearing are not preserved.

    If False, this transform preserves the axes orientation, orthogonal rotation and translation components from the original affine. This option will not flip/swap axes of the original data.

  • mode (reflect|constant|nearest|mirror|wrap) – The mode parameter determines how the input array is extended beyond its boundaries.

  • cval (scalar) – Value to fill past edges of input if mode is “constant”. Default is 0.0.

  • dtype (None or np.dtype) – output array data type, defaults to None to use input data’s dtype.

__call__(data_array, affine=None, interp_order=3)[source]
Parameters
Returns

data_array (resampled into self.pixdim), original pixdim, current pixdim.

Orientation

class Orientation(axcodes=None, as_closest_canonical=False, labels=(('L', 'R'), ('P', 'A'), ('I', 'S')))[source]

Change the input image’s orientation into the specified based on axcodes.

Parameters
  • axcodes (N elements sequence) – for spatial ND input’s orientation. e.g. axcodes=’RAS’ represents 3D orientation: (Left, Right), (Posterior, Anterior), (Inferior, Superior). default orientation labels options are: ‘L’ and ‘R’ for the first dimension, ‘P’ and ‘A’ for the second, ‘I’ and ‘S’ for the third.

  • as_closest_canonical (boo) – if True, load the image as closest to canonical axis format.

  • labels – optional, None or sequence of (2,) sequences (2,) sequences are labels for (beginning, end) of output axis. Defaults to (('L', 'R'), ('P', 'A'), ('I', 'S')).

See Also: nibabel.orientations.ornt2axcodes.

__call__(data_array, affine=None)[source]

original orientation of data_array is defined by affine.

Parameters
  • data_array (ndarray) – in shape (num_channels, H[, W, …]).

  • affine (matrix) – (N+1)x(N+1) original affine matrix for spatially ND data_array. Defaults to identity.

Returns

data_array (reoriented in self.axcodes), original axcodes, current axcodes.

LoadNifti

class LoadNifti(as_closest_canonical=False, image_only=False, dtype=<class 'numpy.float32'>)[source]

Load Nifti format file or files from provided path. If loading a list of files, stack them together and add a new dimension as first dimension, and use the meta data of the first image to represent the stacked result. Note that the affine transform of all the images should be same if image_only=False.

Parameters
  • as_closest_canonical (bool) – if True, load the image as closest to canonical axis format.

  • image_only (bool) – if True return only the image volume, otherwise return image data array and header dict.

  • dtype (np.dtype, optional) – if not None convert the loaded image to this data type.

Note

The transform returns image data array if image_only is True, or a tuple of two elements containing the data array, and the Nifti header in a dict format otherwise. if a dictionary header is returned:

  • header[‘affine’] stores the affine of the image.

  • header[‘original_affine’] will be additionally created to store the original affine.

__call__(filename)[source]
Parameters

filename (str, list, tuple, file) – path file or file-like object or a list of files.

LoadPNG

class LoadPNG(dtype=<class 'numpy.float32'>)[source]

Load common 2D image format (PNG, JPG, etc. using PIL) file or files from provided path. It’s based on the Image module in PIL library.

Args: dtype (np.dtype, optional): if not None convert the loaded image to this data type.

__call__(filename)[source]
Parameters

filename (str, list, tuple, file) – path file or file-like object or a list of files.

AsChannelFirst

class AsChannelFirst(channel_dim=-1)[source]

Change the channel dimension of the image to the first dimension.

Most of the image transformations in monai.transforms assume the input image is in the channel-first format, which has the shape (num_channels, spatial_dim_1[, spatial_dim_2, …]).

This transform could be used to convert, for example, a channel-last image array in shape (spatial_dim_1[, spatial_dim_2, …], num_channels) into the channel-first format, so that the multidimensional image array can be correctly interpreted by the other transforms.

Parameters

channel_dim (int) – which dimension of input image is the channel, default is the last dimension.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

AsChannelLast

class AsChannelLast(channel_dim=0)[source]

Change the channel dimension of the image to the last dimension.

Some of other 3rd party transforms assume the input image is in the channel-last format with shape (spatial_dim_1[, spatial_dim_2, …], num_channels).

This transform could be used to convert, for example, a channel-first image array in shape (num_channels, spatial_dim_1[, spatial_dim_2, …]) into the channel-last format, so that MONAI transforms can construct a chain with other 3rd party transforms together.

Parameters

channel_dim (int) – which dimension of input image is the channel, default is the first dimension.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

AddChannel

class AddChannel[source]

Adds a 1-length channel dimension to the input image.

Most of the image transformations in monai.transforms assumes the input image is in the channel-first format, which has the shape (num_channels, spatial_dim_1[, spatial_dim_2, …]).

This transform could be used, for example, to convert a (spatial_dim_1[, spatial_dim_2, …]) spatial image into the channel-first format so that the multidimensional image array can be correctly interpreted by the other transforms.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RepeatChannel

class RepeatChannel(repeats)[source]

Repeat channel data to construct expected input shape for models. The repeats count includes the origin data, for example: RepeatChannel(repeats=2)([[1, 2], [3, 4]]) generates: [[1, 2], [1, 2], [3, 4], [3, 4]]

Parameters

repeats (int) – the number of repetitions for each element.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

CastToType

class CastToType(dtype=<class 'numpy.float32'>)[source]

Cast the image data to specified numpy data type.

Parameters

dtype (np.dtype) – convert image to this data type, default is np.float32.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

ToTensor

class ToTensor[source]

Converts the input image to a tensor without applying any other transformations.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

Transpose

class Transpose(indices)[source]

Transposes the input image based on the given indices dimension ordering.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandGaussianNoise

class RandGaussianNoise(prob=0.1, mean=0.0, std=0.1)[source]

Add Gaussian noise to image.

Parameters
  • prob (float) – Probability to add Gaussian noise.

  • mean (float or array of floats) – Mean or “centre” of the distribution.

  • std (float) – Standard deviation (spread) of distribution.

__call__(img)[source]

Call self as a function.

randomize(im_shape)[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

Flip

class Flip(spatial_axis=None)[source]

Reverses the order of elements along the given spatial axis. Preserves shape. Uses np.flip in practice. See numpy.flip for additional details. https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html

Parameters

spatial_axis (None, int or tuple of ints) – spatial axes along which to flip over. Default is None.

__call__(img)[source]
Parameters

img (ndarray) – channel first array, must have shape: (num_channels, H[, W, …, ]),

Resize

class Resize(spatial_size, order=1, mode='reflect', cval=0, clip=True, preserve_range=True, anti_aliasing=True, anti_aliasing_sigma=None)[source]

Resize the input image to given resolution. Uses skimage.transform.resize underneath. For additional details, see https://scikit-image.org/docs/dev/api/skimage.transform.html#skimage.transform.resize.

Parameters
  • spatial_size (tuple or list) – expected shape of spatial dimensions after resize operation.

  • order (int) – Order of spline interpolation. Default=1.

  • mode (str) – Points outside boundaries are filled according to given mode. Options are ‘constant’, ‘edge’, ‘symmetric’, ‘reflect’, ‘wrap’.

  • cval (float) – Used with mode ‘constant’, the value outside image boundaries.

  • clip (bool) – Whether to clip range of output values after interpolation. Default: True.

  • preserve_range (bool) – Whether to keep original range of values. Default is True. If False, input is converted according to conventions of img_as_float. See https://scikit-image.org/docs/dev/user_guide/data_types.html.

  • anti_aliasing (bool) – Whether to apply a gaussian filter to image before down-scaling. Default is True.

  • anti_aliasing_sigma (float, tuple of floats) – Standard deviation for gaussian filtering.

__call__(img)[source]
Parameters

img (ndarray) – channel first array, must have shape: (num_channels, H[, W, …, ]),

Rotate

class Rotate(angle, spatial_axes=(0, 1), reshape=True, order=1, mode='constant', cval=0, prefilter=True)[source]

Rotates an input image by given angle. Uses scipy.ndimage.rotate. For more details, see https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.rotate.html

Parameters
  • angle (float) – Rotation angle in degrees.

  • spatial_axes (tuple of 2 ints) – Spatial axes of rotation. Default: (0, 1). This is the first two axis in spatial dimensions.

  • reshape (bool) – If reshape is true, the output shape is adapted so that the input array is contained completely in the output. Default is True.

  • order (int) – Order of spline interpolation. Range 0-5. Default: 1. This is different from scipy where default interpolation is 3.

  • mode (str) – Points outside boundary filled according to this mode. Options are ‘constant’, ‘nearest’, ‘reflect’, ‘wrap’. Default: ‘constant’.

  • cval (scalar) – Values to fill outside boundary. Default: 0.

  • prefilter (bool) – Apply spline_filter before interpolation. Default: True.

__call__(img)[source]
Parameters

img (ndarray) – channel first array, must have shape: (num_channels, H[, W, …, ]),

Zoom

class Zoom(zoom, order=3, mode='constant', cval=0, prefilter=True, use_gpu=False, keep_size=False)[source]

Zooms a nd image. Uses scipy.ndimage.zoom or cupyx.scipy.ndimage.zoom in case of gpu. For details, please see https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.zoom.html.

Parameters
  • zoom (float or sequence) – The zoom factor along the spatial axes. If a float, zoom is the same for each spatial axis. If a sequence, zoom should contain one value for each spatial axis.

  • order (int) – order of interpolation. Default=3.

  • mode (str) – Determines how input is extended beyond boundaries. Default is ‘constant’.

  • cval (scalar, optional) – Value to fill past edges. Default is 0.

  • use_gpu (bool) – Should use cpu or gpu. Uses cupyx which doesn’t support order > 1 and modes ‘wrap’ and ‘reflect’. Defaults to cpu for these cases or if cupyx not found.

  • keep_size (bool) – Should keep original size (pad if needed).

__call__(img)[source]
Parameters

img (ndarray) – channel first array, must have shape: (num_channels, H[, W, …, ]),

ShiftIntensity

class ShiftIntensity(offset)[source]

Shift intensity uniformly for the entire image with specified offset.

Parameters

offset (int or float) – offset value to shift the intensity of image.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandShiftIntensity

class RandShiftIntensity(offsets, prob=0.1)[source]

Randomly shift intensity with randomly picked offset.

Parameters
  • offsets (int, float, tuple or list) – offset range to randomly shift. if single number, offset value is picked from (-offsets, offsets).

  • prob (float) – probability of shift.

__call__(img)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

ScaleIntensity

class ScaleIntensity(minv=0.0, maxv=1.0, factor=None, dtype=<class 'numpy.float32'>)[source]

Scale the intensity of input image to the given value range (minv, maxv). If minv and maxv not provided, use factor to scale image by v = v * (1 + factor).

Parameters
  • minv (int or float) – minimum value of output data.

  • maxv (int or float) – maximum value of output data.

  • factor (float) – factor scale by v = v * (1 + factor).

  • dtype (np.dtype) – expected output data type.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandScaleIntensity

class RandScaleIntensity(factors, prob=0.1, dtype=<class 'numpy.float32'>)[source]

Randomly scale the intensity of input image by v = v * (1 + factor) where the factor is randomly picked from (factors[0], factors[0]).

Parameters
  • factors (float, tuple or list) – factor range to randomly scale by v = v * (1 + factor). if single number, factor value is picked from (-factors, factors).

  • prob (float) – probability of scale.

  • dtype (np.dtype) – expected output data type.

__call__(img)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

NormalizeIntensity

class NormalizeIntensity(subtrahend=None, divisor=None, nonzero=False, channel_wise=False)[source]

Normalize input based on provided args, using calculated mean and std if not provided (shape of subtrahend and divisor must match. if 0, entire volume uses same subtrahend and divisor, otherwise the shape can have dimension 1 for channels). This transform can normalize only non-zero values or entire image, and can also calculate mean and std on each channel separately.

Parameters
  • subtrahend (ndarray) – the amount to subtract by (usually the mean).

  • divisor (ndarray) – the amount to divide by (usually the standard deviation).

  • nonzero (bool) – whether only normalize non-zero values.

  • channel_wise (bool) – if using calculated mean and std, calculate on each channel separately or calculate on the entire image directly.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

ThresholdIntensity

class ThresholdIntensity(threshold, above=True, cval=0)[source]

Filter the intensity values of whole image to below threshold or above threshold. And fill the remaining parts of the image to the cval value.

Parameters
  • threshold (float or int) – the threshold to filter intensity values.

  • above (bool) – filter values above the threshold or below the threshold, default is True.

  • cval (float or int) – value to fill the remaining parts of the image, default is 0.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

ScaleIntensityRange

class ScaleIntensityRange(a_min, a_max, b_min, b_max, clip=False)[source]

Apply specific intensity scaling to the whole numpy array. Scaling from [a_min, a_max] to [b_min, b_max] with clip option.

Parameters
  • a_min (int or float) – intensity original range min.

  • a_max (int or float) – intensity original range max.

  • b_min (int or float) – intensity target range min.

  • b_max (int or float) – intensity target range max.

  • clip (bool) – whether to perform clip after scaling.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

AdjustContrast

class AdjustContrast(gamma)[source]
Changes image intensity by gamma. Each pixel/voxel intensity is updated as:

x = ((x - min) / intensity_range) ^ gamma * intensity_range + min

Parameters

gamma (float) – gamma value to adjust the contrast as function.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandAdjustContrast

class RandAdjustContrast(prob=0.1, gamma=(0.5, 4.5))[source]
Randomly changes image intensity by gamma. Each pixel/voxel intensity is updated as:

x = ((x - min) / intensity_range) ^ gamma * intensity_range + min

Parameters
  • prob (float) – Probability of adjustment.

  • gamma (tuple of float or float) – Range of gamma values. If single number, value is picked from (0.5, gamma), default is (0.5, 4.5).

__call__(img)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

Rotate90

class Rotate90(k=1, spatial_axes=(0, 1))[source]

Rotate an array by 90 degrees in the plane specified by axes.

Parameters
  • k (int) – number of times to rotate by 90 degrees.

  • spatial_axes (2 ints) – defines the plane to rotate with 2 spatial axes. Default: (0, 1), this is the first two axis in spatial dimensions.

__call__(img)[source]
Parameters

img (ndarray) – channel first array, must have shape: (num_channels, H[, W, …, ]),

RandRotate90

class RandRotate90(prob=0.1, max_k=3, spatial_axes=(0, 1))[source]

With probability prob, input arrays are rotated by 90 degrees in the plane specified by spatial_axes.

Parameters
  • prob (float) – probability of rotating. (Default 0.1, with 10% probability it returns a rotated array)

  • max_k (int) – number of rotations will be sampled from np.random.randint(max_k) + 1. (Default 3)

  • spatial_axes (2 ints) – defines the plane to rotate with 2 spatial axes. Default: (0, 1), this is the first two axis in spatial dimensions.

__call__(img)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

SpatialPad

class SpatialPad(spatial_size, method='symmetric', mode='constant')[source]
Performs padding to the data, symmetric for all sides or all on one side for each dimension.

Uses np.pad so in practice, a mode needs to be provided. See numpy.lib.arraypad.pad for additional details.

Parameters
  • spatial_size (list) – the spatial size of output data after padding.

  • method (str) – pad image symmetric on every side or only pad at the end sides. default is ‘symmetric’.

  • mode (str) – one of the following string values or a user supplied function: {‘constant’, ‘edge’, ‘linear_ramp’, ‘maximum’, ‘mean’, ‘median’, ‘minimum’, ‘reflect’, ‘symmetric’, ‘wrap’, ‘empty’, <function>} for more details, please check: https://docs.scipy.org/doc/numpy/reference/generated/numpy.pad.html

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

SpatialCrop

class SpatialCrop(roi_center=None, roi_size=None, roi_start=None, roi_end=None)[source]

General purpose cropper to produce sub-volume region of interest (ROI). It can support to crop ND spatial (channel-first) data. Either a spatial center and size must be provided, or alternatively if center and size are not provided, the start and end coordinates of the ROI must be provided. The sub-volume must sit the within original image. Note: This transform will not work if the crop region is larger than the image itself.

Parameters
  • roi_center (list or tuple) – voxel coordinates for center of the crop ROI.

  • roi_size (list or tuple) – size of the crop ROI.

  • roi_start (list or tuple) – voxel coordinates for start of the crop ROI.

  • roi_end (list or tuple) – voxel coordinates for end of the crop ROI.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

CenterSpatialCrop

class CenterSpatialCrop(roi_size)[source]

Crop at the center of image with specified ROI size.

Parameters

roi_size (list, tuple) – the spatial size of the crop region e.g. [224,224,128]

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandSpatialCrop

class RandSpatialCrop(roi_size, random_center=True, random_size=True)[source]

Crop image with random size or specific size ROI. It can crop at a random position as center or at the image center. And allows to set the minimum size to limit the randomly generated ROI. This transform assumes all the expected fields specified by keys have same shape.

Parameters
  • roi_size (list, tuple) – if random_size is True, the spatial size of the minimum crop region. if random_size is False, specify the expected ROI size to crop. e.g. [224, 224, 128]

  • random_center (bool) – crop at random position as center or the image center.

  • random_size (bool) – crop with random size or specific size ROI.

__call__(img)[source]

Call self as a function.

randomize(img_size)[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

CropForeground

class CropForeground(select_fn=<function CropForeground.<lambda>>, channel_indexes=None, margin=0)[source]

Crop an image using a bounding box. The bounding box is generated by selecting foreground using select_fn at channels channel_indexes. margin is added in each spatial dimension of the bounding box. The typical usage is to help training and evaluation if the valid part is small in the whole medical image. Users can define arbitrary function to select expected foreground from the whole image or specified channels. And it can also add margin to every dim of the bounding box of foreground object. For example:

image = np.array(
    [[[0, 0, 0, 0, 0],
      [0, 1, 2, 1, 0],
      [0, 1, 3, 2, 0],
      [0, 1, 2, 1, 0],
      [0, 0, 0, 0, 0]]])  # 1x5x5, single channel 5x5 image
cropper = CropForeground(select_fn=lambda x: x > 1, margin=0)
print(cropper(image))
[[[2, 1],
  [3, 2],
  [2, 1]]]
Parameters
  • select_fn (Callable) – function to select expected foreground, default is to select values > 0.

  • channel_indexes (int, tuple or list) – if defined, select foregound only on the specified channels of image. if None, select foreground on the whole image.

  • margin (int) – add margin to all dims of the bounding box.

__call__(img)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandRotate

class RandRotate(degrees, prob=0.1, spatial_axes=(0, 1), reshape=True, order=1, mode='constant', cval=0, prefilter=True)[source]

Randomly rotates the input arrays.

Parameters
  • prob (float) – Probability of rotation.

  • degrees (tuple of float or float) – Range of rotation in degrees. If single number, angle is picked from (-degrees, degrees).

  • spatial_axes (tuple of 2 ints) – Spatial axes of rotation. Default: (0, 1). This is the first two axis in spatial dimensions.

  • reshape (bool) – If reshape is true, the output shape is adapted so that the input array is contained completely in the output. Default is True.

  • order (int) – Order of spline interpolation. Range 0-5. Default: 1. This is different from scipy where default interpolation is 3.

  • mode (str) – Points outside boundary filled according to this mode. Options are ‘constant’, ‘nearest’, ‘reflect’, ‘wrap’. Default: ‘constant’.

  • cval (scalar) – Value to fill outside boundary. Default: 0.

  • prefilter (bool) – Apply spline_filter before interpolation. Default: True.

__call__(img)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

RandFlip

class RandFlip(prob=0.1, spatial_axis=None)[source]

Randomly flips the image along axes. Preserves shape. See numpy.flip for additional details. https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html

Parameters
  • prob (float) – Probability of flipping.

  • spatial_axis (None, int or tuple of ints) – Spatial axes along which to flip over. Default is None.

__call__(img)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

RandZoom

class RandZoom(prob=0.1, min_zoom=0.9, max_zoom=1.1, order=3, mode='constant', cval=0, prefilter=True, use_gpu=False, keep_size=False)[source]

Randomly zooms input arrays with given probability within given zoom range.

Parameters
  • prob (float) – Probability of zooming.

  • min_zoom (float or sequence) – Min zoom factor. Can be float or sequence same size as image. If a float, min_zoom is the same for each spatial axis. If a sequence, min_zoom should contain one value for each spatial axis.

  • max_zoom (float or sequence) – Max zoom factor. Can be float or sequence same size as image. If a float, max_zoom is the same for each spatial axis. If a sequence, max_zoom should contain one value for each spatial axis.

  • order (int) – order of interpolation. Default=3.

  • mode ('reflect', 'constant', 'nearest', 'mirror', 'wrap') – Determines how input is extended beyond boundaries. Default: ‘constant’.

  • cval (scalar, optional) – Value to fill past edges. Default is 0.

  • use_gpu (bool) – Should use cpu or gpu. Uses cupyx which doesn’t support order > 1 and modes ‘wrap’ and ‘reflect’. Defaults to cpu for these cases or if cupyx not found.

  • keep_size (bool) – Should keep original size (pad if needed).

__call__(img)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

Affine

class Affine(rotate_params=None, shear_params=None, translate_params=None, scale_params=None, spatial_size=None, mode='bilinear', padding_mode='zeros', as_tensor_output=False, device=None)[source]

transform img given the affine parameters.

The affine transformations are applied in rotate, shear, translate, scale order.

Parameters
  • rotate_params (float, list of floats) – a rotation angle in radians, a scalar for 2D image, a tuple of 3 floats for 3D. Defaults to no rotation.

  • shear_params (list of floats) – a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing.

  • translate_params (list of floats) – a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Translation is in pixel/voxel relative to the center of the input image. Defaults to no translation.

  • scale_params (list of floats) – a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Defaults to no scaling.

  • spatial_size (list or tuple of int) – output image spatial size. if img has two spatial dimensions, spatial_size should have 2 elements [h, w]. if img has three spatial dimensions, spatial_size should have 3 elements [h, w, d].

  • mode ('nearest'|'bilinear') – interpolation order. Defaults to ‘bilinear’.

  • padding_mode ('zeros'|'border'|'reflection') – mode of handling out of range indices. Defaults to ‘zeros’.

  • as_tensor_output (bool) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.

  • device (torch.device) – device on which the tensor will be allocated.

__call__(img, spatial_size=None, mode=None)[source]
Parameters
  • img (ndarray or tensor) – shape must be (num_channels, H, W[, D]),

  • spatial_size (list or tuple of int) – output image spatial size. if img has two spatial dimensions, spatial_size should have 2 elements [h, w]. if img has three spatial dimensions, spatial_size should have 3 elements [h, w, d].

  • mode ('nearest'|'bilinear') – interpolation order. Defaults to ‘bilinear’.

Resample

class Resample(padding_mode='zeros', as_tensor_output=False, device=None)[source]

computes output image using values from img, locations from grid using pytorch. supports spatially 2D or 3D (num_channels, H, W[, D]).

Parameters
  • padding_mode ('zeros'|'border'|'reflection') – mode of handling out of range indices. Defaults to ‘zeros’.

  • as_tensor_output (bool) – whether to return a torch tensor. Defaults to False.

  • device (torch.device) – device on which the tensor will be allocated.

__call__(img, grid, mode='bilinear')[source]
Parameters
  • img (ndarray or tensor) – shape must be (num_channels, H, W[, D]).

  • grid (ndarray or tensor) – shape must be (3, H, W) for 2D or (4, H, W, D) for 3D.

  • mode ('nearest'|'bilinear') – interpolation order. Defaults to ‘bilinear’.

RandAffine

class RandAffine(prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, spatial_size=None, mode='bilinear', padding_mode='zeros', as_tensor_output=True, device=None)[source]

Random affine transform.

Parameters
  • prob (float) – probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid.

  • spatial_size (list or tuple of int) – output image spatial size. if img has two spatial dimensions, spatial_size should have 2 elements [h, w]. if img has three spatial dimensions, spatial_size should have 3 elements [h, w, d].

  • mode ('nearest'|'bilinear') – interpolation order. Defaults to ‘bilinear’.

  • padding_mode ('zeros'|'border'|'reflection') – mode of handling out of range indices. Defaults to ‘zeros’.

  • as_tensor_output (bool) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.

  • device (torch.device) – device on which the tensor will be allocated.

See also

  • RandAffineGrid for the random affine parameters configurations.

  • Affine for the affine transformation parameters configurations.

__call__(img, spatial_size=None, mode=None)[source]
Parameters
  • img (ndarray or tensor) – shape must be (num_channels, H, W[, D]),

  • spatial_size (list or tuple of int) – output image spatial size. if img has two spatial dimensions, spatial_size should have 2 elements [h, w]. if img has three spatial dimensions, spatial_size should have 3 elements [h, w, d].

  • mode ('nearest'|'bilinear') – interpolation order. Defaults to ‘bilinear’.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

set_random_state(seed=None, state=None)[source]

Set the random state locally, to control the randomness, the derived classes should use self.R instead of np.random to introduce random factors.

Parameters
  • seed (int) – set the random state with an integer seed.

  • state (np.random.RandomState) – set the random state with a np.random.RandomState object.

Returns

a Randomizable instance.

RandDeformGrid

class RandDeformGrid(spacing, magnitude_range, as_tensor_output=True, device=None)[source]

generate random deformation grid

Parameters
  • spacing (2 or 3 ints) – spacing of the grid in 2D or 3D. e.g., spacing=(1, 1) indicates pixel-wise deformation in 2D, spacing=(1, 1, 1) indicates voxel-wise deformation in 3D, spacing=(2, 2) indicates deformation field defined on every other pixel in 2D.

  • magnitude_range (2 ints) – the random offsets will be generated from uniform[magnitude[0], magnitude[1]).

  • as_tensor_output (bool) – whether to output tensor instead of numpy array. defaults to True.

  • device (torch device) – device to store the output grid data.

__call__(spatial_size)[source]

Call self as a function.

randomize(grid_size)[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

RandAffineGrid

class RandAffineGrid(rotate_range=None, shear_range=None, translate_range=None, scale_range=None, as_tensor_output=True, device=None)[source]

generate randomised affine grid

Parameters
  • rotate_range (a sequence of positive floats) – rotate_range[0] with be used to generate the 1st rotation parameter from uniform[-rotate_range[0], rotate_range[0]). Similarly, rotate_range[2] and rotate_range[3] are used in 3D affine for the range of 2nd and 3rd axes.

  • shear_range (a sequence of positive floats) – shear_range[0] with be used to generate the 1st shearing parameter from uniform[-shear_range[0], shear_range[0]). Similarly, shear_range[1] to shear_range[N] controls the range of the uniform distribution used to generate the 2nd to N-th parameter.

  • translate_range (a sequence of positive floats) – translate_range[0] with be used to generate the 1st shift parameter from uniform[-translate_range[0], translate_range[0]). Similarly, translate_range[1] to translate_range[N] controls the range of the uniform distribution used to generate the 2nd to N-th parameter.

  • scale_range (a sequence of positive floats) – scaling_range[0] with be used to generate the 1st scaling factor from uniform[-scale_range[0], scale_range[0]) + 1.0. Similarly, scale_range[1] to scale_range[N] controls the range of the uniform distribution used to generate the 2nd to N-th parameter.

__call__(spatial_size=None, grid=None)[source]
Returns

a 2D (3xHxW) or 3D (4xHxWxD) grid.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

Rand2DElastic

class Rand2DElastic(spacing, magnitude_range, prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, spatial_size=None, mode='bilinear', padding_mode='zeros', as_tensor_output=False, device=None)[source]

Random elastic deformation and affine in 2D

Parameters
  • spacing (2 ints) – distance in between the control points.

  • magnitude_range (2 ints) – the random offsets will be generated from uniform[magnitude[0], magnitude[1]).

  • prob (float) – probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid, otherwise returns a spatial_size centered area extracted from the input image.

  • spatial_size (2 ints) – specifying output image spatial size [h, w].

  • mode ('nearest'|'bilinear') – interpolation order. Defaults to 'bilinear'.

  • padding_mode ('zeros'|'border'|'reflection') – mode of handling out of range indices. Defaults to 'zeros'.

  • as_tensor_output (bool) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.

  • device (torch.device) – device on which the tensor will be allocated.

See also

  • RandAffineGrid for the random affine parameters configurations.

  • Affine for the affine transformation parameters configurations.

__call__(img, spatial_size=None, mode=None)[source]
Parameters
  • img (ndarray or tensor) – shape must be (num_channels, H, W),

  • spatial_size (2 ints) – specifying output image spatial size [h, w].

  • mode ('nearest'|'bilinear') – interpolation order. Defaults to self.mode.

randomize(spatial_size)[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

set_random_state(seed=None, state=None)[source]

Set the random state locally, to control the randomness, the derived classes should use self.R instead of np.random to introduce random factors.

Parameters
  • seed (int) – set the random state with an integer seed.

  • state (np.random.RandomState) – set the random state with a np.random.RandomState object.

Returns

a Randomizable instance.

Rand3DElastic

class Rand3DElastic(sigma_range, magnitude_range, prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, spatial_size=None, mode='bilinear', padding_mode='zeros', as_tensor_output=False, device=None)[source]

Random elastic deformation and affine in 3D

Parameters
  • sigma_range (2 ints) – a Gaussian kernel with standard deviation sampled from uniform[sigma_range[0], sigma_range[1]) will be used to smooth the random offset grid.

  • magnitude_range (2 ints) – the random offsets on the grid will be generated from uniform[magnitude[0], magnitude[1]).

  • prob (float) – probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid, otherwise returns a spatial_size centered area extracted from the input image.

  • spatial_size (3 ints) – specifying output image spatial size [h, w, d].

  • mode ('nearest'|'bilinear') – interpolation order. Defaults to 'bilinear'.

  • padding_mode ('zeros'|'border'|'reflection') – mode of handling out of range indices. Defaults to 'zeros'.

  • as_tensor_output (bool) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.

  • device (torch.device) – device on which the tensor will be allocated.

See also

  • RandAffineGrid for the random affine parameters configurations.

  • Affine for the affine transformation parameters configurations.

__call__(img, spatial_size=None, mode=None)[source]
Parameters
  • img (ndarray or tensor) – shape must be (num_channels, H, W, D),

  • spatial_size (3 ints) – specifying spatial 3D output image spatial size [h, w, d].

  • mode ('nearest'|'bilinear') – interpolation order. Defaults to ‘self.mode’.

randomize(grid_size)[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

set_random_state(seed=None, state=None)[source]

Set the random state locally, to control the randomness, the derived classes should use self.R instead of np.random to introduce random factors.

Parameters
  • seed (int) – set the random state with an integer seed.

  • state (np.random.RandomState) – set the random state with a np.random.RandomState object.

Returns

a Randomizable instance.

Dictionary-based Composables

A collection of dictionary-based wrappers around the “vanilla” transforms defined in monai.transforms.transforms.

Class names are ended with ‘d’ to denote dictionary-based transforms.

Spacingd

class Spacingd(keys, pixdim, diagonal=False, mode='constant', cval=0, interp_order=3, dtype=None, meta_key_format='{}.{}')[source]

Dictionary-based wrapper of monai.transforms.transforms.Spacing.

This transform assumes the data dictionary has a key for the input data’s affine. The key is formed by meta_key_format.format(key, 'affine').

After resampling the input array, this transform will write the new affine

to the key formed by meta_key_format.format(key, 'affine').

Parameters
  • pixdim (sequence of floats) – output voxel spacing.

  • diagonal (bool) –

    whether to resample the input to have a diagonal affine matrix. If True, the input data is resampled to the following affine:

    np.diag((pixdim_0, pixdim_1, pixdim_2, 1))
    

    This effectively resets the volume to the world coordinate system (RAS+ in nibabel). The original orientation, rotation, shearing are not preserved.

    If False, the axes orientation, orthogonal rotation and translations components from the original affine will be preserved in the target affine. This option will not flip/swap axes against the original ones.

  • mode (reflect|constant|nearest|mirror|wrap) – The mode parameter determines how the input array is extended beyond its boundaries. Default is ‘constant’.

  • cval (scalar) – Value to fill past edges of input if mode is “constant”. Default is 0.0.

  • interp_order (int or sequence of ints) – int: the same interpolation order for all data indexed by self.keys; sequence of ints, should correspond to an interpolation order for each data item indexed by self.keys respectively.

  • dtype (None or np.dtype) – output array data type, defaults to None to use input data’s dtype.

  • meta_key_format (str) – key format to read/write affine matrices to the data dictionary.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

Orientationd

class Orientationd(keys, axcodes=None, as_closest_canonical=False, labels=(('L', 'R'), ('P', 'A'), ('I', 'S')), meta_key_format='{}.{}')[source]

Dictionary-based wrapper of monai.transforms.transforms.Orientation.

This transform assumes the data dictionary has a key for the input data’s affine. The key is formed by meta_key_format.format(key, 'affine').

After reorientate the input array, this transform will write the new affine

to the key formed by meta_key_format.format(key, 'affine').

Parameters
  • axcodes (N elements sequence) – for spatial ND input’s orientation. e.g. axcodes=’RAS’ represents 3D orientation: (Left, Right), (Posterior, Anterior), (Inferior, Superior). default orientation labels options are: ‘L’ and ‘R’ for the first dimension, ‘P’ and ‘A’ for the second, ‘I’ and ‘S’ for the third.

  • as_closest_canonical (boo) – if True, load the image as closest to canonical axis format.

  • labels – optional, None or sequence of (2,) sequences (2,) sequences are labels for (beginning, end) of output axis. Defaults to (('L', 'R'), ('P', 'A'), ('I', 'S')).

  • meta_key_format (str) – key format to read/write affine matrices to the data dictionary.

See also

nibabel.orientations.ornt2axcodes.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

LoadNiftid

class LoadNiftid(keys, as_closest_canonical=False, dtype=<class 'numpy.float32'>, meta_key_format='{}.{}', overwriting_keys=False)[source]

Dictionary-based wrapper of monai.transforms.transfroms.LoadNifti, must load image and metadata together. If loading a list of files in one key, stack them together and add a new dimension as the first dimension, and use the meta data of the first image to represent the stacked result. Note that the affine transform of all the stacked images should be same. The output metadata field will be created as self.meta_key_format(key, metadata_key).

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • as_closest_canonical (bool) – if True, load the image as closest to canonical axis format.

  • dtype (np.dtype, optional) – if not None convert the loaded image to this data type.

  • meta_key_format (str) – key format to store meta data of the nifti image. it must contain 2 fields for the key of this image and the key of every meta data item.

  • overwriting_keys (bool) – whether allow to overwrite existing keys of meta data. default is False, which will raise exception if encountering existing key.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

LoadPNGd

class LoadPNGd(keys, dtype=<class 'numpy.float32'>)[source]

Dictionary-based wrapper of monai.transforms.transfroms.LoadPNG.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • dtype (np.dtype, optional) – if not None convert the loaded image to this data type.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

AsChannelFirstd

class AsChannelFirstd(keys, channel_dim=-1)[source]

Dictionary-based wrapper of monai.transforms.transfroms.AsChannelFirst.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • channel_dim (int) – which dimension of input image is the channel, default is the last dimension.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

AsChannelLastd

class AsChannelLastd(keys, channel_dim=0)[source]

Dictionary-based wrapper of monai.transforms.transfroms.AsChannelLast.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • channel_dim (int) – which dimension of input image is the channel, default is the first dimension.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

AddChanneld

class AddChanneld(keys)[source]

Dictionary-based wrapper of monai.transforms.transfroms.AddChannel.

Parameters

keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RepeatChanneld

class RepeatChanneld(keys, repeats)[source]

dictionary-based wrapper of monai.transforms.transforms.RepeatChannel.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • repeats (int) – the number of repetitions for each element.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

CastToTyped

class CastToTyped(keys, dtype=<class 'numpy.float32'>)[source]

Dictionary-based wrapper of monai.transforms.transfroms.CastToType.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • dtype (np.dtype) – convert image to this data type, default is np.float32.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

ToTensord

class ToTensord(keys)[source]

Dictionary-based wrapper of monai.transforms.transfroms.ToTensor.

Parameters

keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

Rotate90d

class Rotate90d(keys, k=1, spatial_axes=(0, 1))[source]

Dictionary-based wrapper of monai.transforms.transfroms.Rotate90.

Parameters
  • k (int) – number of times to rotate by 90 degrees.

  • spatial_axes (2 ints) – defines the plane to rotate with 2 spatial axes. Default: (0, 1), this is the first two axis in spatial dimensions.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

Resized

class Resized(keys, spatial_size, order=1, mode='reflect', cval=0, clip=True, preserve_range=True, anti_aliasing=True, anti_aliasing_sigma=None)[source]

Dictionary-based wrapper of monai.transforms.transfroms.Resize.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • spatial_size (tuple or list) – expected shape of spatial dimensions after resize operation.

  • order (int) – Order of spline interpolation. Default=1.

  • mode (str) – Points outside boundaries are filled according to given mode. Options are ‘constant’, ‘edge’, ‘symmetric’, ‘reflect’, ‘wrap’.

  • cval (float) – Used with mode ‘constant’, the value outside image boundaries.

  • clip (bool) – Whether to clip range of output values after interpolation. Default: True.

  • preserve_range (bool) – Whether to keep original range of values. Default is True. If False, input is converted according to conventions of img_as_float. See https://scikit-image.org/docs/dev/user_guide/data_types.html.

  • anti_aliasing (bool) – Whether to apply a gaussian filter to image before down-scaling. Default is True.

  • anti_aliasing_sigma (float, tuple of floats) – Standard deviation for gaussian filtering.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandGaussianNoised

class RandGaussianNoised(keys, prob=0.1, mean=0.0, std=0.1)[source]

Dictionary-based version monai.transforms.transfroms.RandGaussianNoise. Add Gaussian noise to image. This transform assumes all the expected fields have same shape.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • prob (float) – Probability to add Gaussian noise.

  • mean (float or array of floats) – Mean or “centre” of the distribution.

  • std (float) – Standard deviation (spread) of distribution.

__call__(data)[source]

Call self as a function.

randomize(im_shape)[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

RandRotate90d

class RandRotate90d(keys, prob=0.1, max_k=3, spatial_axes=(0, 1))[source]

Dictionary-based version monai.transforms.transfroms.RandRotate90. With probability prob, input arrays are rotated by 90 degrees in the plane specified by spatial_axes.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • prob (float) – probability of rotating. (Default 0.1, with 10% probability it returns a rotated array.)

  • max_k (int) – number of rotations will be sampled from np.random.randint(max_k) + 1. (Default 3)

  • spatial_axes (2 ints) – defines the plane to rotate with 2 spatial axes. Default: (0, 1), this is the first two axis in spatial dimensions.

__call__(data)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

ShiftIntensityd

class ShiftIntensityd(keys, offset)[source]

dictionary-based wrapper of monai.transforms.transforms.ShiftIntensity.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • offset (int or float) – offset value to shift the intensity of image.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandShiftIntensityd

class RandShiftIntensityd(keys, offsets, prob=0.1)[source]

dictionary-based version monai.transforms.transforms.RandShiftIntensity.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • offsets (int, float, tuple or list) – offset range to randomly shift. if single number, offset value is picked from (-offsets, offsets).

  • prob (float) – probability of rotating. (Default 0.1, with 10% probability it returns a rotated array.)

__call__(data)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

ScaleIntensityd

class ScaleIntensityd(keys, minv=0.0, maxv=1.0, factor=None, dtype=<class 'numpy.float32'>)[source]

dictionary-based wrapper of monai.transforms.transforms.ScaleIntensity. Scale the intensity of input image to the given value range (minv, maxv). If minv and maxv not provided, use factor to scale image by v = v * (1 + factor).

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • minv (int or float) – minimum value of output data.

  • maxv (int or float) – maximum value of output data.

  • factor (float) – factor scale by v = v * (1 + factor).

  • dtype (np.dtype) – expected output data type.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandScaleIntensityd

class RandScaleIntensityd(keys, factors, prob=0.1, dtype=<class 'numpy.float32'>)[source]

dictionary-based version monai.transforms.transforms.RandScaleIntensity.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • factors (float, tuple or list) – factor range to randomly scale by v = v * (1 + factor). if single number, factor value is picked from (-factors, factors).

  • prob (float) – probability of rotating. (Default 0.1, with 10% probability it returns a rotated array.)

  • dtype (np.dtype) – expected output data type.

__call__(data)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

NormalizeIntensityd

class NormalizeIntensityd(keys, subtrahend=None, divisor=None, nonzero=False, channel_wise=False)[source]

dictionary-based wrapper of monai.transforms.transforms.NormalizeIntensity. This transform can normalize only non-zero values or entire image, and can also calculate mean and std on each channel separately.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transform.composables.MapTransform

  • subtrahend (ndarray) – the amount to subtract by (usually the mean)

  • divisor (ndarray) – the amount to divide by (usually the standard deviation)

  • nonzero (bool) – whether only normalize non-zero values.

  • channel_wise (bool) – if using calculated mean and std, calculate on each channel separately or calculate on the entire image directly.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

ThresholdIntensityd

class ThresholdIntensityd(keys, threshold, above=True, cval=0)[source]

Dictionary-based wrapper of monai.transforms.transfroms.ThresholdIntensity.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transform.composables.MapTransform

  • threshold (float or int) – the threshold to filter intensity values.

  • above (bool) – filter values above the threshold or below the threshold, default is True.

  • cval (float or int) – value to fill the remaining parts of the image, default is 0.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

ScaleIntensityRanged

class ScaleIntensityRanged(keys, a_min, a_max, b_min, b_max, clip=False)[source]

Dictionary-based wrapper of monai.transforms.transfroms.ScaleIntensityRange.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transform.composables.MapTransform

  • a_min (int or float) – intensity original range min.

  • a_max (int or float) – intensity original range max.

  • b_min (int or float) – intensity target range min.

  • b_max (int or float) – intensity target range max.

  • clip (bool) – whether to perform clip after scaling.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

AdjustContrastd

class AdjustContrastd(keys, gamma)[source]

Dictionary-based wrapper of monai.transforms.transfroms.AdjustContrast. Changes image intensity by gamma. Each pixel/voxel intensity is updated as:

x = ((x - min) / intensity_range) ^ gamma * intensity_range + min

Parameters

gamma (float) – gamma value to adjust the contrast as function.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandAdjustContrastd

class RandAdjustContrastd(keys, prob=0.1, gamma=(0.5, 4.5))[source]

Dictionary-based version monai.transforms.transfroms.RandAdjustContrast. Randomly changes image intensity by gamma. Each pixel/voxel intensity is updated as:

x = ((x - min) / intensity_range) ^ gamma * intensity_range + min

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transform.composables.MapTransform

  • prob (float) – Probability of adjustment.

  • gamma (tuple of float or float) – Range of gamma values. If single number, value is picked from (0.5, gamma), default is (0.5, 4.5).

__call__(data)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

SpatialPadd

class SpatialPadd(keys, spatial_size, method='symmetric', mode='constant')[source]

dictionary-based wrapper of monai.transforms.compose.SpatialPad. Performs padding to the data, symmetric for all sides or all on one side for each dimension.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • spatial_size (list) – the spatial size of output data after padding.

  • method (str) – pad image symmetric on every side or only pad at the end sides. default is ‘symmetric’.

  • mode (str) – one of the following string values or a user supplied function: {‘constant’, ‘edge’, ‘linear_ramp’, ‘maximum’, ‘mean’, ‘median’, ‘minimum’, ‘reflect’, ‘symmetric’, ‘wrap’, ‘empty’, <function>} for more details, please check: https://docs.scipy.org/doc/numpy/reference/generated/numpy.pad.html

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

SpatialCropd

class SpatialCropd(keys, roi_center=None, roi_size=None, roi_start=None, roi_end=None)[source]

dictionary-based wrapper of monai.transforms.compose.SpatialCrop. Either a spatial center and size must be provided, or alternatively if center and size are not provided, the start and end coordinates of the ROI must be provided.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • roi_center (list or tuple) – voxel coordinates for center of the crop ROI.

  • roi_size (list or tuple) – size of the crop ROI.

  • roi_start (list or tuple) – voxel coordinates for start of the crop ROI.

  • roi_end (list or tuple) – voxel coordinates for end of the crop ROI.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

CenterSpatialCropd

class CenterSpatialCropd(keys, roi_size)[source]

Dictionary-based wrapper of monai.transforms.transfroms.CenterSpatialCrop.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transform.composables.MapTransform

  • roi_size (list, tuple) – the size of the crop region e.g. [224,224,128]

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandSpatialCropd

class RandSpatialCropd(keys, roi_size, random_center=True, random_size=True)[source]

Dictionary-based version monai.transforms.transfroms.RandSpatialCrop. Crop image with random size or specific size ROI. It can crop at a random position as center or at the image center. And allows to set the minimum size to limit the randomly generated ROI. Suppose all the expected fields specified by keys have same shape.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transform.composables.MapTransform

  • roi_size (list, tuple) – if random_size is True, the spatial size of the minimum crop region. if random_size is False, specify the expected ROI size to crop. e.g. [224, 224, 128]

  • random_center (bool) – crop at random position as center or the image center.

  • random_size (bool) – crop with random size or specific size ROI.

__call__(data)[source]

Call self as a function.

randomize(img_size)[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

CropForegroundd

class CropForegroundd(keys, source_key, select_fn=<function CropForegroundd.<lambda>>, channel_indexes=None, margin=0)[source]

dictionary-based version monai.transforms.transforms.CropForeground. Crop only the foreground object of the expected images. The typical usage is to help training and evaluation if the valid part is small in the whole medical image. The valid part can be determined by any field in the data with source_key, for example: - Select values > 0 in image field as the foreground and crop on all fields specified by keys. - Select label = 3 in label field as the foreground to crop on all fields specified by keys. - Select label > 0 in the third channel of a One-Hot label field as the foreground to crop all keys fields. Users can define arbitrary function to select expected foreground from the whole source image or specified channels. And it can also add margin to every dim of the bounding box of foreground object.

Parameters
  • keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

  • source_key (str) – data source to generate the bounding box of foreground, can be image or label, etc.

  • select_fn (Callable) – function to select expected foreground, default is to select values > 0.

  • channel_indexes (int, tuple or list) – if defined, select foregound only on the specified channels of image. if None, select foreground on the whole image.

  • margin (int) – add margin to all dims of the bounding box.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandCropByPosNegLabeld

class RandCropByPosNegLabeld(keys, label_key, size, pos=1, neg=1, num_samples=1, image_key=None, image_threshold=0)[source]

Crop random fixed sized regions with the center being a foreground or background voxel based on the Pos Neg Ratio. And will return a list of dictionaries for all the cropped images.

Parameters
  • keys (list) – parameter will be used to get and set the actual data item to transform.

  • label_key (str) – name of key for label image, this will be used for finding foreground/background.

  • size (list, tuple) – the size of the crop region e.g. [224,224,128]

  • pos (int, float) – used to calculate the ratio pos / (pos + neg) for the probability to pick a foreground voxel as a center rather than a background voxel.

  • neg (int, float) – used to calculate the ratio pos / (pos + neg) for the probability to pick a foreground voxel as a center rather than a background voxel.

  • num_samples (int) – number of samples (crop regions) to take in each list.

  • image_key (str) – if image_key is not None, use label == 0 & image > image_threshold to select the negative sample(background) center. so the crop center will only exist on valid image area.

  • image_threshold (int or float) – if enabled image_key, use image > image_threshold to determine the valid image content area.

__call__(data)[source]

Call self as a function.

randomize(label, image)[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

RandAffined

class RandAffined(keys, spatial_size, prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, mode='bilinear', padding_mode='zeros', as_tensor_output=True, device=None)[source]

Dictionary-based wrapper of monai.transforms.transforms.RandAffine.

Parameters
  • keys (Hashable items) – keys of the corresponding items to be transformed.

  • spatial_size (list or tuple of int) – output image spatial size. if data component has two spatial dimensions, spatial_size should have 2 elements [h, w]. if data component has three spatial dimensions, spatial_size should have 3 elements [h, w, d].

  • prob (float) – probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid.

  • mode ('nearest'|'bilinear') – interpolation order. Defaults to 'bilinear'. if mode is a tuple of interpolation mode strings, each string corresponds to a key in keys. this is useful to set different modes for different data items.

  • padding_mode ('zeros'|'border'|'reflection') – mode of handling out of range indices. Defaults to 'zeros'.

  • as_tensor_output (bool) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.

  • device (torch.device) – device on which the tensor will be allocated.

See also

__call__(data)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

set_random_state(seed=None, state=None)[source]

Set the random state locally, to control the randomness, the derived classes should use self.R instead of np.random to introduce random factors.

Parameters
  • seed (int) – set the random state with an integer seed.

  • state (np.random.RandomState) – set the random state with a np.random.RandomState object.

Returns

a Randomizable instance.

Rand2DElasticd

class Rand2DElasticd(keys, spatial_size, spacing, magnitude_range, prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, mode='bilinear', padding_mode='zeros', as_tensor_output=False, device=None)[source]

Dictionary-based wrapper of monai.transforms.transforms.Rand2DElastic.

Parameters
  • keys (Hashable items) – keys of the corresponding items to be transformed.

  • spatial_size (2 ints) – specifying output image spatial size [h, w].

  • spacing (2 ints) – distance in between the control points.

  • magnitude_range (2 ints) – the random offsets will be generated from uniform[magnitude[0], magnitude[1]).

  • prob (float) – probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid, otherwise returns a spatial_size centered area extracted from the input image.

  • mode ('nearest'|'bilinear') – interpolation order. Defaults to 'bilinear'. if mode is a tuple of interpolation mode strings, each string corresponds to a key in keys. this is useful to set different modes for different data items.

  • padding_mode ('zeros'|'border'|'reflection') – mode of handling out of range indices. Defaults to 'zeros'.

  • as_tensor_output (bool) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.

  • device (torch.device) – device on which the tensor will be allocated.

See also

  • RandAffineGrid for the random affine parameters configurations.

  • Affine for the affine transformation parameters configurations.

__call__(data)[source]

Call self as a function.

randomize(spatial_size)[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

set_random_state(seed=None, state=None)[source]

Set the random state locally, to control the randomness, the derived classes should use self.R instead of np.random to introduce random factors.

Parameters
  • seed (int) – set the random state with an integer seed.

  • state (np.random.RandomState) – set the random state with a np.random.RandomState object.

Returns

a Randomizable instance.

Rand3DElasticd

class Rand3DElasticd(keys, spatial_size, sigma_range, magnitude_range, prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, mode='bilinear', padding_mode='zeros', as_tensor_output=False, device=None)[source]

Dictionary-based wrapper of monai.transforms.transforms.Rand3DElastic.

Parameters
  • keys (Hashable items) – keys of the corresponding items to be transformed.

  • spatial_size (3 ints) – specifying output image spatial size [h, w, d].

  • sigma_range (2 ints) – a Gaussian kernel with standard deviation sampled from uniform[sigma_range[0], sigma_range[1]) will be used to smooth the random offset grid.

  • magnitude_range (2 ints) – the random offsets on the grid will be generated from uniform[magnitude[0], magnitude[1]).

  • prob (float) – probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid, otherwise returns a spatial_size centered area extracted from the input image.

  • mode ('nearest'|'bilinear') – interpolation order. Defaults to 'bilinear'. if mode is a tuple of interpolation mode strings, each string corresponds to a key in keys. this is useful to set different modes for different data items.

  • padding_mode ('zeros'|'border'|'reflection') – mode of handling out of range indices. Defaults to 'zeros'.

  • as_tensor_output (bool) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.

  • device (torch.device) – device on which the tensor will be allocated.

See also

  • RandAffineGrid for the random affine parameters configurations.

  • Affine for the affine transformation parameters configurations.

__call__(data)[source]

Call self as a function.

randomize(grid_size)[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

set_random_state(seed=None, state=None)[source]

Set the random state locally, to control the randomness, the derived classes should use self.R instead of np.random to introduce random factors.

Parameters
  • seed (int) – set the random state with an integer seed.

  • state (np.random.RandomState) – set the random state with a np.random.RandomState object.

Returns

a Randomizable instance.

Flipd

class Flipd(keys, spatial_axis=None)[source]

Dictionary-based wrapper of monai.transforms.transfroms.Flip.

See numpy.flip for additional details. https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html

Parameters
  • keys (dict) – Keys to pick data for transformation.

  • spatial_axis (None, int or tuple of ints) – Spatial axes along which to flip over. Default is None.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandFlipd

class RandFlipd(keys, prob=0.1, spatial_axis=None)[source]

Dictionary-based version monai.transforms.transfroms.RandFlip.

See numpy.flip for additional details. https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html

Parameters
  • prob (float) – Probability of flipping.

  • spatial_axis (None, int or tuple of ints) – Spatial axes along which to flip over. Default is None.

__call__(data)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

Rotated

class Rotated(keys, angle, spatial_axes=(0, 1), reshape=True, order=1, mode='constant', cval=0, prefilter=True)[source]

Dictionary-based wrapper of monai.transforms.transfroms.Rotate.

Parameters
  • keys (dict) – Keys to pick data for transformation.

  • angle (float) – Rotation angle in degrees.

  • spatial_axes (tuple of 2 ints) – Spatial axes of rotation. Default: (0, 1). This is the first two axis in spatial dimensions.

  • reshape (bool) – If reshape is true, the output shape is adapted so that the input array is contained completely in the output. Default is True.

  • order (int) – Order of spline interpolation. Range 0-5. Default: 1. This is different from scipy where default interpolation is 3.

  • mode (str) – Points outside boundary filled according to this mode. Options are ‘constant’, ‘nearest’, ‘reflect’, ‘wrap’. Default: ‘constant’.

  • cval (scalar) – Values to fill outside boundary. Default: 0.

  • prefilter (bool) – Apply spline_filter before interpolation. Default: True.

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandRotated

class RandRotated(keys, degrees, prob=0.1, spatial_axes=(0, 1), reshape=True, order=1, mode='constant', cval=0, prefilter=True)[source]

Dictionary-based version monai.transforms.transfroms.RandRotate Randomly rotates the input arrays.

Parameters
  • prob (float) – Probability of rotation.

  • degrees (tuple of float or float) – Range of rotation in degrees. If single number, angle is picked from (-degrees, degrees).

  • spatial_axes (tuple of 2 ints) – Spatial axes of rotation. Default: (0, 1). This is the first two axis in spatial dimensions.

  • reshape (bool) – If reshape is true, the output shape is adapted so that the input array is contained completely in the output. Default is True.

  • order (int) – Order of spline interpolation. Range 0-5. Default: 1. This is different from scipy where default interpolation is 3.

  • mode (str) – Points outside boundary filled according to this mode. Options are ‘constant’, ‘nearest’, ‘reflect’, ‘wrap’. Default: ‘constant’.

  • cval (scalar) – Value to fill outside boundary. Default: 0.

  • prefilter (bool) – Apply spline_filter before interpolation. Default: True.

__call__(data)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

Zoomd

class Zoomd(keys, zoom, order=3, mode='constant', cval=0, prefilter=True, use_gpu=False, keep_size=False)[source]

Dictionary-based wrapper of monai.transforms.transfroms.Zoom.

Parameters
  • zoom (float or sequence) – The zoom factor along the spatial axes. If a float, zoom is the same for each spatial axis. If a sequence, zoom should contain one value for each spatial axis.

  • order (int) – order of interpolation. Default=3.

  • mode (str) – Determines how input is extended beyond boundaries. Default is ‘constant’.

  • cval (scalar, optional) – Value to fill past edges. Default is 0.

  • use_gpu (bool) – Should use cpu or gpu. Uses cupyx which doesn’t support order > 1 and modes ‘wrap’ and ‘reflect’. Defaults to cpu for these cases or if cupyx not found.

  • keep_size (bool) – Should keep original size (pad if needed).

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

RandZoomd

class RandZoomd(keys, prob=0.1, min_zoom=0.9, max_zoom=1.1, order=3, mode='constant', cval=0, prefilter=True, use_gpu=False, keep_size=False)[source]

Dict-based version monai.transforms.transfroms.RandZoom.

Parameters
  • keys (dict) – Keys to pick data for transformation.

  • prob (float) – Probability of zooming.

  • min_zoom (float or sequence) – Min zoom factor. Can be float or sequence same size as image. If a float, min_zoom is the same for each spatial axis. If a sequence, min_zoom should contain one value for each spatial axis.

  • max_zoom (float or sequence) – Max zoom factor. Can be float or sequence same size as image. If a float, max_zoom is the same for each spatial axis. If a sequence, max_zoom should contain one value for each spatial axis.

  • order (int) – order of interpolation. Default=3.

  • mode ('reflect', 'constant', 'nearest', 'mirror', 'wrap') – Determines how input is extended beyond boundaries. Default: ‘constant’.

  • cval (scalar, optional) – Value to fill past edges. Default is 0.

  • use_gpu (bool) – Should use cpu or gpu. Uses cupyx which doesn’t support order > 1 and modes ‘wrap’ and ‘reflect’. Defaults to cpu for these cases or if cupyx not found.

  • keep_size (bool) – Should keep original size (pad if needed).

__call__(data)[source]

Call self as a function.

randomize()[source]

Within this method, self.R should be used, instead of np.random, to introduce random factors.

all self.R calls happen here so that we have a better chance to identify errors of sync the random state.

This method can optionally take additional arguments so that the random factors are generated based on properties of the input data.

DeleteKeysd

class DeleteKeysd(keys)[source]

Delete specified keys from data dictionary to release memory. It will remove the key-values and copy the others to construct a new dictionary.

Parameters

keys (hashable items) – keys of the corresponding items to be transformed. See also: monai.transforms.compose.MapTransform

__call__(data)[source]

data is an element which often comes from an iteration over an iterable, such as torch.utils.data.Dataset. This method should return an updated version of data. To simplify the input validations, most of the transforms assume that

  • data component is a “channel-first” array,

  • the channel dimension is not omitted even if number of channels is one.

This method can optionally take additional arguments to help execute transformation operation.

Transform Adaptors

How to use the adaptor function

The key to using ‘adaptor’ lies in understanding the function that want to adapt. The ‘inputs’ and ‘outputs’ parameters take either strings, lists/tuples of strings or a dictionary mapping strings, depending on call signature of the function being called.

The adaptor function is written to minimise the cognitive load on the caller. There should be a minimal number of cases where the caller has to set anything on the input parameter, and for functions that return a single value, it is only necessary to name the dictionary keyword to which that value is assigned.

Use of outputs

outputs can take either a string, a list/tuple of string or a dict of string to string, depending on what the transform being adapted returns:

  • If the transform returns a single argument, then outputs can be supplied a string that indicates what key to assign the return value to in the dictionary

  • If the transform returns a list/tuple of values, then outputs can be supplied a list/tuple of the same length. The strings in outputs map the return value at the corresponding position to a key in the dictionary

  • If the transform returns a dictionary of values, then outputs must be supplied a dictionary that maps keys in the function’s return dictionary to the dictionary being passed between functions

Note, the caller is free to use a more complex way of specifying the outputs parameter than is required. The following are synonymous and will be treated identically:

# single argument
adaptor(MyTransform(), 'image')
adaptor(MyTransform(), ['image'])
adaptor(MyTransform(), {'image': 'image'})

# multiple arguments
adaptor(MyTransform(), ['image', 'label'])
adaptor(MyTransform(), {'image': 'image', 'label': 'label'})

Use of inputs

inputs can usually be omitted when using adaptor. It is only required when a the function’s parameter names do not match the names in the dictionary that is used to chain transform calls.

class MyTransform1:
    ...
    def __call__(image):
        return '''do stuff to image'''

class MyTransform2:
    ...
    def __call__(img):
        return '''do stuff to image'''

d = {'image': i}

Compose([
    adaptor(MyTransform1(), 'image'),
    adaptor(MyTransform2(), 'image', {'img':'image'})
])

Inputs:

  • dictionary in: None | Name maps

  • params in (match): None | Name list | Name maps

  • params in (mismatch): Name maps

  • params & **kwargs (match) : None | Name maps

  • params & **kwargs (mismatch) : Name maps

Outputs:

  • dictionary out: None | Name maps

  • list/tuple out: list/tuple

  • variable out: string

adaptor

adaptors.adaptor(outputs, inputs=None)

apply_alias

adaptors.apply_alias(name_map)

to_kwargs

adaptors.to_kwargs()