MonaiAdapter
Bases: Transform
Wrap a MONAI transform for use in TorchIO pipelines.
Both dictionary transforms (subclasses of MONAI's
MapTransform, e.g., NormalizeIntensityd) and array
transforms (e.g., NormalizeIntensity) are supported.
Dictionary transforms operate on the full subject dictionary: only the keys specified in the MONAI transform are modified.
Array transforms are applied to each
ScalarImage in the subject individually,
respecting the include / exclude parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monai_transform
|
Callable
|
A MONAI transform or any callable. Requires
MONAI to be installed: |
required |
**kwargs
|
Any
|
See |
{}
|
Examples:
>>> import torchio as tio
>>> from monai.transforms import NormalizeIntensity
>>> # Array transform: applied to each ScalarImage
>>> adapter = tio.MonaiAdapter(NormalizeIntensity())
>>> result = adapter(subject)
>>> # Inside a Compose pipeline
>>> pipeline = tio.Compose([
... tio.MonaiAdapter(NormalizeIntensity()),
... tio.Noise(std=0.1),
... ])
Note
MonaiAdapter does not record itself in the subject's
transform history, because MONAI transform objects are not
serializable.
Source code in src/torchio/transforms/monai_adapter.py
supports_per_instance_params
property
Whether this transform can sample parameters per batch element.
Defaults to False. Transforms that implement per-instance
parameter sampling override this to return True. When False,
the transform always uses batch-shared parameters regardless of
the per_instance flag, preserving the legacy behavior.
supports_per_instance_p
property
Whether this transform can gate each batch element independently.
Defaults to False. Shape-preserving transforms that implement
per-element probability override this to return True.
Shape-changing transforms must leave it False because masked
and unmasked elements would have incompatible shapes.
invertible
property
Whether this transform can be inverted.
make_params(batch)
Sample random parameters for this transform.
Override in subclasses that have random behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
SubjectsBatch
|
A |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict of sampled parameters. |
Source code in src/torchio/transforms/transform.py
inverse(params)
Return a transform that undoes this one.
Override in invertible subclasses. The returned transform, when applied, reverses the effect of the forward pass with the given parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
dict[str, Any]
|
The parameters recorded in the forward pass. |
required |
Returns:
| Type | Description |
|---|---|
Transform
|
A new |
Source code in src/torchio/transforms/transform.py
to_hydra()
Export as a Hydra-compatible config dict.
Returns a dict with _target_ set to the fully qualified
class name and only non-default field values included.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict suitable for |
Source code in src/torchio/transforms/transform.py
forward(data)
Apply without recording history (MONAI transforms are opaque).