<s> from msct_pca import PCA <EOL> from msct_gmseg_utils import * <EOL> import sct_utils as sct <EOL> from math import sqrt <EOL> from math import exp <EOL> class Param : <EOL> def __init__ ( self ) : <EOL> self . debug = <NUM_LIT:0> <EOL> self . path_dictionary = None <EOL> self . todo_model = None <EOL> self . reg = None <EOL> self . target_reg = None <EOL> self . level_fname = None <EOL> self . seg_type = None <EOL> self . verbose = <NUM_LIT:1> <EOL> class ModelDictionary : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dic_param = None ) : <EOL> """<STR_LIT>""" <EOL> if dic_param is None : <EOL> self . param = Param ( ) <EOL> else : <EOL> self . param = dic_param <EOL> self . slices = [ ] <EOL> self . J = <NUM_LIT:0> <EOL> self . N = <NUM_LIT:0> <EOL> self . mean_seg = None <EOL> self . mean_image = None <EOL> self . dic_data_info = { } <EOL> if self . param . reg is not None : <EOL> self . coregistration_transfos = self . param . reg <EOL> else : <EOL> self . coregistration_transfos = [ '<STR_LIT>' ] <EOL> suffix = '<STR_LIT>' <EOL> for transfo in self . coregistration_transfos : <EOL> suffix += '<STR_LIT:_>' + transfo <EOL> self . model_dic_name = '<STR_LIT>' <EOL> if self . param . todo_model == '<STR_LIT>' : <EOL> self . model_dic_name = '<STR_LIT>' + suffix <EOL> self . compute_model_dictionary ( ) <EOL> elif self . param . todo_model == '<STR_LIT>' : <EOL> self . model_dic_name = self . param . path_dictionary <EOL> self . load_model_dictionary ( ) <EOL> else : <EOL> sct . printv ( '<STR_LIT>' , self . param . verbose , '<STR_LIT>' ) <EOL> '''<STR_LIT>''' <EOL> '''<STR_LIT>''' <EOL> def compute_model_dictionary ( self ) : <EOL> """<STR_LIT>""" <EOL> sct . printv ( '<STR_LIT>' , self . param . verbose , '<STR_LIT>' ) <EOL> sct . printv ( '<STR_LIT>' , self . param . verbose , '<STR_LIT>' ) <EOL> sct . run ( '<STR_LIT>' + self . model_dic_name ) <EOL> self . slices = self . load_data_dictionary ( ) <EOL> self . J = len ( [ dic_slice . im for dic_slice in self . slices ] ) <EOL> self . N = len ( self . slices [ <NUM_LIT:0> ] . im . flatten ( ) ) <EOL> self . save_model_data ( '<STR_LIT>' ) <EOL> if self . param . seg_type != '<STR_LIT>' : <EOL> self . invert_seg ( ) <EOL> self . save_model_data ( '<STR_LIT>' ) <EOL> sct . printv ( '<STR_LIT>' , <EOL> self . param . verbose , '<STR_LIT>' ) <EOL> self . mean_seg = self . seg_coregistration ( transfo_to_apply = self . coregistration_transfos ) <EOL> self . save_model_data ( '<STR_LIT>' ) <EOL> sct . printv ( '<STR_LIT>' , self . param . verbose , '<STR_LIT>' ) <EOL> self . coregister_data ( transfo_to_apply = self . coregistration_transfos ) <EOL> '''<STR_LIT>''' <EOL> self . save_model_data ( '<STR_LIT>' ) <EOL> self . mean_image = self . compute_mean_dic_image ( np . asarray ( [ dic_slice . im_M for dic_slice in self . slices ] ) ) <EOL> save_image ( self . mean_image , '<STR_LIT>' , path = self . model_dic_name + '<STR_LIT:/>' , im_type = '<STR_LIT>' ) <EOL> def load_data_dictionary ( self ) : <EOL> """<STR_LIT>""" <EOL> slices = [ ] <EOL> j = <NUM_LIT:0> <EOL> for subject_dir in os . listdir ( self . param . path_dictionary ) : <EOL> subject_path = self . param . path_dictionary + '<STR_LIT:/>' + subject_dir <EOL> if os . path . isdir ( subject_path ) : <EOL> self . dic_data_info [ subject_dir ] = { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } <EOL> subject_seg_in = '<STR_LIT>' <EOL> subject_gm_seg = '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + self . model_dic_name + '<STR_LIT:/>' + subject_dir , verbose = self . param . verbose ) <EOL> for file_name in os . listdir ( subject_path ) : <EOL> if '<STR_LIT>' in file_name or '<STR_LIT>' in file_name : <EOL> subject_gm_seg = file_name <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in file_name and '<STR_LIT>' not in file_name . lower ( ) : <EOL> subject_seg_in = file_name <EOL> '''<STR_LIT>''' <EOL> im = Image ( subject_path + '<STR_LIT:/>' + subject_seg_in ) <EOL> seg = Image ( subject_path + '<STR_LIT:/>' + subject_gm_seg ) <EOL> for im_slice , seg_slice in zip ( im . data , seg . data ) : <EOL> self . dic_data_info [ subject_dir ] [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> slices . append ( Slice ( slice_id = j , im = im_slice , gm_seg = seg_slice , reg_to_m = [ ] ) ) <EOL> j += <NUM_LIT:1> <EOL> return np . asarray ( slices ) <EOL> def save_model_data ( self , what_to_save ) : <EOL> """<STR_LIT>""" <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ ] <EOL> total_n_slices = <NUM_LIT:0> <EOL> if what_to_save == '<STR_LIT>' : <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ dic_slice . gm_seg for dic_slice in self . slices ] <EOL> if what_to_save == '<STR_LIT>' : <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ dic_slice . wm_seg for dic_slice in self . slices ] <EOL> elif what_to_save == '<STR_LIT>' : <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ dic_slice . im for dic_slice in self . slices ] <EOL> elif what_to_save == '<STR_LIT>' : <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ dic_slice . im_M for dic_slice in self . slices ] <EOL> elif what_to_save == '<STR_LIT>' : <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ dic_slice . gm_seg_M for dic_slice in self . slices ] <EOL> elif what_to_save == '<STR_LIT>' : <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ dic_slice . wm_seg_M for dic_slice in self . slices ] <EOL> for subject_name in sorted ( self . dic_data_info . keys ( ) ) : <EOL> first_subject_slice = total_n_slices <EOL> last_subject_slice = first_subject_slice + self . dic_data_info [ subject_name ] [ '<STR_LIT>' ] <EOL> self . dic_data_info [ subject_name ] [ what_to_save ] = data_to_save [ first_subject_slice : last_subject_slice ] <EOL> total_n_slices += self . dic_data_info [ subject_name ] [ '<STR_LIT>' ] <EOL> for subject_name , info in zip ( self . dic_data_info . keys ( ) , self . dic_data_info . values ( ) ) : <EOL> for i , slice_i in enumerate ( info [ what_to_save ] ) : <EOL> to_save = Image ( param = np . asarray ( slice_i ) ) <EOL> to_save . path = self . model_dic_name + '<STR_LIT:/>' + subject_name + '<STR_LIT:/>' <EOL> to_save . file_name = subject_name + '<STR_LIT>' + str ( i ) + suffix <EOL> to_save . ext = '<STR_LIT>' <EOL> to_save . save ( type = '<STR_LIT>' ) <EOL> def invert_seg ( self ) : <EOL> """<STR_LIT>""" <EOL> for dic_slice in self . slices : <EOL> im_dic = Image ( param = dic_slice . im ) <EOL> sc = im_dic . copy ( ) <EOL> nz_coord_sc = sc . getNonZeroCoordinates ( ) <EOL> im_seg = Image ( param = dic_slice . gm_seg ) <EOL> nz_coord_d = im_seg . getNonZeroCoordinates ( ) <EOL> for coord in nz_coord_sc : <EOL> sc . data [ coord . x , coord . y ] = <NUM_LIT:1> <EOL> for coord in nz_coord_d : <EOL> im_seg . data [ coord . x , coord . y ] = <NUM_LIT:1> <EOL> inverted_slice_seg = np . absolute ( sc . data - im_seg . data ) . astype ( int ) <EOL> dic_slice . set ( wm_seg = inverted_slice_seg ) <EOL> def seg_coregistration ( self , transfo_to_apply = None ) : <EOL> """<STR_LIT>""" <EOL> if self . param . seg_type == '<STR_LIT>' : <EOL> current_mean_seg = compute_majority_vote_mean_seg ( np . asarray ( [ dic_slice . gm_seg for dic_slice in self . slices ] ) ) <EOL> else : <EOL> current_mean_seg = compute_majority_vote_mean_seg ( np . asarray ( [ dic_slice . wm_seg for dic_slice in self . slices ] ) ) <EOL> for transfo in transfo_to_apply : <EOL> sct . printv ( '<STR_LIT>' + transfo + '<STR_LIT>' , self . param . verbose , '<STR_LIT>' ) <EOL> current_mean_seg = self . find_coregistration ( mean_seg = current_mean_seg , transfo_type = transfo ) <EOL> '''<STR_LIT>''' <EOL> resulting_mean_seg = current_mean_seg <EOL> return resulting_mean_seg <EOL> '''<STR_LIT>''' <EOL> def find_coregistration ( self , mean_seg = None , transfo_type = '<STR_LIT>' , first = True ) : <EOL> """<STR_LIT>""" <EOL> for dic_slice in self . slices : <EOL> name_j_transform = '<STR_LIT>' + str ( dic_slice . id ) + find_ants_transfo_name ( transfo_type ) [ <NUM_LIT:0> ] <EOL> new_reg_list = dic_slice . reg_to_M . append ( name_j_transform ) <EOL> dic_slice . set ( reg_to_m = new_reg_list ) <EOL> if first : <EOL> if self . param . seg_type == '<STR_LIT>' : <EOL> seg_m = apply_ants_transfo ( mean_seg , dic_slice . gm_seg , transfo_name = name_j_transform , path = self . model_dic_name + '<STR_LIT:/>' , transfo_type = transfo_type ) <EOL> else : <EOL> seg_m = apply_ants_transfo ( mean_seg , dic_slice . wm_seg , transfo_name = name_j_transform , path = self . model_dic_name + '<STR_LIT:/>' , transfo_type = transfo_type ) <EOL> else : <EOL> if self . param . seg_type == '<STR_LIT>' : <EOL> seg_m = apply_ants_transfo ( mean_seg , dic_slice . gm_seg_M , transfo_name = name_j_transform , path = self . model_dic_name + '<STR_LIT:/>' , transfo_type = transfo_type ) <EOL> else : <EOL> seg_m = apply_ants_transfo ( mean_seg , dic_slice . wm_seg_M , transfo_name = name_j_transform , path = self . model_dic_name + '<STR_LIT:/>' , transfo_type = transfo_type ) <EOL> if self . param . seg_type == '<STR_LIT>' : <EOL> dic_slice . set ( gm_seg_m = seg_m . astype ( int ) ) <EOL> dic_slice . set ( gm_seg_m_flat = seg_m . flatten ( ) . astype ( int ) ) <EOL> else : <EOL> dic_slice . set ( wm_seg_m = seg_m . astype ( int ) ) <EOL> dic_slice . set ( wm_seg_m_flat = seg_m . flatten ( ) . astype ( int ) ) <EOL> if self . param . seg_type == '<STR_LIT>' : <EOL> mean_seg = compute_majority_vote_mean_seg ( [ dic_slice . gm_seg_M for dic_slice in self . slices ] ) <EOL> else : <EOL> mean_seg = compute_majority_vote_mean_seg ( [ dic_slice . wm_seg_M for dic_slice in self . slices ] ) <EOL> save_image ( mean_seg , '<STR_LIT>' , path = self . model_dic_name + '<STR_LIT:/>' , im_type = '<STR_LIT>' ) <EOL> return mean_seg <EOL> '''<STR_LIT>''' <EOL> def compute_mean_dic_image ( self , im_data_set ) : <EOL> """<STR_LIT>""" <EOL> mean = np . sum ( im_data_set , axis = <NUM_LIT:0> ) <EOL> mean /= float ( self . J ) <EOL> return mean <EOL> def coregister_data ( self , transfo_to_apply = None ) : <EOL> """<STR_LIT>""" <EOL> list_im = [ dic_slice . im for dic_slice in self . slices ] <EOL> list_gm_seg = [ dic_slice . gm_seg for dic_slice in self . slices ] <EOL> for dic_slice in self . slices : <EOL> for n_transfo , transfo in enumerate ( transfo_to_apply ) : <EOL> im_m = apply_ants_transfo ( self . compute_mean_dic_image ( list_im ) , dic_slice . im , search_reg = False , transfo_name = dic_slice . reg_to_M [ n_transfo ] , binary = False , path = self . model_dic_name + '<STR_LIT:/>' , transfo_type = transfo ) <EOL> if self . param . seg_type == '<STR_LIT>' : <EOL> gm_seg_m = apply_ants_transfo ( self . compute_mean_dic_image ( list_gm_seg ) , dic_slice . gm_seg , search_reg = False , transfo_name = dic_slice . reg_to_M [ n_transfo ] , binary = False , path = self . model_dic_name + '<STR_LIT:/>' , transfo_type = transfo ) <EOL> dic_slice . set ( im_m = im_m ) <EOL> dic_slice . set ( im_m_flat = im_m . flatten ( ) ) <EOL> if self . param . seg_type == '<STR_LIT>' : <EOL> dic_slice . set ( gm_seg_m = gm_seg_m ) <EOL> dic_slice . set ( gm_seg_m_flat = gm_seg_m . flatten ( ) ) <EOL> '''<STR_LIT>''' <EOL> '''<STR_LIT>''' <EOL> def load_model_dictionary ( self ) : <EOL> """<STR_LIT>""" <EOL> import itertools <EOL> sct . printv ( '<STR_LIT>' , self . param . verbose , '<STR_LIT>' ) <EOL> j = <NUM_LIT:0> <EOL> for subject_dir in os . listdir ( self . model_dic_name ) : <EOL> subject_path = self . model_dic_name + subject_dir <EOL> if os . path . isdir ( subject_path ) and '<STR_LIT>' not in subject_path : <EOL> subject_im = None <EOL> im_mat = None <EOL> subject_seg = None <EOL> seg_mat = None <EOL> subject_im_m = None <EOL> im_m = None <EOL> subject_seg_m = None <EOL> seg_m = None <EOL> for file_name in os . listdir ( subject_path ) : <EOL> if '<STR_LIT>' in file_name : <EOL> subject_im = file_name <EOL> if '<STR_LIT>' in file_name : <EOL> subject_seg = file_name <EOL> if '<STR_LIT>' in file_name : <EOL> subject_im_m = file_name <EOL> if '<STR_LIT>' in file_name : <EOL> subject_seg_m = file_name <EOL> if subject_im is not None : <EOL> im = Image ( subject_path + '<STR_LIT:/>' + subject_im ) <EOL> im_mat = im . data <EOL> if subject_seg is not None : <EOL> seg = Image ( subject_path + '<STR_LIT:/>' + subject_seg ) <EOL> seg_mat = seg . data <EOL> if subject_im_m is not None : <EOL> im_m = Image ( subject_path + '<STR_LIT:/>' + subject_im_m ) <EOL> else : <EOL> sct . printv ( '<STR_LIT>' + subject_dir , <EOL> self . param . verbose , '<STR_LIT>' ) <EOL> if subject_seg_m is not None : <EOL> seg_m = Image ( subject_path + '<STR_LIT:/>' + subject_seg_m ) <EOL> else : <EOL> sct . printv ( '<STR_LIT>' + subject_dir , <EOL> self . param . verbose , '<STR_LIT>' ) <EOL> if im_mat is None : <EOL> im_mat = itertools . repeat ( None , im_m . data . shape [ <NUM_LIT:0> ] ) <EOL> if seg_mat is None : <EOL> seg_mat = itertools . repeat ( None , im_m . data . shape [ <NUM_LIT:0> ] ) <EOL> for im_slice , seg_slice , im_m_slice , seg_m_slice in zip ( im_mat , seg_mat , im_m . data , seg_m . data ) : <EOL> reg_list = [ '<STR_LIT>' + str ( j ) + find_ants_transfo_name ( transfo_type ) [ <NUM_LIT:0> ] <EOL> for transfo_type in self . coregistration_transfos ] <EOL> self . slices . append ( Slice ( slice_id = j , im = im_slice , seg = seg_slice , im_m = im_m_slice , seg_m = seg_m_slice , <EOL> im_m_flat = im_m_slice . flatten ( ) , seg_m_flat = seg_m_slice . flatten ( ) , <EOL> reg_to_m = reg_list ) ) <EOL> j += <NUM_LIT:1> <EOL> self . J = len ( self . slices ) <EOL> self . N = len ( self . slices [ <NUM_LIT:0> ] . im_M . flatten ( ) ) <EOL> self . mean_image = Image ( self . model_dic_name + '<STR_LIT>' ) . data <EOL> self . mean_seg = Image ( self . model_dic_name + '<STR_LIT>' ) . data <EOL> def show_data ( self ) : <EOL> """<STR_LIT>""" <EOL> for dic_slice in self . slices [ : <NUM_LIT:10> ] : <EOL> fig = plt . figure ( ) <EOL> seg_subplot = fig . add_subplot ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ) <EOL> seg_subplot . set_title ( '<STR_LIT>' ) <EOL> im_seg = seg_subplot . imshow ( dic_slice . wm_seg ) <EOL> im_seg . set_interpolation ( '<STR_LIT>' ) <EOL> im_seg . set_cmap ( '<STR_LIT>' ) <EOL> seg_m_subplot = fig . add_subplot ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> seg_m_subplot . set_title ( '<STR_LIT>' ) <EOL> im_seg_m = seg_m_subplot . imshow ( dic_slice . wm_seg_M ) <EOL> im_seg_m . set_interpolation ( '<STR_LIT>' ) <EOL> im_seg_m . set_cmap ( '<STR_LIT>' ) <EOL> mean_seg_subplot = fig . add_subplot ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> mean_seg_subplot . set_title ( '<STR_LIT>' ) <EOL> im_mean_seg = mean_seg_subplot . imshow ( np . asarray ( self . mean_seg ) ) <EOL> im_mean_seg . set_interpolation ( '<STR_LIT>' ) <EOL> im_mean_seg . set_cmap ( '<STR_LIT>' ) <EOL> slice_im_subplot = fig . add_subplot ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> slice_im_subplot . set_title ( '<STR_LIT>' ) <EOL> im_slice_im = slice_im_subplot . imshow ( dic_slice . im ) <EOL> im_slice_im . set_interpolation ( '<STR_LIT>' ) <EOL> im_slice_im . set_cmap ( '<STR_LIT>' ) <EOL> slice_im_m_subplot = fig . add_subplot ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:5> ) <EOL> slice_im_m_subplot . set_title ( '<STR_LIT>' ) <EOL> im_slice_im_m = slice_im_m_subplot . imshow ( dic_slice . im_M ) <EOL> im_slice_im_m . set_interpolation ( '<STR_LIT>' ) <EOL> im_slice_im_m . set_cmap ( '<STR_LIT>' ) <EOL> plt . suptitle ( '<STR_LIT>' + str ( dic_slice . id ) ) <EOL> plt . show ( ) <EOL> class ModelDictionaryBySlice : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dic_param = None ) : <EOL> """<STR_LIT>""" <EOL> if dic_param is None : <EOL> self . param = Param ( ) <EOL> else : <EOL> self . param = dic_param <EOL> self . level_label = { <NUM_LIT:0> : '<STR_LIT>' , <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:2> : '<STR_LIT>' , <NUM_LIT:3> : '<STR_LIT>' , <NUM_LIT:4> : '<STR_LIT>' , <NUM_LIT:5> : '<STR_LIT>' , <NUM_LIT:6> : '<STR_LIT>' , <NUM_LIT:7> : '<STR_LIT>' , <NUM_LIT:8> : '<STR_LIT>' , <NUM_LIT:9> : '<STR_LIT>' , <EOL> <NUM_LIT:10> : '<STR_LIT>' , <NUM_LIT:11> : '<STR_LIT>' , <NUM_LIT:12> : '<STR_LIT>' , <NUM_LIT> : '<STR_LIT>' } <EOL> self . slices = [ ] <EOL> self . J = <NUM_LIT:0> <EOL> self . N = <NUM_LIT:0> <EOL> self . mean_seg = None <EOL> self . mean_image = None <EOL> self . dic_data_info = { } <EOL> if self . param . reg is not None : <EOL> self . coregistration_transfos = self . param . reg <EOL> else : <EOL> self . coregistration_transfos = [ '<STR_LIT>' ] <EOL> suffix = '<STR_LIT>' <EOL> for transfo in self . coregistration_transfos : <EOL> suffix += '<STR_LIT:_>' + transfo <EOL> suffix += '<STR_LIT:_>' + self . param . seg_type [ <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> self . model_dic_name = '<STR_LIT>' <EOL> if self . param . todo_model == '<STR_LIT>' : <EOL> self . model_dic_name = '<STR_LIT>' + suffix <EOL> self . compute_model_dictionary ( ) <EOL> elif self . param . todo_model == '<STR_LIT>' : <EOL> self . model_dic_name = self . param . path_dictionary <EOL> self . load_model_dictionary ( ) <EOL> else : <EOL> sct . printv ( '<STR_LIT>' , self . param . verbose , '<STR_LIT>' ) <EOL> def compute_model_dictionary ( self ) : <EOL> """<STR_LIT>""" <EOL> sct . printv ( '<STR_LIT>' , self . param . verbose , '<STR_LIT>' ) <EOL> sct . printv ( '<STR_LIT>' , self . param . verbose , '<STR_LIT>' ) <EOL> sct . run ( '<STR_LIT>' + self . model_dic_name ) <EOL> self . slices = self . load_data_dictionary ( ) <EOL> self . J = len ( [ dic_slice . im for dic_slice in self . slices ] ) <EOL> self . N = len ( self . slices [ <NUM_LIT:0> ] . im . flatten ( ) ) <EOL> if self . param . seg_type != '<STR_LIT>' : <EOL> self . invert_seg ( ) <EOL> self . save_model_data ( '<STR_LIT>' ) <EOL> sct . printv ( '<STR_LIT>' , <EOL> self . param . verbose , '<STR_LIT>' ) <EOL> self . mean_seg = self . seg_coregistration ( transfo_to_apply = self . coregistration_transfos ) <EOL> if self . param . seg_type == '<STR_LIT>' : <EOL> self . save_model_data ( '<STR_LIT>' ) <EOL> else : <EOL> self . save_model_data ( '<STR_LIT>' ) <EOL> sct . printv ( '<STR_LIT>' , self . param . verbose , '<STR_LIT>' ) <EOL> self . coregister_data ( transfo_to_apply = self . coregistration_transfos ) <EOL> self . save_model_data ( '<STR_LIT>' ) <EOL> self . mean_image = self . compute_mean_dic_image ( np . asarray ( [ dic_slice . im_M for dic_slice in self . slices ] ) ) <EOL> save_image ( self . mean_image , '<STR_LIT>' , path = self . model_dic_name + '<STR_LIT:/>' , im_type = '<STR_LIT>' ) <EOL> def load_data_dictionary ( self ) : <EOL> """<STR_LIT>""" <EOL> slices = [ ] <EOL> total_j_im = <NUM_LIT:0> <EOL> total_j_seg = <NUM_LIT:0> <EOL> for subject_dir in os . listdir ( self . param . path_dictionary ) : <EOL> subject_path = self . param . path_dictionary + '<STR_LIT:/>' + subject_dir <EOL> if os . path . isdir ( subject_path ) : <EOL> self . dic_data_info [ subject_dir ] = { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } <EOL> j_im = <NUM_LIT:0> <EOL> j_seg = <NUM_LIT:0> <EOL> first_slice = total_j_im <EOL> sct . run ( '<STR_LIT>' + self . model_dic_name + '<STR_LIT:/>' + subject_dir , verbose = self . param . verbose ) <EOL> for file_name in os . listdir ( subject_path ) : <EOL> if '<STR_LIT>' in file_name : <EOL> slice_level = <NUM_LIT:0> <EOL> name_list = file_name . split ( '<STR_LIT:_>' ) <EOL> for word in name_list : <EOL> if word in self . level_label . values ( ) : <EOL> slice_level = get_key_from_val ( self . level_label , word ) <EOL> slices . append ( Slice ( slice_id = total_j_im , im = Image ( subject_path + '<STR_LIT:/>' + file_name ) . data , level = slice_level , reg_to_m = [ ] ) ) <EOL> self . dic_data_info [ subject_dir ] [ '<STR_LIT>' ] . append ( slice_level ) <EOL> if j_im < <NUM_LIT:10> : <EOL> j_im_str = str ( j_im ) <EOL> j_im_str = '<STR_LIT:0>' + j_im_str <EOL> else : <EOL> j_im_str = str ( j_im ) <EOL> im_file_name = subject_dir + '<STR_LIT>' + j_im_str + '<STR_LIT:_>' + self . level_label [ slice_level ] + '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + self . param . path_dictionary + '<STR_LIT:/>' + subject_dir + '<STR_LIT:/>' + file_name + '<STR_LIT:U+0020>' + self . model_dic_name + '<STR_LIT:/>' + subject_dir + '<STR_LIT:/>' + im_file_name ) <EOL> j_im += <NUM_LIT:1> <EOL> total_j_im += <NUM_LIT:1> <EOL> if '<STR_LIT>' in file_name : <EOL> slices [ total_j_seg ] . set ( gm_seg = Image ( subject_path + '<STR_LIT:/>' + file_name ) . data ) <EOL> j_seg += <NUM_LIT:1> <EOL> total_j_seg += <NUM_LIT:1> <EOL> if j_im == j_seg : <EOL> self . dic_data_info [ subject_dir ] [ '<STR_LIT>' ] = ( first_slice , total_j_im ) <EOL> else : <EOL> sct . printv ( '<STR_LIT>' + subject_dir + '<STR_LIT>' , verbose = self . param . verbose , type = '<STR_LIT:error>' ) <EOL> return np . asarray ( slices ) <EOL> def save_model_data ( self , what_to_save ) : <EOL> """<STR_LIT>""" <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ ] <EOL> if what_to_save == '<STR_LIT>' : <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ dic_slice . gm_seg for dic_slice in self . slices ] <EOL> if what_to_save == '<STR_LIT>' : <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ dic_slice . wm_seg for dic_slice in self . slices ] <EOL> elif what_to_save == '<STR_LIT>' : <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ dic_slice . im for dic_slice in self . slices ] <EOL> elif what_to_save == '<STR_LIT>' : <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ dic_slice . im_M for dic_slice in self . slices ] <EOL> elif what_to_save == '<STR_LIT>' : <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ dic_slice . gm_seg_M for dic_slice in self . slices ] <EOL> elif what_to_save == '<STR_LIT>' : <EOL> suffix = '<STR_LIT>' <EOL> data_to_save = [ dic_slice . wm_seg_M for dic_slice in self . slices ] <EOL> for subject_name in sorted ( self . dic_data_info . keys ( ) ) : <EOL> first_subject_slice , last_subject_slice = self . dic_data_info [ subject_name ] [ '<STR_LIT>' ] <EOL> self . dic_data_info [ subject_name ] [ what_to_save ] = data_to_save [ first_subject_slice : last_subject_slice ] <EOL> for i , slice_i in enumerate ( self . dic_data_info [ subject_name ] [ what_to_save ] ) : <EOL> to_save = Image ( param = np . asarray ( slice_i ) ) <EOL> to_save . path = self . model_dic_name + '<STR_LIT:/>' + subject_name + '<STR_LIT:/>' <EOL> if i < <NUM_LIT:10> : <EOL> i_str = str ( i ) <EOL> i_str = '<STR_LIT:0>' + i_str <EOL> else : <EOL> i_str = str ( i ) <EOL> to_save . file_name = subject_name + '<STR_LIT>' + i_str + '<STR_LIT:_>' + self . level_label [ self . dic_data_info [ subject_name ] [ '<STR_LIT>' ] [ i ] ] + suffix <EOL> to_save . ext = '<STR_LIT>' <EOL> to_save . save ( type = '<STR_LIT>' ) <EOL> def invert_seg ( self ) : <EOL> """<STR_LIT>""" <EOL> for dic_slice in self . slices : <EOL> im_dic = Image ( param = dic_slice . im ) <EOL> sc = im_dic . copy ( ) <EOL> nz_coord_sc = sc . getNonZeroCoordinates ( ) <EOL> im_seg = Image ( param = dic_slice . gm_seg ) <EOL> nz_coord_d = im_seg . getNonZeroCoordinates ( ) <EOL> for coord in nz_coord_sc : <EOL> sc . data [ coord . x , coord . y ] = <NUM_LIT:1> <EOL> for coord in nz_coord_d : <EOL> im_seg . data [ coord . x , coord . y ] = <NUM_LIT:1> <EOL> inverted_slice_decision = np . absolute ( sc . data - im_seg . data ) . astype ( int ) <EOL> dic_slice . set ( wm_seg = inverted_slice_decision ) <EOL> def seg_coregistration ( self , transfo_to_apply = None ) : <EOL> """<STR_LIT>""" <EOL> if self . param . seg_type == '<STR_LIT>' : <EOL> current_mean_seg = compute_majority_vote_mean_seg ( np . asarray ( [ dic_slice . gm_seg for dic_slice in self . slices ] ) ) <EOL> else : <EOL> current_mean_seg = compute_majority_vote_mean_seg ( np . asarray ( [ dic_slice . wm_seg for dic_slice in self . slices ] ) ) <EOL> for transfo in transfo_to_apply : <EOL> sct . printv ( '<STR_LIT>' + transfo + '<STR_LIT>' , self . param . verbose , '<STR_LIT>' ) <EOL> current_mean_seg = self . find_coregistration ( mean_seg = current_mean_seg , transfo_type = transfo ) <EOL> resulting_mean_seg = current_mean_seg <EOL> return resulting_mean_seg <EOL> def find_coregistration ( self , mean_seg = None , transfo_type = '<STR_LIT>' , first = True ) : <EOL> """<STR_LIT>""" <EOL> if self . param . seg_type == '<STR_LIT>' : <EOL> for dic_slice in self . slices : <EOL> name_j_transform = '<STR_LIT>' + str ( dic_slice . id ) + find_ants_transfo_name ( transfo_type ) [ <NUM_LIT:0> ] <EOL> new_reg_list = dic_slice . reg_to_M . append ( name_j_transform ) <EOL> dic_slice . set ( reg_to_m = new_reg_list ) <EOL> if first : <EOL> seg_m = apply_ants_transfo ( mean_seg , dic_slice . gm_seg , transfo_name = name_j_transform , path = self . model_dic_name + '<STR_LIT:/>' , transfo_type = transfo_type ) <EOL> else : <EOL> seg_m = apply_ants_transfo ( mean_seg , dic_slice . gm_seg_M , transfo_name = name_j_transform , path = self . model_dic_name + '<STR_LIT:/>' , transfo_type = transfo_type ) <EOL> dic_slice . set ( gm_seg_m = seg_m . astype ( int ) ) <EOL> dic_slice . set ( gm_seg_m_flat = seg_m . flatten ( ) . astype ( int ) ) <EOL> mean_seg = compute_majority_vote_mean_seg ( [ dic_slice . gm_seg_M for dic_slice in self . slices ] ) <EOL> else : <EOL> for dic_slice in self . slices : <EOL> name_j_transform = '<STR_LIT>' + str ( dic_slice . id ) + find_ants_transfo_name ( transfo_type ) [ <NUM_LIT:0> ] <EOL> new_reg_list = dic_slice . reg_to_M . append ( name_j_transform ) <EOL> dic_slice . set ( reg_to_m = new_reg_list ) <EOL> if first : <EOL> seg_m = apply_ants_transfo ( mean_seg , dic_slice . wm_seg , transfo_name = name_j_transform , path = self . model_dic_name + '<STR_LIT:/>' , transfo_type = transfo_type ) <EOL> else : <EOL> seg_m = apply_ants_transfo ( mean_seg , dic_slice . wm_seg_M , transfo_name = name_j_transform , path = self . model_dic_name + '<STR_LIT:/>' , transfo_type = transfo_type ) <EOL> dic_slice . set ( wm_seg_m = seg_m . astype ( int ) ) <EOL> dic_slice . set ( wm_seg_m_flat = seg_m . flatten ( ) . astype ( int ) ) <EOL> mean_seg = compute_majority_vote_mean_seg ( [ dic_slice . wm_seg_M for dic_slice in self . slices ] ) <EOL> save_image ( mean_seg , '<STR_LIT>' , path = self . model_dic_name + '<STR_LIT:/>' , im_type = '<STR_LIT>' ) <EOL> return mean_seg <EOL> def compute_mean_dic_image ( self , im_data_set ) : <EOL> """<STR_LIT>""" <EOL> mean = np . sum ( im_data_set , axis = <NUM_LIT:0> ) <EOL> mean /= float ( self . J ) <EOL> return mean <EOL> def coregister_data ( self , transfo_to_apply = None ) : <EOL> """<STR_LIT>""" <EOL> list_im = [ dic_slice . im for dic_slice in self . slices ] <EOL> list_gm_seg = [ dic_slice . gm_seg for dic_slice in self . slices ] <EOL> for dic_slice in self . slices : <EOL> for n_transfo , transfo in enumerate ( transfo_to_apply ) : <EOL> im_m = apply_ants_transfo ( self . compute_mean_dic_image ( list_im ) , dic_slice . im , search_reg = False , transfo_name = dic_slice . reg_to_M [ n_transfo ] , binary = False , path = self . model_dic_name + '<STR_LIT:/>' , transfo_type = transfo ) <EOL> if self . param . seg_type == '<STR_LIT>' : <EOL> gm_seg_m = apply_ants_transfo ( self . compute_mean_dic_image ( list_gm_seg ) , dic_slice . gm_seg , search_reg = False , transfo_name = dic_slice . reg_to_M [ n_transfo ] , binary = False , path = self . model_dic_name + '<STR_LIT:/>' , transfo_type = transfo ) <EOL> dic_slice . set ( im_m = im_m ) <EOL> dic_slice . set ( im_m_flat = im_m . flatten ( ) ) <EOL> if self . param . seg_type == '<STR_LIT>' : <EOL> dic_slice . set ( gm_seg_m = gm_seg_m ) <EOL> dic_slice . set ( gm_seg_m_flat = gm_seg_m . flatten ( ) ) <EOL> def load_model_dictionary ( self ) : <EOL> """<STR_LIT>""" <EOL> import itertools <EOL> sct . printv ( '<STR_LIT>' , self . param . verbose , '<STR_LIT>' ) <EOL> j_im = <NUM_LIT:0> <EOL> j_seg = <NUM_LIT:0> <EOL> j_im_m = <NUM_LIT:0> <EOL> j_seg_m = <NUM_LIT:0> <EOL> for subject_dir in os . listdir ( self . model_dic_name ) : <EOL> subject_path = self . model_dic_name + '<STR_LIT:/>' + subject_dir <EOL> if os . path . isdir ( subject_path ) and '<STR_LIT>' not in subject_path : <EOL> for file_name in os . listdir ( subject_path ) : <EOL> if '<STR_LIT>' in file_name : <EOL> reg_list = [ '<STR_LIT>' + str ( j_im ) + find_ants_transfo_name ( transfo_type ) [ <NUM_LIT:0> ] <EOL> for transfo_type in self . coregistration_transfos ] <EOL> slice_level = <NUM_LIT:0> <EOL> name_list = file_name . split ( '<STR_LIT:_>' ) <EOL> for word in name_list : <EOL> if word in self . level_label . values ( ) : <EOL> slice_level = get_key_from_val ( self . level_label , word ) <EOL> self . slices . append ( Slice ( slice_id = j_im , im = Image ( subject_path + '<STR_LIT:/>' + file_name ) . data , <EOL> level = slice_level , reg_to_m = reg_list ) ) <EOL> j_im += <NUM_LIT:1> <EOL> if '<STR_LIT>' in file_name : <EOL> self . slices [ j_seg ] . set ( seg = Image ( subject_path + '<STR_LIT:/>' + file_name ) . data ) <EOL> j_seg += <NUM_LIT:1> <EOL> if '<STR_LIT>' in file_name : <EOL> im_m_slice = Image ( subject_path + '<STR_LIT:/>' + file_name ) . data <EOL> self . slices [ j_im_m ] . set ( im_m = im_m_slice , im_m_flat = im_m_slice . flatten ( ) ) <EOL> j_im_m += <NUM_LIT:1> <EOL> if '<STR_LIT>' in file_name : <EOL> seg_m_slice = Image ( subject_path + '<STR_LIT:/>' + file_name ) . data <EOL> self . slices [ j_seg_m ] . set ( seg_m = seg_m_slice , seg_m_flat = seg_m_slice . flatten ( ) ) <EOL> j_seg_m += <NUM_LIT:1> <EOL> self . J = len ( self . slices ) <EOL> self . N = len ( self . slices [ <NUM_LIT:0> ] . im_M . flatten ( ) ) <EOL> self . mean_image = Image ( self . model_dic_name + '<STR_LIT>' ) . data <EOL> self . mean_seg = Image ( self . model_dic_name + '<STR_LIT>' ) . data <EOL> def show_data ( self ) : <EOL> """<STR_LIT>""" <EOL> for dic_slice in self . slices [ : <NUM_LIT:10> ] : <EOL> fig = plt . figure ( ) <EOL> seg_subplot = fig . add_subplot ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ) <EOL> seg_subplot . set_title ( '<STR_LIT>' ) <EOL> im_seg = seg_subplot . imshow ( dic_slice . wm_seg ) <EOL> im_seg . set_interpolation ( '<STR_LIT>' ) <EOL> im_seg . set_cmap ( '<STR_LIT>' ) <EOL> seg_m_subplot = fig . add_subplot ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> seg_m_subplot . set_title ( '<STR_LIT>' ) <EOL> im_seg_m = seg_m_subplot . imshow ( dic_slice . wm_seg_M ) <EOL> im_seg_m . set_interpolation ( '<STR_LIT>' ) <EOL> im_seg_m . set_cmap ( '<STR_LIT>' ) <EOL> mean_seg_subplot = fig . add_subplot ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> mean_seg_subplot . set_title ( '<STR_LIT>' ) <EOL> im_mean_seg = mean_seg_subplot . imshow ( np . asarray ( self . mean_seg ) ) <EOL> im_mean_seg . set_interpolation ( '<STR_LIT>' ) <EOL> im_mean_seg . set_cmap ( '<STR_LIT>' ) <EOL> slice_im_subplot = fig . add_subplot ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> slice_im_subplot . set_title ( '<STR_LIT>' ) <EOL> im_slice_im = slice_im_subplot . imshow ( dic_slice . im ) <EOL> im_slice_im . set_interpolation ( '<STR_LIT>' ) <EOL> im_slice_im . set_cmap ( '<STR_LIT>' ) <EOL> slice_im_m_subplot = fig . add_subplot ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:5> ) <EOL> slice_im_m_subplot . set_title ( '<STR_LIT>' ) <EOL> im_slice_im_m = slice_im_m_subplot . imshow ( dic_slice . im_M ) <EOL> im_slice_im_m . set_interpolation ( '<STR_LIT>' ) <EOL> im_slice_im_m . set_cmap ( '<STR_LIT>' ) <EOL> plt . suptitle ( '<STR_LIT>' + str ( dic_slice . id ) ) <EOL> plt . show ( ) <EOL> class Model : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model_param = None , dictionary = None , k = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if model_param is None : <EOL> if dictionary is not None : <EOL> self . param = dictionary . param <EOL> else : <EOL> self . param = Param ( ) <EOL> else : <EOL> self . param = model_param <EOL> self . dictionary = dictionary <EOL> sct . printv ( "<STR_LIT>" <EOL> "<STR_LIT:(>" + str ( self . dictionary . N ) + "<STR_LIT:U+002C>" + str ( self . dictionary . J ) + "<STR_LIT:)>" , verbose = self . param . verbose ) <EOL> sct . printv ( '<STR_LIT>' , self . param . verbose , '<STR_LIT>' ) <EOL> if self . param . todo_model == '<STR_LIT>' : <EOL> self . pca = PCA ( np . asarray ( self . dictionary . slices ) , k = k ) <EOL> self . dictionary . mean_image = self . pca . mean_image <EOL> save_image ( self . pca . mean_image , '<STR_LIT>' , path = self . dictionary . model_dic_name + '<STR_LIT:/>' ) <EOL> self . pca . save_data ( self . dictionary . model_dic_name ) <EOL> elif self . param . todo_model == '<STR_LIT>' : <EOL> pca_mean_data , pca_eig_pairs = self . load_pca_file ( ) <EOL> self . pca = PCA ( np . asarray ( self . dictionary . slices ) , mean_vect = pca_mean_data , eig_pairs = pca_eig_pairs , k = k ) <EOL> if self . param . verbose == <NUM_LIT:2> : <EOL> self . pca . plot_projected_dic ( ) <EOL> def load_pca_file ( self , file_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> fic_data_pca = open ( self . param . path_dictionary + '<STR_LIT:/>' + file_name , '<STR_LIT:r>' ) <EOL> mean_data_list = fic_data_pca . readline ( ) . split ( '<STR_LIT:U+002C>' ) <EOL> eig_pairs_list = fic_data_pca . readline ( ) . split ( '<STR_LIT:U+002C>' ) <EOL> fic_data_pca . close ( ) <EOL> mean_data_vect = [ ] <EOL> for val in mean_data_list : <EOL> mean_data_vect . append ( [ float ( val ) ] ) <EOL> mean_data_vect = np . asarray ( mean_data_vect ) <EOL> eig_pairs_vect = [ ] <EOL> for pair in eig_pairs_list : <EOL> eig_val_str , eig_vect_str = pair . split ( '<STR_LIT:;>' ) <EOL> eig_vect_str = eig_vect_str . split ( '<STR_LIT:U+0020>' ) <EOL> eig_vect = [ ] <EOL> for i , v in enumerate ( eig_vect_str ) : <EOL> if v != '<STR_LIT>' and v != '<STR_LIT:\n>' : <EOL> eig_vect . append ( float ( v ) ) <EOL> eig_pairs_vect . append ( ( float ( eig_val_str ) , eig_vect ) ) <EOL> return mean_data_vect , eig_pairs_vect <EOL> class TargetSegmentationPairwise : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model , target_image = None , levels_image = None , tau = None ) : <EOL> """<STR_LIT>""" <EOL> self . model = model <EOL> if len ( target_image . data . shape ) == <NUM_LIT:3> : <EOL> self . target = [ Slice ( slice_id = i_slice , im = target_slice ) for i_slice , target_slice in enumerate ( target_image . data ) ] <EOL> elif len ( target_image . data . shape ) == <NUM_LIT:2> : <EOL> self . target = [ Slice ( slice_id = <NUM_LIT:0> , im = target_image . data ) ] <EOL> if isinstance ( levels_image , Image ) : <EOL> nz_coord = levels_image . getNonZeroCoordinates ( ) <EOL> for i_level_slice , level_slice in enumerate ( levels_image . data ) : <EOL> nz_val = [ ] <EOL> for coord in nz_coord : <EOL> if coord . x == i_level_slice : <EOL> nz_val . append ( level_slice [ coord . y , coord . z ] ) <EOL> try : <EOL> self . target [ i_level_slice ] . set ( level = int ( round ( sum ( nz_val ) / len ( nz_val ) ) ) ) <EOL> except ZeroDivisionError : <EOL> sct . printv ( '<STR_LIT>' + str ( i_level_slice ) + '<STR_LIT>' ) <EOL> self . target [ i_level_slice ] . set ( level = <NUM_LIT:0> ) <EOL> elif isinstance ( levels_image , str ) : <EOL> self . target [ <NUM_LIT:0> ] . set ( level = get_key_from_val ( self . model . dictionary . level_label , levels_image ) ) <EOL> self . target_pairwise_registration ( ) <EOL> sct . printv ( '<STR_LIT>' , model . param . verbose , '<STR_LIT>' ) <EOL> self . coord_projected_target = model . pca . project ( [ target_slice . im_M for target_slice in self . target ] ) <EOL> self . epsilon = round ( <NUM_LIT:1.0> / self . model . dictionary . J , <NUM_LIT:4> ) / <NUM_LIT:2> <EOL> print '<STR_LIT>' , self . epsilon <EOL> if tau is None : <EOL> self . tau = self . compute_tau ( ) <EOL> else : <EOL> self . tau = tau <EOL> if levels_image is not None : <EOL> self . beta = self . compute_beta ( self . coord_projected_target , target_levels = np . asarray ( [ target_slice . level for target_slice in self . target ] ) , tau = self . tau ) <EOL> else : <EOL> self . beta = self . compute_beta ( self . coord_projected_target , tau = self . tau ) <EOL> sct . printv ( '<STR_LIT>' , model . param . verbose , '<STR_LIT>' ) <EOL> self . selected_k_slices = self . select_k_slices ( self . beta ) <EOL> slice_levels = np . asarray ( [ self . model . dictionary . level_label [ dic_slice . level ] for dic_slice in self . model . dictionary . slices ] ) <EOL> fic_selected_slices = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> fic_selected_slices . write ( str ( slice_levels [ self . selected_k_slices . reshape ( self . model . dictionary . J , ) ] ) ) <EOL> fic_selected_slices . close ( ) <EOL> sct . printv ( '<STR_LIT>' , model . param . verbose , '<STR_LIT>' ) <EOL> self . label_fusion ( self . selected_k_slices ) <EOL> sct . printv ( '<STR_LIT>' , <EOL> model . param . verbose , '<STR_LIT>' ) <EOL> self . target_pairwise_registration ( inverse = True ) <EOL> def target_pairwise_registration ( self , inverse = False ) : <EOL> """<STR_LIT>""" <EOL> if not inverse : <EOL> target_model_space = [ ] <EOL> mean_dic_im = self . model . pca . mean_image <EOL> for i , target_slice in enumerate ( self . target ) : <EOL> moving_target_slice = target_slice . im <EOL> for transfo in self . model . dictionary . coregistration_transfos : <EOL> transfo_name = transfo + '<STR_LIT>' + str ( i ) + find_ants_transfo_name ( transfo ) [ <NUM_LIT:0> ] <EOL> moving_target_slice = apply_ants_transfo ( mean_dic_im , moving_target_slice , binary = False , transfo_type = transfo , transfo_name = transfo_name ) <EOL> self . target [ i ] . set ( im_m = moving_target_slice ) <EOL> else : <EOL> res_seg = [ ] <EOL> for i , target_slice in enumerate ( self . target ) : <EOL> if self . model . param . seg_type == '<STR_LIT>' : <EOL> moving_seg_slice = target_slice . wm_seg_M <EOL> else : <EOL> moving_seg_slice = target_slice . gm_seg_M <EOL> for transfo in self . model . dictionary . coregistration_transfos : <EOL> transfo_name = transfo + '<STR_LIT>' + str ( i ) + find_ants_transfo_name ( transfo ) [ <NUM_LIT:0> ] <EOL> moving_seg_slice = apply_ants_transfo ( self . model . dictionary . mean_seg , moving_seg_slice , search_reg = False , binary = True , inverse = <NUM_LIT:1> , transfo_type = transfo , transfo_name = transfo_name ) <EOL> if self . model . param . seg_type == '<STR_LIT>' : <EOL> target_slice . set ( wm_seg = moving_seg_slice ) <EOL> else : <EOL> target_slice . set ( gm_seg = moving_seg_slice ) <EOL> def compute_beta ( self , coord_target , target_levels = None , dataset_coord = None , dataset_levels = None , tau = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if dataset_coord is None : <EOL> dataset_coord = self . model . pca . dataset_coord . T <EOL> dataset_levels = [ dic_slice . level for dic_slice in self . model . dictionary . slices ] <EOL> beta = [ ] <EOL> if isinstance ( coord_target [ <NUM_LIT:0> ] , ( list , np . ndarray ) ) : <EOL> for i_target , coord_projected_slice in enumerate ( coord_target ) : <EOL> beta_slice = [ ] <EOL> for j_slice , coord_slice_j in enumerate ( dataset_coord ) : <EOL> square_norm = np . linalg . norm ( ( coord_projected_slice - coord_slice_j ) , <NUM_LIT:2> ) <EOL> if target_levels is not None : <EOL> if target_levels [ i_target ] == dataset_levels [ j_slice ] : <EOL> beta_slice . append ( exp ( tau * square_norm ) ) <EOL> else : <EOL> beta_slice . append ( exp ( - tau * square_norm ) / <NUM_LIT> * ( target_levels [ i_target ] - dataset_levels [ j_slice ] ) ) <EOL> else : <EOL> beta_slice . append ( exp ( tau * square_norm ) ) <EOL> try : <EOL> beta_slice /= np . sum ( beta_slice ) <EOL> except ZeroDivisionError : <EOL> sct . printv ( '<STR_LIT>' , self . model . param . verbose , '<STR_LIT>' ) <EOL> print beta_slice <EOL> beta . append ( beta_slice ) <EOL> else : <EOL> for j_slice , coord_slice_j in enumerate ( dataset_coord ) : <EOL> square_norm = np . linalg . norm ( ( coord_target - coord_slice_j ) , <NUM_LIT:2> ) <EOL> if target_levels is not None : <EOL> if target_levels == dataset_levels [ j_slice ] : <EOL> beta . append ( exp ( tau * square_norm ) ) <EOL> else : <EOL> beta . append ( exp ( - tau * square_norm ) / <NUM_LIT> * ( target_levels - dataset_levels [ j_slice ] ) ) <EOL> else : <EOL> beta . append ( exp ( tau * square_norm ) ) <EOL> try : <EOL> beta /= np . sum ( beta ) <EOL> except ZeroDivisionError : <EOL> sct . printv ( '<STR_LIT>' , self . model . param . verbose , '<STR_LIT>' ) <EOL> print beta <EOL> return np . asarray ( beta ) <EOL> def compute_tau ( self ) : <EOL> """<STR_LIT>""" <EOL> sct . printv ( '<STR_LIT>' <EOL> '<STR_LIT>' , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> from scipy . optimize import minimize <EOL> def to_minimize ( tau ) : <EOL> """<STR_LIT>""" <EOL> sum_norm = <NUM_LIT:0> <EOL> for dic_slice in self . model . dictionary . slices : <EOL> projected_dic_slice_coord = self . model . pca . project_array ( dic_slice . im_M_flat ) <EOL> coord_dic_slice_dataset = np . delete ( self . model . pca . dataset_coord . T , dic_slice . id , <NUM_LIT:0> ) <EOL> dic_slice_dataset_levels = np . delete ( np . asarray ( dic_levels ) , dic_slice . id , <NUM_LIT:0> ) <EOL> beta_dic_slice = self . compute_beta ( projected_dic_slice_coord , target_levels = dic_slice . level , dataset_coord = coord_dic_slice_dataset , dataset_levels = dic_slice_dataset_levels , tau = tau ) <EOL> kj = self . select_k_slices ( beta_dic_slice ) <EOL> est_segm_j = self . label_fusion ( kj ) <EOL> if self . model . param . seg_type == '<STR_LIT>' : <EOL> sum_norm += l0_norm ( dic_slice . wm_seg_M , est_segm_j . data ) <EOL> else : <EOL> sum_norm += l0_norm ( dic_slice . gm_seg_M , est_segm_j . data ) <EOL> return sum_norm <EOL> dic_levels = [ dic_slice . level for dic_slice in self . model . dictionary . slices ] <EOL> est_tau = minimize ( to_minimize , <NUM_LIT> , method = '<STR_LIT>' , options = { '<STR_LIT>' : <NUM_LIT> } ) <EOL> sct . printv ( '<STR_LIT>' + str ( est_tau . x [ <NUM_LIT:0> ] ) ) <EOL> if self . model . param . todo_model == '<STR_LIT>' : <EOL> fic = open ( self . model . dictionary . model_dic_name + '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> fic . write ( str ( est_tau . x [ <NUM_LIT:0> ] ) ) <EOL> fic . close ( ) <EOL> return float ( est_tau . x [ <NUM_LIT:0> ] ) <EOL> '''<STR_LIT>''' <EOL> def select_k_slices ( self , beta ) : <EOL> """<STR_LIT>""" <EOL> kept_slice_index = [ ] <EOL> if isinstance ( beta [ <NUM_LIT:0> ] , ( list , np . ndarray ) ) : <EOL> for beta_slice in beta : <EOL> selected_index = beta_slice > self . epsilon <EOL> '''<STR_LIT>''' <EOL> kept_slice_index . append ( selected_index ) <EOL> else : <EOL> kept_slice_index = beta > self . epsilon <EOL> '''<STR_LIT>''' <EOL> return np . asarray ( kept_slice_index ) <EOL> def label_fusion ( self , selected_index ) : <EOL> """<STR_LIT>""" <EOL> if self . model . param . seg_type == '<STR_LIT>' : <EOL> segmentation_slices = np . asarray ( [ dic_slice . wm_seg_M for dic_slice in self . model . dictionary . slices ] ) <EOL> else : <EOL> segmentation_slices = np . asarray ( [ dic_slice . gm_seg_M for dic_slice in self . model . dictionary . slices ] ) <EOL> res_seg_model_space = [ ] <EOL> '''<STR_LIT>''' <EOL> if isinstance ( selected_index [ <NUM_LIT:0> ] , ( list , np . ndarray ) ) : <EOL> for i , selected_ind_by_slice in enumerate ( selected_index ) : <EOL> slice_seg = compute_majority_vote_mean_seg ( segmentation_slices [ selected_ind_by_slice ] ) <EOL> res_seg_model_space . append ( slice_seg ) <EOL> if self . model . param . seg_type == '<STR_LIT>' : <EOL> self . target [ i ] . set ( wm_seg_m = slice_seg ) <EOL> else : <EOL> self . target [ i ] . set ( gm_seg_m = slice_seg ) <EOL> else : <EOL> res_seg_model_space = compute_majority_vote_mean_seg ( segmentation_slices [ selected_index ] ) <EOL> res_seg_model_space = np . asarray ( res_seg_model_space ) <EOL> return Image ( param = res_seg_model_space ) <EOL> def plot_projected_dic ( self , nb_modes = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> self . model . pca . plot_projected_dic ( nb_mode = nb_modes , target_coord = self . coord_projected_target ) if self . coord_projected_target is not None else self . model . pca . plot_projected_dic ( ) <EOL> self . model . pca . plot_projected_dic ( nb_mode = nb_modes , target_coord = self . coord_projected_target , to_highlight = ( <NUM_LIT:5> , self . selected_k_slices [ <NUM_LIT:5> ] ) ) if self . coord_projected_target is not None else self . model . pca . plot_projected_dic ( ) <EOL> '''<STR_LIT>''' <EOL> '''<STR_LIT>''' <EOL> class TargetSegmentationGroupwise : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model , target_image = None , tau = None ) : <EOL> """<STR_LIT>""" <EOL> self . model = model <EOL> self . target = target_image <EOL> '''<STR_LIT>''' <EOL> self . epsilon = round ( <NUM_LIT:1.0> / self . model . dictionary . J , <NUM_LIT:5> ) - <NUM_LIT> <EOL> if tau is None : <EOL> self . tau = self . compute_tau ( ) <EOL> else : <EOL> self . tau = tau <EOL> self . target_M , self . R_target_to_M = self . target_groupwise_registration ( ) <EOL> self . target_M . file_name = '<STR_LIT>' <EOL> self . target_M . ext = '<STR_LIT>' <EOL> self . target_M . save ( ) <EOL> '''<STR_LIT>''' <EOL> sct . printv ( '<STR_LIT>' , model . param . verbose , '<STR_LIT>' ) <EOL> self . coord_projected_target = model . pca . project ( self . target_M ) if self . target_M is not None else None <EOL> '''<STR_LIT>''' <EOL> self . beta = self . compute_beta ( self . coord_projected_target , tau = self . tau ) <EOL> '''<STR_LIT>''' <EOL> sct . printv ( '<STR_LIT>' , model . param . verbose , '<STR_LIT>' ) <EOL> self . selected_k_slices = self . select_k_slices ( self . beta ) <EOL> '''<STR_LIT>''' <EOL> sct . printv ( '<STR_LIT>' , model . param . verbose , '<STR_LIT>' ) <EOL> self . target_GM_seg_M = self . label_fusion ( self . selected_k_slices ) <EOL> self . target_GM_seg_M . file_name = '<STR_LIT>' <EOL> self . target_GM_seg_M . ext = '<STR_LIT>' <EOL> self . target_GM_seg_M . save ( ) <EOL> sct . printv ( '<STR_LIT>' , <EOL> model . param . verbose , '<STR_LIT>' ) <EOL> self . target_GM_seg = self . target_groupwise_registration ( inverse = True ) <EOL> def compute_beta ( self , coord_target , dataset_coord = None , tau = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if dataset_coord is None : <EOL> dataset_coord = self . model . pca . dataset_coord . T <EOL> beta = [ ] <EOL> '''<STR_LIT>''' <EOL> if isinstance ( coord_target [ <NUM_LIT:0> ] , ( list , np . ndarray ) ) : <EOL> for i_target , coord_projected_slice in enumerate ( coord_target ) : <EOL> beta_slice = [ ] <EOL> for coord_slice_j in dataset_coord : <EOL> square_norm = np . linalg . norm ( ( coord_projected_slice - coord_slice_j ) , <NUM_LIT:2> ) <EOL> beta_slice . append ( exp ( - tau * square_norm ) ) <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if np . sum ( beta_slice ) != <NUM_LIT:0> : <EOL> beta_slice /= np . sum ( beta_slice ) <EOL> except ZeroDivisionError : <EOL> sct . printv ( '<STR_LIT>' , self . model . param . verbose , '<STR_LIT>' ) <EOL> print beta_slice <EOL> beta . append ( beta_slice ) <EOL> else : <EOL> for coord_slice_j in dataset_coord : <EOL> square_norm = np . linalg . norm ( ( coord_target - coord_slice_j ) , <NUM_LIT:2> ) <EOL> beta . append ( exp ( - tau * square_norm ) ) <EOL> try : <EOL> beta /= np . sum ( beta ) <EOL> except ZeroDivisionError : <EOL> sct . printv ( '<STR_LIT>' , self . model . param . verbose , '<STR_LIT>' ) <EOL> print beta <EOL> return np . asarray ( beta ) <EOL> def compute_tau ( self ) : <EOL> """<STR_LIT>""" <EOL> sct . printv ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> from scipy . optimize import minimize <EOL> def to_minimize ( tau ) : <EOL> """<STR_LIT>""" <EOL> sum_norm = <NUM_LIT:0> <EOL> for dic_slice in self . model . dictionary . slices : <EOL> projected_dic_slice_coord = self . model . pca . project_array ( dic_slice . im_M_flat ) <EOL> coord_dic_slice_dataset = np . delete ( self . model . pca . dataset_coord . T , dic_slice . id , <NUM_LIT:0> ) <EOL> beta_dic_slice = self . compute_beta ( projected_dic_slice_coord , dataset_coord = coord_dic_slice_dataset , <EOL> tau = tau ) <EOL> kj = self . select_k_slices ( beta_dic_slice ) <EOL> est_segm_j = self . label_fusion ( kj ) <EOL> sum_norm += l0_norm ( dic_slice . seg_M , est_segm_j . data ) <EOL> return sum_norm <EOL> est_tau = minimize ( to_minimize , <NUM_LIT:0> , method = '<STR_LIT>' , options = { '<STR_LIT>' : <NUM_LIT> } ) <EOL> sct . printv ( '<STR_LIT>' + str ( est_tau . x [ <NUM_LIT:0> ] ) ) <EOL> if self . model . param . todo_model == '<STR_LIT>' : <EOL> fic = open ( self . model . dictionary . model_dic_name + '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> fic . write ( str ( est_tau . x [ <NUM_LIT:0> ] ) ) <EOL> fic . close ( ) <EOL> return float ( est_tau . x [ <NUM_LIT:0> ] ) <EOL> def compute_mu ( self , beta ) : <EOL> """<STR_LIT>""" <EOL> '''<STR_LIT>''' <EOL> return self . model . pca . dataset_coord . dot ( beta ) <EOL> def compute_sigma ( self , beta , mu ) : <EOL> """<STR_LIT>""" <EOL> '''<STR_LIT>''' <EOL> return np . asarray ( [ beta . dot ( ( self . model . pca . dataset_coord [ v , : ] - mu [ v ] ) ** <NUM_LIT:2> ) for v in range ( len ( mu ) ) ] ) <EOL> def target_groupwise_registration ( self , inverse = False ) : <EOL> """<STR_LIT>""" <EOL> if not inverse : <EOL> from scipy . optimize import minimize <EOL> target_m = [ ] <EOL> def to_minimize ( t_param , n_slice ) : <EOL> """<STR_LIT>""" <EOL> moved_target_slice = Image ( param = np . asarray ( apply_2d_transformation ( self . target . data [ n_slice ] , <EOL> tx = t_param [ <NUM_LIT:0> ] , ty = t_param [ <NUM_LIT:1> ] , <EOL> theta = t_param [ <NUM_LIT:2> ] , <EOL> s = t_param [ <NUM_LIT:3> ] ) [ <NUM_LIT:0> ] ) ) <EOL> coord_moved_target = self . model . pca . project_array ( moved_target_slice . data . flatten ( ) ) <EOL> coord_moved_target = coord_moved_target . reshape ( coord_moved_target . shape [ <NUM_LIT:0> ] , ) <EOL> beta = self . compute_beta ( coord_moved_target , tau = self . tau ) <EOL> mu = self . compute_mu ( beta ) <EOL> sigma = self . compute_sigma ( beta , mu ) <EOL> target_th_slice = np . sum ( ( np . asarray ( [ dic_slice . im_M for dic_slice in self . model . dictionary . slices ] ) . T <EOL> * beta [ n_slice ] ) . T , axis = <NUM_LIT:0> ) <EOL> sq_norm = np . linalg . norm ( target_th_slice - moved_target_slice . data , <NUM_LIT:2> ) ** <NUM_LIT:2> <EOL> gauss = np . sum ( ( ( coord_moved_target - mu ) / sigma ) ** <NUM_LIT:2> ) <EOL> return sq_norm * gauss <EOL> r_target_to_m = [ ] <EOL> for i_slice , target_slice in enumerate ( self . target . data ) : <EOL> x0 = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> est_transfo = minimize ( to_minimize , x0 , args = i_slice , method = '<STR_LIT>' , options = { '<STR_LIT>' : <NUM_LIT> } ) <EOL> target_m_slice , r_slice = apply_2d_transformation ( target_slice , tx = est_transfo . x [ <NUM_LIT:0> ] , <EOL> ty = est_transfo . x [ <NUM_LIT:1> ] , theta = est_transfo . x [ <NUM_LIT:2> ] , <EOL> s = est_transfo . x [ <NUM_LIT:3> ] ) <EOL> print est_transfo . x <EOL> target_m . append ( target_m_slice ) <EOL> r_target_to_m . append ( r_slice ) <EOL> return Image ( param = np . asarray ( target_m ) ) , r_target_to_m <EOL> else : <EOL> moved_res = [ ] <EOL> for i_slice , res_m_slice in enumerate ( self . target_GM_seg_M . data ) : <EOL> moved_res . append ( apply_2d_transformation ( res_m_slice , transfo = self . R_target_to_M [ i_slice ] . inverse ) [ <NUM_LIT:0> ] ) <EOL> return Image ( np . asarray ( moved_res ) . astype ( '<STR_LIT>' ) ) <EOL> def select_k_slices ( self , beta ) : <EOL> """<STR_LIT>""" <EOL> kept_slice_index = [ ] <EOL> if isinstance ( beta [ <NUM_LIT:0> ] , ( list , np . ndarray ) ) : <EOL> for beta_slice in beta : <EOL> selected_index = beta_slice > self . epsilon <EOL> '''<STR_LIT>''' <EOL> kept_slice_index . append ( selected_index ) <EOL> else : <EOL> kept_slice_index = beta > self . epsilon <EOL> '''<STR_LIT>''' <EOL> return np . asarray ( kept_slice_index ) <EOL> def label_fusion ( self , selected_index ) : <EOL> """<STR_LIT>""" <EOL> segmentation_slices = np . asarray ( [ dic_slice . seg_M for dic_slice in self . model . dictionary . slices ] ) <EOL> res_seg_model_space = [ ] <EOL> if isinstance ( selected_index [ <NUM_LIT:0> ] , ( list , np . ndarray ) ) : <EOL> for selected_ind_by_slice in selected_index : <EOL> slice_seg = compute_majority_vote_mean_seg ( segmentation_slices [ selected_ind_by_slice ] , threshold = <NUM_LIT> ) <EOL> res_seg_model_space . append ( slice_seg ) <EOL> else : <EOL> res_seg_model_space = compute_majority_vote_mean_seg ( segmentation_slices [ selected_index ] , threshold = <NUM_LIT> ) <EOL> res_seg_model_space = np . asarray ( res_seg_model_space ) <EOL> return Image ( param = res_seg_model_space ) <EOL> def plot_projected_dic ( self , nb_modes = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> self . model . pca . plot_projected_dic ( nb_mode = nb_modes , target_coord = self . coord_projected_target ) if self . coord_projected_target is not None else self . model . pca . plot_projected_dic ( ) <EOL> self . model . pca . plot_projected_dic ( nb_mode = nb_modes , target_coord = self . coord_projected_target , to_highlight = ( <NUM_LIT:6> , self . selected_k_slices [ <NUM_LIT:6> ] ) ) if self . coord_projected_target is not None else self . model . pca . plot_projected_dic ( ) <EOL> print self . selected_k_slices [ <NUM_LIT:6> ] <EOL> class GMsegSupervisedMethod ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , target_fname , gm_seg_param = None ) : <EOL> self . dictionary = ModelDictionaryBySlice ( dic_param = gm_seg_param ) <EOL> sct . printv ( '<STR_LIT>' , verbose = gm_seg_param . verbose , type = '<STR_LIT>' ) <EOL> self . model = Model ( model_param = gm_seg_param , dictionary = self . dictionary , k = <NUM_LIT> ) <EOL> sct . printv ( '<STR_LIT>' , verbose = gm_seg_param . verbose , type = '<STR_LIT>' ) <EOL> self . target_image = Image ( target_fname ) <EOL> tau = None <EOL> if gm_seg_param . todo_model == '<STR_LIT>' : <EOL> fic = open ( self . model . dictionary . model_dic_name + '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> tau = float ( fic . read ( ) ) <EOL> fic . close ( ) <EOL> levels_im = None <EOL> if gm_seg_param . level_fname is not None : <EOL> if isinstance ( gm_seg_param . level_fname , str ) : <EOL> levels_im = gm_seg_param . level_fname <EOL> else : <EOL> levels_im = Image ( gm_seg_param . level_fname ) <EOL> if gm_seg_param . target_reg == '<STR_LIT>' : <EOL> if levels_im is not None : <EOL> self . target_seg_methods = TargetSegmentationPairwise ( self . model , target_image = self . target_image , levels_image = levels_im , tau = tau ) <EOL> else : <EOL> self . target_seg_methods = TargetSegmentationPairwise ( self . model , target_image = self . target_image , tau = tau ) <EOL> elif gm_seg_param . target_reg == '<STR_LIT>' : <EOL> self . target_seg_methods = TargetSegmentationGroupwise ( self . model , target_image = self . target_image , tau = tau ) <EOL> suffix = '<STR_LIT:_>' <EOL> suffix += gm_seg_param . target_reg <EOL> suffix += gm_seg_param . seg_type <EOL> for transfo in self . dictionary . coregistration_transfos : <EOL> suffix += '<STR_LIT:_>' + transfo <EOL> if levels_im is not None : <EOL> suffix += '<STR_LIT>' <EOL> else : <EOL> suffix += '<STR_LIT>' <EOL> if gm_seg_param . seg_type == '<STR_LIT>' : <EOL> if len ( self . target_seg_methods . target ) == <NUM_LIT:1> : <EOL> self . res = Image ( param = np . asarray ( self . target_seg_methods . target [ <NUM_LIT:0> ] . wm_seg ) ) <EOL> else : <EOL> self . res = Image ( param = np . asarray ( [ target_slice . wm_seg for target_slice in self . target_seg_methods . target ] ) ) <EOL> else : <EOL> if len ( self . target_seg_methods . target ) == <NUM_LIT:1> : <EOL> self . res = Image ( param = np . asarray ( self . target_seg_methods . target [ <NUM_LIT:0> ] . gm_seg ) ) <EOL> else : <EOL> self . res = Image ( param = np . asarray ( [ target_slice . gm_seg for target_slice in self . target_seg_methods . target ] ) ) <EOL> name_res = sct . extract_fname ( target_fname ) [ <NUM_LIT:1> ] + '<STR_LIT>' + suffix <EOL> self . res . file_name = name_res <EOL> self . res . ext = '<STR_LIT>' <EOL> self . res . save ( ) <EOL> '''<STR_LIT>''' <EOL> sct . printv ( '<STR_LIT>' ) <EOL> sct . printv ( '<STR_LIT>' + target_fname + '<STR_LIT:U+0020>' + name_res + '<STR_LIT>' , gm_seg_param . verbose , '<STR_LIT:info>' ) <EOL> def show ( self ) : <EOL> sct . printv ( '<STR_LIT>' ) <EOL> self . model . pca . show_all_modes ( ) <EOL> sct . printv ( '<STR_LIT>' ) <EOL> self . target_seg_methods . plot_projected_dic ( nb_modes = <NUM_LIT:3> ) <EOL> sct . printv ( '<STR_LIT>' ) <EOL> self . model . pca . show_mode_variation ( ) <EOL> print '<STR_LIT>' , self . dictionary . J <EOL> '''<STR_LIT>''' <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> param = Param ( ) <EOL> input_target_fname = None <EOL> if param . debug : <EOL> print '<STR_LIT>' <EOL> fname_input = param . path_dictionary + "<STR_LIT>" <EOL> fname_input = param . path_dictionary + "<STR_LIT>" <EOL> else : <EOL> param_default = Param ( ) <EOL> parser = Parser ( __file__ ) <EOL> parser . usage . set_description ( '<STR_LIT>' ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT:file>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = True , <EOL> example = '<STR_LIT>' ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = True , <EOL> example = '<STR_LIT>' ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = True , <EOL> example = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT:str>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> example = '<STR_LIT>' ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = [ [ '<STR_LIT:U+002C>' ] , '<STR_LIT:str>' ] , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> default_value = [ '<STR_LIT>' ] , <EOL> example = [ '<STR_LIT>' ] ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = '<STR_LIT>' , <EOL> description = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> mandatory = False , <EOL> default_value = '<STR_LIT>' , <EOL> example = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = '<STR_LIT>' , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> default_value = '<STR_LIT>' , <EOL> example = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT:int>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> default_value = <NUM_LIT:0> , <EOL> example = '<STR_LIT:1>' ) <EOL> arguments = parser . parse ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> input_target_fname = arguments [ "<STR_LIT>" ] <EOL> param . path_dictionary = arguments [ "<STR_LIT>" ] <EOL> param . todo_model = arguments [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in arguments : <EOL> param . reg = arguments [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in arguments : <EOL> param . target_reg = arguments [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in arguments : <EOL> param . seg_type = arguments [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in arguments : <EOL> param . level_fname = arguments [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in arguments : <EOL> param . verbose = arguments [ "<STR_LIT>" ] <EOL> gm_seg_method = GMsegSupervisedMethod ( input_target_fname , gm_seg_param = param ) <EOL> if param . verbose == <NUM_LIT:2> : <EOL> gm_seg_method . show ( ) </s>
