10
10
from nibabel .tmpdirs import InTemporaryDirectory
11
11
from numpy .compat .py3k import asbytes
12
12
13
- from nibabel .testing import data_path
14
- from nibabel .testing import clear_and_catch_warnings
15
- from nose .tools import assert_equal , assert_raises , assert_true , assert_false
13
+ from nibabel .testing import data_path , clear_and_catch_warnings
16
14
17
15
from .test_tractogram import assert_tractogram_equal
18
16
from ..tractogram import Tractogram , LazyTractogram
@@ -82,50 +80,50 @@ def test_is_supported_detect_format():
82
80
# Test is_supported and detect_format functions
83
81
# Empty file/string
84
82
f = BytesIO ()
85
- assert_false ( nib .streamlines .is_supported (f ) )
86
- assert_false ( nib .streamlines .is_supported ("" ) )
87
- assert_true ( nib .streamlines .detect_format (f ) is None )
88
- assert_true ( nib .streamlines .detect_format ("" ) is None )
83
+ assert not nib .streamlines .is_supported (f )
84
+ assert not nib .streamlines .is_supported ("" )
85
+ assert nib .streamlines .detect_format (f ) is None
86
+ assert nib .streamlines .detect_format ("" ) is None
89
87
90
88
# Valid file without extension
91
89
for tfile_cls in FORMATS .values ():
92
90
f = BytesIO ()
93
91
f .write (asbytes (tfile_cls .MAGIC_NUMBER ))
94
92
f .seek (0 , os .SEEK_SET )
95
- assert_true ( nib .streamlines .is_supported (f ) )
96
- assert_true ( nib .streamlines .detect_format (f ) is tfile_cls )
93
+ assert nib .streamlines .is_supported (f )
94
+ assert nib .streamlines .detect_format (f ) is tfile_cls
97
95
98
96
# Wrong extension but right magic number
99
97
for tfile_cls in FORMATS .values ():
100
98
with tempfile .TemporaryFile (mode = "w+b" , suffix = ".txt" ) as f :
101
99
f .write (asbytes (tfile_cls .MAGIC_NUMBER ))
102
100
f .seek (0 , os .SEEK_SET )
103
- assert_true ( nib .streamlines .is_supported (f ) )
104
- assert_true ( nib .streamlines .detect_format (f ) is tfile_cls )
101
+ assert nib .streamlines .is_supported (f )
102
+ assert nib .streamlines .detect_format (f ) is tfile_cls
105
103
106
104
# Good extension but wrong magic number
107
105
for ext , tfile_cls in FORMATS .items ():
108
106
with tempfile .TemporaryFile (mode = "w+b" , suffix = ext ) as f :
109
107
f .write (b"pass" )
110
108
f .seek (0 , os .SEEK_SET )
111
- assert_false ( nib .streamlines .is_supported (f ) )
112
- assert_true ( nib .streamlines .detect_format (f ) is None )
109
+ assert not nib .streamlines .is_supported (f )
110
+ assert nib .streamlines .detect_format (f ) is None
113
111
114
112
# Wrong extension, string only
115
113
f = "my_tractogram.asd"
116
- assert_false ( nib .streamlines .is_supported (f ) )
117
- assert_true ( nib .streamlines .detect_format (f ) is None )
114
+ assert not nib .streamlines .is_supported (f )
115
+ assert nib .streamlines .detect_format (f ) is None
118
116
119
117
# Good extension, string only
120
118
for ext , tfile_cls in FORMATS .items ():
121
119
f = "my_tractogram" + ext
122
- assert_true ( nib .streamlines .is_supported (f ) )
123
- assert_equal ( nib .streamlines .detect_format (f ), tfile_cls )
120
+ assert nib .streamlines .is_supported (f )
121
+ assert nib .streamlines .detect_format (f ) == tfile_cls
124
122
125
123
# Extension should not be case-sensitive.
126
124
for ext , tfile_cls in FORMATS .items ():
127
125
f = "my_tractogram" + ext .upper ()
128
- assert_true ( nib .streamlines .detect_format (f ) is tfile_cls )
126
+ assert nib .streamlines .detect_format (f ) is tfile_cls
129
127
130
128
131
129
class TestLoadSave (unittest .TestCase ):
@@ -135,12 +133,12 @@ def test_load_empty_file(self):
135
133
for empty_filename in DATA ['empty_filenames' ]:
136
134
tfile = nib .streamlines .load (empty_filename ,
137
135
lazy_load = lazy_load )
138
- assert_true ( isinstance (tfile , TractogramFile ) )
136
+ assert isinstance (tfile , TractogramFile )
139
137
140
138
if lazy_load :
141
- assert_true ( type (tfile .tractogram ), Tractogram )
139
+ assert type (tfile .tractogram ), Tractogram
142
140
else :
143
- assert_true ( type (tfile .tractogram ), LazyTractogram )
141
+ assert type (tfile .tractogram ), LazyTractogram
144
142
145
143
assert_tractogram_equal (tfile .tractogram ,
146
144
DATA ['empty_tractogram' ])
@@ -150,12 +148,12 @@ def test_load_simple_file(self):
150
148
for simple_filename in DATA ['simple_filenames' ]:
151
149
tfile = nib .streamlines .load (simple_filename ,
152
150
lazy_load = lazy_load )
153
- assert_true ( isinstance (tfile , TractogramFile ) )
151
+ assert isinstance (tfile , TractogramFile )
154
152
155
153
if lazy_load :
156
- assert_true ( type (tfile .tractogram ), Tractogram )
154
+ assert type (tfile .tractogram ), Tractogram
157
155
else :
158
- assert_true ( type (tfile .tractogram ), LazyTractogram )
156
+ assert type (tfile .tractogram ), LazyTractogram
159
157
160
158
assert_tractogram_equal (tfile .tractogram ,
161
159
DATA ['simple_tractogram' ])
@@ -165,12 +163,12 @@ def test_load_complex_file(self):
165
163
for complex_filename in DATA ['complex_filenames' ]:
166
164
tfile = nib .streamlines .load (complex_filename ,
167
165
lazy_load = lazy_load )
168
- assert_true ( isinstance (tfile , TractogramFile ) )
166
+ assert isinstance (tfile , TractogramFile )
169
167
170
168
if lazy_load :
171
- assert_true ( type (tfile .tractogram ), Tractogram )
169
+ assert type (tfile .tractogram ), Tractogram
172
170
else :
173
- assert_true ( type (tfile .tractogram ), LazyTractogram )
171
+ assert type (tfile .tractogram ), LazyTractogram
174
172
175
173
tractogram = Tractogram (DATA ['streamlines' ],
176
174
affine_to_rasmm = np .eye (4 ))
@@ -191,19 +189,19 @@ def test_save_tractogram_file(self):
191
189
trk_file = trk .TrkFile (tractogram )
192
190
193
191
# No need for keyword arguments.
194
- assert_raises ( ValueError , nib . streamlines . save ,
195
- trk_file , "dummy.trk" , header = {})
192
+ with self . assertRaises ( ValueError ):
193
+ nib . streamlines . save ( trk_file , "dummy.trk" , header = {})
196
194
197
195
# Wrong extension.
198
196
with clear_and_catch_warnings (record = True ,
199
197
modules = [nib .streamlines ]) as w :
200
198
trk_file = trk .TrkFile (tractogram )
201
- assert_raises ( ValueError , nib . streamlines . save ,
202
- trk_file , "dummy.tck" , header = {})
199
+ with self . assertRaises ( ValueError ):
200
+ nib . streamlines . save ( trk_file , "dummy.tck" , header = {})
203
201
204
- assert_equal ( len (w ), 1 )
205
- assert_true ( issubclass (w [0 ].category , ExtensionWarning ) )
206
- assert_true ( "extension" in str (w [0 ].message ) )
202
+ assert len (w ) == 1
203
+ assert issubclass (w [0 ].category , ExtensionWarning )
204
+ assert "extension" in str (w [0 ].message )
207
205
208
206
with InTemporaryDirectory ():
209
207
nib .streamlines .save (trk_file , "dummy.trk" )
@@ -250,9 +248,9 @@ def test_save_complex_file(self):
250
248
((not cls .SUPPORTS_DATA_PER_POINT ) +
251
249
(not cls .SUPPORTS_DATA_PER_STREAMLINE ))
252
250
253
- assert_equal ( len (w ), nb_expected_warnings )
251
+ assert len (w ) == nb_expected_warnings
254
252
for i in range (nb_expected_warnings ):
255
- assert_true ( issubclass (w [i ].category , Warning ) )
253
+ assert issubclass (w [i ].category , Warning )
256
254
257
255
tractogram = Tractogram (DATA ['streamlines' ],
258
256
affine_to_rasmm = np .eye (4 ))
@@ -281,10 +279,12 @@ def test_save_sliced_tractogram(self):
281
279
assert_tractogram_equal (tractogram , original_tractogram )
282
280
283
281
def test_load_unknown_format (self ):
284
- assert_raises (ValueError , nib .streamlines .load , "" )
282
+ with self .assertRaises (ValueError ):
283
+ nib .streamlines .load ("" )
285
284
286
285
def test_save_unknown_format (self ):
287
- assert_raises (ValueError , nib .streamlines .save , Tractogram (), "" )
286
+ with self .assertRaises (ValueError ):
287
+ nib .streamlines .save (Tractogram (), "" )
288
288
289
289
def test_save_from_generator (self ):
290
290
tractogram = Tractogram (DATA ['streamlines' ],
0 commit comments