<s> import sys <EOL> import os <EOL> import commands <EOL> import getopt <EOL> import nibabel <EOL> import time <EOL> import math <EOL> import sct_utils as sct <EOL> class param : <EOL> def __init__ ( self ) : <EOL> self . debug = <NUM_LIT:0> <EOL> self . interp = '<STR_LIT>' <EOL> self . verbose = <NUM_LIT:0> <EOL> self . remove_temp_files = <NUM_LIT:1> <EOL> def main ( ) : <EOL> status , path_sct = commands . getstatusoutput ( '<STR_LIT>' ) <EOL> print path_sct <EOL> fsloutput = '<STR_LIT>' <EOL> fname_data = '<STR_LIT>' <EOL> fname_bvecs = '<STR_LIT>' <EOL> fname_schedule = path_sct + '<STR_LIT>' <EOL> interp = param . interp <EOL> remove_temp_files = param . remove_temp_files <EOL> verbose = param . verbose <EOL> start_time = time . time ( ) <EOL> if param . debug : <EOL> fname_data = path_sct + '<STR_LIT>' <EOL> fname_bvecs = path_sct + '<STR_LIT>' <EOL> interp = '<STR_LIT>' <EOL> remove_temp_files = <NUM_LIT:0> <EOL> verbose = <NUM_LIT:1> <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' ) <EOL> except getopt . GetoptError : <EOL> usage ( ) <EOL> for opt , arg in opts : <EOL> if opt == '<STR_LIT>' : <EOL> usage ( ) <EOL> elif opt in ( "<STR_LIT>" ) : <EOL> fname_bvecs = arg <EOL> elif opt in ( "<STR_LIT>" ) : <EOL> fname_data = arg <EOL> elif opt in ( '<STR_LIT>' ) : <EOL> interp = str ( arg ) <EOL> elif opt in ( '<STR_LIT>' ) : <EOL> verbose = int ( arg ) <EOL> if fname_data == '<STR_LIT>' or fname_bvecs == '<STR_LIT>' : <EOL> usage ( ) <EOL> sct . check_file_exist ( fname_data ) <EOL> sct . check_file_exist ( fname_bvecs ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' + fname_data <EOL> print '<STR_LIT>' + fname_bvecs <EOL> print '<STR_LIT>' <EOL> fname_data = os . path . abspath ( fname_data ) <EOL> fname_bvecs = os . path . abspath ( fname_bvecs ) <EOL> path_data , file_data , ext_data = sct . extract_fname ( fname_data ) <EOL> path_tmp = '<STR_LIT>' + time . strftime ( "<STR_LIT>" ) <EOL> sct . run ( '<STR_LIT>' + path_tmp ) <EOL> os . chdir ( path_tmp ) <EOL> print '<STR_LIT>' <EOL> nx , ny , nz , nt , px , py , pz , pt = sct . get_dimension ( fname_data ) <EOL> print '<STR_LIT>' + str ( nx ) + '<STR_LIT>' + str ( ny ) + '<STR_LIT>' + str ( nz ) + '<STR_LIT>' + str ( nt ) <EOL> print '<STR_LIT>' <EOL> bvecs = [ ] <EOL> with open ( 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> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> bvecs = zip ( * bvecs ) <EOL> print '<STR_LIT>' <EOL> index_b0 = [ ] <EOL> index_dwi = [ ] <EOL> for it in xrange ( <NUM_LIT:0> , nt ) : <EOL> if math . sqrt ( math . fsum ( [ i ** <NUM_LIT:2> for i in bvecs [ it ] ] ) ) < <NUM_LIT> : <EOL> index_b0 . append ( it ) <EOL> else : <EOL> index_dwi . append ( it ) <EOL> n_b0 = len ( index_b0 ) <EOL> n_dwi = len ( index_dwi ) <EOL> print '<STR_LIT>' + str ( index_b0 ) <EOL> print '<STR_LIT>' + str ( index_dwi ) <EOL> print '<STR_LIT>' <EOL> status , output = sct . run ( fsloutput + '<STR_LIT>' + fname_data + '<STR_LIT>' ) <EOL> status , output = sct . run ( '<STR_LIT>' ) <EOL> file_data_split = output . split ( ) <EOL> file_data_split = [ file_data_split [ i ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) for i in xrange ( <NUM_LIT:0> , len ( file_data_split ) ) ] <EOL> print '<STR_LIT>' <EOL> file_b0 = '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' + file_b0 <EOL> for it in xrange ( <NUM_LIT:0> , n_b0 ) : <EOL> cmd += '<STR_LIT:U+0020>' + file_data_split [ index_b0 [ it ] ] <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> file_dwi = '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' + file_dwi <EOL> for it in xrange ( <NUM_LIT:0> , n_dwi ) : <EOL> cmd += '<STR_LIT:U+0020>' + file_data_split [ index_dwi [ it ] ] <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> file_b0_mean = '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' + file_b0 + '<STR_LIT>' + file_b0_mean <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> file_dwi_mean = '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' + file_dwi + '<STR_LIT>' + file_dwi_mean <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' + file_dwi_mean + '<STR_LIT>' <EOL> for it in xrange ( <NUM_LIT:0> , n_dwi ) : <EOL> file_target = file_dwi_mean <EOL> file_mat = '<STR_LIT>' + str ( index_dwi [ it ] ) . zfill ( <NUM_LIT:4> ) <EOL> cmd = fsloutput + '<STR_LIT>' + file_data_split [ index_dwi [ it ] ] + '<STR_LIT>' + file_target + '<STR_LIT>' + file_mat + '<STR_LIT>' + fname_schedule + '<STR_LIT>' + file_data_split [ index_dwi [ it ] ] + '<STR_LIT>' <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> file_dwi = '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' + file_dwi <EOL> for it in xrange ( <NUM_LIT:0> , n_dwi ) : <EOL> cmd += '<STR_LIT:U+0020>' + file_data_split [ index_dwi [ it ] ] + '<STR_LIT>' <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> file_dwi_mean = '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' + file_dwi + '<STR_LIT>' + file_dwi_mean <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> for it in xrange ( <NUM_LIT:0> , n_b0 ) : <EOL> file_target = file_data_split [ int ( index_b0 [ <NUM_LIT:0> ] ) ] <EOL> file_mat = '<STR_LIT>' + str ( index_b0 [ it ] ) . zfill ( <NUM_LIT:4> ) <EOL> cmd = fsloutput + '<STR_LIT>' + file_data_split [ index_b0 [ it ] ] + '<STR_LIT>' + file_target + '<STR_LIT>' + file_mat + '<STR_LIT>' + file_data_split [ index_b0 [ it ] ] + '<STR_LIT>' <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' <EOL> for it in xrange ( <NUM_LIT:0> , n_b0 ) : <EOL> cmd += '<STR_LIT:U+0020>' + file_data_split [ index_b0 [ it ] ] + '<STR_LIT>' <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> for it in xrange ( <NUM_LIT:0> , n_dwi ) : <EOL> cmd = '<STR_LIT>' + str ( index_dwi [ it ] ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' + str ( index_dwi [ it ] ) . zfill ( <NUM_LIT:4> ) <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> for it in xrange ( <NUM_LIT:0> , n_b0 ) : <EOL> cmd = '<STR_LIT>' + str ( index_b0 [ it ] ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' + str ( index_b0 [ it ] ) . zfill ( <NUM_LIT:4> ) <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> for it in xrange ( <NUM_LIT:0> , nt ) : <EOL> cmd = fsloutput + '<STR_LIT>' + str ( it ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' + str ( index_b0 [ <NUM_LIT:0> ] ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' + str ( it ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' + str ( it ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' + '<STR_LIT>' + interp <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' <EOL> for it in xrange ( <NUM_LIT:0> , nt ) : <EOL> cmd += '<STR_LIT>' + str ( it ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' <EOL> for it in xrange ( <NUM_LIT:0> , n_dwi ) : <EOL> cmd += '<STR_LIT>' + str ( index_dwi [ it ] ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' <EOL> for it in xrange ( <NUM_LIT:0> , n_b0 ) : <EOL> cmd += '<STR_LIT>' + str ( index_b0 [ it ] ) . zfill ( <NUM_LIT:4> ) + '<STR_LIT>' <EOL> status , output = sct . run ( cmd ) <EOL> print '<STR_LIT>' <EOL> cmd = fsloutput + '<STR_LIT>' <EOL> status , output = sct . run ( cmd ) <EOL> print ( '<STR_LIT>' ) <EOL> sct . generate_output_file ( '<STR_LIT>' , path_data , file_data + '<STR_LIT>' , ext_data ) <EOL> sct . generate_output_file ( '<STR_LIT>' , path_data , '<STR_LIT>' , ext_data ) <EOL> sct . generate_output_file ( '<STR_LIT>' , path_data , '<STR_LIT>' , ext_data ) <EOL> os . chdir ( '<STR_LIT:..>' ) <EOL> if remove_temp_files == <NUM_LIT:1> : <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + path_tmp ) <EOL> elapsed_time = time . time ( ) - start_time <EOL> print '<STR_LIT>' + str ( int ( round ( elapsed_time ) ) ) + '<STR_LIT:s>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' + file_data + '<STR_LIT:U+0020>' + file_data + '<STR_LIT>' <EOL> def usage ( ) : <EOL> print '<STR_LIT:\n>' '<STR_LIT>' + os . path . basename ( __file__ ) + '<STR_LIT:\n>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT:\n>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT:\n>' '<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 ( param . interp ) + '<STR_LIT:\n>' '<STR_LIT>' + str ( param . verbose ) + '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> param = param ( ) <EOL> main ( ) </s>
