14
14
from .. import tck as tck_module
15
15
from ..tck import TckFile
16
16
17
- import pytest ; pytestmark = pytest .mark .skip ()
18
-
19
- from nose .tools import assert_equal , assert_raises , assert_true
17
+ import pytest
20
18
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
22
20
from .test_tractogram import assert_tractogram_equal
23
21
24
22
DATA = {}
25
23
26
24
27
- def setup ():
25
+ def setup_module ():
28
26
global DATA
29
27
30
28
DATA ['empty_tck_fname' ] = pjoin (data_path , "empty.tck" )
@@ -71,8 +69,8 @@ def test_load_matlab_nan_file(self):
71
69
for lazy_load in [False , True ]:
72
70
tck = TckFile .load (DATA ['matlab_nan_tck_fname' ], lazy_load = lazy_load )
73
71
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 )
76
74
77
75
def test_writeable_data (self ):
78
76
data = DATA ['simple_tractogram' ]
@@ -82,53 +80,57 @@ def test_writeable_data(self):
82
80
for actual , expected_tgi in zip (tck .streamlines , data ):
83
81
assert_array_equal (actual , expected_tgi .streamline )
84
82
# Test we can write to arrays
85
- assert_true ( actual .flags .writeable )
83
+ assert actual .flags .writeable
86
84
actual [0 , 0 ] = 99
87
85
88
86
def test_load_simple_file_in_big_endian (self ):
89
87
for lazy_load in [False , True ]:
90
88
tck = TckFile .load (DATA ['simple_tck_big_endian_fname' ],
91
89
lazy_load = lazy_load )
92
90
assert_tractogram_equal (tck .tractogram , DATA ['simple_tractogram' ])
93
- assert_equal ( tck .header ['datatype' ], 'Float32BE' )
91
+ assert tck .header ['datatype' ] == 'Float32BE'
94
92
95
93
def test_load_file_with_wrong_information (self ):
96
94
tck_file = open (DATA ['simple_tck_fname' ], 'rb' ).read ()
97
95
98
96
# Simulate a TCK file where `datatype` has not the right endianness.
99
97
new_tck_file = tck_file .replace (asbytes ("Float32LE" ),
100
98
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 ))
102
102
103
103
# Simulate a TCK file with unsupported `datatype`.
104
104
new_tck_file = tck_file .replace (asbytes ("Float32LE" ),
105
105
asbytes ("int32" ))
106
- assert_raises (HeaderError , TckFile .load , BytesIO (new_tck_file ))
106
+ with pytest .raises (HeaderError ):
107
+ TckFile .load (BytesIO (new_tck_file ))
107
108
108
109
# Simulate a TCK file with no `datatype` field.
109
110
new_tck_file = tck_file .replace (b"datatype: Float32LE\n " , b"" )
110
111
# Need to adjust data offset.
111
112
new_tck_file = new_tck_file .replace (b"file: . 67\n " , b"file: . 47\n " )
112
113
with clear_and_catch_warnings (record = True , modules = [tck_module ]) as w :
113
114
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 )
117
118
assert_array_equal (tck .header ['datatype' ], "Float32LE" )
118
119
119
120
# Simulate a TCK file with no `file` field.
120
121
new_tck_file = tck_file .replace (b"\n file: . 67" , b"" )
121
122
with clear_and_catch_warnings (record = True , modules = [tck_module ]) as w :
122
123
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 )
126
127
assert_array_equal (tck .header ['file' ], ". 56" )
127
128
128
129
# Simulate a TCK file with `file` field pointing to another file.
129
130
new_tck_file = tck_file .replace (b"file: . 67\n " ,
130
131
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 ))
132
134
133
135
# Simulate a TCK file which is missing a streamline delimiter.
134
136
eos = TckFile .FIBER_DELIMITER .tostring ()
@@ -139,11 +141,13 @@ def test_load_file_with_wrong_information(self):
139
141
buffer_size = 1. / 1024 ** 2 # 1 bytes
140
142
hdr = TckFile ._read_header (BytesIO (new_tck_file ))
141
143
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 )
143
146
144
147
# Simulate a TCK file which is missing the end-of-file delimiter.
145
148
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 ))
147
151
148
152
def test_write_empty_file (self ):
149
153
tractogram = Tractogram (affine_to_rasmm = np .eye (4 ))
@@ -160,8 +164,7 @@ def test_write_empty_file(self):
160
164
assert_tractogram_equal (new_tck .tractogram , new_tck_orig .tractogram )
161
165
162
166
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 ()
165
168
166
169
def test_write_simple_file (self ):
167
170
tractogram = Tractogram (DATA ['streamlines' ],
@@ -179,17 +182,18 @@ def test_write_simple_file(self):
179
182
assert_tractogram_equal (new_tck .tractogram , new_tck_orig .tractogram )
180
183
181
184
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 ()
184
186
185
187
# TCK file containing not well formatted entries in its header.
186
188
tck_file = BytesIO ()
187
189
tck = TckFile (tractogram )
188
190
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 )
190
193
191
194
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 )
193
197
194
198
def test_load_write_file (self ):
195
199
for fname in [DATA ['empty_tck_fname' ],
@@ -204,7 +208,7 @@ def test_load_write_file(self):
204
208
205
209
# Check that the written file is the same as the one read.
206
210
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 ()
208
212
209
213
# Save tractogram that has an affine_to_rasmm.
210
214
for lazy_load in [False , True ]:
0 commit comments