Skip to content

Commit f99d926

Browse files
committed
Changed test_mghformat.py to pytest
1 parent 441e4fe commit f99d926

File tree

1 file changed

+79
-75
lines changed

1 file changed

+79
-75
lines changed

nibabel/freesurfer/tests/test_mghformat.py

Lines changed: 79 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@
2626

2727
import pytest
2828

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)
3331

34-
from ...testing import data_path
32+
33+
from ...testing_pytest import data_path
3534

3635
from ...tests import test_spatialimages as tsi
3736
from ...tests.test_wrapstruct import _TestLabeledWrapStruct
@@ -68,10 +67,10 @@ def test_read_mgh():
6867

6968
# header
7069
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
7574
assert_array_equal(h['dims'], [3, 4, 5, 2])
7675
assert_almost_equal(h['tr'], 2.0)
7776
assert_almost_equal(h['flip_angle'], 0.0)
@@ -102,10 +101,10 @@ def test_write_mgh():
102101
# Delete loaded image to allow file deletion by windows
103102
del mgz
104103
# 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
109108
assert_array_equal(h['dims'], [5, 4, 3, 2])
110109
assert_almost_equal(h['tr'], 0.0)
111110
assert_almost_equal(h['flip_angle'], 0.0)
@@ -132,10 +131,10 @@ def test_write_noaffine_mgh():
132131
# Delete loaded image to allow file deletion by windows
133132
del mgz
134133
# 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
139138
assert_array_equal(h['dims'], [7, 13, 3, 22])
140139
assert_almost_equal(h['tr'], 0.0)
141140
assert_almost_equal(h['flip_angle'], 0.0)
@@ -158,7 +157,7 @@ def test_set_zooms():
158157
(1, 1, -1, 1),
159158
(1, 1, 1, -1),
160159
(1, 1, 1, 1, 5)):
161-
with assert_raises(HeaderDataError):
160+
with pytest.raises(HeaderDataError):
162161
h.set_zooms(zooms)
163162
# smoke test for tr=0
164163
h.set_zooms((1, 1, 1, 0))
@@ -178,7 +177,8 @@ def bad_dtype_mgh():
178177

179178
def test_bad_dtype_mgh():
180179
# Now test the above function
181-
assert_raises(MGHError, bad_dtype_mgh)
180+
with pytest.raises(MGHError):
181+
bad_dtype_mgh()
182182

183183

184184
def test_filename_exts():
@@ -219,14 +219,14 @@ def test_header_updating():
219219
assert_almost_equal(mgz.affine, exp_aff, 6)
220220
assert_almost_equal(hdr.get_affine(), exp_aff, 6)
221221
# Test that initial wonky header elements have not changed
222-
assert_equal(hdr['delta'], 1)
222+
assert hdr['delta'] == 1
223223
assert_almost_equal(hdr['Mdc'].T, exp_aff[:3, :3])
224224
# Save, reload, same thing
225225
img_fobj = io.BytesIO()
226226
mgz2 = _mgh_rt(mgz, img_fobj)
227227
hdr2 = mgz2.header
228228
assert_almost_equal(hdr2.get_affine(), exp_aff, 6)
229-
assert_equal(hdr2['delta'], 1)
229+
assert hdr2['delta'] == 1
230230
# Change affine, change underlying header info
231231
exp_aff_d = exp_aff.copy()
232232
exp_aff_d[0, -1] = -14
@@ -259,17 +259,17 @@ def test_eq():
259259
# Test headers compare properly
260260
hdr = MGHHeader()
261261
hdr2 = MGHHeader()
262-
assert_equal(hdr, hdr2)
262+
assert hdr == hdr2
263263
hdr.set_data_shape((2, 3, 4))
264264
assert(hdr != hdr2)
265265
hdr2.set_data_shape((2, 3, 4))
266-
assert_equal(hdr, hdr2)
266+
assert hdr == hdr2
267267

268268

269269
def test_header_slope_inter():
270270
# Test placeholder slope / inter method
271271
hdr = MGHHeader()
272-
assert_equal(hdr.get_slope_inter(), (None, None))
272+
assert hdr.get_slope_inter() == (None, None)
273273

274274

275275
def test_mgh_load_fileobj():
@@ -281,7 +281,7 @@ def test_mgh_load_fileobj():
281281
# pass the filename to the array proxy, please feel free to change this
282282
# test.
283283
img = MGHImage.load(MGZ_FNAME)
284-
assert_equal(img.dataobj.file_like, MGZ_FNAME)
284+
assert img.dataobj.file_like == MGZ_FNAME
285285
# Check fileobj also passed into dataobj
286286
with ImageOpener(MGZ_FNAME) as fobj:
287287
contents = fobj.read()
@@ -296,7 +296,7 @@ def test_mgh_affine_default():
296296
hdr = MGHHeader()
297297
hdr['goodRASFlag'] = 0
298298
hdr2 = MGHHeader(hdr.binaryblock)
299-
assert_equal(hdr2['goodRASFlag'], 1)
299+
assert hdr2['goodRASFlag'] == 1
300300
assert_array_equal(hdr['Mdc'], hdr2['Mdc'])
301301
assert_array_equal(hdr['Pxyz_c'], hdr2['Pxyz_c'])
302302

@@ -311,33 +311,33 @@ def test_mgh_set_data_shape():
311311
assert_array_equal(hdr.get_data_shape(), (5, 4, 3))
312312
hdr.set_data_shape((5, 4, 3, 2))
313313
assert_array_equal(hdr.get_data_shape(), (5, 4, 3, 2))
314-
with assert_raises(ValueError):
314+
with pytest.raises(ValueError):
315315
hdr.set_data_shape((5, 4, 3, 2, 1))
316316

317317

318318
def test_mghheader_default_structarr():
319319
hdr = MGHHeader.default_structarr()
320-
assert_equal(hdr['version'], 1)
320+
assert hdr['version'] == 1
321321
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
325325
assert_array_equal(hdr['delta'], 1)
326326
assert_array_equal(hdr['Mdc'], [[-1, 0, 0], [0, 0, 1], [0, -1, 0]])
327327
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
333333

334334
for endianness in (None,) + BIG_CODES:
335335
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
338338

339339
for endianness in LITTLE_CODES:
340-
with assert_raises(ValueError):
340+
with pytest.raises(ValueError):
341341
MGHHeader.default_structarr(endianness=endianness)
342342

343343

@@ -352,17 +352,17 @@ def test_deprecated_fields():
352352

353353
hdr['mrparams'] = [1, 2, 3, 4]
354354
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
360360
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
366366

367367
hdr['tr'] = 5
368368
hdr['flip_angle'] = 6
@@ -389,7 +389,7 @@ def check_dtypes(self, expected, actual):
389389
# Some images will want dtypes to be equal including endianness,
390390
# others may only require the same type
391391
# MGH requires the actual to be a big endian version of expected
392-
assert_equal(expected.newbyteorder('>'), actual)
392+
assert expected.newbyteorder('>') == actual
393393

394394

395395
class TestMGHHeader(_TestLabeledWrapStruct):
@@ -406,9 +406,9 @@ def test_general_init(self):
406406
hdr = self.header_class()
407407
# binaryblock has length given by header data dtype
408408
binblock = hdr.binaryblock
409-
assert_equal(len(binblock), hdr.structarr.dtype.itemsize)
409+
assert len(binblock) == hdr.structarr.dtype.itemsize
410410
# Endianness will always be big, and cannot be set
411-
assert_equal(hdr.endianness, '>')
411+
assert hdr.endianness == '>'
412412
# You can also pass in a check flag, without data this has no
413413
# effect
414414
hdr = self.header_class(check=False)
@@ -417,15 +417,15 @@ def test__eq__(self):
417417
# Test equal and not equal
418418
hdr1 = self.header_class()
419419
hdr2 = self.header_class()
420-
assert_equal(hdr1, hdr2)
420+
assert hdr1 == hdr2
421421
self._set_something_into_hdr(hdr1)
422-
assert_not_equal(hdr1, hdr2)
422+
assert hdr1 != hdr2
423423
self._set_something_into_hdr(hdr2)
424-
assert_equal(hdr1, hdr2)
424+
assert hdr1 == hdr2
425425
# REMOVED as_byteswapped() test
426426
# 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
429429

430430
def test_to_from_fileobj(self):
431431
# Successful write using write_to
@@ -434,64 +434,68 @@ def test_to_from_fileobj(self):
434434
hdr.write_to(str_io)
435435
str_io.seek(0)
436436
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
439439

440440
def test_endian_guess(self):
441441
# Check guesses of endian
442442
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) == '>'
445445

446446
def test_bytes(self):
447447
# Test get of bytes
448448
hdr1 = self.header_class()
449449
bb = hdr1.binaryblock
450450
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
453453
# Do a set into the header, and try again. The specifics of 'setting
454454
# something' will depend on the nature of the bytes object
455455
self._set_something_into_hdr(hdr1)
456456
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
459459
# Short binaryblocks give errors (here set through init)
460460
# 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+
464464
# Checking set to true by default, and prevents nonsense being
465465
# set into the header.
466466
bb_bad = self.get_bad_bb()
467467
if bb_bad is None:
468468
return
469469
with imageglobals.LoggingOutputSuppressor():
470-
assert_raises(HeaderDataError, self.header_class, bb_bad)
470+
with pytest.raises(HeaderDataError):
471+
self.header_class(bb_bad)
472+
471473
# now slips past without check
472474
_ = self.header_class(bb_bad, check=False)
473475

474476
def test_as_byteswapped(self):
475477
# Check byte swapping
476478
hdr = self.header_class()
477-
assert_equal(hdr.endianness, '>')
479+
assert hdr.endianness == '>'
478480
# same code just returns a copy
479481
for endianness in BIG_CODES:
480482
hdr2 = hdr.as_byteswapped(endianness)
481483
assert(hdr2 is not hdr)
482-
assert_equal(hdr2, hdr)
484+
assert hdr2 == hdr
483485

484486
# Different code raises error
485487
for endianness in (None,) + LITTLE_CODES:
486-
with assert_raises(ValueError):
488+
with pytest.raises(ValueError):
487489
hdr.as_byteswapped(endianness)
488490
# Note that contents is not rechecked on swap / copy
489491
class DC(self.header_class):
490492
def check_fix(self, *args, **kwargs):
491493
raise Exception
492494

493495
# Assumes check=True default
494-
assert_raises(Exception, DC, hdr.binaryblock)
496+
with pytest.raises(Exception):
497+
DC(hdr.binaryblock)
498+
495499
hdr = DC(hdr.binaryblock, check=False)
496500
hdr2 = hdr.as_byteswapped('>')
497501

@@ -500,8 +504,8 @@ def test_checks(self):
500504
hdr_t = self.header_class()
501505
# _dxer just returns the diagnostics as a string
502506
# Default hdr is OK
503-
assert_equal(self._dxer(hdr_t), '')
507+
assert self._dxer(hdr_t) == ''
504508
# Version should be 1
505509
hdr = hdr_t.copy()
506510
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

Comments
 (0)