16
16
17
17
from ..tmpdirs import InTemporaryDirectory
18
18
from ..loadsave import load
19
+ from ..orientations import flip_axis , aff2axcodes , inv_ornt_aff
19
20
20
21
from nose .tools import (assert_true , assert_false , assert_not_equal ,
21
22
assert_equal )
@@ -89,6 +90,8 @@ def vox_size(affine):
89
90
def check_conversion (cmd , pr_data , out_fname ):
90
91
run_command (cmd )
91
92
img = load (out_fname )
93
+ # Check orientations always LAS
94
+ assert_equal (aff2axcodes (img .affine ), tuple ('LAS' ))
92
95
data = img .get_data ()
93
96
assert_true (np .allclose (data , pr_data ))
94
97
assert_true (np .allclose (img .header ['cal_min' ], data .min ()))
@@ -137,19 +140,22 @@ def test_parrec2nii():
137
140
assert_equal (code , 1 )
138
141
# Default scaling is dv
139
142
pr_img = load (fname )
143
+ flipped_data = flip_axis (pr_img .get_data (), 1 )
140
144
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 )
142
146
check_conversion (base_cmd + ['--scaling=dv' ],
143
- pr_img . get_data () ,
147
+ flipped_data ,
144
148
out_froot )
145
149
# fp
146
150
pr_img = load (fname , scaling = 'fp' )
151
+ flipped_data = flip_axis (pr_img .get_data (), 1 )
147
152
check_conversion (base_cmd + ['--scaling=fp' ],
148
- pr_img . get_data () ,
153
+ flipped_data ,
149
154
out_froot )
150
155
# no scaling
156
+ unscaled_flipped = flip_axis (pr_img .dataobj .get_unscaled (), 1 )
151
157
check_conversion (base_cmd + ['--scaling=off' ],
152
- pr_img . dataobj . get_unscaled () ,
158
+ unscaled_flipped ,
153
159
out_froot )
154
160
# Save extensions
155
161
run_command (base_cmd + ['--store-header' ])
@@ -161,6 +167,8 @@ def test_parrec2nii():
161
167
@needs_nibabel_data ('nitest-balls1' )
162
168
def test_parrec2nii_with_data ():
163
169
# 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 ))
164
172
with InTemporaryDirectory ():
165
173
for par in glob (pjoin (BALLS , 'PARREC' , '*.PAR' )):
166
174
par_root , ext = splitext (basename (par ))
@@ -171,23 +179,31 @@ def test_parrec2nii_with_data():
171
179
# Do conversion
172
180
run_command (['parrec2nii' , par ])
173
181
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
174
185
assert_equal (conved_img .shape [:3 ], (80 , 80 , 10 ))
175
- # Test against converted NIfTI
186
+ # Test against original converted NIfTI
176
187
nifti_fname = pjoin (BALLS , 'NIFTI' , par_root + '.nii.gz' )
177
188
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 )
181
196
# 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 )
184
199
# 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 )
186
201
assert_true (np .all (np .abs (aff_off / vox_sizes ) <= 0.5 ))
187
202
# The data is very close, unless it's the fieldmap
188
203
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 ))
191
207
with InTemporaryDirectory ():
192
208
# Test some options
193
209
dti_par = pjoin (BALLS , 'PARREC' , 'DTI.PAR' )
@@ -202,8 +218,12 @@ def test_parrec2nii_with_data():
202
218
# Writes bvals, bvecs files if asked
203
219
run_command (['parrec2nii' , '--overwrite' , '--bvs' , dti_par ])
204
220
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
207
227
assert_false (exists ('DTI.dwell_time' ))
208
228
# Need field strength if requesting dwell time
209
229
code , _ , _ , = run_command (
0 commit comments