viscy-transforms¶
Image transforms for virtual staining microscopy.
What's here
Normalization, noise, and percentile-scaling transforms plus MONAI-compatible wrappers — composable into training and inference pipelines.
API reference¶
BatchedCenterSpatialCrop
¶
Bases: CenterSpatialCrop
Batched version of CenterSpatialCrop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roi_size
|
Sequence[int] | int
|
Expected ROI size to crop. e.g. [224, 224, 128]. If int, same size used for all dimensions. |
required |
__call__(img, lazy=None)
¶
Apply batched center spatial crop to input tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Tensor
|
Input tensor of shape (B, C, H, W, D) or (B, C, H, W). |
required |
lazy
|
bool | None
|
Not used in batched version. Default is None. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Cropped tensor with same batch size and consistent crop size across batch. |
BatchedCenterSpatialCropd
¶
Bases: Cropd
Batched version of CenterSpatialCropd.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str]
|
Keys to pick data for transformation. |
required |
roi_size
|
Sequence[int] | int
|
Expected ROI size to crop. e.g. [224, 224, 128]. If int, same size used for all dimensions. |
required |
allow_missing_keys
|
bool
|
Don't raise exception if key is missing. Default is False. |
False
|
BatchedChannelWiseZReduction
¶
Reduce the Z dimension of a (B, C, Z, Y, X) tensor.
Label-free samples get the center z-slice; fluorescence samples get a max-intensity projection (MIP). A per-sample boolean mask selects the strategy when the batch mixes both types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default_strategy
|
str
|
Strategy when no mask is provided: |
'mip'
|
__call__(img, is_labelfree=None)
¶
Apply z-reduction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Tensor
|
Shape |
required |
is_labelfree
|
Tensor or None
|
Boolean tensor of shape |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Shape |
BatchedChannelWiseZReductiond
¶
Bases: MapTransform
Dict transform that applies channel-wise Z-reduction.
In bag-of-channels mode each sample may represent a different channel.
The transform reads a _is_labelfree boolean tensor from the data dict
(injected by the datamodule) to decide per-sample strategy.
In all-channels mode the dict keys identify channel type. Pass
labelfree_keys to specify which keys should use center-slice; all
others get MIP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
KeysCollection
|
Keys of the image tensors to transform. |
required |
labelfree_keys
|
list[str] or None
|
Channel keys that should use center-slice (all-channels mode).
When set, |
None
|
default_strategy
|
str
|
Fallback strategy when neither |
'mip'
|
allow_missing_keys
|
bool
|
If |
False
|
BatchedDivisibleCropd
¶
Bases: MapTransform
Center-crop spatial dimensions to the nearest smaller multiple of k.
Useful for validation where the FOV may not be divisible by the model's downsampling factor. Computes the crop size from the input shape at call time — no hardcoded spatial dimensions needed in the config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str]
|
Keys to pick data for transformation. |
required |
k
|
int or Sequence[int]
|
Divisibility factor per spatial dimension. If int, same factor for all spatial dims. |
required |
allow_missing_keys
|
bool
|
Don't raise exception if key is missing. Default is False. |
False
|
BatchedRand3DElasticd
¶
Bases: MapTransform, RandomizableTransform
Randomly apply 3D elastic deformation to batched data.
Simulates natural tissue deformations commonly seen in biological structures. Uses a smoothed random displacement field to warp the input volume while preserving local structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys of the data dictionary to apply deformation to. |
required |
sigma_range
|
tuple[float, float]
|
Range for the Gaussian smoothing sigma applied to the displacement field. Higher values produce smoother, more gradual deformations. |
required |
magnitude_range
|
tuple[float, float]
|
Range for the displacement magnitude. Higher values produce stronger deformations. |
required |
prob
|
float
|
Probability of applying the transform to the entire batch. When triggered, all samples in the batch are deformed with independent random fields. Default: 0.1. |
0.1
|
mode
|
str
|
Interpolation mode for grid sampling. Options: "bilinear", "nearest". Default: "bilinear". |
'bilinear'
|
padding_mode
|
str
|
Padding mode for out-of-bounds values. Options: "zeros", "border", "reflection". Default: "reflection". |
'reflection'
|
allow_missing_keys
|
bool
|
Whether to allow missing keys in the data dictionary. Default: False. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with deformed tensors for specified keys. |
Examples:
>>> elastic = BatchedRand3DElasticd(
... keys=["image", "label"],
... sigma_range=(3.0, 5.0),
... magnitude_range=(50.0, 100.0),
... prob=0.5,
... )
>>> sample = {"image": torch.randn(2, 1, 32, 64, 64)}
>>> output = elastic(sample)
__call__(sample)
¶
Apply elastic deformation to the sample.
The displacement field and sampling grid are generated once from the first key and reused for all keys so that paired inputs (e.g. source/target) receive identical spatial deformations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
dict[str, Tensor]
|
Dictionary containing tensors with shape (B, C, D, H, W). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with deformed tensors for specified keys. |
BatchedRandAdjustContrast
¶
Bases: RandomizableTransform
Randomly adjust contrast of a batch of images using gamma transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gamma
|
tuple[float, float] | float
|
Range of gamma values for contrast adjustment. If tuple, samples between min and max. If single float, uses that exact value. Must be positive. Default is (0.5, 4.5). |
(0.5, 4.5)
|
prob
|
float
|
Probability of applying the transform to each sample. Default is 0.1. |
0.1
|
invert_image
|
bool
|
Whether to invert the image before applying gamma correction. Default is False. |
False
|
retain_stats
|
bool
|
If True, applies scaling and offset to maintain original mean and std. Default is False. |
False
|
__call__(data, randomize=True)
¶
Apply contrast adjustment to batch of images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Tensor
|
Input batch with shape (B, C, D, H, W) or (B, C, H, W). |
required |
randomize
|
bool
|
Whether to randomize parameters. Default is True. |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Contrast-adjusted batch with same shape as input. |
BatchedRandAdjustContrastd
¶
Bases: MapTransform, RandomizableTransform
Dictionary version of BatchedRandAdjustContrast.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys in the dictionary to apply the transform to. |
required |
gamma
|
tuple[float, float] | float
|
Range of gamma values for contrast adjustment. If tuple, samples between min and max. If single float, uses that exact value. Must be positive. Default is (0.5, 4.5). |
(0.5, 4.5)
|
prob
|
float
|
Probability of applying the transform to each sample. Default is 0.1. |
0.1
|
invert_image
|
bool
|
Whether to invert images before applying gamma correction. Default is False. |
False
|
retain_stats
|
bool
|
If True, applies scaling and offset to maintain original mean and std. Default is False. |
False
|
allow_missing_keys
|
bool
|
Whether to allow missing keys in the dictionary. Default is False. |
False
|
__call__(sample)
¶
Apply contrast adjustment to dictionary data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
dict[str, Tensor]
|
Dictionary containing tensors to transform. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with transformed tensors. |
BatchedRandAffined
¶
Bases: MapTransform
Randomly apply 3D affine transformations using Kornia.
GPU-optimized affine transform using Kornia's RandomAffine3D for batched data. Supports rotation, shearing, translation, and scaling with efficient GPU execution.
Random parameters are generated once per __call__ and reused
across all keys so that paired inputs (e.g. source/target) receive
identical spatial transforms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys of the data dictionary to transform. |
required |
prob
|
float
|
Probability of applying the transform. Default: 0.1. |
0.1
|
rotate_range
|
Sequence[tuple[float, float] | float] | float | None
|
Rotation angle range in radians per axis in (Z, Y, X) order. Reversed to Kornia's (X, Y, Z) order and converted to degrees. Default: None. |
None
|
shear_range
|
Sequence[float] | Sequence[tuple[float, float]] | tuple[float, float] | None
|
Shear range in degrees. Three forms:
Default: None (no shearing). |
None
|
translate_range
|
Sequence[tuple[float, float] | float] | float | None
|
Translation range as a fraction of image size per axis in (Z, Y, X) order. Reversed to Kornia's (X, Y, Z) order. Default: None. |
None
|
scale_range
|
tuple[float, float] | Sequence[tuple[float, float]] | None
|
Scale factor range (absolute, not offset from 1.0). Two forms:
Default: None (no scaling). |
None
|
isotropic_scale
|
bool
|
When True and |
False
|
scale_z_shear
|
bool
|
When True and |
True
|
mode
|
str
|
Interpolation mode. Default: "bilinear". |
'bilinear'
|
padding_mode
|
str
|
Padding mode for areas outside the rotated image boundary.
Use |
'zeros'
|
safe_crop_size
|
Sequence[int] | None
|
ZYX size of the downstream center crop. When set, the sampled scale is clamped so that the rotated source covers this crop region, reducing zero-corner artifacts. The per-sample lower bound on Kornia scale is:
where |
None
|
safe_crop_coverage
|
float
|
Fraction of the |
1.0
|
allow_missing_keys
|
bool
|
Whether to allow missing keys. Default: False. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with transformed tensors for specified keys. |
Notes
Rotation, shear, and per-axis scale parameter ordering follows MONAI
convention (Z, Y, X) but is internally converted to Kornia's convention
(X, Y, Z). A flat scale_range=(min, max) is not axis-inverted.
See Also
kornia.augmentation.RandomAffine3D : Underlying Kornia transform.
__call__(sample)
¶
Apply random affine transformation to specified keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
dict[str, Tensor]
|
Dictionary containing tensors with shape (B, C, D, H, W). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with transformed tensors for specified keys. |
BatchedRandFlip
¶
Bases: RandomizableTransform
Randomly flip a batch of images along specified spatial axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spatial_axes
|
int | Sequence[int]
|
The spatial axes along which to flip the images. |
required |
prob
|
float
|
The probability of applying the flip. |
0.5
|
BatchedRandFlipd
¶
Bases: MapTransform, RandomizableTransform
Apply random flips to batched data.
This transform applies random flips along specified spatial axes to batched data with shape [B, C, D, H, W].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
list
|
Keys to apply flipping to. |
required |
spatial_axes
|
list of int
|
List of spatial axes to randomly flip (0=D, 1=H, 2=W). Default is [0, 1, 2]. |
[0, 1, 2]
|
prob
|
float
|
Probability of applying each flip. Default is 0.5. |
0.5
|
allow_missing_keys
|
bool
|
Whether to allow missing keys. Default is False. |
False
|
BatchedRandGaussianNoise
¶
Bases: RandGaussianNoiseTensor
Add Gaussian noise to batched tensors with per-sample randomization.
GPU-optimized noise transform that applies independent noise decisions and noise realizations to each sample in the batch. Shares a single noise pattern across selected samples for memory efficiency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prob
|
float
|
Probability of applying noise to each sample. Inherited. |
required |
mean
|
float
|
Mean of the Gaussian distribution. Inherited from parent class. |
required |
std
|
float
|
Standard deviation (or max std if sample_std=True). Inherited. |
required |
dtype
|
type
|
Output data type. Inherited from parent class. |
required |
sample_std
|
bool
|
If True, samples std uniformly from [0, std] per batch. Inherited. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Input tensor with added Gaussian noise for selected samples. |
Notes
Unlike the single-sample version, this transform makes independent random decisions for each sample in the batch, enabling efficient GPU training with diverse augmentations across the batch.
See Also
RandGaussianNoiseTensor : Single-sample version. BatchedRandGaussianNoised : Dictionary wrapper for this transform.
__call__(img, mean=None, randomize=True)
¶
Add Gaussian noise to the input tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Tensor
|
Input tensor with shape (B, C, D, H, W). |
required |
mean
|
float | None
|
Override mean of noise distribution. If None, uses instance mean. |
None
|
randomize
|
bool
|
Whether to randomize noise parameters. Default: True. |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Input tensor with added noise for selected batch samples. |
BatchedRandGaussianNoised
¶
Bases: RandGaussianNoiseTensord
Dictionary wrapper for batched Gaussian noise transform.
GPU-optimized noise transform for dictionary data with per-sample randomization across the batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys of the data dictionary to add noise to. |
required |
prob
|
float
|
Probability of applying noise to each sample. Default: 0.1. |
0.1
|
mean
|
float
|
Mean of the Gaussian distribution. Default: 0.0. |
0.0
|
std
|
float
|
Standard deviation (or max std if sample_std=True). Default: 0.1. |
0.1
|
dtype
|
type
|
Output data type. Default: np.float32. |
float32
|
allow_missing_keys
|
bool
|
Whether to allow missing keys in data dictionary. Default: False. |
False
|
sample_std
|
bool
|
If True, samples std uniformly from [0, std] per batch. Default: True. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with noisy tensors for specified keys. |
See Also
BatchedRandGaussianNoise : Underlying batched noise transform. RandGaussianNoiseTensord : Single-sample version.
BatchedRandGaussianSmooth
¶
Bases: RandomizableTransform
Randomly apply 3D Gaussian blur to a batch of images.
Uses separable 3D filtering for efficiency. Dimensions with sigma=0.0 are skipped to avoid NaN values from degenerate Gaussian kernels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sigma_x
|
tuple[float, float] | float
|
Standard deviation range for x-axis blur. If tuple, samples between min and max. If float, uses fixed value. Use 0.0 to skip filtering in this dimension. |
(1.0, 3.0)
|
sigma_y
|
tuple[float, float] | float
|
Standard deviation range for y-axis blur. If tuple, samples between min and max. If float, uses fixed value. Use 0.0 to skip filtering in this dimension. |
(1.0, 3.0)
|
sigma_z
|
tuple[float, float] | float
|
Standard deviation range for z-axis blur. If tuple, samples between min and max. If float, uses fixed value. Use 0.0 to skip filtering in this dimension. |
(1.0, 3.0)
|
truncated
|
float
|
Factor for automatic kernel size estimation: size = sigma * truncated. Default is 4.0. |
4.0
|
prob
|
float
|
Probability of applying the transform. Default is 0.5. |
0.5
|
border_type
|
str
|
Border mode for padding. Default is "constant" to match MONAI. |
'constant'
|
BatchedRandGaussianSmoothd
¶
Bases: MapTransform, RandomizableTransform
Apply random Gaussian blur to dictionary data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys to apply the transform to. |
required |
sigma_x
|
tuple[float, float] | float
|
Standard deviation range for x-axis blur. |
(1.0, 3.0)
|
sigma_y
|
tuple[float, float] | float
|
Standard deviation range for y-axis blur. |
(1.0, 3.0)
|
sigma_z
|
tuple[float, float] | float
|
Standard deviation range for z-axis blur. |
(1.0, 3.0)
|
truncated
|
float
|
Factor for automatic kernel size estimation. Default is 4.0. |
4.0
|
prob
|
float
|
Probability of applying the transform. Default is 0.5. |
0.5
|
border_type
|
str
|
Border mode for padding. Default is "constant" to match MONAI. |
'constant'
|
allow_missing_keys
|
bool
|
Whether to allow missing keys. Default is False. |
False
|
BatchedRandHistogramShiftd
¶
Bases: MapTransform, RandomizableTransform
Randomly shift intensity histogram by adding a constant offset.
Simulates global intensity variations that can occur in microscopy due to illumination changes, photobleaching, or detector drift. Each sample in the batch receives an independently sampled shift.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys of the data dictionary to apply the shift to. |
required |
shift_range
|
tuple[float, float]
|
Range for the intensity shift value. Samples uniformly between min and max values. Default: (-0.1, 0.1). |
(-0.1, 0.1)
|
prob
|
float
|
Probability of applying the transform. Default: 0.1. |
0.1
|
allow_missing_keys
|
bool
|
Whether to allow missing keys in the data dictionary. Default: False. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with intensity-shifted tensors for specified keys. |
Examples:
>>> shift = BatchedRandHistogramShiftd(
... keys=["image"],
... shift_range=(-0.2, 0.2),
... prob=0.5,
... )
>>> sample = {"image": torch.randn(2, 1, 32, 64, 64)}
>>> output = shift(sample)
__call__(sample)
¶
Apply histogram shift to the sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
dict[str, Tensor]
|
Dictionary containing tensors with shape (B, C, D, H, W). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with intensity-shifted tensors for specified keys. |
BatchedRandInvertIntensityd
¶
Bases: MapTransform, RandomizableTransform
Randomly invert intensity per sample in a batch.
For each sample in the batch, independently decides whether to
negate the tensor values. Uses MONAI-style randomize() to
generate per-sample decisions, matching the pattern in
:class:~viscy_transforms.BatchedRandAdjustContrastd.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys of the data dictionary to potentially invert. |
required |
prob
|
float
|
Probability of applying inversion per sample. Default: 0.1. |
0.1
|
allow_missing_keys
|
bool
|
Whether to allow missing keys. Default: False. |
False
|
__call__(sample)
¶
Randomly invert intensities with per-sample randomization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
Sample
|
Dictionary with batched tensors |
required |
Returns:
| Type | Description |
|---|---|
Sample
|
Dictionary with potentially inverted tensors. |
randomize(data)
¶
Generate per-sample inversion decisions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Tensor
|
Reference tensor to determine batch size and device. |
required |
BatchedRandLocalPixelShufflingd
¶
Bases: MapTransform, RandomizableTransform
Randomly shuffle pixels within local patches for texture augmentation.
Performs local pixel permutation within small 3D patches throughout the volume. This can be used as a data augmentation technique or as a pretext task for self-supervised learning to encourage models to learn local texture features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys of the data dictionary to apply shuffling to. |
required |
patch_size
|
int
|
Size of the cubic patch for local shuffling. Pixels within each patch are randomly permuted. Default: 3. |
3
|
shuffle_prob
|
float
|
Fraction of the volume to shuffle, controlling the number of patches selected for shuffling. Default: 0.1. |
0.1
|
prob
|
float
|
Probability of applying the transform to each sample. Default: 0.1. |
0.1
|
allow_missing_keys
|
bool
|
Whether to allow missing keys in the data dictionary. Default: False. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with shuffled tensors for specified keys. |
Examples:
>>> shuffle = BatchedRandLocalPixelShufflingd(
... keys=["image"],
... patch_size=5,
... shuffle_prob=0.2,
... prob=0.5,
... )
>>> sample = {"image": torch.randn(2, 1, 32, 64, 64)}
>>> output = shuffle(sample)
__call__(sample)
¶
Apply local pixel shuffling to the sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
dict[str, Tensor]
|
Dictionary containing tensors with shape (B, C, D, H, W). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with shuffled tensors for specified keys. |
BatchedRandScaleIntensity
¶
Bases: RandomizableTransform
Randomly scale intensity of a batch of images.
The transform multiplies the input by (1 + factor), where factor is randomly sampled from the specified range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factors
|
tuple[float, float] | float
|
Range of scaling factors. If tuple, samples between (min, max). If single float, uses range (-factors, +factors). Default is 0.1. |
0.1
|
prob
|
float
|
Probability of applying the transform to each sample. Default is 0.1. |
0.1
|
channel_wise
|
bool
|
If True, scale each channel separately. Default is False. |
False
|
__call__(data, randomize=True)
¶
Apply intensity scaling to batch of images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Tensor
|
Input batch with shape (B, C, D, H, W) or (B, C, H, W). |
required |
randomize
|
bool
|
Whether to randomize parameters. Default is True. |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Intensity-scaled batch with same shape as input. |
BatchedRandScaleIntensityd
¶
Bases: MapTransform, RandomizableTransform
Dictionary version of BatchedRandScaleIntensity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys in the dictionary to apply the transform to. |
required |
factors
|
tuple[float, float] | float
|
Range of scaling factors. If tuple, samples between (min, max). If single float, uses range (-factors, +factors). Default is 0.1. |
0.1
|
prob
|
float
|
Probability of applying the transform to each sample. Default is 0.1. |
0.1
|
channel_wise
|
bool
|
If True, scale each channel separately. Default is False. |
False
|
allow_missing_keys
|
bool
|
Whether to allow missing keys in the dictionary. Default is False. |
False
|
__call__(sample)
¶
Apply intensity scaling to dictionary data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
dict[str, Tensor]
|
Dictionary containing tensors to transform. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with transformed tensors. |
BatchedRandSharpend
¶
Bases: MapTransform, RandomizableTransform
Randomly apply sharpening to batched 3D microscopy images.
Uses unsharp masking with a 3D Laplacian kernel to enhance edges and fine details in the image. The sharpening strength is controlled by alpha blending between the original and sharpened image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys of the data dictionary to apply sharpening to. |
required |
alpha_range
|
tuple[float, float]
|
Range for the sharpening strength factor. Samples uniformly between min and max values. Higher values produce stronger sharpening. Default: (0.1, 0.5). |
(0.1, 0.5)
|
prob
|
float
|
Probability of applying the transform to each key. Default: 0.1. |
0.1
|
allow_missing_keys
|
bool
|
Whether to allow missing keys in the data dictionary. Default: False. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with sharpened tensors for specified keys. |
Notes
The transform uses a 3x3x3 Laplacian kernel for edge detection. The kernel is cached for efficiency across multiple calls.
Examples:
>>> sharpen = BatchedRandSharpend(keys=["image"], alpha_range=(0.2, 0.4))
>>> sample = {"image": torch.randn(2, 1, 32, 64, 64)}
>>> output = sharpen(sample)
__call__(sample)
¶
Apply sharpening to the sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
dict[str, Tensor]
|
Dictionary containing tensors with shape (B, C, D, H, W). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with sharpened tensors for specified keys. |
BatchedRandSpatialCrop
¶
Bases: RandSpatialCrop
Batched version of RandSpatialCrop that applies random spatial cropping to a batch of images.
Each image in the batch gets its own random crop parameters. When random_size=True, all crops use the same randomly chosen size to ensure consistent output tensor shapes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roi_size
|
Sequence[int] | int
|
Expected ROI size to crop. e.g. [224, 224, 128]. If int, same size used for all dimensions. |
required |
max_roi_size
|
Sequence[int] | int | None
|
Maximum ROI size when random_size=True. If None, defaults to input image size. |
None
|
random_center
|
bool
|
Whether to crop at random position (True) or image center (False). Default is True. |
True
|
random_size
|
bool
|
Not supported in batched mode, must be False. |
False
|
__call__(img, randomize=True, lazy=None)
¶
Apply batched random spatial crop to input tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Tensor
|
Input tensor of shape (B, C, H, W, D) or (B, C, H, W). |
required |
randomize
|
bool
|
Whether to generate new random parameters. Default is True. |
True
|
lazy
|
bool | None
|
Not used in batched version. Default is None. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Cropped tensor with same batch size. When random_size=True, all crops use the same randomly chosen size to ensure consistent output shapes. |
randomize(img_size)
¶
Generate random crop parameters for each image in the batch.
BatchedRandWeightedCropd
¶
Bases: MapTransform
Randomly crop regions weighted by a spatial importance map.
Samples crop positions with probability proportional to the weight map values, then extracts crops at those positions. Each sample in the batch gets an independently sampled crop position. All keys share the same crop coordinates so paired inputs (e.g. source/target) remain aligned.
The weight map is reduced to (B, Y, X) by summing over all channels
and Z slices. For single-channel targets (the common case), this is
equivalent to using the channel directly. For multi-channel targets, all
channels contribute equally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to crop. |
required |
w_key
|
str
|
Key of the weight map tensor in the data dictionary. |
required |
spatial_size
|
Sequence[int]
|
Size of the crop region as |
required |
allow_missing_keys
|
bool
|
Whether to skip missing keys. Default: False. |
False
|
__call__(data)
¶
Apply weighted spatial crop to all specified keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Tensor]
|
Dictionary with tensors of shape |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with cropped tensors. |
BatchedRandZStackShiftd
¶
Bases: MapTransform, RandomizableTransform
Randomly shift Z-stack slices along the depth axis.
Simulates focal plane drift or sample movement that can occur during 3D microscopy acquisition. Each sample receives an independent random shift in the Z (depth) dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys of the data dictionary to apply the shift to. |
required |
max_shift
|
int
|
Maximum number of slices to shift in either direction. The actual shift is sampled uniformly from [-max_shift, max_shift]. Default: 3. |
3
|
prob
|
float
|
Probability of applying the transform. Default: 0.1. |
0.1
|
mode
|
str
|
Padding mode for empty slices after shifting. Currently only "constant" is implemented. Default: "constant". |
'constant'
|
cval
|
float
|
Constant value used for padding when mode="constant". Default: 0.0. |
0.0
|
allow_missing_keys
|
bool
|
Whether to allow missing keys in the data dictionary. Default: False. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with Z-shifted tensors for specified keys. |
Examples:
>>> zshift = BatchedRandZStackShiftd(
... keys=["image"],
... max_shift=5,
... prob=0.5,
... )
>>> sample = {"image": torch.randn(2, 1, 32, 64, 64)}
>>> output = zshift(sample)
__call__(sample)
¶
Apply Z-stack shift to the sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
dict[str, Tensor]
|
Dictionary containing tensors with shape (B, C, D, H, W). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with Z-shifted tensors for specified keys. |
BatchedScaleIntensityRangePercentiles
¶
Bases: ScaleIntensityRangePercentiles
Scale batched tensor intensity based on percentile range.
GPU-optimized version of MONAI's ScaleIntensityRangePercentiles that operates on batched data. Computes percentiles across spatial dimensions for each sample in the batch independently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lower
|
float
|
Lower percentile for input range (0-100). |
required |
upper
|
float
|
Upper percentile for input range (0-100). |
required |
b_min
|
float | None
|
Minimum value of output range. None to skip scaling. |
required |
b_max
|
float | None
|
Maximum value of output range. None to skip scaling. |
required |
clip
|
bool
|
Whether to clip output values to [b_min, b_max]. Default: False. |
required |
relative
|
bool
|
Whether to compute relative percentile range. Default: False. |
required |
channel_wise
|
bool
|
Whether to compute percentiles per channel. Default: False. |
required |
dtype
|
DTypeLike
|
Output data type. Default: np.float32. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scaled tensor with normalized intensity values. |
See Also
monai.transforms.ScaleIntensityRangePercentiles : Parent MONAI transform. BatchedScaleIntensityRangePercentilesd : Dictionary wrapper.
__call__(img)
¶
Scale the input tensor based on percentile range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Tensor
|
Input tensor with shape (B, C, D, H, W). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scaled tensor with normalized intensity values. |
BatchedScaleIntensityRangePercentilesd
¶
Bases: MapTransform
Dictionary wrapper for batched percentile intensity scaling.
Applies BatchedScaleIntensityRangePercentiles to specified keys in a data dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys of the data dictionary to scale. |
required |
lower
|
float
|
Lower percentile for input range (0-100). |
required |
upper
|
float
|
Upper percentile for input range (0-100). |
required |
b_min
|
float | None
|
Minimum value of output range. |
required |
b_max
|
float | None
|
Maximum value of output range. |
required |
clip
|
bool
|
Whether to clip output values. Default: False. |
False
|
relative
|
bool
|
Whether to compute relative percentile range. Default: False. |
False
|
channel_wise
|
bool
|
Whether to compute percentiles per channel. Default: False. |
False
|
dtype
|
DTypeLike
|
Output data type. Default: np.float32. |
float32
|
allow_missing_keys
|
bool
|
Whether to allow missing keys. Default: False. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with scaled tensors for specified keys. |
See Also
BatchedScaleIntensityRangePercentiles : Underlying transform.
__call__(data)
¶
Scale intensity of specified keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Tensor]
|
Dictionary containing tensors with shape (B, C, D, H, W). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with scaled tensors for specified keys. |
BatchedStackChannelsd
¶
Bases: StackChannelsd
Stack source and target channels from batched tensors.
Like :class:StackChannelsd but for batched (B, C, Z, Y, X)
tensors where concatenation is along dim=1 (channel) instead
of dim=0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel_map
|
ChannelMap
|
Dictionary mapping output keys to lists of input channel keys.
Example: |
required |
BatchedZoom
¶
Bases: Transform
Zoom (resize) a batched tensor by a scale factor.
Uses torch.nn.functional.interpolate for GPU-efficient resizing
of batched 3D data. Supports various interpolation modes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scale_factor
|
float | tuple[float, float, float]
|
Multiplier for spatial size. If float, same factor is used for all dimensions. If tuple, specifies (depth, height, width) factors. |
required |
mode
|
str
|
Interpolation algorithm. Options: - "nearest": Nearest neighbor interpolation - "nearest-exact": Exact nearest neighbor - "linear": Linear interpolation (1D) - "bilinear": Bilinear interpolation (2D) - "bicubic": Bicubic interpolation (2D) - "trilinear": Trilinear interpolation (3D) - "area": Area interpolation |
required |
align_corners
|
bool | None
|
If True, aligns corners of input and output tensors. Only applicable for linear, bilinear, bicubic, trilinear modes. Default: None. |
None
|
recompute_scale_factor
|
bool | None
|
If True, recomputes scale_factor for use in interpolation. Default: None. |
None
|
antialias
|
bool
|
If True, applies anti-aliasing when downsampling. Only effective for bilinear and bicubic modes. Default: False. |
False
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Resized tensor with scaled spatial dimensions. |
Examples:
>>> zoom = BatchedZoom(scale_factor=0.5, mode="trilinear")
>>> x = torch.randn(2, 1, 32, 64, 64) # [B, C, D, H, W]
>>> y = zoom(x)
>>> y.shape
torch.Size([2, 1, 16, 32, 32])
__call__(sample)
¶
Zoom the input tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
Tensor
|
Input tensor with shape (B, C, D, H, W). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Resized tensor with scaled spatial dimensions. |
BatchedZoomd
¶
Bases: MapTransform
Dictionary wrapper for BatchedZoom transform.
Applies zoom (resize) to specified keys in a data dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str]
|
Keys of the data dictionary to apply zoom to. |
required |
scale_factor
|
float | tuple[float, float, float]
|
Multiplier for spatial size. If float, same factor is used for all dimensions. If tuple, specifies (depth, height, width) factors. |
required |
mode
|
str
|
Interpolation algorithm. See :class: |
required |
align_corners
|
bool | None
|
If True, aligns corners of input and output tensors. Default: None. |
None
|
recompute_scale_factor
|
bool | None
|
If True, recomputes scale_factor for interpolation. Default: None. |
None
|
antialias
|
bool
|
If True, applies anti-aliasing when downsampling. Default: False. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with zoomed tensors for specified keys. |
See Also
BatchedZoom : Underlying zoom transform.
Examples:
>>> zoom = BatchedZoomd(keys=["image"], scale_factor=2.0, mode="trilinear")
>>> sample = {"image": torch.randn(2, 1, 16, 32, 32)}
>>> output = zoom(sample)
>>> output["image"].shape
torch.Size([2, 1, 32, 64, 64])
__call__(data)
¶
Apply zoom to the specified keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Tensor]
|
Dictionary containing tensors with shape (B, C, D, H, W). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with zoomed tensors for specified keys. |
CenterSpatialCropd
¶
Bases: CenterSpatialCropd
Crop a region of specified size from the center of the input.
Wrapper around MONAI's CenterSpatialCropd transform with explicit constructor signature for jsonargparse compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to crop. |
required |
roi_size
|
Sequence[int] | int
|
Size of the crop region. If int, applies same size to all dimensions. |
required |
**kwargs
|
Additional arguments passed to the parent class. |
{}
|
See Also
monai.transforms.CenterSpatialCropd : Parent MONAI transform. RandSpatialCropd : Random position cropping.
Decollate
¶
Bases: Transform
Decollate a batched tensor into a list of individual samples.
This transform splits a batched tensor along the batch dimension, returning a list of individual sample tensors. Useful for applying per-sample post-processing operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
detach
|
bool
|
Whether to detach tensors from the computation graph before decollating. Default: True. |
True
|
pad_batch
|
bool
|
Whether to pad smaller tensors to match the batch size when samples have different sizes. Default: True. |
True
|
fill_value
|
float | None
|
Value used for padding when pad_batch is True and samples have different sizes. Default: None (use zeros). |
None
|
Returns:
| Type | Description |
|---|---|
list[Tensor]
|
List of individual sample tensors, one per batch element. |
Examples:
>>> decollate = Decollate(detach=True)
>>> batch = torch.randn(4, 1, 32, 64, 64) # [B, C, D, H, W]
>>> samples = decollate(batch)
>>> len(samples)
4
>>> samples[0].shape
torch.Size([1, 32, 64, 64])
See Also
monai.data.decollate_batch : Underlying MONAI function. Decollated : Dictionary-based version for keyed data.
__call__(data)
¶
Decollate a batched tensor into individual samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Tensor
|
Batched input tensor with shape (B, ...) where B is batch size. |
required |
Returns:
| Type | Description |
|---|---|
list[Tensor]
|
List of B tensors, each with shape (...). |
Decollated
¶
Bases: Decollated
is_spatial = False
class-attribute
instance-attribute
¶
Decollate batch data back into a list of samples.
Wrapper around MONAI's Decollated transform with explicit constructor signature for jsonargparse compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to decollate. |
required |
detach
|
bool
|
Whether to detach tensors from the computation graph. Default: True. |
required |
pad_batch
|
bool
|
Whether to pad smaller tensors to match the batch size. Default: True. |
required |
fill_value
|
float | None
|
Value used for padding when pad_batch is True. Default: None. |
required |
**kwargs
|
Additional arguments passed to the parent class. |
required |
See Also
monai.transforms.Decollated : Parent MONAI transform.
MinMaxSampled
¶
Bases: MapTransform
is_spatial = False
class-attribute
instance-attribute
¶
Normalize to [-1, 1] by clipping then rescaling with precomputed range statistics.
Applies::
x_clipped = clamp(x, low, high)
x_norm = 2 * (x_clipped - low) / (high - low) - 1
where low and high are determined by data_range.
Expects norm_meta to have structure::
{channel_label: {level: {stat_name: Tensor, ...}, ...}, ...}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys to normalize. |
required |
level
|
(fov_statistics, dataset_statistics, timepoint_statistics)
|
Level of normalization. |
'fov_statistics'
|
data_range
|
(min_max, p1_p99, p5_p95)
|
Statistic pair to use as |
'min_max'
|
remove_meta
|
bool
|
Whether to remove metadata after normalization, defaults to False. |
required |
NormalizeIntensityd
¶
Bases: NormalizeIntensityd
is_spatial = False
class-attribute
instance-attribute
¶
Normalize intensity values using mean and standard deviation.
Wrapper around MONAI's NormalizeIntensityd transform with explicit constructor signature for jsonargparse compatibility. Computes z-score normalization: (x - mean) / std.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to normalize. |
required |
**kwargs
|
Additional arguments passed to the parent class, including: - subtrahend : float | None Value to subtract (overrides computed mean). - divisor : float | None Value to divide by (overrides computed std). - nonzero : bool Whether to compute statistics only on non-zero values. - channel_wise : bool Whether to normalize each channel independently. |
required |
See Also
monai.transforms.NormalizeIntensityd : Parent MONAI transform. NormalizeSampled : Custom normalization using precomputed statistics.
NormalizeSampled
¶
Bases: MapTransform
is_spatial = False
class-attribute
instance-attribute
¶
Normalize using precomputed statistics stored in sample["norm_meta"].
Expects norm_meta to have structure::
{channel_label: {level: {stat_name: Tensor, ...}, ...}, ...}
For timepoint_statistics, the dataset must pre-resolve the correct
timepoint so that the level value is {stat_name: Tensor} directly
(not nested by timepoint index).
Stats tensors may be scalar () or batched (B,).
_match_image reshapes them to broadcast against
(B, 1, Z, Y, X) image tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys to normalize. |
required |
level
|
(fov_statistics, dataset_statistics, timepoint_statistics)
|
Level of normalization. |
'fov_statistics'
|
subtrahend
|
str
|
Subtrahend for normalization, defaults to "mean". |
required |
divisor
|
str
|
Divisor for normalization, defaults to "std". |
required |
remove_meta
|
bool
|
Whether to remove metadata after normalization, defaults to False. |
required |
RandAdjustContrastd
¶
Bases: RandAdjustContrastd
is_spatial = False
class-attribute
instance-attribute
¶
Randomly adjust image contrast using gamma correction.
Wrapper around MONAI's RandAdjustContrastd transform with explicit constructor signature for jsonargparse compatibility. Applies the transformation: output = input^gamma.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to transform. |
required |
prob
|
float
|
Probability of applying the transform (0.0 to 1.0). |
required |
gamma
|
tuple[float, float] | float
|
Gamma value range. If tuple, samples uniformly between min and max. Values < 1 increase contrast, values > 1 decrease contrast. |
required |
**kwargs
|
Additional arguments passed to the parent class. |
required |
See Also
monai.transforms.RandAdjustContrastd : Parent MONAI transform.
RandAffined
¶
Bases: RandAffined
Apply random affine transformations to data.
Wrapper around MONAI's RandAffined transform with explicit constructor signature for jsonargparse compatibility. Applies rotation, shearing, and scaling transformations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to transform. |
required |
prob
|
float
|
Probability of applying the transform (0.0 to 1.0). |
required |
rotate_range
|
Sequence[float | Sequence[float]] | float
|
Range of rotation angles in radians for each axis. Can be a single value or (min, max) tuple per axis. |
required |
shear_range
|
Sequence[float | Sequence[float]] | float
|
Range of shear factors for each axis. Can be a single value or (min, max) tuple per axis. |
required |
scale_range
|
Sequence[float | Sequence[float]] | float
|
Range of scale factors for each axis. Can be a single value or (min, max) tuple per axis. |
required |
**kwargs
|
Additional arguments passed to the parent class, including: - mode : str Interpolation mode (e.g., "bilinear", "nearest"). - padding_mode : str Padding mode for out-of-bounds values. |
{}
|
See Also
monai.transforms.RandAffined : Parent MONAI transform.
RandFlipd
¶
Bases: RandFlipd
Randomly flip the input along specified spatial axes.
Wrapper around MONAI's RandFlipd transform with explicit constructor signature for jsonargparse compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to flip. |
required |
prob
|
float
|
Probability of applying the flip (0.0 to 1.0). |
required |
spatial_axis
|
Sequence[int] | int
|
Spatial axis or axes along which to flip. For 3D data: 0 = depth (Z), 1 = height (Y), 2 = width (X). |
required |
**kwargs
|
Additional arguments passed to the parent class. |
{}
|
See Also
monai.transforms.RandFlipd : Parent MONAI transform.
RandGaussianNoiseTensor
¶
Bases: RandGaussianNoise
Add Gaussian noise directly to PyTorch tensors.
Extends MONAI's RandGaussianNoise to generate noise on the same device as the input tensor, avoiding CPU-GPU data transfers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prob
|
float
|
Probability of applying noise. Inherited from parent class. |
required |
mean
|
float
|
Mean of the Gaussian distribution. Inherited from parent class. |
required |
std
|
float
|
Standard deviation (or max std if sample_std=True). Inherited. |
required |
dtype
|
type
|
Output data type. Inherited from parent class. |
required |
sample_std
|
bool
|
If True, samples std uniformly from [0, std]. Inherited. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Input tensor with added Gaussian noise. |
See Also
monai.transforms.RandGaussianNoise : Parent MONAI transform. BatchedRandGaussianNoise : Batched version with per-sample randomization.
RandGaussianNoiseTensord
¶
Bases: RandGaussianNoised
Dictionary wrapper for tensor-based Gaussian noise.
Applies RandGaussianNoiseTensor to specified keys in a data dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys of the data dictionary to add noise to. |
required |
prob
|
float
|
Probability of applying noise. Default: 0.1. |
0.1
|
mean
|
float
|
Mean of the Gaussian distribution. Default: 0.0. |
0.0
|
std
|
float
|
Standard deviation (or max std if sample_std=True). Default: 0.1. |
0.1
|
dtype
|
type
|
Output data type. Default: np.float32. |
float32
|
allow_missing_keys
|
bool
|
Whether to allow missing keys in data dictionary. Default: False. |
False
|
sample_std
|
bool
|
If True, samples std uniformly from [0, std]. Default: True. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with noisy tensors for specified keys. |
See Also
RandGaussianNoiseTensor : Underlying noise transform. BatchedRandGaussianNoised : Batched version for GPU efficiency.
RandGaussianNoised
¶
Bases: RandGaussianNoised
is_spatial = False
class-attribute
instance-attribute
¶
Randomly add Gaussian noise to image data.
Wrapper around MONAI's RandGaussianNoised transform with explicit constructor signature for jsonargparse compatibility. Adds noise sampled from a Gaussian distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to transform. |
required |
prob
|
float
|
Probability of applying the transform (0.0 to 1.0). |
required |
mean
|
float
|
Mean of the Gaussian noise distribution. |
required |
std
|
float
|
Standard deviation of the Gaussian noise distribution. |
required |
**kwargs
|
Additional arguments passed to the parent class. |
required |
See Also
monai.transforms.RandGaussianNoised : Parent MONAI transform. BatchedRandGaussianNoised : Batch-optimized version for GPU efficiency.
RandGaussianSmoothd
¶
Bases: RandGaussianSmoothd
is_spatial = False
class-attribute
instance-attribute
¶
Randomly apply Gaussian smoothing (blur) to image data.
Wrapper around MONAI's RandGaussianSmoothd transform with explicit constructor signature for jsonargparse compatibility. Applies Gaussian blur with independently sampled sigma values per axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to transform. |
required |
prob
|
float
|
Probability of applying the transform (0.0 to 1.0). |
required |
sigma_x
|
tuple[float, float] | float
|
Standard deviation range for x-axis blur. If tuple, samples uniformly between min and max. |
required |
sigma_y
|
tuple[float, float] | float
|
Standard deviation range for y-axis blur. If tuple, samples uniformly between min and max. |
required |
sigma_z
|
tuple[float, float] | float
|
Standard deviation range for z-axis blur. If tuple, samples uniformly between min and max. |
required |
**kwargs
|
Additional arguments passed to the parent class. |
required |
See Also
monai.transforms.RandGaussianSmoothd : Parent MONAI transform. BatchedRandGaussianSmoothd : Batch-optimized version for GPU efficiency.
RandInvertIntensityd
¶
Bases: MapTransform, RandomizableTransform
is_spatial = False
class-attribute
instance-attribute
¶
Randomly invert the intensity of the image.
Multiplies intensity values by -1 to invert the image contrast. Useful for augmentation in microscopy where structures may appear as bright or dark depending on imaging modality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys of the data dictionary to potentially invert. |
required |
prob
|
float
|
Probability of applying inversion. Default: 0.1. |
required |
allow_missing_keys
|
bool
|
Whether to allow missing keys in the data dictionary. Default: False. |
required |
Returns:
| Type | Description |
|---|---|
Sample
|
Dictionary with potentially inverted tensors for specified keys. |
__call__(sample)
¶
Randomly invert the sample intensities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
Sample
|
Dictionary containing tensors. |
required |
Returns:
| Type | Description |
|---|---|
Sample
|
Dictionary with potentially inverted tensors. |
RandScaleIntensityd
¶
Bases: RandScaleIntensityd
is_spatial = False
class-attribute
instance-attribute
¶
Randomly scale image intensity by a multiplicative factor.
Wrapper around MONAI's RandScaleIntensityd transform with explicit constructor signature for jsonargparse compatibility. Multiplies intensity values by a randomly sampled factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to transform. |
required |
factors
|
tuple[float, float] | float
|
Scale factor range. If tuple, samples uniformly between min and max. Factor of 1.0 means no change, < 1 darkens, > 1 brightens. |
required |
prob
|
float
|
Probability of applying the transform (0.0 to 1.0). |
required |
**kwargs
|
Additional arguments passed to the parent class. |
required |
See Also
monai.transforms.RandScaleIntensityd : Parent MONAI transform.
RandSpatialCropd
¶
Bases: RandSpatialCropd
Randomly crop a region of specified size from the input.
Wrapper around MONAI's RandSpatialCropd transform with explicit constructor signature for jsonargparse compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to crop. |
required |
roi_size
|
Sequence[int] | int
|
Size of the crop region. If int, applies same size to all dimensions. |
required |
random_center
|
bool
|
Whether to randomly select the crop center. If False, crops from the center of the input. Default: True. |
True
|
**kwargs
|
Additional arguments passed to the parent class. |
{}
|
See Also
monai.transforms.RandSpatialCropd : Parent MONAI transform. CenterSpatialCropd : Deterministic center cropping.
RandWeightedCropd
¶
Bases: RandWeightedCropd
Randomly crop regions weighted by a spatial importance map.
Wrapper around MONAI's RandWeightedCropd transform with explicit constructor signature for jsonargparse compatibility. Crops are sampled with probability proportional to the weight map values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to apply the crop to. |
required |
w_key
|
str
|
Key of the weight map in the data dictionary. |
required |
spatial_size
|
Sequence[int]
|
Size of the crop region as (D, H, W) for 3D data. |
required |
num_samples
|
int
|
Number of crop samples to generate per input. Default: 1. |
1
|
**kwargs
|
Additional arguments passed to the parent class. |
{}
|
See Also
monai.transforms.RandWeightedCropd : Parent MONAI transform.
ScaleIntensityRangePercentilesd
¶
Bases: ScaleIntensityRangePercentilesd
is_spatial = False
class-attribute
instance-attribute
¶
Scale intensity values based on percentile range.
Wrapper around MONAI's ScaleIntensityRangePercentilesd transform with explicit constructor signature for jsonargparse compatibility. Maps intensity values from a percentile-defined range to a target range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to transform. |
required |
lower
|
float
|
Lower percentile for input range (0-100). |
required |
upper
|
float
|
Upper percentile for input range (0-100). |
required |
b_min
|
float | None
|
Minimum value of output range. None to skip scaling. |
required |
b_max
|
float | None
|
Maximum value of output range. None to skip scaling. |
required |
clip
|
bool
|
Whether to clip output values to [b_min, b_max]. Default: False. |
required |
relative
|
bool
|
Whether to compute relative percentile range. Default: False. |
required |
channel_wise
|
bool
|
Whether to compute percentiles per channel. Default: False. |
required |
dtype
|
DTypeLike | None
|
Output data type. Default: None (preserve input dtype). |
required |
allow_missing_keys
|
bool
|
Whether to allow missing keys in data dictionary. Default: False. |
required |
See Also
monai.transforms.ScaleIntensityRangePercentilesd : Parent MONAI transform. BatchedScaleIntensityRangePercentilesd : Batch-optimized version.
StackChannelsd
¶
Bases: MapTransform
is_spatial = False
class-attribute
instance-attribute
¶
Stack source and target channels from multiple keys.
Combines multiple single-channel tensors into multi-channel tensors
based on a channel mapping configuration. Concatenates along dim=0
for per-sample (C, Z, Y, X) tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel_map
|
ChannelMap
|
Dictionary mapping output keys to lists of input channel keys. Example: {"source": ["phase", "bf"], "target": ["nuclei", "membrane"]} |
required |
Examples:
__call__(sample)
¶
Stack channels according to the channel map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
Sample
|
Dictionary containing single-channel tensors. |
required |
Returns:
| Type | Description |
|---|---|
Sample
|
Dictionary with stacked multi-channel tensors. |
TiledSpatialCropSamplesd
¶
Bases: MapTransform, MultiSampleTrait
is_spatial = True
class-attribute
instance-attribute
¶
Crop multiple tiled ROIs from an image.
Generates multiple non-overlapping crops arranged in a grid pattern. Used for deterministic cropping in validation to ensure reproducible evaluation across the full field of view.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
str | Iterable[str]
|
Keys of the data dictionary to crop. |
required |
roi_size
|
tuple[int, int, int]
|
Size of each crop region as (D, H, W). |
required |
num_samples
|
int
|
Number of crops to generate. Must not exceed the maximum number of non-overlapping crops that fit in the image. |
required |
Returns:
| Type | Description |
|---|---|
list[Sample]
|
List of num_samples dictionaries, each containing cropped regions. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If num_samples exceeds the number of possible non-overlapping crops. |
__call__(sample)
¶
Generate tiled crops from the sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
Sample
|
Dictionary containing tensors with shape (C, D, H, W). |
required |
Returns:
| Type | Description |
|---|---|
list[Sample]
|
List of num_samples dictionaries with cropped regions. |
ToDeviced
¶
Bases: ToDeviced
is_spatial = False
class-attribute
instance-attribute
¶
Move data to a specified device.
Wrapper around MONAI's ToDeviced transform with explicit constructor signature for jsonargparse compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str] | str
|
Keys of the data dictionary to move to the device. |
required |
**kwargs
|
Additional arguments passed to the parent class, including: - device : torch.device or str Target device (e.g., "cuda:0", "cpu"). - non_blocking : bool Whether to use non-blocking transfer. |
required |
See Also
monai.transforms.ToDeviced : Parent MONAI transform.