22
22
23
23
from numpy .testing import assert_array_almost_equal
24
24
25
- from nose .tools import (assert_true , assert_false , assert_equal ,
26
- assert_raises )
25
+ import pytest
27
26
from ...testing import clear_and_catch_warnings
28
27
29
28
@@ -106,9 +105,9 @@ def assert_default_types(loaded):
106
105
if defaulttype is type (None ):
107
106
continue
108
107
loadedtype = type (getattr (loaded , attr ))
109
- assert_equal ( loadedtype , defaulttype ,
110
- "Type mismatch for attribute: {} ({!s} != {!s})" .format (
111
- attr , loadedtype , defaulttype ))
108
+ assert loadedtype == defaulttype , (
109
+ "Type mismatch for attribute: {} ({!s} != {!s})" .format (
110
+ attr , loadedtype , defaulttype ))
112
111
113
112
114
113
def test_default_types ():
@@ -142,18 +141,18 @@ def test_read_ordering():
142
141
# read another image first (DATA_FILE2) then the shape is wrong
143
142
# Read an image
144
143
img2 = load (DATA_FILE2 )
145
- assert_equal ( img2 .darrays [0 ].data .shape , (143479 , 1 ) )
144
+ assert img2 .darrays [0 ].data .shape == (143479 , 1 )
146
145
# Read image for which we know output shape
147
146
img = load (DATA_FILE1 )
148
- assert_equal ( img .darrays [0 ].data .shape , (3 , 3 ) )
147
+ assert img .darrays [0 ].data .shape == (3 , 3 )
149
148
150
149
151
150
def test_load_metadata ():
152
151
for i , dat in enumerate (datafiles ):
153
152
img = load (dat )
154
153
img .meta
155
- assert_equal ( numDA [i ], img .numDA )
156
- assert_equal ( img .version , '1.0' )
154
+ assert numDA [i ] == img .numDA
155
+ assert img .version == '1.0'
157
156
158
157
159
158
def test_metadata_deprecations ():
@@ -163,12 +162,12 @@ def test_metadata_deprecations():
163
162
# Test deprecation
164
163
with clear_and_catch_warnings () as w :
165
164
warnings .filterwarnings ('once' , category = DeprecationWarning )
166
- assert_equal ( me , img .get_meta () )
165
+ assert me == img .get_meta ()
167
166
168
167
with clear_and_catch_warnings () as w :
169
168
warnings .filterwarnings ('once' , category = DeprecationWarning )
170
169
img .set_metadata (me )
171
- assert_equal ( me , img .meta )
170
+ assert me == img .meta
172
171
173
172
174
173
def test_load_dataarray1 ():
@@ -181,14 +180,14 @@ def test_load_dataarray1():
181
180
assert_array_almost_equal (img .darrays [0 ].data , DATA_FILE1_darr1 )
182
181
assert_array_almost_equal (img .darrays [1 ].data , DATA_FILE1_darr2 )
183
182
me = img .darrays [0 ].meta .metadata
184
- assert_true ( 'AnatomicalStructurePrimary' in me )
185
- assert_true ( 'AnatomicalStructureSecondary' in me )
186
- assert_equal ( me ['AnatomicalStructurePrimary' ], 'CortexLeft' )
183
+ assert 'AnatomicalStructurePrimary' in me
184
+ assert 'AnatomicalStructureSecondary' in me
185
+ me ['AnatomicalStructurePrimary' ] == 'CortexLeft'
187
186
assert_array_almost_equal (img .darrays [0 ].coordsys .xform , np .eye (4 , 4 ))
188
- assert_equal ( xform_codes .niistring [ img . darrays [
189
- 0 ].coordsys .dataspace ], 'NIFTI_XFORM_TALAIRACH' )
190
- assert_equal ( xform_codes .niistring [img .darrays [
191
- 0 ].coordsys .xformspace ], 'NIFTI_XFORM_TALAIRACH' )
187
+ assert xform_codes .niistring [
188
+ img . darrays [ 0 ].coordsys .dataspace ] == 'NIFTI_XFORM_TALAIRACH'
189
+ assert xform_codes .niistring [img .darrays [
190
+ 0 ].coordsys .xformspace ] == 'NIFTI_XFORM_TALAIRACH'
192
191
193
192
194
193
def test_load_dataarray2 ():
@@ -223,7 +222,7 @@ def test_load_dataarray4():
223
222
def test_dataarray5 ():
224
223
img5 = load (DATA_FILE5 )
225
224
for da in img5 .darrays :
226
- assert_equal ( gifti_endian_codes .byteorder [da .endian ], 'little' )
225
+ gifti_endian_codes .byteorder [da .endian ] == 'little'
227
226
assert_array_almost_equal (img5 .darrays [0 ].data , DATA_FILE5_darr1 )
228
227
assert_array_almost_equal (img5 .darrays [1 ].data , DATA_FILE5_darr2 )
229
228
# Round trip tested below
@@ -234,24 +233,24 @@ def test_base64_written():
234
233
with open (DATA_FILE5 , 'rb' ) as fobj :
235
234
contents = fobj .read ()
236
235
# Confirm the bad tags are still in the file
237
- assert_true ( b'GIFTI_ENCODING_B64BIN' in contents )
238
- assert_true ( b'GIFTI_ENDIAN_LITTLE' in contents )
236
+ assert b'GIFTI_ENCODING_B64BIN' in contents
237
+ assert b'GIFTI_ENDIAN_LITTLE' in contents
239
238
# The good ones are missing
240
- assert_false ( b'Base64Binary' in contents )
241
- assert_false ( b'LittleEndian' in contents )
239
+ assert b'Base64Binary' not in contents
240
+ assert b'LittleEndian' not in contents
242
241
# Round trip
243
242
img5 = load (DATA_FILE5 )
244
243
save (img5 , 'fixed.gii' )
245
244
with open ('fixed.gii' , 'rb' ) as fobj :
246
245
contents = fobj .read ()
247
246
# The bad codes have gone, replaced by the good ones
248
- assert_false ( b'GIFTI_ENCODING_B64BIN' in contents )
249
- assert_false ( b'GIFTI_ENDIAN_LITTLE' in contents )
250
- assert_true ( b'Base64Binary' in contents )
247
+ assert b'GIFTI_ENCODING_B64BIN' not in contents
248
+ assert b'GIFTI_ENDIAN_LITTLE' not in contents
249
+ assert b'Base64Binary' in contents
251
250
if sys .byteorder == 'little' :
252
- assert_true ( b'LittleEndian' in contents )
251
+ assert b'LittleEndian' in contents
253
252
else :
254
- assert_true ( b'BigEndian' in contents )
253
+ assert b'BigEndian' in contents
255
254
img5_fixed = load ('fixed.gii' )
256
255
darrays = img5_fixed .darrays
257
256
assert_array_almost_equal (darrays [0 ].data , DATA_FILE5_darr1 )
@@ -263,7 +262,7 @@ def test_readwritedata():
263
262
with InTemporaryDirectory ():
264
263
save (img , 'test.gii' )
265
264
img2 = load ('test.gii' )
266
- assert_equal ( img .numDA , img2 .numDA )
265
+ assert img .numDA == img2 .numDA
267
266
assert_array_almost_equal (img .darrays [0 ].data ,
268
267
img2 .darrays [0 ].data )
269
268
@@ -272,7 +271,7 @@ def test_modify_darray():
272
271
img = load (fname )
273
272
darray = img .darrays [0 ]
274
273
darray .data [:] = 0
275
- assert_true ( np .array_equiv (darray .data , 0 ) )
274
+ assert np .array_equiv (darray .data , 0 )
276
275
277
276
278
277
def test_write_newmetadata ():
@@ -281,32 +280,32 @@ def test_write_newmetadata():
281
280
newmeta = gi .GiftiMetaData (attr )
282
281
img .meta = newmeta
283
282
myme = img .meta .metadata
284
- assert_true ( 'mykey' in myme )
283
+ assert 'mykey' in myme
285
284
newmeta = gi .GiftiMetaData .from_dict ({'mykey1' : 'val2' })
286
285
img .meta = newmeta
287
286
myme = img .meta .metadata
288
- assert_true ( 'mykey1' in myme )
289
- assert_false ( 'mykey' in myme )
287
+ assert 'mykey1' in myme
288
+ assert 'mykey' not in myme
290
289
291
290
292
291
def test_load_getbyintent ():
293
292
img = load (DATA_FILE1 )
294
293
da = img .get_arrays_from_intent ("NIFTI_INTENT_POINTSET" )
295
- assert_equal ( len (da ), 1 )
294
+ assert len (da ) == 1
296
295
297
296
with clear_and_catch_warnings () as w :
298
297
warnings .filterwarnings ('once' , category = DeprecationWarning )
299
298
da = img .getArraysFromIntent ("NIFTI_INTENT_POINTSET" )
300
- assert_equal ( len (da ), 1 )
301
- assert_equal ( len (w ), 1 )
302
- assert_equal ( w [0 ].category , DeprecationWarning )
299
+ assert len (da ) == 1
300
+ assert len (w ) == 1
301
+ w [0 ].category == DeprecationWarning
303
302
304
303
da = img .get_arrays_from_intent ("NIFTI_INTENT_TRIANGLE" )
305
- assert_equal ( len (da ), 1 )
304
+ assert len (da ) == 1
306
305
307
306
da = img .get_arrays_from_intent ("NIFTI_INTENT_CORREL" )
308
- assert_equal ( len (da ), 0 )
309
- assert_equal ( da , [])
307
+ assert len (da ) == 0
308
+ assert da == []
310
309
311
310
312
311
def test_load_labeltable ():
@@ -317,15 +316,15 @@ def test_load_labeltable():
317
316
bimg = load ('test.gii' )
318
317
for img in (img6 , bimg ):
319
318
assert_array_almost_equal (img .darrays [0 ].data [:3 ], DATA_FILE6_darr1 )
320
- assert_equal ( len (img .labeltable .labels ), 36 )
319
+ assert len (img .labeltable .labels ) == 36
321
320
labeldict = img .labeltable .get_labels_as_dict ()
322
- assert_true ( 660700 in labeldict )
323
- assert_equal ( labeldict [660700 ], 'entorhinal' )
324
- assert_equal ( img .labeltable .labels [1 ].key , 2647065 )
325
- assert_equal ( img .labeltable .labels [1 ].red , 0.0980392 )
326
- assert_equal ( img .labeltable .labels [1 ].green , 0.392157 )
327
- assert_equal ( img .labeltable .labels [1 ].blue , 0.156863 )
328
- assert_equal ( img .labeltable .labels [1 ].alpha , 1 )
321
+ assert 660700 in labeldict
322
+ assert labeldict [660700 ] == 'entorhinal'
323
+ assert img .labeltable .labels [1 ].key == 2647065
324
+ assert img .labeltable .labels [1 ].red == 0.0980392
325
+ assert img .labeltable .labels [1 ].green == 0.392157
326
+ assert img .labeltable .labels [1 ].blue == 0.156863
327
+ assert img .labeltable .labels [1 ].alpha == 1
329
328
330
329
331
330
def test_labeltable_deprecations ():
@@ -335,14 +334,14 @@ def test_labeltable_deprecations():
335
334
# Test deprecation
336
335
with clear_and_catch_warnings () as w :
337
336
warnings .filterwarnings ('always' , category = DeprecationWarning )
338
- assert_equal ( lt , img .get_labeltable () )
339
- assert_equal ( len (w ), 1 )
337
+ assert lt == img .get_labeltable ()
338
+ assert len (w ) == 1
340
339
341
340
with clear_and_catch_warnings () as w :
342
341
warnings .filterwarnings ('always' , category = DeprecationWarning )
343
342
img .set_labeltable (lt )
344
- assert_equal ( len (w ), 1 )
345
- assert_equal ( lt , img .labeltable )
343
+ assert len (w ) == 1
344
+ assert lt == img .labeltable
346
345
347
346
348
347
def test_parse_dataarrays ():
@@ -361,8 +360,8 @@ def test_parse_dataarrays():
361
360
with clear_and_catch_warnings () as w :
362
361
warnings .filterwarnings ('once' , category = UserWarning )
363
362
load (fn )
364
- assert_equal ( len (w ), 1 )
365
- assert_equal ( img .numDA , 0 )
363
+ assert len (w ) == 1
364
+ assert img .numDA == 0
366
365
367
366
368
367
def test_parse_deprecated ():
@@ -371,16 +370,16 @@ def test_parse_deprecated():
371
370
with clear_and_catch_warnings () as w :
372
371
warnings .filterwarnings ('always' , category = DeprecationWarning )
373
372
op = Outputter ()
374
- assert_equal ( len (w ), 1 )
373
+ assert len (w ) == 1
375
374
op .initialize () # smoke test--no error.
376
375
377
376
with clear_and_catch_warnings () as w :
378
377
warnings .filterwarnings ('always' , category = DeprecationWarning )
379
- assert_raises (ValueError , parse_gifti_file )
380
- assert_equal ( len (w ), 1 )
378
+ pytest . raises (ValueError , parse_gifti_file )
379
+ assert len (w ) == 1
381
380
382
381
383
382
def test_parse_with_buffersize ():
384
383
for buff_sz in [None , 1 , 2 ** 12 ]:
385
384
img2 = load (DATA_FILE2 , buffer_size = buff_sz )
386
- assert_equal ( img2 .darrays [0 ].data .shape , (143479 , 1 ) )
385
+ assert img2 .darrays [0 ].data .shape == (143479 , 1 )
0 commit comments