pymic.transform package

Submodules

pymic.transform.abstract_transform module

class pymic.transform.abstract_transform.AbstractTransform(params)

Bases: object

The abstract class for Transform.

inverse_transform_for_prediction(sample)

Inverse transform for the sample dictionary. Especially, it will update sample[‘predict’] obtained by a network’s prediction based on the inverse transform. This function is only useful for spatial transforms.

pymic.transform.crop module

class pymic.transform.crop.CenterCrop(params)

Bases: AbstractTransform

Crop the given image at the center. Input shape should be [C, D, H, W] or [C, H, W].

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • CenterCrop_output_size – (list or tuple) The output size. [D, H, W] for 3D images and [H, W] for 2D images. If D is None, then the z-axis is not cropped.

  • CenterCrop_inverse – (optional, bool) Is inverse transform needed for inference. Default is True.

inverse_transform_for_prediction(sample)

Inverse transform for the sample dictionary. Especially, it will update sample[‘predict’] obtained by a network’s prediction based on the inverse transform. This function is only useful for spatial transforms.

class pymic.transform.crop.CropWithBoundingBox(params)

Bases: CenterCrop

Crop the image (shape [C, D, H, W] or [C, H, W]) based on a bounding box. The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • CropWithBoundingBox_start – (None, or list/tuple) The start index along each spatial axis. If None, calculate the start index automatically so that the cropped region is centered at the non-zero region.

  • CropWithBoundingBox_output_size – (None or tuple/list): Desired spatial output size. If None, set it as the size of bounding box of non-zero region.

  • CropWithBoundingBox_inverse – (optional, bool) Is inverse transform needed for inference. Default is True.

class pymic.transform.crop.RandomCrop(params)

Bases: CenterCrop

Randomly crop the input image (shape [C, D, H, W] or [C, H, W]).

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • RandomCrop_output_size – (list/tuple) Desired output size [D, H, W] or [H, W]. The output channel is the same as the input channel. If D is None for 3D images, the z-axis is not cropped.

  • RandomCrop_foreground_focus – (optional, bool) If true, allow crop around the foreground. Default is False.

  • RandomCrop_foreground_ratio – (optional, float) Specifying the probability of foreground focus cropping when RandomCrop_foreground_focus is True.

  • RandomCrop_mask_label – (optional, None, or list/tuple) Specifying the foreground labels for foreground focus cropping when RandomCrop_foreground_focus is True.

  • RandomCrop_inverse – (optional, bool) Is inverse transform needed for inference. Default is True.

class pymic.transform.crop.RandomResizedCrop(params)

Bases: CenterCrop

Randomly crop the input image (shape [C, H, W]). Only 2D images are supported.

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • RandomResizedCrop_output_size – (list/tuple) Desired output size [H, W]. The output channel is the same as the input channel.

  • RandomResizedCrop_scale – (list/tuple) Range of scale, e.g. (0.08, 1.0).

  • RandomResizedCrop_ratio – (list/tuple) Range of aspect ratio, e.g. (0.75, 1.33).

  • RandomResizedCrop_inverse – (optional, bool) Is inverse transform needed for inference. Default is False. Currently, the inverse transform is not supported, and this transform is assumed to be used only during training stage.

pymic.transform.flip module

class pymic.transform.flip.RandomFlip(params)

Bases: AbstractTransform

Random flip the image. The shape is [C, D, H, W] or [C, H, W].

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • RandomFlip_flip_depth – (bool) Random flip along depth axis or not, only used for 3D images.

  • RandomFlip_flip_height – (bool) Random flip along height axis or not.

  • RandomFlip_flip_width – (bool) Random flip along width axis or not.

  • RandomFlip_inverse – (optional, bool) Is inverse transform needed for inference. Default is True.

inverse_transform_for_prediction(sample)

Inverse transform for the sample dictionary. Especially, it will update sample[‘predict’] obtained by a network’s prediction based on the inverse transform. This function is only useful for spatial transforms.

pymic.transform.intensity module

class pymic.transform.intensity.GammaCorrection(params)

Bases: AbstractTransform

Apply random gamma correction to given channels.

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • GammaCorrection_channels – (list) A list of int for specifying the channels.

  • GammaCorrection_gamma_min – (float) The minimal gamma value.

  • GammaCorrection_gamma_max – (float) The maximal gamma value.

  • GammaCorrection_probability – (optional, float) The probability of applying GammaCorrection. Default is 0.5.

  • GammaCorrection_inverse – (optional, bool) Is inverse transform needed for inference. Default is False.

class pymic.transform.intensity.GaussianNoise(params)

Bases: AbstractTransform

Add Gaussian Noise to given channels.

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • GaussianNoise_channels – (list) A list of int for specifying the channels.

  • GaussianNoise_mean – (float) The mean value of noise.

  • GaussianNoise_std – (float) The std of noise.

  • GaussianNoise_probability – (optional, float) The probability of applying GaussianNoise. Default is 0.5.

  • GaussianNoise_inverse – (optional, bool) Is inverse transform needed for inference. Default is False.

class pymic.transform.intensity.GrayscaleToRGB(params)

Bases: AbstractTransform

Convert gray scale images to RGB by copying channels.

class pymic.transform.intensity.InOutPainting(params)

Bases: AbstractTransform

Apply in-painting or out-patining randomly. They are mutually exclusive.

class pymic.transform.intensity.InPainting(params)

Bases: AbstractTransform

In-painting of an input image, used for self-supervised learning

class pymic.transform.intensity.LocalShuffling(params)

Bases: AbstractTransform

local pixel shuffling of an input image, used for self-supervised learning

class pymic.transform.intensity.NonLinearTransform(params)

Bases: AbstractTransform

class pymic.transform.intensity.OutPainting(params)

Bases: AbstractTransform

Out-painting of an input image, used for self-supervised learning

pymic.transform.intensity.bernstein_poly(i, n, t)

The Bernstein polynomial of n, i as a function of t.

pymic.transform.intensity.bezier_curve(points, nTimes=1000)

Given a set of control points, return the bezier curve defined by the control points. Control points should be a list of lists, or list of tuples such as [ [1,1], [2,3], [4,5], ..[Xn, Yn] ].

nTimes is the number of time steps, defaults to 1000. See http://processingjs.nihongoresources.com/bezierinfo/

pymic.transform.label_convert module

class pymic.transform.label_convert.LabelConvert(params)

Bases: AbstractTransform

Convert the label based on a source list and target list.

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • LabelConvert_source_list – (list) A list of labels to be converted.

  • LabelConvert_target_list – (list) The target label list.

  • LabelConvert_inverse – (optional, bool) Is inverse transform needed for inference. Default is False.

class pymic.transform.label_convert.LabelConvertNonzero(params)

Bases: AbstractTransform

Convert label into binary, i.e., setting nonzero labels as 1.

class pymic.transform.label_convert.LabelToProbability(params)

Bases: AbstractTransform

Convert one-channel label map to one-hot multi-channel probability map.

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • LabelToProbability_class_num – (int) The class number in the label map.

  • LabelToProbability_inverse – (optional, bool) Is inverse transform needed for inference. Default is False.

class pymic.transform.label_convert.PartialLabelToProbability(params)

Bases: AbstractTransform

Convert one-channel partial label map to one-hot multi-channel probability map. This is used for segmentation tasks only. In the input label map, 0 represents the background class, 1 to C-1 represent the foreground classes, and C represents unlabeled pixels. In the output dictionary, label_prob is the one-hot probability map, and pixel_weight represents a weighting map, where the weight for a pixel is 0 if the label is unkown.

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • PartialLabelToProbability_class_num – (int) The class number for the segmentation task.

  • PartialLabelToProbability_inverse – (optional, bool) Is inverse transform needed for inference. Default is False.

class pymic.transform.label_convert.ReduceLabelDim(params)

Bases: AbstractTransform

Remove the first dimension of label tensor.

pymic.transform.normalize module

class pymic.transform.normalize.NormalizeWithMeanStd(params)

Bases: AbstractTransform

Normalize the image based on mean and std. The image should have a shape of [C, D, H, W] or [C, H, W].

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • NormalizeWithMeanStd_channels – (list/tuple or None) A list or tuple of int for specifying the channels. If None, the transform operates on all the channels.

  • NormalizeWithMeanStd_mean – (list/tuple or None) The mean values along each specified channel. If None, the mean values are calculated automatically.

  • NormalizeWithMeanStd_std – (list/tuple or None) The std values along each specified channel. If None, the std values are calculated automatically.

  • NormalizeWithMeanStd_ignore_non_positive – (optional, bool) Only used when mean and std are not given. Default is False. If True, calculate mean and std in the positive region for normalization, and set non-positive region to random. If False, calculate the mean and std values in the entire image region.

  • NormalizeWithMeanStd_inverse – (optional, bool) Is inverse transform needed for inference. Default is False.

class pymic.transform.normalize.NormalizeWithMinMax(params)

Bases: AbstractTransform

Nomralize the image to [0, 1]. The shape should be [C, D, H, W] or [C, H, W].

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • NormalizeWithMinMax_channels – (list/tuple or None) A list or tuple of int for specifying the channels. If None, the transform operates on all the channels.

  • NormalizeWithMinMax_threshold_lower – (list/tuple or None) The min values along each specified channel. If None, the min values are calculated automatically.

  • NormalizeWithMinMax_threshold_upper – (list/tuple or None) The max values along each specified channel. If None, the max values are calculated automatically.

  • NormalizeWithMinMax_inverse – (optional, bool) Is inverse transform needed for inference. Default is False.

class pymic.transform.normalize.NormalizeWithPercentiles(params)

Bases: AbstractTransform

Nomralize the image to [0, 1] with percentiles for given channels. The shape should be [C, D, H, W] or [C, H, W].

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • NormalizeWithPercentiles_channels – (list/tuple or None) A list or tuple of int for specifying the channels. If None, the transform operates on all the channels.

  • NormalizeWithPercentiles_percentile_lower – (float) The min percentile, which must be between 0 and 100 inclusive.

  • NormalizeWithPercentiles_percentile_upper – (float) The max percentile, which must be between 0 and 100 inclusive.

  • NormalizeWithMinMax_inverse – (optional, bool) Is inverse transform needed for inference. Default is False.

pymic.transform.pad module

class pymic.transform.pad.Pad(params)

Bases: AbstractTransform

Pad an image to an new spatial shape. The image has a shape of [C, D, H, W] or [C, H, W]. The real output size will be max(image_size, output_size).

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • Pad_output_size – (list/tuple) The output size along each spatial axis.

  • Pad_ceil_mode – (optional, bool) If true (by default), the real output size will be the minimal integer multiples of output_size higher than the input size. For example, the input image has a shape of [3, 100, 100], Pad_output_size = [32, 32], and the real output size will be [3, 128, 128] if Pad_ceil_mode = True.

  • Pad_inverse – (optional, bool) Is inverse transform needed for inference. Default is True.

inverse_transform_for_prediction(sample)

Inverse transform for the sample dictionary. Especially, it will update sample[‘predict’] obtained by a network’s prediction based on the inverse transform. This function is only useful for spatial transforms.

pymic.transform.rescale module

class pymic.transform.rescale.RandomRescale(params)

Bases: AbstractTransform

Rescale the input image randomly along each spatial axis.

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • RandomRescale_lower_bound – (list/tuple or float) Desired minimal rescale ratio. If tuple/list, the length should be 3 or 2.

  • RandomRescale_upper_bound – (list/tuple or float) Desired maximal rescale ratio. If tuple/list, the length should be 3 or 2.

  • RandomRescale_probability – (optional, float) The probability of applying RandomRescale. Default is 0.5.

  • RandomRescale_inverse – (optional, bool) Is inverse transform needed for inference. Default is True.

inverse_transform_for_prediction(sample)

Inverse transform for the sample dictionary. Especially, it will update sample[‘predict’] obtained by a network’s prediction based on the inverse transform. This function is only useful for spatial transforms.

class pymic.transform.rescale.Rescale(params)

Bases: AbstractTransform

Rescale the image to a given size.

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • Rescale_output_size – (list/tuple or int) The output size along each spatial axis, such as [D, H, W] or [H, W]. If D is None, the input image is only reslcaled in 2D. If int, the smallest axis is matched to output_size keeping aspect ratio the same as the input.

  • Rescale_inverse – (optional, bool) Is inverse transform needed for inference. Default is True.

inverse_transform_for_prediction(sample)

Inverse transform for the sample dictionary. Especially, it will update sample[‘predict’] obtained by a network’s prediction based on the inverse transform. This function is only useful for spatial transforms.

pymic.transform.rotate module

class pymic.transform.rotate.RandomRotate(params)

Bases: AbstractTransform

Random rotate an image, wiht a shape of [C, D, H, W] or [C, H, W].

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • RandomRotate_angle_range_d – (list/tuple or None) Rotation angle (degree) range along depth axis (x-y plane), e.g., (-90, 90). If None, no rotation along this axis.

  • RandomRotate_angle_range_h – (list/tuple or None) Rotation angle (degree) range along height axis (x-z plane), e.g., (-90, 90). If None, no rotation along this axis. Only used for 3D images.

  • RandomRotate_angle_range_w – (list/tuple or None) Rotation angle (degree) range along width axis (y-z plane), e.g., (-90, 90). If None, no rotation along this axis. Only used for 3D images.

  • RandomRotate_probability – (optional, float) The probability of applying RandomRotate. Default is 0.5.

  • RandomRotate_inverse – (optional, bool) Is inverse transform needed for inference. Default is True.

inverse_transform_for_prediction(sample)

Inverse transform for the sample dictionary. Especially, it will update sample[‘predict’] obtained by a network’s prediction based on the inverse transform. This function is only useful for spatial transforms.

pymic.transform.threshold module

class pymic.transform.threshold.ChannelWiseThreshold(params)

Bases: AbstractTransform

Thresholding the image for given channels.

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • ChannelWiseThreshold_channels – (list/tuple or None) A list of specified channels for thresholding. If None (by default), all the channels will be thresholded.

  • ChannelWiseThreshold_threshold_lower – (list/tuple or None) The lower threshold for the given channels.

  • ChannelWiseThreshold_threshold_upper – (list/tuple or None) The upper threshold for the given channels.

  • ChannelWiseThreshold_replace_lower – (list/tuple or None) The output value for pixels with an input value lower than the threshold_lower.

  • ChannelWiseThreshold_replace_upper – (list/tuple or None) The output value for pixels with an input value higher than the threshold_upper.

  • ChannelWiseThreshold_inverse – (optional, bool) Is inverse transform needed for inference. Default is False.

class pymic.transform.threshold.ChannelWiseThresholdWithNormalize(params)

Bases: AbstractTransform

Apply thresholding and normalization for given channels. Pixel intensity will be truncated to the range of (lower, upper) and then normalized. If mean_std_mode is True, the mean and std values for pixel in the target range is calculated for normalization, and input intensity outside that range will be replaced by random values. Otherwise, the intensity will be normalized to [0, 1].

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • ChannelWiseThresholdWithNormalize_channels – (list/tuple or None) A list of specified channels for thresholding. If None (by default), all the channels will be affected by this transform.

  • ChannelWiseThresholdWithNormalize_threshold_lower – (list/tuple or None) The lower threshold for the given channels.

  • ChannelWiseThresholdWithNormalize_threshold_upper – (list/tuple or None) The upper threshold for the given channels.

  • ChannelWiseThresholdWithNormalize_mean_std_mode – (bool) If True, using mean and std for normalization. If False, using min and max values for normalization.

  • ChannelWiseThresholdWithNormalize_inverse – (optional, bool) Is inverse transform needed for inference. Default is False.

pymic.transform.trans_dict module

The built-in transforms in PyMIC are:

'ChannelWiseThreshold': ChannelWiseThreshold,
'ChannelWiseThresholdWithNormalize': ChannelWiseThresholdWithNormalize,
'CropWithBoundingBox': CropWithBoundingBox,
'CenterCrop': CenterCrop,
'GrayscaleToRGB': GrayscaleToRGB,
'GammaCorrection': GammaCorrection,
'GaussianNoise': GaussianNoise,
'LabelConvert': LabelConvert,
'LabelConvertNonzero': LabelConvertNonzero,
'LabelToProbability': LabelToProbability,
'NormalizeWithMeanStd': NormalizeWithMeanStd,
'NormalizeWithMinMax': NormalizeWithMinMax,
'NormalizeWithPercentiles': NormalizeWithPercentiles,
'PartialLabelToProbability':PartialLabelToProbability,
'RandomCrop': RandomCrop,
'RandomResizedCrop': RandomResizedCrop,
'RandomRescale': RandomRescale,
'RandomFlip': RandomFlip,
'RandomRotate': RandomRotate,
'ReduceLabelDim': ReduceLabelDim,
'Rescale': Rescale,
'Pad': Pad.

Module contents