Skip to content

Commit 78f8e71

Browse files
authored
Merge pull request #884 from effigies/test/dicom_pytest
TEST: Convert nibabel.nicom to unittest/pytest
2 parents 328c236 + 40d2471 commit 78f8e71

8 files changed

+256
-243
lines changed

nibabel/nicom/tests/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
# init to allow relative imports in tests
1+
from ...pydicom_compat import have_dicom
2+
import unittest
3+
4+
dicom_test = unittest.skipUnless(have_dicom, "Could not import dicom or pydicom")

nibabel/nicom/tests/test_csareader.py

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77

88
import numpy as np
99

10+
from ...pydicom_compat import pydicom
1011
from .. import csareader as csa
1112
from .. import dwiparams as dwp
1213

13-
from nose.tools import (assert_true, assert_false, assert_equal, assert_raises)
14-
15-
from ...testing import skipif
16-
17-
from nibabel.pydicom_compat import dicom_test, pydicom
18-
from .test_dicomwrappers import (IO_DATA_PATH, DATA)
14+
import pytest
15+
from . import dicom_test
16+
from .test_dicomwrappers import IO_DATA_PATH, DATA
1917

2018
CSA2_B0 = open(pjoin(IO_DATA_PATH, 'csa2_b0.bin'), 'rb').read()
2119
CSA2_B1000 = open(pjoin(IO_DATA_PATH, 'csa2_b1000.bin'), 'rb').read()
@@ -27,59 +25,61 @@
2725
@dicom_test
2826
def test_csa_header_read():
2927
hdr = csa.get_csa_header(DATA, 'image')
30-
assert_equal(hdr['n_tags'], 83)
31-
assert_equal(csa.get_csa_header(DATA, 'series')['n_tags'], 65)
32-
assert_raises(ValueError, csa.get_csa_header, DATA, 'xxxx')
33-
assert_true(csa.is_mosaic(hdr))
28+
assert hdr['n_tags'] == 83
29+
assert csa.get_csa_header(DATA, 'series')['n_tags'] == 65
30+
with pytest.raises(ValueError):
31+
csa.get_csa_header(DATA, 'xxxx')
32+
assert csa.is_mosaic(hdr)
3433
# Get a shallow copy of the data, lacking the CSA marker
3534
# Need to do it this way because del appears broken in pydicom 0.9.7
3635
data2 = pydicom.dataset.Dataset()
3736
for element in DATA:
3837
if (element.tag.group, element.tag.elem) != (0x29, 0x10):
3938
data2.add(element)
40-
assert_equal(csa.get_csa_header(data2, 'image'), None)
39+
assert csa.get_csa_header(data2, 'image') is None
4140
# Add back the marker - CSA works again
4241
data2[(0x29, 0x10)] = DATA[(0x29, 0x10)]
43-
assert_true(csa.is_mosaic(csa.get_csa_header(data2, 'image')))
42+
assert csa.is_mosaic(csa.get_csa_header(data2, 'image'))
4443

4544

4645
def test_csas0():
4746
for csa_str in (CSA2_B0, CSA2_B1000):
4847
csa_info = csa.read(csa_str)
49-
assert_equal(csa_info['type'], 2)
50-
assert_equal(csa_info['n_tags'], 83)
48+
assert csa_info['type'] == 2
49+
assert csa_info['n_tags'] == 83
5150
tags = csa_info['tags']
52-
assert_equal(len(tags), 83)
51+
assert len(tags) == 83
5352
n_o_m = tags['NumberOfImagesInMosaic']
54-
assert_equal(n_o_m['items'], [48])
53+
assert n_o_m['items'] == [48]
5554
csa_info = csa.read(CSA2_B1000)
5655
b_matrix = csa_info['tags']['B_matrix']
57-
assert_equal(len(b_matrix['items']), 6)
56+
assert len(b_matrix['items']) == 6
5857
b_value = csa_info['tags']['B_value']
59-
assert_equal(b_value['items'], [1000])
58+
assert b_value['items'] == [1000]
6059

6160

6261
def test_csa_len0():
6362
# We did get a failure for item with item_len of 0 - gh issue #92
6463
csa_info = csa.read(CSA2_0len)
65-
assert_equal(csa_info['type'], 2)
66-
assert_equal(csa_info['n_tags'], 44)
64+
assert csa_info['type'] == 2
65+
assert csa_info['n_tags'] == 44
6766
tags = csa_info['tags']
68-
assert_equal(len(tags), 44)
67+
assert len(tags) == 44
6968

7069

7170
def test_csa_nitem():
7271
# testing csa.read's ability to raise an error when n_items >= 200
73-
assert_raises(csa.CSAReadError, csa.read, CSA_STR_1001n_items)
72+
with pytest.raises(csa.CSAReadError):
73+
csa.read(CSA_STR_1001n_items)
7474
# OK when < 1000
7575
csa_info = csa.read(CSA_STR_valid)
76-
assert_equal(len(csa_info['tags']), 1)
76+
assert len(csa_info['tags']) == 1
7777
# OK after changing module global
7878
n_items_thresh = csa.MAX_CSA_ITEMS
7979
try:
8080
csa.MAX_CSA_ITEMS = 2000
8181
csa_info = csa.read(CSA_STR_1001n_items)
82-
assert_equal(len(csa_info['tags']), 1)
82+
assert len(csa_info['tags']) == 1
8383
finally:
8484
csa.MAX_CSA_ITEMS = n_items_thresh
8585

@@ -88,32 +88,30 @@ def test_csa_params():
8888
for csa_str in (CSA2_B0, CSA2_B1000):
8989
csa_info = csa.read(csa_str)
9090
n_o_m = csa.get_n_mosaic(csa_info)
91-
assert_equal(n_o_m, 48)
91+
assert n_o_m == 48
9292
snv = csa.get_slice_normal(csa_info)
93-
assert_equal(snv.shape, (3,))
94-
assert_true(np.allclose(1,
95-
np.sqrt((snv * snv).sum())))
93+
assert snv.shape == (3,)
94+
assert np.allclose(1, np.sqrt((snv * snv).sum()))
9695
amt = csa.get_acq_mat_txt(csa_info)
97-
assert_equal(amt, '128p*128')
96+
assert amt == '128p*128'
9897
csa_info = csa.read(CSA2_B0)
9998
b_matrix = csa.get_b_matrix(csa_info)
100-
assert_equal(b_matrix, None)
99+
assert b_matrix is None
101100
b_value = csa.get_b_value(csa_info)
102-
assert_equal(b_value, 0)
101+
assert b_value == 0
103102
g_vector = csa.get_g_vector(csa_info)
104-
assert_equal(g_vector, None)
103+
assert g_vector is None
105104
csa_info = csa.read(CSA2_B1000)
106105
b_matrix = csa.get_b_matrix(csa_info)
107-
assert_equal(b_matrix.shape, (3, 3))
106+
assert b_matrix.shape == (3, 3)
108107
# check (by absence of error) that the B matrix is positive
109108
# semi-definite.
110109
dwp.B2q(b_matrix) # no error
111110
b_value = csa.get_b_value(csa_info)
112-
assert_equal(b_value, 1000)
111+
assert b_value == 1000
113112
g_vector = csa.get_g_vector(csa_info)
114-
assert_equal(g_vector.shape, (3,))
115-
assert_true(
116-
np.allclose(1, np.sqrt((g_vector * g_vector).sum())))
113+
assert g_vector.shape == (3,)
114+
assert np.allclose(1, np.sqrt((g_vector * g_vector).sum()))
117115

118116

119117
def test_ice_dims():
@@ -124,9 +122,8 @@ def test_ice_dims():
124122
for csa_str, ex_dims in ((CSA2_B0, ex_dims0),
125123
(CSA2_B1000, ex_dims1)):
126124
csa_info = csa.read(csa_str)
127-
assert_equal(csa.get_ice_dims(csa_info),
128-
ex_dims)
129-
assert_equal(csa.get_ice_dims({}), None)
125+
assert csa.get_ice_dims(csa_info) == ex_dims
126+
assert csa.get_ice_dims({}) is None
130127

131128

132129
@dicom_test
@@ -138,4 +135,4 @@ def test_missing_csa_elem():
138135
csa_tag = pydicom.dataset.Tag(0x29, 0x1010)
139136
del dcm[csa_tag]
140137
hdr = csa.get_csa_header(dcm, 'image')
141-
assert_equal(hdr, None)
138+
assert hdr is None

nibabel/nicom/tests/test_dicomreaders.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@
22
33
"""
44

5-
from os.path import join as pjoin, abspath
5+
from os.path import join as pjoin
66

77
import numpy as np
88

99
from .. import dicomreaders as didr
10+
from ...pydicom_compat import pydicom
1011

11-
from nibabel.pydicom_compat import dicom_test, pydicom
12+
import pytest
13+
from . import dicom_test
1214

13-
from .test_dicomwrappers import (EXPECTED_AFFINE,
14-
EXPECTED_PARAMS,
15-
IO_DATA_PATH,
16-
DATA)
17-
18-
from nose.tools import (assert_true, assert_false, assert_equal, assert_raises)
15+
from .test_dicomwrappers import EXPECTED_AFFINE, EXPECTED_PARAMS, IO_DATA_PATH, DATA
1916

2017
from numpy.testing import assert_array_equal, assert_array_almost_equal
2118

@@ -24,19 +21,20 @@
2421
def test_read_dwi():
2522
img = didr.mosaic_to_nii(DATA)
2623
arr = img.get_data()
27-
assert_equal(arr.shape, (128, 128, 48))
24+
assert arr.shape == (128, 128, 48)
2825
assert_array_almost_equal(img.affine, EXPECTED_AFFINE)
2926

3027

3128
@dicom_test
3229
def test_read_dwis():
3330
data, aff, bs, gs = didr.read_mosaic_dwi_dir(IO_DATA_PATH,
3431
'siemens_dwi_*.dcm.gz')
35-
assert_equal(data.ndim, 4)
32+
assert data.ndim == 4
3633
assert_array_almost_equal(aff, EXPECTED_AFFINE)
3734
assert_array_almost_equal(bs, (0, EXPECTED_PARAMS[0]))
3835
assert_array_almost_equal(gs, (np.zeros((3,)), EXPECTED_PARAMS[1]))
39-
assert_raises(IOError, didr.read_mosaic_dwi_dir, 'improbable')
36+
with pytest.raises(IOError):
37+
didr.read_mosaic_dwi_dir('improbable')
4038

4139

4240
@dicom_test
@@ -53,29 +51,21 @@ def test_passing_kwds():
5351
dicom_kwargs=dict(force=True))
5452
assert_array_equal(data, data2)
5553
# This should raise an error in pydicom.dicomio.read_file
56-
assert_raises(TypeError,
57-
func,
58-
IO_DATA_PATH,
59-
dwi_glob,
60-
dicom_kwargs=dict(not_a_parameter=True))
54+
with pytest.raises(TypeError):
55+
func(IO_DATA_PATH, dwi_glob, dicom_kwargs=dict(not_a_parameter=True))
6156
# These are invalid dicoms, so will raise an error unless force=True
62-
assert_raises(pydicom.filereader.InvalidDicomError,
63-
func,
64-
IO_DATA_PATH,
65-
csa_glob)
57+
with pytest.raises(pydicom.filereader.InvalidDicomError):
58+
func(IO_DATA_PATH, csa_glob)
6659
# But here, we catch the error because the dicoms are in the wrong
6760
# format
68-
assert_raises(didr.DicomReadError,
69-
func,
70-
IO_DATA_PATH,
71-
csa_glob,
72-
dicom_kwargs=dict(force=True))
61+
with pytest.raises(didr.DicomReadError):
62+
func(IO_DATA_PATH, csa_glob, dicom_kwargs=dict(force=True))
7363

7464
@dicom_test
7565
def test_slices_to_series():
7666
dicom_files = (pjoin(IO_DATA_PATH, "%d.dcm" % i) for i in range(2))
7767
wrappers = [didr.wrapper_from_file(f) for f in dicom_files]
7868
series = didr.slices_to_series(wrappers)
79-
assert_equal(len(series), 1)
80-
assert_equal(len(series[0]), 2)
69+
assert len(series) == 1
70+
assert len(series[0]) == 2
8171

0 commit comments

Comments
 (0)