<s> import os , sys , commands <EOL> status , path_sct = commands . getstatusoutput ( '<STR_LIT>' ) <EOL> sys . path . append ( path_sct + '<STR_LIT>' ) <EOL> import sct_utils as sct <EOL> import nibabel <EOL> from scipy import ndimage <EOL> from numpy import array <EOL> import matplotlib . pyplot as plt <EOL> import time <EOL> PATH_INFO = '<STR_LIT>' <EOL> PATH_OUTPUT = '<STR_LIT>' <EOL> folder_data_errsm = '<STR_LIT>' <EOL> folder_data_sct = '<STR_LIT>' <EOL> folder_data_marseille = '<STR_LIT>' <EOL> folder_data_pain = '<STR_LIT>' <EOL> """<STR_LIT:U+0020>""" <EOL> SUBJECTS_LIST = [ [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_sct + '<STR_LIT>' , folder_data_sct + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_sct + '<STR_LIT>' , folder_data_sct + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_pain + '<STR_LIT>' , folder_data_pain + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_pain + '<STR_LIT>' , folder_data_pain + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_pain + '<STR_LIT>' , folder_data_pain + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_pain + '<STR_LIT>' , folder_data_pain + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_pain + '<STR_LIT>' , folder_data_pain + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' , folder_data_errsm + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' , folder_data_marseille + '<STR_LIT>' ] <EOL> ] <EOL> height_of_template_space = <NUM_LIT> <EOL> x_size_of_template_space = <NUM_LIT:200> <EOL> y_size_of_template_space = <NUM_LIT:200> <EOL> number_labels_for_template = <NUM_LIT:20> <EOL> straightening_parameters = '<STR_LIT>' <EOL> class TimeObject : <EOL> def __init__ ( self , number_of_subjects = <NUM_LIT:1> ) : <EOL> self . start_timer = <NUM_LIT:0> <EOL> self . time_list = [ ] <EOL> self . total_number_of_subjects = number_of_subjects <EOL> self . number_of_subjects_done = <NUM_LIT:0> <EOL> self . is_started = False <EOL> def start ( self ) : <EOL> self . start_timer = time . time ( ) <EOL> self . is_started = True <EOL> def one_subject_done ( self ) : <EOL> self . number_of_subjects_done += <NUM_LIT:1> <EOL> self . time_list . append ( time . time ( ) - self . start_timer ) <EOL> remaining_subjects = self . total_number_of_subjects - self . number_of_subjects_done <EOL> time_one_subject = self . time_list [ - <NUM_LIT:1> ] / self . number_of_subjects_done <EOL> remaining_time = remaining_subjects * time_one_subject <EOL> hours , rem = divmod ( remaining_time , <NUM_LIT> ) <EOL> minutes , seconds = divmod ( rem , <NUM_LIT> ) <EOL> sct . printv ( '<STR_LIT>' . format ( int ( hours ) , int ( minutes ) , seconds ) ) <EOL> def stop ( self ) : <EOL> self . time_list . append ( time . time ( ) - self . start_timer ) <EOL> hours , rem = divmod ( self . time_list [ - <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> minutes , seconds = divmod ( rem , <NUM_LIT> ) <EOL> sct . printv ( '<STR_LIT>' . format ( int ( hours ) , int ( minutes ) , seconds ) ) <EOL> self . is_started = False <EOL> def printRemainingTime ( self ) : <EOL> remaining_subjects = self . total_number_of_subjects - self . number_of_subjects_done <EOL> time_one_subject = self . time_list [ - <NUM_LIT:1> ] / self . number_of_subjects_done <EOL> remaining_time = remaining_subjects * time_one_subject <EOL> hours , rem = divmod ( remaining_time , <NUM_LIT> ) <EOL> minutes , seconds = divmod ( rem , <NUM_LIT> ) <EOL> if self . is_started : <EOL> sct . printv ( '<STR_LIT>' . format ( int ( hours ) , int ( minutes ) , seconds ) ) <EOL> else : <EOL> sct . printv ( '<STR_LIT>' . format ( int ( hours ) , int ( minutes ) , seconds ) ) <EOL> def printTotalTime ( self ) : <EOL> hours , rem = divmod ( self . time_list [ - <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> minutes , seconds = divmod ( rem , <NUM_LIT> ) <EOL> if self . is_started : <EOL> sct . printv ( '<STR_LIT>' . format ( int ( hours ) , int ( minutes ) , seconds ) ) <EOL> else : <EOL> sct . printv ( '<STR_LIT>' . format ( int ( hours ) , int ( minutes ) , seconds ) ) <EOL> timer = dict ( ) <EOL> timer [ '<STR_LIT>' ] = TimeObject ( number_of_subjects = len ( SUBJECTS_LIST ) ) <EOL> timer [ '<STR_LIT>' ] = TimeObject ( number_of_subjects = len ( SUBJECTS_LIST ) ) <EOL> timer [ '<STR_LIT>' ] = TimeObject ( number_of_subjects = <NUM_LIT:1> ) <EOL> def main ( ) : <EOL> timer [ '<STR_LIT>' ] . start ( ) <EOL> timer [ '<STR_LIT>' ] . stop ( ) <EOL> timer [ '<STR_LIT>' ] . start ( ) <EOL> timer [ '<STR_LIT>' ] . stop ( ) <EOL> align_vertebrae ( '<STR_LIT>' ) <EOL> timer [ '<STR_LIT>' ] . stop ( ) <EOL> timer [ '<STR_LIT>' ] . stop ( ) <EOL> sct . printv ( '<STR_LIT>' ) <EOL> timer [ '<STR_LIT>' ] . printTotalTime ( ) <EOL> sct . printv ( '<STR_LIT>' ) <EOL> timer [ '<STR_LIT>' ] . printTotalTime ( ) <EOL> sct . printv ( '<STR_LIT>' ) <EOL> timer [ '<STR_LIT>' ] . printTotalTime ( ) <EOL> def do_preprocessing ( contrast ) : <EOL> for i in range ( <NUM_LIT:0> , len ( SUBJECTS_LIST ) ) : <EOL> subject = SUBJECTS_LIST [ i ] [ <NUM_LIT:0> ] <EOL> print '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast <EOL> if not os . path . isdir ( PATH_OUTPUT + '<STR_LIT>' + subject ) : <EOL> os . makedirs ( PATH_OUTPUT + '<STR_LIT>' + subject ) <EOL> if not os . path . isdir ( PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast ) : <EOL> os . makedirs ( PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast ) <EOL> os . chdir ( PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast ) <EOL> print '<STR_LIT>' <EOL> list_file = os . listdir ( PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast ) <EOL> if '<STR_LIT>' not in list_file : <EOL> print '<STR_LIT>' <EOL> if contrast == '<STR_LIT>' : <EOL> sct . run ( '<STR_LIT>' + SUBJECTS_LIST [ i ] [ <NUM_LIT:1> ] + '<STR_LIT>' ) <EOL> if contrast == '<STR_LIT>' : <EOL> sct . run ( '<STR_LIT>' + SUBJECTS_LIST [ i ] [ <NUM_LIT:2> ] + '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' + PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject + '<STR_LIT:/>' + '<STR_LIT>' <EOL> file_name = '<STR_LIT>' <EOL> os . chdir ( PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject ) <EOL> file_results = open ( PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject + '<STR_LIT:/>' + file_name , '<STR_LIT:r>' ) <EOL> ymin_anatomic = None <EOL> ymax_anatomic = None <EOL> for line in file_results : <EOL> line_list = line . split ( '<STR_LIT:U+002C>' ) <EOL> zmin_anatomic = line . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:0> ] <EOL> zmax_anatomic = line . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:1> ] <EOL> zmin_seg = line . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:2> ] <EOL> zmax_seg = line . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:3> ] <EOL> if len ( line_list ) == <NUM_LIT:6> : <EOL> ymin_anatomic = line . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:4> ] <EOL> ymax_anatomic = line . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:5> ] <EOL> file_results . close ( ) <EOL> os . chdir ( PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast ) <EOL> print '<STR_LIT>' <EOL> if ymin_anatomic == None and ymax_anatomic == None : <EOL> sct . run ( '<STR_LIT>' + zmin_anatomic + '<STR_LIT>' + zmax_anatomic ) <EOL> else : sct . run ( '<STR_LIT>' + ymin_anatomic + '<STR_LIT:U+002C>' + zmin_anatomic + '<STR_LIT>' + ymax_anatomic + '<STR_LIT:U+002C>' + zmax_anatomic ) <EOL> print '<STR_LIT>' <EOL> list_dir = os . listdir ( PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject ) <EOL> centerline_proseg = False <EOL> for k in range ( len ( list_dir ) ) : <EOL> if list_dir [ k ] == '<STR_LIT>' : <EOL> centerline_proseg = True <EOL> if centerline_proseg == True : <EOL> if contrast == '<STR_LIT>' : <EOL> sct . run ( '<STR_LIT>' + PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject + '<STR_LIT>' ) <EOL> if contrast == '<STR_LIT>' : <EOL> sct . run ( '<STR_LIT>' + PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject + '<STR_LIT>' ) <EOL> else : <EOL> if contrast == '<STR_LIT>' : <EOL> sct . run ( '<STR_LIT>' ) <EOL> if contrast == '<STR_LIT>' : <EOL> sct . run ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> path_seg , file_seg , ext_seg = sct . extract_fname ( '<STR_LIT>' ) <EOL> image_seg = nibabel . load ( '<STR_LIT>' ) <EOL> from msct_image import Image <EOL> nx , ny , nz , nt , px , py , pz , pt = Image ( '<STR_LIT>' ) . dim <EOL> data_seg = image_seg . get_data ( ) <EOL> hdr_seg = image_seg . get_header ( ) <EOL> z_centerline = [ iz for iz in range ( <NUM_LIT:0> , nz , <NUM_LIT:1> ) if data_seg [ : , : , iz ] . any ( ) ] <EOL> for k in range ( <NUM_LIT:0> , <NUM_LIT:3> ) : <EOL> data_seg [ : , : , z_centerline [ - <NUM_LIT:1> ] - k ] = <NUM_LIT:0> <EOL> if z_centerline [ <NUM_LIT:0> ] + k < nz : <EOL> data_seg [ : , : , z_centerline [ <NUM_LIT:0> ] + k ] = <NUM_LIT:0> <EOL> img_seg = nibabel . Nifti1Image ( data_seg , None , hdr_seg ) <EOL> nibabel . save ( img_seg , file_seg + '<STR_LIT>' + ext_seg ) <EOL> print '<STR_LIT>' <EOL> if zmax_seg == '<STR_LIT>' : <EOL> nx , ny , nz , nt , px , py , pz , pt = Image ( '<STR_LIT>' ) . dim <EOL> sct . run ( '<STR_LIT>' + zmin_seg + '<STR_LIT>' + str ( nz ) + '<STR_LIT>' ) <EOL> else : <EOL> sct . run ( '<STR_LIT>' + zmin_seg + '<STR_LIT>' + zmax_seg + '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> labels_updown = False <EOL> list_file_info = os . listdir ( PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject ) <EOL> for k in range ( <NUM_LIT:0> , len ( list_file_info ) ) : <EOL> if list_file_info [ k ] == '<STR_LIT>' : <EOL> labels_updown = True <EOL> if centerline_proseg == False and labels_updown == False : <EOL> print '<STR_LIT>' + PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject + '<STR_LIT>' + path_sct + '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if labels_updown : <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject + '<STR_LIT>' ) <EOL> sct . run ( '<STR_LIT>' + PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject + '<STR_LIT>' ) <EOL> else : <EOL> sct . run ( '<STR_LIT>' + PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject + '<STR_LIT>' ) <EOL> sct . run ( '<STR_LIT>' + PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject + '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> cmd_straighten = ( '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast + '<STR_LIT>' + straightening_parameters ) <EOL> sct . run ( cmd_straighten ) <EOL> print '<STR_LIT>' <EOL> if ymin_anatomic == None and ymax_anatomic == None : <EOL> sct . run ( '<STR_LIT>' + PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject + '<STR_LIT>' + zmin_anatomic + '<STR_LIT>' + zmax_anatomic + '<STR_LIT>' ) <EOL> else : sct . run ( '<STR_LIT>' + PATH_INFO + '<STR_LIT:/>' + contrast + '<STR_LIT:/>' + subject + '<STR_LIT>' + ymin_anatomic + '<STR_LIT:U+002C>' + zmin_anatomic + '<STR_LIT>' + ymax_anatomic + '<STR_LIT:U+002C>' + zmax_anatomic + '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast + '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' ) <EOL> file = nibabel . load ( '<STR_LIT>' ) <EOL> data_c = file . get_data ( ) <EOL> X , Y , Z = ( data_c > <NUM_LIT:0> ) . nonzero ( ) <EOL> z_max = max ( Z ) <EOL> z_min = min ( Z ) <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + str ( z_min ) + '<STR_LIT>' + str ( z_max ) ) <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + str ( z_min ) + '<STR_LIT>' + str ( z_max ) ) <EOL> def create_cross ( contrast ) : <EOL> list_distances_1 = [ ] <EOL> list_distances_2 = [ ] <EOL> for i in range ( <NUM_LIT:0> , len ( SUBJECTS_LIST ) ) : <EOL> subject = SUBJECTS_LIST [ i ] [ <NUM_LIT:0> ] <EOL> print '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast <EOL> os . chdir ( PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast ) <EOL> print '<STR_LIT>' <EOL> img_label = nibabel . load ( '<STR_LIT>' ) <EOL> data_labels = img_label . get_data ( ) <EOL> from msct_image import Image <EOL> nx , ny , nz , nt , px , py , pz , pt = Image ( '<STR_LIT>' ) . dim <EOL> X , Y , Z = ( data_labels > <NUM_LIT:0> ) . nonzero ( ) <EOL> list_coordinates = [ ( [ X [ i ] , Y [ i ] , Z [ i ] , data_labels [ X [ i ] , Y [ i ] , Z [ i ] ] ] ) for i in range ( <NUM_LIT:0> , len ( X ) ) ] <EOL> for i in range ( len ( list_coordinates ) ) : <EOL> if list_coordinates [ i ] [ <NUM_LIT:3> ] == <NUM_LIT:1> : <EOL> coordinates_first_label = list_coordinates [ i ] <EOL> if list_coordinates [ i ] [ <NUM_LIT:3> ] == <NUM_LIT:20> : <EOL> coordinates_last_label = list_coordinates [ i ] <EOL> distance_1 = nz - <NUM_LIT:1> - coordinates_first_label [ <NUM_LIT:2> ] <EOL> distance_2 = nz - <NUM_LIT:1> - coordinates_last_label [ <NUM_LIT:2> ] <EOL> list_distances_1 . append ( distance_1 ) <EOL> list_distances_2 . append ( distance_2 ) <EOL> print '<STR_LIT>' <EOL> os . system ( '<STR_LIT>' + str ( int ( round ( nx / <NUM_LIT> ) ) ) + '<STR_LIT>' + str ( int ( round ( ny / <NUM_LIT> ) ) ) + '<STR_LIT>' + str ( coordinates_last_label [ <NUM_LIT:2> ] ) + '<STR_LIT>' + str ( coordinates_first_label [ <NUM_LIT:2> ] ) ) <EOL> print '<STR_LIT>' <EOL> mean_distance_1 = int ( round ( sum ( list_distances_1 ) / len ( list_distances_1 ) ) ) <EOL> mean_distance_2 = int ( round ( sum ( list_distances_2 ) / len ( list_distances_2 ) ) ) <EOL> L = height_of_template_space - <NUM_LIT:2> * mean_distance_2 <EOL> H = height_of_template_space - <NUM_LIT:2> * mean_distance_1 <EOL> os . chdir ( path_sct + '<STR_LIT>' ) <EOL> os . system ( '<STR_LIT>' + str ( x_size_of_template_space / <NUM_LIT:2> ) + '<STR_LIT>' + str ( y_size_of_template_space / <NUM_LIT:2> ) + '<STR_LIT>' + str ( L ) + '<STR_LIT>' + str ( H ) ) <EOL> def push_into_templace_space ( contrast ) : <EOL> for i in range ( <NUM_LIT:0> , len ( SUBJECTS_LIST ) ) : <EOL> subject = SUBJECTS_LIST [ i ] [ <NUM_LIT:0> ] <EOL> print '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast <EOL> os . chdir ( PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast ) <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' ) <EOL> sct . run ( '<STR_LIT>' ) <EOL> sct . run ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + contrast + '<STR_LIT>' <EOL> if not os . path . isdir ( PATH_OUTPUT + '<STR_LIT>' + contrast ) : <EOL> os . makedirs ( PATH_OUTPUT + '<STR_LIT>' + contrast ) <EOL> sct . run ( '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + contrast + '<STR_LIT:/>' + subject + '<STR_LIT>' ) <EOL> def average_levels ( contrast ) : <EOL> if contrast == '<STR_LIT>' : <EOL> print '<STR_LIT>' <EOL> from numpy import mean , zeros , array <EOL> n_i , n_l = <NUM_LIT:2> , number_labels_for_template <EOL> average = zeros ( ( n_i , n_l ) ) <EOL> compteur = <NUM_LIT:0> <EOL> img_T1 = nibabel . load ( PATH_OUTPUT + '<STR_LIT>' ) <EOL> data_T1 = img_T1 . get_data ( ) <EOL> X , Y , Z = ( data_T1 > <NUM_LIT:0> ) . nonzero ( ) <EOL> Z = [ Z [ i ] for i in Z . argsort ( ) ] <EOL> Z . reverse ( ) <EOL> for i in xrange ( n_l ) : <EOL> if i < len ( Z ) : <EOL> average [ compteur ] [ i ] = Z [ i ] <EOL> compteur = compteur + <NUM_LIT:1> <EOL> img_T2 = nibabel . load ( PATH_OUTPUT + '<STR_LIT>' ) <EOL> data_T2 = img_T2 . get_data ( ) <EOL> X , Y , Z = ( data_T1 > <NUM_LIT:0> ) . nonzero ( ) <EOL> Z = [ Z [ i ] for i in Z . argsort ( ) ] <EOL> Z . reverse ( ) <EOL> for i in xrange ( n_l ) : <EOL> if i < len ( Z ) : <EOL> average [ compteur ] [ i ] = Z [ i ] <EOL> average = array ( [ int ( round ( mean ( [ average [ average [ : , i ] > <NUM_LIT:0> , i ] ] ) ) ) for i in xrange ( n_l ) ] ) <EOL> template_absolute_path = path_sct + '<STR_LIT>' <EOL> print template_absolute_path <EOL> print '<STR_LIT>' <EOL> from msct_image import Image <EOL> nx , ny , nz , nt , px , py , pz , pt = Image ( template_absolute_path ) . dim <EOL> print '<STR_LIT>' + str ( nx ) + '<STR_LIT>' + str ( ny ) + '<STR_LIT>' + str ( nz ) <EOL> print '<STR_LIT>' + str ( px ) + '<STR_LIT>' + str ( py ) + '<STR_LIT>' + str ( pz ) + '<STR_LIT>' <EOL> img = nibabel . load ( template_absolute_path ) <EOL> data = img . get_data ( ) <EOL> hdr = img . get_header ( ) <EOL> data [ : , : , : ] = <NUM_LIT:0> <EOL> compteur = <NUM_LIT:1> <EOL> for i in average : <EOL> print int ( round ( nx / <NUM_LIT> ) ) , int ( round ( ny / <NUM_LIT> ) ) , int ( round ( i ) ) , int ( round ( compteur ) ) <EOL> data [ int ( round ( nx / <NUM_LIT> ) ) , int ( round ( ny / <NUM_LIT> ) ) , int ( round ( i ) ) ] = int ( round ( compteur ) ) <EOL> compteur = compteur + <NUM_LIT:1> <EOL> print '<STR_LIT>' <EOL> img = nibabel . Nifti1Image ( data , None , hdr ) <EOL> file_name = PATH_OUTPUT + '<STR_LIT>' <EOL> nibabel . save ( img , file_name ) <EOL> print '<STR_LIT>' + file_name <EOL> file_name = PATH_OUTPUT + '<STR_LIT>' <EOL> nibabel . save ( img , file_name ) <EOL> print '<STR_LIT>' + file_name <EOL> else : <EOL> print '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + contrast + '<STR_LIT:\n>' <EOL> os . chdir ( PATH_OUTPUT + '<STR_LIT>' + contrast ) <EOL> print '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + contrast + '<STR_LIT>' <EOL> template_shape = path_sct + '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + contrast + '<STR_LIT>' + template_shape + '<STR_LIT>' + str ( number_labels_for_template ) ) <EOL> def align_vertebrae ( contrast ) : <EOL> for i in range ( <NUM_LIT:0> , len ( SUBJECTS_LIST ) ) : <EOL> subject = SUBJECTS_LIST [ i ] [ <NUM_LIT:0> ] <EOL> print '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast + '<STR_LIT:\n>' <EOL> os . chdir ( PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast ) <EOL> print '<STR_LIT>' + subject + '<STR_LIT>' <EOL> sct . printv ( '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast + '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + contrast + '<STR_LIT>' + subject + '<STR_LIT>' ) <EOL> os . system ( '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT:/>' + contrast + '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + contrast + '<STR_LIT>' + subject + '<STR_LIT>' ) <EOL> sct . run ( '<STR_LIT>' + subject + '<STR_LIT>' + subject + '<STR_LIT>' ) <EOL> print '<STR_LIT>' + subject + '<STR_LIT>' + subject + '<STR_LIT>' <EOL> if not os . path . isdir ( PATH_OUTPUT + '<STR_LIT>' ) : <EOL> os . makedirs ( PATH_OUTPUT + '<STR_LIT>' ) <EOL> sct . run ( '<STR_LIT>' + subject + '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT>' + contrast + '<STR_LIT>' ) <EOL> print '<STR_LIT>' + PATH_OUTPUT + '<STR_LIT>' <EOL> if not os . path . isdir ( PATH_OUTPUT + '<STR_LIT>' ) : <EOL> os . makedirs ( PATH_OUTPUT + '<STR_LIT>' ) <EOL> f = nibabel . load ( PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT>' + contrast + '<STR_LIT>' ) <EOL> data = f . get_data ( ) <EOL> from msct_image import Image <EOL> nx , ny , nz , nt , px , py , pz , pt = Image ( PATH_OUTPUT + '<STR_LIT>' + subject + '<STR_LIT>' + contrast + '<STR_LIT>' ) . dim <EOL> sagital_middle = nx / <NUM_LIT:2> <EOL> coronal_middle = ny / <NUM_LIT:2> <EOL> sagittal = data [ sagital_middle , : , : ] . T <EOL> coronal = data [ : , coronal_middle , : ] . T <EOL> fig , ax = plt . subplots ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> ax [ <NUM_LIT:0> ] . imshow ( sagittal , cmap = '<STR_LIT>' , origin = '<STR_LIT>' ) <EOL> ax [ <NUM_LIT:0> ] . set_title ( '<STR_LIT>' ) <EOL> ax [ <NUM_LIT:1> ] . imshow ( coronal , cmap = '<STR_LIT>' , origin = '<STR_LIT>' ) <EOL> ax [ <NUM_LIT:1> ] . set_title ( '<STR_LIT>' ) <EOL> for i in range ( <NUM_LIT:2> ) : <EOL> ax [ i ] . set_axis_off ( ) <EOL> fig1 = plt . gcf ( ) <EOL> fig1 . savefig ( PATH_OUTPUT + '<STR_LIT>' + '<STR_LIT:/>' + subject + '<STR_LIT>' + contrast + '<STR_LIT>' , format = '<STR_LIT>' ) <EOL> timer [ contrast ] . one_subject_done ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
