def reorient_bvecs(in_dwi, old_dwi, in_bvec):
 import os
 import numpy as np
 import nibabel as nb
 (name, fext) = os.path.splitext(os.path.basename(in_bvec))
 if (fext == u'.gz'):
  (name, _) = os.path.splitext(name)
 out_file = os.path.abspath((u'%s_reorient.bvec' % name))
 bvecs = np.loadtxt(in_bvec).T
 new_bvecs = []
 N = nb.load(in_dwi).affine
 O = nb.load(old_dwi).affine
 RS = N.dot(np.linalg.inv(O))[:3, :3]
 sc_idx = np.where(((np.abs(RS) != 1) & (RS != 0)))
 S = np.ones_like(RS)
 S[sc_idx] = RS[sc_idx]
 R = (RS / S)
 new_bvecs = [R.dot(b) for b in bvecs]
 np.savetxt(out_file, np.array(new_bvecs).T, fmt='%0.15f')
 return out_file

def reorient_bvecs ( in_dwi , old_dwi , in_bvec ) : import os import numpy as np import nibabel as nb ( name , fext ) = os . path . splitext ( os . path . basename ( in_bvec ) ) if ( fext == u'.gz' ) : ( name , _ ) = os . path . splitext ( name ) out_file = os . path . abspath ( ( u'%s_reorient.bvec' % name ) ) bvecs = np . loadtxt ( in_bvec ) . T new_bvecs = [ ] N = nb . load ( in_dwi ) . affine O = nb . load ( old_dwi ) . affine RS = N . dot ( np . linalg . inv ( O ) ) [ : 3 , : 3 ] sc_idx = np . where ( ( ( np . abs ( RS ) != 1 ) & ( RS != 0 ) ) ) S = np . ones_like ( RS ) S [ sc_idx ] = RS [ sc_idx ] R = ( RS / S ) new_bvecs = [ R . dot ( b ) for b in bvecs ] np . savetxt ( out_file , np . array ( new_bvecs ) . T , fmt = '%0.15f' ) return out_file
checks reorientations of in_dwi w .