MonaiAdapter
MonaiAdapter
Bases: Transform
Wraps a MONAI transform for use in TorchIO pipelines.
This adapter allows using
MONAI transforms
within TorchIO workflows. Both dictionary transforms (e.g.,
NormalizeIntensityd) and array transforms (e.g.,
NormalizeIntensity) are supported.
The adapter handles conversion between TorchIO's
Subject (where values are
Image objects) and MONAI's expected format
(where values are tensors or
MetaTensor
objects). Image tensors are passed as MetaTensor instances with the
affine matrix embedded, so spatial transforms (e.g., cropping, resizing)
correctly propagate affine changes.
Dictionary transforms (subclasses of MONAI's MapTransform)
operate on the full subject dictionary — only the keys specified in
the MONAI transform are modified.
Array transforms (all other callables) are applied to each
image in the subject individually, respecting the include and
exclude parameters inherited from
Transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monai_transform
|
Callable
|
A MONAI transform (dictionary or array) or a
MONAI-compatible callable. Requires MONAI to be installed,
e.g., via |
required |
**kwargs
|
See |
{}
|
Examples:
>>> import torch
>>> import torchio as tio
>>> from monai.transforms import NormalizeIntensity
>>> from monai.transforms import NormalizeIntensityd
>>> from monai.transforms import RandSpatialCropd
>>> subject = tio.Subject(
... t1=tio.ScalarImage(tensor=torch.randn(1, 64, 64, 64)),
... seg=tio.LabelMap(tensor=torch.ones(1, 64, 64, 64)),
... )
>>> # Array transform — applied to each image
>>> adapter = tio.MonaiAdapter(NormalizeIntensity())
>>> transformed = adapter(subject)
>>> # Dictionary transform — applied to specified keys
>>> adapter = tio.MonaiAdapter(NormalizeIntensityd(keys=["t1"]))
>>> transformed = adapter(subject)
>>> # Spatial dict transform (affine is updated)
>>> adapter = tio.MonaiAdapter(
... RandSpatialCropd(keys=["t1", "seg"], roi_size=[32, 32, 32]),
... )
>>> transformed = adapter(subject)
>>> # Inside a Compose pipeline
>>> pipeline = tio.Compose([
... tio.ToCanonical(),
... tio.MonaiAdapter(NormalizeIntensity()),
... tio.RandomFlip(),
... ])
>>> transformed = pipeline(subject)
__call__(data)
Transform data and return a result of the same type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
TypeTransformInput
|
Instance of |
required |
to_hydra_config()
Not supported — MONAI transforms are not serializable.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. |