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

__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.

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

Randomizable

class Randomizable[source]

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

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.

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.

Note

thread safety

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.

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.

Note

thread safety

Vanilla Transforms

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

Spacing

class Spacing(pixdim, keep_shape=False)[source]

Resample input image into the specified pixdim.

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

  • keep_shape (bool) – whether to maintain the original spatial shape after resampling. Defaults to False.

__call__(data_array, original_affine=None, original_pixdim=None, interp_order=1)[source]
Parameters
Returns

self.pixdim), original pixdim, current pixdim.

Return type

resampled array (in spacing

Orientation

class Orientation(axcodes, labels=None)[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.

  • labels – optional, None or sequence of (2,) sequences (2,) sequences are labels for (beginning, end) of output axis.

See Also: nibabel.orientations.ornt2axcodes.

__call__(data_array, original_affine=None, original_axcodes=None)[source]

if original_affine is provided, the orientation is computed from the affine.

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

  • original_affine (4x4 matrix) – original affine.

  • original_axcodes (N elements sequence) – for spatial ND input’s orientation.

Returns

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

LoadNifti

class LoadNifti(as_closest_canonical=False, image_only=False, dtype=None)[source]

Load Nifti format file from provided path.

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, other return image volume and header dict.

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

Note

The loaded image volume if image_only is True, or a tuple containing the volume and the Nifti header in dict format otherwise. header[‘original_affine’] stores the original affine loaded from filename_or_obj. header[‘affine’] stores the affine after the optional as_closest_canonical transform.

__call__(filename)[source]
Parameters

filename (str or file) – path to file or file-like object.

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 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 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]

Call self as a function.

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]

Call self as a function.

Transpose

class Transpose(indices)[source]

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

__call__(img)[source]

Call self as a function.

Rescale

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

Rescales the input image to the given value range.

__call__(img)[source]

Call self as a function.

GaussianNoise

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

Add gaussian noise to image.

Parameters
  • 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.

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(output_spatial_shape, 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
  • output_spatial_shape (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) – Wheter 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 true, output shape is made same as input. Default: 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.

  • prefiter (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, …, ]),

ToTensor

class ToTensor[source]

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

__call__(img)[source]

Call self as a function.

UniformRandomPatch

class UniformRandomPatch(patch_spatial_size)[source]

Selects a patch of the given size chosen at a uniformly random position in the image.

Parameters

patch_spatial_size (tuple or list) – Expected patch size of spatial dimensions.

__call__(img)[source]

Call self as a function.

randomize(image_shape, patch_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.

IntensityNormalizer

class IntensityNormalizer(subtrahend=None, divisor=None)[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). Current implementation can only support ‘channel_last’ format data.

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

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

__call__(img)[source]

Call self as a function.

ImageEndPadder

class ImageEndPadder(out_size, mode)[source]
Performs padding by appending to the end of the data 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
  • out_size (list) – the size of region of interest at the end of the operation.

  • mode (string) – a portion from numpy.lib.arraypad.pad is copied below.

__call__(img)[source]

Call self as a function.

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.

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]

Call self as a function.

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 true, output shape is made same as input. Default: 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.

  • prefiter (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.

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.

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.

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 affines 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’.

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 paramters 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.

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.

Note

thread safety

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 paramters 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.

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.

Note

thread safety

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 paramters 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.

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.

Note

thread safety

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, affine_key, pixdim, interp_order=2, keep_shape=False, output_key='spacing')[source]

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

Parameters
  • affine_key (hashable) – the key to the original affine. The affine will be used to compute input data’s pixdim.

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

  • 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.

  • keep_shape (bool) – whether to maintain the original spatial shape after resampling. Defaults to False.

  • output_key (hashable) – key to be added to the output dictionary to track the pixdim status.

__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.

Orientationd

class Orientationd(keys, affine_key, axcodes, labels=None, output_key='orientation')[source]

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

Parameters
  • affine_key (hashable) – the key to the original affine. The affine will be used to compute input data’s orientation.

  • 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.

  • labels – optional, None or sequence of (2,) sequences (2,) sequences are labels for (beginning, end) of output axis.

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.

LoadNiftid

class LoadNiftid(keys, as_closest_canonical=False, dtype=None, meta_key_format='{}.{}', overwriting_keys=False)[source]

dictionary-based wrapper of LoadNifti, must load image and metadata together.

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.

AsChannelFirstd

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

dictionary-based wrapper of 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.

AddChanneld

class AddChanneld(keys)[source]

dictionary-based wrapper of 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.

Rotate90d

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

dictionary-based wrapper of 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.

Rescaled

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

dictionary-based wrapper of Rescale.

__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.

Resized

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

dictionary-based wrapper of Resize.

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

  • output_spatial_shape (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) – Wheter 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.

UniformRandomPatchd

class UniformRandomPatchd(keys, patch_spatial_size)[source]

Selects a patch of the given size chosen at a uniformly random position in the image.

Parameters

patch_spatial_size (tuple or list) – Expected patch size of spatial dimensions.

__call__(data)[source]

Call self as a function.

randomize(image_shape, patch_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.

RandRotate90d

class RandRotate90d(keys, 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
  • 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.

RandCropByPosNegLabeld

class RandCropByPosNegLabeld(keys, label_key, size, pos=1, neg=1, num_samples=1)[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.

__call__(data)[source]

Call self as a function.

randomize(label)[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.

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]

A 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.

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.

Note

thread safety

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]

A 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 paramters 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.

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.

Note

thread safety

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]

A 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 paramters 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.

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.

Note

thread safety

Flipd

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

Dictionary-based wrapper of 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.

RandFlipd

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

Dict-based wrapper of 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.

Rotated

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

Dictionary-based wrapper of 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 true, output shape is made same as input. Default: 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.

  • prefiter (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.

RandRotated

class RandRotated(keys, 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 true, output shape is made same as input. Default: 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.

  • prefiter (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.

Zoomd

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

Dictionary-based wrapper of Zoom transform.

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.

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 wrapper of 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.

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.

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()