Skip to content

Commit

Permalink
TEST: Test get/set_norm_zooms
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Dec 21, 2017
1 parent 9b8a7fa commit bddf15c
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
48 changes: 48 additions & 0 deletions nibabel/freesurfer/tests/test_mghformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,54 @@ def check_dtypes(self, expected, actual):
# MGH requires the actual to be a big endian version of expected
assert_equal(expected.newbyteorder('>'), actual)

def test_norm_zooms_edge_cases(self):
img_klass = self.image_class
aff = np.eye(4)
arr = np.arange(120, dtype=np.int16).reshape((2, 3, 4, 5))
img = img_klass(arr, aff)

assert_array_almost_equal(img.header.get_zooms(),
(1, 1, 1, 0))
assert_array_almost_equal(img.header.get_norm_zooms(),
(1, 1, 1, 0))

img.header.set_zooms((1, 1, 1, 2000))
assert_array_almost_equal(img.header.get_zooms(),
(1, 1, 1, 2000))
assert_array_almost_equal(img.header.get_norm_zooms(),
(1, 1, 1, 2))

img.header.set_norm_zooms((2, 2, 2, 3))
assert_array_almost_equal(img.header.get_zooms(),
(2, 2, 2, 3000))
assert_array_almost_equal(img.header.get_norm_zooms(),
(2, 2, 2, 3))

# It's legal to set zooms for spatial dimensions only
img.header.set_norm_zooms((3, 3, 3))
assert_array_almost_equal(img.header.get_zooms(),
(3, 3, 3, 3000))
assert_array_almost_equal(img.header.get_norm_zooms(),
(3, 3, 3, 3))

arr = np.arange(24, dtype=np.int16).reshape((2, 3, 4))
img = img_klass(arr, aff)

assert_array_almost_equal(img.header.get_zooms(), (1, 1, 1))
assert_array_almost_equal(img.header.get_norm_zooms(), (1, 1, 1))

img.header.set_zooms((2, 2, 2))
assert_array_almost_equal(img.header.get_zooms(), (2, 2, 2))
assert_array_almost_equal(img.header.get_norm_zooms(), (2, 2, 2))

img.header.set_norm_zooms((3, 3, 3))
assert_array_almost_equal(img.header.get_zooms(), (3, 3, 3))
assert_array_almost_equal(img.header.get_norm_zooms(), (3, 3, 3))

# Cannot set TR as zoom for 3D image
assert_raises(HeaderDataError, img.header.set_zooms, (4, 4, 4, 5))
assert_raises(HeaderDataError, img.header.set_norm_zooms, (4, 4, 4, 5))


class TestMGHHeader(_TestLabeledWrapStruct):
header_class = MGHHeader
Expand Down
49 changes: 49 additions & 0 deletions nibabel/tests/test_nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,55 @@ def test_write_scaling(self):
with np.errstate(invalid='ignore'):
self._check_write_scaling(slope, inter, e_slope, e_inter)

def test_norm_zooms_edge_cases(self):
img_klass = self.image_class
arr = np.arange(120, dtype=np.int16).reshape((2, 3, 4, 5))
aff = np.eye(4)
img = img_klass(arr, aff)

# Unknown units = 2 warnings
with warnings.catch_warnings(record=True) as warns:
assert_array_almost_equal(img.header.get_norm_zooms(),
(1, 1, 1, 1))
assert_equal(len(warns), 2)
assert_raises(ValueError, img.header.get_norm_zooms, True)

img.header.set_xyzt_units(xyz='meter')
with warnings.catch_warnings(record=True) as warns:
assert_array_almost_equal(img.header.get_norm_zooms(),
(1000, 1000, 1000, 1))
assert_equal(len(warns), 1)
assert_raises(ValueError, img.header.get_norm_zooms, True)

img.header.set_xyzt_units(xyz='mm', t='sec')
assert_array_almost_equal(img.header.get_norm_zooms(),
(1, 1, 1, 1))
img.header.set_xyzt_units(xyz='micron', t='sec')
assert_array_almost_equal(img.header.get_norm_zooms(),
(0.001, 0.001, 0.001, 1))

img.header.set_xyzt_units(t='sec')
with warnings.catch_warnings(record=True) as warns:
assert_array_equal(img.header.get_norm_zooms(), (1, 1, 1, 1))
assert_equal(len(warns), 1)
assert_raises(ValueError, img.header.get_norm_zooms, True)

img.header.set_xyzt_units(xyz='mm', t='msec')
assert_array_almost_equal(img.header.get_norm_zooms(),
(1, 1, 1, 0.001))

img.header.set_xyzt_units(xyz='mm', t='usec')
assert_array_almost_equal(img.header.get_norm_zooms(),
(1, 1, 1, 0.000001))

# Verify `set_norm_zooms` resets units
img.header.set_xyzt_units(xyz='meter', t='usec')
assert_equal(img.header.get_xyzt_units(), ('meter', 'usec'))
img.header.set_norm_zooms((2, 2, 2, 2.5))
assert_array_almost_equal(img.header.get_norm_zooms(), (2, 2, 2, 2.5))
assert_array_almost_equal(img.header.get_zooms(), (2, 2, 2, 2.5))
assert_equal(img.header.get_xyzt_units(), ('mm', 'sec'))


class TestNifti1Image(TestNifti1Pair):
# Run analyze-flavor spatialimage tests
Expand Down
22 changes: 22 additions & 0 deletions nibabel/tests/test_spatialimages.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,28 @@ def test_get_data(self):
assert_false(rt_img.get_data() is out_data)
assert_array_equal(rt_img.get_data(), in_data)

def test_norm_zooms(self):
''' Should be true for all images '''
img_klass = self.image_class
arr = np.arange(120, dtype=np.int16).reshape((2, 3, 4, 5))
aff = np.eye(4)
img = img_klass(arr, aff)
img.header.set_norm_zooms((2, 2, 2, 2.5))
assert_array_equal(img.header.get_norm_zooms(), (2, 2, 2, 2.5))

def test_norm_zooms_edge_cases(self):
''' Override for classes where *_norm_zooms != *_zooms '''
img_klass = self.image_class
arr = np.arange(120, dtype=np.int16).reshape((2, 3, 4, 5))
aff = np.eye(4)
img = img_klass(arr, aff)
img.header.set_zooms((2, 2, 2, 2.5))
assert_array_equal(img.header.get_zooms(), (2, 2, 2, 2.5))
assert_array_equal(img.header.get_norm_zooms(), (2, 2, 2, 2.5))
img.header.set_norm_zooms((2, 2, 2, 2.5))
assert_array_equal(img.header.get_zooms(), (2, 2, 2, 2.5))
assert_array_equal(img.header.get_norm_zooms(), (2, 2, 2, 2.5))

def test_api_deprecations(self):

class FakeImage(self.image_class):
Expand Down

0 comments on commit bddf15c

Please sign in to comment.