def recompose_dwi(in_dwi, in_bval, in_corrected, out_file=None):
 import numpy as np
 import nibabel as nb
 import os.path as op
 if (out_file is None):
  (fname, ext) = op.splitext(op.basename(in_dwi))
  if (ext == u'.gz'):
   (fname, ext2) = op.splitext(fname)
   ext = (ext2 + ext)
  out_file = op.abspath((u'%s_eccorrect%s' % (fname, ext)))
 im = nb.load(in_dwi)
 dwidata = im.get_data()
 bvals = np.loadtxt(in_bval)
 dwis = np.where((bvals != 0))[0].tolist()
 if (len(dwis) != len(in_corrected)):
  raise RuntimeError(u'Length of DWIs in b-values table and aftercorrection should match')
 for (bindex, dwi) in zip(dwis, in_corrected):
  dwidata[..., bindex] = nb.load(dwi).get_data()
 nb.Nifti1Image(dwidata, im.affine, im.header).to_filename(out_file)
 return out_file

def recompose_dwi ( in_dwi , in_bval , in_corrected , out_file = None ) : import numpy as np import nibabel as nb import os . path as op if ( out_file is None ) : ( fname , ext ) = op . splitext ( op . basename ( in_dwi ) ) if ( ext == u'.gz' ) : ( fname , ext2 ) = op . splitext ( fname ) ext = ( ext2 + ext ) out_file = op . abspath ( ( u'%s_eccorrect%s' % ( fname , ext ) ) ) im = nb . load ( in_dwi ) dwidata = im . get_data ( ) bvals = np . loadtxt ( in_bval ) dwis = np . where ( ( bvals != 0 ) ) [ 0 ] . tolist ( ) if ( len ( dwis ) != len ( in_corrected ) ) : raise RuntimeError ( u'Length of DWIs in b-values table and aftercorrection should match' ) for ( bindex , dwi ) in zip ( dwis , in_corrected ) : dwidata [ ... , bindex ] = nb . load ( dwi ) . get_data ( ) nb . Nifti1Image ( dwidata , im . affine , im . header ) . to_filename ( out_file ) return out_file
recompose back the dmri data accordingly the b-values table after ec correction .