Skip to content

Commit 22d45da

Browse files
committed
Perf(test): shrink synthetic volumes in image loading and dataset tests
Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
1 parent ed76cd5 commit 22d45da

6 files changed

Lines changed: 53 additions & 53 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: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,64 +52,64 @@ def get_data(self, _obj):
5252
return np.zeros((1, 1, 1)), {"name": "my test"}
5353

5454

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

57-
TEST_CASE_2 = [{}, ["test_image.nii.gz"], (128, 128, 128)]
57+
TEST_CASE_2 = [{}, ["test_image.nii.gz"], (32, 32, 32)]
5858

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

6161
TEST_CASE_3_1 = [ # .mgz format
6262
{"reader": "nibabelreader"},
6363
["test_image.mgz", "test_image2.mgz", "test_image3.mgz"],
64-
(3, 128, 128, 128),
64+
(3, 32, 32, 32),
6565
]
6666

67-
TEST_CASE_4 = [{}, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128)]
67+
TEST_CASE_4 = [{}, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 32, 32, 32)]
6868

6969
TEST_CASE_4_1 = [ # additional parameter
7070
{"mmap": False},
7171
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
72-
(3, 128, 128, 128),
72+
(3, 32, 32, 32),
7373
]
7474

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

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

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

8181
TEST_CASE_GPU_3 = [
8282
{"reader": "nibabelreader", "to_gpu": True},
8383
["test_image.nii", "test_image2.nii", "test_image3.nii"],
84-
(3, 128, 128, 128),
84+
(3, 32, 32, 32),
8585
]
8686

8787
TEST_CASE_GPU_4 = [
8888
{"reader": "nibabelreader", "to_gpu": True},
8989
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
90-
(3, 128, 128, 128),
90+
(3, 32, 32, 32),
9191
]
9292

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

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

9797
TEST_CASE_8 = [
9898
{"reader": ITKReader() if has_itk else "itkreader"},
9999
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
100-
(3, 128, 128, 128),
100+
(3, 32, 32, 32),
101101
]
102102

103103
TEST_CASE_8_1 = [
104104
{"reader": ITKReader(channel_dim=0) if has_itk else "itkreader"},
105105
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
106-
(384, 128, 128),
106+
(96, 32, 32),
107107
]
108108

109109
TEST_CASE_9 = [
110110
{"reader": ITKReader() if has_itk else "itkreader"},
111111
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
112-
(3, 128, 128, 128),
112+
(3, 32, 32, 32),
113113
]
114114

115115
TEST_CASE_10 = [
@@ -128,24 +128,24 @@ def get_data(self, _obj):
128128
(4, 16, 16),
129129
]
130130

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

133133
TEST_CASE_14 = [
134134
{"reader": "nibabelreader", "channel_dim": -1, "ensure_channel_first": True},
135135
"test_image.nii.gz",
136-
(128, 128, 128, 3),
136+
(32, 32, 32, 3),
137137
]
138138

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

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

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

145145
TEST_CASE_18 = [
146146
{"reader": "ITKReader", "channel_dim": 2, "ensure_channel_first": True},
147147
"test_image.nii.gz",
148-
(128, 128, 3, 128),
148+
(32, 32, 3, 32),
149149
]
150150

151151
# test same dicom data with PydicomReader
@@ -180,8 +180,8 @@ def get_data(self, _obj):
180180

181181
TESTS_META = []
182182
for track_meta in (False, True):
183-
TESTS_META.append([{}, (128, 128, 128), track_meta])
184-
TESTS_META.append([{"reader": "ITKReader", "fallback_only": False}, (128, 128, 128), track_meta])
183+
TESTS_META.append([{}, (32, 32, 32), track_meta])
184+
TESTS_META.append([{"reader": "ITKReader", "fallback_only": False}, (32, 32, 32), track_meta])
185185

186186

187187
@unittest.skipUnless(has_itk, "itk not installed")
@@ -209,7 +209,7 @@ def tearDownClass(cls):
209209
[TEST_CASE_1, TEST_CASE_2, TEST_CASE_3, TEST_CASE_3_1, TEST_CASE_4, TEST_CASE_4_1, TEST_CASE_5]
210210
)
211211
def test_nibabel_reader(self, input_param, filenames, expected_shape):
212-
test_image = np.random.rand(128, 128, 128)
212+
test_image = np.random.rand(32, 32, 32)
213213
with tempfile.TemporaryDirectory() as tempdir:
214214
for i, name in enumerate(filenames):
215215
filenames[i] = os.path.join(tempdir, name)
@@ -229,9 +229,9 @@ def test_nibabel_reader_gpu(self, input_param, filenames, expected_shape):
229229
if torch.__version__.endswith("nv24.8"):
230230
# related issue: https://github.com/Project-MONAI/MONAI/issues/8274
231231
# for this version, use randint test case to avoid the issue
232-
test_image = torch.randint(0, 256, (128, 128, 128), dtype=torch.uint8).numpy()
232+
test_image = torch.randint(0, 256, (32, 32, 32), dtype=torch.uint8).numpy()
233233
else:
234-
test_image = np.random.rand(128, 128, 128)
234+
test_image = np.random.rand(32, 32, 32)
235235
with tempfile.TemporaryDirectory() as tempdir:
236236
for i, name in enumerate(filenames):
237237
filenames[i] = os.path.join(tempdir, name)
@@ -251,7 +251,7 @@ def test_nibabel_reader_gpu(self, input_param, filenames, expected_shape):
251251

252252
@parameterized.expand([TEST_CASE_6, TEST_CASE_7, TEST_CASE_8, TEST_CASE_8_1, TEST_CASE_9])
253253
def test_itk_reader(self, input_param, filenames, expected_shape):
254-
test_image = torch.randint(0, 256, (128, 128, 128), dtype=torch.uint8).numpy()
254+
test_image = torch.randint(0, 256, (32, 32, 32), dtype=torch.uint8).numpy()
255255
print("Test image value range:", test_image.min(), test_image.max())
256256
with tempfile.TemporaryDirectory() as tempdir:
257257
for i, name in enumerate(filenames):
@@ -462,7 +462,7 @@ def test_channel_dim(self, input_param, filename, expected_shape):
462462
result = LoadImage(image_only=True, **input_param)(filename) # with itk, meta has 'qto_xyz': itkMatrixF44
463463

464464
self.assertTupleEqual(
465-
result.shape, (3, 128, 128, 128) if input_param.get("ensure_channel_first", False) else expected_shape
465+
result.shape, (3, 32, 32, 32) if input_param.get("ensure_channel_first", False) else expected_shape
466466
)
467467
self.assertEqual(result.meta["original_channel_dim"], input_param["channel_dim"])
468468

@@ -473,7 +473,7 @@ class TestLoadImageMeta(unittest.TestCase):
473473
def setUpClass(cls):
474474
super().setUpClass()
475475
cls.tmpdir = tempfile.mkdtemp()
476-
test_image = nib.Nifti1Image(np.random.rand(128, 128, 128), np.eye(4))
476+
test_image = nib.Nifti1Image(np.random.rand(32, 32, 32), np.eye(4))
477477
nib.save(test_image, os.path.join(cls.tmpdir, "im.nii.gz"))
478478
cls.test_data = os.path.join(cls.tmpdir, "im.nii.gz")
479479

@@ -502,7 +502,7 @@ def test_track_meta_false_ensure_channel_first(self):
502502
try:
503503
set_track_meta(False)
504504
r = LoadImage(image_only=True, ensure_channel_first=True)(self.test_data)
505-
self.assertTupleEqual(r.shape, (1, 128, 128, 128))
505+
self.assertTupleEqual(r.shape, (1, 32, 32, 32))
506506
self.assertIsInstance(r, torch.Tensor)
507507
self.assertNotIsInstance(r, MetaTensor)
508508
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)