"""Pre-built augmentation pipelines for common medical imaging modalities.
Each factory returns a :class:`~medaugmentx.core.compose.Compose` pipeline
configured with clinically-motivated defaults. They are starting points —
tune parameters to your dataset rather than treating them as ground truth.
Available presets::
from medaugmentx.presets import mri_pipeline, ct_pipeline, dxr_pipeline, dbt_pipeline
pipeline = mri_pipeline(seed=42)
augmented = pipeline(volume)
All pipelines are serialisable via :mod:`medaugmentx.serialization`.
"""
from __future__ import annotations
from medaugmentx.core.compose import Compose, OneOf
from medaugmentx.core.utils import SeedLike
# ---------------------------------------------------------------------------
# MRI
# ---------------------------------------------------------------------------
[docs]
def mri_pipeline(
seed: SeedLike = None, *, allow_flip: bool = False, normalize: bool = False
) -> Compose:
"""Standard MRI augmentation pipeline.
Applies a representative mix of spatial deformations, intensity
perturbations, and MRI-specific artifacts:
* Random flip along the array column axis (task/orientation dependent).
* Random affine — small rotations and scaling.
* Elastic deformation — simulate tissue deformability.
* Bias field — RF coil / B0 inhomogeneity.
* Rician noise — a single-complex-signal magnitude-noise approximation.
* Gamma correction — scanner contrast variation.
* One of: ghosting artifact, k-space line dropout, *or* motion blur.
Args:
seed: Top-level RNG seed for reproducibility.
allow_flip: Opt in only when the dataset/task permits laterality reversal.
normalize: Prepend percentile normalization for arbitrary-unit inputs.
Returns:
A seeded :class:`~medaugmentx.core.compose.Compose` pipeline.
"""
from medaugmentx.transforms import NormalizeIntensity
from medaugmentx.transforms.intensity.bias_field import BiasField
from medaugmentx.transforms.intensity.contrast import GammaCorrection
from medaugmentx.transforms.intensity.noise import RicianNoise
from medaugmentx.transforms.modality.mri.ghosting import GhostingArtifact
from medaugmentx.transforms.modality.mri.kspace import KSpaceDropout
from medaugmentx.transforms.modality.mri.motion import MRIMotion
from medaugmentx.transforms.spatial.affine import RandomAffine
from medaugmentx.transforms.spatial.elastic import ElasticDeform
from medaugmentx.transforms.spatial.flip import RandomFlip
return Compose(
([NormalizeIntensity()] if normalize else [])
+ [
RandomFlip(axes=("x",), p_per_axis=0.5, p=0.5 if allow_flip else 0.0),
RandomAffine(rotation=10.0, scale=(0.9, 1.1), translation=(-0.05, 0.05), p=0.7),
ElasticDeform(alpha=30.0, sigma=4.0, p=0.5),
BiasField(alpha=0.3, p=0.7),
RicianNoise(std=(0.005, 0.02), p=0.5),
GammaCorrection(gamma=(0.85, 1.15), p=0.5),
OneOf(
[
GhostingArtifact(ghost_intensity=(0.05, 0.12), p=1.0),
KSpaceDropout(dropout_fraction=(0.01, 0.04), p=1.0),
MRIMotion(degrees=(1.0, 4.0), translation=(1.0, 3.0), p=1.0),
],
p=0.3,
),
],
seed=seed,
)
# ---------------------------------------------------------------------------
# CT
# ---------------------------------------------------------------------------
[docs]
def ct_pipeline(
seed: SeedLike = None, *, allow_flip: bool = False, normalize_output: bool = False
) -> Compose:
"""Standard CT augmentation pipeline.
Combines spatial augmentations with CT-specific intensity transforms:
* Random flip along the array column axis (task/orientation dependent).
* Random affine — small rotations and scaling.
* Elastic deformation.
* Window / level variation — simulates protocol differences across sites.
* Gaussian noise — additive CT quantum noise.
* Gamma correction — contrast variation.
* Beam hardening (cupping artifact) — applied occasionally.
* Metal streak artifact — implant/clip simulation, applied rarely.
Expects native HU-scale CT input and keeps that intensity scale throughout:
windowing clips without normalising, so the noise sigma remains in HU.
Apply deterministic windowing/normalisation after this preset if needed.
Augmented values are synthetic and no longer calibrated measurements.
Args:
seed: Top-level RNG seed for reproducibility.
allow_flip: Opt in only when the dataset/task permits laterality reversal.
normalize_output: Append deterministic window normalization after HU-domain effects.
Returns:
A seeded :class:`~medaugmentx.core.compose.Compose` pipeline.
"""
from medaugmentx.transforms.intensity.contrast import GammaCorrection
from medaugmentx.transforms.intensity.noise import GaussianNoise
from medaugmentx.transforms.intensity.window_level import WindowLevel
from medaugmentx.transforms.modality.ct.beam_hardening import BeamHardening
from medaugmentx.transforms.modality.ct.metal import MetalStreak
from medaugmentx.transforms.spatial.affine import RandomAffine
from medaugmentx.transforms.spatial.elastic import ElasticDeform
from medaugmentx.transforms.spatial.flip import RandomFlip
return Compose(
[
RandomFlip(axes=("x",), p_per_axis=0.5, p=0.5 if allow_flip else 0.0),
RandomAffine(
rotation=8.0, scale=(0.9, 1.1), translation=(-0.03, 0.03), cval=-1000, p=0.7
),
ElasticDeform(alpha=20.0, sigma=4.0, cval=-1000, mode="constant", p=0.4),
WindowLevel(
center_shift_frac=0.05, width_scale=(0.85, 1.15), rescale_output=False, p=0.6
),
GaussianNoise(std=(5.0, 20.0), p=0.4),
GammaCorrection(gamma=(0.9, 1.1), p=0.4),
BeamHardening(alpha=(0.02, 0.07), p=0.3),
MetalStreak(intensity=(0.1, 0.25), p=0.15),
]
+ ([WindowLevel(center_shift_frac=0, width_scale=1)] if normalize_output else []),
seed=seed,
)
# ---------------------------------------------------------------------------
# Digital X-Ray (DXR / Chest X-Ray)
# ---------------------------------------------------------------------------
[docs]
def dxr_pipeline(
seed: SeedLike = None, *, allow_flip: bool = False, normalize: bool = False
) -> Compose:
"""Augmentation pipeline for digital X-ray (chest X-ray / DXR).
Applies 2-D-safe transforms only:
* Horizontal flip (requires a task that tolerates laterality reversal).
* Slight affine — small rotation (patient positioning variation).
* Gaussian blur — simulates detector resolution variation.
* Brightness/contrast — exposure variation between scanners.
* Gamma correction — film/detector response variation.
* CLAHE — adaptive contrast equalisation (standard CXR pre-processing).
* Simulate low resolution — cross-site resolution augmentation.
* One of: scatter (veiling glare) *or* anti-scatter grid lines.
Args:
seed: Top-level RNG seed for reproducibility.
allow_flip: Opt in only when the dataset/task permits laterality reversal.
normalize: Prepend percentile normalization for arbitrary-unit inputs.
Returns:
A seeded :class:`~medaugmentx.core.compose.Compose` pipeline.
"""
from medaugmentx.transforms import NormalizeIntensity
from medaugmentx.transforms.intensity.blur import GaussianBlur, SimulateLowResolution
from medaugmentx.transforms.intensity.brightness_contrast import BrightnessContrast
from medaugmentx.transforms.intensity.clahe import CLAHEContrast
from medaugmentx.transforms.intensity.contrast import GammaCorrection
from medaugmentx.transforms.modality.xray.grid import GridArtifact
from medaugmentx.transforms.modality.xray.scatter import ScatterSimulation
from medaugmentx.transforms.spatial.affine import RandomAffine
from medaugmentx.transforms.spatial.flip import RandomFlip
return Compose(
([NormalizeIntensity()] if normalize else [])
+ [
RandomFlip(axes=("x",), p_per_axis=0.5, p=0.5 if allow_flip else 0.0),
RandomAffine(
rotation=5.0,
scale=(0.95, 1.05),
axes_enabled=("x", "y"),
p=0.6,
),
GaussianBlur(sigma=(0.3, 1.2), p=0.4),
BrightnessContrast(brightness=0.05, contrast=(0.9, 1.1), p=0.5),
GammaCorrection(gamma=(0.8, 1.2), p=0.5),
CLAHEContrast(clip_limit=(1.0, 2.5), p=0.3),
SimulateLowResolution(zoom_range=(0.6, 0.95), p=0.3),
OneOf(
[
ScatterSimulation(fraction=(0.1, 0.3), p=1.0),
GridArtifact(amplitude=(0.03, 0.08), p=1.0),
],
p=0.25,
),
],
seed=seed,
)
# ---------------------------------------------------------------------------
# Digital Breast Tomosynthesis (DBT)
# ---------------------------------------------------------------------------
[docs]
def dbt_pipeline(
seed: SeedLike = None, *, allow_flip: bool = False, normalize: bool = False
) -> Compose:
"""Augmentation pipeline for Digital Breast Tomosynthesis (DBT).
DBT volumes are strongly anisotropic (thin slices, wide in-plane FOV).
This pipeline combines the Phase-1 DBT-specific transforms with the
new Phase-2 intensity transforms:
* Random flip — horizontal axis only (breast anatomy).
* Affine — in-plane rotation only (``axes_enabled=("z",)``).
* Anisotropic elastic deformation — larger XY alpha, small Z.
* Compression variation — breast-paddle compression differences.
* Slab shift — reconstruction centre variation.
* Limited-angle blur — arc-angle-dependent Z blur.
* Recon streak — limited-angle out-of-plane parallax replicas.
* Slice dropout — robustness to missing reconstruction slices.
* Bias field — detector gain non-uniformity.
* Gamma correction — scatter / beam hardening contrast variation.
Args:
seed: Top-level RNG seed for reproducibility.
allow_flip: Opt in only when the dataset/task permits laterality reversal.
normalize: Prepend percentile normalization for arbitrary-unit inputs.
Returns:
A seeded :class:`~medaugmentx.core.compose.Compose` pipeline.
"""
from medaugmentx.transforms import NormalizeIntensity
from medaugmentx.transforms.intensity.bias_field import BiasField
from medaugmentx.transforms.intensity.contrast import GammaCorrection
from medaugmentx.transforms.modality.tomosynthesis.blur import LimitedAngleBlur
from medaugmentx.transforms.modality.tomosynthesis.compression import CompressionVariation
from medaugmentx.transforms.modality.tomosynthesis.dropout import SliceDropout
from medaugmentx.transforms.modality.tomosynthesis.elastic import AnisotropicElastic
from medaugmentx.transforms.modality.tomosynthesis.recon_streak import ReconStreak
from medaugmentx.transforms.modality.tomosynthesis.slab import SlabShift
from medaugmentx.transforms.spatial.affine import RandomAffine
from medaugmentx.transforms.spatial.flip import RandomFlip
return Compose(
([NormalizeIntensity()] if normalize else [])
+ [
RandomFlip(axes=("x",), p_per_axis=0.5, p=0.5 if allow_flip else 0.0),
RandomAffine(
rotation=5.0,
scale=(0.95, 1.05),
axes_enabled=("z",),
scale_axes=("y", "x"),
p=0.7,
),
AnisotropicElastic(
alpha=(6.0, 80.0, 80.0),
sigma=(2.0, 8.0, 8.0),
p=0.5,
),
CompressionVariation(scale=(0.9, 1.1), axis="y", p=0.4),
SlabShift(max_shift=2, p=0.5),
LimitedAngleBlur(arc_degrees=(15.0, 25.0), p=0.6),
ReconStreak(amplitude=(0.05, 0.15), p=0.3),
SliceDropout(num_slices=(1, 2), p=0.3),
BiasField(alpha=0.2, coarse_shape=3, p=0.5),
GammaCorrection(gamma=(0.85, 1.15), p=0.5),
],
seed=seed,
)
__all__ = ["mri_pipeline", "ct_pipeline", "dxr_pipeline", "dbt_pipeline"]