<s> import sys <EOL> import os <EOL> import commands <EOL> import getopt <EOL> import time <EOL> import numpy as np <EOL> from msct_image import Image <EOL> import sct_utils as sct <EOL> status , path_sct = commands . getstatusoutput ( '<STR_LIT>' ) <EOL> fsloutput = '<STR_LIT>' <EOL> class Param : <EOL> def __init__ ( self ) : <EOL> self . fname_data = '<STR_LIT>' <EOL> self . fname_bvecs = '<STR_LIT>' <EOL> self . slicewise = <NUM_LIT:1> <EOL> self . output_path = '<STR_LIT>' <EOL> self . mat_eddy = '<STR_LIT>' <EOL> self . min_norm = <NUM_LIT> <EOL> self . swapXY = <NUM_LIT:0> <EOL> self . cost_function_flirt = '<STR_LIT>' <EOL> self . interp = '<STR_LIT>' <EOL> self . delete_tmp_files = <NUM_LIT:1> <EOL> self . merge_back = <NUM_LIT:1> <EOL> self . verbose = <NUM_LIT:0> <EOL> self . plot_graph = <NUM_LIT:0> <EOL> def main ( ) : <EOL> start_time = time . time ( ) <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' ) <EOL> except getopt . GetoptError : <EOL> usage ( ) <EOL> if not opts : <EOL> usage ( ) <EOL> for opt , arg in opts : <EOL> if opt == '<STR_LIT>' : <EOL> usage ( ) <EOL> elif opt in ( '<STR_LIT>' ) : <EOL> param . fname_data = arg <EOL> elif opt in ( '<STR_LIT>' ) : <EOL> param . fname_bvecs = arg <EOL> elif opt in ( '<STR_LIT:-c>' ) : <EOL> param . cost_function_flirt = arg <EOL> elif opt in ( '<STR_LIT>' ) : <EOL> param . plot_graph = int ( arg ) <EOL> elif opt in ( '<STR_LIT>' ) : <EOL> param . mat_eddy = arg <EOL> elif opt in ( '<STR_LIT>' ) : <EOL> param . output_path = arg <EOL> elif opt in ( '<STR_LIT>' ) : <EOL> param . interp = arg <EOL> elif opt in ( '<STR_LIT>' ) : <EOL> param . delete_tmp_files = int ( arg ) <EOL> elif opt in ( '<STR_LIT>' ) : <EOL> param . slicewise = int ( arg ) <EOL> elif opt in ( '<STR_LIT>' ) : <EOL> param . verbose = int ( arg ) <EOL> if param . fname_data == '<STR_LIT>' or param . fname_bvecs == '<STR_LIT>' : <EOL> print '<STR_LIT>' <EOL> usage ( ) <EOL> if param . output_path == '<STR_LIT>' : param . output_path = os . getcwd ( ) + '<STR_LIT:/>' <EOL> path_tmp = '<STR_LIT>' + time . strftime ( "<STR_LIT>" ) <EOL> sct . run ( '<STR_LIT>' + path_tmp , param . verbose ) <EOL> os . chdir ( path_tmp ) <EOL> eddy_correct ( param ) <EOL> os . chdir ( '<STR_LIT:..>' ) <EOL> if param . delete_tmp_files == <NUM_LIT:1> : <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + path_tmp , param . verbose ) <EOL> elapsed_time = time . time ( ) - start_time <EOL> print '<STR_LIT>' + str ( int ( round ( elapsed_time ) ) ) + '<STR_LIT:s>' <EOL> def eddy_correct ( param ) : <EOL> sct . printv ( '<STR_LIT>' , param . verbose ) <EOL> sct . printv ( '<STR_LIT>' , param . verbose ) <EOL> sct . printv ( '<STR_LIT>' , param . verbose ) <EOL> fname_data = param . fname_data <EOL> min_norm = param . min_norm <EOL> cost_function = param . cost_function_flirt <EOL> verbose = param . verbose <EOL> sct . printv ( ( '<STR_LIT>' + param . fname_data ) , verbose ) <EOL> sct . printv ( ( '<STR_LIT>' + param . fname_bvecs ) , verbose ) <EOL> path_data , file_data , ext_data = sct . extract_fname ( fname_data ) <EOL> if param . mat_eddy == '<STR_LIT>' : param . mat_eddy = '<STR_LIT>' <EOL> if not os . path . exists ( param . mat_eddy ) : os . makedirs ( param . mat_eddy ) <EOL> mat_eddy = param . mat_eddy <EOL> schedule_file = path_sct + '<STR_LIT>' <EOL> sct . printv ( ( '<STR_LIT>' + schedule_file ) , verbose ) <EOL> if param . swapXY == <NUM_LIT:1> : <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> fname_data_new = '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' + fname_data + '<STR_LIT>' + fname_data_new <EOL> status , output = sct . run ( cmd , verbose ) <EOL> sct . printv ( ( '<STR_LIT>' + fname_data_new ) , verbose ) <EOL> else : <EOL> fname_data_new = fname_data <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> nx , ny , nz , nt , px , py , pz , pt = Image ( fname_data ) . dim <EOL> sct . printv ( '<STR_LIT>' + str ( nx ) + '<STR_LIT>' + str ( ny ) + '<STR_LIT>' + str ( nz ) + '<STR_LIT>' + str ( nt ) , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> from sct_image import split_data <EOL> im_to_split = Image ( fname_data_new + '<STR_LIT>' ) <EOL> im_split_list = split_data ( im_to_split , <NUM_LIT:3> ) <EOL> for im in im_split_list : <EOL> im . save ( ) <EOL> if param . slicewise : <EOL> nb_loops = nz <EOL> file_suffix = [ ] <EOL> for iZ in range ( nz ) : <EOL> file_suffix . append ( '<STR_LIT>' + str ( iZ ) . zfill ( <NUM_LIT:4> ) ) <EOL> else : <EOL> nb_loops = <NUM_LIT:1> <EOL> file_suffix = [ '<STR_LIT>' ] <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> bvecs = [ ] <EOL> with open ( param . fname_bvecs ) as f : <EOL> for line in f : <EOL> bvecs_new = map ( float , line . split ( ) ) <EOL> bvecs . append ( bvecs_new ) <EOL> if not len ( bvecs [ <NUM_LIT:0> ] [ : ] ) == <NUM_LIT:3> : <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> bvecs = zip ( * bvecs ) <EOL> bvecs = np . array ( bvecs ) <EOL> opposite_gradients_iT = [ ] <EOL> opposite_gradients_jT = [ ] <EOL> index_identified = [ ] <EOL> index_b0 = [ ] <EOL> for iT in range ( nt - <NUM_LIT:1> ) : <EOL> if np . linalg . norm ( bvecs [ iT , : ] ) != <NUM_LIT:0> : <EOL> if iT not in index_identified : <EOL> jT = iT + <NUM_LIT:1> <EOL> if np . linalg . norm ( ( bvecs [ iT , : ] + bvecs [ jT , : ] ) ) < min_norm : <EOL> sct . printv ( ( '<STR_LIT>' + str ( iT ) + '<STR_LIT>' + str ( jT ) ) , verbose ) <EOL> opposite_gradients_iT . append ( iT ) <EOL> opposite_gradients_jT . append ( jT ) <EOL> index_identified . append ( iT ) <EOL> else : <EOL> index_b0 . append ( iT ) <EOL> sct . printv ( ( '<STR_LIT>' + str ( iT ) + '<STR_LIT>' ) , verbose ) <EOL> nb_oppositeGradients = len ( opposite_gradients_iT ) <EOL> sct . printv ( ( '<STR_LIT>' + str ( <NUM_LIT:2> * nb_oppositeGradients ) + '<STR_LIT>' + str ( nb_oppositeGradients ) + '<STR_LIT:)>' ) , verbose ) <EOL> sct . printv ( '<STR_LIT>' + str ( index_b0 ) , verbose ) <EOL> for iN in range ( nb_oppositeGradients ) : <EOL> i_plus = opposite_gradients_iT [ iN ] <EOL> i_minus = opposite_gradients_jT [ iN ] <EOL> sct . printv ( ( '<STR_LIT>' + str ( i_plus ) + '<STR_LIT>' + str ( i_minus ) + '<STR_LIT>' + str ( iN ) + '<STR_LIT:/>' + str ( nb_oppositeGradients ) + '<STR_LIT:)>' ) , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> if param . slicewise : <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> fname_plus = file_data + '<STR_LIT>' + str ( i_plus ) . zfill ( <NUM_LIT:4> ) <EOL> fname_plus_Z = file_data + '<STR_LIT>' + str ( i_plus ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' <EOL> im_plus = Image ( fname_plus + '<STR_LIT>' ) <EOL> im_plus_split_list = split_data ( im_plus , <NUM_LIT:2> ) <EOL> for im_p in im_plus_split_list : <EOL> im_p . save ( ) <EOL> fname_minus = file_data + '<STR_LIT>' + str ( i_minus ) . zfill ( <NUM_LIT:4> ) <EOL> fname_minus_Z = file_data + '<STR_LIT>' + str ( i_minus ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' <EOL> im_minus = Image ( fname_minus + '<STR_LIT>' ) <EOL> im_minus_split_list = split_data ( im_minus , <NUM_LIT:2> ) <EOL> for im_m in im_minus_split_list : <EOL> im_m . save ( ) <EOL> for iZ in range ( nb_loops ) : <EOL> fname_plus = file_data + '<STR_LIT>' + str ( i_plus ) . zfill ( <NUM_LIT:4> ) + file_suffix [ iZ ] <EOL> fname_minus = file_data + '<STR_LIT>' + str ( i_minus ) . zfill ( <NUM_LIT:4> ) + file_suffix [ iZ ] <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> fname_plus_corr = file_data + '<STR_LIT>' + str ( i_plus ) . zfill ( <NUM_LIT:4> ) + file_suffix [ iZ ] + '<STR_LIT>' <EOL> omat = '<STR_LIT>' + file_data + '<STR_LIT>' + str ( i_plus ) . zfill ( <NUM_LIT:4> ) + file_suffix [ iZ ] + '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' + fname_plus + '<STR_LIT>' + fname_minus + '<STR_LIT>' + schedule_file + '<STR_LIT>' + omat + '<STR_LIT>' + cost_function + '<STR_LIT>' <EOL> status , output = sct . run ( cmd , verbose ) <EOL> file = open ( omat ) <EOL> Matrix = np . loadtxt ( file ) <EOL> file . close ( ) <EOL> M = Matrix [ <NUM_LIT:0> : <NUM_LIT:4> , <NUM_LIT:0> : <NUM_LIT:4> ] <EOL> sct . printv ( ( '<STR_LIT>' + str ( M ) ) , verbose ) <EOL> sct . printv ( ( '<STR_LIT>' + omat ) , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> A = ( M - np . identity ( <NUM_LIT:4> ) ) / <NUM_LIT:2> <EOL> Mplus = np . identity ( <NUM_LIT:4> ) + A <EOL> omat_plus = mat_eddy + '<STR_LIT>' + str ( i_plus ) + '<STR_LIT>' + str ( iZ ) + '<STR_LIT>' <EOL> file = open ( omat_plus , '<STR_LIT:w>' ) <EOL> np . savetxt ( omat_plus , Mplus , fmt = '<STR_LIT>' , delimiter = '<STR_LIT:U+0020>' , newline = '<STR_LIT:\n>' , header = '<STR_LIT>' , footer = '<STR_LIT>' , comments = '<STR_LIT:#>' ) <EOL> file . close ( ) <EOL> sct . printv ( ( '<STR_LIT>' + omat_plus ) , verbose ) <EOL> Mminus = np . identity ( <NUM_LIT:4> ) - A <EOL> omat_minus = mat_eddy + '<STR_LIT>' + str ( i_minus ) + '<STR_LIT>' + str ( iZ ) + '<STR_LIT>' <EOL> file = open ( omat_minus , '<STR_LIT:w>' ) <EOL> np . savetxt ( omat_minus , Mminus , fmt = '<STR_LIT>' , delimiter = '<STR_LIT:U+0020>' , newline = '<STR_LIT:\n>' , header = '<STR_LIT>' , footer = '<STR_LIT>' , comments = '<STR_LIT:#>' ) <EOL> file . close ( ) <EOL> sct . printv ( ( '<STR_LIT>' + omat_minus ) , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> for iN in range ( nb_oppositeGradients ) : <EOL> for iFile in range ( <NUM_LIT:2> ) : <EOL> if iFile == <NUM_LIT:0> : <EOL> i_file = opposite_gradients_iT [ iN ] <EOL> else : <EOL> i_file = opposite_gradients_jT [ iN ] <EOL> for iZ in range ( nb_loops ) : <EOL> fname = file_data + '<STR_LIT>' + str ( i_file ) . zfill ( <NUM_LIT:4> ) + file_suffix [ iZ ] <EOL> fname_corr = fname + '<STR_LIT>' + '<STR_LIT>' <EOL> omat = mat_eddy + '<STR_LIT>' + str ( i_file ) + '<STR_LIT>' + str ( iZ ) + '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' + fname + '<STR_LIT>' + fname + '<STR_LIT>' + fname_corr + '<STR_LIT>' + omat + '<STR_LIT>' + param . interp <EOL> status , output = sct . run ( cmd , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> for iN in range ( nb_oppositeGradients ) : <EOL> i_plus = opposite_gradients_iT [ iN ] <EOL> fname_plus_corr = file_data + '<STR_LIT>' + str ( i_plus ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' + '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' + fname_plus_corr <EOL> for iZ in range ( nz ) : <EOL> fname_plus_Z_corr = file_data + '<STR_LIT>' + str ( i_plus ) . zfill ( <NUM_LIT:4> ) + file_suffix [ iZ ] + '<STR_LIT>' + '<STR_LIT>' <EOL> cmd = cmd + '<STR_LIT:U+0020>' + fname_plus_Z_corr <EOL> status , output = sct . run ( cmd , verbose ) <EOL> i_minus = opposite_gradients_jT [ iN ] <EOL> fname_minus_corr = file_data + '<STR_LIT>' + str ( i_minus ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' + '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' + fname_minus_corr <EOL> for iZ in range ( nz ) : <EOL> fname_minus_Z_corr = file_data + '<STR_LIT>' + str ( i_minus ) . zfill ( <NUM_LIT:4> ) + file_suffix [ iZ ] + '<STR_LIT>' + '<STR_LIT>' <EOL> cmd = cmd + '<STR_LIT:U+0020>' + fname_minus_Z_corr <EOL> status , output = sct . run ( cmd , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> fname_data_corr = param . output_path + file_data + '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' + fname_data_corr <EOL> path_tmp = os . getcwd ( ) <EOL> for iT in range ( nt ) : <EOL> if os . path . isfile ( ( path_tmp + '<STR_LIT:/>' + file_data + '<STR_LIT>' + str ( iT ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' + '<STR_LIT>' ) ) : <EOL> fname_data_corr_3d = file_data + '<STR_LIT>' + str ( iT ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' + '<STR_LIT>' <EOL> elif iT in index_b0 : <EOL> fname_data_corr_3d = file_data + '<STR_LIT>' + str ( iT ) . zfill ( <NUM_LIT:4> ) <EOL> cmd = cmd + '<STR_LIT:U+0020>' + fname_data_corr_3d <EOL> status , output = sct . run ( cmd , verbose ) <EOL> if param . swapXY == <NUM_LIT:1> : <EOL> fname_data_final = fname_data <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> cmd = fsloutput_temp + '<STR_LIT>' + fname_data_corr + '<STR_LIT>' + fname_data_final <EOL> status , output = sct . run ( cmd , verbose ) <EOL> else : <EOL> fname_data_final = fname_data_corr <EOL> sct . printv ( ( '<STR_LIT>' + fname_data_final ) , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> sct . printv ( '<STR_LIT>' , verbose ) <EOL> def usage ( ) : <EOL> print '<STR_LIT:\n>' '<STR_LIT:U+0020>' + os . path . basename ( __file__ ) + '<STR_LIT:\n>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT:\n>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT:U+0020>' + os . path . basename ( __file__ ) + '<STR_LIT>' '<STR_LIT:\n>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT:\n>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT:\n>' '<STR_LIT>' '<STR_LIT:U+0020>' + os . path . basename ( __file__ ) + '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> param = Param ( ) <EOL> main ( ) </s>
