Skip to content

Commit d4dc055

Browse files
authored
Fix Lazy=True ignored when using Dataset call (#6975)
Fixes #6974. ### Description Change default value of `lazy` in `apply_transform` to None. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: KumoLiu <yunl@nvidia.com>
1 parent 392c5c1 commit d4dc055

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

monai/transforms/transform.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def apply_transform(
104104
map_items: bool = True,
105105
unpack_items: bool = False,
106106
log_stats: bool | str = False,
107-
lazy: bool | None = False,
107+
lazy: bool | None = None,
108108
overrides: dict | None = None,
109109
) -> list[ReturnType] | ReturnType:
110110
"""
@@ -124,7 +124,7 @@ def apply_transform(
124124
disables the logger for processing pipeline errors. Setting it to None or True will enable logging to the
125125
default logger name. Setting it to a string specifies the logger to which errors should be logged.
126126
lazy: whether to execute in lazy mode or not. See the :ref:`Lazy Resampling topic<lazy_resampling> for more
127-
information about lazy resampling.
127+
information about lazy resampling. Defaults to None.
128128
overrides: optional overrides to apply to transform parameters. This parameter is ignored unless transforms
129129
are being executed lazily. See the :ref:`Lazy Resampling topic<lazy_resampling> for more details and
130130
examples of its usage.

tests/test_compose.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,12 @@ def test_compose_with_logger(self, keys, pipeline):
607607
"INFO - Pending transforms applied: applied_operations: 1\n"
608608
),
609609
],
610+
[
611+
mt.OneOf,
612+
(mt.Flip(0),),
613+
False,
614+
("INFO - Apply pending transforms - lazy: False, pending: 0, " "upcoming 'Flip', transform.lazy: False\n"),
615+
],
610616
]
611617

612618

tests/test_dataset.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111

1212
from __future__ import annotations
1313

14+
import logging
1415
import os
1516
import tempfile
1617
import unittest
18+
from copy import deepcopy
19+
from io import StringIO
1720

1821
import nibabel as nib
1922
import numpy as np
2023
from parameterized import parameterized
2124

2225
from monai.data import Dataset
2326
from monai.transforms import Compose, LoadImaged, SimulateDelayd
27+
from tests.test_compose import TEST_COMPOSE_LAZY_ON_CALL_LOGGING_TEST_CASES, data_from_keys
2428

2529
TEST_CASE_1 = [(128, 128, 128)]
2630

@@ -89,6 +93,39 @@ def test_shape(self, expected_shape):
8993
for d in data4_list:
9094
self.assertTupleEqual(d["image"].shape, expected_shape)
9195

96+
def test_dataset_lazy_on_call(self):
97+
data = np.zeros((1, 5, 5))
98+
data[0, 0:2, 0:2] = 1
99+
100+
101+
class TestDatsesetWithLazy(unittest.TestCase):
102+
LOGGER_NAME = "a_logger_name"
103+
104+
def init_logger(self, name=LOGGER_NAME):
105+
stream = StringIO()
106+
handler = logging.StreamHandler(stream)
107+
formatter = logging.Formatter("%(levelname)s - %(message)s")
108+
handler.setFormatter(formatter)
109+
logger = logging.getLogger(name)
110+
logger.setLevel(logging.INFO)
111+
while len(logger.handlers) > 0:
112+
logger.removeHandler(logger.handlers[-1])
113+
logger.addHandler(handler)
114+
return handler, stream
115+
116+
@parameterized.expand(TEST_COMPOSE_LAZY_ON_CALL_LOGGING_TEST_CASES)
117+
def test_dataset_lazy_with_logging(self, compose_type, pipeline, lazy, expected):
118+
handler, stream = self.init_logger(name=self.LOGGER_NAME)
119+
120+
data = data_from_keys(None, 12, 16)
121+
c = compose_type(deepcopy(pipeline), log_stats=self.LOGGER_NAME, lazy=lazy)
122+
ds = Dataset([data], transform=c)
123+
ds[0]
124+
125+
handler.flush()
126+
actual = stream.getvalue()
127+
self.assertEqual(actual, expected)
128+
92129

93130
if __name__ == "__main__":
94131
unittest.main()

0 commit comments

Comments
 (0)