Repository: Project-MONAI/MONAI

Issue:
`lazy` argument has no effect in `Compose.__call__`
According to the code, only `self.lazy` is used:
https://github.com/Project-MONAI/MONAI/blob/8e99af5f96df0746d6cbcaed88feaea0e51abd56/monai/transforms/compose.py#L321-L335

There's the same issue in `OneOf`. I'm not sure if this is intended. Also, the typing of `lazy` strangely has `str`.

https://github.com/Project-MONAI/MONAI/blob/8e99af5f96df0746d6cbcaed88feaea0e51abd56/monai/transforms/compose.py#L450-L473


Discussion:
thanks, these look like bugs, I'm labelling this contribution wanted.

Resolving patch:
diff --git a/monai/transforms/compose.py b/monai/transforms/compose.py
--- a/monai/transforms/compose.py
+++ b/monai/transforms/compose.py
@@ -319,6 +319,7 @@ def __len__(self):
         return len(self.flatten().transforms)
 
     def __call__(self, input_, start=0, end=None, threading=False, lazy: bool | None = None):
+        _lazy = self.lazy if lazy is None else lazy
         result = execute_compose(
             input_,
             transforms=self.transforms,
@@ -326,7 +327,7 @@ def __call__(self, input_, start=0, end=None, threading=False, lazy: bool | None
             end=end,
             map_items=self.map_items,
             unpack_items=self.unpack_items,
-            lazy=self.lazy,
+            lazy=_lazy,
             overrides=self.overrides,
             threading=threading,
             log_stats=self.log_stats,
@@ -341,7 +342,7 @@ def inverse(self, data):
         if not invertible_transforms:
             warnings.warn("inverse has been called but no invertible transforms have been supplied")
 
-        if self.lazy is not False:
+        if self.lazy is True:
             warnings.warn(
                 f"'lazy' is set to {self.lazy} but lazy execution is not supported when inverting. "
                 f"'lazy' has been overridden to False for the call to inverse"
@@ -447,7 +448,7 @@ def flatten(self):
                 weights.append(w)
         return OneOf(transforms, weights, self.map_items, self.unpack_items)
 
-    def __call__(self, data, start=0, end=None, threading=False, lazy: str | bool | None = None):
+    def __call__(self, data, start=0, end=None, threading=False, lazy: bool | None = None):
         if start != 0:
             raise ValueError(f"OneOf requires 'start' parameter to be 0 (start set to {start})")
         if end is not None:
@@ -458,6 +459,7 @@ def __call__(self, data, start=0, end=None, threading=False, lazy: str | bool |
 
         index = self.R.multinomial(1, self.weights).argmax()
         _transform = self.transforms[index]
+        _lazy = self.lazy if lazy is None else lazy
 
         data = execute_compose(
             data,
@@ -466,7 +468,7 @@ def __call__(self, data, start=0, end=None, threading=False, lazy: str | bool |
             end=end,
             map_items=self.map_items,
             unpack_items=self.unpack_items,
-            lazy=self.lazy,
+            lazy=_lazy,
             overrides=self.overrides,
             threading=threading,
             log_stats=self.log_stats,
@@ -553,6 +555,7 @@ def __call__(self, input_, start=0, end=None, threading=False, lazy: bool | None
 
         num = len(self.transforms)
         applied_order = self.R.permutation(range(num))
+        _lazy = self.lazy if lazy is None else lazy
 
         input_ = execute_compose(
             input_,
@@ -561,7 +564,7 @@ def __call__(self, input_, start=0, end=None, threading=False, lazy: bool | None
             end=end,
             map_items=self.map_items,
             unpack_items=self.unpack_items,
-            lazy=self.lazy,
+            lazy=_lazy,
             threading=threading,
             log_stats=self.log_stats,
         )
@@ -718,6 +721,7 @@ def __call__(self, data, start=0, end=None, threading=False, lazy: bool | None =
 
         sample_size = self.R.randint(self.min_num_transforms, self.max_num_transforms + 1)
         applied_order = self.R.choice(len(self.transforms), sample_size, replace=self.replace, p=self.weights).tolist()
+        _lazy = self.lazy if lazy is None else lazy
 
         data = execute_compose(
             data,
@@ -726,7 +730,7 @@ def __call__(self, data, start=0, end=None, threading=False, lazy: bool | None =
             end=end,
             map_items=self.map_items,
             unpack_items=self.unpack_items,
-            lazy=self.lazy,
+            lazy=_lazy,
             overrides=self.overrides,
             threading=threading,
             log_stats=self.log_stats,


Test patch: