From b54ab19e9978591803227f8df01288d2426e8939 Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Tue, 5 Sep 2023 14:31:13 +0800 Subject: [PATCH 1/7] fix #6942 Signed-off-by: KumoLiu --- monai/data/image_reader.py | 1 + 1 file changed, 1 insertion(+) diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index 0f8de6b57d..1a78b4daf5 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -244,6 +244,7 @@ def read(self, data: Sequence[PathLike] | PathLike, **kwargs): series_identifier = series_uid[0] if not self.series_name else self.series_name name = names_generator.GetFileNames(series_identifier) + name = name[0] if len(name) == 1 else name _obj = itk.imread(name, **kwargs_) if self.series_meta: _reader = itk.ImageSeriesReader.New(FileNames=name) From 39b1b8ad4fd9e747792882abfbc2350c719ae632 Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Tue, 5 Sep 2023 15:06:17 +0800 Subject: [PATCH 2/7] fix mypy Signed-off-by: KumoLiu --- monai/data/image_reader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index 1a78b4daf5..24b5e548ee 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -244,7 +244,7 @@ def read(self, data: Sequence[PathLike] | PathLike, **kwargs): series_identifier = series_uid[0] if not self.series_name else self.series_name name = names_generator.GetFileNames(series_identifier) - name = name[0] if len(name) == 1 else name + name = name[0] if len(name) == 1 else name # type: ignore _obj = itk.imread(name, **kwargs_) if self.series_meta: _reader = itk.ImageSeriesReader.New(FileNames=name) From 7a3ddc710deb648864da8fd1f5e17d14600e4b64 Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Tue, 5 Sep 2023 17:29:52 +0800 Subject: [PATCH 3/7] add unittest; fix #6944 Signed-off-by: KumoLiu --- monai/data/image_reader.py | 6 ++-- tests/test_load_image.py | 35 ++++++++++++++++++++++++ tests/testing_data/CT_DICOM_SINGLE/7106 | Bin 0 -> 3808 bytes 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 tests/testing_data/CT_DICOM_SINGLE/7106 diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index 24b5e548ee..78877e2b40 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -632,7 +632,7 @@ def _get_meta_dict(self, img) -> dict: if self.prune_metadata: prune_metadata = {} - for key in ["00200037", "00200032", "52009229", "52009230"]: + for key in ["00200037", "00200032", "00280030", "52009229", "52009230"]: if key in metadata.keys(): prune_metadata[key] = metadata[key] return prune_metadata @@ -662,7 +662,9 @@ def _get_affine(self, metadata: dict, lps_to_ras: bool = True): rx, ry, rz, cx, cy, cz = metadata["00200037"]["Value"] # "00200032" is the tag of `ImagePositionPatient` sx, sy, sz = metadata["00200032"]["Value"] - dr, dc = metadata.get("spacing", (1.0, 1.0))[:2] + # "00280030" is the tag of `PixelSpacing` + spacing = metadata["00280030"]["Value"] + dr, dc = metadata.get("spacing", spacing)[:2] affine[0, 0] = cx * dr affine[0, 1] = rx * dc affine[0, 3] = sx diff --git a/tests/test_load_image.py b/tests/test_load_image.py index ec748f9951..aefc4ff884 100644 --- a/tests/test_load_image.py +++ b/tests/test_load_image.py @@ -151,6 +151,11 @@ def get_data(self, _obj): # test reader consistency between PydicomReader and ITKReader on dicom data TEST_CASE_22 = ["tests/testing_data/CT_DICOM"] +TEST_CASE_23 = ["tests/testing_data/CT_DICOM_SINGLE"] + +# test ITKReader with only one file in a folder +TEST_CASE_24 = [{"reader": "ITKReader"}, "tests/testing_data/CT_DICOM_SINGLE", (16, 16, 1)] + TESTS_META = [] for track_meta in (False, True): TESTS_META.append([{}, (128, 128, 128), track_meta]) @@ -206,6 +211,23 @@ def test_itk_dicom_series_reader(self, input_param, filenames, expected_shape, e ) self.assertTupleEqual(result.shape, expected_np_shape) + @parameterized.expand([TEST_CASE_24]) + def test_itk_dicom_series_reader_single(self, input_param, filenames, expected_shape): + result = LoadImage(image_only=True, **input_param)(filenames) + self.assertEqual(result.meta["filename_or_obj"], f"{Path(filenames)}") + assert_allclose( + result.affine, + torch.tensor( + [ + [-0.488281, 0.0, 0.0, 125.0], + [0.0, -0.488281, 0.0, 128.100006], + [0.0, 0.0, 1.0, -99.480003], + [0.0, 0.0, 0.0, 1.0], + ] + ), + ) + self.assertTupleEqual(result.shape, expected_shape) + def test_itk_reader_multichannel(self): test_image = np.random.randint(0, 256, size=(256, 224, 3)).astype("uint8") with tempfile.TemporaryDirectory() as tempdir: @@ -231,6 +253,19 @@ def test_dicom_reader_consistency(self, filenames): np.testing.assert_allclose(pydicom_result, itk_result) np.testing.assert_allclose(pydicom_result.affine, itk_result.affine) + @parameterized.expand([TEST_CASE_23]) + def test_dicom_reader_consistency_single(self, filenames): + itk_param = {"reader": "ITKReader"} + pydicom_param = {"reader": "PydicomReader"} + for affine_flag in [True, False]: + itk_param["affine_lps_to_ras"] = affine_flag + pydicom_param["affine_lps_to_ras"] = affine_flag + itk_result = LoadImage(image_only=True, **itk_param)(filenames) + pydicom_result = LoadImage(image_only=True, **pydicom_param)(filenames) + np.testing.assert_allclose(pydicom_result, itk_result.squeeze()) + np.testing.assert_allclose(pydicom_result.affine[:2, :2], itk_result.affine[:2, :2]) + np.testing.assert_allclose(pydicom_result.affine[..., 3], itk_result.affine[..., 3]) + 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: diff --git a/tests/testing_data/CT_DICOM_SINGLE/7106 b/tests/testing_data/CT_DICOM_SINGLE/7106 new file mode 100644 index 0000000000000000000000000000000000000000..727bea124b15505483d036c4f57e648ff62ec16c GIT binary patch literal 3808 zcmcImU2G#)6+ZXQZ0dw;GD&vRWU1OowlqQ8b!W!ozjoPrGM?B|Y)|5Gx`bw%4T+PO zwu#A4A1V-)cxVx2H(O|F`%+axfQnEnK;ox76i|^sJQNVT@Itj9frQEf6p)H2-@UeH zO}Zel5))1Q-E+=8|M$!#I`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n2yOVkPS2tsnhhio7nF*$#emiVAJ~ z=-pE^*P%n_e1xYF`2yl+G)6FE2jJ`Jz(KnY_SKFO(xSu#yk;2!jtVcA%PQpVIR>XWk< zgJi*Vy)Xo&9~44QidorDdEY5ig0SYhf|mddb2y#WbCl=Zca$7?ynRQ(7d}TdUJ6G4 z!8h*@N5?@M;+x-rGs;x%-KNe&QbJAeh?yS zp2#S&rjm!LFYF7Cz4l2w0)#sU`29dVni~#Kuu#*3S-s%k;I=CWBidMmT2&W* zPPS>i=23hRiF!?Ob0Yf9Cp8Mr!4Qm<)UsMs4fQ7VAIdArdF4%hk+1NO2fWU|#cidi z=GBwxACwoAyz&!1!(V0VY?j@{Y*u1lVrTem>YK_XWmI{FALAFu5zpLF#i?1kNt(dOpl3p7jjKw^;= zFrzEkp|g-l;KrRo2m9DFM^8Ze2Hgvs1usiB#px7olxIO}&~x-#_BK5RUvt=J zQ4+5WDG$7F&>;H+`#gW1{hl6&Rl%{L;~seXB8{-GFo%DK->ZC_Kh0i)hiSSCS}xGb z^j>zES^Vd`qCBPiLP;wQKgBM=#s&H`{gFP)GW<23L&jfMt|)IQk15abPq8cX68(%a z?04*|+*O_j@6XEH%0D6TG38nQBFms6UtlKx6Ms_qrSdoBh^ncB>WKP1C8IpTf688B Ye`SyJ)5`a;M^jIzH>(rssCuLNFYZc+=Kufz literal 0 HcmV?d00001 From b1512a14360681b11efd8c2e113ac781af9cc0ad Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Tue, 5 Sep 2023 18:12:01 +0800 Subject: [PATCH 4/7] address comments Signed-off-by: KumoLiu --- tests/test_load_image.py | 47 ++++++++++++++++-------- tests/testing_data/CT_DICOM_SINGLE/7106 | Bin 3808 -> 0 bytes tests/testing_data/data_config.json | 5 +++ 3 files changed, 37 insertions(+), 15 deletions(-) delete mode 100644 tests/testing_data/CT_DICOM_SINGLE/7106 diff --git a/tests/test_load_image.py b/tests/test_load_image.py index aefc4ff884..a5688de5ad 100644 --- a/tests/test_load_image.py +++ b/tests/test_load_image.py @@ -23,12 +23,13 @@ from parameterized import parameterized from PIL import Image +from monai.apps import download_and_extract from monai.data import NibabelReader, PydicomReader from monai.data.meta_obj import set_track_meta from monai.data.meta_tensor import MetaTensor from monai.transforms import LoadImage from monai.utils import optional_import -from tests.utils import assert_allclose +from tests.utils import assert_allclose, testing_data_config, skip_if_downloading_fails itk, has_itk = optional_import("itk", allow_namespace_pkg=True) ITKReader, _ = optional_import("monai.data", name="ITKReader", as_type="decorator") @@ -151,11 +152,6 @@ def get_data(self, _obj): # test reader consistency between PydicomReader and ITKReader on dicom data TEST_CASE_22 = ["tests/testing_data/CT_DICOM"] -TEST_CASE_23 = ["tests/testing_data/CT_DICOM_SINGLE"] - -# test ITKReader with only one file in a folder -TEST_CASE_24 = [{"reader": "ITKReader"}, "tests/testing_data/CT_DICOM_SINGLE", (16, 16, 1)] - TESTS_META = [] for track_meta in (False, True): TESTS_META.append([{}, (128, 128, 128), track_meta]) @@ -164,6 +160,29 @@ def get_data(self, _obj): @unittest.skipUnless(has_itk, "itk not installed") class TestLoadImage(unittest.TestCase): + @classmethod + def setUpClass(cls): + super(__class__, cls).setUpClass() + with skip_if_downloading_fails(): + cls.tmpdir = tempfile.mkdtemp() + key = "DICOM_single" + url = testing_data_config("images", key, "url") + hash_type = testing_data_config("images", key, "hash_type") + hash_val = testing_data_config("images", key, "hash_val") + download_and_extract( + url=url, + output_dir=cls.tmpdir, + hash_val=hash_val, + hash_type=hash_type, + file_type="zip", + ) + cls.data_dir = os.path.join(cls.tmpdir, "CT_DICOM_SINGLE") + + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdir) + super(__class__, cls).tearDownClass() + @parameterized.expand( [TEST_CASE_1, TEST_CASE_2, TEST_CASE_3, TEST_CASE_3_1, TEST_CASE_4, TEST_CASE_4_1, TEST_CASE_5] ) @@ -211,10 +230,9 @@ def test_itk_dicom_series_reader(self, input_param, filenames, expected_shape, e ) self.assertTupleEqual(result.shape, expected_np_shape) - @parameterized.expand([TEST_CASE_24]) - def test_itk_dicom_series_reader_single(self, input_param, filenames, expected_shape): - result = LoadImage(image_only=True, **input_param)(filenames) - self.assertEqual(result.meta["filename_or_obj"], f"{Path(filenames)}") + def test_itk_dicom_series_reader_single(self): + result = LoadImage(image_only=True, reader="ITKReader")(self.data_dir) + self.assertEqual(result.meta["filename_or_obj"], f"{Path(self.data_dir)}") assert_allclose( result.affine, torch.tensor( @@ -226,7 +244,7 @@ def test_itk_dicom_series_reader_single(self, input_param, filenames, expected_s ] ), ) - self.assertTupleEqual(result.shape, expected_shape) + self.assertTupleEqual(result.shape, (16, 16, 1)) def test_itk_reader_multichannel(self): test_image = np.random.randint(0, 256, size=(256, 224, 3)).astype("uint8") @@ -253,15 +271,14 @@ def test_dicom_reader_consistency(self, filenames): np.testing.assert_allclose(pydicom_result, itk_result) np.testing.assert_allclose(pydicom_result.affine, itk_result.affine) - @parameterized.expand([TEST_CASE_23]) - def test_dicom_reader_consistency_single(self, filenames): + def test_dicom_reader_consistency_single(self): itk_param = {"reader": "ITKReader"} pydicom_param = {"reader": "PydicomReader"} for affine_flag in [True, False]: itk_param["affine_lps_to_ras"] = affine_flag pydicom_param["affine_lps_to_ras"] = affine_flag - itk_result = LoadImage(image_only=True, **itk_param)(filenames) - pydicom_result = LoadImage(image_only=True, **pydicom_param)(filenames) + itk_result = LoadImage(image_only=True, **itk_param)(self.data_dir) + pydicom_result = LoadImage(image_only=True, **pydicom_param)(self.data_dir) np.testing.assert_allclose(pydicom_result, itk_result.squeeze()) np.testing.assert_allclose(pydicom_result.affine[:2, :2], itk_result.affine[:2, :2]) np.testing.assert_allclose(pydicom_result.affine[..., 3], itk_result.affine[..., 3]) diff --git a/tests/testing_data/CT_DICOM_SINGLE/7106 b/tests/testing_data/CT_DICOM_SINGLE/7106 deleted file mode 100644 index 727bea124b15505483d036c4f57e648ff62ec16c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3808 zcmcImU2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=Po>~{W}Jq!5GPLT9z)(Z`LJ_BB4P2Q+~0|PoNkr- z%?<$)@00r(_TMHwS;u!|oTlVHtDUoPx?S?VeS_e|59z!^>da>BNIENZa>&j^-nkJf zMBJ)G!uChoG@XuK$Luq4@&>MBpM`x|dv@tGgxN2qn2yOVkPS2tsnhhio7nF*$#emiVAJ~ z=-pE^*P%n_e1xYF`2yl+G)6FE2jJ`Jz(KnY_SKFO(xSu#yk;2!jtVcA%PQpVIR>XWk< zgJi*Vy)Xo&9~44QidorDdEY5ig0SYhf|mddb2y#WbCl=Zca$7?ynRQ(7d}TdUJ6G4 z!8h*@N5?@M;+x-rGs;x%-KNe&QbJAeh?yS zp2#S&rjm!LFYF7Cz4l2w0)#sU`29dVni~#Kuu#*3S-s%k;I=CWBidMmT2&W* zPPS>i=23hRiF!?Ob0Yf9Cp8Mr!4Qm<)UsMs4fQ7VAIdArdF4%hk+1NO2fWU|#cidi z=GBwxACwoAyz&!1!(V0VY?j@{Y*u1lVrTem>YK_XWmI{FALAFu5zpLF#i?1kNt(dOpl3p7jjKw^;= zFrzEkp|g-l;KrRo2m9DFM^8Ze2Hgvs1usiB#px7olxIO}&~x-#_BK5RUvt=J zQ4+5WDG$7F&>;H+`#gW1{hl6&Rl%{L;~seXB8{-GFo%DK->ZC_Kh0i)hiSSCS}xGb z^j>zES^Vd`qCBPiLP;wQKgBM=#s&H`{gFP)GW<23L&jfMt|)IQk15abPq8cX68(%a z?04*|+*O_j@6XEH%0D6TG38nQBFms6UtlKx6Ms_qrSdoBh^ncB>WKP1C8IpTf688B Ye`SyJ)5`a;M^jIzH>(rssCuLNFYZc+=Kufz diff --git a/tests/testing_data/data_config.json b/tests/testing_data/data_config.json index abda77f7eb..4bdac6abba 100644 --- a/tests/testing_data/data_config.json +++ b/tests/testing_data/data_config.json @@ -79,6 +79,11 @@ "url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/CT_2D_head_moving.mha", "hash_type": "sha256", "hash_val": "a37c5fe388c38b3f4ac564f456277d09d3982eda58c4da05ead8ee2332360f47" + }, + "DICOM_single": { + "url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/CT_DICOM_SINGLE.zip", + "hash_type": "sha256", + "hash_val": "a41f6e93d2e3d68956144f9a847273041d36441da12377d6a1d5ae610e0a7023" } }, "videos": { From 982755e5a567f1c8a3cd93ac44a9684af2d030ed Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Tue, 5 Sep 2023 18:12:25 +0800 Subject: [PATCH 5/7] fix flake8 Signed-off-by: KumoLiu --- tests/test_load_image.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/test_load_image.py b/tests/test_load_image.py index a5688de5ad..42c356b74a 100644 --- a/tests/test_load_image.py +++ b/tests/test_load_image.py @@ -29,7 +29,7 @@ from monai.data.meta_tensor import MetaTensor from monai.transforms import LoadImage from monai.utils import optional_import -from tests.utils import assert_allclose, testing_data_config, skip_if_downloading_fails +from tests.utils import assert_allclose, skip_if_downloading_fails, testing_data_config itk, has_itk = optional_import("itk", allow_namespace_pkg=True) ITKReader, _ = optional_import("monai.data", name="ITKReader", as_type="decorator") @@ -170,11 +170,7 @@ def setUpClass(cls): hash_type = testing_data_config("images", key, "hash_type") hash_val = testing_data_config("images", key, "hash_val") download_and_extract( - url=url, - output_dir=cls.tmpdir, - hash_val=hash_val, - hash_type=hash_type, - file_type="zip", + url=url, output_dir=cls.tmpdir, hash_val=hash_val, hash_type=hash_type, file_type="zip" ) cls.data_dir = os.path.join(cls.tmpdir, "CT_DICOM_SINGLE") From 26e891397aac6216f6ef36b1680812e910a1d11e Mon Sep 17 00:00:00 2001 From: Wenqi Li <831580+wyli@users.noreply.github.com> Date: Tue, 5 Sep 2023 11:44:50 +0100 Subject: [PATCH 6/7] fixes pydicom default affine 2d slice --- monai/data/image_reader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index 78877e2b40..e5ddbf8f33 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -673,7 +673,7 @@ def _get_affine(self, metadata: dict, lps_to_ras: bool = True): affine[1, 3] = sy affine[2, 0] = cz * dr affine[2, 1] = rz * dc - affine[2, 2] = 0 + affine[2, 2] = 1.0 affine[2, 3] = sz # 3d From b5038a32f8e65b07cc87517ffd08b31de7498c87 Mon Sep 17 00:00:00 2001 From: Wenqi Li <831580+wyli@users.noreply.github.com> Date: Tue, 5 Sep 2023 11:47:40 +0100 Subject: [PATCH 7/7] consistent affien pydicom/itk backend test_load_image.py --- tests/test_load_image.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_load_image.py b/tests/test_load_image.py index 42c356b74a..6f29e7ac50 100644 --- a/tests/test_load_image.py +++ b/tests/test_load_image.py @@ -276,8 +276,7 @@ def test_dicom_reader_consistency_single(self): itk_result = LoadImage(image_only=True, **itk_param)(self.data_dir) pydicom_result = LoadImage(image_only=True, **pydicom_param)(self.data_dir) np.testing.assert_allclose(pydicom_result, itk_result.squeeze()) - np.testing.assert_allclose(pydicom_result.affine[:2, :2], itk_result.affine[:2, :2]) - np.testing.assert_allclose(pydicom_result.affine[..., 3], itk_result.affine[..., 3]) + np.testing.assert_allclose(pydicom_result.affine, itk_result.affine) def test_load_nifti_multichannel(self): test_image = np.random.randint(0, 256, size=(31, 64, 16, 2)).astype(np.float32)