Transforms
Transform
Bases: Module
Abstract class for all TorchIO transforms.
When called, the input can be an instance of
Subject,
Image,
torch.Tensor,
numpy.ndarray,
SimpleITK.Image,
nibabel.Nifti1Image,
dict containing 4D tensors as values,
ImagesBatch, or
SubjectsBatch.
The output type always matches the input type.
All subclasses must override
apply_transform(),
which receives a SubjectsBatch and
returns the transformed batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
float
|
Probability that this transform will be applied. |
1.0
|
copy
|
bool
|
Make a deep copy of the input before applying the
transform. When transforms are composed with
|
True
|
include
|
list[str] | None
|
Sequence of strings with the names of the only images to which the transform will be applied. |
None
|
exclude
|
list[str] | None
|
Sequence of strings with the names of the images to which the transform will not be applied. |
None
|
Source code in src/torchio/transforms/transform.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |
invertible
property
Whether this transform can be inverted.
forward(data)
Apply the transform.
The output type always matches the input type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Input data to transform. |
required |
Source code in src/torchio/transforms/transform.py
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
apply_transform(batch, params)
Apply the transform with the given parameters.
Must be overridden by subclasses. Receives a SubjectsBatch
whose ImagesBatch entries contain 5D tensors
(B, C, I, J, K). Use negative indexing (-3, -2,
-1) for spatial dims.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
SubjectsBatch
|
A |
required |
params
|
dict[str, Any]
|
Parameters from |
required |
Returns:
| Type | Description |
|---|---|
SubjectsBatch
|
Transformed |
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
SpatialTransform
Bases: Transform
Base for transforms that modify spatial geometry.
Spatial transforms apply to all images (ScalarImage and LabelMap), and also transform any Points and BoundingBoxes attached to the Subject.
Source code in src/torchio/transforms/transform.py
invertible
property
Whether this transform can be inverted.
forward(data)
Apply the transform.
The output type always matches the input type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Input data to transform. |
required |
Source code in src/torchio/transforms/transform.py
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
apply_transform(batch, params)
Apply the transform with the given parameters.
Must be overridden by subclasses. Receives a SubjectsBatch
whose ImagesBatch entries contain 5D tensors
(B, C, I, J, K). Use negative indexing (-3, -2,
-1) for spatial dims.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
SubjectsBatch
|
A |
required |
params
|
dict[str, Any]
|
Parameters from |
required |
Returns:
| Type | Description |
|---|---|
SubjectsBatch
|
Transformed |
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
IntensityTransform
Bases: Transform
Base for transforms that modify voxel intensities.
Intensity transforms apply only to ScalarImage instances,
leaving LabelMap and annotations unchanged.
Source code in src/torchio/transforms/transform.py
invertible
property
Whether this transform can be inverted.
forward(data)
Apply the transform.
The output type always matches the input type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Input data to transform. |
required |
Source code in src/torchio/transforms/transform.py
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
apply_transform(batch, params)
Apply the transform with the given parameters.
Must be overridden by subclasses. Receives a SubjectsBatch
whose ImagesBatch entries contain 5D tensors
(B, C, I, J, K). Use negative indexing (-3, -2,
-1) for spatial dims.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
SubjectsBatch
|
A |
required |
params
|
dict[str, Any]
|
Parameters from |
required |
Returns:
| Type | Description |
|---|---|
SubjectsBatch
|
Transformed |
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
AppliedTransform
dataclass
To
Bases: Transform
Move all data to a device and/or cast to a dtype.
Wraps the to() method as a transform so it can be used inside
Compose pipelines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*to_args
|
Any
|
Positional arguments forwarded to
|
()
|
**to_kwargs
|
Any
|
Keyword arguments forwarded to
|
{}
|
Examples:
>>> import torchio as tio
>>> transform = tio.To(torch.float16)
>>> transform = tio.To("cuda")
>>> pipeline = tio.Compose([
... tio.To("cuda"),
... tio.Noise(std=0.1),
... ])
Source code in src/torchio/transforms/to.py
invertible
property
Whether this transform can be inverted.
forward(data)
Apply the transform.
The output type always matches the input type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Input data to transform. |
required |
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
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
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).