Skip to content

Commit e625347

Browse files
committed
TST: test new parrec2nii LAS voxel orientation
Adjust and extend tests for new LAS voxel orientation default.
1 parent e8b588f commit e625347

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

nibabel/tests/test_scripts.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from ..tmpdirs import InTemporaryDirectory
1818
from ..loadsave import load
19+
from ..orientations import flip_axis, aff2axcodes, inv_ornt_aff
1920

2021
from nose.tools import (assert_true, assert_false, assert_not_equal,
2122
assert_equal)
@@ -89,6 +90,8 @@ def vox_size(affine):
8990
def check_conversion(cmd, pr_data, out_fname):
9091
run_command(cmd)
9192
img = load(out_fname)
93+
# Check orientations always LAS
94+
assert_equal(aff2axcodes(img.affine), tuple('LAS'))
9295
data = img.get_data()
9396
assert_true(np.allclose(data, pr_data))
9497
assert_true(np.allclose(img.header['cal_min'], data.min()))
@@ -137,19 +140,22 @@ def test_parrec2nii():
137140
assert_equal(code, 1)
138141
# Default scaling is dv
139142
pr_img = load(fname)
143+
flipped_data = flip_axis(pr_img.get_data(), 1)
140144
base_cmd = ['parrec2nii', '--overwrite', fname]
141-
check_conversion(base_cmd, pr_img.get_data(), out_froot)
145+
check_conversion(base_cmd, flipped_data, out_froot)
142146
check_conversion(base_cmd + ['--scaling=dv'],
143-
pr_img.get_data(),
147+
flipped_data,
144148
out_froot)
145149
# fp
146150
pr_img = load(fname, scaling='fp')
151+
flipped_data = flip_axis(pr_img.get_data(), 1)
147152
check_conversion(base_cmd + ['--scaling=fp'],
148-
pr_img.get_data(),
153+
flipped_data,
149154
out_froot)
150155
# no scaling
156+
unscaled_flipped = flip_axis(pr_img.dataobj.get_unscaled(), 1)
151157
check_conversion(base_cmd + ['--scaling=off'],
152-
pr_img.dataobj.get_unscaled(),
158+
unscaled_flipped,
153159
out_froot)
154160
# Save extensions
155161
run_command(base_cmd + ['--store-header'])
@@ -161,6 +167,8 @@ def test_parrec2nii():
161167
@needs_nibabel_data('nitest-balls1')
162168
def test_parrec2nii_with_data():
163169
# Use nibabel-data to test conversion
170+
# Premultiplier to relate our affines to Philips conversion
171+
LAS2LPS = inv_ornt_aff([[0, 1], [1, -1], [2, 1]], (80, 80, 10))
164172
with InTemporaryDirectory():
165173
for par in glob(pjoin(BALLS, 'PARREC', '*.PAR')):
166174
par_root, ext = splitext(basename(par))
@@ -171,23 +179,31 @@ def test_parrec2nii_with_data():
171179
# Do conversion
172180
run_command(['parrec2nii', par])
173181
conved_img = load(par_root + '.nii')
182+
# Confirm parrec2nii conversions are LAS
183+
assert_equal(aff2axcodes(conved_img.affine), tuple('LAS'))
184+
# Shape same whether LPS or LAS
174185
assert_equal(conved_img.shape[:3], (80, 80, 10))
175-
# Test against converted NIfTI
186+
# Test against original converted NIfTI
176187
nifti_fname = pjoin(BALLS, 'NIFTI', par_root + '.nii.gz')
177188
if exists(nifti_fname):
178-
nimg = load(nifti_fname)
179-
assert_almost_equal(nimg.affine[:3, :3],
180-
conved_img.affine[:3, :3], 3)
189+
philips_img = load(nifti_fname)
190+
# Confirm Philips converted image always LPS
191+
assert_equal(aff2axcodes(philips_img.affine), tuple('LPS'))
192+
# Equivalent to Philips LPS affine
193+
equiv_affine = conved_img.affine.dot(LAS2LPS)
194+
assert_almost_equal(philips_img.affine[:3, :3],
195+
equiv_affine[:3, :3], 3)
181196
# The translation part is always off by the same ammout
182-
aff_off = conved_img.affine[:3, 3] - nimg.affine[:3, 3]
183-
assert_almost_equal(aff_off, AFF_OFF, 4)
197+
aff_off = equiv_affine[:3, 3] - philips_img.affine[:3, 3]
198+
assert_almost_equal(aff_off, AFF_OFF, 3)
184199
# The difference is max in the order of 0.5 voxel
185-
vox_sizes = np.sqrt((nimg.affine[:3, :3] ** 2).sum(axis=0))
200+
vox_sizes = vox_size(philips_img.affine)
186201
assert_true(np.all(np.abs(aff_off / vox_sizes) <= 0.5))
187202
# The data is very close, unless it's the fieldmap
188203
if par_root != 'fieldmap':
189-
assert_true(np.allclose(conved_img.dataobj,
190-
nimg.dataobj))
204+
conved_data_lps = flip_axis(conved_img.dataobj, 1)
205+
assert_true(np.allclose(conved_data_lps,
206+
philips_img.dataobj))
191207
with InTemporaryDirectory():
192208
# Test some options
193209
dti_par = pjoin(BALLS, 'PARREC', 'DTI.PAR')
@@ -202,8 +218,12 @@ def test_parrec2nii_with_data():
202218
# Writes bvals, bvecs files if asked
203219
run_command(['parrec2nii', '--overwrite', '--bvs', dti_par])
204220
assert_almost_equal(np.loadtxt('DTI.bvals'), DTI_PAR_BVALS)
205-
assert_almost_equal(np.loadtxt('DTI.bvecs'),
206-
DTI_PAR_BVECS[:, [2, 0, 1]].T)
221+
# Bvecs in header, transposed from PSL to LPS
222+
bvecs_LPS = DTI_PAR_BVECS[:, [2, 0, 1]]
223+
# Adjust for output flip of Y axis in data and bvecs
224+
bvecs_LAS = bvecs_LPS * [1, -1, 1]
225+
assert_almost_equal(np.loadtxt('DTI.bvecs'), bvecs_LAS.T)
226+
# Dwell time
207227
assert_false(exists('DTI.dwell_time'))
208228
# Need field strength if requesting dwell time
209229
code, _, _, = run_command(

0 commit comments

Comments
 (0)