From b7239c29ae5af3727c613ae84998a04642af8caa Mon Sep 17 00:00:00 2001 From: "Lumberbot (aka Jack)" <39504233+meeseeksmachine@users.noreply.github.com> Date: Sun, 29 Sep 2024 15:35:02 -0700 Subject: [PATCH] Backport PR #503 on branch 3.x (PR: Update `load_dicom` to accommodate Pydicom 3.0) (#507) Co-authored-by: Ryan Clary <9618975+mrclary@users.noreply.github.com> --- spyder_kernels/utils/iofuncs.py | 11 +++++++++-- spyder_kernels/utils/tests/test_iofuncs.py | 6 +++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/spyder_kernels/utils/iofuncs.py b/spyder_kernels/utils/iofuncs.py index 7da2828f..5102d4a4 100644 --- a/spyder_kernels/utils/iofuncs.py +++ b/spyder_kernels/utils/iofuncs.py @@ -506,9 +506,16 @@ def load_dicom(filename): name = osp.splitext(osp.basename(filename))[0] try: - data = dicomio.read_file(filename, force=True) + # For Pydicom 3/Python 3.10+ + data = dicomio.dcmread(filename, force=True) except TypeError: - data = dicomio.read_file(filename) + data = dicomio.dcmread(filename) + except AttributeError: + # For Pydicom 2/Python 3.9- + try: + data = dicomio.read_file(filename, force=True) + except TypeError: + data = dicomio.read_file(filename) arr = data.pixel_array return {name: arr}, None except Exception as error: diff --git a/spyder_kernels/utils/tests/test_iofuncs.py b/spyder_kernels/utils/tests/test_iofuncs.py index d88b2e10..ee8c1b1b 100644 --- a/spyder_kernels/utils/tests/test_iofuncs.py +++ b/spyder_kernels/utils/tests/test_iofuncs.py @@ -11,9 +11,9 @@ """ # Standard library imports +import copy import io import os -import copy # Third party imports from PIL import ImageFile @@ -350,6 +350,10 @@ def test_save_load_hdf5_files(tmp_path): assert repr(iofuncs.load_hdf5(h5_file)) == repr(expected) +@pytest.mark.skipif( + os.environ.get("USE_CONDA") == "true", + reason="Pydicom is not installed correctly in Conda envs" +) def test_load_dicom_files(): """Check that we can load DICOM files.""" # This test pass locally but we need to set the variable below for it to