From 8dff7957c49a53ce3465ac9a9825efaa671716fe Mon Sep 17 00:00:00 2001 From: scaramallion Date: Mon, 28 Oct 2024 23:03:22 +1100 Subject: [PATCH] Fix tests failing with pydicom v3 --- openjpeg/tests/test_decode.py | 31 +++++---- openjpeg/tests/test_handler.py | 117 +++++++++++++++++++++++---------- 2 files changed, 101 insertions(+), 47 deletions(-) diff --git a/openjpeg/tests/test_decode.py b/openjpeg/tests/test_decode.py index 297926f..ace7346 100644 --- a/openjpeg/tests/test_decode.py +++ b/openjpeg/tests/test_decode.py @@ -3,8 +3,11 @@ from io import BytesIO try: - from pydicom.encaps import generate_frames - from pydicom.pixels.utils import reshape_pixel_array, pixel_dtype + from pydicom.encaps import generate_pixel_data_frame + from pydicom.pixel_data_handlers.util import ( + reshape_pixel_array, + pixel_dtype, + ) HAS_PYDICOM = True except ImportError: @@ -69,10 +72,10 @@ def test_version(): assert 5 == version[1] -def _generate_frames(ds): +def generate_frames(ds): """Return a frame generator for DICOM datasets.""" nr_frames = ds.get("NumberOfFrames", 1) - return generate_frames(ds.PixelData, number_of_frames=nr_frames) + return generate_pixel_data_frame(ds.PixelData, nr_frames) def test_get_format_raises(): @@ -88,7 +91,7 @@ def test_bad_decode(): """Test trying to decode bad data.""" index = get_indexed_datasets("1.2.840.10008.1.2.4.90") ds = index["966.dcm"]["ds"] - frame = next(_generate_frames(ds)) + frame = next(generate_frames(ds)) msg = r"Error decoding the J2K data: failed to decode image" with pytest.raises(RuntimeError, match=msg): decode(frame) @@ -105,7 +108,7 @@ def test_decode_bytes(self): """Test decoding using bytes.""" index = get_indexed_datasets("1.2.840.10008.1.2.4.90") ds = index["MR_small_jp2klossless.dcm"]["ds"] - frame = next(_generate_frames(ds)) + frame = next(generate_frames(ds)) assert isinstance(frame, bytes) arr = decode(frame) assert arr.flags.writeable @@ -123,7 +126,7 @@ def test_decode_filelike(self): """Test decoding using file-like.""" index = get_indexed_datasets("1.2.840.10008.1.2.4.90") ds = index["MR_small_jp2klossless.dcm"]["ds"] - frame = BytesIO(next(_generate_frames(ds))) + frame = BytesIO(next(generate_frames(ds))) assert isinstance(frame, BytesIO) arr = decode(frame) assert arr.flags.writeable @@ -141,7 +144,7 @@ def test_decode_bad_type_raises(self): """Test decoding using invalid type raises.""" index = get_indexed_datasets("1.2.840.10008.1.2.4.90") ds = index["MR_small_jp2klossless.dcm"]["ds"] - frame = tuple(next(_generate_frames(ds))) + frame = tuple(next(generate_frames(ds))) assert not hasattr(frame, "tell") and not isinstance(frame, bytes) msg = ( @@ -156,7 +159,7 @@ def test_decode_bad_format_raises(self): """Test decoding using invalid jpeg format raises.""" index = get_indexed_datasets("1.2.840.10008.1.2.4.90") ds = index["MR_small_jp2klossless.dcm"]["ds"] - frame = next(_generate_frames(ds)) + frame = next(generate_frames(ds)) msg = r"Unsupported 'j2k_format' value: 3" with pytest.raises(ValueError, match=msg): @@ -167,7 +170,7 @@ def test_decode_reshape_true(self): """Test decoding using invalid jpeg format raises.""" index = get_indexed_datasets("1.2.840.10008.1.2.4.90") ds = index["US1_J2KR.dcm"]["ds"] - frame = next(_generate_frames(ds)) + frame = next(generate_frames(ds)) arr = decode(frame) assert arr.flags.writeable @@ -202,7 +205,7 @@ def test_decode_reshape_false(self): """Test decoding using invalid jpeg format raises.""" index = get_indexed_datasets("1.2.840.10008.1.2.4.90") ds = index["US1_J2KR.dcm"]["ds"] - frame = next(_generate_frames(ds)) + frame = next(generate_frames(ds)) arr = decode(frame, reshape=False) assert arr.flags.writeable @@ -213,7 +216,7 @@ def test_signed_error(self): """Regression test for #30.""" index = get_indexed_datasets("1.2.840.10008.1.2.4.90") ds = index["693_J2KR.dcm"]["ds"] - frame = next(_generate_frames(ds)) + frame = next(generate_frames(ds)) arr = decode(frame) assert -2000 == arr[0, 0] @@ -369,7 +372,7 @@ def test_jpeg2000r(self, fname, info): # info: (rows, columns, spp, bps) index = get_indexed_datasets("1.2.840.10008.1.2.4.90") ds = index[fname]["ds"] - frame = next(_generate_frames(ds)) + frame = next(generate_frames(ds)) arr = decode(BytesIO(frame), reshape=False) assert arr.flags.writeable @@ -403,7 +406,7 @@ def test_jpeg2000i(self, fname, info): index = get_indexed_datasets("1.2.840.10008.1.2.4.91") ds = index[fname]["ds"] - frame = next(_generate_frames(ds)) + frame = next(generate_frames(ds)) arr = decode(BytesIO(frame), reshape=False) assert arr.flags.writeable diff --git a/openjpeg/tests/test_handler.py b/openjpeg/tests/test_handler.py index d33c877..fe213d9 100644 --- a/openjpeg/tests/test_handler.py +++ b/openjpeg/tests/test_handler.py @@ -3,7 +3,12 @@ import pytest try: - from pydicom.encaps import generate_frames + from pydicom import __version__ + from pydicom.encaps import generate_pixel_data_frame + from pydicom.pixel_data_handlers.util import ( + reshape_pixel_array, + pixel_dtype, + ) HAS_PYDICOM = True except ImportError: @@ -12,11 +17,14 @@ from openjpeg import get_parameters, decode_pixel_data from openjpeg.data import get_indexed_datasets +if HAS_PYDICOM: + PYD_VERSION = int(__version__.split(".")[0]) -# def generate_frames(ds.PixelData): -# """Return a frame generator for DICOM datasets.""" -# nr_frames = ds.get("NumberOfFrames", 1) -# return generate_frames(ds.PixelData, number_of_frames=nr_frames) + +def generate_frames(ds): + """Return a frame generator for DICOM datasets.""" + nr_frames = ds.get("NumberOfFrames", 1) + return generate_pixel_data_frame(ds.PixelData, nr_frames) @pytest.mark.skipif(not HAS_PYDICOM, reason="pydicom unavailable") @@ -27,7 +35,7 @@ def test_invalid_type_raises(self): """Test decoding using invalid type raises.""" index = get_indexed_datasets("1.2.840.10008.1.2.4.90") ds = index["MR_small_jp2klossless.dcm"]["ds"] - frame = tuple(next(generate_frames(ds.PixelData))) + frame = tuple(next(generate_frames(ds))) assert not hasattr(frame, "tell") and not isinstance(frame, bytes) msg = "a bytes-like object is required, not 'tuple'" @@ -37,7 +45,7 @@ def test_invalid_type_raises(self): def test_no_dataset(self): index = get_indexed_datasets("1.2.840.10008.1.2.4.90") ds = index["MR_small_jp2klossless.dcm"]["ds"] - frame = next(generate_frames(ds.PixelData)) + frame = next(generate_frames(ds)) arr = decode_pixel_data(frame) assert arr.flags.writeable assert "uint8" == arr.dtype @@ -113,10 +121,17 @@ def test_raises(self): assert 8 == ds.BitsAllocated == ds.BitsStored assert 0 == ds.PixelRepresentation - msg = ( - "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is " - "not installed" - ) + if PYD_VERSION < 3: + msg = ( + "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is " + "not installed" + ) + else: + msg = ( + r"Unable to decompress 'JPEG Baseline \(Process 1\)' pixel data because " + "all plugins are missing dependencies:" + ) + with pytest.raises(RuntimeError, match=msg): ds.pixel_array @@ -143,10 +158,17 @@ def test_raises(self): assert 10 == ds.BitsStored assert 0 == ds.PixelRepresentation - msg = ( - "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is " - "not installed" - ) + if PYD_VERSION < 3: + msg = ( + "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is " + "not installed" + ) + else: + msg = ( + r"Unable to decompress 'JPEG Extended \(Process 2 and 4\)' pixel data because " + "all plugins are missing dependencies:" + ) + with pytest.raises(RuntimeError, match=msg): ds.pixel_array @@ -171,10 +193,17 @@ def test_raises(self): assert 12 == ds.BitsStored assert 0 == ds.PixelRepresentation - msg = ( - "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is " - "not installed" - ) + if PYD_VERSION < 3: + msg = ( + "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is " + "not installed" + ) + else: + msg = ( + r"Unable to decompress 'JPEG Lossless, Non-Hierarchical \(Process " + r"14\)' pixel data because all plugins are missing dependencies:" + ) + with pytest.raises(RuntimeError, match=msg): ds.pixel_array @@ -200,10 +229,18 @@ def test_raises(self): assert 8 == ds.BitsStored assert 0 == ds.PixelRepresentation - msg = ( - "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is " - "not installed" - ) + if PYD_VERSION < 3: + msg = ( + "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is " + "not installed" + ) + else: + msg = ( + "Unable to decompress 'JPEG Lossless, Non-Hierarchical, First-Order " + r"Prediction \(Process 14 \[Selection Value 1\]\)' " + "pixel data because all plugins are missing dependencies:" + ) + with pytest.raises(RuntimeError, match=msg): ds.pixel_array @@ -229,10 +266,17 @@ def test_raises(self): assert 16 == ds.BitsStored assert 1 == ds.PixelRepresentation - msg = ( - "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is " - "not installed" - ) + if PYD_VERSION < 3: + msg = ( + "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is " + "not installed" + ) + else: + msg = ( + r"Unable to decompress 'JPEG-LS Lossless Image Compression' " + "pixel data because all plugins are missing dependencies:" + ) + with pytest.raises(RuntimeError, match=msg): ds.pixel_array @@ -257,10 +301,17 @@ def test_raises(self): assert 16 == ds.BitsStored assert 1 == ds.PixelRepresentation - msg = ( - "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is " - "not installed" - ) + if PYD_VERSION < 3: + msg = ( + "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is " + "not installed" + ) + else: + msg = ( + r"Unable to decompress 'JPEG-LS Lossy \(Near-Lossless\) Image Compression' " + "pixel data because all plugins are missing dependencies:" + ) + with pytest.raises(RuntimeError, match=msg): ds.pixel_array @@ -585,7 +636,7 @@ def test_data_unsigned_pr_1(self): # Note: if PR is 1 but the JPEG data is unsigned then it should # probably be converted to signed using 2s complement ds.pixel_array - frame = next(generate_frames(ds.PixelData)) + frame = next(generate_frames(ds)) params = get_parameters(frame) assert params["is_signed"] is False @@ -605,7 +656,7 @@ def test_data_signed_pr_0(self): # Note: if PR is 0 but the JPEG data is signed then... ? ds.pixel_array - frame = next(generate_frames(ds.PixelData)) + frame = next(generate_frames(ds)) params = get_parameters(frame) assert params["is_signed"] is True