Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 8 additions & 8 deletions tests/data/test_arraydataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
Compose([LoadImage(image_only=True), EnsureChannelFirst(channel_dim="no_channel"), RandGaussianNoise(prob=1.0)]),
Compose([LoadImage(image_only=True), EnsureChannelFirst(channel_dim="no_channel"), RandGaussianNoise(prob=1.0)]),
(0, 1),
(1, 128, 128, 128),
(1, 32, 32, 32),
]

TEST_CASE_2 = [
Compose([LoadImage(image_only=True), EnsureChannelFirst(channel_dim="no_channel"), RandAdjustContrast(prob=1.0)]),
Compose([LoadImage(image_only=True), EnsureChannelFirst(channel_dim="no_channel"), RandAdjustContrast(prob=1.0)]),
(0, 1),
(1, 128, 128, 128),
(1, 32, 32, 32),
]


Expand Down Expand Up @@ -69,20 +69,20 @@ def __call__(self, input_, lazy=False):
]
),
(0, 2),
(1, 64, 64, 33),
(1, 16, 16, 9),
]

TEST_CASE_4 = [
Compose([LoadImage(image_only=True), EnsureChannelFirst(channel_dim="no_channel"), RandGaussianNoise(prob=1.0)]),
(1, 128, 128, 128),
(1, 32, 32, 32),
]


class TestArrayDataset(unittest.TestCase):

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

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

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

@parameterized.expand([TEST_CASE_4])
def test_dataloading_img_label(self, img_transform, expected_shape):
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=(128, 128, 128)).astype(float), np.eye(4))
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=(32, 32, 32)).astype(float), np.eye(4))
with tempfile.TemporaryDirectory() as tempdir:
test_image1 = os.path.join(tempdir, "test_image1.nii.gz")
test_image2 = os.path.join(tempdir, "test_image2.nii.gz")
Expand Down
8 changes: 4 additions & 4 deletions tests/data/test_cachedataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
from monai.data import CacheDataset, DataLoader, PersistentDataset, SmartCacheDataset
from monai.transforms import Compose, Lambda, LoadImaged, RandLambda, ThreadUnsafe, Transform

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

TEST_CASE_2 = [None, (128, 128, 128)]
TEST_CASE_2 = [None, (32, 32, 32)]

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

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

@parameterized.expand([TEST_CASE_1, TEST_CASE_2])
def test_hash_as_key(self, transform, expected_shape):
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[128, 128, 128]).astype(float), np.eye(4))
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[32, 32, 32]).astype(float), np.eye(4))
with tempfile.TemporaryDirectory() as tempdir:
test_data = []
for i in ["1", "2", "2", "3", "3"]:
Expand Down
6 changes: 3 additions & 3 deletions tests/data/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
from monai.transforms import Compose, Lambda, LoadImage, LoadImaged, SimulateDelay, SimulateDelayd
from tests.transforms.compose.test_compose import TEST_COMPOSE_LAZY_ON_CALL_LOGGING_TEST_CASES, data_from_keys

TEST_CASE_1 = [(128, 128, 128)]
TEST_CASE_1 = [(32, 32, 32)]


class TestDataset(unittest.TestCase):
@parameterized.expand([TEST_CASE_1])
def test_shape(self, expected_shape):
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[128, 128, 128]).astype(float), np.eye(4))
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[32, 32, 32]).astype(float), np.eye(4))
with tempfile.TemporaryDirectory() as tempdir:
nib.save(test_image, os.path.join(tempdir, "test_image1.nii.gz"))
nib.save(test_image, os.path.join(tempdir, "test_label1.nii.gz"))
Expand Down Expand Up @@ -101,7 +101,7 @@ def test_dataset_lazy_on_call(self):
class TestTupleDataset(unittest.TestCase):
@parameterized.expand([TEST_CASE_1])
def test_shape(self, expected_shape):
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[128, 128, 128]).astype(float), np.eye(4))
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[32, 32, 32]).astype(float), np.eye(4))
with tempfile.TemporaryDirectory() as tempdir:
nib.save(test_image, os.path.join(tempdir, "test_image1.nii.gz"))
nib.save(test_image, os.path.join(tempdir, "test_label1.nii.gz"))
Expand Down
2 changes: 1 addition & 1 deletion tests/data/test_mapping_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


def create_input_file(temp_dir, name):
test_image = np.random.rand(128, 128, 128)
test_image = np.random.rand(16, 16, 16)
output_ext = ".nii.gz"
input_file = os.path.join(temp_dir, name + output_ext)
nib.save(nib.Nifti1Image(test_image, np.eye(4)), input_file)
Expand Down
62 changes: 31 additions & 31 deletions tests/transforms/test_load_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,64 +52,64 @@ def get_data(self, _obj):
return np.zeros((1, 1, 1)), {"name": "my test"}


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

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

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

TEST_CASE_3_1 = [ # .mgz format
{"reader": "nibabelreader"},
["test_image.mgz", "test_image2.mgz", "test_image3.mgz"],
(3, 128, 128, 128),
(3, 32, 32, 32),
]

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

TEST_CASE_4_1 = [ # additional parameter
{"mmap": False},
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
(3, 128, 128, 128),
(3, 32, 32, 32),
]

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

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

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

TEST_CASE_GPU_3 = [
{"reader": "nibabelreader", "to_gpu": True},
["test_image.nii", "test_image2.nii", "test_image3.nii"],
(3, 128, 128, 128),
(3, 32, 32, 32),
]

TEST_CASE_GPU_4 = [
{"reader": "nibabelreader", "to_gpu": True},
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
(3, 128, 128, 128),
(3, 32, 32, 32),
]

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

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

TEST_CASE_8 = [
{"reader": ITKReader() if has_itk else "itkreader"},
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
(3, 128, 128, 128),
(3, 32, 32, 32),
]

TEST_CASE_8_1 = [
{"reader": ITKReader(channel_dim=0) if has_itk else "itkreader"},
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
(384, 128, 128),
(96, 32, 32),
]

TEST_CASE_9 = [
{"reader": ITKReader() if has_itk else "itkreader"},
["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"],
(3, 128, 128, 128),
(3, 32, 32, 32),
]

TEST_CASE_10 = [
Expand All @@ -128,24 +128,24 @@ def get_data(self, _obj):
(4, 16, 16),
]

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

TEST_CASE_14 = [
{"reader": "nibabelreader", "channel_dim": -1, "ensure_channel_first": True},
"test_image.nii.gz",
(128, 128, 128, 3),
(32, 32, 32, 3),
]

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

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

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

TEST_CASE_18 = [
{"reader": "ITKReader", "channel_dim": 2, "ensure_channel_first": True},
"test_image.nii.gz",
(128, 128, 3, 128),
(32, 32, 3, 32),
]

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

TESTS_META = []
for track_meta in (False, True):
TESTS_META.append([{}, (128, 128, 128), track_meta])
TESTS_META.append([{"reader": "ITKReader", "fallback_only": False}, (128, 128, 128), track_meta])
TESTS_META.append([{}, (32, 32, 32), track_meta])
TESTS_META.append([{"reader": "ITKReader", "fallback_only": False}, (32, 32, 32), track_meta])


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

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

self.assertTupleEqual(
result.shape, (3, 128, 128, 128) if input_param.get("ensure_channel_first", False) else expected_shape
result.shape, (3, 32, 32, 32) if input_param.get("ensure_channel_first", False) else expected_shape
)
self.assertEqual(result.meta["original_channel_dim"], input_param["channel_dim"])

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

Expand Down Expand Up @@ -502,7 +502,7 @@ def test_track_meta_false_ensure_channel_first(self):
try:
set_track_meta(False)
r = LoadImage(image_only=True, ensure_channel_first=True)(self.test_data)
self.assertTupleEqual(r.shape, (1, 128, 128, 128))
self.assertTupleEqual(r.shape, (1, 32, 32, 32))
self.assertIsInstance(r, torch.Tensor)
self.assertNotIsInstance(r, MetaTensor)
finally:
Expand Down
12 changes: 6 additions & 6 deletions tests/transforms/test_load_imaged.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@

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

TEST_CASE_1 = [{"keys": KEYS}, (128, 128, 128)]
TEST_CASE_1 = [{"keys": KEYS}, (32, 32, 32)]

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

TESTS_META = []
for track_meta in (False, True):
TESTS_META.append([{"keys": KEYS}, (128, 128, 128), track_meta])
TESTS_META.append([{"keys": KEYS, "reader": "ITKReader", "fallback_only": False}, (128, 128, 128), track_meta])
TESTS_META.append([{"keys": KEYS}, (32, 32, 32), track_meta])
TESTS_META.append([{"keys": KEYS, "reader": "ITKReader", "fallback_only": False}, (32, 32, 32), track_meta])


@unittest.skipUnless(has_itk, "itk not installed")
class TestLoadImaged(unittest.TestCase):
@parameterized.expand([TEST_CASE_1, TEST_CASE_2])
def test_shape(self, input_param, expected_shape):
test_image = nib.Nifti1Image(np.random.rand(128, 128, 128), np.eye(4))
test_image = nib.Nifti1Image(np.random.rand(32, 32, 32), np.eye(4))
test_data = {}
with tempfile.TemporaryDirectory() as tempdir:
for key in KEYS:
Expand Down Expand Up @@ -159,7 +159,7 @@ class TestLoadImagedMeta(unittest.TestCase):
def setUpClass(cls):
super().setUpClass()
cls.tmpdir = tempfile.mkdtemp()
test_image = nib.Nifti1Image(np.random.rand(128, 128, 128), np.eye(4))
test_image = nib.Nifti1Image(np.random.rand(32, 32, 32), np.eye(4))
cls.test_data = {}
for key in KEYS:
nib.save(test_image, os.path.join(cls.tmpdir, key + ".nii.gz"))
Expand Down
Loading