From d8ab2612995e78a64cb605f50d30e1e549ba4c64 Mon Sep 17 00:00:00 2001 From: Arne Brys Date: Fri, 27 Sep 2024 09:06:13 +0200 Subject: [PATCH] icometrix/dicom2nifti#148: Make compatible with pydicom >= 3.0.0 --- dicom2nifti/common.py | 5 +++-- dicom2nifti/convert_dicom.py | 3 ++- dicom2nifti/convert_dir.py | 3 ++- scripts/anonymize_testdata.py | 3 ++- scripts/debug_directory.py | 2 +- scripts/dicomdiff.py | 3 ++- scripts/shrink_multiframe.py | 3 ++- scripts/shrink_singleframe.py | 3 ++- tests/test_siemens.py | 3 ++- 9 files changed, 18 insertions(+), 10 deletions(-) diff --git a/dicom2nifti/common.py b/dicom2nifti/common.py index cfb3956..c08a357 100644 --- a/dicom2nifti/common.py +++ b/dicom2nifti/common.py @@ -11,6 +11,7 @@ import numpy import pydicom +from pydicom import dcmread from pydicom.tag import Tag import dicom2nifti.settings @@ -36,7 +37,7 @@ def read_dicom_directory(dicom_directory, stop_before_pixels=False): for dicom_file in files: file_path = os.path.join(root, dicom_file) if is_dicom_file(file_path): - dicom_headers = pydicom.read_file(file_path, + dicom_headers = dcmread(file_path, defer_size="1 KB", stop_before_pixels=stop_before_pixels, force=dicom2nifti.settings.pydicom_read_force) @@ -1161,7 +1162,7 @@ def is_dicom_file(filename): return True if dicom2nifti.settings.pydicom_read_force: try: - dicom_headers = pydicom.read_file(filename, defer_size="1 KB", stop_before_pixels=True, force=True) + dicom_headers = dcmread(filename, defer_size="1 KB", stop_before_pixels=True, force=True) if dicom_headers is not None: return True except: diff --git a/dicom2nifti/convert_dicom.py b/dicom2nifti/convert_dicom.py index 0b31e9c..639e9cd 100644 --- a/dicom2nifti/convert_dicom.py +++ b/dicom2nifti/convert_dicom.py @@ -11,6 +11,7 @@ import tempfile import pydicom +from pydicom import dcmread from pydicom.tag import Tag import dicom2nifti.common as common @@ -199,7 +200,7 @@ def _get_first_header(dicom_directory): if not common.is_dicom_file(file_path): continue # read the headers - return pydicom.read_file(file_path, + return dcmread(file_path, stop_before_pixels=True, force=dicom2nifti.settings.pydicom_read_force) # no dicom files found diff --git a/dicom2nifti/convert_dir.py b/dicom2nifti/convert_dir.py index 2393c2d..59eef5a 100644 --- a/dicom2nifti/convert_dir.py +++ b/dicom2nifti/convert_dir.py @@ -12,6 +12,7 @@ import unicodedata import pydicom +from pydicom import dcmread from pydicom.tag import Tag import dicom2nifti.common as common @@ -41,7 +42,7 @@ def convert_directory(dicom_directory, output_folder, compression=True, reorient # read the dicom as fast as possible # (max length for SeriesInstanceUID is 64 so defer_size 100 should be ok) - dicom_headers = pydicom.read_file(file_path, + dicom_headers = dcmread(file_path, defer_size="1 KB", stop_before_pixels=False, force=dicom2nifti.settings.pydicom_read_force) diff --git a/scripts/anonymize_testdata.py b/scripts/anonymize_testdata.py index fefe166..1beb19a 100644 --- a/scripts/anonymize_testdata.py +++ b/scripts/anonymize_testdata.py @@ -12,6 +12,7 @@ import pydicom import pydicom.dataset import pydicom.uid +from pydicom import dcmread import common from dicom2nifti.common import read_dicom_directory, is_philips, is_siemens, is_ge @@ -132,7 +133,7 @@ def _anonymize_file(dicom_file_in, dicom_file_out, fields_to_keep): 'ImplementationClassUID'] # Load dicom_file_in - dicom_in = pydicom.read_file(dicom_file_in) + dicom_in = dcmread(dicom_file_in) # Create new dicom file # Set new file meta information diff --git a/scripts/debug_directory.py b/scripts/debug_directory.py index d946268..181ee40 100644 --- a/scripts/debug_directory.py +++ b/scripts/debug_directory.py @@ -34,7 +34,7 @@ def run_convert_directory2(): tmp_output_dir = tempfile.mkdtemp() try: import pydicom - # headers = pydicom.read_file("/Users/abrys/Downloads/failing_cases/test.dcm") + # headers = dcmread("/Users/abrys/Downloads/failing_cases/test.dcm") convert_directory.convert_directory("/Users/abrys/Downloads/dti", "/Users/abrys/Downloads/dti") diff --git a/scripts/dicomdiff.py b/scripts/dicomdiff.py index 3f55392..f7febd3 100644 --- a/scripts/dicomdiff.py +++ b/scripts/dicomdiff.py @@ -7,6 +7,7 @@ import sys import pydicom +from pydicom import dcmread def dicom_diff(file1, file2): @@ -15,7 +16,7 @@ def dicom_diff(file1, file2): Inspired by https://code.google.com/p/pydicom/source/browse/source/dicom/examples/DicomDiff.py """ - datasets = pydicom.read_file(file1), pydicom.read_file(file2) + datasets = dcmread(file1), dcmread(file2) rep = [] diff --git a/scripts/shrink_multiframe.py b/scripts/shrink_multiframe.py index acb7449..3b02086 100644 --- a/scripts/shrink_multiframe.py +++ b/scripts/shrink_multiframe.py @@ -9,6 +9,7 @@ import numpy import pydicom +from pydicom import dcmread import dicom2nifti.common as common from dicom2nifti.convert_philips import _is_multiframe_diffusion_imaging, _is_multiframe_4d @@ -19,7 +20,7 @@ def shrink_multiframe(input_file, output_file=None, slice_count=8, timepoint_cou output_file = input_file # Load dicom_file_in - dicom_in = pydicom.read_file(input_file) + dicom_in = dcmread(input_file) if _is_multiframe_diffusion_imaging([dicom_in]) or _is_multiframe_4d([dicom_in]): diff --git a/scripts/shrink_singleframe.py b/scripts/shrink_singleframe.py index f3baca3..d1862aa 100644 --- a/scripts/shrink_singleframe.py +++ b/scripts/shrink_singleframe.py @@ -9,6 +9,7 @@ import pydicom import pydicom.dataset +from pydicom import dcmread def _shrink_file(dicom_file_in, subsample_factor): @@ -24,7 +25,7 @@ def _shrink_file(dicom_file_in, subsample_factor): dicom_file_out = dicom_file_in # Load dicom_file_in - dicom_in = pydicom.read_file(dicom_file_in) + dicom_in = dcmread(dicom_file_in) # Create new dicom file # Set new file meta information diff --git a/tests/test_siemens.py b/tests/test_siemens.py index e80039a..6011cb4 100644 --- a/tests/test_siemens.py +++ b/tests/test_siemens.py @@ -12,6 +12,7 @@ import nibabel import numpy import pydicom +from pydicom import dcmread import dicom2nifti.common as common import dicom2nifti.convert_siemens as convert_siemens @@ -199,7 +200,7 @@ def test_is_siemens(self): assert not common.is_siemens(read_dicom_directory(test_data.HITACHI_ANATOMICAL)) def test_get_asconv_headers(self): - mosaic = pydicom.read_file(os.path.join(test_data.SIEMENS_FMRI, 'IM-0001-0001.dcm')) + mosaic = dcmread(os.path.join(test_data.SIEMENS_FMRI, 'IM-0001-0001.dcm')) asconv_headers = convert_siemens._get_asconv_headers(mosaic) assert len(asconv_headers) == 64022