Skip to content

Commit 0011c11

Browse files
authored
Merge pull request #874 from robbisg/test_streamline
TEST: test_tck and test_trk conversion to pytest
2 parents 636dc55 + 3a2b0d1 commit 0011c11

File tree

2 files changed

+100
-85
lines changed

2 files changed

+100
-85
lines changed

nibabel/streamlines/tests/test_tck.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@
1414
from .. import tck as tck_module
1515
from ..tck import TckFile
1616

17-
import pytest; pytestmark = pytest.mark.skip()
18-
19-
from nose.tools import assert_equal, assert_raises, assert_true
17+
import pytest
2018
from numpy.testing import assert_array_equal
21-
from nibabel.testing import data_path, clear_and_catch_warnings
19+
from ...testing_pytest import data_path, clear_and_catch_warnings
2220
from .test_tractogram import assert_tractogram_equal
2321

2422
DATA = {}
2523

2624

27-
def setup():
25+
def setup_module():
2826
global DATA
2927

3028
DATA['empty_tck_fname'] = pjoin(data_path, "empty.tck")
@@ -71,8 +69,8 @@ def test_load_matlab_nan_file(self):
7169
for lazy_load in [False, True]:
7270
tck = TckFile.load(DATA['matlab_nan_tck_fname'], lazy_load=lazy_load)
7371
streamlines = list(tck.tractogram.streamlines)
74-
assert_equal(len(streamlines), 1)
75-
assert_equal(streamlines[0].shape, (108, 3))
72+
assert len(streamlines) == 1
73+
assert streamlines[0].shape == (108, 3)
7674

7775
def test_writeable_data(self):
7876
data = DATA['simple_tractogram']
@@ -82,53 +80,57 @@ def test_writeable_data(self):
8280
for actual, expected_tgi in zip(tck.streamlines, data):
8381
assert_array_equal(actual, expected_tgi.streamline)
8482
# Test we can write to arrays
85-
assert_true(actual.flags.writeable)
83+
assert actual.flags.writeable
8684
actual[0, 0] = 99
8785

8886
def test_load_simple_file_in_big_endian(self):
8987
for lazy_load in [False, True]:
9088
tck = TckFile.load(DATA['simple_tck_big_endian_fname'],
9189
lazy_load=lazy_load)
9290
assert_tractogram_equal(tck.tractogram, DATA['simple_tractogram'])
93-
assert_equal(tck.header['datatype'], 'Float32BE')
91+
assert tck.header['datatype'] == 'Float32BE'
9492

9593
def test_load_file_with_wrong_information(self):
9694
tck_file = open(DATA['simple_tck_fname'], 'rb').read()
9795

9896
# Simulate a TCK file where `datatype` has not the right endianness.
9997
new_tck_file = tck_file.replace(asbytes("Float32LE"),
10098
asbytes("Float32BE"))
101-
assert_raises(DataError, TckFile.load, BytesIO(new_tck_file))
99+
100+
with pytest.raises(DataError):
101+
TckFile.load(BytesIO(new_tck_file))
102102

103103
# Simulate a TCK file with unsupported `datatype`.
104104
new_tck_file = tck_file.replace(asbytes("Float32LE"),
105105
asbytes("int32"))
106-
assert_raises(HeaderError, TckFile.load, BytesIO(new_tck_file))
106+
with pytest.raises(HeaderError):
107+
TckFile.load(BytesIO(new_tck_file))
107108

108109
# Simulate a TCK file with no `datatype` field.
109110
new_tck_file = tck_file.replace(b"datatype: Float32LE\n", b"")
110111
# Need to adjust data offset.
111112
new_tck_file = new_tck_file.replace(b"file: . 67\n", b"file: . 47\n")
112113
with clear_and_catch_warnings(record=True, modules=[tck_module]) as w:
113114
tck = TckFile.load(BytesIO(new_tck_file))
114-
assert_equal(len(w), 1)
115-
assert_true(issubclass(w[0].category, HeaderWarning))
116-
assert_true("Missing 'datatype'" in str(w[0].message))
115+
assert len(w) == 1
116+
assert issubclass(w[0].category, HeaderWarning)
117+
assert "Missing 'datatype'" in str(w[0].message)
117118
assert_array_equal(tck.header['datatype'], "Float32LE")
118119

119120
# Simulate a TCK file with no `file` field.
120121
new_tck_file = tck_file.replace(b"\nfile: . 67", b"")
121122
with clear_and_catch_warnings(record=True, modules=[tck_module]) as w:
122123
tck = TckFile.load(BytesIO(new_tck_file))
123-
assert_equal(len(w), 1)
124-
assert_true(issubclass(w[0].category, HeaderWarning))
125-
assert_true("Missing 'file'" in str(w[0].message))
124+
assert len(w) == 1
125+
assert issubclass(w[0].category, HeaderWarning)
126+
assert "Missing 'file'" in str(w[0].message)
126127
assert_array_equal(tck.header['file'], ". 56")
127128

128129
# Simulate a TCK file with `file` field pointing to another file.
129130
new_tck_file = tck_file.replace(b"file: . 67\n",
130131
b"file: dummy.mat 75\n")
131-
assert_raises(HeaderError, TckFile.load, BytesIO(new_tck_file))
132+
with pytest.raises(HeaderError):
133+
TckFile.load(BytesIO(new_tck_file))
132134

133135
# Simulate a TCK file which is missing a streamline delimiter.
134136
eos = TckFile.FIBER_DELIMITER.tostring()
@@ -139,11 +141,13 @@ def test_load_file_with_wrong_information(self):
139141
buffer_size = 1. / 1024**2 # 1 bytes
140142
hdr = TckFile._read_header(BytesIO(new_tck_file))
141143
tck_reader = TckFile._read(BytesIO(new_tck_file), hdr, buffer_size)
142-
assert_raises(DataError, list, tck_reader)
144+
with pytest.raises(DataError):
145+
list(tck_reader)
143146

144147
# Simulate a TCK file which is missing the end-of-file delimiter.
145148
new_tck_file = tck_file[:-len(eof)]
146-
assert_raises(DataError, TckFile.load, BytesIO(new_tck_file))
149+
with pytest.raises(DataError):
150+
TckFile.load(BytesIO(new_tck_file))
147151

148152
def test_write_empty_file(self):
149153
tractogram = Tractogram(affine_to_rasmm=np.eye(4))
@@ -160,8 +164,7 @@ def test_write_empty_file(self):
160164
assert_tractogram_equal(new_tck.tractogram, new_tck_orig.tractogram)
161165

162166
tck_file.seek(0, os.SEEK_SET)
163-
assert_equal(tck_file.read(),
164-
open(DATA['empty_tck_fname'], 'rb').read())
167+
assert tck_file.read() == open(DATA['empty_tck_fname'], 'rb').read()
165168

166169
def test_write_simple_file(self):
167170
tractogram = Tractogram(DATA['streamlines'],
@@ -179,17 +182,18 @@ def test_write_simple_file(self):
179182
assert_tractogram_equal(new_tck.tractogram, new_tck_orig.tractogram)
180183

181184
tck_file.seek(0, os.SEEK_SET)
182-
assert_equal(tck_file.read(),
183-
open(DATA['simple_tck_fname'], 'rb').read())
185+
assert tck_file.read() == open(DATA['simple_tck_fname'], 'rb').read()
184186

185187
# TCK file containing not well formatted entries in its header.
186188
tck_file = BytesIO()
187189
tck = TckFile(tractogram)
188190
tck.header['new_entry'] = 'value\n' # \n not allowed
189-
assert_raises(HeaderError, tck.save, tck_file)
191+
with pytest.raises(HeaderError):
192+
tck.save(tck_file)
190193

191194
tck.header['new_entry'] = 'val:ue' # : not allowed
192-
assert_raises(HeaderError, tck.save, tck_file)
195+
with pytest.raises(HeaderError):
196+
tck.save(tck_file)
193197

194198
def test_load_write_file(self):
195199
for fname in [DATA['empty_tck_fname'],
@@ -204,7 +208,7 @@ def test_load_write_file(self):
204208

205209
# Check that the written file is the same as the one read.
206210
tck_file.seek(0, os.SEEK_SET)
207-
assert_equal(tck_file.read(), open(fname, 'rb').read())
211+
assert tck_file.read() == open(fname, 'rb').read()
208212

209213
# Save tractogram that has an affine_to_rasmm.
210214
for lazy_load in [False, True]:

0 commit comments

Comments
 (0)