Skip to content

Commit 15b4e9d

Browse files
committed
TEST: Test orientations directly
1 parent 0473185 commit 15b4e9d

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

nibabel/tests/test_nifti1.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
from nibabel.spatialimages import HeaderDataError
2929
from nibabel.tmpdirs import InTemporaryDirectory
3030
from ..freesurfer import load as mghload
31-
from ..orientations import axcodes2ornt
31+
from ..orientations import aff2axcodes
3232

3333
from .test_arraywriters import rt_err_estimate, IUINT_TYPES
34+
from .test_orientations import ALL_ORNTS
3435
from .test_helpers import bytesio_filemap, bytesio_round_trip
3536
from .nibabel_data import get_nibabel_data, needs_nibabel_data
3637

@@ -1410,14 +1411,10 @@ def test_reoriented_dim_info(self):
14101411
# Start as RAS
14111412
aff = np.diag([2, 3, 4, 1])
14121413
simg = self.single_class(arr, aff)
1413-
axcodes = ('RAS', 'RPS', # Include a flip
1414-
'RSA', 'RSP', # Include a flip + reorientation
1415-
'ARS',
1416-
'ASR',
1417-
'SRA',
1418-
'SAR')
14191414
for freq, phas, slic in ((0, 1, 2),
14201415
(0, 2, 1),
1416+
(1, 0, 2),
1417+
(2, 0, 1),
14211418
(None, None, None),
14221419
(0, 2, None),
14231420
(0, None, None),
@@ -1428,10 +1425,10 @@ def test_reoriented_dim_info(self):
14281425
fdir = 'RAS'[freq] if freq is not None else None
14291426
pdir = 'RAS'[phas] if phas is not None else None
14301427
sdir = 'RAS'[slic] if slic is not None else None
1431-
for axcode in axcodes:
1432-
ornt = axcodes2ornt(axcode)
1433-
rimg = simg.as_reoriented(ornt)
1434-
dirs = axcode.replace('P', 'A') # Polarity doesn't matter
1428+
for ornt in ALL_ORNTS:
1429+
rimg = simg.as_reoriented(np.array(ornt))
1430+
axcode = aff2axcodes(rimg.affine)
1431+
dirs = ''.join(axcode).replace('P', 'A').replace('I', 'S').replace('L', 'R')
14351432
new_freq, new_phas, new_slic = rimg.header.get_dim_info()
14361433
new_fdir = dirs[new_freq] if new_freq is not None else None
14371434
new_pdir = dirs[new_phas] if new_phas is not None else None

nibabel/tests/test_orientations.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@
8383
OUT_ORNTS = [np.array(ornt) for ornt in OUT_ORNTS]
8484

8585

86+
_LABELS = ['RL', 'AP', 'SI']
87+
ALL_AXCODES = [(_LABELS[i0][j0], _LABELS[i1][j1], _LABELS[i2][j2])
88+
for i0 in range(3) for i1 in range(3) for i2 in range(3)
89+
if i0 != i1 != i2 != i0
90+
for j0 in range(2) for j1 in range(2) for j2 in range(2)]
91+
92+
ALL_ORNTS = [[[i0, j0], [i1, j1], [i2, j2]]
93+
for i0 in range(3) for i1 in range(3) for i2 in range(3)
94+
if i0 != i1 != i2 != i0
95+
for j0 in [1, -1] for j1 in [1, -1] for j2 in [1, -1]]
96+
97+
8698
def same_transform(taff, ornt, shape):
8799
# Applying transformations implied by `ornt` to a made-up array
88100
# ``arr`` of shape `shape`, results in ``t_arr``. When the point
@@ -125,6 +137,10 @@ def test_apply():
125137
apply_orientation,
126138
a,
127139
[[0, 1], [np.nan, np.nan], [2, 1]])
140+
shape = np.array(a.shape)
141+
for ornt in ALL_ORNTS:
142+
t_arr = apply_orientation(a, ornt)
143+
assert_array_equal(a.shape, np.array(t_arr.shape)[np.array(ornt)[:, 0]])
128144

129145

130146
def test_flip_axis():
@@ -282,6 +298,9 @@ def test_ornt2axcodes():
282298
# As do directions not in range
283299
assert_raises(ValueError, ornt2axcodes, [[0, 0]])
284300

301+
for axcodes, ornt in zip(ALL_AXCODES, ALL_ORNTS):
302+
assert_equal(ornt2axcodes(ornt), axcodes)
303+
285304

286305
def test_axcodes2ornt():
287306
# Go from axcodes back to orientations
@@ -340,6 +359,9 @@ def test_axcodes2ornt():
340359
assert_raises(ValueError, axcodes2ornt, 'blD', ('SD', 'BF', 'lD'))
341360
assert_raises(ValueError, axcodes2ornt, 'blD', ('SD', 'SF', 'lD'))
342361

362+
for axcodes, ornt in zip(ALL_AXCODES, ALL_ORNTS):
363+
assert_array_equal(axcodes2ornt(axcodes), ornt)
364+
343365

344366
def test_aff2axcodes():
345367
assert_equal(aff2axcodes(np.eye(4)), tuple('RAS'))

0 commit comments

Comments
 (0)