def test_itk_reader_multichannel(self): test_image = np.random.randint(0, 256, size=(256, 224, 3)).astype("uint8") with tempfile.TemporaryDirectory() as tempdir: filename = os.path.join(tempdir, "test_image.png") itk_np_view = itk.image_view_from_array(test_image, is_vector=True) itk.imwrite(itk_np_view, filename) for flag in (False, True): result = LoadImage(image_only=True, reader=ITKReader(reverse_indexing=flag))(Path(filename)) test_image = test_image.transpose(1, 0, 2) np.testing.assert_allclose(result[:, :, 0], test_image[:, :, 0]) np.testing.assert_allclose(result[:, :, 1], test_image[:, :, 1]) np.testing.assert_allclose(result[:, :, 2], test_image[:, :, 2])
def test_register(self): spatial_size = (32, 64, 128) test_image = np.random.rand(*spatial_size) with tempfile.TemporaryDirectory() as tempdir: filename = os.path.join(tempdir, "test_image.nii.gz") itk_np_view = itk.image_view_from_array(test_image) itk.imwrite(itk_np_view, filename) loader = LoadImaged(keys="img", image_only=True) loader.register(ITKReader()) result = loader({"img": Path(filename)}) self.assertTupleEqual(result["img"].shape, spatial_size[::-1])
def test_itk_reader_multichannel(self): test_image = np.random.randint(0, 256, size=(256, 256, 3)).astype("uint8") with tempfile.TemporaryDirectory() as tempdir: filename = os.path.join(tempdir, "test_image.png") itk_np_view = itk.image_view_from_array(test_image, is_vector=True) itk.imwrite(itk_np_view, filename) result, header = LoadImage(reader=ITKReader())(filename) self.assertTupleEqual(tuple(header["spatial_shape"]), (256, 256)) np.testing.assert_allclose(result[0, :, :], test_image[:, :, 0]) np.testing.assert_allclose(result[1, :, :], test_image[:, :, 1]) np.testing.assert_allclose(result[2, :, :], test_image[:, :, 2])
def test_channel_dim(self): spatial_size = (32, 64, 3, 128) test_image = np.random.rand(*spatial_size) with tempfile.TemporaryDirectory() as tempdir: filename = os.path.join(tempdir, "test_image.nii.gz") nib.save(nib.Nifti1Image(test_image, affine=np.eye(4)), filename) loader = LoadImaged(keys="img", image_only=True) loader.register(ITKReader(channel_dim=2)) t = Compose([EnsureChannelFirstD("img"), FromMetaTensord("img")]) result = t(loader({"img": filename})) self.assertTupleEqual(result["img"].shape, (3, 32, 64, 128))
def test_channel_dim(self): spatial_size = (32, 64, 3, 128) test_image = np.random.rand(*spatial_size) with tempfile.TemporaryDirectory() as tempdir: filename = os.path.join(tempdir, "test_image.nii.gz") nib.save(nib.Nifti1Image(test_image, affine=np.eye(4)), filename) loader = LoadImaged(keys="img") loader.register(ITKReader(channel_dim=2)) result = EnsureChannelFirstD("img")(loader({"img": filename})) self.assertTupleEqual(tuple(result["img_meta_dict"]["spatial_shape"]), (32, 64, 128)) self.assertTupleEqual(result["img"].shape, (3, 32, 64, 128))
def test_register(self): spatial_size = (32, 64, 128) test_image = np.random.rand(*spatial_size) with tempfile.TemporaryDirectory() as tempdir: filename = os.path.join(tempdir, "test_image.nii.gz") itk_np_view = itk.image_view_from_array(test_image) itk.imwrite(itk_np_view, filename) loader = LoadImage(image_only=False) loader.register(ITKReader()) result, header = loader(filename) self.assertTupleEqual(tuple(header["spatial_shape"]), spatial_size) self.assertTupleEqual(result.shape, spatial_size)
def test_load_nifti_multichannel(self): test_image = np.random.randint(0, 256, size=(31, 64, 16, 2)).astype(np.float32) with tempfile.TemporaryDirectory() as tempdir: filename = os.path.join(tempdir, "test_image.nii.gz") itk_np_view = itk.image_view_from_array(test_image, is_vector=True) itk.imwrite(itk_np_view, filename) itk_img = LoadImage(image_only=True, reader=ITKReader())(Path(filename)) self.assertTupleEqual(tuple(itk_img.shape), (16, 64, 31, 2)) nib_image = LoadImage(image_only=True, reader=NibabelReader(squeeze_non_spatial_dims=True))(Path(filename)) self.assertTupleEqual(tuple(nib_image.shape), (16, 64, 31, 2)) np.testing.assert_allclose(itk_img, nib_image, atol=1e-3, rtol=1e-3)
def test_readers(self): inst = ITKReader() self.assertIsInstance(inst, ITKReader) inst = NibabelReader() self.assertIsInstance(inst, NibabelReader) inst = NibabelReader(as_closest_canonical=True) self.assertIsInstance(inst, NibabelReader) inst = NumpyReader() self.assertIsInstance(inst, NumpyReader) inst = NumpyReader(npz_keys="test") self.assertIsInstance(inst, NumpyReader) inst = PILReader() self.assertIsInstance(inst, PILReader)
def test_register(self): spatial_size = (32, 64, 128) expected_shape = (128, 64, 32) test_image = np.random.rand(*spatial_size) with tempfile.TemporaryDirectory() as tempdir: filename = os.path.join(tempdir, "test_image.nii.gz") itk_np_view = itk.image_view_from_array(test_image) itk.imwrite(itk_np_view, filename) loader = LoadImaged(keys="img") loader.register(ITKReader(c_order_axis_indexing=True)) result = loader({"img": filename}) self.assertTupleEqual( tuple(result["img_meta_dict"]["spatial_shape"]), expected_shape) self.assertTupleEqual(result["img"].shape, spatial_size)
def test_kwargs(self): spatial_size = (32, 64, 128) test_image = np.random.rand(*spatial_size) with tempfile.TemporaryDirectory() as tempdir: filename = os.path.join(tempdir, "test_image.nii.gz") itk_np_view = itk.image_view_from_array(test_image) itk.imwrite(itk_np_view, filename) loader = LoadImage(image_only=True) reader = ITKReader(fallback_only=False) loader.register(reader) result = loader(filename) reader = ITKReader() img = reader.read(filename, fallback_only=False) result_raw = reader.get_data(img) result_raw = MetaTensor.ensure_torch_and_prune_meta(*result_raw) self.assertTupleEqual(result.shape, result_raw.shape)
def test_kwargs(self): spatial_size = (32, 64, 128) test_image = np.random.rand(*spatial_size) with tempfile.TemporaryDirectory() as tempdir: filename = os.path.join(tempdir, "test_image.nii.gz") itk_np_view = itk.image_view_from_array(test_image) itk.imwrite(itk_np_view, filename) loader = LoadImage(image_only=False) reader = ITKReader(fallback_only=False) loader.register(reader) result, header = loader(filename) reader = ITKReader() img = reader.read(filename, fallback_only=False) result_raw, header_raw = reader.get_data(img) np.testing.assert_allclose(header["spatial_shape"], header_raw["spatial_shape"]) self.assertTupleEqual(result.shape, result_raw.shape)
TEST_CASE_4_1 = [ # additional parameter { "image_only": False, "mmap": False }, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128), ] TEST_CASE_5 = [{ "reader": NibabelReader(mmap=False), "image_only": False }, ["test_image.nii.gz"], (128, 128, 128)] TEST_CASE_6 = [{ "reader": ITKReader(), "image_only": True }, ["test_image.nii.gz"], (128, 128, 128)] TEST_CASE_7 = [{ "reader": ITKReader(), "image_only": False }, ["test_image.nii.gz"], (128, 128, 128)] TEST_CASE_8 = [ { "reader": ITKReader(), "image_only": True }, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128),
"test_image3.nii.gz" ], (3, 128, 128, 128)] TEST_CASE_4_1 = [ # additional parameter { "mmap": False }, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128), ] TEST_CASE_5 = [{ "reader": NibabelReader(mmap=False) }, ["test_image.nii.gz"], (128, 128, 128)] TEST_CASE_6 = [{"reader": ITKReader()}, ["test_image.nii.gz"], (128, 128, 128)] TEST_CASE_7 = [{"reader": ITKReader()}, ["test_image.nii.gz"], (128, 128, 128)] TEST_CASE_8 = [ { "reader": ITKReader() }, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128), ] TEST_CASE_8_1 = [ { "reader": ITKReader(channel_dim=0) },
(3, 128, 128, 128), ] TEST_CASE_4 = [ {"image_only": False}, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128), ] TEST_CASE_5 = [ {"reader": NibabelReader(mmap=False), "image_only": False}, ["test_image.nii.gz"], (128, 128, 128), ] TEST_CASE_6 = [{"reader": ITKReader(), "image_only": True}, ["test_image.nii.gz"], (128, 128, 128)] TEST_CASE_7 = [{"reader": ITKReader(), "image_only": False}, ["test_image.nii.gz"], (128, 128, 128)] TEST_CASE_8 = [ {"reader": ITKReader(), "image_only": True}, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128), ] TEST_CASE_9 = [ {"reader": ITKReader(), "image_only": False}, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128), ]
from monai.data import ITKReader from monai.data.meta_tensor import MetaTensor from monai.transforms import EnsureChannelFirst, LoadImage TEST_CASE_1 = [{}, ["test_image.nii.gz"], None] TEST_CASE_2 = [{}, ["test_image.nii.gz"], -1] TEST_CASE_3 = [{}, [ "test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz" ], None] TEST_CASE_4 = [{"reader": ITKReader()}, ["test_image.nii.gz"], None] TEST_CASE_5 = [{"reader": ITKReader()}, ["test_image.nii.gz"], -1] TEST_CASE_6 = [{ "reader": ITKReader() }, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], None] TEST_CASE_7 = [{ "reader": ITKReader(pixel_type=itk.UC) }, "tests/testing_data/CT_DICOM", None] class TestEnsureChannelFirst(unittest.TestCase): @parameterized.expand([ TEST_CASE_1, TEST_CASE_2, TEST_CASE_3, TEST_CASE_4, TEST_CASE_5,
{"reader": "nibabelreader"}, ["test_image.mgz", "test_image2.mgz", "test_image3.mgz"], (3, 128, 128, 128), ] TEST_CASE_4 = [{}, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128)] TEST_CASE_4_1 = [ # additional parameter {"mmap": False}, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128), ] TEST_CASE_5 = [{"reader": NibabelReader(mmap=False)}, ["test_image.nii.gz"], (128, 128, 128)] TEST_CASE_6 = [{"reader": ITKReader()}, ["test_image.nii.gz"], (128, 128, 128)] TEST_CASE_7 = [{"reader": ITKReader()}, ["test_image.nii.gz"], (128, 128, 128)] TEST_CASE_8 = [ {"reader": ITKReader()}, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128), ] TEST_CASE_8_1 = [ {"reader": ITKReader(channel_dim=0)}, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (384, 128, 128), ]
TEST_CASE_7 = [{"image_only": False}, ["test_image.nii.gz"], (128, 128, 128)] TEST_CASE_8 = [ {"image_only": True}, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128), ] TEST_CASE_9 = [ {"image_only": False}, ["test_image.nii.gz", "test_image2.nii.gz", "test_image3.nii.gz"], (3, 128, 128, 128), ] TEST_CASE_10 = [ {"image_only": False, "reader": ITKReader(pixel_type=itk.UC)}, "tests/testing_data/CT_DICOM", (4, 16, 16), ] class TestLoadImage(unittest.TestCase): @parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3, TEST_CASE_4, TEST_CASE_5]) def test_nibabel_reader(self, input_param, filenames, expected_shape): test_image = np.random.rand(128, 128, 128) with tempfile.TemporaryDirectory() as tempdir: for i, name in enumerate(filenames): filenames[i] = os.path.join(tempdir, name) nib.save(nib.Nifti1Image(test_image, np.eye(4)), filenames[i]) result = LoadImage(**input_param)(filenames)