Skip to content

Commit 8dff795

Browse files
committed
Fix tests failing with pydicom v3
1 parent 13ced42 commit 8dff795

File tree

2 files changed

+101
-47
lines changed

2 files changed

+101
-47
lines changed

openjpeg/tests/test_decode.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
from io import BytesIO
44

55
try:
6-
from pydicom.encaps import generate_frames
7-
from pydicom.pixels.utils import reshape_pixel_array, pixel_dtype
6+
from pydicom.encaps import generate_pixel_data_frame
7+
from pydicom.pixel_data_handlers.util import (
8+
reshape_pixel_array,
9+
pixel_dtype,
10+
)
811

912
HAS_PYDICOM = True
1013
except ImportError:
@@ -69,10 +72,10 @@ def test_version():
6972
assert 5 == version[1]
7073

7174

72-
def _generate_frames(ds):
75+
def generate_frames(ds):
7376
"""Return a frame generator for DICOM datasets."""
7477
nr_frames = ds.get("NumberOfFrames", 1)
75-
return generate_frames(ds.PixelData, number_of_frames=nr_frames)
78+
return generate_pixel_data_frame(ds.PixelData, nr_frames)
7679

7780

7881
def test_get_format_raises():
@@ -88,7 +91,7 @@ def test_bad_decode():
8891
"""Test trying to decode bad data."""
8992
index = get_indexed_datasets("1.2.840.10008.1.2.4.90")
9093
ds = index["966.dcm"]["ds"]
91-
frame = next(_generate_frames(ds))
94+
frame = next(generate_frames(ds))
9295
msg = r"Error decoding the J2K data: failed to decode image"
9396
with pytest.raises(RuntimeError, match=msg):
9497
decode(frame)
@@ -105,7 +108,7 @@ def test_decode_bytes(self):
105108
"""Test decoding using bytes."""
106109
index = get_indexed_datasets("1.2.840.10008.1.2.4.90")
107110
ds = index["MR_small_jp2klossless.dcm"]["ds"]
108-
frame = next(_generate_frames(ds))
111+
frame = next(generate_frames(ds))
109112
assert isinstance(frame, bytes)
110113
arr = decode(frame)
111114
assert arr.flags.writeable
@@ -123,7 +126,7 @@ def test_decode_filelike(self):
123126
"""Test decoding using file-like."""
124127
index = get_indexed_datasets("1.2.840.10008.1.2.4.90")
125128
ds = index["MR_small_jp2klossless.dcm"]["ds"]
126-
frame = BytesIO(next(_generate_frames(ds)))
129+
frame = BytesIO(next(generate_frames(ds)))
127130
assert isinstance(frame, BytesIO)
128131
arr = decode(frame)
129132
assert arr.flags.writeable
@@ -141,7 +144,7 @@ def test_decode_bad_type_raises(self):
141144
"""Test decoding using invalid type raises."""
142145
index = get_indexed_datasets("1.2.840.10008.1.2.4.90")
143146
ds = index["MR_small_jp2klossless.dcm"]["ds"]
144-
frame = tuple(next(_generate_frames(ds)))
147+
frame = tuple(next(generate_frames(ds)))
145148
assert not hasattr(frame, "tell") and not isinstance(frame, bytes)
146149

147150
msg = (
@@ -156,7 +159,7 @@ def test_decode_bad_format_raises(self):
156159
"""Test decoding using invalid jpeg format raises."""
157160
index = get_indexed_datasets("1.2.840.10008.1.2.4.90")
158161
ds = index["MR_small_jp2klossless.dcm"]["ds"]
159-
frame = next(_generate_frames(ds))
162+
frame = next(generate_frames(ds))
160163

161164
msg = r"Unsupported 'j2k_format' value: 3"
162165
with pytest.raises(ValueError, match=msg):
@@ -167,7 +170,7 @@ def test_decode_reshape_true(self):
167170
"""Test decoding using invalid jpeg format raises."""
168171
index = get_indexed_datasets("1.2.840.10008.1.2.4.90")
169172
ds = index["US1_J2KR.dcm"]["ds"]
170-
frame = next(_generate_frames(ds))
173+
frame = next(generate_frames(ds))
171174

172175
arr = decode(frame)
173176
assert arr.flags.writeable
@@ -202,7 +205,7 @@ def test_decode_reshape_false(self):
202205
"""Test decoding using invalid jpeg format raises."""
203206
index = get_indexed_datasets("1.2.840.10008.1.2.4.90")
204207
ds = index["US1_J2KR.dcm"]["ds"]
205-
frame = next(_generate_frames(ds))
208+
frame = next(generate_frames(ds))
206209

207210
arr = decode(frame, reshape=False)
208211
assert arr.flags.writeable
@@ -213,7 +216,7 @@ def test_signed_error(self):
213216
"""Regression test for #30."""
214217
index = get_indexed_datasets("1.2.840.10008.1.2.4.90")
215218
ds = index["693_J2KR.dcm"]["ds"]
216-
frame = next(_generate_frames(ds))
219+
frame = next(generate_frames(ds))
217220

218221
arr = decode(frame)
219222
assert -2000 == arr[0, 0]
@@ -369,7 +372,7 @@ def test_jpeg2000r(self, fname, info):
369372
# info: (rows, columns, spp, bps)
370373
index = get_indexed_datasets("1.2.840.10008.1.2.4.90")
371374
ds = index[fname]["ds"]
372-
frame = next(_generate_frames(ds))
375+
frame = next(generate_frames(ds))
373376
arr = decode(BytesIO(frame), reshape=False)
374377
assert arr.flags.writeable
375378

@@ -403,7 +406,7 @@ def test_jpeg2000i(self, fname, info):
403406
index = get_indexed_datasets("1.2.840.10008.1.2.4.91")
404407
ds = index[fname]["ds"]
405408

406-
frame = next(_generate_frames(ds))
409+
frame = next(generate_frames(ds))
407410
arr = decode(BytesIO(frame), reshape=False)
408411
assert arr.flags.writeable
409412

openjpeg/tests/test_handler.py

Lines changed: 84 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import pytest
44

55
try:
6-
from pydicom.encaps import generate_frames
6+
from pydicom import __version__
7+
from pydicom.encaps import generate_pixel_data_frame
8+
from pydicom.pixel_data_handlers.util import (
9+
reshape_pixel_array,
10+
pixel_dtype,
11+
)
712

813
HAS_PYDICOM = True
914
except ImportError:
@@ -12,11 +17,14 @@
1217
from openjpeg import get_parameters, decode_pixel_data
1318
from openjpeg.data import get_indexed_datasets
1419

20+
if HAS_PYDICOM:
21+
PYD_VERSION = int(__version__.split(".")[0])
1522

16-
# def generate_frames(ds.PixelData):
17-
# """Return a frame generator for DICOM datasets."""
18-
# nr_frames = ds.get("NumberOfFrames", 1)
19-
# return generate_frames(ds.PixelData, number_of_frames=nr_frames)
23+
24+
def generate_frames(ds):
25+
"""Return a frame generator for DICOM datasets."""
26+
nr_frames = ds.get("NumberOfFrames", 1)
27+
return generate_pixel_data_frame(ds.PixelData, nr_frames)
2028

2129

2230
@pytest.mark.skipif(not HAS_PYDICOM, reason="pydicom unavailable")
@@ -27,7 +35,7 @@ def test_invalid_type_raises(self):
2735
"""Test decoding using invalid type raises."""
2836
index = get_indexed_datasets("1.2.840.10008.1.2.4.90")
2937
ds = index["MR_small_jp2klossless.dcm"]["ds"]
30-
frame = tuple(next(generate_frames(ds.PixelData)))
38+
frame = tuple(next(generate_frames(ds)))
3139
assert not hasattr(frame, "tell") and not isinstance(frame, bytes)
3240

3341
msg = "a bytes-like object is required, not 'tuple'"
@@ -37,7 +45,7 @@ def test_invalid_type_raises(self):
3745
def test_no_dataset(self):
3846
index = get_indexed_datasets("1.2.840.10008.1.2.4.90")
3947
ds = index["MR_small_jp2klossless.dcm"]["ds"]
40-
frame = next(generate_frames(ds.PixelData))
48+
frame = next(generate_frames(ds))
4149
arr = decode_pixel_data(frame)
4250
assert arr.flags.writeable
4351
assert "uint8" == arr.dtype
@@ -113,10 +121,17 @@ def test_raises(self):
113121
assert 8 == ds.BitsAllocated == ds.BitsStored
114122
assert 0 == ds.PixelRepresentation
115123

116-
msg = (
117-
"Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
118-
"not installed"
119-
)
124+
if PYD_VERSION < 3:
125+
msg = (
126+
"Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
127+
"not installed"
128+
)
129+
else:
130+
msg = (
131+
r"Unable to decompress 'JPEG Baseline \(Process 1\)' pixel data because "
132+
"all plugins are missing dependencies:"
133+
)
134+
120135
with pytest.raises(RuntimeError, match=msg):
121136
ds.pixel_array
122137

@@ -143,10 +158,17 @@ def test_raises(self):
143158
assert 10 == ds.BitsStored
144159
assert 0 == ds.PixelRepresentation
145160

146-
msg = (
147-
"Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
148-
"not installed"
149-
)
161+
if PYD_VERSION < 3:
162+
msg = (
163+
"Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
164+
"not installed"
165+
)
166+
else:
167+
msg = (
168+
r"Unable to decompress 'JPEG Extended \(Process 2 and 4\)' pixel data because "
169+
"all plugins are missing dependencies:"
170+
)
171+
150172
with pytest.raises(RuntimeError, match=msg):
151173
ds.pixel_array
152174

@@ -171,10 +193,17 @@ def test_raises(self):
171193
assert 12 == ds.BitsStored
172194
assert 0 == ds.PixelRepresentation
173195

174-
msg = (
175-
"Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
176-
"not installed"
177-
)
196+
if PYD_VERSION < 3:
197+
msg = (
198+
"Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
199+
"not installed"
200+
)
201+
else:
202+
msg = (
203+
r"Unable to decompress 'JPEG Lossless, Non-Hierarchical \(Process "
204+
r"14\)' pixel data because all plugins are missing dependencies:"
205+
)
206+
178207
with pytest.raises(RuntimeError, match=msg):
179208
ds.pixel_array
180209

@@ -200,10 +229,18 @@ def test_raises(self):
200229
assert 8 == ds.BitsStored
201230
assert 0 == ds.PixelRepresentation
202231

203-
msg = (
204-
"Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
205-
"not installed"
206-
)
232+
if PYD_VERSION < 3:
233+
msg = (
234+
"Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
235+
"not installed"
236+
)
237+
else:
238+
msg = (
239+
"Unable to decompress 'JPEG Lossless, Non-Hierarchical, First-Order "
240+
r"Prediction \(Process 14 \[Selection Value 1\]\)' "
241+
"pixel data because all plugins are missing dependencies:"
242+
)
243+
207244
with pytest.raises(RuntimeError, match=msg):
208245
ds.pixel_array
209246

@@ -229,10 +266,17 @@ def test_raises(self):
229266
assert 16 == ds.BitsStored
230267
assert 1 == ds.PixelRepresentation
231268

232-
msg = (
233-
"Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
234-
"not installed"
235-
)
269+
if PYD_VERSION < 3:
270+
msg = (
271+
"Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
272+
"not installed"
273+
)
274+
else:
275+
msg = (
276+
r"Unable to decompress 'JPEG-LS Lossless Image Compression' "
277+
"pixel data because all plugins are missing dependencies:"
278+
)
279+
236280
with pytest.raises(RuntimeError, match=msg):
237281
ds.pixel_array
238282

@@ -257,10 +301,17 @@ def test_raises(self):
257301
assert 16 == ds.BitsStored
258302
assert 1 == ds.PixelRepresentation
259303

260-
msg = (
261-
"Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
262-
"not installed"
263-
)
304+
if PYD_VERSION < 3:
305+
msg = (
306+
"Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
307+
"not installed"
308+
)
309+
else:
310+
msg = (
311+
r"Unable to decompress 'JPEG-LS Lossy \(Near-Lossless\) Image Compression' "
312+
"pixel data because all plugins are missing dependencies:"
313+
)
314+
264315
with pytest.raises(RuntimeError, match=msg):
265316
ds.pixel_array
266317

@@ -585,7 +636,7 @@ def test_data_unsigned_pr_1(self):
585636
# Note: if PR is 1 but the JPEG data is unsigned then it should
586637
# probably be converted to signed using 2s complement
587638
ds.pixel_array
588-
frame = next(generate_frames(ds.PixelData))
639+
frame = next(generate_frames(ds))
589640
params = get_parameters(frame)
590641
assert params["is_signed"] is False
591642

@@ -605,7 +656,7 @@ def test_data_signed_pr_0(self):
605656
# Note: if PR is 0 but the JPEG data is signed then... ?
606657
ds.pixel_array
607658

608-
frame = next(generate_frames(ds.PixelData))
659+
frame = next(generate_frames(ds))
609660
params = get_parameters(frame)
610661
assert params["is_signed"] is True
611662

0 commit comments

Comments
 (0)