Skip to content

Commit 798fc4e

Browse files
committed
TEST: Add units specification to tests
1 parent 096da88 commit 798fc4e

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

nibabel/analyze.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def from_header(klass, header=None, check=True):
399399
f"but output header {klass} does not support it")
400400
obj.set_data_dtype(header.get_data_dtype())
401401
obj.set_data_shape(header.get_data_shape())
402-
obj.set_zooms(header.get_zooms())
402+
obj.set_zooms(header.get_zooms(units='raw'))
403403
if check:
404404
obj.check_fix()
405405
return obj

nibabel/nifti1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ def get_data_shape(self):
784784
785785
Expanding number of dimensions gets default zooms
786786
787-
>>> hdr.get_zooms()
787+
>>> hdr.get_zooms(units='canonical')
788788
(1.0, 1.0, 1.0)
789789
790790
Notes

nibabel/tests/test_analyze.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def test_general_init(self):
9191
assert_array_equal(np.diag(hdr.get_base_affine()),
9292
[-1, 1, 1, 1])
9393
# But zooms only go with number of dimensions
94-
assert hdr.get_zooms() == (1.0,)
94+
assert hdr.get_zooms(units='raw') == (1.0,)
95+
assert hdr.get_zooms(units='canonical') == (1.0,)
9596

9697
def test_header_size(self):
9798
assert self.header_class.template_dtype.itemsize == self.sizeof_hdr
@@ -437,7 +438,8 @@ def test_data_shape_zooms_affine(self):
437438
else:
438439
assert hdr.get_data_shape() == (0,)
439440
# Default zoom - for 3D - is 1(())
440-
assert hdr.get_zooms() == (1,) * L
441+
assert hdr.get_zooms(units='raw') == (1,) * L
442+
assert hdr.get_zooms(units='canonical') == (1,) * L
441443
# errors if zooms do not match shape
442444
if len(shape):
443445
with pytest.raises(HeaderDataError):
@@ -455,11 +457,14 @@ def test_data_shape_zooms_affine(self):
455457
hdr = self.header_class()
456458
hdr.set_data_shape((1, 2, 3))
457459
hdr.set_zooms((4, 5, 6))
458-
assert_array_equal(hdr.get_zooms(), (4, 5, 6))
460+
assert_array_equal(hdr.get_zooms(units='raw'), (4, 5, 6))
461+
assert_array_equal(hdr.get_zooms(units='canonical'), (4, 5, 6))
459462
hdr.set_data_shape((1, 2))
460-
assert_array_equal(hdr.get_zooms(), (4, 5))
463+
assert_array_equal(hdr.get_zooms(units='raw'), (4, 5))
464+
assert_array_equal(hdr.get_zooms(units='canonical'), (4, 5))
461465
hdr.set_data_shape((1, 2, 3))
462-
assert_array_equal(hdr.get_zooms(), (4, 5, 1))
466+
assert_array_equal(hdr.get_zooms(units='raw'), (4, 5, 1))
467+
assert_array_equal(hdr.get_zooms(units='canonical'), (4, 5, 1))
463468
# Setting zooms changes affine
464469
assert_array_equal(np.diag(hdr.get_base_affine()),
465470
[-4, 5, 1, 1])
@@ -529,12 +534,13 @@ def get_data_dtype(self): return np.dtype('i2')
529534

530535
def get_data_shape(self): return (5, 4, 3)
531536

532-
def get_zooms(self): return (10.0, 9.0, 8.0)
537+
def get_zooms(self, units=None, raise_unknown=None): return (10.0, 9.0, 8.0)
533538
converted = klass.from_header(C())
534539
assert isinstance(converted, klass)
535540
assert converted.get_data_dtype() == np.dtype('i2')
536541
assert converted.get_data_shape() == (5, 4, 3)
537-
assert converted.get_zooms() == (10.0, 9.0, 8.0)
542+
assert converted.get_zooms(units='raw') == (10.0, 9.0, 8.0)
543+
assert converted.get_zooms(units='canonical') == (10.0, 9.0, 8.0)
538544

539545
def test_base_affine(self):
540546
klass = self.header_class
@@ -640,7 +646,7 @@ def get_data_shape(self):
640646

641647
class H4(H3):
642648

643-
def get_zooms(self):
649+
def get_zooms(self, units=None, raise_unknown=None):
644650
return 4., 5., 6.
645651
exp_hdr = klass()
646652
exp_hdr.set_data_dtype(np.dtype('u1'))

nibabel/tests/test_nifti1.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -881,11 +881,11 @@ def test_set_qform(self):
881881
img.set_qform(new_affine, 1, update_affine=False)
882882
assert_array_almost_equal(img.affine, aff_affine)
883883
# Clear qform using None, zooms unchanged
884-
assert_array_almost_equal(hdr.get_zooms(), [1.1, 1.1, 1.1])
884+
assert_array_almost_equal(hdr.get_zooms(units='raw'), [1.1, 1.1, 1.1])
885885
img.set_qform(None)
886886
qaff, code = img.get_qform(coded=True)
887887
assert (qaff, code) == (None, 0)
888-
assert_array_almost_equal(hdr.get_zooms(), [1.1, 1.1, 1.1])
888+
assert_array_almost_equal(hdr.get_zooms(units='raw'), [1.1, 1.1, 1.1])
889889
# Best affine similarly
890890
assert_array_almost_equal(img.affine, hdr.get_best_affine())
891891
# If sform is not set, qform should update affine
@@ -942,9 +942,9 @@ def test_set_sform(self):
942942
assert_array_almost_equal(img.affine, aff_affine)
943943
# zooms do not get updated when qform is 0
944944
assert_array_almost_equal(img.get_qform(), orig_aff)
945-
assert_array_almost_equal(hdr.get_zooms(), [2.2, 3.3, 4.3])
945+
assert_array_almost_equal(hdr.get_zooms(units='raw'), [2.2, 3.3, 4.3])
946946
img.set_qform(None)
947-
assert_array_almost_equal(hdr.get_zooms(), [2.2, 3.3, 4.3])
947+
assert_array_almost_equal(hdr.get_zooms(units='raw'), [2.2, 3.3, 4.3])
948948
# Set sform using new_affine when qform is set
949949
img.set_qform(qform_affine, 1)
950950
img.set_sform(new_affine, 1)
@@ -953,7 +953,7 @@ def test_set_sform(self):
953953
assert_array_almost_equal(saff, new_affine)
954954
assert_array_almost_equal(img.affine, new_affine)
955955
# zooms follow qform
956-
assert_array_almost_equal(hdr.get_zooms(), [1.2, 1.2, 1.2])
956+
assert_array_almost_equal(hdr.get_zooms(units='raw'), [1.2, 1.2, 1.2])
957957
# Clear sform using None, best_affine should fall back on qform
958958
img.set_sform(None)
959959
assert hdr['sform_code'] == 0
@@ -1042,15 +1042,15 @@ def test_load_pixdims(self):
10421042
# Check qform, sform, pixdims are the same
10431043
assert_array_equal(img_hdr.get_qform(), qaff)
10441044
assert_array_equal(img_hdr.get_sform(), saff)
1045-
assert_array_equal(img_hdr.get_zooms(), [2, 3, 4])
1045+
assert_array_equal(img_hdr.get_zooms(units='raw'), [2, 3, 4])
10461046
# Save to stringio
10471047
re_simg = bytesio_round_trip(simg)
10481048
assert_array_equal(re_simg.get_fdata(), arr)
10491049
# Check qform, sform, pixdims are the same
10501050
rimg_hdr = re_simg.header
10511051
assert_array_equal(rimg_hdr.get_qform(), qaff)
10521052
assert_array_equal(rimg_hdr.get_sform(), saff)
1053-
assert_array_equal(rimg_hdr.get_zooms(), [2, 3, 4])
1053+
assert_array_equal(rimg_hdr.get_zooms(units='raw'), [2, 3, 4])
10541054

10551055
def test_affines_init(self):
10561056
# Test we are doing vaguely spec-related qform things. The 'spec' here
@@ -1064,20 +1064,20 @@ def test_affines_init(self):
10641064
hdr = img.header
10651065
assert hdr['qform_code'] == 0
10661066
assert hdr['sform_code'] == 2
1067-
assert_array_equal(hdr.get_zooms(), [2, 3, 4])
1067+
assert_array_equal(hdr.get_zooms(units='raw'), [2, 3, 4])
10681068
# This is also true for affines with header passed
10691069
qaff = np.diag([3, 4, 5, 1])
10701070
saff = np.diag([6, 7, 8, 1])
10711071
hdr.set_qform(qaff, code='scanner')
10721072
hdr.set_sform(saff, code='talairach')
1073-
assert_array_equal(hdr.get_zooms(), [3, 4, 5])
1073+
assert_array_equal(hdr.get_zooms(units='raw'), [3, 4, 5])
10741074
img = IC(arr, aff, hdr)
10751075
new_hdr = img.header
10761076
# Again affine is sort of anonymous space
10771077
assert new_hdr['qform_code'] == 0
10781078
assert new_hdr['sform_code'] == 2
10791079
assert_array_equal(new_hdr.get_sform(), aff)
1080-
assert_array_equal(new_hdr.get_zooms(), [2, 3, 4])
1080+
assert_array_equal(new_hdr.get_zooms(units='raw'), [2, 3, 4])
10811081
# But if no affine passed, codes and matrices stay the same
10821082
img = IC(arr, None, hdr)
10831083
new_hdr = img.header
@@ -1086,7 +1086,7 @@ def test_affines_init(self):
10861086
assert new_hdr['sform_code'] == 3 # Still talairach
10871087
assert_array_equal(new_hdr.get_sform(), saff)
10881088
# Pixdims as in the original header
1089-
assert_array_equal(new_hdr.get_zooms(), [3, 4, 5])
1089+
assert_array_equal(new_hdr.get_zooms(units='raw'), [3, 4, 5])
10901090

10911091
def test_read_no_extensions(self):
10921092
IC = self.image_class

0 commit comments

Comments
 (0)