Transforms¶
Generic Interfaces¶
Transform¶

class
monai.transforms.
Transform
[source]¶ An abstract class of a
Transform
. A transform is callable that processesdata
.It could be stateful and may modify
data
in place, the implementation should be aware of:thread safety when mutating its own states. When used from a multiprocess context, transform’s instance variables are readonly.
data
content unused by this transform may still be used in the subsequent transforms in a composed transform.storing too much information in
data
may not scale.
See Also

abstract
__call__
(data)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadNifti and LoadPNG transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
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.
 Raises
NotImplementedError – When the subclass does not override this method.
MapTransform¶

class
monai.transforms.
MapTransform
(keys)[source]¶ A subclass of
monai.transforms.Transform
with an assumption that thedata
input ofself.__call__
is a MutableMapping such asdict
.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
 Raises
ValueError – When
keys
is an empty iterable.TypeError – When
keys
type is not inUnion[Hashable, Iterable[Hashable]]
.

abstract
__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
Randomizable¶

class
monai.transforms.
Randomizable
[source]¶ An interface for handling random state locally, currently based on a class variable R, which is an instance of np.random.RandomState. This is mainly for randomized data augmentation transforms. For example:
class RandShiftIntensity(Randomizable): def randomize(): self._offset = self.R.uniform(low=0, high=100) def __call__(self, img): self.randomize() return img + self._offset transform = RandShiftIntensity() transform.set_random_state(seed=0)

abstract
randomize
(data)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None

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 (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Returns
a Randomizable instance.

abstract
Compose¶

class
monai.transforms.
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:With a series of transforms that accept and return a single ndarray / tensor / tensorlike parameter.
With a series of transforms that accept and return a dictionary that contains one or more parameters. Such transforms must have passthrough 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:
transformA normalizes the intensity of ‘img’ field in the dict data.
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' } }, }]
transformC then randomly rotates or flips ‘img’ and ‘seg’ fields of every dictionary item in the list.
The composed transforms will be set the same global random seed if user called set_determinism().
When using the passthrough 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 preprocessing pipelines. Preprocessing is often not a strictly sequential series of operations, and much of the complexity arises when a notsequential 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 preprocessing functions taking into account that not all of them are called on the labels.

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None

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 (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Returns
a Randomizable instance.
Vanilla Transforms¶
Crop and Pad¶
SpatialPad¶

class
monai.transforms.
SpatialPad
(spatial_size, method=<Method.SYMMETRIC: 'symmetric'>, mode=<NumpyPadMode.CONSTANT: '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 (
Union
[Sequence
[int
],int
]) – the spatial size of output data after padding. If its components have nonpositive values, the corresponding size of input image will be used (no padding).method (
Union
[Method
,str
]) – {"symmetric"
,"end"
} Pad image symmetric on every side or only pad at the end sides. Defaults to"symmetric"
.mode (
Union
[NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html

__call__
(img, mode=None)[source]¶  Parameters
img (
ndarray
) – data to be transformed, assuming img is channelfirst and padding doesn’t apply to the channel dim.mode (
Union
[NumpyPadMode
,str
,None
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults toself.mode
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
 Return type
ndarray
BorderPad¶

class
monai.transforms.
BorderPad
(spatial_border, mode=<NumpyPadMode.CONSTANT: 'constant'>)[source]¶ Pad the input data by adding specified borders to every dimension.
 Parameters
spatial_border (
Union
[Sequence
[int
],int
]) –specified size for every spatial border. it can be 3 shapes:
single int number, pad all the borders with the same size.
length equals the length of image shape, pad every spatial dimension separately. for example, image shape(CHW) is [1, 4, 4], spatial_border is [2, 1], pad every border of H dim with 2, pad every border of W dim with 1, result shape is [1, 8, 6].
length equals 2 x (length of image shape), pad every border of every dimension separately. for example, image shape(CHW) is [1, 4, 4], spatial_border is [1, 2, 3, 4], pad top of H dim with 1, pad bottom of H dim with 2, pad left of W dim with 3, pad right of W dim with 4. the result shape is [1, 7, 11].
mode (
Union
[NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html

__call__
(img, mode=None)[source]¶  Parameters
img (
ndarray
) – data to be transformed, assuming img is channelfirst and padding doesn’t apply to the channel dim.mode (
Union
[NumpyPadMode
,str
,None
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults toself.mode
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
 Raises
ValueError – When
self.spatial_border
contains a nonnegative int.ValueError – When
self.spatial_border
length is not one of [1, len(spatial_shape), 2*len(spatial_shape)].
 Return type
ndarray
DivisiblePad¶

class
monai.transforms.
DivisiblePad
(k, mode=<NumpyPadMode.CONSTANT: 'constant'>)[source]¶ Pad the input data, so that the spatial sizes are divisible by k.
 Parameters
k (
Union
[Sequence
[int
],int
]) – the target k for each spatial dimension. if k is negative or 0, the original size is preserved. if k is an int, the same k be applied to all the input spatial dimensions.mode (
Union
[NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
See also
monai.transforms.SpatialPad

__call__
(img, mode=None)[source]¶  Parameters
img (
ndarray
) – data to be transformed, assuming img is channelfirst and padding doesn’t apply to the channel dim.mode (
Union
[NumpyPadMode
,str
,None
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults toself.mode
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
 Return type
ndarray
SpatialCrop¶

class
monai.transforms.
SpatialCrop
(roi_center=None, roi_size=None, roi_start=None, roi_end=None)[source]¶ General purpose cropper to produce subvolume region of interest (ROI). It can support to crop ND spatial (channelfirst) 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 subvolume 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 (
Optional
[Sequence
[int
]]) – voxel coordinates for center of the crop ROI.roi_size (
Optional
[Sequence
[int
]]) – size of the crop ROI.roi_start (
Optional
[Sequence
[int
]]) – voxel coordinates for start of the crop ROI.roi_end (
Optional
[Sequence
[int
]]) – voxel coordinates for end of the crop ROI.
CenterSpatialCrop¶

class
monai.transforms.
CenterSpatialCrop
(roi_size)[source]¶ Crop at the center of image with specified ROI size.
 Parameters
roi_size (
Union
[Sequence
[int
],int
]) – the spatial size of the crop region e.g. [224,224,128] If its components have nonpositive values, the corresponding size of input image will be used.
RandSpatialCrop¶

class
monai.transforms.
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.
 Parameters
roi_size (
Union
[Sequence
[int
],int
]) – if random_size is True, it specifies the minimum crop region. if random_size is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128] If its components have nonpositive values, the corresponding size of input image will be used.random_center (
bool
) – crop at random position as center or the image center.random_size (
bool
) – crop with random size or specific size ROI. The actual size is sampled from randint(roi_size, img_size).

__call__
(img)[source]¶ Apply the transform to img, assuming img is channelfirst and slicing doesn’t apply to the channel dim.
 Return type
ndarray

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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
RandSpatialCropSamples¶

class
monai.transforms.
RandSpatialCropSamples
(roi_size, num_samples, random_center=True, random_size=True)[source]¶ Crop image with random size or specific size ROI to generate a list of N samples. 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. It will return a list of cropped images.
 Parameters
roi_size (
Union
[Sequence
[int
],int
]) – 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]num_samples (
int
) – number of samples (crop regions) to take in the returned list.random_center (
bool
) – crop at random position as center or the image center.random_size (
bool
) – crop with random size or specific size ROI. The actual size is sampled from randint(roi_size, img_size).
 Raises
ValueError – When
num_samples
is nonpositive.

__call__
(img)[source]¶ Apply the transform to img, assuming img is channelfirst and cropping doesn’t change the channel dim.
 Return type
List
[ndarray
]

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
CropForeground¶

class
monai.transforms.
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 (
Union
[Iterable
[int
],int
,None
]) – if defined, select foreground 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.
RandCropByPosNegLabel¶

class
monai.transforms.
RandCropByPosNegLabel
(spatial_size, label=None, pos=1.0, neg=1.0, num_samples=1, image=None, image_threshold=0.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 arrays for all the cropped images. For example, crop two (3 x 3) arrays from (5 x 5) array with pos/neg=1:
[[[0, 0, 0, 0, 0], [0, 1, 2, 1, 0], [[0, 1, 2], [[2, 1, 0], [0, 1, 3, 0, 0], > [0, 1, 3], [3, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0]] [0, 0, 0]] [0, 0, 0, 0, 0]]]
 Parameters
spatial_size (
Union
[Sequence
[int
],int
]) – the spatial size of the crop region e.g. [224, 224, 128]. If its components have nonpositive values, the corresponding size of label will be used.label (
Optional
[ndarray
]) – the label image that is used for finding foreground/background, if None, must set at self.__call__. Nonzero indicates foreground, zero indicates background.pos (
float
) – used with neg together to calculate the ratiopos / (pos + neg)
for the probability to pick a foreground voxel as a center rather than a background voxel.neg (
float
) – used with pos together to calculate the ratiopos / (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 (
Optional
[ndarray
]) – optional image data to help select valid area, can be same as img or another image array. if not None, uselabel == 0 & image > image_threshold
to select the negative sample (background) center. So the crop center will only come from the valid image areas.image_threshold (
float
) – if enabled image, useimage > image_threshold
to determine the valid image content areas.
 Raises
ValueError – When
pos
orneg
are negative.ValueError – When
pos=0
andneg=0
. Incompatible values.

__call__
(img, label=None, image=None)[source]¶  Parameters
img (
ndarray
) – input data to crop samples from based on the pos/neg ratio of label and image. Assumes img is a channelfirst array.label (
Optional
[ndarray
]) – the label image that is used for finding foreground/background, if None, use self.label.image (
Optional
[ndarray
]) – optional image data to help select valid area, can be same as img or another image array. uselabel == 0 & image > image_threshold
to select the negative sample(background) center. so the crop center will only exist on valid image area. if None, use self.image.
 Return type
List
[ndarray
]

randomize
(label, image=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
Intensity¶
RandGaussianNoise¶

class
monai.transforms.
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 (
Union
[Sequence
[float
],float
]) – Mean or “centre” of the distribution.std (
float
) – Standard deviation (spread) of distribution.

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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
ShiftIntensity¶
RandShiftIntensity¶

class
monai.transforms.
RandShiftIntensity
(offsets, prob=0.1)[source]¶ Randomly shift intensity with randomly picked offset.
 Parameters
offsets (
Union
[Tuple
[float
,float
],float
]) – offset range to randomly shift. if single number, offset value is picked from (offsets, offsets).prob (
float
) – probability of shift.

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
ScaleIntensity¶

class
monai.transforms.
ScaleIntensity
(minv=0.0, maxv=1.0, factor=None)[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 (
Optional
[float
]) – minimum value of output data.maxv (
Optional
[float
]) – maximum value of output data.factor (
Optional
[float
]) – factor scale byv = v * (1 + factor)
.
RandScaleIntensity¶

class
monai.transforms.
RandScaleIntensity
(factors, prob=0.1)[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 (
Union
[Tuple
[float
,float
],float
]) – factor range to randomly scale byv = v * (1 + factor)
. if single number, factor value is picked from (factors, factors).prob (
float
) – probability of scale.

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
NormalizeIntensity¶

class
monai.transforms.
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 nonzero values or entire image, and can also calculate mean and std on each channel separately.
 Parameters
subtrahend (
Optional
[ndarray
]) – the amount to subtract by (usually the mean).divisor (
Optional
[ndarray
]) – the amount to divide by (usually the standard deviation).nonzero (
bool
) – whether only normalize nonzero values.channel_wise (
bool
) – if using calculated mean and std, calculate on each channel separately or calculate on the entire image directly.
ThresholdIntensity¶

class
monai.transforms.
ThresholdIntensity
(threshold, above=True, cval=0.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
) – the threshold to filter intensity values.above (
bool
) – filter values above the threshold or below the threshold, default is True.cval (
float
) – value to fill the remaining parts of the image, default is 0.
ScaleIntensityRange¶

class
monai.transforms.
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 (
float
) – intensity original range min.a_max (
float
) – intensity original range max.b_min (
float
) – intensity target range min.b_max (
float
) – intensity target range max.clip (
bool
) – whether to perform clip after scaling.
ScaleIntensityRangePercentiles¶

class
monai.transforms.
ScaleIntensityRangePercentiles
(lower, upper, b_min, b_max, clip=False, relative=False)[source]¶ Apply range scaling to a numpy array based on the intensity distribution of the input.
By default this transform will scale from [lower_intensity_percentile, upper_intensity_percentile] to [b_min, b_max], where {lower,upper}_intensity_percentile are the intensity values at the corresponding percentiles of
img
.The
relative
parameter can also be set to scale from [lower_intensity_percentile, upper_intensity_percentile] to the lower and upper percentiles of the output range [b_min, b_max]For example:
image = np.array( [[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]]) # Scale from lower and upper image intensity percentiles # to output range [b_min, b_max] scaler = ScaleIntensityRangePercentiles(10, 90, 0, 200, False, False) print(scaler(image)) [[[0., 50., 100., 150., 200.], [0., 50., 100., 150., 200.], [0., 50., 100., 150., 200.], [0., 50., 100., 150., 200.], [0., 50., 100., 150., 200.], [0., 50., 100., 150., 200.]]] # Scale from lower and upper image intensity percentiles # to lower and upper percentiles of the output range [b_min, b_max] rel_scaler = ScaleIntensityRangePercentiles(10, 90, 0, 200, False, True) print(rel_scaler(image)) [[[20., 60., 100., 140., 180.], [20., 60., 100., 140., 180.], [20., 60., 100., 140., 180.], [20., 60., 100., 140., 180.], [20., 60., 100., 140., 180.], [20., 60., 100., 140., 180.]]]
 Parameters
lower (
float
) – lower intensity percentile.upper (
float
) – upper intensity percentile.b_min (
float
) – intensity target range min.b_max (
float
) – intensity target range max.clip (
bool
) – whether to perform clip after scaling.relative (
bool
) – whether to scale to the corresponding percentiles of [b_min, b_max].
AdjustContrast¶
RandAdjustContrast¶

class
monai.transforms.
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 (
Union
[Sequence
[float
],float
]) – Range of gamma values. If single number, value is picked from (0.5, gamma), default is (0.5, 4.5).

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
MaskIntensity¶

class
monai.transforms.
MaskIntensity
(mask_data)[source]¶ Mask the intensity values of input image with the specified mask data. Mask data must have the same spatial size as the input image, and all the intensity values of input image corresponding to 0 in the mask data will be set to 0, others will keep the original value.
 Parameters
mask_data (
ndarray
) – if mask data is single channel, apply to evey channel of input image. if multiple channels, the channel number must match input data. mask_data will be converted to bool values by mask_data > 0 before applying transform to input image.

__call__
(img, mask_data=None)[source]¶  Parameters
mask_data (
Optional
[ndarray
]) – if mask data is single channel, apply to evey channel of input image. if multiple channels, the channel number must match input data. mask_data will be converted to bool values by mask_data > 0 before applying transform to input image. Raises
ValueError – When
mask_data
andimg
channels differ andmask_data
is not single channel. Return type
ndarray
GaussianSmooth¶

class
monai.transforms.
GaussianSmooth
(sigma=1.0)[source]¶ Apply Gaussian smooth to the input data based on specified sigma parameter. A default value sigma=1.0 is provided for reference.
 Parameters
sigma (
Union
[Sequence
[float
],float
]) – if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, use it for all spatial dimensions.

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadNifti and LoadPNG transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
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.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
ndarray
RandGaussianSmooth¶

class
monai.transforms.
RandGaussianSmooth
(sigma_x=(0.25, 1.5), sigma_y=(0.25, 1.5), sigma_z=(0.25, 1.5), prob=0.1)[source]¶ Apply Gaussian smooth to the input data based on randomly selected sigma parameters.
 Parameters
sigma_x (
Sequence
[float
]) – randomly select sigma value for the first spatial dimension.sigma_y (
Sequence
[float
]) – randomly select sigma value for the second spatial dimension if have.sigma_z (
Sequence
[float
]) – randomly select sigma value for the third spatial dimension if have.prob (
float
) – probability of Gaussian smooth.

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
GaussianSharpen¶

class
monai.transforms.
GaussianSharpen
(sigma1=3.0, sigma2=1.0, alpha=30.0)[source]¶ Sharpen images using the Gaussian Blur filter. Referring to: http://scipylectures.org/advanced/image_processing/auto_examples/plot_sharpen.html. The algorithm is shown as below
blurred_f = gaussian_filter(img, sigma1) filter_blurred_f = gaussian_filter(blurred_f, sigma2) img = blurred_f + alpha * (blurred_f  filter_blurred_f)
A set of default values sigma1=3.0, sigma2=1.0 and alpha=30.0 is provide for reference.
 Parameters
sigma1 (
Union
[Sequence
[float
],float
]) – sigma parameter for the first gaussian kernel. if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, use it for all spatial dimensions.sigma2 (
Union
[Sequence
[float
],float
]) – sigma parameter for the second gaussian kernel. if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, use it for all spatial dimensions.alpha (
float
) – weight parameter to compute the final result.

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadNifti and LoadPNG transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
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.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
ndarray
IO¶
LoadNifti¶

class
monai.transforms.
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 (
Optional
[dtype
]) – 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.
LoadPNG¶

class
monai.transforms.
LoadPNG
(image_only=False, dtype=<class 'numpy.float32'>)[source]¶ Load common 2D image format (PNG, JPG, etc. using PIL) 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. It’s based on the Image module in PIL library: https://pillow.readthedocs.io/en/stable/reference/Image.html
 Parameters
image_only (
bool
) – if True return only the image volume, otherwise return image data array and metadata.dtype (
Optional
[dtype
]) – if not None convert the loaded image to this data type.
LoadNumpy¶

class
monai.transforms.
LoadNumpy
(data_only=False, dtype=<class 'numpy.float32'>, npz_keys=None)[source]¶ Load arrays or pickled objects from .npy, .npz or pickled files, file or files are from provided path. A typical usage is to load the mask data for classification task. If loading a list of files or laoding npz file, stack results together and add a new dimension as first dimension, and use the meta data of the first file to represent the stacked result. It can load part of the npz file with specified npz_keys. It’s based on the Numpy load/read API: https://numpy.org/doc/stable/reference/generated/numpy.load.html
 Parameters
data_only (
bool
) – if True return only the data array, otherwise return data array and metadata.dtype (
Optional
[dtype
]) – if not None convert the loaded data to this data type.npz_keys (
Union
[Collection
[Hashable
],Hashable
,None
]) – if loading npz file, only load the specified keys, if None, load all the items. stack the loaded items together to construct a new first dimension.
Postprocessing¶
SplitChannel¶

class
monai.transforms.
SplitChannel
(to_onehot=False, num_classes=None)[source]¶ Split PyTorch Tensor data according to the channel dim, if only 1 channel, convert to OneHot format first based on the class number. Users can use this transform to compute metrics on every single class to get more details of validation/evaluation. Expected input shape:
(batch_size, num_channels, [spatial_dim_1, spatial_dim_2, ...])
 Parameters
to_onehot (
bool
) – whether to convert the data to OneHot format first. Defaults toFalse
.num_classes (
Optional
[int
]) – the class number used to convert to OneHot format if to_onehot is True. Defaults toNone
.

__call__
(img, to_onehot=None, num_classes=None)[source]¶  Parameters
to_onehot (
Optional
[bool
]) – whether to convert the data to OneHot format first. Defaults toself.to_onehot
.num_classes (
Optional
[int
]) – the class number used to convert to OneHot format if to_onehot is True. Defaults toself.num_classes
.
 Return type
List
[Tensor
]
Activations¶

class
monai.transforms.
Activations
(sigmoid=False, softmax=False, other=None)[source]¶ Add activation operations to the model output, typically Sigmoid or Softmax.
 Parameters
sigmoid (
bool
) – whether to execute sigmoid function on model output before transform. Defaults toFalse
.softmax (
bool
) – whether to execute softmax function on model output before transform. Defaults toFalse
.other (
Optional
[Callable
]) – callable function to execute other activation layers, for example: other = lambda x: torch.tanh(x). Defaults toNone
.
 Raises
TypeError – When
other
is not anOptional[Callable]
.

__call__
(img, sigmoid=None, softmax=None, other=None)[source]¶  Parameters
sigmoid (
Optional
[bool
]) – whether to execute sigmoid function on model output before transform. Defaults toself.sigmoid
.softmax (
Optional
[bool
]) – whether to execute softmax function on model output before transform. Defaults toself.softmax
.other (
Optional
[Callable
]) – callable function to execute other activation layers, for example: other = lambda x: torch.tanh(x). Defaults toself.other
.
 Raises
ValueError – When
sigmoid=True
andsoftmax=True
. Incompatible values.TypeError – When
other
is not anOptional[Callable]
.ValueError – When
self.other=None
andother=None
. Incompatible values.
 Return type
Tensor
AsDiscrete¶

class
monai.transforms.
AsDiscrete
(argmax=False, to_onehot=False, n_classes=None, threshold_values=False, logit_thresh=0.5)[source]¶ Execute after model forward to transform model output to discrete values. It can complete below operations:
execute argmax for input logits values.
threshold input value to 0.0 or 1.0.
convert input value to OneHot format
 Parameters
argmax (
bool
) – whether to execute argmax function on input data before transform. Defaults toFalse
.to_onehot (
bool
) – whether to convert input data into the onehot format. Defaults toFalse
.n_classes (
Optional
[int
]) – the number of classes to convert to OneHot format. Defaults toNone
.threshold_values (
bool
) – whether threshold the float value to int number 0 or 1. Defaults toFalse
.logit_thresh (
float
) – the threshold value for thresholding operation.. Defaults to0.5
.

__call__
(img, argmax=None, to_onehot=None, n_classes=None, threshold_values=None, logit_thresh=None)[source]¶  Parameters
argmax (
Optional
[bool
]) – whether to execute argmax function on input data before transform. Defaults toself.argmax
.to_onehot (
Optional
[bool
]) – whether to convert input data into the onehot format. Defaults toself.to_onehot
.n_classes (
Optional
[int
]) – the number of classes to convert to OneHot format. Defaults toself.n_classes
.threshold_values (
Optional
[bool
]) – whether threshold the float value to int number 0 or 1. Defaults toself.threshold_values
.logit_thresh (
Optional
[float
]) – the threshold value for thresholding operation.. Defaults toself.logit_thresh
.
 Return type
Tensor
KeepLargestConnectedComponent¶

class
monai.transforms.
KeepLargestConnectedComponent
(applied_labels, independent=True, connectivity=None)[source]¶ Keeps only the largest connected component in the image. This transform can be used as a postprocessing step to clean up oversegment areas in model output.
 The input is assumed to be a PyTorch Tensor:
With shape (batch_size, 1, spatial_dim1[, spatial_dim2, …]) and the values correspond to expected labels.
With shape (batch_size, C, spatial_dim1[, spatial_dim2, …]) and the values should be 0, 1 on each labels.
Note
For single channel data, 0 will be treated as background and the oversegment pixels will be set to 0. For onehot data, the oversegment pixels will be set to 0 in its channel.
For example: Use KeepLargestConnectedComponent with applied_labels=[1], connectivity=1:
[1, 0, 0] [0, 0, 0] [0, 1, 1] => [0, 1 ,1] [0, 1, 1] [0, 1, 1]
Use KeepLargestConnectedComponent with applied_labels[1, 2], independent=False, connectivity=1:
[0, 0, 1, 0 ,0] [0, 0, 1, 0 ,0] [0, 2, 1, 1 ,1] [0, 2, 1, 1 ,1] [1, 2, 1, 0 ,0] => [1, 2, 1, 0 ,0] [1, 2, 0, 1 ,0] [1, 2, 0, 0 ,0] [2, 2, 0, 0 ,2] [2, 2, 0, 0 ,0]
Use KeepLargestConnectedComponent with applied_labels[1, 2], independent=True, connectivity=1:
[0, 0, 1, 0 ,0] [0, 0, 1, 0 ,0] [0, 2, 1, 1 ,1] [0, 2, 1, 1 ,1] [1, 2, 1, 0 ,0] => [0, 2, 1, 0 ,0] [1, 2, 0, 1 ,0] [0, 2, 0, 0 ,0] [2, 2, 0, 0 ,2] [2, 2, 0, 0 ,0]
Use KeepLargestConnectedComponent with applied_labels[1, 2], independent=False, connectivity=2:
[0, 0, 1, 0 ,0] [0, 0, 1, 0 ,0] [0, 2, 1, 1 ,1] [0, 2, 1, 1 ,1] [1, 2, 1, 0 ,0] => [1, 2, 1, 0 ,0] [1, 2, 0, 1 ,0] [1, 2, 0, 1 ,0] [2, 2, 0, 0 ,2] [2, 2, 0, 0 ,2]
 Parameters
applied_labels (
Union
[Sequence
[int
],int
]) – Labels for applying the connected component on. If only one channel. The pixel whose value is not in this list will remain unchanged. If the data is in onehot format, this is used to determine what channels to apply.independent (
bool
) – consider several labels as a whole or independent, default is True. Example use case would be segment label 1 is liver and label 2 is liver tumor, in that case you want this “independent” to be specified as False.connectivity (
Optional
[int
]) – Maximum number of orthogonal hops to consider a pixel/voxel as a neighbor. Accepted values are ranging from 1 to input.ndim. IfNone
, a full connectivity ofinput.ndim
is used.
LabelToContour¶

class
monai.transforms.
LabelToContour
(kernel_type='Laplace')[source]¶ Return the contour of binary input images that only compose of 0 and 1, with Laplace kernel set as default for edge detection. Typical usage is to plot the edge of label or segmentation output.
 Parameters
kernel_type (
str
) – the method applied to do edge detection, default is “Laplace”. Raises
NotImplementedError – When
kernel_type
is not “Laplace”.

__call__
(img)[source]¶  Parameters
img (
Tensor
) – torch tensor data to extract the contour, with shape: [batch_size, channels, height, width[, depth]] Raises
ValueError – When
image
ndim is not one of [4, 5]. Returns
it’s the binary classification result of whether a pixel is edge or not.
in order to keep the original shape of mask image, we use padding as default.
the edge detection is just approximate because it defects inherent to Laplace kernel, ideally the edge should be thin enough, but now it has a thickness.
 Return type
A torch tensor with the same shape as img, note
MeanEnsemble¶

class
monai.transforms.
MeanEnsemble
(weights=None)[source]¶ Execute mean ensemble on the input data. The input data can be a list or tuple of PyTorch Tensor with shape: [B, C[, H, W, D]], Or a single PyTorch Tensor with shape: [E, B, C[, H, W, D]], the E dimension represents the output data from different models. Typcally, the input data is model output of segmentation task or classificaiton task. And it also can support to add weights for the input data.
 Parameters
weights (
Union
[Sequence
[float
],Tensor
,ndarray
,None
]) – can be a list or tuple of numbers for input data with shape: [E, B, C, H, W[, D]]. or a Numpy ndarray or a PyTorch Tensor data. the weights will be added to input data from highest dimension, for example: 1. if the weights only has 1 dimension, it will be added to the E dimension of input data. 2. if the weights has 3 dimensions, it will be added to E, B and C dimensions. it’s a typical practice to add weights for different classes: to ensemble 3 segmentation model outputs, every output has 4 channels(classes), so the input data shape can be: [3, B, 4, H, W, D]. and add different weights for different classes, so the weights shape can be: [3, 1, 4]. for example: weights = [[[1, 2, 3, 4]], [[4, 3, 2, 1]], [[1, 1, 1, 1]]].

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadNifti and LoadPNG transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
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.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Tensor
VoteEnsemble¶

class
monai.transforms.
VoteEnsemble
(num_classes=None)[source]¶ Execute vote ensemble on the input data. The input data can be a list or tuple of PyTorch Tensor with shape: [B[, C, H, W, D]], Or a single PyTorch Tensor with shape: [E, B[, C, H, W, D]], the E dimension represents the output data from different models. Typcally, the input data is model output of segmentation task or classificaiton task.
Note
This vote transform expects the input data is discrete values. It can be multiple channels data in OneHot format or single channel data. It will vote to select the most common data between items. The output data has the same shape as every item of the input data.
 Parameters
num_classes (
Optional
[int
]) – if the input is single channel data instead of OneHot, we can’t get class number from channel, need to explicitly specify the number of classes to vote.

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadNifti and LoadPNG transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
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.
 Raises
NotImplementedError – When the subclass does not override this method.
Spatial¶
Spacing¶

class
monai.transforms.
Spacing
(pixdim, diagonal=False, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.BORDER: 'border'>, align_corners=False, dtype=<class 'numpy.float64'>)[source]¶ Resample input image into the specified pixdim.
 Parameters
pixdim (
Union
[Sequence
[float
],float
]) – 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 (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"border"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplealign_corners (
bool
) – Geometrically, we consider the pixels of the input as squares rather than points. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampledtype (
Optional
[dtype
]) – data type for resampling computation. Defaults tonp.float64
for best precision. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
.

__call__
(data_array, affine=None, mode=None, padding_mode=None, align_corners=None, dtype=None)[source]¶  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.
mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplealign_corners (
Optional
[bool
]) – Geometrically, we consider the pixels of the input as squares rather than points. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampledtype (
Optional
[dtype
]) – data type for resampling computation. Defaults toself.dtype
. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
.
 Raises
ValueError – When
data_array
has no spatial dimensions.ValueError – When
pixdim
is nonpositive.
 Returns
data_array (resampled into self.pixdim), original pixdim, current pixdim.
Orientation¶

class
monai.transforms.
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 (
Optional
[str
]) – 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 (
bool
) – 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'))
.
 Raises
ValueError – When
axcodes=None
andas_closest_canonical=True
. Incompatible values.
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.
 Raises
ValueError – When
data_array
has no spatial dimensions.ValueError – When
axcodes
spatiality differs fromdata_array
.
 Returns
data_array (reoriented in self.axcodes), original axcodes, current axcodes.
RandRotate¶

class
monai.transforms.
RandRotate
(range_x=0.0, range_y=0.0, range_z=0.0, prob=0.1, keep_size=True, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.BORDER: 'border'>, align_corners=False, dtype=<class 'numpy.float64'>)[source]¶ Randomly rotate the input arrays.
 Parameters
range_x (
Union
[Tuple
[float
,float
],float
]) – Range of rotation angle in degrees in the plane defined by the first and second axes. If single number, angle is uniformly sampled from (range_x, range_x).range_y (
Union
[Tuple
[float
,float
],float
]) – Range of rotation angle in degrees in the plane defined by the first and third axes. If single number, angle is uniformly sampled from (range_y, range_y).range_z (
Union
[Tuple
[float
,float
],float
]) – Range of rotation angle in degrees in the plane defined by the second and third axes. If single number, angle is uniformly sampled from (range_z, range_z).prob (
float
) – Probability of rotation.keep_size (
bool
) – If it is False, the output shape is adapted so that the input array is contained completely in the output. If it is True, the output shape is the same as the input. Default is True.mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"border"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplealign_corners (
bool
) – Defaults to False. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampledtype (
Optional
[dtype
]) – data type for resampling computation. Defaults tonp.float64
for best precision. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
.

__call__
(img, mode=None, padding_mode=None, align_corners=None, dtype=None)[source]¶  Parameters
img (
ndarray
) – channel first array, must have shape 2D: (nchannels, H, W), or 3D: (nchannels, H, W, D).mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplealign_corners (
Optional
[bool
]) – Defaults toself.align_corners
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampledtype (
Optional
[dtype
]) – data type for resampling computation. Defaults toself.dtype
. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
.
 Return type
ndarray

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
RandFlip¶

class
monai.transforms.
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 (
Union
[Sequence
[int
],int
,None
]) – 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, …, ]), Return type
ndarray

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
RandZoom¶

class
monai.transforms.
RandZoom
(prob=0.1, min_zoom=0.9, max_zoom=1.1, mode=<InterpolateMode.AREA: 'area'>, align_corners=None, keep_size=True)[source]¶ Randomly zooms input arrays with given probability within given zoom range.
 Parameters
prob (
float
) – Probability of zooming.min_zoom (
Union
[Sequence
[float
],float
]) – Min zoom factor. Can be float or sequence same size as image. If a float, select a random factor from [min_zoom, max_zoom] then apply to all spatial dims to keep the original spatial shape ratio. If a sequence, min_zoom should contain one value for each spatial axis.max_zoom (
Union
[Sequence
[float
],float
]) – Max zoom factor. Can be float or sequence same size as image. If a float, select a random factor from [min_zoom, max_zoom] then apply to all spatial dims to keep the original spatial shape ratio. If a sequence, max_zoom should contain one value for each spatial axis.mode (
Union
[InterpolateMode
,str
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults to"area"
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatealign_corners (
Optional
[bool
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Default: None. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatekeep_size (
bool
) – Should keep original size (pad if needed), default is True.

__call__
(img, mode=None, align_corners=None)[source]¶  Parameters
img (
ndarray
) – channel first array, must have shape 2D: (nchannels, H, W), or 3D: (nchannels, H, W, D).mode (
Union
[InterpolateMode
,str
,None
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatealign_corners (
Optional
[bool
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Defaults toself.align_corners
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate
 Return type
ndarray

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
Affine¶

class
monai.transforms.
Affine
(rotate_params=None, shear_params=None, translate_params=None, scale_params=None, spatial_size=None, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.REFLECTION: 'reflection'>, 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 (
Union
[Sequence
[float
],float
,None
]) – a rotation angle in radians, a scalar for 2D image, a tuple of 3 floats for 3D. Defaults to no rotation.shear_params (
Union
[Sequence
[float
],float
,None
]) – a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing.translate_params (
Union
[Sequence
[float
],float
,None
]) – 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 (
Union
[Sequence
[float
],float
,None
]) – a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Defaults to no scaling.spatial_size (
Union
[Sequence
[int
],int
,None
]) – output image spatial size. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 1) will be adapted to (32, 64) if the second spatial dimension size of img is 64.mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"reflection"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampleas_tensor_output (
bool
) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.device (
Optional
[device
]) – device on which the tensor will be allocated.

__call__
(img, spatial_size=None, mode=None, padding_mode=None)[source]¶  Parameters
img (
Union
[ndarray
,Tensor
]) – shape must be (num_channels, H, W[, D]),spatial_size (
Union
[Sequence
[int
],int
,None
]) – output image spatial size. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. 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 (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
 Return type
Union
[ndarray
,Tensor
]
Resample¶

class
monai.transforms.
Resample
(mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.BORDER: 'border'>, 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
mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"border"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampleas_tensor_output (
bool
) – whether to return a torch tensor. Defaults to False.device (
Optional
[device
]) – device on which the tensor will be allocated.

__call__
(img, grid=None, mode=None, padding_mode=None)[source]¶  Parameters
img (
Union
[ndarray
,Tensor
]) – shape must be (num_channels, H, W[, D]).grid (
Union
[ndarray
,Tensor
,None
]) – shape must be (3, H, W) for 2D or (4, H, W, D) for 3D.mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
 Return type
Union
[ndarray
,Tensor
]
RandAffine¶

class
monai.transforms.
RandAffine
(prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, spatial_size=None, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.REFLECTION: 'reflection'>, 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.rotate_range (
Union
[Sequence
[float
],float
,None
]) – angle range in radians. rotate_range[0] with be used to generate the 1st rotation parameter from uniform[rotate_range[0], rotate_range[0]). Similarly, rotate_range[1] and rotate_range[2] are used in 3D affine for the range of 2nd and 3rd axes.shear_range (
Union
[Sequence
[float
],float
,None
]) – 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 Nth parameter.translate_range (
Union
[Sequence
[float
],float
,None
]) – 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 Nth parameter.scale_range (
Union
[Sequence
[float
],float
,None
]) – 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 Nth parameter.spatial_size (
Union
[Sequence
[float
],float
,None
]) – output image spatial size. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 1) will be adapted to (32, 64) if the second spatial dimension size of img is 64.mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"reflection"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampleas_tensor_output (
bool
) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.device (
Optional
[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, padding_mode=None)[source]¶  Parameters
img (
Union
[ndarray
,Tensor
]) – shape must be (num_channels, H, W[, D]),spatial_size (
Union
[Sequence
[int
],int
,None
]) – output image spatial size. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. 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 (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
 Return type
Union
[ndarray
,Tensor
]

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None

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 (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Returns
a Randomizable instance.
RandDeformGrid¶

class
monai.transforms.
RandDeformGrid
(spacing, magnitude_range, as_tensor_output=True, device=None)[source]¶ Generate random deformation grid.
 Parameters
spacing (
Union
[Sequence
[float
],float
]) – spacing of the grid in 2D or 3D. e.g., spacing=(1, 1) indicates pixelwise deformation in 2D, spacing=(1, 1, 1) indicates voxelwise deformation in 3D, spacing=(2, 2) indicates deformation field defined on every other pixel in 2D.magnitude_range (
Tuple
[float
,float
]) – 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 (
Optional
[device
]) – device to store the output grid data.

__call__
(spatial_size)[source]¶  Parameters
spatial_size (
Sequence
[int
]) – spatial size of the grid. Return type
Union
[ndarray
,Tensor
]

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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
AffineGrid¶

class
monai.transforms.
AffineGrid
(rotate_params=None, shear_params=None, translate_params=None, scale_params=None, as_tensor_output=True, device=None)[source]¶ Affine transforms on the coordinates.
 Parameters
rotate_range – angle range in radians. rotate_range[0] with be used to generate the 1st rotation parameter from uniform[rotate_range[0], rotate_range[0]). Similarly, rotate_range[1] and rotate_range[2] are used in 3D affine for the range of 2nd and 3rd axes.
shear_range – 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 Nth parameter.
translate_range – 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 Nth parameter.
scale_range – 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 Nth parameter.
as_tensor_output (
bool
) – whether to output tensor instead of numpy array. defaults to True.device (
Optional
[device
]) – device to store the output grid data.

__call__
(spatial_size=None, grid=None)[source]¶  Parameters
spatial_size (
Optional
[Sequence
[int
]]) – output grid size.grid (
Union
[ndarray
,Tensor
,None
]) – grid to be transformed. Shape must be (3, H, W) for 2D or (4, H, W, D) for 3D.
 Raises
ValueError – When
grid=None
andspatial_size=None
. Incompatible values. Return type
Union
[ndarray
,Tensor
]
RandAffineGrid¶

class
monai.transforms.
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 (
Union
[Sequence
[float
],float
,None
]) – angle range in radians. rotate_range[0] with be used to generate the 1st rotation parameter from uniform[rotate_range[0], rotate_range[0]). Similarly, rotate_range[1] and rotate_range[2] are used in 3D affine for the range of 2nd and 3rd axes.shear_range (
Union
[Sequence
[float
],float
,None
]) – 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 Nth parameter.translate_range (
Union
[Sequence
[float
],float
,None
]) – 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 Nth parameter.scale_range (
Union
[Sequence
[float
],float
,None
]) – 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 Nth parameter.as_tensor_output (
bool
) – whether to output tensor instead of numpy array. defaults to True.device (
Optional
[device
]) – device to store the output grid data.
See also

__call__
(spatial_size=None, grid=None)[source]¶  Parameters
spatial_size (
Optional
[Sequence
[int
]]) – output grid size.grid (
Union
[ndarray
,Tensor
,None
]) – grid to be transformed. Shape must be (3, H, W) for 2D or (4, H, W, D) for 3D.
 Return type
Union
[ndarray
,Tensor
] Returns
a 2D (3xHxW) or 3D (4xHxWxD) grid.

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
Rand2DElastic¶

class
monai.transforms.
Rand2DElastic
(spacing, magnitude_range, prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, spatial_size=None, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.REFLECTION: 'reflection'>, as_tensor_output=False, device=None)[source]¶ Random elastic deformation and affine in 2D
 Parameters
spacing (
Union
[Tuple
[float
,float
],float
]) – distance in between the control points.magnitude_range (
Tuple
[float
,float
]) – the random offsets will be generated fromuniform[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 aspatial_size
centered area extracted from the input image.rotate_range (
Union
[Sequence
[float
],float
,None
]) – angle range in radians. rotate_range[0] with be used to generate the 1st rotation parameter from uniform[rotate_range[0], rotate_range[0]).shear_range (
Union
[Sequence
[float
],float
,None
]) – shear_range[0] with be used to generate the 1st shearing parameter from uniform[shear_range[0], shear_range[0]). Similarly, shear_range[1] controls the range of the uniform distribution used to generate the 2nd parameter.translate_range (
Union
[Sequence
[float
],float
,None
]) – translate_range[0] with be used to generate the 1st shift parameter from uniform[translate_range[0], translate_range[0]). Similarly, translate_range[1] controls the range of the uniform distribution used to generate the 2nd parameter.scale_range (
Union
[Sequence
[float
],float
,None
]) – 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] controls the range of the uniform distribution used to generate the 2nd parameter.spatial_size (
Union
[Sequence
[int
],int
,None
]) – specifying output image spatial size [h, w]. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 1) will be adapted to (32, 64) if the second spatial dimension size of img is 64.mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"reflection"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampleas_tensor_output (
bool
) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.device (
Optional
[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, padding_mode=None)[source]¶  Parameters
img (
Union
[ndarray
,Tensor
]) – shape must be (num_channels, H, W),spatial_size (
Union
[int
,Tuple
[int
,int
],None
]) – specifying output image spatial size [h, w]. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img.mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
 Return type
Union
[ndarray
,Tensor
]

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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None

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 (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Returns
a Randomizable instance.
Rand3DElastic¶

class
monai.transforms.
Rand3DElastic
(sigma_range, magnitude_range, prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, spatial_size=None, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.REFLECTION: 'reflection'>, as_tensor_output=False, device=None)[source]¶ Random elastic deformation and affine in 3D
 Parameters
sigma_range (
Tuple
[float
,float
]) – a Gaussian kernel with standard deviation sampled fromuniform[sigma_range[0], sigma_range[1])
will be used to smooth the random offset grid.magnitude_range (
Tuple
[float
,float
]) – the random offsets on the grid will be generated fromuniform[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 aspatial_size
centered area extracted from the input image.rotate_range (
Union
[Sequence
[float
],float
,None
]) – angle range in radians. rotate_range[0] with be used to generate the 1st rotation parameter from uniform[rotate_range[0], rotate_range[0]). Similarly, rotate_range[1] and rotate_range[2] are used in 3D affine for the range of 2nd and 3rd axes.shear_range (
Union
[Sequence
[float
],float
,None
]) – shear_range[0] with be used to generate the 1st shearing parameter from uniform[shear_range[0], shear_range[0]). Similarly, shear_range[1] and shear_range[2] controls the range of the uniform distribution used to generate the 2nd and 3rd parameters.translate_range (
Union
[Sequence
[float
],float
,None
]) – translate_range[0] with be used to generate the 1st shift parameter from uniform[translate_range[0], translate_range[0]). Similarly, translate_range[1] and translate_range[2] controls the range of the uniform distribution used to generate the 2nd and 3rd parameters.scale_range (
Union
[Sequence
[float
],float
,None
]) – 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] and scale_range[2] controls the range of the uniform distribution used to generate the 2nd and 3rd parameters.spatial_size (
Union
[Sequence
[int
],int
,None
]) – specifying output image spatial size [h, w, d]. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 32, 1) will be adapted to (32, 32, 64) if the third spatial dimension size of img is 64.mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"reflection"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampleas_tensor_output (
bool
) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.device (
Optional
[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, padding_mode=None)[source]¶  Parameters
img (
Union
[ndarray
,Tensor
]) – shape must be (num_channels, H, W, D),spatial_size (
Union
[int
,Tuple
[int
,int
,int
],None
]) – specifying spatial 3D output image spatial size [h, w, d]. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img.mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
 Return type
Union
[ndarray
,Tensor
]

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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None

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 (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Returns
a Randomizable instance.
Rotate90¶

class
monai.transforms.
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 (
Tuple
[int
,int
]) – 2 int numbers, defines the plane to rotate with 2 spatial axes. Default: (0, 1), this is the first two axis in spatial dimensions.
RandRotate90¶

class
monai.transforms.
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 (
Tuple
[int
,int
]) – 2 int numbers, 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, …, ]), Return type
ndarray

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
Flip¶

class
monai.transforms.
Flip
(spatial_axis)[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 (
Union
[Sequence
[int
],int
,None
]) – spatial axes along which to flip over. Default is None.
Resize¶

class
monai.transforms.
Resize
(spatial_size, mode=<InterpolateMode.AREA: 'area'>, align_corners=None)[source]¶ Resize the input image to given spatial size. Implemented using
torch.nn.functional.interpolate
. Parameters
spatial_size (
Union
[Sequence
[int
],int
]) – expected shape of spatial dimensions after resize operation. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 1) will be adapted to (32, 64) if the second spatial dimension size of img is 64.mode (
Union
[InterpolateMode
,str
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults to"area"
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatealign_corners (
Optional
[bool
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Default: None. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate

__call__
(img, mode=None, align_corners=None)[source]¶  Parameters
img (
ndarray
) – channel first array, must have shape: (num_channels, H[, W, …, ]).mode (
Union
[InterpolateMode
,str
,None
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatealign_corners (
Optional
[bool
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Defaults toself.align_corners
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate
 Raises
ValueError – When
self.spatial_size
length is less thanimg
spatial dimensions. Return type
ndarray
Rotate¶

class
monai.transforms.
Rotate
(angle, keep_size=True, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.BORDER: 'border'>, align_corners=False, dtype=<class 'numpy.float64'>)[source]¶ Rotates an input image by given angle using
monai.networks.layers.AffineTransform
. Parameters
angle (
Union
[Sequence
[float
],float
]) – Rotation angle(s) in degrees. should a float for 2D, three floats for 3D.keep_size (
bool
) – If it is True, the output shape is kept the same as the input. If it is False, the output shape is adapted so that the input array is contained completely in the output. Default is True.mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"border"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplealign_corners (
bool
) – Defaults to False. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampledtype (
Optional
[dtype
]) – data type for resampling computation. Defaults tonp.float64
for best precision. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
.

__call__
(img, mode=None, padding_mode=None, align_corners=None, dtype=None)[source]¶  Parameters
img (
ndarray
) – channel first array, must have shape: [chns, H, W] or [chns, H, W, D].mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample align_corners: Defaults toself.align_corners
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplealign_corners (
Optional
[bool
]) – Defaults toself.align_corners
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampledtype (
Optional
[dtype
]) – data type for resampling computation. Defaults toself.dtype
. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
.
 Raises
ValueError – When
img
spatially is not one of [2D, 3D]. Return type
ndarray
Zoom¶

class
monai.transforms.
Zoom
(zoom, mode=<InterpolateMode.AREA: 'area'>, align_corners=None, keep_size=True)[source]¶ Zooms an ND image using
torch.nn.functional.interpolate
. For details, please see https://pytorch.org/docs/stable/nn.functional.html#interpolate.Different from
monai.transforms.resize
, this transform takes scaling factors as input, and provides an option of preserving the input spatial size. Parameters
zoom (
Union
[Sequence
[float
],float
]) – 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.mode (
Union
[InterpolateMode
,str
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults to"area"
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatealign_corners (
Optional
[bool
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Default: None. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatekeep_size (
bool
) – Should keep original size (padding/slicing if needed), default is True.

__call__
(img, mode=None, align_corners=None)[source]¶  Parameters
img (
ndarray
) – channel first array, must have shape: (num_channels, H[, W, …, ]).mode (
Union
[InterpolateMode
,str
,None
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatealign_corners (
Optional
[bool
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Defaults toself.align_corners
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate
 Return type
ndarray
Utility¶
Identity¶
AsChannelFirst¶

class
monai.transforms.
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 channelfirst format, which has the shape (num_channels, spatial_dim_1[, spatial_dim_2, …]).This transform could be used to convert, for example, a channellast image array in shape (spatial_dim_1[, spatial_dim_2, …], num_channels) into the channelfirst 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.
AsChannelLast¶

class
monai.transforms.
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 channellast format with shape (spatial_dim_1[, spatial_dim_2, …], num_channels).
This transform could be used to convert, for example, a channelfirst image array in shape (num_channels, spatial_dim_1[, spatial_dim_2, …]) into the channellast 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.
AddChannel¶

class
monai.transforms.
AddChannel
[source]¶ Adds a 1length channel dimension to the input image.
Most of the image transformations in
monai.transforms
assumes the input image is in the channelfirst 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 channelfirst format so that the multidimensional image array can be correctly interpreted by the other transforms.
RepeatChannel¶

class
monai.transforms.
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.
CastToType¶

class
monai.transforms.
CastToType
(dtype=<class 'numpy.float32'>)[source]¶ Cast the Numpy data to specified numpy data type, or cast the PyTorch Tensor to specified PyTorch data type.
 Parameters
dtype (
Union
[dtype
,dtype
]) – convert image to this data type, default is np.float32.

__call__
(img, dtype=None)[source]¶ Apply the transform to img, assuming img is a numpy array or PyTorch Tensor.
 Parameters
dtype (
Union
[dtype
,dtype
,None
]) – convert image to this data type, default is self.dtype. Raises
TypeError – When
img
type is not inUnion[numpy.ndarray, torch.Tensor]
. Return type
Union
[ndarray
,Tensor
]
ToTensor¶
ToNumpy¶
Transpose¶
SqueezeDim¶
DataStats¶

class
monai.transforms.
DataStats
(prefix='Data', data_shape=True, value_range=True, data_value=False, additional_info=None, logger_handler=None)[source]¶ Utility transform to show the statistics of data for debug or analysis. It can be inserted into any place of a transform chain and check results of previous transforms. It support both numpy.ndarray and torch.tensor as input data, so it can be used in preprocessing and postprocessing.
 Parameters
prefix (
str
) – will be printed in format: “{prefix} statistics”.data_shape (
bool
) – whether to show the shape of input data.value_range (
bool
) – whether to show the value range of input data.data_value (
bool
) – whether to show the raw value of input data. a typical example is to print some properties of Nifti image: affine, pixdim, etc.additional_info (
Optional
[Callable
]) – user can define callable function to extract additional info from input data.logger_handler (
Optional
[Handler
]) – add additional handler to output data: save to file, etc. add existing python logging handlers: https://docs.python.org/3/library/logging.handlers.html
 Raises
TypeError – When
additional_info
is not anOptional[Callable]
.
SimulateDelay¶

class
monai.transforms.
SimulateDelay
(delay_time=0.0)[source]¶ This is a pass through transform to be used for testing purposes. It allows adding fake behaviors that are useful for testing purposes to simulate how large datasets behave without needing to test on large data sets.
For example, simulating slow NFS data transfers, or slow network transfers in testing by adding explicit timing delays. Testing of small test data can lead to incomplete understanding of real world issues, and may lead to suboptimal design choices.
 Parameters
delay_time (
float
) – The minimum amount of time, in fractions of seconds, to accomplish this delay task.
Lambda¶

class
monai.transforms.
Lambda
(func=None)[source]¶ Apply a userdefined lambda as a transform.
For example:
image = np.ones((10, 2, 2)) lambd = Lambda(func=lambda x: x[:4, :, :]) print(lambd(image).shape) (4, 2, 2)
 Parameters
func (
Optional
[Callable
]) – Lambda/function to be applied. Raises
TypeError – When
func
is not anOptional[Callable]
.
LabelToMask¶

class
monai.transforms.
LabelToMask
(select_labels, merge_channels=False)[source]¶ Convert labels to mask for other tasks. A typical usage is to convert segmentation labels to mask data to preprocess images and then feed the images into classification network. It can support single channel labels or OneHot labels with specified select_labels. For example, users can select label value = [2, 3] to construct mask data, or select the second and the third channels of labels to construct mask data. The output mask data can be a multiple channels binary data or a single channel binary data that merges all the channels.
 Parameters
select_labels (
Union
[Sequence
[int
],int
]) – labels to generate mask from. for 1 channel label, the select_labels is the expected label values, like: [1, 2, 3]. for OneHot format label, the select_labels is the expected channel indexes.merge_channels (
bool
) – whether to use np.any() to merge the result on channel dim. if yes, will return a single channel mask with binary data.

__call__
(img, select_labels=None, merge_channels=None)[source]¶  Parameters
select_labels (
Union
[Sequence
[int
],int
,None
]) – labels to generate mask from. for 1 channel label, the select_labels is the expected label values, like: [1, 2, 3]. for OneHot format label, the select_labels is the expected channel indexes.merge_channels (
Optional
[bool
]) – whether to use np.any() to merge the result on channel dim. if yes, will return a single channel mask with binary data.
 Return type
ndarray
Dictionary Transforms¶
Crop and Pad (Dict)¶
SpatialPadd¶

class
monai.transforms.
SpatialPadd
(keys, spatial_size, method=<Method.SYMMETRIC: 'symmetric'>, mode=<NumpyPadMode.CONSTANT: 'constant'>)[source]¶ Dictionarybased wrapper of
monai.transforms.SpatialPad
. Performs padding to the data, symmetric for all sides or all on one side for each dimension. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
spatial_size (
Union
[Sequence
[int
],int
]) – the spatial size of output data after padding. If its components have nonpositive values, the corresponding size of input image will be used.method (
Union
[Method
,str
]) – {"symmetric"
,"end"
} Pad image symmetric on every side or only pad at the end sides. Defaults to"symmetric"
.mode (
Union
[Sequence
[Union
[NumpyPadMode
,str
]],NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html It also can be a sequence of string, each element corresponds to a key inkeys
.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
BorderPadd¶

class
monai.transforms.
BorderPadd
(keys, spatial_border, mode=<NumpyPadMode.CONSTANT: 'constant'>)[source]¶ Pad the input data by adding specified borders to every dimension. Dictionarybased wrapper of
monai.transforms.BorderPad
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
spatial_border (
Union
[Sequence
[int
],int
]) –specified size for every spatial border. it can be 3 shapes:
single int number, pad all the borders with the same size.
length equals the length of image shape, pad every spatial dimension separately. for example, image shape(CHW) is [1, 4, 4], spatial_border is [2, 1], pad every border of H dim with 2, pad every border of W dim with 1, result shape is [1, 8, 6].
length equals 2 x (length of image shape), pad every border of every dimension separately. for example, image shape(CHW) is [1, 4, 4], spatial_border is [1, 2, 3, 4], pad top of H dim with 1, pad bottom of H dim with 2, pad left of W dim with 3, pad right of W dim with 4. the result shape is [1, 7, 11].
mode (
Union
[Sequence
[Union
[NumpyPadMode
,str
]],NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html It also can be a sequence of string, each element corresponds to a key inkeys
.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
DivisiblePadd¶

class
monai.transforms.
DivisiblePadd
(keys, k, mode=<NumpyPadMode.CONSTANT: 'constant'>)[source]¶ Pad the input data, so that the spatial sizes are divisible by k. Dictionarybased wrapper of
monai.transforms.DivisiblePad
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
k (
Union
[Sequence
[int
],int
]) – the target k for each spatial dimension. if k is negative or 0, the original size is preserved. if k is an int, the same k be applied to all the input spatial dimensions.mode (
Union
[Sequence
[Union
[NumpyPadMode
,str
]],NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html It also can be a sequence of string, each element corresponds to a key inkeys
.
See also
monai.transforms.SpatialPad

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
SpatialCropd¶

class
monai.transforms.
SpatialCropd
(keys, roi_center=None, roi_size=None, roi_start=None, roi_end=None)[source]¶ Dictionarybased wrapper of
monai.transforms.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 (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
roi_center (
Optional
[Sequence
[int
]]) – voxel coordinates for center of the crop ROI.roi_size (
Optional
[Sequence
[int
]]) – size of the crop ROI.roi_start (
Optional
[Sequence
[int
]]) – voxel coordinates for start of the crop ROI.roi_end (
Optional
[Sequence
[int
]]) – voxel coordinates for end of the crop ROI.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
CenterSpatialCropd¶

class
monai.transforms.
CenterSpatialCropd
(keys, roi_size)[source]¶ Dictionarybased wrapper of
monai.transforms.CenterSpatialCrop
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformroi_size (
Union
[Sequence
[int
],int
]) – the size of the crop region e.g. [224,224,128] If its components have nonpositive values, the corresponding size of input image will be used.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
RandSpatialCropd¶

class
monai.transforms.
RandSpatialCropd
(keys, roi_size, random_center=True, random_size=True)[source]¶ Dictionarybased version
monai.transforms.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 (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformroi_size (
Union
[Sequence
[int
],int
]) – if random_size is True, it specifies the minimum crop region. if random_size is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128] If its components have nonpositive values, the corresponding size of input image will be used.random_center (
bool
) – crop at random position as center or the image center.random_size (
bool
) – crop with random size or specific size ROI. The actual size is sampled from randint(roi_size, img_size).

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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
RandSpatialCropSamplesd¶

class
monai.transforms.
RandSpatialCropSamplesd
(keys, roi_size, num_samples, random_center=True, random_size=True)[source]¶ Dictionarybased version
monai.transforms.RandSpatialCropSamples
. Crop image with random size or specific size ROI to generate a list of N samples. 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. It will return a list of dictionaries for all the cropped images. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformroi_size (
Union
[Sequence
[int
],int
]) – 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]num_samples (
int
) – number of samples (crop regions) to take in the returned list.random_center (
bool
) – crop at random position as center or the image center.random_size (
bool
) – crop with random size or specific size ROI. The actual size is sampled from randint(roi_size, img_size).
 Raises
ValueError – When
num_samples
is nonpositive.

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
CropForegroundd¶

class
monai.transforms.
CropForegroundd
(keys, source_key, select_fn=<function CropForegroundd.<lambda>>, channel_indexes=None, margin=0)[source]¶ Dictionarybased version
monai.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 OneHot 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 (
Union
[Collection
[Hashable
],Hashable
]) – 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 (
Union
[Iterable
[int
],int
,None
]) – if defined, select foreground 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
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
RandCropByPosNegLabeld¶

class
monai.transforms.
RandCropByPosNegLabeld
(keys, label_key, spatial_size, pos=1.0, neg=1.0, num_samples=1, image_key=None, image_threshold=0.0)[source]¶ Dictionarybased version
monai.transforms.RandCropByPosNegLabel
. 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 (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
label_key (
str
) – name of key for label image, this will be used for finding foreground/background.spatial_size (
Union
[Sequence
[int
],int
]) – the spatial size of the crop region e.g. [224, 224, 128]. If its components have nonpositive values, the corresponding size of data[label_key] will be used.pos (
float
) – used with neg together to calculate the ratiopos / (pos + neg)
for the probability to pick a foreground voxel as a center rather than a background voxel.neg (
float
) – used with pos together to calculate the ratiopos / (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 (
Optional
[str
]) – if image_key is not None, uselabel == 0 & image > image_threshold
to select the negative sample(background) center. so the crop center will only exist on valid image area.image_threshold (
float
) – if enabled image_key, useimage > image_threshold
to determine the valid image content area.
 Raises
ValueError – When
pos
orneg
are negative.ValueError – When
pos=0
andneg=0
. Incompatible values.

randomize
(label, image=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
Instensity (Dict)¶
RandGaussianNoised¶

class
monai.transforms.
RandGaussianNoised
(keys, prob=0.1, mean=0.0, std=0.1)[source]¶ Dictionarybased version
monai.transforms.RandGaussianNoise
. Add Gaussian noise to image. This transform assumes all the expected fields have same shape. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
prob (
float
) – Probability to add Gaussian noise.mean (
Union
[Sequence
[float
],float
]) – Mean or “centre” of the distribution.std (
float
) – Standard deviation (spread) of distribution.

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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
ShiftIntensityd¶

class
monai.transforms.
ShiftIntensityd
(keys, offset)[source]¶ Dictionarybased wrapper of
monai.transforms.ShiftIntensity
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
offset (
float
) – offset value to shift the intensity of image.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
RandShiftIntensityd¶

class
monai.transforms.
RandShiftIntensityd
(keys, offsets, prob=0.1)[source]¶ Dictionarybased version
monai.transforms.RandShiftIntensity
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
offsets (
Union
[Tuple
[float
,float
],float
]) – 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.)

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
ScaleIntensityd¶

class
monai.transforms.
ScaleIntensityd
(keys, minv=0.0, maxv=1.0, factor=None)[source]¶ Dictionarybased wrapper of
monai.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 byv = v * (1 + factor)
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
minv (
float
) – minimum value of output data.maxv (
float
) – maximum value of output data.factor (
Optional
[float
]) – factor scale byv = v * (1 + factor)
.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
RandScaleIntensityd¶

class
monai.transforms.
RandScaleIntensityd
(keys, factors, prob=0.1)[source]¶ Dictionarybased version
monai.transforms.RandScaleIntensity
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
factors (
Union
[Tuple
[float
,float
],float
]) – factor range to randomly scale byv = 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.)

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
NormalizeIntensityd¶

class
monai.transforms.
NormalizeIntensityd
(keys, subtrahend=None, divisor=None, nonzero=False, channel_wise=False)[source]¶ Dictionarybased wrapper of
monai.transforms.NormalizeIntensity
. This transform can normalize only nonzero values or entire image, and can also calculate mean and std on each channel separately. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformsubtrahend (
Optional
[ndarray
]) – the amount to subtract by (usually the mean)divisor (
Optional
[ndarray
]) – the amount to divide by (usually the standard deviation)nonzero (
bool
) – whether only normalize nonzero 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
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
ThresholdIntensityd¶

class
monai.transforms.
ThresholdIntensityd
(keys, threshold, above=True, cval=0.0)[source]¶ Dictionarybased wrapper of
monai.transforms.ThresholdIntensity
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformthreshold (
float
) – the threshold to filter intensity values.above (
bool
) – filter values above the threshold or below the threshold, default is True.cval (
float
) – value to fill the remaining parts of the image, default is 0.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
ScaleIntensityRanged¶

class
monai.transforms.
ScaleIntensityRanged
(keys, a_min, a_max, b_min, b_max, clip=False)[source]¶ Dictionarybased wrapper of
monai.transforms.ScaleIntensityRange
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransforma_min (
float
) – intensity original range min.a_max (
float
) – intensity original range max.b_min (
float
) – intensity target range min.b_max (
float
) – intensity target range max.clip (
bool
) – whether to perform clip after scaling.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
ScaleIntensityRangePercentilesd¶

class
monai.transforms.
ScaleIntensityRangePercentilesd
(keys, lower, upper, b_min, b_max, clip=False, relative=False)[source]¶ Dictionarybased wrapper of
monai.transforms.ScaleIntensityRangePercentiles
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformlower (
float
) – lower percentile.upper (
float
) – upper percentile.b_min (
float
) – intensity target range min.b_max (
float
) – intensity target range max.clip (
bool
) – whether to perform clip after scaling.relative (
bool
) – whether to scale to the corresponding percentiles of [b_min, b_max]

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
AdjustContrastd¶

class
monai.transforms.
AdjustContrastd
(keys, gamma)[source]¶ Dictionarybased wrapper of
monai.transforms.AdjustContrast
. Changes image intensity by gamma. Each pixel/voxel intensity is updated as:x = ((x  min) / intensity_range) ^ gamma * intensity_range + min
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformgamma (
float
) – gamma value to adjust the contrast as function.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
RandAdjustContrastd¶

class
monai.transforms.
RandAdjustContrastd
(keys, prob=0.1, gamma=(0.5, 4.5))[source]¶ Dictionarybased version
monai.transforms.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 (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformprob (
float
) – Probability of adjustment.gamma (
Union
[Tuple
[float
,float
],float
]) – Range of gamma values. If single number, value is picked from (0.5, gamma), default is (0.5, 4.5).

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
MaskIntensityd¶

class
monai.transforms.
MaskIntensityd
(keys, mask_data)[source]¶ Dictionarybased wrapper of
monai.transforms.MaskIntensity
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
mask_data (
ndarray
) – if mask data is single channel, apply to evey channel of input image. if multiple channels, the channel number must match input data. mask_data will be converted to bool values by mask_data > 0 before applying transform to input image.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
GaussianSmoothd¶

class
monai.transforms.
GaussianSmoothd
(keys, sigma)[source]¶ Dictionarybased wrapper of
monai.transforms.GaussianSmooth
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
sigma (
Union
[Sequence
[float
],float
]) – if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, use it for all spatial dimensions.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
RandGaussianSmoothd¶

class
monai.transforms.
RandGaussianSmoothd
(keys, sigma_x=(0.25, 1.5), sigma_y=(0.25, 1.5), sigma_z=(0.25, 1.5), prob=0.1)[source]¶ Dictionarybased wrapper of
monai.transforms.GaussianSmooth
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
sigma_x (
Sequence
[float
]) – randomly select sigma value for the first spatial dimension.sigma_y (
Sequence
[float
]) – randomly select sigma value for the second spatial dimension if have.sigma_z (
Sequence
[float
]) – randomly select sigma value for the third spatial dimension if have.prob (
float
) – probability of Gaussian smooth.

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
GaussianSharpend¶

class
monai.transforms.
GaussianSharpend
(keys, sigma1=3.0, sigma2=1.0, alpha=30.0)[source]¶ Dictionarybased wrapper of
monai.transforms.GaussianSharpen
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
sigma1 (
Union
[Sequence
[float
],float
]) – sigma parameter for the first gaussian kernel. if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, use it for all spatial dimensions.sigma2 (
Union
[Sequence
[float
],float
]) – sigma parameter for the second gaussian kernel. if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, use it for all spatial dimensions.alpha (
float
) – weight parameter to compute the final result.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
IO (Dict)¶
LoadDatad¶

class
monai.transforms.
LoadDatad
(keys, loader, meta_key_postfix='meta_dict', overwriting=False)[source]¶ Base class for dictionarybased wrapper of IO loader transforms. It 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
key_{meta_key_postfix}
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
loader (
Callable
) – callable function to load data from expected source. typically, it’s array level transform, for example: LoadNifti, LoadPNG and LoadNumpy, etc.meta_key_postfix (
str
) – use key_{postfix} to store the metadata of the loaded data, default is meta_dict. The meta data is a dictionary object. For example, load Nifti file for image, store the metadata into image_meta_dict.overwriting (
bool
) – whether allow to overwrite existing meta data of same key. default is False, which will raise exception if encountering existing key.
 Raises
TypeError – When
loader
is notcallable
.TypeError – When
meta_key_postfix
is not astr
.
LoadNiftid¶

class
monai.transforms.
LoadNiftid
(keys, as_closest_canonical=False, dtype=<class 'numpy.float32'>, meta_key_postfix='meta_dict', overwriting=False)[source]¶ Dictionarybased wrapper of
monai.transforms.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 askey_{meta_key_postfix}
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – 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 (
Optional
[dtype
]) – if not None convert the loaded image data to this data type.meta_key_postfix (
str
) – use key_{postfix} to store the metadata of the nifti image, default is meta_dict. The meta data is a dictionary object. For example, load nifti file for image, store the metadata into image_meta_dict.overwriting (
bool
) – whether allow to overwrite existing meta data of same key. default is False, which will raise exception if encountering existing key.
LoadPNGd¶

class
monai.transforms.
LoadPNGd
(keys, dtype=<class 'numpy.float32'>, meta_key_postfix='meta_dict', overwriting=False)[source]¶ Dictionarybased wrapper of
monai.transforms.LoadPNG
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
dtype (
Optional
[dtype
]) – if not None convert the loaded image data to this data type.meta_key_postfix (
str
) – use key_{postfix} to store the metadata of the PNG image, default is meta_dict. The meta data is a dictionary object. For example, load PNG file for image, store the metadata into image_meta_dict.overwriting (
bool
) – whether allow to overwrite existing meta data of same key. default is False, which will raise exception if encountering existing key.
LoadNumpyd¶

class
monai.transforms.
LoadNumpyd
(keys, dtype=<class 'numpy.float32'>, npz_keys=None, meta_key_postfix='meta_dict', overwriting=False)[source]¶ Dictionarybased wrapper of
monai.transforms.LoadNumpy
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
dtype (
Optional
[dtype
]) – if not None convert the loaded data to this data type.npz_keys (
Union
[Collection
[Hashable
],Hashable
,None
]) – if loading npz file, only load the specified keys, if None, load all the items. stack the loaded items together to construct a new first dimension.meta_key_postfix (
str
) – use key_{postfix} to store the metadata of the Numpy data, default is meta_dict. The meta data is a dictionary object. For example, load Numpy file for mask, store the metadata into mask_meta_dict.overwriting (
bool
) – whether allow to overwrite existing meta data of same key. default is False, which will raise exception if encountering existing key.
Postprocessing (Dict)¶
SplitChanneld¶

class
monai.transforms.
SplitChanneld
(keys, output_postfixes, to_onehot=False, num_classes=None)[source]¶ Dictionarybased wrapper of
monai.transforms.SplitChannel
. All the input specified by keys should be splitted into same count of data. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
output_postfixes (
Sequence
[str
]) – the postfixes to construct keys to store split data. for example: if the key of input data is pred and split 2 classes, the output data keys will be: pred_(output_postfixes[0]), pred_(output_postfixes[1])to_onehot (
Union
[Sequence
[bool
],bool
]) – whether to convert the data to OneHot format, default is False. it also can be a sequence of bool, each element corresponds to a key inkeys
.num_classes (
Union
[Sequence
[int
],int
,None
]) – the class number used to convert to OneHot format if to_onehot is True. it also can be a sequence of int, each element corresponds to a key inkeys
.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
Activationsd¶

class
monai.transforms.
Activationsd
(keys, sigmoid=False, softmax=False, other=None)[source]¶ Dictionarybased wrapper of
monai.transforms.AddActivations
. Add activation layers to the input data specified by keys. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to model output and label. See also:monai.transforms.compose.MapTransform
sigmoid (
Union
[Sequence
[bool
],bool
]) – whether to execute sigmoid function on model output before transform. it also can be a sequence of bool, each element corresponds to a key inkeys
.softmax (
Union
[Sequence
[bool
],bool
]) – whether to execute softmax function on model output before transform. it also can be a sequence of bool, each element corresponds to a key inkeys
.other (
Union
[Sequence
[Callable
],Callable
,None
]) – callable function to execute other activation layers, for example: other = lambda x: torch.tanh(x). it also can be a sequence of Callable, each element corresponds to a key inkeys
.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
AsDiscreted¶

class
monai.transforms.
AsDiscreted
(keys, argmax=False, to_onehot=False, n_classes=None, threshold_values=False, logit_thresh=0.5)[source]¶ Dictionarybased wrapper of
monai.transforms.AsDiscrete
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to model output and label. See also:monai.transforms.compose.MapTransform
argmax (
Union
[Sequence
[bool
],bool
]) – whether to execute argmax function on input data before transform. it also can be a sequence of bool, each element corresponds to a key inkeys
.to_onehot (
Union
[Sequence
[bool
],bool
]) – whether to convert input data into the onehot format. Defaults to False. it also can be a sequence of bool, each element corresponds to a key inkeys
.n_classes (
Union
[Sequence
[int
],int
,None
]) – the number of classes to convert to OneHot format. it also can be a sequence of int, each element corresponds to a key inkeys
.threshold_values (
Union
[Sequence
[bool
],bool
]) – whether threshold the float value to int number 0 or 1, default is False. it also can be a sequence of bool, each element corresponds to a key inkeys
.logit_thresh (
Union
[Sequence
[float
],float
]) – the threshold value for thresholding operation, default is 0.5. it also can be a sequence of float, each element corresponds to a key inkeys
.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
KeepLargestConnectedComponentd¶

class
monai.transforms.
KeepLargestConnectedComponentd
(keys, applied_labels, independent=True, connectivity=None)[source]¶ Dictionarybased wrapper of :py:class:monai.transforms.KeepLargestConnectedComponent.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
applied_labels (
Union
[Sequence
[int
],int
]) – Labels for applying the connected component on. If only one channel. The pixel whose value is not in this list will remain unchanged. If the data is in onehot format, this is the channel indexes to apply transform.independent (
bool
) – consider several labels as a whole or independent, default is True. Example use case would be segment label 1 is liver and label 2 is liver tumor, in that case you want this “independent” to be specified as False.connectivity (
Optional
[int
]) – Maximum number of orthogonal hops to consider a pixel/voxel as a neighbor. Accepted values are ranging from 1 to input.ndim. IfNone
, a full connectivity ofinput.ndim
is used.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
LabelToContourd¶

class
monai.transforms.
LabelToContourd
(keys, kernel_type='Laplace')[source]¶ Dictionarybased wrapper of :py:class:monai.transforms.LabelToContour.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
kernel_type (
str
) – the method applied to do edge detection, default is “Laplace”.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
Ensembled¶

class
monai.transforms.
Ensembled
(keys, ensemble, output_key=None)[source]¶ Base class of dictionarybased ensemble transforms.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be stack and execute ensemble. if only 1 key provided, suppose it’s a PyTorch Tensor with data stacked on dimension E.output_key (
Optional
[str
]) – the key to store ensemble result in the dictionary.ensemble (
Callable
) – callable method to execute ensemble on specified data. if only 1 key provided in keys, output_key can be None and use keys as default.
 Raises
TypeError – When
ensemble
is notcallable
.ValueError – When
len(keys) > 1
andoutput_key=None
. Incompatible values.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
MeanEnsembled¶

class
monai.transforms.
MeanEnsembled
(keys, output_key=None, weights=None)[source]¶ Dictionarybased wrapper of :py:class:monai.transforms.MeanEnsemble.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be stack and execute ensemble. if only 1 key provided, suppose it’s a PyTorch Tensor with data stacked on dimension E.output_key (
Optional
[str
]) – the key to store ensemble result in the dictionary. if only 1 key provided in keys, output_key can be None and use keys as default.weights (
Union
[Sequence
[float
],Tensor
,ndarray
,None
]) – can be a list or tuple of numbers for input data with shape: [E, B, C, H, W[, D]]. or a Numpy ndarray or a PyTorch Tensor data. the weights will be added to input data from highest dimension, for example: 1. if the weights only has 1 dimension, it will be added to the E dimension of input data. 2. if the weights has 3 dimensions, it will be added to E, B and C dimensions. it’s a typical practice to add weights for different classes: to ensemble 3 segmentation model outputs, every output has 4 channels(classes), so the input data shape can be: [3, B, 4, H, W, D]. and add different weights for different classes, so the weights shape can be: [3, 1, 4]. for example: weights = [[[1, 2, 3, 4]], [[4, 3, 2, 1]], [[1, 1, 1, 1]]].
VoteEnsembled¶

class
monai.transforms.
VoteEnsembled
(keys, output_key=None, num_classes=None)[source]¶ Dictionarybased wrapper of :py:class:monai.transforms.VoteEnsemble.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be stack and execute ensemble. if only 1 key provided, suppose it’s a PyTorch Tensor with data stacked on dimension E.output_key (
Optional
[str
]) – the key to store ensemble result in the dictionary. if only 1 key provided in keys, output_key can be None and use keys as default.num_classes (
Optional
[int
]) – if the input is single channel data instead of OneHot, we can’t get class number from channel, need to explicitly specify the number of classes to vote.
Spatial (Dict)¶
Spacingd¶

class
monai.transforms.
Spacingd
(keys, pixdim, diagonal=False, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.BORDER: 'border'>, align_corners=False, dtype=<class 'numpy.float64'>, meta_key_postfix='meta_dict')[source]¶ Dictionarybased wrapper of
monai.transforms.Spacing
.This transform assumes the
data
dictionary has a key for the input data’s metadata and contains affine field. The key is formed bykey_{meta_key_postfix}
.After resampling the input array, this transform will write the new affine to the affine field of metadata which is formed by
key_{meta_key_postfix}
.See also
 Parameters
pixdim (
Sequence
[float
]) – 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 (
Union
[Sequence
[Union
[GridSampleMode
,str
]],GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of string, each element corresponds to a key inkeys
.padding_mode (
Union
[Sequence
[Union
[GridSamplePadMode
,str
]],GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"border"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of string, each element corresponds to a key inkeys
.align_corners (
Union
[Sequence
[bool
],bool
]) – Geometrically, we consider the pixels of the input as squares rather than points. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of bool, each element corresponds to a key inkeys
.dtype (
Union
[Sequence
[dtype
],dtype
,None
]) – data type for resampling computation. Defaults tonp.float64
for best precision. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
. It also can be a sequence of np.dtype, each element corresponds to a key inkeys
.meta_key_postfix (
str
) – use key_{postfix} to to fetch the meta data according to the key data, default is meta_dict, the meta data is a dictionary object. For example, to handle key image, read/write affine matrices from the metadata image_meta_dict dictionary’s affine field.
 Raises
TypeError – When
meta_key_postfix
is not astr
.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
Orientationd¶

class
monai.transforms.
Orientationd
(keys, axcodes=None, as_closest_canonical=False, labels=(('L', 'R'), ('P', 'A'), ('I', 'S')), meta_key_postfix='meta_dict')[source]¶ Dictionarybased wrapper of
monai.transforms.Orientation
.This transform assumes the
data
dictionary has a key for the input data’s metadata and contains affine field. The key is formed bykey_{meta_key_postfix}
.After reorienting the input array, this transform will write the new affine to the affine field of metadata which is formed by
key_{meta_key_postfix}
. Parameters
axcodes (
Optional
[str
]) – 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 (
bool
) – 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_postfix (
str
) – use key_{postfix} to to fetch the meta data according to the key data, default is meta_dict, the meta data is a dictionary object. For example, to handle key image, read/write affine matrices from the metadata image_meta_dict dictionary’s affine field.
 Raises
TypeError – When
meta_key_postfix
is not astr
.
See also
nibabel.orientations.ornt2axcodes.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
Flipd¶

class
monai.transforms.
Flipd
(keys, spatial_axis=None)[source]¶ Dictionarybased wrapper of
monai.transforms.Flip
.See numpy.flip for additional details. https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – Keys to pick data for transformation.spatial_axis (
Union
[Sequence
[int
],int
,None
]) – Spatial axes along which to flip over. Default is None.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
RandFlipd¶

class
monai.transforms.
RandFlipd
(keys, prob=0.1, spatial_axis=None)[source]¶ Dictionarybased version
monai.transforms.RandFlip
.See numpy.flip for additional details. https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – Keys to pick data for transformation.prob (
float
) – Probability of flipping.spatial_axis (
Union
[Sequence
[int
],int
,None
]) – Spatial axes along which to flip over. Default is None.

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
Rotated¶

class
monai.transforms.
Rotated
(keys, angle, keep_size=True, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.BORDER: 'border'>, align_corners=False, dtype=<class 'numpy.float64'>)[source]¶ Dictionarybased wrapper of
monai.transforms.Rotate
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – Keys to pick data for transformation.angle (
Union
[Sequence
[float
],float
]) – Rotation angle(s) in degrees.keep_size (
bool
) – If it is False, the output shape is adapted so that the input array is contained completely in the output. If it is True, the output shape is the same as the input. Default is True.mode (
Union
[Sequence
[Union
[GridSampleMode
,str
]],GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of string, each element corresponds to a key inkeys
.padding_mode (
Union
[Sequence
[Union
[GridSamplePadMode
,str
]],GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"border"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of string, each element corresponds to a key inkeys
.align_corners (
Union
[Sequence
[bool
],bool
]) – Defaults to False. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of bool, each element corresponds to a key inkeys
.dtype (
Union
[Sequence
[Optional
[dtype
]],dtype
,None
]) – data type for resampling computation. Defaults tonp.float64
for best precision. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
. It also can be a sequence of dtype or None, each element corresponds to a key inkeys
.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
RandRotated¶

class
monai.transforms.
RandRotated
(keys, range_x=0.0, range_y=0.0, range_z=0.0, prob=0.1, keep_size=True, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.BORDER: 'border'>, align_corners=False, dtype=<class 'numpy.float64'>)[source]¶ Dictionarybased version
monai.transforms.RandRotate
Randomly rotates the input arrays. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – Keys to pick data for transformation.range_x (
Union
[Tuple
[float
,float
],float
]) – Range of rotation angle in degrees in the plane defined by the first and second axes. If single number, angle is uniformly sampled from (range_x, range_x).range_y (
Union
[Tuple
[float
,float
],float
]) – Range of rotation angle in degrees in the plane defined by the first and third axes. If single number, angle is uniformly sampled from (range_y, range_y).range_z (
Union
[Tuple
[float
,float
],float
]) – Range of rotation angle in degrees in the plane defined by the second and third axes. If single number, angle is uniformly sampled from (range_z, range_z).prob (
float
) – Probability of rotation.keep_size (
bool
) – If it is False, the output shape is adapted so that the input array is contained completely in the output. If it is True, the output shape is the same as the input. Default is True.mode (
Union
[Sequence
[Union
[GridSampleMode
,str
]],GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of string, each element corresponds to a key inkeys
.padding_mode (
Union
[Sequence
[Union
[GridSamplePadMode
,str
]],GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"border"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of string, each element corresponds to a key inkeys
.align_corners (
Union
[Sequence
[bool
],bool
]) – Defaults to False. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate It also can be a sequence of bool, each element corresponds to a key inkeys
.dtype (
Union
[Sequence
[Optional
[dtype
]],dtype
,None
]) – data type for resampling computation. Defaults tonp.float64
for best precision. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
. It also can be a sequence of dtype or None, each element corresponds to a key inkeys
.

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
Zoomd¶

class
monai.transforms.
Zoomd
(keys, zoom, mode=<InterpolateMode.AREA: 'area'>, align_corners=None, keep_size=True)[source]¶ Dictionarybased wrapper of
monai.transforms.Zoom
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – Keys to pick data for transformation.zoom (
Union
[Sequence
[float
],float
]) – 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.mode (
Union
[Sequence
[Union
[InterpolateMode
,str
]],InterpolateMode
,str
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults to"area"
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate It also can be a sequence of string, each element corresponds to a key inkeys
.align_corners (
Union
[Sequence
[Optional
[bool
]],bool
,None
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Default: None. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate It also can be a sequence of bool or None, each element corresponds to a key inkeys
.keep_size (
bool
) – Should keep original size (pad if needed), default is True.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
RandZoomd¶

class
monai.transforms.
RandZoomd
(keys, prob=0.1, min_zoom=0.9, max_zoom=1.1, mode=<InterpolateMode.AREA: 'area'>, align_corners=None, keep_size=True)[source]¶ Dictbased version
monai.transforms.RandZoom
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – Keys to pick data for transformation.prob (
float
) – Probability of zooming.min_zoom (
Union
[Sequence
[float
],float
]) – Min zoom factor. Can be float or sequence same size as image. If a float, select a random factor from [min_zoom, max_zoom] then apply to all spatial dims to keep the original spatial shape ratio. If a sequence, min_zoom should contain one value for each spatial axis.max_zoom (
Union
[Sequence
[float
],float
]) – Max zoom factor. Can be float or sequence same size as image. If a float, select a random factor from [min_zoom, max_zoom] then apply to all spatial dims to keep the original spatial shape ratio. If a sequence, max_zoom should contain one value for each spatial axis.mode (
Union
[Sequence
[Union
[InterpolateMode
,str
]],InterpolateMode
,str
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults to"area"
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate It also can be a sequence of string, each element corresponds to a key inkeys
.align_corners (
Union
[Sequence
[Optional
[bool
]],bool
,None
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Default: None. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate It also can be a sequence of bool or None, each element corresponds to a key inkeys
.keep_size (
bool
) – Should keep original size (pad if needed), default is True.

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
RandRotate90d¶

class
monai.transforms.
RandRotate90d
(keys, prob=0.1, max_k=3, spatial_axes=(0, 1))[source]¶ Dictionarybased version
monai.transforms.RandRotate90
. With probability prob, input arrays are rotated by 90 degrees in the plane specified by spatial_axes. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – 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 (
Tuple
[int
,int
]) – 2 int numbers, defines the plane to rotate with 2 spatial axes. Default: (0, 1), this is the first two axis in spatial dimensions.

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
Rotate90d¶

class
monai.transforms.
Rotate90d
(keys, k=1, spatial_axes=(0, 1))[source]¶ Dictionarybased wrapper of
monai.transforms.Rotate90
. Parameters
k (
int
) – number of times to rotate by 90 degrees.spatial_axes (
Tuple
[int
,int
]) – 2 int numbers, 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
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
Resized¶

class
monai.transforms.
Resized
(keys, spatial_size, mode=<InterpolateMode.AREA: 'area'>, align_corners=None)[source]¶ Dictionarybased wrapper of
monai.transforms.Resize
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
spatial_size (
Union
[Sequence
[int
],int
]) – expected shape of spatial dimensions after resize operation. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 1) will be adapted to (32, 64) if the second spatial dimension size of img is 64.mode (
Union
[Sequence
[Union
[InterpolateMode
,str
]],InterpolateMode
,str
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults to"area"
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate It also can be a sequence of string, each element corresponds to a key inkeys
.align_corners (
Union
[Sequence
[Optional
[bool
]],bool
,None
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Default: None. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate It also can be a sequence of bool or None, each element corresponds to a key inkeys
.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
RandAffined¶

class
monai.transforms.
RandAffined
(keys, spatial_size=None, prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.REFLECTION: 'reflection'>, as_tensor_output=True, device=None)[source]¶ Dictionarybased wrapper of
monai.transforms.RandAffine
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed.spatial_size (
Union
[Sequence
[int
],int
,None
]) – output image spatial size. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 1) will be adapted to (32, 64) if the second spatial dimension size of img is 64.prob (
float
) – probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid.rotate_range (
Union
[Sequence
[float
],float
,None
]) – angle range in radians. rotate_range[0] with be used to generate the 1st rotation parameter from uniform[rotate_range[0], rotate_range[0]). Similarly, rotate_range[1] and rotate_range[2] are used in 3D affine for the range of 2nd and 3rd axes.shear_range (
Union
[Sequence
[float
],float
,None
]) – 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 Nth parameter.translate_range (
Union
[Sequence
[float
],float
,None
]) – 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 Nth parameter.scale_range (
Union
[Sequence
[float
],float
,None
]) – 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 Nth parameter.mode (
Union
[Sequence
[Union
[GridSampleMode
,str
]],GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of string, each element corresponds to a key inkeys
.padding_mode (
Union
[Sequence
[Union
[GridSamplePadMode
,str
]],GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"reflection"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of string, each element corresponds to a key inkeys
.as_tensor_output (
bool
) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.device (
Optional
[device
]) – device on which the tensor will be allocated.
See also
monai.transforms.compose.MapTransform
RandAffineGrid
for the random affine parameters configurations.

randomize
(data=None)[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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None

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 (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Returns
a Randomizable instance.
Rand2DElasticd¶

class
monai.transforms.
Rand2DElasticd
(keys, spacing, magnitude_range, spatial_size=None, prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.REFLECTION: 'reflection'>, as_tensor_output=False, device=None)[source]¶ Dictionarybased wrapper of
monai.transforms.Rand2DElastic
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed.spacing (
Union
[Tuple
[float
,float
],float
]) – distance in between the control points.magnitude_range (
Tuple
[float
,float
]) – 2 int numbers, the random offsets will be generated fromuniform[magnitude[0], magnitude[1])
.spatial_size (
Union
[Sequence
[int
],int
,None
]) – specifying output image spatial size [h, w]. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 1) will be adapted to (32, 64) if the second spatial dimension size of img is 64.prob (
float
) – probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid, otherwise returns aspatial_size
centered area extracted from the input image.rotate_range (
Union
[Sequence
[float
],float
,None
]) – angle range in radians. rotate_range[0] with be used to generate the 1st rotation parameter from uniform[rotate_range[0], rotate_range[0]).shear_range (
Union
[Sequence
[float
],float
,None
]) – shear_range[0] with be used to generate the 1st shearing parameter from uniform[shear_range[0], shear_range[0]). Similarly, shear_range[1] controls the range of the uniform distribution used to generate the 2nd parameter.translate_range (
Union
[Sequence
[float
],float
,None
]) – translate_range[0] with be used to generate the 1st shift parameter from uniform[translate_range[0], translate_range[0]). Similarly, translate_range[1] controls the range of the uniform distribution used to generate the 2nd parameter.scale_range (
Union
[Sequence
[float
],float
,None
]) – 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] controls the range of the uniform distribution used to generate the 2nd parameter.mode (
Union
[Sequence
[Union
[GridSampleMode
,str
]],GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of string, each element corresponds to a key inkeys
.padding_mode (
Union
[Sequence
[Union
[GridSamplePadMode
,str
]],GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"reflection"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of string, each element corresponds to a key inkeys
.as_tensor_output (
bool
) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.device (
Optional
[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.

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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None

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 (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Returns
a Randomizable instance.
Rand3DElasticd¶

class
monai.transforms.
Rand3DElasticd
(keys, sigma_range, magnitude_range, spatial_size=None, prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.REFLECTION: 'reflection'>, as_tensor_output=False, device=None)[source]¶ Dictionarybased wrapper of
monai.transforms.Rand3DElastic
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed.sigma_range (
Tuple
[float
,float
]) – a Gaussian kernel with standard deviation sampled fromuniform[sigma_range[0], sigma_range[1])
will be used to smooth the random offset grid.magnitude_range (
Tuple
[float
,float
]) – the random offsets on the grid will be generated fromuniform[magnitude[0], magnitude[1])
.spatial_size (
Union
[Sequence
[int
],int
,None
]) – specifying output image spatial size [h, w, d]. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 32, 1) will be adapted to (32, 32, 64) if the third spatial dimension size of img is 64.prob (
float
) – probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid, otherwise returns aspatial_size
centered area extracted from the input image.rotate_range (
Union
[Sequence
[float
],float
,None
]) – angle range in radians. rotate_range[0] with be used to generate the 1st rotation parameter from uniform[rotate_range[0], rotate_range[0]). Similarly, rotate_range[1] and rotate_range[2] are used in 3D affine for the range of 2nd and 3rd axes.shear_range (
Union
[Sequence
[float
],float
,None
]) – shear_range[0] with be used to generate the 1st shearing parameter from uniform[shear_range[0], shear_range[0]). Similarly, shear_range[1] and shear_range[2] controls the range of the uniform distribution used to generate the 2nd and 3rd parameters.translate_range (
Union
[Sequence
[float
],float
,None
]) – translate_range[0] with be used to generate the 1st shift parameter from uniform[translate_range[0], translate_range[0]). Similarly, translate_range[1] and translate_range[2] controls the range of the uniform distribution used to generate the 2nd and 3rd parameters.scale_range (
Union
[Sequence
[float
],float
,None
]) – 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] and scale_range[2] controls the range of the uniform distribution used to generate the 2nd and 3rd parameters.mode (
Union
[Sequence
[Union
[GridSampleMode
,str
]],GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of string, each element corresponds to a key inkeys
.padding_mode (
Union
[Sequence
[Union
[GridSamplePadMode
,str
]],GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"reflection"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample It also can be a sequence of string, each element corresponds to a key inkeys
.as_tensor_output (
bool
) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.device (
Optional
[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.

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 generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None

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 (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Returns
a Randomizable instance.
Utility (Dict)¶
Identityd¶

class
monai.transforms.
Identityd
(keys)[source]¶ Dictionarybased wrapper of
monai.transforms.Identity
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
AsChannelFirstd¶

class
monai.transforms.
AsChannelFirstd
(keys, channel_dim=1)[source]¶ Dictionarybased wrapper of
monai.transforms.AsChannelFirst
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – 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
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
AsChannelLastd¶

class
monai.transforms.
AsChannelLastd
(keys, channel_dim=0)[source]¶ Dictionarybased wrapper of
monai.transforms.AsChannelLast
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – 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
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
AddChanneld¶

class
monai.transforms.
AddChanneld
(keys)[source]¶ Dictionarybased wrapper of
monai.transforms.AddChannel
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
RepeatChanneld¶

class
monai.transforms.
RepeatChanneld
(keys, repeats)[source]¶ Dictionarybased wrapper of
monai.transforms.RepeatChannel
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – 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
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
CastToTyped¶

class
monai.transforms.
CastToTyped
(keys, dtype=<class 'numpy.float32'>)[source]¶ Dictionarybased wrapper of
monai.transforms.CastToType
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
dtype (
Union
[Sequence
[Union
[dtype
,dtype
]],dtype
,dtype
]) – convert image to this data type, default is np.float32. it also can be a sequence of np.dtype or torch.dtype, each element corresponds to a key inkeys
.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
ToTensord¶

class
monai.transforms.
ToTensord
(keys)[source]¶ Dictionarybased wrapper of
monai.transforms.ToTensor
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
ToNumpyd¶

class
monai.transforms.
ToNumpyd
(keys)[source]¶ Dictionarybased wrapper of
monai.transforms.ToNumpy
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
DeleteItemsd¶

class
monai.transforms.
DeleteItemsd
(keys)[source]¶ Delete specified items from data dictionary to release memory. It will remove the keyvalues and copy the others to construct a new dictionary.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
SqueezeDimd¶

class
monai.transforms.
SqueezeDimd
(keys, dim=0)[source]¶ Dictionarybased wrapper of
monai.transforms.SqueezeDim
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
dim (
int
) – dimension to be squeezed. Default: 0 (the first dimension)

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
DataStatsd¶

class
monai.transforms.
DataStatsd
(keys, prefix='Data', data_shape=True, value_range=True, data_value=False, additional_info=None, logger_handler=None)[source]¶ Dictionarybased wrapper of
monai.transforms.DataStats
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
prefix (
Union
[Sequence
[str
],str
]) – will be printed in format: “{prefix} statistics”. it also can be a sequence of string, each element corresponds to a key inkeys
.data_shape (
Union
[Sequence
[bool
],bool
]) – whether to show the shape of input data. it also can be a sequence of bool, each element corresponds to a key inkeys
.value_range (
Union
[Sequence
[bool
],bool
]) – whether to show the value range of input data. it also can be a sequence of bool, each element corresponds to a key inkeys
.data_value (
Union
[Sequence
[bool
],bool
]) – whether to show the raw value of input data. it also can be a sequence of bool, each element corresponds to a key inkeys
. a typical example is to print some properties of Nifti image: affine, pixdim, etc.additional_info (
Union
[Sequence
[Callable
],Callable
,None
]) – user can define callable function to extract additional info from input data. it also can be a sequence of string, each element corresponds to a key inkeys
.logger_handler (
Optional
[Handler
]) – add additional handler to output data: save to file, etc. add existing python logging handlers: https://docs.python.org/3/library/logging.handlers.html

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
SimulateDelayd¶

class
monai.transforms.
SimulateDelayd
(keys, delay_time=0.0)[source]¶ Dictionarybased wrapper of :py:class:monai.transforms.utility.array.SimulateDelay.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
delay_time (
Union
[Sequence
[float
],float
]) – The minimum amount of time, in fractions of seconds, to accomplish this identity task. It also can be a sequence of string, each element corresponds to a key inkeys
.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
CopyItemsd¶

class
monai.transforms.
CopyItemsd
(keys, times, names)[source]¶ Copy specified items from data dictionary and save with different key names. It can copy several items together and copy several times.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
times (
int
) – expected copy times, for example, if keys is “img”, times is 3, it will add 3 copies of “img” data to the dictionary.names (
Union
[Collection
[Hashable
],Hashable
]) – the names coresponding to the newly copied data, the length should match len(keys) x times. for example, if keys is [“img”, “seg”] and times is 2, names can be: [“img_1”, “seg_1”, “img_2”, “seg_2”].
 Raises
ValueError – When
times
is nonpositive.ValueError – When
len(names)
is notlen(keys) * times
. Incompatible values.
ConcatItemsd¶

class
monai.transforms.
ConcatItemsd
(keys, name, dim=0)[source]¶ Concatenate specified items from data dictionary together on the first dim to construct a big array. Expect all the items are numpy array or PyTorch Tensor.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be concatenated together. See also:monai.transforms.compose.MapTransform
name (
str
) – the name coresponding to the key to store the concatenated data.dim (
int
) – on which dimension to concatenate the items, default is 0.
 Raises
ValueError – When insufficient keys are given (
len(self.keys) < 2
).
Lambdad¶

class
monai.transforms.
Lambdad
(keys, func)[source]¶ Dictionarybased wrapper of
monai.transforms.Lambda
.For example:
input_data={'image': np.zeros((10, 2, 2)), 'label': np.ones((10, 2, 2))} lambd = Lambdad(keys='label', func=lambda x: x[:4, :, :]) print(lambd(input_data)['label'].shape) (4, 2, 2)
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
func (
Union
[Sequence
[Callable
],Callable
]) – Lambda/function to be applied. It also can be a sequence of Callable, each element corresponds to a key inkeys
.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
LabelToMaskd¶

class
monai.transforms.
LabelToMaskd
(keys, select_labels, merge_channels=False)[source]¶ Dictionarybased wrapper of
monai.transforms.LabelToMask
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
select_labels (
Union
[Sequence
[int
],int
]) – labels to generate mask from. for 1 channel label, the select_labels is the expected label values, like: [1, 2, 3]. for OneHot format label, the select_labels is the expected channel indexes.merge_channels (
bool
) – whether to use np.any() to merge the result on channel dim. if yes, will return a single channel mask with binary data.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadNiftid and LoadPNGd transforms expect file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
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__(self, image):
# do stuff to image
return image + 1
class MyTransform2:
def __call__(self, img_dict):
# do stuff to image
img_dict["image"] += 1
return img_dict
xform = Compose([adaptor(MyTransform1(), "image"), MyTransform2()])
d = {"image": 1}
print(xform(d))
>>> {'image': 3}
class MyTransform3:
def __call__(self, img_dict):
# do stuff to image
img_dict["image"] = 1
img_dict["segment"] = img_dict["image"]
return img_dict
class MyTransform4:
def __call__(self, img, seg):
# do stuff to image
img = 1
seg = 1
return img, seg
xform = Compose([MyTransform3(), adaptor(MyTransform4(), ["img", "seg"], {"image": "img", "segment": "seg"})])
d = {"image": 1}
print(xform(d))
>>> {'image': 0, 'segment': 0, 'img': 1, 'seg': 1}
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
Utilities¶

monai.transforms.utils.
apply_transform
(transform, data, map_items=True)[source]¶ Transform data with transform. If data is a list or tuple and map_data is True, each item of data will be transformed and this method returns a list of outcomes. otherwise transform will be applied once with data as the argument.
 Parameters
transform (
Callable
) – a callable to be used to transform datadata (
object
) – an object to be transformed.map_items (
bool
) – whether to apply transform to each item in data, if data is a list or tuple. Defaults to True.
 Raises
Exception – When
transform
raises an exception.

monai.transforms.utils.
copypaste_arrays
(src, dest, srccenter, destcenter, dims)[source]¶ Calculate the slices to copy a sliced area of array src into array dest. The area has dimensions dims (use 0 or None to copy everything in that dimension), the source area is centered at srccenter index in src and copied into area centered at destcenter in dest. The dimensions of the copied area will be clipped to fit within the source and destination arrays so a smaller area may be copied than expected. Return value is the tuples of slice objects indexing the copied area in src, and those indexing the copy area in dest.
Example
src = np.random.randint(0,10,(6,6)) dest = np.zeros_like(src) srcslices, destslices = copypaste_arrays(src, dest, (3, 2),(2, 1),(3, 4)) dest[destslices] = src[srcslices] print(src) print(dest) >>> [[9 5 6 6 9 6] [4 3 5 6 1 2] [0 7 3 2 4 1] [3 0 0 1 5 1] [9 4 7 1 8 2] [6 6 5 8 6 7]] [[0 0 0 0 0 0] [7 3 2 4 0 0] [0 0 1 5 0 0] [4 7 1 8 0 0] [0 0 0 0 0 0] [0 0 0 0 0 0]]

monai.transforms.utils.
create_control_grid
(spatial_shape, spacing, homogeneous=True, dtype=<class 'float'>)[source]¶ control grid with two additional point in each direction

monai.transforms.utils.
create_grid
(spatial_size, spacing=None, homogeneous=True, dtype=<class 'float'>)[source]¶ compute a spatial_size mesh.
 Parameters
spatial_size (
Sequence
[int
]) – spatial size of the grid.spacing (
Optional
[Sequence
[float
]]) – same len asspatial_size
, defaults to 1.0 (dense grid).homogeneous (
bool
) – whether to make homogeneous coordinates.dtype (
dtype
) – output grid data type.
 Return type
ndarray

monai.transforms.utils.
create_rotate
(spatial_dims, radians)[source]¶ create a 2D or 3D rotation matrix
 Parameters
spatial_dims (
int
) – {2
,3
} spatial rankradians (
Union
[Sequence
[float
],float
]) – rotation radians when spatial_dims == 3, the radians sequence corresponds to rotation in the 1st, 2nd, and 3rd dim respectively.
 Raises
ValueError – When
radians
is empty.ValueError – When
spatial_dims
is not one of [2, 3].

monai.transforms.utils.
create_scale
(spatial_dims, scaling_factor)[source]¶ create a scaling matrix
 Parameters
spatial_dims (
int
) – spatial rankscaling_factor (
Union
[Sequence
[float
],float
]) – scaling factors, defaults to 1.

monai.transforms.utils.
create_shear
(spatial_dims, coefs)[source]¶ create a shearing matrix
 Parameters
spatial_dims (
int
) – spatial rankcoefs (
Union
[Sequence
[float
],float
]) – shearing factors, defaults to 0.
 Raises
NotImplementedError – When
spatial_dims
is not one of [2, 3].

monai.transforms.utils.
create_translate
(spatial_dims, shift)[source]¶ create a translation matrix
 Parameters
spatial_dims (
int
) – spatial rankshift (
Union
[Sequence
[float
],float
]) – translate factors, defaults to 0.

monai.transforms.utils.
generate_pos_neg_label_crop_centers
(label, spatial_size, num_samples, pos_ratio, image=None, image_threshold=0.0, rand_state=<module 'numpy.random' from '/home/docs/checkouts/readthedocs.org/user_builds/monai/envs/latest/lib/python3.7/sitepackages/numpy/random/__init__.py'>)[source]¶ Generate valid sample locations based on image with option for specifying foreground ratio Valid: samples sitting entirely within image, expected input shape: [C, H, W, D] or [C, H, W]
 Parameters
label (
ndarray
) – use the label data to get the foreground/background information.spatial_size (
Union
[Sequence
[int
],int
]) – spatial size of the ROIs to be sampled.num_samples (
int
) – total sample centers to be generated.pos_ratio (
float
) – ratio of total locations generated that have center being foreground.image (
Optional
[ndarray
]) – if image is not None, uselabel = 0 & image > image_threshold
to select background. so the crop center will only exist on valid image area.image_threshold (
float
) – if enabled image_key, useimage > image_threshold
to determine the valid image content area.rand_state (
RandomState
) – numpy randomState object to align with other modules.
 Raises
ValueError – When the proposed roi is larger than the image.
ValueError – When the foreground and background indices lengths are 0.

monai.transforms.utils.
generate_spatial_bounding_box
(img, select_fn=<function <lambda>>, channel_indexes=None, margin=0)[source]¶ generate the spatial bounding box of foreground in the image with startend positions. 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.
 Parameters
img (
ndarray
) – source image to generate bounding box from.select_fn (
Callable
) – function to select expected foreground, default is to select values > 0.channel_indexes (
Union
[Iterable
[int
],int
,None
]) – if defined, select foreground 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.

monai.transforms.utils.
get_largest_connected_component_mask
(img, connectivity=None)[source]¶ Gets the largest connected component mask of an image.
 Parameters
img – Image to get largest connected component from. Shape is (batch_size, spatial_dim1 [, spatial_dim2, …])
connectivity (
Optional
[int
]) – Maximum number of orthogonal hops to consider a pixel/voxel as a neighbor. Accepted values are ranging from 1 to input.ndim. IfNone
, a full connectivity ofinput.ndim
is used.

monai.transforms.utils.
img_bounds
(img)[source]¶ Returns the minimum and maximum indices of nonzero lines in axis 0 of img, followed by that for axis 1.

monai.transforms.utils.
in_bounds
(x, y, margin, maxx, maxy)[source]¶ Returns True if (x,y) is within the rectangle (margin, margin, maxxmargin, maxymargin).
 Return type
bool

monai.transforms.utils.
is_empty
(img)[source]¶ Returns True if img is empty, that is its maximum value is not greater than its minimum.
 Return type
bool

monai.transforms.utils.
rand_choice
(prob=0.5)[source]¶ Returns True if a randomly chosen number is less than or equal to prob, by default this is a 50/50 chance.
 Return type
bool

monai.transforms.utils.
rescale_array
(arr, minv=0.0, maxv=1.0, dtype=<class 'numpy.float32'>)[source]¶ Rescale the values of numpy array arr to be from minv to maxv.
 Return type
ndarray

monai.transforms.utils.
rescale_array_int_max
(arr, dtype=<class 'numpy.uint16'>)[source]¶ Rescale the array arr to be between the minimum and maximum values of the type dtype.

monai.transforms.utils.
rescale_instance_array
(arr, minv=0.0, maxv=1.0, dtype=<class 'numpy.float32'>)[source]¶ Rescale each array slice along the first dimension of arr independently.

monai.transforms.utils.
resize_center
(img, *resize_dims, fill_value=0)[source]¶ Resize img by cropping or expanding the image from the center. The resize_dims values are the output dimensions (or None to use original dimension of img). If a dimension is smaller than that of img then the result will be cropped and if larger padded with zeros, in both cases this is done relative to the center of img. The result is a new image with the specified dimensions and values from img copied into its center.