Skip to content

Commit 20a9632

Browse files
aymuos15ericspod
andauthored
Perf(test): shrink synthetic volumes in image loading and dataset tests (#9034)
### Description Shrinks the synthetic volumes these six test files generate, so each test still exercises the same code paths in a fraction of the runtime. Pure test-fixture change: no production code touched, no test logic altered: - `tests/transforms/test_load_image.py`, `tests/transforms/test_load_imaged.py`: 128³ volumes → 32³; multi-image cases 3×128³ → 3×32³; the 384×128³ channel-dim case scales to 96×32³. - `tests/data/test_arraydataset.py`, `test_cachedataset.py`, `test_dataset.py`: 128³ → 32³; the Spacing case 64×64×33 → 16×16×9. - `tests/data/test_mapping_file.py`: 128³ → 16³. The 128³ sizes were an unexamined default; nothing depends on the magnitude, only that shapes round-trip through `.nii.gz` I/O. Expected shapes were updated in lockstep, so the assertions still verify the same behaviour. ### Types of changes - [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: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk> Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
1 parent 8605065 commit 20a9632

6 files changed

Lines changed: 49 additions & 49 deletions

File tree

‎tests/data/test_arraydataset.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
Compose([LoadImage(image_only=True), EnsureChannelFirst(channel_dim="no_channel"), RandGaussianNoise(prob=1.0)]),
2929
Compose([LoadImage(image_only=True), EnsureChannelFirst(channel_dim="no_channel"), RandGaussianNoise(prob=1.0)]),
3030
(0, 1),
31-
(1, 128, 128, 128),
31+
(1, 32, 32, 32),
3232
]
3333

3434
TEST_CASE_2 = [
3535
Compose([LoadImage(image_only=True), EnsureChannelFirst(channel_dim="no_channel"), RandAdjustContrast(prob=1.0)]),
3636
Compose([LoadImage(image_only=True), EnsureChannelFirst(channel_dim="no_channel"), RandAdjustContrast(prob=1.0)]),
3737
(0, 1),
38-
(1, 128, 128, 128),
38+
(1, 32, 32, 32),
3939
]
4040

4141

@@ -69,20 +69,20 @@ def __call__(self, input_, lazy=False):
6969
]
7070
),
7171
(0, 2),
72-
(1, 64, 64, 33),
72+
(1, 16, 16, 9),
7373
]
7474

7575
TEST_CASE_4 = [
7676
Compose([LoadImage(image_only=True), EnsureChannelFirst(channel_dim="no_channel"), RandGaussianNoise(prob=1.0)]),
77-
(1, 128, 128, 128),
77+
(1, 32, 32, 32),
7878
]
7979

8080

8181
class TestArrayDataset(unittest.TestCase):
8282

8383
@parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3])
8484
def test_shape(self, img_transform, label_transform, indices, expected_shape):
85-
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=(128, 128, 128)).astype(float), np.eye(4))
85+
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=(32, 32, 32)).astype(float), np.eye(4))
8686
with tempfile.TemporaryDirectory() as tempdir:
8787
test_image1 = os.path.join(tempdir, "test_image1.nii.gz")
8888
test_seg1 = os.path.join(tempdir, "test_seg1.nii.gz")
@@ -116,7 +116,7 @@ def test_shape(self, img_transform, label_transform, indices, expected_shape):
116116

117117
@parameterized.expand([TEST_CASE_4])
118118
def test_default_none(self, img_transform, expected_shape):
119-
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=(128, 128, 128)).astype(float), np.eye(4))
119+
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=(32, 32, 32)).astype(float), np.eye(4))
120120
with tempfile.TemporaryDirectory() as tempdir:
121121
test_image1 = os.path.join(tempdir, "test_image1.nii.gz")
122122
test_image2 = os.path.join(tempdir, "test_image2.nii.gz")
@@ -139,7 +139,7 @@ def test_default_none(self, img_transform, expected_shape):
139139

140140
@parameterized.expand([TEST_CASE_4])
141141
def test_dataloading_img(self, img_transform, expected_shape):
142-
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=(128, 128, 128)).astype(float), np.eye(4))
142+
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=(32, 32, 32)).astype(float), np.eye(4))
143143
with tempfile.TemporaryDirectory() as tempdir:
144144
test_image1 = os.path.join(tempdir, "test_image1.nii.gz")
145145
test_image2 = os.path.join(tempdir, "test_image2.nii.gz")
@@ -160,7 +160,7 @@ def test_dataloading_img(self, img_transform, expected_shape):
160160

161161
@parameterized.expand([TEST_CASE_4])
162162
def test_dataloading_img_label(self, img_transform, expected_shape):
163-
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=(128, 128, 128)).astype(float), np.eye(4))
163+
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=(32, 32, 32)).astype(float), np.eye(4))
164164
with tempfile.TemporaryDirectory() as tempdir:
165165
test_image1 = os.path.join(tempdir, "test_image1.nii.gz")
166166
test_image2 = os.path.join(tempdir, "test_image2.nii.gz")

‎tests/data/test_cachedataset.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
from monai.data import CacheDataset, DataLoader, PersistentDataset, SmartCacheDataset
2424
from monai.transforms import Compose, Lambda, LoadImaged, RandLambda, ThreadUnsafe, Transform
2525

26-
TEST_CASE_1 = [Compose([LoadImaged(keys=["image", "label", "extra"])]), (128, 128, 128)]
26+
TEST_CASE_1 = [Compose([LoadImaged(keys=["image", "label", "extra"])]), (32, 32, 32)]
2727

28-
TEST_CASE_2 = [None, (128, 128, 128)]
28+
TEST_CASE_2 = [None, (32, 32, 32)]
2929

3030
TEST_DS = []
3131
for c in (0, 1, 2):
@@ -41,7 +41,7 @@ class TestCacheDataset(unittest.TestCase):
4141

4242
@parameterized.expand([TEST_CASE_1, TEST_CASE_2])
4343
def test_shape(self, transform, expected_shape):
44-
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[128, 128, 128]).astype(float), np.eye(4))
44+
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[32, 32, 32]).astype(float), np.eye(4))
4545
with tempfile.TemporaryDirectory() as tempdir:
4646
test_data = []
4747
for i in ["1", "2"]:
@@ -192,7 +192,7 @@ def test_thread_safe(self, persistent_workers, cache_workers, loader_workers):
192192

193193
@parameterized.expand([TEST_CASE_1, TEST_CASE_2])
194194
def test_hash_as_key(self, transform, expected_shape):
195-
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[128, 128, 128]).astype(float), np.eye(4))
195+
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[32, 32, 32]).astype(float), np.eye(4))
196196
with tempfile.TemporaryDirectory() as tempdir:
197197
test_data = []
198198
for i in ["1", "2", "2", "3", "3"]:

‎tests/data/test_dataset.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
from monai.transforms import Compose, Lambda, LoadImage, LoadImaged, SimulateDelay, SimulateDelayd
2727
from tests.transforms.compose.test_compose import TEST_COMPOSE_LAZY_ON_CALL_LOGGING_TEST_CASES, data_from_keys
2828

29-
TEST_CASE_1 = [(128, 128, 128)]
29+
TEST_CASE_1 = [(32, 32, 32)]
3030

3131

3232
class TestDataset(unittest.TestCase):
3333
@parameterized.expand([TEST_CASE_1])
3434
def test_shape(self, expected_shape):
35-
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[128, 128, 128]).astype(float), np.eye(4))
35+
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[32, 32, 32]).astype(float), np.eye(4))
3636
with tempfile.TemporaryDirectory() as tempdir:
3737
nib.save(test_image, os.path.join(tempdir, "test_image1.nii.gz"))
3838
nib.save(test_image, os.path.join(tempdir, "test_label1.nii.gz"))
@@ -101,7 +101,7 @@ def test_dataset_lazy_on_call(self):
101101
class TestTupleDataset(unittest.TestCase):
102102
@parameterized.expand([TEST_CASE_1])
103103
def test_shape(self, expected_shape):
104-
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[128, 128, 128]).astype(float), np.eye(4))
104+
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[32, 32, 32]).astype(float), np.eye(4))
105105
with tempfile.TemporaryDirectory() as tempdir:
106106
nib.save(test_image, os.path.join(tempdir, "test_image1.nii.gz"))
107107
nib.save(test_image, os.path.join(tempdir, "test_label1.nii.gz"))

‎tests/data/test_mapping_file.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929

3030
def create_input_file(temp_dir, name):
31-
test_image = np.random.rand(128, 128, 128)
31+
test_image = np.random.rand(16, 16, 16)
3232
output_ext = ".nii.gz"
3333
input_file = os.path.join(temp_dir, name + output_ext)
3434
nib.save(nib.Nifti1Image(test_image, np.eye(4)), input_file)

‎tests/transforms/test_load_image.py‎

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -86,52 +86,52 @@ def get_data(self, _img):
8686
return np.zeros((1, 1)), {"name": "fallback"}
8787

8888

89-
TEST_CASE_1 = [{}, ["test_image.nii.gz"], (128, 128, 128)]
89+
TEST_CASE_1 = [{}, ["test_image.nii.gz"], (32, 32, 32)]
9090

91-
TEST_CASE_3 = [{}, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128)]
91+
TEST_CASE_3 = [{}, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 32, 32, 32)]
9292

9393
TEST_CASE_3_1 = [ # .mgz format
9494
{"reader": "nibabelreader"},
9595
["test_image.mgz", "test_image2.mgz", "test_image3.mgz"],
96-
(3, 128, 128, 128),
96+
(3, 32, 32, 32),
9797
]
9898

9999
TEST_CASE_4_1 = [ # additional parameter
100100
{"mmap": False},
101101
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
102-
(3, 128, 128, 128),
102+
(3, 32, 32, 32),
103103
]
104104

105-
TEST_CASE_5 = [{"reader": NibabelReader(mmap=False)}, ["test_image.nii.gz"], (128, 128, 128)]
105+
TEST_CASE_5 = [{"reader": NibabelReader(mmap=False)}, ["test_image.nii.gz"], (32, 32, 32)]
106106

107-
TEST_CASE_GPU_1 = [{"reader": "nibabelreader", "to_gpu": True}, ["test_image.nii.gz"], (128, 128, 128)]
107+
TEST_CASE_GPU_1 = [{"reader": "nibabelreader", "to_gpu": True}, ["test_image.nii.gz"], (32, 32, 32)]
108108

109-
TEST_CASE_GPU_2 = [{"reader": "nibabelreader", "to_gpu": True}, ["test_image.nii"], (128, 128, 128)]
109+
TEST_CASE_GPU_2 = [{"reader": "nibabelreader", "to_gpu": True}, ["test_image.nii"], (32, 32, 32)]
110110

111111
TEST_CASE_GPU_3 = [
112112
{"reader": "nibabelreader", "to_gpu": True},
113113
["test_image.nii", "test_image2.nii", "test_image3.nii"],
114-
(3, 128, 128, 128),
114+
(3, 32, 32, 32),
115115
]
116116

117117
TEST_CASE_GPU_4 = [
118118
{"reader": "nibabelreader", "to_gpu": True},
119119
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
120-
(3, 128, 128, 128),
120+
(3, 32, 32, 32),
121121
]
122122

123-
TEST_CASE_6 = [{"reader": ITKReader() if has_itk else "itkreader"}, ["test_image.nii.gz"], (128, 128, 128)]
123+
TEST_CASE_6 = [{"reader": ITKReader() if has_itk else "itkreader"}, ["test_image.nii.gz"], (32, 32, 32)]
124124

125125
TEST_CASE_8 = [
126126
{"reader": ITKReader() if has_itk else "itkreader"},
127127
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
128-
(3, 128, 128, 128),
128+
(3, 32, 32, 32),
129129
]
130130

131131
TEST_CASE_8_1 = [
132132
{"reader": ITKReader(channel_dim=0) if has_itk else "itkreader"},
133133
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
134-
(384, 128, 128),
134+
(96, 32, 32),
135135
]
136136

137137
TEST_CASE_10 = [
@@ -150,24 +150,24 @@ def get_data(self, _img):
150150
(4, 16, 16),
151151
]
152152

153-
TEST_CASE_13 = [{"reader": "nibabelreader", "channel_dim": 0}, "test_image.nii.gz", (3, 128, 128, 128)]
153+
TEST_CASE_13 = [{"reader": "nibabelreader", "channel_dim": 0}, "test_image.nii.gz", (3, 32, 32, 32)]
154154

155155
TEST_CASE_14 = [
156156
{"reader": "nibabelreader", "channel_dim": -1, "ensure_channel_first": True},
157157
"test_image.nii.gz",
158-
(128, 128, 128, 3),
158+
(32, 32, 32, 3),
159159
]
160160

161-
TEST_CASE_15 = [{"reader": "nibabelreader", "channel_dim": 2}, "test_image.nii.gz", (128, 128, 3, 128)]
161+
TEST_CASE_15 = [{"reader": "nibabelreader", "channel_dim": 2}, "test_image.nii.gz", (32, 32, 3, 32)]
162162

163-
TEST_CASE_16 = [{"reader": "itkreader", "channel_dim": 0}, "test_image.nii.gz", (3, 128, 128, 128)]
163+
TEST_CASE_16 = [{"reader": "itkreader", "channel_dim": 0}, "test_image.nii.gz", (3, 32, 32, 32)]
164164

165-
TEST_CASE_17 = [{"reader": "monai.data.ITKReader", "channel_dim": -1}, "test_image.nii.gz", (128, 128, 128, 3)]
165+
TEST_CASE_17 = [{"reader": "monai.data.ITKReader", "channel_dim": -1}, "test_image.nii.gz", (32, 32, 32, 3)]
166166

167167
TEST_CASE_18 = [
168168
{"reader": "ITKReader", "channel_dim": 2, "ensure_channel_first": True},
169169
"test_image.nii.gz",
170-
(128, 128, 3, 128),
170+
(32, 32, 3, 32),
171171
]
172172

173173
# test same dicom data with PydicomReader
@@ -202,8 +202,8 @@ def get_data(self, _img):
202202

203203
TESTS_META = []
204204
for track_meta in (False, True):
205-
TESTS_META.append([{}, (128, 128, 128), track_meta])
206-
TESTS_META.append([{"reader": "ITKReader", "fallback_only": False}, (128, 128, 128), track_meta])
205+
TESTS_META.append([{}, (32, 32, 32), track_meta])
206+
TESTS_META.append([{"reader": "ITKReader", "fallback_only": False}, (32, 32, 32), track_meta])
207207

208208

209209
class TestLoadImageReaderSelection(unittest.TestCase):
@@ -248,7 +248,7 @@ def tearDownClass(cls):
248248

249249
@parameterized.expand([TEST_CASE_1, TEST_CASE_3, TEST_CASE_3_1, TEST_CASE_4_1, TEST_CASE_5])
250250
def test_nibabel_reader(self, input_param, filenames, expected_shape):
251-
test_image = np.random.rand(128, 128, 128)
251+
test_image = np.random.rand(32, 32, 32)
252252
with tempfile.TemporaryDirectory() as tempdir:
253253
for i, name in enumerate(filenames):
254254
filenames[i] = os.path.join(tempdir, name)
@@ -268,9 +268,9 @@ def test_nibabel_reader_gpu(self, input_param, filenames, expected_shape):
268268
if torch.__version__.endswith("nv24.8"):
269269
# related issue: https://github.com/Project-MONAI/MONAI/issues/8274
270270
# for this version, use randint test case to avoid the issue
271-
test_image = torch.randint(0, 256, (128, 128, 128), dtype=torch.uint8).numpy()
271+
test_image = torch.randint(0, 256, (32, 32, 32), dtype=torch.uint8).numpy()
272272
else:
273-
test_image = np.random.rand(128, 128, 128)
273+
test_image = np.random.rand(32, 32, 32)
274274
with tempfile.TemporaryDirectory() as tempdir:
275275
for i, name in enumerate(filenames):
276276
filenames[i] = os.path.join(tempdir, name)
@@ -290,7 +290,7 @@ def test_nibabel_reader_gpu(self, input_param, filenames, expected_shape):
290290

291291
@parameterized.expand([TEST_CASE_6, TEST_CASE_8, TEST_CASE_8_1])
292292
def test_itk_reader(self, input_param, filenames, expected_shape):
293-
test_image = torch.randint(0, 256, (128, 128, 128), dtype=torch.uint8).numpy()
293+
test_image = torch.randint(0, 256, (32, 32, 32), dtype=torch.uint8).numpy()
294294
print("Test image value range:", test_image.min(), test_image.max())
295295
with tempfile.TemporaryDirectory() as tempdir:
296296
for i, name in enumerate(filenames):
@@ -501,7 +501,7 @@ def test_channel_dim(self, input_param, filename, expected_shape):
501501
result = LoadImage(image_only=True, **input_param)(filename) # with itk, meta has 'qto_xyz': itkMatrixF44
502502

503503
self.assertTupleEqual(
504-
result.shape, (3, 128, 128, 128) if input_param.get("ensure_channel_first", False) else expected_shape
504+
result.shape, (3, 32, 32, 32) if input_param.get("ensure_channel_first", False) else expected_shape
505505
)
506506
self.assertEqual(result.meta["original_channel_dim"], input_param["channel_dim"])
507507

@@ -512,7 +512,7 @@ class TestLoadImageMeta(unittest.TestCase):
512512
def setUpClass(cls):
513513
super().setUpClass()
514514
cls.tmpdir = tempfile.mkdtemp()
515-
test_image = nib.Nifti1Image(np.random.rand(128, 128, 128), np.eye(4))
515+
test_image = nib.Nifti1Image(np.random.rand(32, 32, 32), np.eye(4))
516516
nib.save(test_image, os.path.join(cls.tmpdir, "im.nii.gz"))
517517
cls.test_data = os.path.join(cls.tmpdir, "im.nii.gz")
518518

@@ -541,7 +541,7 @@ def test_track_meta_false_ensure_channel_first(self):
541541
try:
542542
set_track_meta(False)
543543
r = LoadImage(image_only=True, ensure_channel_first=True)(self.test_data)
544-
self.assertTupleEqual(r.shape, (1, 128, 128, 128))
544+
self.assertTupleEqual(r.shape, (1, 32, 32, 32))
545545
self.assertIsInstance(r, torch.Tensor)
546546
self.assertNotIsInstance(r, MetaTensor)
547547
finally:

‎tests/transforms/test_load_imaged.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@
3434

3535
KEYS = ["image", "label", "extra"]
3636

37-
TEST_CASE_1 = [{"keys": KEYS}, (128, 128, 128)]
37+
TEST_CASE_1 = [{"keys": KEYS}, (32, 32, 32)]
3838

39-
TEST_CASE_2 = [{"keys": KEYS, "reader": "ITKReader", "fallback_only": False}, (128, 128, 128)]
39+
TEST_CASE_2 = [{"keys": KEYS, "reader": "ITKReader", "fallback_only": False}, (32, 32, 32)]
4040

4141
TESTS_META = []
4242
for track_meta in (False, True):
43-
TESTS_META.append([{"keys": KEYS}, (128, 128, 128), track_meta])
44-
TESTS_META.append([{"keys": KEYS, "reader": "ITKReader", "fallback_only": False}, (128, 128, 128), track_meta])
43+
TESTS_META.append([{"keys": KEYS}, (32, 32, 32), track_meta])
44+
TESTS_META.append([{"keys": KEYS, "reader": "ITKReader", "fallback_only": False}, (32, 32, 32), track_meta])
4545

4646

4747
@unittest.skipUnless(has_itk, "itk not installed")
4848
class TestLoadImaged(unittest.TestCase):
4949
@parameterized.expand([TEST_CASE_1, TEST_CASE_2])
5050
def test_shape(self, input_param, expected_shape):
51-
test_image = nib.Nifti1Image(np.random.rand(128, 128, 128), np.eye(4))
51+
test_image = nib.Nifti1Image(np.random.rand(32, 32, 32), np.eye(4))
5252
test_data = {}
5353
with tempfile.TemporaryDirectory() as tempdir:
5454
for key in KEYS:
@@ -159,7 +159,7 @@ class TestLoadImagedMeta(unittest.TestCase):
159159
def setUpClass(cls):
160160
super().setUpClass()
161161
cls.tmpdir = tempfile.mkdtemp()
162-
test_image = nib.Nifti1Image(np.random.rand(128, 128, 128), np.eye(4))
162+
test_image = nib.Nifti1Image(np.random.rand(32, 32, 32), np.eye(4))
163163
cls.test_data = {}
164164
for key in KEYS:
165165
nib.save(test_image, os.path.join(cls.tmpdir, key + ".nii.gz"))

0 commit comments

Comments
 (0)