Skip to content

Commit

Permalink
Fix tests failing with pydicom v3
Browse files Browse the repository at this point in the history
  • Loading branch information
scaramallion committed Oct 28, 2024
1 parent 13ced42 commit 8dff795
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 47 deletions.
31 changes: 17 additions & 14 deletions openjpeg/tests/test_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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():
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 = (
Expand All @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
117 changes: 84 additions & 33 deletions openjpeg/tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
Expand All @@ -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'"
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 8dff795

Please sign in to comment.