1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
""" Test DICOM functionality.
"""
from zipfile import ZipFile
import numpy as np
import pytest
import imageio.v2 as iio
import imageio.v3 as iio3
from imageio import core
import imageio.plugins.dicom
from conftest import deprecated_test
@pytest.fixture(scope="module")
def examples(test_images, tmp_path_factory):
"""Create two dirs, one with one dataset and one with two datasets"""
workdir = tmp_path_factory.getbasetemp() / "test_dicom"
# Prepare sources
fname1 = test_images / "dicom_sample1.zip"
fname2 = test_images / "dicom_sample2.zip"
dname1 = workdir / "dicom_sample1"
dname2 = workdir / "dicom_sample2"
# folder 1
ZipFile(fname1).extractall(dname1)
# folder 2
ZipFile(fname1).extractall(dname2)
ZipFile(fname2).extractall(dname2)
# a file from each folder
# tests expect this to be a string
fname1 = str(next(dname1.iterdir()))
fname2 = str(next(dname2.iterdir()))
yield dname1, dname2, fname1, fname2
@deprecated_test
def test_read_empty_dir(tmp_path):
# Test that no format is found, but no error is raised
request = core.Request(tmp_path, "ri")
assert iio.formats.search_read_format(request) is None
def test_dcmtk():
# This should not crach, we make no assumptions on whether its
# available or not
imageio.plugins.dicom.get_dcmdjpeg_exe()
@deprecated_test
def test_selection(test_images, tmp_path, examples):
dname1, dname2, fname1, fname2 = examples
# Test that DICOM can examine file
F = iio.formats.search_read_format(core.Request(fname1, "ri"))
assert F.name == "DICOM"
assert isinstance(F, type(iio.formats["DICOM"]))
# Test that we cannot save
request = core.Request(tmp_path / "test.dcm", "wi")
assert not F.can_write(request)
# Test fail on wrong file
fname2 = fname1 + ".fake"
bb = open(fname1, "rb").read()
bb = bb[:128] + b"XXXX" + bb[132:]
open(fname2, "wb").write(bb)
with pytest.raises(Exception):
F.get_reader(core.Request(fname2, "ri"))
# Test special files with other formats
im = iio.imread(test_images / "dicom_file01.dcm")
assert im.shape == (512, 512)
im = iio.imread(test_images / "dicom_file03.dcm")
assert im.shape == (512, 512)
im = iio.imread(test_images / "dicom_file04.dcm")
assert im.shape == (512, 512)
# Expected fails
fname = test_images / "dicom_file90.dcm"
with pytest.raises(RuntimeError):
iio.imread(fname) # 1.2.840.10008.1.2.4.91
fname = test_images / "dicom_file91.dcm"
with pytest.raises(RuntimeError):
iio.imread(fname) # not pixel data
# This one *should* work, but does not, see issue #18
try:
iio.imread(test_images / "dicom_file02.dcm")
except Exception:
pass
def test_progress(examples):
dname1, dname2, fname1, fname2 = examples
iio.imread(fname1, progress=True)
iio.imread(fname1, progress=core.StdoutProgressIndicator("test"))
iio.imread(fname1, progress=None)
with pytest.raises(ValueError):
iio.imread(fname1, progress=3)
def test_different_read_modes(examples):
dname1, dname2, fname1, fname2 = examples
for fname, dname, n in [(fname1, dname1, 1), (fname2, dname2, 2)]:
# Test imread()
im = iio.imread(fname)
assert isinstance(im, np.ndarray)
assert im.shape == (512, 512)
# Test mimread()
ims = iio.mimread(fname)
assert isinstance(ims, list)
assert ims[0].shape == im.shape
assert len(ims) > 1
#
ims2 = iio.mimread(dname, format="DICOM")
assert len(ims) == len(ims2)
# Test volread()
vol = iio.volread(dname, format="DICOM")
assert vol.ndim == 3
assert vol.shape[0] > 10
assert vol.shape[1:] == (512, 512)
#
vol2 = iio.volread(fname) # fname works as well
assert (vol == vol2).all()
# Test mvolread()
vols = iio.mvolread(dname, format="DICOM")
assert isinstance(vols, list)
assert len(vols) == n
assert vols[0].shape == vol.shape
assert sum([v.shape[0] for v in vols]) == len(ims)
def test_different_read_modes_with_readers(examples):
dname1, dname2, fname1, fname2 = examples
for fname, dname, n in [(fname1, dname1, 1), (fname2, dname2, 2)]:
# Test imread()
R = iio.read(fname, "DICOM", "i")
assert len(R) == 1
assert isinstance(R.get_meta_data(), dict)
assert isinstance(R.get_meta_data(0), dict)
# Test mimread()
R = iio.read(fname, "DICOM", "I")
if n == 1:
assert len(R) > 10
else:
assert len(R) == 20 + 25
assert isinstance(R.get_meta_data(), dict)
assert isinstance(R.get_meta_data(0), dict)
# Test volread()
R = iio.read(fname, "DICOM", "v")
assert len(R) == n # we ask for one, but get an honest number
assert isinstance(R.get_meta_data(), dict)
assert isinstance(R.get_meta_data(0), dict)
# Test mvolread()
R = iio.read(fname, "DICOM", "V")
assert len(R) == n
assert isinstance(R.get_meta_data(), dict)
assert isinstance(R.get_meta_data(0), dict)
# Touch DicomSeries objects
assert repr(R._series[0])
assert R._series[0].description
assert len(R._series[0].sampling) == 3
R = iio.read(fname, "DICOM", "?")
with pytest.raises(RuntimeError):
R.get_length()
def test_v3_reading(test_images):
# this is a regression test for
# https://github.com/imageio/imageio/issues/862
expected = iio.imread(test_images / "dicom_file01.dcm")
actual = iio3.imread(test_images / "dicom_file01.dcm")
assert np.allclose(actual, expected)
def test_contiguous_dicom_series(test_images, tmp_path):
# this is a regression test for
# https://github.com/imageio/imageio/issues/1067
fname = test_images / "dicom_issue_1067.zip"
with ZipFile(fname) as zip_ref:
zip_ref.extractall(tmp_path)
dname = tmp_path / "modified_dicom_sample1"
ims = iio.volread(dname, "DICOM")
assert ims.shape == (25, 512, 512)
|