Skip to content

Commit bd01484

Browse files
committed
Fix optional imports for tests
1 parent db72170 commit bd01484

5 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/highdicom/frame.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Optional, Union
44

55
import numpy as np
6-
from openjpeg.utils import encode_array
76
from PIL import Image
87
from pydicom.dataset import Dataset, FileMetaDataset
98
from pydicom.encaps import encapsulate
@@ -313,6 +312,15 @@ def encode_frame(
313312
'Array must contain only 0 and 1 for bits_allocated = 1'
314313
)
315314
array = array.astype(bool)
315+
316+
try:
317+
from openjpeg.utils import encode_array
318+
except ModuleNotFoundError:
319+
raise ModuleNotFoundError(
320+
"Highdicom requires the pylibjpeg-openjpeg package to compress "
321+
"frames using the JPEG2000Lossless transfer syntax."
322+
)
323+
316324
data = encode_array(
317325
array,
318326
bits_stored=1,

tests/test_frame.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def setUp(self):
2525
)
2626

2727
def test_jpeg_rgb(self):
28+
pytest.importorskip("libjpeg")
2829
filepath = str(self._test_files_dir.joinpath('frame_rgb.jpeg'))
2930
with open(filepath, 'br') as fp:
3031
compressed_frame = fp.read()
@@ -70,6 +71,7 @@ def test_jpeg_rgb(self):
7071
)
7172

7273
def test_jpeg_rgb_empty(self):
74+
pytest.importorskip("libjpeg")
7375
filepath = str(self._test_files_dir.joinpath('frame_rgb_empty.jpeg'))
7476
with open(filepath, 'br') as fp:
7577
compressed_frame = fp.read()
@@ -161,6 +163,7 @@ def test_jpeg_monochrome(self):
161163
assert compressed_frame.endswith(b'\xFF\xD9')
162164

163165
def test_jpeg2000_rgb(self):
166+
pytest.importorskip("openjpeg")
164167
bits_allocated = 8
165168
frame = np.ones((48, 32, 3), dtype=np.dtype(f'uint{bits_allocated}'))
166169
frame[2:4, 5:30, 0] = 7
@@ -190,6 +193,7 @@ def test_jpeg2000_rgb(self):
190193
np.testing.assert_allclose(frame, decoded_frame, atol=2)
191194

192195
def test_jpeg2000_monochrome(self):
196+
pytest.importorskip("openjpeg")
193197
bits_allocated = 8
194198
frame = np.zeros((48, 32), dtype=np.dtype(f'uint{bits_allocated}'))
195199
frame[2:4, 5:30] = 7
@@ -218,6 +222,7 @@ def test_jpeg2000_monochrome(self):
218222
np.testing.assert_allclose(frame, decoded_frame, atol=2)
219223

220224
def test_jpeg2000lossless_rgb(self):
225+
pytest.importorskip("openjpeg")
221226
bits_allocated = 8
222227
frame = np.ones((48, 32, 3), dtype=np.dtype(f'uint{bits_allocated}'))
223228
frame *= 255
@@ -247,6 +252,7 @@ def test_jpeg2000lossless_rgb(self):
247252
np.testing.assert_array_equal(frame, decoded_frame)
248253

249254
def test_jpeg2000lossless_monochrome(self):
255+
pytest.importorskip("openjpeg")
250256
bits_allocated = 16
251257
frame = np.zeros((48, 32), dtype=np.dtype(f'uint{bits_allocated}'))
252258
compressed_frame = encode_frame(
@@ -274,6 +280,7 @@ def test_jpeg2000lossless_monochrome(self):
274280
np.testing.assert_array_equal(frame, decoded_frame)
275281

276282
def test_jpeg2000lossless_single_bit(self):
283+
pytest.importorskip("openjpeg")
277284
bits_allocated = 1
278285
frame = np.zeros((48, 32), dtype=np.dtype('uint8'))
279286
frame[12:45, 3:6] = 1

tests/test_pm.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ def test_multi_frame_sm_image_ushort_palette_lut(self):
555555
assert instance.PixelPresentation == 'MONOCHROME'
556556

557557
def test_multi_frame_sm_image_ushort_encapsulated_jpeg2000(self):
558+
pytest.importorskip("openjpeg")
558559
pixel_array = np.random.randint(
559560
low=0,
560561
high=2**8,

tests/test_sc.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ def test_rgb_rle(self):
320320
)
321321

322322
def test_monochrome_jpeg_baseline(self):
323+
pytest.importorskip("libjpeg")
323324
bits_allocated = 8
324325
photometric_interpretation = 'MONOCHROME2'
325326
coordinate_system = 'PATIENT'
@@ -349,6 +350,7 @@ def test_monochrome_jpeg_baseline(self):
349350
)
350351

351352
def test_rgb_jpeg_baseline(self):
353+
pytest.importorskip("libjpeg")
352354
bits_allocated = 8
353355
photometric_interpretation = 'YBR_FULL_422'
354356
coordinate_system = 'PATIENT'
@@ -376,6 +378,7 @@ def test_rgb_jpeg_baseline(self):
376378
np.testing.assert_allclose(frame, reread_frame, rtol=1.2)
377379

378380
def test_monochrome_jpeg2000lossless(self):
381+
pytest.importorskip("openjpeg")
379382
bits_allocated = 8
380383
photometric_interpretation = 'MONOCHROME2'
381384
coordinate_system = 'PATIENT'
@@ -403,6 +406,7 @@ def test_monochrome_jpeg2000lossless(self):
403406
)
404407

405408
def test_monochrome_jpeg2000(self):
409+
pytest.importorskip("openjpeg")
406410
bits_allocated = 8
407411
photometric_interpretation = 'MONOCHROME2'
408412
coordinate_system = 'PATIENT'
@@ -431,6 +435,7 @@ def test_monochrome_jpeg2000(self):
431435
)
432436

433437
def test_rgb_jpeg2000(self):
438+
pytest.importorskip("openjpeg")
434439
bits_allocated = 8
435440
photometric_interpretation = 'YBR_RCT'
436441
coordinate_system = 'PATIENT'

tests/test_seg.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,8 +1641,15 @@ def test_construction_autotile(
16411641

16421642
transfer_syntax_uids = [
16431643
ExplicitVRLittleEndian,
1644-
JPEG2000Lossless,
16451644
]
1645+
try:
1646+
import openjpeg # noqa: F401
1647+
except ModuleNotFoundError:
1648+
pass
1649+
else:
1650+
transfer_syntax_uids += [
1651+
JPEG2000Lossless,
1652+
]
16461653
if segmentation_type.value == 'FRACTIONAL':
16471654
try:
16481655
import libjpeg # noqa: F401
@@ -1749,6 +1756,8 @@ def test_pixel_types_fractional(
17491756
pix_type,
17501757
test_data,
17511758
):
1759+
if fractional_transfer_syntax_uid == JPEG2000Lossless:
1760+
pytest.importorskip("openjpeg")
17521761
if fractional_transfer_syntax_uid == JPEGLSLossless:
17531762
pytest.importorskip("libjpeg")
17541763

@@ -1907,6 +1916,8 @@ def test_pixel_types_binary(
19071916
pix_type,
19081917
test_data,
19091918
):
1919+
if binary_transfer_syntax_uid == JPEG2000Lossless:
1920+
pytest.importorskip("openjpeg")
19101921
sources, mask = self._tests[test_data]
19111922

19121923
# Two segments, overlapping

0 commit comments

Comments
 (0)