From d36f63fbfb20ddecf3dadbb2cba61c00c4fc338a Mon Sep 17 00:00:00 2001 From: Bradley Lowekamp Date: Tue, 20 Jul 2021 16:26:45 -0400 Subject: [PATCH] Use SimpleITK 2.1 meta-data as dictionary operators --- rap_sitkcore/read_dcm.py | 10 +++++----- test/unit/test_read_dcm.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rap_sitkcore/read_dcm.py b/rap_sitkcore/read_dcm.py index 731c2b2..3ecf8ad 100644 --- a/rap_sitkcore/read_dcm.py +++ b/rap_sitkcore/read_dcm.py @@ -49,9 +49,9 @@ def _read_dcm_pydicom(filename: Path) -> sitk.Image: if tag in ds: de = ds.data_element(tag) if de.VR in ["CS", "UI"]: - img.SetMetaData(f"{de.tag.group:04x}|{de.tag.elem:04x}", de.value) + img[f"{de.tag.group:04x}|{de.tag.elem:04x}"] = de.value elif de.VR == "DS": - img.SetMetaData(f"{de.tag.group:04x}|{de.tag.elem:04x}", convert_float_list_to_mv_ds(de.value)) + img[f"{de.tag.group:04x}|{de.tag.elem:04x}"] = convert_float_list_to_mv_ds(de.value) else: raise ValueError( f'"{filename}" has data element "{de.name}" non-conforming value representation "{de.VR}".' @@ -121,14 +121,14 @@ def read_dcm(filename: Path) -> sitk.Image: key_to_keep = [keyword_to_gdcm_tag(n) for n in _keyword_to_copy] for k in old_keys: if k not in key_to_keep: - img.EraseMetaData(k) + del img[k] return img elif img.GetNumberOfComponentsPerPixel() == 3: out = srgb2gray(img) # copy tags for tag_name in _keyword_to_copy: key = keyword_to_gdcm_tag(tag_name) - if img.HasMetaDataKey(key): - out.SetMetaData(key, img.GetMetaData(key)) + if key in img: + out[key] = img[key] return out raise RuntimeError(f"Unsupported number of components: {img.GetNumberOfComponentsPerPixel()}") diff --git a/test/unit/test_read_dcm.py b/test/unit/test_read_dcm.py index 7b50cf9..04851d6 100644 --- a/test/unit/test_read_dcm.py +++ b/test/unit/test_read_dcm.py @@ -55,7 +55,7 @@ def test_read_dcm1(test_file): for tag in required_tags: key = keyword_to_gdcm_tag(tag) - assert img.HasMetaDataKey(key) + assert key in img for k in img.GetMetaDataKeys(): assert k in _white_listed_dicom_tags