Skip to content

Commit 3f62445

Browse files
committed
TEST: Add and test version check
1 parent 664f11c commit 3f62445

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

nibabel/freesurfer/mghformat.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from ..arrayproxy import ArrayProxy, reshape_dataobj
2222
from ..keywordonly import kw_only_meth
2323
from ..openers import ImageOpener
24+
from ..batteryrunners import BatteryRunner, Report
2425
from ..wrapstruct import LabeledWrapStruct
2526
from ..deprecated import deprecate_with_version
2627

@@ -120,6 +121,20 @@ def __init__(self,
120121
if check:
121122
self.check_fix()
122123

124+
@staticmethod
125+
def chk_version(hdr, fix=False):
126+
rep = Report()
127+
if hdr['version'] != 1:
128+
rep = Report(HeaderDataError, 40)
129+
rep.problem_msg = 'Unknown MGH format version'
130+
if fix:
131+
hdr['version'] = 1
132+
return hdr, rep
133+
134+
@classmethod
135+
def _get_checks(klass):
136+
return (klass.chk_version,)
137+
123138
@classmethod
124139
def from_header(klass, header=None, check=True):
125140
''' Class method to create MGH header from another MGH header

nibabel/freesurfer/tests/test_mghformat.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from ...spatialimages import HeaderDataError
2222
from ...volumeutils import sys_is_le
2323
from ...wrapstruct import WrapStructError
24+
from ... import imageglobals
2425

2526
from nose.tools import assert_true, assert_false
2627

@@ -32,7 +33,7 @@
3233
from ...testing import data_path
3334

3435
from ...tests import test_spatialimages as tsi
35-
from ...tests.test_wrapstruct import _TestWrapStructBase
36+
from ...tests.test_wrapstruct import _TestLabeledWrapStruct
3637

3738
MGZ_FNAME = os.path.join(data_path, 'test.mgz')
3839

@@ -371,9 +372,16 @@ def check_dtypes(self, expected, actual):
371372
assert_equal(expected.newbyteorder('>'), actual)
372373

373374

374-
class TestMGHHeader(_TestWrapStructBase):
375+
class TestMGHHeader(_TestLabeledWrapStruct):
375376
header_class = MGHHeader
376377

378+
def _set_something_into_hdr(self, hdr):
379+
hdr['dims'] = [4, 3, 2, 1]
380+
381+
def get_bad_bb(self):
382+
return b'\xff' + bytes(self.header_class._hdrdtype.itemsize)
383+
384+
# Update tests to account for big-endian requirement
377385
def test_general_init(self):
378386
hdr = self.header_class()
379387
# binaryblock has length given by header data dtype
@@ -385,10 +393,6 @@ def test_general_init(self):
385393
# effect
386394
hdr = self.header_class(check=False)
387395

388-
def _set_something_into_hdr(self, hdr):
389-
hdr['dims'] = [4, 3, 2, 1]
390-
391-
# Update tests to account for big-endian requirement
392396
def test__eq__(self):
393397
# Test equal and not equal
394398
hdr1 = self.header_class()
@@ -470,3 +474,14 @@ def check_fix(self, *args, **kwargs):
470474
assert_raises(Exception, DC, hdr.binaryblock)
471475
hdr = DC(hdr.binaryblock, check=False)
472476
hdr2 = hdr.as_byteswapped('>')
477+
478+
def test_checks(self):
479+
# Test header checks
480+
hdr_t = self.header_class()
481+
# _dxer just returns the diagnostics as a string
482+
# Default hdr is OK
483+
assert_equal(self._dxer(hdr_t), '')
484+
# Version should be 1
485+
hdr = hdr_t.copy()
486+
hdr['version'] = 2
487+
assert_equal(self._dxer(hdr), 'Unknown MGH format version')

0 commit comments

Comments
 (0)