forked from nipy/nibabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_dicomreaders.py
72 lines (61 loc) · 2.46 KB
/
test_dicomreaders.py
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
""" Testing reading DICOM files
"""
import numpy as np
from .. import dicomreaders as didr
from .test_dicomwrappers import (dicom_test,
EXPECTED_AFFINE,
EXPECTED_PARAMS,
IO_DATA_PATH,
DATA)
from nose.tools import (assert_true, assert_false, assert_equal, assert_raises)
from numpy.testing import assert_array_equal, assert_array_almost_equal
@dicom_test
def test_read_dwi():
img = didr.mosaic_to_nii(DATA)
arr = img.get_data()
assert_equal(arr.shape, (128,128,48))
assert_array_almost_equal(img.affine, EXPECTED_AFFINE)
@dicom_test
def test_read_dwis():
data, aff, bs, gs = didr.read_mosaic_dwi_dir(IO_DATA_PATH,
'siemens_dwi_*.dcm.gz')
assert_equal(data.ndim, 4)
assert_array_almost_equal(aff, EXPECTED_AFFINE)
assert_array_almost_equal(bs, (0, EXPECTED_PARAMS[0]))
assert_array_almost_equal(gs, (np.zeros((3,)), EXPECTED_PARAMS[1]))
assert_raises(IOError, didr.read_mosaic_dwi_dir, 'improbable')
@dicom_test
def test_passing_kwds():
# Check that we correctly pass keywords to dicom
dwi_glob = 'siemens_dwi_*.dcm.gz'
csa_glob = 'csa*.bin'
try:
from dicom.filereader import InvalidDicomError
except ImportError:
from pydicom.filereader import InvalidDicomError
for func in (didr.read_mosaic_dwi_dir, didr.read_mosaic_dir):
data, aff, bs, gs = func(IO_DATA_PATH, dwi_glob)
# This should not raise an error
data2, aff2, bs2, gs2 = func(
IO_DATA_PATH,
dwi_glob,
dicom_kwargs=dict(force=True))
assert_array_equal(data, data2)
# This should raise an error in pydicom.dicomio.read_file
assert_raises(TypeError,
func,
IO_DATA_PATH,
dwi_glob,
dicom_kwargs=dict(not_a_parameter=True))
# These are invalid dicoms, so will raise an error unless force=True
assert_raises(InvalidDicomError,
func,
IO_DATA_PATH,
csa_glob)
# But here, we catch the error because the dicoms are in the wrong
# format
assert_raises(didr.DicomReadError,
func,
IO_DATA_PATH,
csa_glob,
dicom_kwargs=dict(force=True))