@@ -5,6 +5,7 @@ from __future__ import division, print_function, absolute_import
5
5
6
6
from optparse import OptionParser , Option
7
7
import numpy as np
8
+ import numpy .linalg as npl
8
9
import sys
9
10
import os
10
11
import nibabel
@@ -14,6 +15,9 @@ from nibabel.mriutils import calculate_dwell_time, MRIError
14
15
import nibabel .nifti1 as nifti1
15
16
from nibabel .filename_parser import splitext_addext
16
17
from nibabel .volumeutils import fname_ext_ul_case
18
+ from nibabel .orientations import (io_orientation , inv_ornt_aff ,
19
+ apply_orientation )
20
+ from nibabel .affines import apply_affine , from_matvec , to_matvec
17
21
18
22
19
23
def get_opt_parser ():
@@ -163,6 +167,17 @@ def proc_file(infile, opts):
163
167
intercept = np .array ([0. ])
164
168
in_data = np .array (pr_img .dataobj )
165
169
out_dtype = np .float64
170
+ # Reorient data block to LAS+ if necessary
171
+ ornt = io_orientation (np .diag ([- 1 , 1 , 1 , 1 ]).dot (affine ))
172
+ if np .all (ornt == [[0 , 1 ],
173
+ [1 , 1 ],
174
+ [2 , 1 ]]): # already in LAS+
175
+ t_aff = np .eye (4 )
176
+ else : # Not in LAS+
177
+ t_aff = inv_ornt_aff (ornt , pr_img .shape )
178
+ affine = np .dot (affine , t_aff )
179
+ in_data = apply_orientation (in_data , ornt )
180
+ # Make corresponding NIfTI image
166
181
nimg = nifti1 .Nifti1Image (in_data , affine , pr_hdr )
167
182
nhdr = nimg .header
168
183
nhdr .set_data_dtype (out_dtype )
@@ -198,6 +213,10 @@ def proc_file(infile, opts):
198
213
verbose ('No DTI volumes detected, bvals and bvecs not written' )
199
214
else :
200
215
verbose ('Writing .bvals and .bvecs files' )
216
+ # Transform bvecs with reorientation affine
217
+ orig2new = npl .inv (t_aff )
218
+ bv_reorient = from_matvec (to_matvec (orig2new )[0 ], [0 , 0 , 0 ])
219
+ bvecs = apply_affine (bv_reorient , bvecs )
201
220
with open (basefilename + '.bvals' , 'w' ) as fid :
202
221
# np.savetxt could do this, but it's just a loop anyway
203
222
for val in bvals :
0 commit comments