26
26
27
27
import pytest
28
28
29
- from numpy .testing import (assert_equal , assert_array_equal ,
30
- assert_array_almost_equal , assert_almost_equal ,
31
- assert_raises )
32
- from ...testing import assert_not_equal
29
+ from numpy .testing import (assert_array_equal ,
30
+ assert_array_almost_equal , assert_almost_equal )
33
31
34
- from ...testing import data_path
32
+
33
+ from ...testing_pytest import data_path
35
34
36
35
from ...tests import test_spatialimages as tsi
37
36
from ...tests .test_wrapstruct import _TestLabeledWrapStruct
@@ -68,10 +67,10 @@ def test_read_mgh():
68
67
69
68
# header
70
69
h = mgz .header
71
- assert_equal ( h ['version' ], 1 )
72
- assert_equal ( h ['type' ], 3 )
73
- assert_equal ( h ['dof' ], 0 )
74
- assert_equal ( h ['goodRASFlag' ], 1 )
70
+ assert h ['version' ] == 1
71
+ assert h ['type' ] == 3
72
+ assert h ['dof' ] == 0
73
+ assert h ['goodRASFlag' ] == 1
75
74
assert_array_equal (h ['dims' ], [3 , 4 , 5 , 2 ])
76
75
assert_almost_equal (h ['tr' ], 2.0 )
77
76
assert_almost_equal (h ['flip_angle' ], 0.0 )
@@ -102,10 +101,10 @@ def test_write_mgh():
102
101
# Delete loaded image to allow file deletion by windows
103
102
del mgz
104
103
# header
105
- assert_equal ( h ['version' ], 1 )
106
- assert_equal ( h ['type' ], 3 )
107
- assert_equal ( h ['dof' ], 0 )
108
- assert_equal ( h ['goodRASFlag' ], 1 )
104
+ assert h ['version' ] == 1
105
+ assert h ['type' ] == 3
106
+ assert h ['dof' ] == 0
107
+ assert h ['goodRASFlag' ] == 1
109
108
assert_array_equal (h ['dims' ], [5 , 4 , 3 , 2 ])
110
109
assert_almost_equal (h ['tr' ], 0.0 )
111
110
assert_almost_equal (h ['flip_angle' ], 0.0 )
@@ -132,10 +131,10 @@ def test_write_noaffine_mgh():
132
131
# Delete loaded image to allow file deletion by windows
133
132
del mgz
134
133
# header
135
- assert_equal ( h ['version' ], 1 )
136
- assert_equal ( h ['type' ], 0 ) # uint8 for mgh
137
- assert_equal ( h ['dof' ], 0 )
138
- assert_equal ( h ['goodRASFlag' ], 1 )
134
+ assert h ['version' ] == 1
135
+ assert h ['type' ] == 0 # uint8 for mgh
136
+ assert h ['dof' ] == 0
137
+ assert h ['goodRASFlag' ] == 1
139
138
assert_array_equal (h ['dims' ], [7 , 13 , 3 , 22 ])
140
139
assert_almost_equal (h ['tr' ], 0.0 )
141
140
assert_almost_equal (h ['flip_angle' ], 0.0 )
@@ -158,7 +157,7 @@ def test_set_zooms():
158
157
(1 , 1 , - 1 , 1 ),
159
158
(1 , 1 , 1 , - 1 ),
160
159
(1 , 1 , 1 , 1 , 5 )):
161
- with assert_raises (HeaderDataError ):
160
+ with pytest . raises (HeaderDataError ):
162
161
h .set_zooms (zooms )
163
162
# smoke test for tr=0
164
163
h .set_zooms ((1 , 1 , 1 , 0 ))
@@ -178,7 +177,8 @@ def bad_dtype_mgh():
178
177
179
178
def test_bad_dtype_mgh ():
180
179
# Now test the above function
181
- assert_raises (MGHError , bad_dtype_mgh )
180
+ with pytest .raises (MGHError ):
181
+ bad_dtype_mgh ()
182
182
183
183
184
184
def test_filename_exts ():
@@ -219,14 +219,14 @@ def test_header_updating():
219
219
assert_almost_equal (mgz .affine , exp_aff , 6 )
220
220
assert_almost_equal (hdr .get_affine (), exp_aff , 6 )
221
221
# Test that initial wonky header elements have not changed
222
- assert_equal ( hdr ['delta' ], 1 )
222
+ assert hdr ['delta' ] == 1
223
223
assert_almost_equal (hdr ['Mdc' ].T , exp_aff [:3 , :3 ])
224
224
# Save, reload, same thing
225
225
img_fobj = io .BytesIO ()
226
226
mgz2 = _mgh_rt (mgz , img_fobj )
227
227
hdr2 = mgz2 .header
228
228
assert_almost_equal (hdr2 .get_affine (), exp_aff , 6 )
229
- assert_equal ( hdr2 ['delta' ], 1 )
229
+ assert hdr2 ['delta' ] == 1
230
230
# Change affine, change underlying header info
231
231
exp_aff_d = exp_aff .copy ()
232
232
exp_aff_d [0 , - 1 ] = - 14
@@ -259,17 +259,17 @@ def test_eq():
259
259
# Test headers compare properly
260
260
hdr = MGHHeader ()
261
261
hdr2 = MGHHeader ()
262
- assert_equal ( hdr , hdr2 )
262
+ assert hdr == hdr2
263
263
hdr .set_data_shape ((2 , 3 , 4 ))
264
264
assert (hdr != hdr2 )
265
265
hdr2 .set_data_shape ((2 , 3 , 4 ))
266
- assert_equal ( hdr , hdr2 )
266
+ assert hdr == hdr2
267
267
268
268
269
269
def test_header_slope_inter ():
270
270
# Test placeholder slope / inter method
271
271
hdr = MGHHeader ()
272
- assert_equal ( hdr .get_slope_inter (), (None , None ) )
272
+ assert hdr .get_slope_inter () == (None , None )
273
273
274
274
275
275
def test_mgh_load_fileobj ():
@@ -281,7 +281,7 @@ def test_mgh_load_fileobj():
281
281
# pass the filename to the array proxy, please feel free to change this
282
282
# test.
283
283
img = MGHImage .load (MGZ_FNAME )
284
- assert_equal ( img .dataobj .file_like , MGZ_FNAME )
284
+ assert img .dataobj .file_like == MGZ_FNAME
285
285
# Check fileobj also passed into dataobj
286
286
with ImageOpener (MGZ_FNAME ) as fobj :
287
287
contents = fobj .read ()
@@ -296,7 +296,7 @@ def test_mgh_affine_default():
296
296
hdr = MGHHeader ()
297
297
hdr ['goodRASFlag' ] = 0
298
298
hdr2 = MGHHeader (hdr .binaryblock )
299
- assert_equal ( hdr2 ['goodRASFlag' ], 1 )
299
+ assert hdr2 ['goodRASFlag' ] == 1
300
300
assert_array_equal (hdr ['Mdc' ], hdr2 ['Mdc' ])
301
301
assert_array_equal (hdr ['Pxyz_c' ], hdr2 ['Pxyz_c' ])
302
302
@@ -311,33 +311,33 @@ def test_mgh_set_data_shape():
311
311
assert_array_equal (hdr .get_data_shape (), (5 , 4 , 3 ))
312
312
hdr .set_data_shape ((5 , 4 , 3 , 2 ))
313
313
assert_array_equal (hdr .get_data_shape (), (5 , 4 , 3 , 2 ))
314
- with assert_raises (ValueError ):
314
+ with pytest . raises (ValueError ):
315
315
hdr .set_data_shape ((5 , 4 , 3 , 2 , 1 ))
316
316
317
317
318
318
def test_mghheader_default_structarr ():
319
319
hdr = MGHHeader .default_structarr ()
320
- assert_equal ( hdr ['version' ], 1 )
320
+ assert hdr ['version' ] == 1
321
321
assert_array_equal (hdr ['dims' ], 1 )
322
- assert_equal ( hdr ['type' ], 3 )
323
- assert_equal ( hdr ['dof' ], 0 )
324
- assert_equal ( hdr ['goodRASFlag' ], 1 )
322
+ assert hdr ['type' ] == 3
323
+ assert hdr ['dof' ] == 0
324
+ assert hdr ['goodRASFlag' ] == 1
325
325
assert_array_equal (hdr ['delta' ], 1 )
326
326
assert_array_equal (hdr ['Mdc' ], [[- 1 , 0 , 0 ], [0 , 0 , 1 ], [0 , - 1 , 0 ]])
327
327
assert_array_equal (hdr ['Pxyz_c' ], 0 )
328
- assert_equal ( hdr ['tr' ], 0 )
329
- assert_equal ( hdr ['flip_angle' ], 0 )
330
- assert_equal ( hdr ['te' ], 0 )
331
- assert_equal ( hdr ['ti' ], 0 )
332
- assert_equal ( hdr ['fov' ], 0 )
328
+ assert hdr ['tr' ] == 0
329
+ assert hdr ['flip_angle' ] == 0
330
+ assert hdr ['te' ] == 0
331
+ assert hdr ['ti' ] == 0
332
+ assert hdr ['fov' ] == 0
333
333
334
334
for endianness in (None ,) + BIG_CODES :
335
335
hdr2 = MGHHeader .default_structarr (endianness = endianness )
336
- assert_equal ( hdr2 , hdr )
337
- assert_equal ( hdr2 .newbyteorder ('>' ), hdr )
336
+ assert hdr2 == hdr
337
+ assert hdr2 .newbyteorder ('>' ) == hdr
338
338
339
339
for endianness in LITTLE_CODES :
340
- with assert_raises (ValueError ):
340
+ with pytest . raises (ValueError ):
341
341
MGHHeader .default_structarr (endianness = endianness )
342
342
343
343
@@ -352,17 +352,17 @@ def test_deprecated_fields():
352
352
353
353
hdr ['mrparams' ] = [1 , 2 , 3 , 4 ]
354
354
assert_array_almost_equal (hdr ['mrparams' ], [1 , 2 , 3 , 4 ])
355
- assert_equal ( hdr ['tr' ], 1 )
356
- assert_equal ( hdr ['flip_angle' ], 2 )
357
- assert_equal ( hdr ['te' ], 3 )
358
- assert_equal ( hdr ['ti' ], 4 )
359
- assert_equal ( hdr ['fov' ], 0 )
355
+ assert hdr ['tr' ] == 1
356
+ assert hdr ['flip_angle' ] == 2
357
+ assert hdr ['te' ] == 3
358
+ assert hdr ['ti' ] == 4
359
+ assert hdr ['fov' ] == 0
360
360
assert_array_almost_equal (hdr_data ['mrparams' ], [1 , 2 , 3 , 4 ])
361
- assert_equal ( hdr_data ['tr' ], 1 )
362
- assert_equal ( hdr_data ['flip_angle' ], 2 )
363
- assert_equal ( hdr_data ['te' ], 3 )
364
- assert_equal ( hdr_data ['ti' ], 4 )
365
- assert_equal ( hdr_data ['fov' ], 0 )
361
+ assert hdr_data ['tr' ] == 1
362
+ assert hdr_data ['flip_angle' ] == 2
363
+ assert hdr_data ['te' ] == 3
364
+ assert hdr_data ['ti' ] == 4
365
+ assert hdr_data ['fov' ] == 0
366
366
367
367
hdr ['tr' ] = 5
368
368
hdr ['flip_angle' ] = 6
@@ -389,7 +389,7 @@ def check_dtypes(self, expected, actual):
389
389
# Some images will want dtypes to be equal including endianness,
390
390
# others may only require the same type
391
391
# MGH requires the actual to be a big endian version of expected
392
- assert_equal ( expected .newbyteorder ('>' ), actual )
392
+ assert expected .newbyteorder ('>' ) == actual
393
393
394
394
395
395
class TestMGHHeader (_TestLabeledWrapStruct ):
@@ -406,9 +406,9 @@ def test_general_init(self):
406
406
hdr = self .header_class ()
407
407
# binaryblock has length given by header data dtype
408
408
binblock = hdr .binaryblock
409
- assert_equal ( len (binblock ), hdr .structarr .dtype .itemsize )
409
+ assert len (binblock ) == hdr .structarr .dtype .itemsize
410
410
# Endianness will always be big, and cannot be set
411
- assert_equal ( hdr .endianness , '>' )
411
+ assert hdr .endianness == '>'
412
412
# You can also pass in a check flag, without data this has no
413
413
# effect
414
414
hdr = self .header_class (check = False )
@@ -417,15 +417,15 @@ def test__eq__(self):
417
417
# Test equal and not equal
418
418
hdr1 = self .header_class ()
419
419
hdr2 = self .header_class ()
420
- assert_equal ( hdr1 , hdr2 )
420
+ assert hdr1 == hdr2
421
421
self ._set_something_into_hdr (hdr1 )
422
- assert_not_equal ( hdr1 , hdr2 )
422
+ assert hdr1 != hdr2
423
423
self ._set_something_into_hdr (hdr2 )
424
- assert_equal ( hdr1 , hdr2 )
424
+ assert hdr1 == hdr2
425
425
# REMOVED as_byteswapped() test
426
426
# Check comparing to funny thing says no
427
- assert_not_equal ( hdr1 , None )
428
- assert_not_equal ( hdr1 , 1 )
427
+ assert hdr1 != None
428
+ assert hdr1 != 1
429
429
430
430
def test_to_from_fileobj (self ):
431
431
# Successful write using write_to
@@ -434,64 +434,68 @@ def test_to_from_fileobj(self):
434
434
hdr .write_to (str_io )
435
435
str_io .seek (0 )
436
436
hdr2 = self .header_class .from_fileobj (str_io )
437
- assert_equal ( hdr2 .endianness , '>' )
438
- assert_equal ( hdr2 .binaryblock , hdr .binaryblock )
437
+ assert hdr2 .endianness == '>'
438
+ assert hdr2 .binaryblock == hdr .binaryblock
439
439
440
440
def test_endian_guess (self ):
441
441
# Check guesses of endian
442
442
eh = self .header_class ()
443
- assert_equal ( eh .endianness , '>' )
444
- assert_equal ( self .header_class .guessed_endian (eh ), '>' )
443
+ assert eh .endianness == '>'
444
+ assert self .header_class .guessed_endian (eh ) == '>'
445
445
446
446
def test_bytes (self ):
447
447
# Test get of bytes
448
448
hdr1 = self .header_class ()
449
449
bb = hdr1 .binaryblock
450
450
hdr2 = self .header_class (hdr1 .binaryblock )
451
- assert_equal ( hdr1 , hdr2 )
452
- assert_equal ( hdr1 .binaryblock , hdr2 .binaryblock )
451
+ assert hdr1 == hdr2
452
+ assert hdr1 .binaryblock == hdr2 .binaryblock
453
453
# Do a set into the header, and try again. The specifics of 'setting
454
454
# something' will depend on the nature of the bytes object
455
455
self ._set_something_into_hdr (hdr1 )
456
456
hdr2 = self .header_class (hdr1 .binaryblock )
457
- assert_equal ( hdr1 , hdr2 )
458
- assert_equal ( hdr1 .binaryblock , hdr2 .binaryblock )
457
+ assert hdr1 == hdr2
458
+ assert hdr1 .binaryblock == hdr2 .binaryblock
459
459
# Short binaryblocks give errors (here set through init)
460
460
# Long binaryblocks are truncated
461
- assert_raises (WrapStructError ,
462
- self .header_class ,
463
- bb [: self . header_class . _hdrdtype . itemsize - 1 ])
461
+ with pytest . raises (WrapStructError ):
462
+ self .header_class ( bb [: self . header_class . _hdrdtype . itemsize - 1 ])
463
+
464
464
# Checking set to true by default, and prevents nonsense being
465
465
# set into the header.
466
466
bb_bad = self .get_bad_bb ()
467
467
if bb_bad is None :
468
468
return
469
469
with imageglobals .LoggingOutputSuppressor ():
470
- assert_raises (HeaderDataError , self .header_class , bb_bad )
470
+ with pytest .raises (HeaderDataError ):
471
+ self .header_class (bb_bad )
472
+
471
473
# now slips past without check
472
474
_ = self .header_class (bb_bad , check = False )
473
475
474
476
def test_as_byteswapped (self ):
475
477
# Check byte swapping
476
478
hdr = self .header_class ()
477
- assert_equal ( hdr .endianness , '>' )
479
+ assert hdr .endianness == '>'
478
480
# same code just returns a copy
479
481
for endianness in BIG_CODES :
480
482
hdr2 = hdr .as_byteswapped (endianness )
481
483
assert (hdr2 is not hdr )
482
- assert_equal ( hdr2 , hdr )
484
+ assert hdr2 == hdr
483
485
484
486
# Different code raises error
485
487
for endianness in (None ,) + LITTLE_CODES :
486
- with assert_raises (ValueError ):
488
+ with pytest . raises (ValueError ):
487
489
hdr .as_byteswapped (endianness )
488
490
# Note that contents is not rechecked on swap / copy
489
491
class DC (self .header_class ):
490
492
def check_fix (self , * args , ** kwargs ):
491
493
raise Exception
492
494
493
495
# Assumes check=True default
494
- assert_raises (Exception , DC , hdr .binaryblock )
496
+ with pytest .raises (Exception ):
497
+ DC (hdr .binaryblock )
498
+
495
499
hdr = DC (hdr .binaryblock , check = False )
496
500
hdr2 = hdr .as_byteswapped ('>' )
497
501
@@ -500,8 +504,8 @@ def test_checks(self):
500
504
hdr_t = self .header_class ()
501
505
# _dxer just returns the diagnostics as a string
502
506
# Default hdr is OK
503
- assert_equal ( self ._dxer (hdr_t ), '' )
507
+ assert self ._dxer (hdr_t ) == ''
504
508
# Version should be 1
505
509
hdr = hdr_t .copy ()
506
510
hdr ['version' ] = 2
507
- assert_equal ( self ._dxer (hdr ), 'Unknown MGH format version' )
511
+ assert self ._dxer (hdr ) == 'Unknown MGH format version'
0 commit comments