|
26 | 26 | from monai.utils.enums import TransformBackends
|
27 | 27 | from monai.utils.misc import MONAIEnvVars
|
28 | 28 |
|
29 |
| -__all__ = ["ThreadUnsafe", "apply_transform", "Randomizable", "RandomizableTransform", "Transform", "MapTransform"] |
| 29 | +__all__ = [ |
| 30 | + "ThreadUnsafe", |
| 31 | + "apply_transform", |
| 32 | + "LazyTrait", |
| 33 | + "RandomizableTrait", |
| 34 | + "MultiSampleTrait", |
| 35 | + "Randomizable", |
| 36 | + "LazyTransform", |
| 37 | + "RandomizableTransform", |
| 38 | + "Transform", |
| 39 | + "MapTransform", |
| 40 | +] |
30 | 41 |
|
31 | 42 | ReturnType = TypeVar("ReturnType")
|
32 | 43 |
|
@@ -118,6 +129,56 @@ def _log_stats(data, prefix: Optional[str] = "Data"):
|
118 | 129 | raise RuntimeError(f"applying transform {transform}") from e
|
119 | 130 |
|
120 | 131 |
|
| 132 | +class LazyTrait: |
| 133 | + """ |
| 134 | + An interface to indicate that the transform has the capability to execute using |
| 135 | + MONAI's lazy resampling feature. In order to do this, the implementing class needs |
| 136 | + to be able to describe its operation as an affine matrix or grid with accompanying metadata. |
| 137 | + This interface can be extended from by people adapting transforms to the MONAI framework as |
| 138 | + well as by implementors of MONAI transforms. |
| 139 | + """ |
| 140 | + |
| 141 | + @property |
| 142 | + def lazy_evaluation(self): |
| 143 | + """ |
| 144 | + Get whether lazy_evaluation is enabled for this transform instance. |
| 145 | + Returns: |
| 146 | + True if the transform is operating in a lazy fashion, False if not. |
| 147 | + """ |
| 148 | + raise NotImplementedError() |
| 149 | + |
| 150 | + @lazy_evaluation.setter |
| 151 | + def lazy_evaluation(self, enabled: bool): |
| 152 | + """ |
| 153 | + Set whether lazy_evaluation is enabled for this transform instance. |
| 154 | + Args: |
| 155 | + enabled: True if the transform should operate in a lazy fashion, False if not. |
| 156 | + """ |
| 157 | + raise NotImplementedError() |
| 158 | + |
| 159 | + |
| 160 | +class RandomizableTrait: |
| 161 | + """ |
| 162 | + An interface to indicate that the transform has the capability to perform |
| 163 | + randomized transforms to the data that it is called upon. This interface |
| 164 | + can be extended from by people adapting transforms to the MONAI framework as well as by |
| 165 | + implementors of MONAI transforms. |
| 166 | + """ |
| 167 | + |
| 168 | + pass |
| 169 | + |
| 170 | + |
| 171 | +class MultiSampleTrait: |
| 172 | + """ |
| 173 | + An interface to indicate that the transform has the capability to return multiple samples |
| 174 | + given an input, such as when performing random crops of a sample. This interface can be |
| 175 | + extended from by people adapting transforms to the MONAI framework as well as by implementors |
| 176 | + of MONAI transforms. |
| 177 | + """ |
| 178 | + |
| 179 | + pass |
| 180 | + |
| 181 | + |
121 | 182 | class ThreadUnsafe:
|
122 | 183 | """
|
123 | 184 | A class to denote that the transform will mutate its member variables,
|
@@ -251,7 +312,27 @@ def __call__(self, data: Any):
|
251 | 312 | raise NotImplementedError(f"Subclass {self.__class__.__name__} must implement this method.")
|
252 | 313 |
|
253 | 314 |
|
254 |
| -class RandomizableTransform(Randomizable, Transform): |
| 315 | +class LazyTransform(Transform, LazyTrait): |
| 316 | + """ |
| 317 | + An implementation of functionality for lazy transforms that can be subclassed by array and |
| 318 | + dictionary transforms to simplify implementation of new lazy transforms. |
| 319 | + """ |
| 320 | + |
| 321 | + def __init__(self, lazy_evaluation: Optional[bool] = True): |
| 322 | + self.lazy_evaluation = lazy_evaluation |
| 323 | + |
| 324 | + @property |
| 325 | + def lazy_evaluation(self): |
| 326 | + return self.lazy_evaluation |
| 327 | + |
| 328 | + @lazy_evaluation.setter |
| 329 | + def lazy_evaluation(self, lazy_evaluation: bool): |
| 330 | + if not isinstance(lazy_evaluation, bool): |
| 331 | + raise TypeError("'lazy_evaluation must be a bool but is of " f"type {type(lazy_evaluation)}'") |
| 332 | + self.lazy_evaluation = lazy_evaluation |
| 333 | + |
| 334 | + |
| 335 | +class RandomizableTransform(Randomizable, Transform, RandomizableTrait): |
255 | 336 | """
|
256 | 337 | An interface for handling random state locally, currently based on a class variable `R`,
|
257 | 338 | which is an instance of `np.random.RandomState`.
|
|
0 commit comments