ai4med.components.transforms package

Submodules

ai4med.components.transforms.add_extreme_points_channel module

class AddExtremePointsChannel(image_field, label_field, dtype='float32', sigma=3, permutation=0, rescale_min=- 1.0, rescale_max=1.0, points_channel_field=None)

Bases: ai4med.components.transforms.transformer.Transformer

Calculate Extreme points from labels and add as new channel.

Parameters
  • image_field – Key for retrieving image.

  • label_field – Key for retrieving field.

  • dtype (str) – string corresponding to NumPy data type for output.

  • sigma – Standard deviation for gaussian filter. (default: 3)

  • permutation – Permutation to be added to each dimension (default: 0)

  • rescale_min – Min value for rescaling. (Default: -1.0)

  • rescale_max – Max value for rescaling. (Default: 1.0)

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

Creates new channel for extreme points using labels and image.

Parameters

transform_ctx – Transform context containing label and image.

Returns

transform_ctx with new channel for extreme points.

ai4med.components.transforms.add_gaussian_noise module

class AddGaussianNoise(fields, probability=0.5, variance=0.1)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Randomly add Gaussian noise to an image.

Parameters
  • fields (list, str) – a list of strings, e.g. [“image”].

  • probability (float) – probability of adding noise. (Default: 0.5)

  • variance (float) – variance of Gaussian noise (Default: 0.1)

Returns

Transform context

is_deterministic()

Returns: True if probability is 0, otherwise False.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

Adds noises to the Medical Images specified in fields.

The probability that noise is added is predefined at 0.50. Nothing happens otherwise.

The noise is created with zero mean and with variance generated from a random number uniformly sampled between 0 and 0.1.

Parameters

transform_ctx – a context containing Medical Images.

Returns

Transform context

ai4med.components.transforms.adjust_contrast module

class AdjustContrast(fields, probability=0.5, min_gamma=0.5, max_gamma=4.5)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Changes intensity with a random gamma curve for entire image.

If multiple fields are provided, each is changed by the a new random gamma curve.

The probability the contrast is modified by this operation is determined by the probability parameter. When it runs, a gamma value is generated either within 0.5 and 1 at 50% of the time, or 1 and 4.5 at 50% of the time.

Parameters
  • fields (list, str) – fields to be adjusted.

  • probability (float) – probability of adjusting contrast. (Default: 0.5)

Returns

A transform context

is_deterministic()

Returns: True if probability is 0, otherwise False.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.apply_threshold module

class ThresholdValues(fields, threshold=0.5, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Applies thresholding.

Select values above a certain threshold. Please note that this transform was previously named ThresholdValues.

Parameters
  • fields (list, str) – fields to be scaled.

  • threshold (float) – Threshold above which to select values.

  • dtype (str) – string corresponding to NumPy data type for output.

Returns

A dict mapping fields to the transformed data.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

Applies threshold to all fields in transform_ctx.

Parameters

transform_ctx – transform_ctx containing fields.

Returns

transform_ctx with threshold applied to all fields.

ai4med.components.transforms.argmax_across_channels module

class ArgmaxAcrossChannels(fields, fields_new=None, dtype='uint16')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Computes multi-label segmentation output using argmax function.

Numpy Argmax performed over channels to collapse data along channel axis, converting each voxel across channels to the label in integer encoding.

Parameters
  • fields (list, str) – fields over which to compute argmax

  • fields_new (list) – new names for output data so original fields are not overwritten, otherwise the result will be written to the same field

  • dtype (str) – string corresponding to NumPy data type for output.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.base_crop_by_pos_neg_ratio module

class BaseCropByPosNegRatio(fields, label_field, size, pos, neg, batch_size)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Base class for Pos Neg Ratio Croppers. This class should not be used directly but just performs the asserts in one place.

Parameters
  • fields (list, str) – which input data to crop

  • label_field (str) – name of field for label image. This will be used for finding foreground/background

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

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

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

  • batch_size (int) – number of samples (crop regions) to take in each batch

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.batch_props module

class BatchProps(fields=None, exclude_fields=False)

Bases: ai4med.components.transforms.transformer.Transformer

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.cast_to_type module

class CastToType(fields, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Casts data in fields to specified dtype

Parameters
  • fields (list, str) – which data fields to cast

  • dtype (str) – string corresponding to NumPy data type for output.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.center_data module

class CenterData(fields, subtrahend=None, divisor=None, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Normalize intensities based on parameters provided. If the subtrahend and divisor are not both provided, the mean and standard deviation are calculated and used for the subtrahend and divisor respectively.

Parameters
  • fields (list, str) – fields to be normalized.

  • subtrahend – the amount to subtract by (usually the mean). This can be a single value or list or tuple that can be broadcast together with the shape of the input image once converted into a NumPy array.

  • divisor – the amount to divide by (usually the standard deviation). This can be a single value or list or tuple that can be broadcast together with the shape of the input image once converted into a NumPy array.

  • dtype (str) – string corresponding to NumPy data type for output.

  • subtrahend and divisors are multi-dimension, currently only 1 extra dimension is supported for channel so (If) –

  • subtraction and division of the NumPy arrays will be across the channel dimension of the image data (the) –

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.classification module

class ProcessMulticlassPreds(field, use_sigmoid_for_binary=True, use_softmax_for_multiclass=True)

Bases: ai4med.components.transforms.transformer.Transformer

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.classification_crop_by_pos_neg_ratio module

class ClassificationFastCropByPosNegRatio(fields, label_field, size, pos=1, neg=1, batch_size=1, dtype='float32', classification_label_field='label', classification_label_background=0)

Bases: ai4med.components.transforms.base_crop_by_pos_neg_ratio.BaseCropByPosNegRatio

This transform crops random fixed sized regions with the center being a foreground or background voxel based on the Pos Neg Ratio. This uses RandomSampleLocationGenerator3 to generate a list of centers to crop from with the fast-crop algorithm.

Parameters
  • fields (list, str) – which input data to crop

  • label_field (str) – name of field for label image. This will be used for finding foreground/background

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

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

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

  • batch_size (int) – number of samples (crop regions) to take in each batch

  • dtype (str) – string corresponding to NumPy data type for output.

is_deterministic()

Returns: False as RandomSampleLocationGenerator3 has randomness.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.classification_crop_with_location_generator module

class ClassificationCropWithLocationGenerator(fields, label_field, size, location_generator, batch_size=1, dtype='float32', classification_label_field='label', classification_label_background=0)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

This is the base for CropByPosNegRatio, FastCropByPosNegRatio, and CropByPosNegRatioLabelOnly to crop fixed sized sub-volumes from images with the centers provided by the location_generator. This class accepts a location_generator for picking centers.

Parameters
  • fields (list, str) – which input data to crop

  • label_field (str) – name of field for label image. This will be used for finding foreground/background

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

  • location_generator – An instance of location generator

  • batch_size (int) – number of samples (crop regions) to take in each batch

  • dtype (str) – string corresponding to NumPy data type for output.

  • classification_label_field (str) – name of field for label. label is a list of integers.

  • classification_label_background (int) – Value to use for background.

is_deterministic()

Returns: False as all location generators right now have randomness.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.convert_to_channels_first module

class ConvertToChannelsFirst(fields)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Convert image to channels_first format. :param fields: fields to be converted :type fields: list, str

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.convert_to_channels_last module

class ConvertToChannelsLast(fields)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Convert image to channels_last format. :param fields: fields to be converted :type fields: list, str

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.convert_to_multi_channel_based_on_brats_classes module

class ConvertToMultiChannelBasedOnBratsClasses(fields, class_names='tc', 'wt', 'et', dtype='int8')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Convert data in fields to multi-channel using brats classes.

Parameters
  • fields (list, str) – keys to all fields to apply this transform to.

  • class_names – Names of brats classes. (TC is tumor core. WT is whole tumor and ET is enhancing tumor)

  • dtype (str) – string corresponding to NumPy data type for output.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.convert_to_multi_channel_based_on_label module

class ConvertToMultiChannelBasedOnLabel(fields, num_indices, translation_list=None, dtype='int8')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Convert data in fields to multi-channel using class indices.

Parameters
  • fields (list, str) – keys to all fields to apply this transform to.

  • num_indices (int) – Number of class indices

  • translation_list (list) – List of ints used for creating channels.

  • dtype (str) – string corresponding to NumPy data type for output.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.copy_properties module

class CopyProperties(fields, from_field, properties, must_copy=True)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.crop_by_pos_neg_ratio module

class CropByPosNegRatio(fields, label_field, size, pos=1, neg=1, batch_size=1, batches_to_gen_at_once=1, dtype='float32')

Bases: ai4med.components.transforms.base_crop_by_pos_neg_ratio.BaseCropByPosNegRatio

This transform crops random fixed sized regions with the center being a foreground or background voxel based on the Pos Neg Ratio. This uses RandomSampleLocationGenerator2 to generate a list of centers to crop from without the fast-crop algorithm. This cropper will automatically pad to increase the image to the minimum size if necessary, padding with default mode constant and with the value 0.

Parameters
  • fields (list, str) – which input data to crop

  • label_field (str) – name of field for label image. This will be used for finding foreground/background

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

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

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

  • batch_size (int) – number of samples (crop regions) to take in each batch

  • batches_to_gen_at_once (int) – if there are any non-deterministic transforms before this one in the pipeline, batches_to_gen_at_once must remain at 1. Otherwise, setting this parameter will batch and cache the generation of ROI centers for multiple epochs to help reduce overhead during training.

  • dtype (str) – string corresponding to NumPy data type for output.

is_deterministic()

Returns: False as RandomSampleLocationGenerator2 has randomness.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.crop_by_pos_neg_ratio_label_only module

class CropByPosNegRatioLabelOnly(fields, label_field, size, pos=1, neg=1, batch_size=1, batches_to_gen_at_once=1, dtype='float32')

Bases: ai4med.components.transforms.base_crop_by_pos_neg_ratio.BaseCropByPosNegRatio

This transform crops random fixed sized regions with the center being a foreground or background voxel based on the Pos Neg Ratio. This transform is like CropByPosNegRatio but does not use the image but only the label to determine foreground/background (see RandomSampleLocationGenerator for details). This cropper will automatically pad to increase the image to the minimum size if necessary, padding with default mode constant and with the value 0.

Parameters
  • fields (list, str) – which input data to crop

  • label_field (str) – name of field for label image. This will be used for finding foreground/background

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

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

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

  • batch_size (int) – number of samples (crop regions) to take in each batch

  • batches_to_gen_at_once (int) – if there are any non-deterministic transforms before this one in the pipeline, batches_to_gen_at_once must remain at 1. Otherwise, setting this parameter will batch and cache the generation of ROI centers for multiple epochs to help reduce overhead during training.

  • dtype (str) – string corresponding to NumPy data type for output.

is_deterministic()

Returns: False as RandomSampleLocationGenerator has randomness.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.crop_fixed_size_random_center module

class CropFixedSizeRandomCenter(fields, size, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Crop sub-volume from image using fixed size with random center. The center is randomly picked from a uniform distribution of the valid possible centers that would fit given the specified size.

Note: This transform will not work if the crop region is larger than the image itself. For small volumes that may run into this issue, it is important to use something like PadSymmetric before this transform to bring the image up to the minimum size.

Parameters
  • fields (list, str) – which input data to crop

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

  • dtype (str) – string corresponding to NumPy data type for output.

is_deterministic()

Returns: False because the center of the ROI to crop is random.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.crop_foreground_object module

class CropForegroundObject(fields, size=None, label_field=None, pad=20, use_only_one_class=False, keep_classes=False, use_gpu=False, pert=0, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Crops sub-volumes conforming to the foreground of the label (greater than 0) from label_field and all fields. The regions are then scaled to the specified size.

Parameters
  • fields (list, str) – input data fields to crop, not including label_field

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

  • label_field (str) – field of the label to use for defining crop area

  • pad (int) – amount of padding around object foreground in millimeters

  • use_only_one_class (bool) – if true, one non-zero class is randomly picked to be the foreground while the rest are ignored. Otherwise, all classes are considered foreground

  • keep_classes (bool) – if true, keep original label indices in label image (no thresholding). If false, the label indices will be altered and all the classes of the input will be consolidated into one class.

  • use_gpu (bool) – if true, use gpu for resizing (currently not yet implemented)

  • pert (int) – maximum magnitude of random perturbation in each dimension added to padding (in voxels)

  • dtype (str) – string corresponding to NumPy data type for output.

is_deterministic()

Returns: False if pert is not 0 because that adds or subtracts randomly from the padding, and false if use_only_one_class is True because a class is randomly chosen to be used as the foreground.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.crop_random_size_w_displacement module

class CropRandomSizeWithDisplacement(fields, lower_size, max_displacement=50, keep_aspect=True, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Crop randomly sized sub-volume from image between lower_size and the image size. The size of the resulting image after this transformation will vary.

Parameters
  • fields (list, str) – which input data to crop

  • lower_size (list, tuple) – lower limit of crop size, can be a list or tuple for the spatial dimensions of the image. lower_size can also be a single value in which case a list will be created automatically and lower_size for each dimension will be that value. int values in lower_size are interpreted as the size in voxels. Alternatively, lower_size can be type float, in which case it must be < 1, to represent the ratio of the cropped lower_size to the original image size.

  • max_displacement (list, tuple) – max displacement from center of the input image to the center of the crop region. This can be one integer greater than or equal to 0, or a list or tuple of size equal to the spatial dimensions of the input images with an integer greater than or equal to 0 for each respective dimension.

  • keep_aspect (bool) – if true, then original aspect ratio is kept. if true, the first dimension of lower_size will be used and the rest will be ignored.

  • dtype (str) – string corresponding to NumPy data type for output.

is_deterministic()

Returns: False as the size of the crop is random, as can be the displacement of the ROI center from the original image’s center.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.crop_sub_volume_center module

class CropSubVolumeCenter(fields, size, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Crops the sub-volume at the center of input data fields.

Parameters
  • fields (list, str) – which input data to crop

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

  • dtype (str) – string corresponding to NumPy data type for output.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.crop_with_location_generator module

class CropWithLocationGenerator(fields, label_field, size, location_generator, batch_size=1, batches_to_gen_at_once=1, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

This is the base for CropByPosNegRatio, FastCropByPosNegRatio, and CropByPosNegRatioLabelOnly to crop fixed sized sub-volumes from images with the centers provided by the location_generator. This class accepts a location_generator for picking centers.

Parameters
  • fields (list, str) – which input data to crop

  • label_field (str) – name of field for label image. This will be used for finding foreground/background

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

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

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

  • batch_size (int) – number of samples (crop regions) to take in each batch

  • batches_to_gen_at_once (int) – if there are any non-deterministic transforms before this one in the pipeline, batches_to_gen_at_once must remain at 1. Otherwise, setting this parameter will batch and cache the generation of ROI centers for multiple epochs to help reduce overhead during training.

  • dtype (str) – string corresponding to NumPy data type for output.

is_deterministic()

Returns: False as all location generators right now have randomness.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.fast_crop_by_pos_neg_ratio module

class FastCropByPosNegRatio(fields, label_field, size, pos=1, neg=1, batch_size=1, batches_to_gen_at_once=1, dtype='float32')

Bases: ai4med.components.transforms.base_crop_by_pos_neg_ratio.BaseCropByPosNegRatio

This transform crops random fixed sized regions with the center being a foreground or background voxel based on the Pos Neg Ratio. This uses RandomSampleLocationGenerator3 to generate a list of centers to crop from with the fast-crop algorithm. This cropper will automatically pad to increase the image to the minimum size if necessary, padding with default mode constant and with the value 0.

Parameters
  • fields (list, str) – which input data to crop

  • label_field (str) – name of field for label image. This will be used for finding foreground/background

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

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

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

  • batch_size (int) – number of samples (crop regions) to take in each batch

  • batches_to_gen_at_once (int) – if there are any non-deterministic transforms before this one in the pipeline, batches_to_gen_at_once must remain at 1. Otherwise, setting this parameter will batch and cache the generation of ROI centers for multiple epochs to help reduce overhead during training.

  • dtype (str) – string corresponding to NumPy data type for output.

is_deterministic()

Returns: False as RandomSampleLocationGenerator3 has randomness.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.fast_pos_neg_ratio_crop_roi module

class FastPosNegRatioCropROI(size, image_field, label_field, deform=False, rotation=True, rotation_degree=15, scale=True, scale_factor=0.1, pos=1, neg=1, fast_crop=False)

Bases: ai4med.components.transforms.transformer.Transformer

Crops a sub volume after sequence of augmentation operations.

Simultaneously crop image and label to size, after specified augmentation operations, including deform, rotation, scale, pos/neg ratio.

Parameters
  • size (tuple, list) – crop size

  • image_field (str) – usually “image”

  • label_field (str) – usually “label”

  • deform (bool) – elastic deformation

  • rotation (bool) – augmented with rotation

  • rotation_degree – maximum rotation degree

  • scale (bool) – augmented with spatial scaling

  • scale_factor – maximum spatial scaling factor ([1-scaling_factor, 1+scaling_factor])

  • pos – factor to control the ratio of positive ROI sampling

  • neg – factor to control the ratio of negative ROI sampling

  • fast_crop (bool) – augmented with fast crop algorithm

Returns

Transform context

is_deterministic()

This is not a deterministic transform.

Returns

False (bool)

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.inference module

class Inference(model_path, model_type='ckpt', inferer_type='simple', roi_size=None, sw_batch_size=1)

Bases: ai4med.components.transforms.transformer.Transformer

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
batch_data(tc: dict)
set_image_shape_format(tc, image_key_name)

ai4med.components.transforms.keep_largest_connected_component module

class KeepLargestCC(label_field, applied_labels, is_independent=True)

Bases: ai4med.components.transforms.transformer.Transformer

Keep only the largest connected component from labels.

Parameters
  • label_field – Key for retrieving label.

  • applied_labels – label number list for applying the connected component on.

  • is_independent – whethere or not to consider several labels as a whole

keep_largest_cc(nda)
transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

Keep the largest connected component from labels .

Parameters

transform_ctx – Transform context containing label.

Returns

transform_ctx with edited label.

ai4med.components.transforms.load_image_masks_from_numpy module

class LoadImageMasksFromNumpy(fields, shape: ai4med.common.shape_format.ShapeFormat, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Load Image and mask pairs from Numpy files.

Parameters
  • shape (ShapeFormat) – Shape of output image.

  • dtype – Type for output data.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.load_jpg module

class LoadJpg(fields, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Loads jpg files.

Parameters
  • fields (list, str) – keys containing paths to load files from.

  • dtype (str) – string corresponding to NumPy data type for output.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.load_nifti module

class LoadNifti(fields, dtype='float32', nii_has_channels_first=False, as_closest_canonical=False)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Loads NIFTI files.

Parameters
  • fields (list, str) – keys containing paths to load files from.

  • dtype (str) – string corresponding to NumPy data type for output.

  • nii_has_channels_first (bool) – is NIFTI stored as channels_first. (Default: False)

  • as_closest_canonical (bool) – is loading in canonical form (Default: False)

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.load_png module

class LoadPng(fields, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Load Png files.

Parameters
  • fields (list, str) – keys containing paths to load files from.

  • dtype (str) – string corresponding to NumPy data type for output.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.mask_image module

class MaskImage(image_field, mask_field, dtype='float32', exclude_labels=None, update_mask_field=False)

Bases: ai4med.components.transforms.transformer.Transformer

Masked the image with the given mask field.

If mask entry m[i,j] is in exclude_labels, then img[i,j] is set to 0.

Parameters
  • image_field (str) – Input image to be masked

  • mask_field (str) – Mask image to use

  • dtype (str) – Data type of result image

  • exclude_labels (list) – A list of values to be excluded

  • update_mask_field (bool) – To update mask or not

is_deterministic()
transform(transform_ctx)

ai4med.components.transforms.merge_dims module

class MergeBatchDims(fields)

Bases: ai4med.components.transforms.transformer.BatchTransformer

transform(batched_data: dict)
merge_dims(data: numpy.ndarray)

ai4med.components.transforms.multi_field_transformer module

class MultiFieldTransformer(fields)

Bases: ai4med.components.transforms.transformer.Transformer

Base class for transformers with multi-fields input data.

Args: fields (list, str): fields of data to be transformed. If a str is provided, it will be turned into a list.

ai4med.components.transforms.normalize_nonzero_intensities module

class NormalizeNonzeroIntensities(fields, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Normalize input to zero mean and unit standard deviation, based on non-zero elements only and for each input channel individually.

Parameters
  • fields (list, str) – fields to be scaled.

  • dtype (str) – string corresponding to NumPy data type for output.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.pad_symmetric module

class PadSymmetric(fields, size, mode='constant')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Pad image to new size with default of same number of 0s on each side. If the number of cells that need to be padded is odd, there will be one more added to the end of the data compared to the beginning. Uses np.pad, so mode can be used to change padding behavior if desired.

Parameters
  • fields (list, str) – keys to all fields to apply this transform to.

  • size – the size of the output e.g. [196,196,196]

  • mode (str) – pad mode. See numpy.lib.arraypad.pad for additional details.

Returns

A transform context

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.print_output_object_info module

class PrintOutputObjectInfo(step)

Bases: ai4med.components.transforms.transformer.Transformer

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.random_axis_flip module

class RandomAxisFlip(fields, probability=0.5)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Flip the image along one randomly picked axis.

Parameters
  • fields (list, str) – key strings for target volume.

  • probability (float) – probability to utilize the transform, between 0 and 1.0. (Default: 0.5)

Returns

Random flipped data.

is_deterministic()

Returns: True if the probability to use the transform is 0, otherwise False.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

Picks a random axis and flips along it.

ai4med.components.transforms.random_bias_field module

class RandomMRBiasField(fields, degree, lower_limit=None, upper_limit=None)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Random bias field augmentation for MR images.

Parameters
  • fields (list, str) – key strings for target volume.

  • degree (int) – degrees of freedom of the polynomials

  • lower_limit (float) – Lower limit for random values of bias

  • upper_limit (float) – Upper limit for random values of bias

is_deterministic()
transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.random_gaussian_smooth module

class RandomGaussianSmooth(fields, probability=0.5, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Randomly smooth the image with a specified probability.

Parameters
  • fields (list, str) – key strings for target volume.

  • probability (float) – probability to flip axis, between 0 and 1.0. (Default: 0.5)

  • dtype (dtype attribute) – data type identifier

Returns

Random smoothed data.

is_deterministic()

Returns: True if probability is 0, otherwise False.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.random_rotate_2d module

class RandomRotate2D(fields, angle, is_random=False, interpolation=1, border_mode=2, dtype=<class 'numpy.float32'>)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Randomly rotates a 2D image by a given angle.

Parameters
  • fields (list, str) – keys for data to be rotated.

  • angle (float) – angle of rotation in degrees.

  • is_random (bool) – should angle be chosen at random. (Default: False)

  • interpolation (int) – Same as ‘flags’ argument to OpenCV’s warpAffine function. (Default = 1 = cv2.INTER_LINEAR).

  • border_mode (int) – Same as ‘border_mode’ argument to OpenCV’s warpAffine function. (Default = 2 = cv2.BORDER_REFLECT).

  • dtype – np array type to convert to after rotation happens.

is_deterministic()

Returns: True if is_random is false, otherwise False.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.random_rotate_3d module

class RandomRotate3D(fields, probability=0.5)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Randomly rotates an image 90, 180 or 270 degrees. The angle is randomly picked.

Parameters
  • fields (list, str) – keys to data.

  • probability (float) – probability of rotating. (Default: 0.5)

is_deterministic()

Returns: True if the probability of rotating is 0, otherwise False.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.random_sharpen module

class RandomSharpen(fields, probability=0.5, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Randomly sharpen the image with a specified probability.

Parameters
  • fields (list, str) – key strings for target volume.

  • probability (float) – probability to flip axis, between 0 and 1.0. (Default: 0.5)

  • dtype (dtype attribute) – data type identifier

Returns

Random sharpened data.

is_deterministic()

Returns: True if probability is 0, otherwise False.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.random_spatial_flip module

class RandomSpatialFlip(fields, probability=0.5)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Flip the image randomly along each axis in spatial domain. This is different from RandomAxisFlip because this can pick multiple axes to flip with probability being the probability for each axis to be flipped.

Parameters
  • fields (list, str) – key strings for target volume.

  • probability (float) – probability to flip axis, between 0 and 1.0. (Default: 0.5)

Returns

Random flipped data.

is_deterministic()

Returns: True if probability is 0, otherwise False.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.random_zoom module

class RandomZoom(image_field, label_field, lower_limits, upper_limits, keep_size=False, use_gpu=False, probability=1)

Bases: ai4med.components.transforms.transformer.Transformer

Zooms the image randomly along each axis in spatial domain. Interpolation is inferred. image_field uses linear interpolation while label_field uses nearest neighbor.

Parameters
  • image_field (str) – key for target image.

  • label_field (str) – Key for target label.

  • lower_limits (tuple) – Lower limits for random values of zoom

  • upper_limits (tuple) – Upper limits for random values of zoom

  • keep_size (bool) – Maintains the original size using padding. Default: True.

  • use_gpu (bool) – Should this transform be run on GPU or CPU. Note that running on GPU can be costly for GPU memory. Default: False.

  • probability (float) – Probability to run this transform. Default=1 to stay consistent with Clara 2.0.

Returns

Random zoomed data.

is_deterministic()
transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.repeat_channel module

class RepeatChannel(fields, repeat_times)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Repeat the image over the channel axis repeat_times times.

Parameters
  • fields (list, str) – keys to all fields to apply this transform to.

  • repeat_times (int) – number of times to repeat the image over the channel axis

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.replace_labels module

class ReplaceLabels(fields, input_labels, output_labels, dtype=<class 'numpy.float32'>)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Replace input_labels to output_labels in data.

Given a set of input labels ([0, 1, 2]), replaces labels to set of output labels ([0, 1, 1]).

Parameters
  • fields (list, str) – fields to apply this transform to.

  • input_labels (list) – Input indices for mapping.

  • output_labels (list) – Output indices for mapping the input_labels to.

Returns

TransformContext with updated fields.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.restore_original_shape module

class RestoreOriginalShape(field, src_field=None, is_label=None)

Bases: ai4med.components.transforms.transformer.Transformer

Restore original shape of image with ImageProperty.ORIGINAL_SHAPE and ImageProperty.ORIGINAL_SHAPE_FORMAT

Parameters
  • field (str) – target field for this transform. If src_field is not provided, this is also used as src_field.

  • src_field (str) – source field for this transform.

  • is_label (bool) – to determine whether to use nearest neighbor or linear interpolation for label data and not label data, respectively.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.save_as_nifti module

class SaveAsNifti(fields, out_dir='./temp/debug', interrupt=False, compressed=True, use_identity=False, revert_canonical=False)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Save data as NIFTI file.

Parameters
  • fields (list) – keys of data to be saved.

  • out_dir (str) – Output directory for storing file.

  • interrupt (bool) – Command line interrupt upon saving. (Default: False)

  • compressed (bool) – save as compressed file. (Default: True)

  • use_identity (bool) – whether to use identify for affine matrix (Default: False)

  • revert_canonical (bool) – Revert the as close canonical operation in LoadNifti (Default: False)

get_unique_output_filename(filename, field)
transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.scale_by_factor module

class ScaleByFactor(fields, factor, is_label=None)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Scales spatial data by specified factor.

Scaling 2D or 3D spatial data to specified size.

Parameters
  • fields (list, str) – fields to scale.

  • factor (float) – factor to scale from original size. 2.0 means 2x.

  • is_label (bool) – to determine whether to use nearest neighbor or linear interpolation for label data and not label data, respectively.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

Performs the scaling operation.

Parameters

transform_ctx (dict) – Transform context.

Returns

The transform context with updated values based on fields.

Return type

dict

ai4med.components.transforms.scale_by_resolution module

class ScaleByResolution(fields, target_resolution, is_label=None)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Scales spatial data to target resolution.

Scaling 2D or 3D spatial data to specified size.

Parameters
  • fields (list, str) – fields to scale.

  • target_resolution (list, tuple) – target resolution to scale to.

  • is_label (bool) – to determine whether to use nearest neighbor or linear interpolation for label data and not label data, respectively.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

Performs the scaling operation.

Parameters

transform_ctx (dict) – Transform context.

Returns

The transform context with updated values based on fields.

Return type

dict

ai4med.components.transforms.scale_by_spacing module

class ScaleBySpacing(fields, target_spacing, is_label=None)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Scales spatial data to target spacing.

Scaling 2D or 3D spatial data to specified size.

Parameters
  • fields (list, str) – fields to scale.

  • target_spacing (list, tuple) – target spacing in mm.

  • is_label (bool) – to determine whether to use nearest neighbor or linear interpolation for label data and not label data, respectively.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

Performs the scaling operation.

Parameters

transform_ctx (dict) – Transform context.

Returns

The transform context with updated values based on fields.

Return type

dict

ai4med.components.transforms.scale_intensity_oscillation module

class ScaleIntensityOscillation(fields, magnitude, probability=0.5, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Randomly shifts intensity uniformly for entire image. If multiple fields are provided, all are shifted by the same amount.

Parameters
  • fields (list, str) – fields to be scaled.

  • magnitude – magnitude of maximum scale shift, has to be greater than zero. This is usually a single scalar value but it could also be a list or tuple that can be broadcast together with the shape of the input image once converted into a NumPy array.

  • probability (float) – probability of operation. (Default: 0.5)

  • dtype (dtype attribute) – data type identifier

is_deterministic()

Returns: True if probability is 0, otherwise False.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.scale_intensity_range module

class ScaleIntensityRange(fields, a_min, a_max, b_min, b_max, clip=False, dtype='float32')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Applies intensity scaling.

Applies the same intensity scaling from (a_min, a_max) to (b_min, b_max) for fields to be scaled.

Parameters
  • fields (list or str) – fields to be scaled.

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

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

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

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

  • clip (bool) – whether to perform clipping after scaling in case there are any values outside of (b_min, b_max)

  • dtype (str) – string corresponding to NumPy data type for output.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.scale_shift_intensity module

class ScaleShiftIntensity(fields, scale=0.1, shift=0.1)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Perturbs intensity as: c = c * (1+scale) + shift * std(c)

Parameters
  • fields (list, str) – list of fields to which this transform applies, eg [‘image’]

  • scale – increase or decrease the range of intensity

  • shift – move up or down the intensity

Returns

Transform context

is_deterministic()

This is not a deterministic transform.

Returns

False (bool)

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.scale_to_shape module

class ScaleToShape(fields, target_shape, is_label=None)

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Scales spatial data to target shape.

Scaling 2D or 3D spatial data to specified size.

Parameters
  • fields (list, str) – fields to scale.

  • target_shape (list, tuple) – target shape, eg [256, 256, 256] for 256x256x256

  • is_label (bool) – to determine whether to use nearest neighbor or linear interpolation for label data and not label data, respectively.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.split_across_channels module

class SplitAcrossChannels(field, channel_names=None)

Bases: ai4med.components.transforms.transformer.Transformer

Split image into one MedicalImage per channel and gets rid of the channel dimension.

Parameters
  • field (str) – field to split.

  • channel_names (list) – list of keys to save each respective channel of the image that has been split. Must match the number of channels of the image. If channel_names is not provided, the new names of the images will be the original field name appended by the channel number.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.split_based_on_brats_classes module

class SplitBasedOnBratsClasses(field, class_names, image_names=None, dtype=<class 'numpy.int8'>)

Bases: ai4med.components.transforms.transformer.Transformer

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.split_based_on_label module

class SplitBasedOnLabel(field, label_names, dtype='int8')

Bases: ai4med.components.transforms.transformer.Transformer

Splits multi-label images.

Splitting multi-label images identified by “field” to new dict entries under specified label names. The input images are expected to be in integer encoding format and the output is in one hot encoding.

Parameters
  • field (str) – field to split.

  • label_names (list) – names for split outputs, one for each label starting from 0. Label values greater than the number of label_names provided will be ignored.

  • dtype (str) – string corresponding to NumPy data type for output.

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)
Parameters

transform_ctx – Transform context.

Returns: The transform context with updated values based on fields.

ai4med.components.transforms.symmetric_padder_div module

class SymmetricPadderDiv(fields, div_int=16, mode='constant')

Bases: ai4med.components.transforms.multi_field_transformer.MultiFieldTransformer

Pad image with new size divisible by given div_int.

Parameters
  • fields (list, str) – keys to all fields to apply this transform to.

  • div_int (int) – integer that each final dimension should be divisible by after padding

  • mode (str) – pad mode. See numpy.lib.arraypad.pad for additional details.

Returns

A transform context

transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

ai4med.components.transforms.transformer module

class BatchTransformer

Bases: abc.ABC

abstract transform(batched_data: dict)
class Transformer

Bases: abc.ABC

Base class for transformers.

is_deterministic()
abstract transform(transform_ctx: ai4med.common.transform_ctx.TransformContext)

Module contents