Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RandomizableTransformType, LazyTransformType, and MultiSampleTransformType #5410

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 77 additions & 2 deletions monai/transforms/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
from monai.utils.enums import TransformBackends
from monai.utils.misc import MONAIEnvVars

__all__ = ["ThreadUnsafe", "apply_transform", "Randomizable", "RandomizableTransform", "Transform", "MapTransform"]
__all__ = ["ThreadUnsafe", "apply_transform",
"ILazyTransform", "IRandomizableTransform", "IMultiSampleTransform",
"Randomizable", "RandomizableTransform", "Transform", "MapTransform"]

ReturnType = TypeVar("ReturnType")

Expand Down Expand Up @@ -118,6 +120,79 @@ def _log_stats(data, prefix: Optional[str] = "Data"):
raise RuntimeError(f"applying transform {transform}") from e


class ILazyTransform:
"""
An interface to indicate that the transform has the capability to describe
its operation as an affine matrix or grid with accompanying metadata. This
interface can be extended from by people adapting transforms to the MONAI framework as well as
by implementors of MONAI transforms.
"""

@property
def lazy_evaluation(
self,
):
"""
Get whether lazy_evaluation is enabled for this transform instance.

Returns:
True if the transform is operating in a lazy fashion, False if not.
"""
raise NotImplementedError()

@lazy_evaluation.setter
def lazy_evaluation(
self,
enabled: bool
):
"""
Set whether lazy_evaluation is enabled for this transform instance.

Args:
enabled: True if the transform should operate in a lazy fashion, False if not.
"""
raise NotImplementedError()


class IRandomizableTransform:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that we need IMultiSampleTransform, but I'm not sure about IRandomizableTransform or ILazyTransform, these are not required concepts at the moment according to the prototypes... I can redirect my PR (#5407) to your fork if that addresses any potential merging conflicts

Copy link
Contributor Author

@atbenmurray atbenmurray Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the dictionary transform implementations on #4922, they make use of IRandomizableTransform.

The point of IRandomizableTransform is twofold:

  1. It can be the way that a transform is detected as randomizable or not, regardless of whether it is implemented in MONAI or extended from another library
  2. Randomizable dictionary transforms inherit from RandomizableTransform but then create a randomizable array transform to do the heavy lifting. However, they only delegate most of the randomization to the array class, whereas they could delegate all of it. IRandomizableTransform would allow that to be fixed as the dictionary transform can just say "hey, I'm randomizable" and then delegate all of its work to the array transform rather than having an implementation that is only partially used

The addition of this interface is partially motivated by the way I've implemented RandRotated in the big PR #4922 .

class RandRotated(MapTransform, InvertibleTransform, LazyTransform, IRandomizableTransform):

I might have gone too far in creating a separate Randomizer class that can be reused across the dictionary and array implementations, but I think it results in pretty clean code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, that looks like another refactoring effort introducing new concepts of Randomizer and IRandomizableTransform... how about we limit the scope for lazy resampling feature, and all the other nice-to-have features stay on a feature branch for now?

really hope we can ship the lazy resampling soon. The other refactorings add much risks but at the moment they are not critical from a user experience's perspective...

"""
An interface to indicate that the transform has the capability to perform
randomized transforms to the data that it is called upon. This interface
can be extended from by people adapting transforms to the MONAI framework as well as by
implementors of MONAI transforms.
"""

def set_random_state(
self,
seed: Optional[int] = None,
state: Optional[np.random.RandomState] = None
) -> "IRandomizableTransform":
"""
Set either the seed for an inbuilt random generator (assumed to be np.random.RandomState)
or set a random generator for this transform to use (again, assumed to be
np.random.RandomState). One one of these parameters should be set. If your random transform
that implements this interface doesn't support setting or reseeding of its random
generator, this method does not need to be implemented.

Args:
seed: set the random state with an integer seed.
state: set the random state with a `np.random.RandomState` object.

Returns:
self as a convenience for assignment
"""
raise TypeError(f"{self.__class__.__name__} does not support setting of random state via set_random_state.")


class IMultiSampleTransform:
"""
An interface to indicate that the transform has the capability to return multiple samples
given an input, such as when performing random crops of a sample. This interface can be
extended from by people adapting transforms to the MONAI framework as well as by implementors
of MONAI transforms.
"""


class ThreadUnsafe:
"""
A class to denote that the transform will mutate its member variables,
Expand Down Expand Up @@ -251,7 +326,7 @@ def __call__(self, data: Any):
raise NotImplementedError(f"Subclass {self.__class__.__name__} must implement this method.")


class RandomizableTransform(Randomizable, Transform):
class RandomizableTransform(Randomizable, Transform, IRandomizableTransform):
"""
An interface for handling random state locally, currently based on a class variable `R`,
which is an instance of `np.random.RandomState`.
Expand Down
41 changes: 41 additions & 0 deletions tests/test_irandomizable_transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest


from monai.transforms.transform import IRandomizableTransform, RandomizableTransform


class InheritsInterface(IRandomizableTransform):
pass


class InheritsImplementation(RandomizableTransform):

def __call__(self, data):
return data


class TestIRandomizableTransform(unittest.TestCase):

def test_is_irandomizable(self):
inst = InheritsInterface()
self.assertIsInstance(inst, IRandomizableTransform)

def test_set_random_state_default_impl(self):
inst = InheritsInterface()
with self.assertRaises(TypeError):
inst.set_random_state(seed=0)

def test_set_random_state_randomizable_transform(self):
inst = InheritsImplementation()
inst.set_random_state(0)