OneOf
Bases: Transform
Apply one of the given transforms, chosen at random.
When applied to a batch with per_instance=True (the default),
each batch element independently chooses which transform to apply.
This requires shape- and schema-preserving transforms so the
elements can be re-stacked. Pass per_instance=False to choose a
single transform for the whole batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transforms
|
Sequence[Transform] | dict[Transform, float]
|
Sequence of transforms, or a |
required |
**kwargs
|
Any
|
See |
{}
|
Examples:
>>> import torchio as tio
>>> augmentation = tio.OneOf({
... tio.Noise(std=0.1): 0.7,
... tio.Flip(axes=(0,)): 0.3,
... })
Source code in src/torchio/transforms/compose.py
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 | |
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
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 |