<s> from math import sqrt <EOL> import os <EOL> import sys <EOL> import numpy as np <EOL> from msct_image import Image , get_dimension <EOL> import sct_utils as sct <EOL> from msct_parser import Parser <EOL> from sct_image import set_orientation , get_orientation_3d <EOL> def get_parser ( ) : <EOL> parser = Parser ( __file__ ) <EOL> parser . usage . set_description ( '<STR_LIT>' ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT>" , <EOL> description = "<STR_LIT>" <EOL> "<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> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> mandatory = False , <EOL> example = '<STR_LIT>' ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> example = '<STR_LIT>' ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> example = '<STR_LIT>' ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> example = '<STR_LIT>' ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT>" , <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:file>' ] , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> example = '<STR_LIT>' ) <EOL> return parser <EOL> class Slice : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , slice_id = None , im = None , sc_seg = None , gm_seg = None , wm_seg = None , reg_to_m = None , im_m = None , gm_seg_m = None , wm_seg_m = None , im_m_flat = None , gm_seg_m_flat = None , wm_seg_m_flat = None , level = None ) : <EOL> """<STR_LIT>""" <EOL> self . id = slice_id <EOL> self . im = im <EOL> self . sc_seg = sc_seg <EOL> self . gm_seg = gm_seg <EOL> self . wm_seg = wm_seg <EOL> self . reg_to_M = reg_to_m <EOL> self . im_M = im_m <EOL> self . gm_seg_M = gm_seg_m <EOL> self . wm_seg_M = wm_seg_m <EOL> self . im_M_flat = im_m_flat <EOL> self . gm_seg_M_flat = gm_seg_m_flat <EOL> self . wm_seg_M_flat = wm_seg_m_flat <EOL> self . level = level <EOL> def set ( self , slice_id = None , im = None , sc_seg = None , gm_seg = None , wm_seg = None , reg_to_m = None , im_m = None , gm_seg_m = None , wm_seg_m = None , im_m_flat = None , gm_seg_m_flat = None , wm_seg_m_flat = None , level = None ) : <EOL> """<STR_LIT>""" <EOL> if slice_id is not None : <EOL> self . id = slice_id <EOL> if im is not None : <EOL> self . im = im <EOL> if sc_seg is not None : <EOL> self . sc_seg = sc_seg <EOL> if gm_seg is not None : <EOL> self . gm_seg = gm_seg <EOL> if wm_seg is not None : <EOL> self . wm_seg = wm_seg <EOL> if reg_to_m is not None : <EOL> self . reg_to_M = reg_to_m <EOL> if im_m is not None : <EOL> self . im_M = im_m <EOL> if gm_seg_m is not None : <EOL> self . gm_seg_M = gm_seg_m <EOL> if wm_seg_m is not None : <EOL> self . wm_seg_M = wm_seg_m <EOL> if im_m_flat is not None : <EOL> self . im_M_flat = im_m_flat <EOL> if gm_seg_m_flat is not None : <EOL> self . gm_seg_M_flat = gm_seg_m_flat <EOL> if wm_seg_m_flat is not None : <EOL> self . wm_seg_M_flat = wm_seg_m_flat <EOL> if level is not None : <EOL> self . level = level <EOL> def __repr__ ( self ) : <EOL> s = '<STR_LIT>' + str ( self . id ) <EOL> if self . level is not None : <EOL> s += '<STR_LIT>' + str ( self . level ) <EOL> s += '<STR_LIT>' + str ( self . im ) + '<STR_LIT>' + str ( self . gm_seg ) + '<STR_LIT>' + self . reg_to_M <EOL> if self . im_M is not None : <EOL> s += '<STR_LIT>' + str ( self . im_M ) <EOL> if self . gm_seg_M is not None : <EOL> s += '<STR_LIT>' + str ( self . gm_seg_M ) <EOL> return s <EOL> def inverse_gmseg_to_wmseg ( gm_seg , original_im , name_gm_seg = '<STR_LIT>' , save = True , verbose = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> type_res = '<STR_LIT>' <EOL> if isinstance ( gm_seg , Image ) : <EOL> original_hdr = gm_seg . hdr <EOL> gm_seg_dat = gm_seg . data <EOL> type_res += '<STR_LIT:image>' <EOL> elif isinstance ( gm_seg , np . ndarray ) : <EOL> gm_seg_dat = gm_seg <EOL> type_res += '<STR_LIT>' <EOL> else : <EOL> sct . printv ( '<STR_LIT>' + type ( gm_seg ) , verbose , '<STR_LIT>' ) <EOL> gm_seg_dat = None <EOL> if isinstance ( original_im , Image ) : <EOL> original_dat = original_im . data <EOL> type_res += '<STR_LIT:image>' <EOL> elif isinstance ( original_im , np . ndarray ) : <EOL> original_dat = original_im <EOL> type_res += '<STR_LIT>' <EOL> else : <EOL> sct . printv ( '<STR_LIT>' + type ( original_im ) , verbose , '<STR_LIT>' ) <EOL> original_dat = None <EOL> assert gm_seg_dat . shape == original_dat . shape <EOL> binary_gm_seg_dat = ( gm_seg_dat > <NUM_LIT:0> ) . astype ( int ) <EOL> sc_dat = ( original_dat > <NUM_LIT:0> ) . astype ( int ) <EOL> res_wm_seg = np . asarray ( np . absolute ( sc_dat - binary_gm_seg_dat ) . astype ( int ) ) <EOL> if '<STR_LIT:image>' in type_res : <EOL> res_wm_seg_im = Image ( param = res_wm_seg , absolutepath = name_gm_seg + '<STR_LIT>' ) <EOL> res_wm_seg_im . hdr = original_hdr <EOL> if save : <EOL> res_wm_seg_im . save ( ) <EOL> res_wm_seg_im . orientation = gm_seg . orientation <EOL> return res_wm_seg_im <EOL> else : <EOL> return res_wm_seg <EOL> def apply_ants_transfo ( fixed_im , moving_im , search_reg = True , transfo_type = '<STR_LIT>' , metric = '<STR_LIT>' , apply_transfo = True , transfo_name = '<STR_LIT>' , binary = True , path = '<STR_LIT>' , inverse = <NUM_LIT:0> , verbose = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> import time <EOL> res_im = None <EOL> try : <EOL> transfo_dir = transfo_type . lower ( ) + '<STR_LIT>' <EOL> if transfo_dir not in os . listdir ( path ) : <EOL> sct . run ( '<STR_LIT>' + path + transfo_dir ) <EOL> dir_name = '<STR_LIT>' + time . strftime ( "<STR_LIT>" ) + '<STR_LIT:_>' + str ( time . time ( ) ) <EOL> sct . run ( '<STR_LIT>' + dir_name , verbose = verbose ) <EOL> os . chdir ( '<STR_LIT>' + dir_name ) <EOL> if binary : <EOL> t = '<STR_LIT>' <EOL> else : <EOL> t = '<STR_LIT>' <EOL> fixed_im_name = '<STR_LIT>' <EOL> Image ( param = np . asarray ( fixed_im ) , absolutepath = '<STR_LIT>' + fixed_im_name + '<STR_LIT>' ) . save ( type = t ) <EOL> moving_im_name = '<STR_LIT>' <EOL> Image ( param = np . asarray ( moving_im ) , absolutepath = '<STR_LIT>' + moving_im_name + '<STR_LIT>' ) . save ( type = t ) <EOL> mat_name , inverse_mat_name = find_ants_transfo_name ( transfo_type ) <EOL> if search_reg : <EOL> reg_interpolation = '<STR_LIT>' <EOL> transfo_params = '<STR_LIT>' <EOL> if transfo_type == '<STR_LIT>' : <EOL> transfo_params = '<STR_LIT>' <EOL> elif transfo_type == '<STR_LIT>' : <EOL> transfo_params = '<STR_LIT>' <EOL> reg_interpolation = '<STR_LIT>' <EOL> elif transfo_type == '<STR_LIT>' : <EOL> transfo_params = '<STR_LIT>' <EOL> gradientstep = <NUM_LIT:0.5> <EOL> metric_params = '<STR_LIT>' <EOL> niter = <NUM_LIT:5> <EOL> smooth = <NUM_LIT:0> <EOL> shrink = <NUM_LIT:1> <EOL> cmd_reg = '<STR_LIT>' + reg_interpolation + '<STR_LIT>' + transfo_type + '<STR_LIT:[>' + str ( gradientstep ) + transfo_params + '<STR_LIT>' '<STR_LIT>' + metric + '<STR_LIT:[>' + fixed_im_name + '<STR_LIT>' + moving_im_name + '<STR_LIT>' + metric_params + '<STR_LIT>' + str ( niter ) + '<STR_LIT>' + str ( smooth ) + '<STR_LIT>' + str ( shrink ) + '<STR_LIT>' + str ( verbose ) <EOL> sct . run ( cmd_reg , verbose = verbose ) <EOL> sct . run ( '<STR_LIT>' + mat_name + '<STR_LIT>' + path + transfo_dir + '<STR_LIT:/>' + transfo_name , verbose = verbose ) <EOL> if '<STR_LIT>' in transfo_type : <EOL> sct . run ( '<STR_LIT>' + inverse_mat_name + '<STR_LIT>' + path + transfo_dir + '<STR_LIT:/>' + transfo_name + '<STR_LIT>' , <EOL> verbose = verbose ) <EOL> if apply_transfo : <EOL> if not search_reg : <EOL> sct . run ( '<STR_LIT>' + path + transfo_dir + '<STR_LIT:/>' + transfo_name + '<STR_LIT>' + mat_name , verbose = verbose ) <EOL> if '<STR_LIT>' in transfo_type : <EOL> sct . run ( '<STR_LIT>' + path + transfo_dir + '<STR_LIT:/>' + transfo_name + '<STR_LIT>' + '<STR_LIT>' + inverse_mat_name , <EOL> verbose = verbose ) <EOL> if binary or moving_im . max ( ) == <NUM_LIT:1> or fixed_im . max ( ) == <NUM_LIT:1> : <EOL> apply_transfo_interpolation = '<STR_LIT>' <EOL> else : <EOL> apply_transfo_interpolation = '<STR_LIT>' <EOL> if '<STR_LIT>' in transfo_type and inverse : <EOL> cmd_apply = '<STR_LIT>' + moving_im_name + '<STR_LIT>' + moving_im_name + '<STR_LIT>' '<STR_LIT>' + apply_transfo_interpolation + '<STR_LIT>' + inverse_mat_name + '<STR_LIT>' '<STR_LIT>' + fixed_im_name + '<STR_LIT>' + str ( verbose ) <EOL> else : <EOL> cmd_apply = '<STR_LIT>' + moving_im_name + '<STR_LIT>' + moving_im_name + '<STR_LIT>' '<STR_LIT>' + apply_transfo_interpolation + '<STR_LIT>' + mat_name + '<STR_LIT:U+002C>' + str ( inverse ) + '<STR_LIT>' '<STR_LIT>' + fixed_im_name + '<STR_LIT>' + str ( verbose ) <EOL> sct . run ( cmd_apply , verbose = verbose ) <EOL> res_im = Image ( moving_im_name + '<STR_LIT>' ) <EOL> except Exception , e : <EOL> sct . printv ( '<STR_LIT>' , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> print e <EOL> else : <EOL> sct . printv ( '<STR_LIT>' , verbose = verbose , type = '<STR_LIT>' ) <EOL> os . chdir ( '<STR_LIT:..>' ) <EOL> sct . run ( '<STR_LIT>' + dir_name + '<STR_LIT:/>' , verbose = verbose ) <EOL> if apply_transfo and res_im is not None : <EOL> return res_im . data <EOL> def find_ants_transfo_name ( transfo_type ) : <EOL> """<STR_LIT>""" <EOL> transfo_name = '<STR_LIT>' <EOL> inverse_transfo_name = '<STR_LIT>' <EOL> if transfo_type == '<STR_LIT>' or transfo_type == '<STR_LIT>' : <EOL> transfo_name = '<STR_LIT>' <EOL> elif transfo_type == '<STR_LIT>' : <EOL> transfo_name = '<STR_LIT>' <EOL> elif transfo_type == '<STR_LIT>' or transfo_type == '<STR_LIT>' : <EOL> transfo_name = '<STR_LIT>' <EOL> inverse_transfo_name = '<STR_LIT>' <EOL> return transfo_name , inverse_transfo_name <EOL> def l0_norm ( x , y ) : <EOL> """<STR_LIT>""" <EOL> return np . linalg . norm ( x . flatten ( ) - y . flatten ( ) , <NUM_LIT:0> ) <EOL> def compute_majority_vote_mean_seg ( seg_data_set , threshold = <NUM_LIT:0.5> , weights = None , type = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if weights is None : <EOL> average = np . sum ( seg_data_set , axis = <NUM_LIT:0> ) / float ( len ( seg_data_set ) ) <EOL> else : <EOL> average = np . sum ( np . einsum ( '<STR_LIT>' , seg_data_set , weights ) , axis = <NUM_LIT:0> ) <EOL> if type == '<STR_LIT>' : <EOL> return ( average >= threshold ) . astype ( int ) <EOL> else : <EOL> return average . astype ( float ) <EOL> def correct_wmseg ( res_gmseg , original_im , name_wm_seg , hdr ) : <EOL> '''<STR_LIT>''' <EOL> wmseg_dat = ( original_im . data > <NUM_LIT:0> ) . astype ( int ) - res_gmseg . data <EOL> corrected_wm_seg = Image ( param = wmseg_dat ) <EOL> corrected_wm_seg . file_name = name_wm_seg + '<STR_LIT>' <EOL> corrected_wm_seg . ext = '<STR_LIT>' <EOL> corrected_wm_seg . hdr = hdr <EOL> corrected_wm_seg . save ( ) <EOL> sct . run ( '<STR_LIT>' + corrected_wm_seg . file_name + '<STR_LIT>' + corrected_wm_seg . file_name + '<STR_LIT>' ) <EOL> return corrected_wm_seg <EOL> def get_key_from_val ( dic , val ) : <EOL> """<STR_LIT>""" <EOL> for k , v in dic . items ( ) : <EOL> if v == val : <EOL> return k <EOL> def check_file_to_niigz ( file_name , verbose = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> ext = '<STR_LIT>' <EOL> if os . path . isfile ( file_name ) : <EOL> if sct . extract_fname ( file_name ) [ <NUM_LIT:2> ] != ext : <EOL> new_file_name = sct . extract_fname ( file_name ) [ <NUM_LIT:1> ] + ext <EOL> sct . run ( '<STR_LIT>' + file_name + '<STR_LIT>' + new_file_name ) <EOL> else : <EOL> new_file_name = file_name <EOL> return new_file_name <EOL> else : <EOL> sct . printv ( '<STR_LIT>' + file_name + '<STR_LIT>' , verbose , '<STR_LIT>' ) <EOL> return False <EOL> def save_dic_slices ( path_to_model ) : <EOL> """<STR_LIT>""" <EOL> import pickle , gzip <EOL> 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>' , <NUM_LIT:10> : '<STR_LIT>' , <NUM_LIT:11> : '<STR_LIT>' , <NUM_LIT:12> : '<STR_LIT>' , <NUM_LIT> : '<STR_LIT>' } <EOL> model_slices_list = pickle . load ( gzip . open ( path_to_model + '<STR_LIT>' , '<STR_LIT:rb>' ) ) <EOL> sct . run ( '<STR_LIT>' + path_to_model + '<STR_LIT>' ) <EOL> model_slices = [ Slice ( slice_id = i_slice , level = dic_slice [ <NUM_LIT:3> ] , im_m = dic_slice [ <NUM_LIT:0> ] , wm_seg_m = dic_slice [ <NUM_LIT:1> ] , gm_seg_m = dic_slice [ <NUM_LIT:2> ] , im_m_flat = dic_slice [ <NUM_LIT:0> ] . flatten ( ) , wm_seg_m_flat = dic_slice [ <NUM_LIT:1> ] . flatten ( ) ) for i_slice , dic_slice in enumerate ( model_slices_list ) ] <EOL> for mod_slice in model_slices : <EOL> Image ( param = mod_slice . im_M , absolutepath = path_to_model + '<STR_LIT>' + str ( mod_slice . id ) + '<STR_LIT:_>' + level_label [ mod_slice . level ] + '<STR_LIT>' ) . save ( ) <EOL> Image ( param = mod_slice . wm_seg_M , absolutepath = path_to_model + '<STR_LIT>' + str ( mod_slice . id ) + '<STR_LIT:_>' + level_label [ mod_slice . level ] + '<STR_LIT>' ) . save ( ) <EOL> def extract_metric_from_slice_set ( slices_set , seg_to_use = None , metric = '<STR_LIT>' , gm_percentile = <NUM_LIT> , wm_percentile = <NUM_LIT> , save = False , output = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> import copy <EOL> 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>' , <NUM_LIT:10> : '<STR_LIT>' , <NUM_LIT:11> : '<STR_LIT>' , <NUM_LIT:12> : '<STR_LIT>' , <NUM_LIT> : '<STR_LIT>' } <EOL> if save : <EOL> f = open ( output , '<STR_LIT:w>' ) <EOL> f . write ( '<STR_LIT>' + metric + '<STR_LIT>' + metric + '<STR_LIT>' ) <EOL> else : <EOL> f = None <EOL> slice_set_metric = { } <EOL> for i , slice_i in enumerate ( slices_set ) : <EOL> gm_dat = copy . deepcopy ( slice_i . im_M ) <EOL> wm_dat = copy . deepcopy ( slice_i . im_M ) <EOL> if seg_to_use is None : <EOL> gm_dat [ slice_i . gm_seg_M == <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> wm_dat [ slice_i . gm_seg_M == <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> else : <EOL> gm_dat [ seg_to_use [ i ] == <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> wm_dat [ seg_to_use [ i ] == <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> gm_dat = gm_dat [ gm_dat > <NUM_LIT:0.1> ] <EOL> wm_dat = wm_dat [ wm_dat > <NUM_LIT:0.1> ] <EOL> if gm_percentile != <NUM_LIT:0> : <EOL> gm_dat = sorted ( gm_dat . flatten ( ) ) <EOL> n_gm_outliers = int ( round ( gm_percentile * len ( gm_dat ) / <NUM_LIT> ) ) <EOL> if n_gm_outliers != <NUM_LIT:0> and n_gm_outliers * <NUM_LIT:2> < len ( gm_dat ) : <EOL> gm_dat = gm_dat [ n_gm_outliers : - n_gm_outliers ] <EOL> if gm_dat == [ ] : <EOL> gm_met = <NUM_LIT:0> <EOL> gm_std = <NUM_LIT:0> <EOL> else : <EOL> if metric . lower ( ) == '<STR_LIT>' : <EOL> gm_met = np . mean ( gm_dat ) <EOL> elif metric . lower ( ) == '<STR_LIT>' : <EOL> gm_met = np . median ( gm_dat ) <EOL> gm_std = np . std ( gm_dat ) <EOL> if wm_percentile != <NUM_LIT:0> : <EOL> wm_dat = sorted ( wm_dat . flatten ( ) ) <EOL> n_wm_outliers = int ( round ( wm_percentile * len ( wm_dat ) / <NUM_LIT> ) ) <EOL> if n_wm_outliers != <NUM_LIT:0> and n_wm_outliers * <NUM_LIT:2> < len ( wm_dat ) : <EOL> wm_dat = wm_dat [ n_wm_outliers : - n_wm_outliers ] <EOL> if wm_dat == [ ] : <EOL> wm_met = <NUM_LIT:0> <EOL> wm_std = <NUM_LIT:0> <EOL> else : <EOL> if metric . lower ( ) == '<STR_LIT>' : <EOL> wm_met = np . mean ( wm_dat ) <EOL> elif metric . lower ( ) == '<STR_LIT>' : <EOL> wm_met = np . median ( wm_dat ) <EOL> wm_std = np . std ( wm_dat ) <EOL> slice_set_metric [ slice_i . id ] = ( wm_met , gm_met , wm_std , gm_std ) <EOL> if save : <EOL> f . write ( str ( slice_i . id ) + '<STR_LIT>' + level_label [ int ( slice_i . level ) ] + '<STR_LIT>' + str ( wm_met ) + '<STR_LIT>' + str ( gm_met ) + '<STR_LIT>' + str ( wm_std ) + '<STR_LIT>' + str ( gm_std ) + '<STR_LIT:\n>' ) <EOL> if save : <EOL> f . close ( ) <EOL> return slice_set_metric <EOL> def crop_t2_star_pipeline ( path , box_size = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> for subject_dir in os . listdir ( path ) : <EOL> if os . path . isdir ( path + subject_dir + '<STR_LIT:/>' ) : <EOL> t2star = '<STR_LIT>' <EOL> sc_seg = '<STR_LIT>' <EOL> seg_in = '<STR_LIT>' <EOL> seg_in_name = '<STR_LIT>' <EOL> manual_seg = '<STR_LIT>' <EOL> manual_seg_name = '<STR_LIT>' <EOL> mask_box = '<STR_LIT>' <EOL> seg_in_croped = '<STR_LIT>' <EOL> manual_seg_croped = '<STR_LIT>' <EOL> for subject_file in os . listdir ( path + '<STR_LIT:/>' + subject_dir ) : <EOL> file_low = subject_file . lower ( ) <EOL> if '<STR_LIT>' in file_low or '<STR_LIT>' in file_low in file_low and '<STR_LIT>' not in file_low and '<STR_LIT>' not in file_low and '<STR_LIT>' not in file_low : <EOL> t2star = subject_file <EOL> t2star_path , t2star_name , ext = sct . extract_fname ( t2star ) <EOL> elif '<STR_LIT>' in file_low and '<STR_LIT>' in file_low and '<STR_LIT>' not in file_low : <EOL> mask_box = subject_file <EOL> elif '<STR_LIT>' in file_low and '<STR_LIT>' not in file_low and '<STR_LIT>' not in file_low and '<STR_LIT>' not in file_low and '<STR_LIT>' not in file_low : <EOL> sc_seg = subject_file <EOL> elif '<STR_LIT>' in file_low and '<STR_LIT>' not in file_low and '<STR_LIT>' not in file_low : <EOL> manual_seg = subject_file <EOL> manual_seg_name = sct . extract_fname ( manual_seg ) [ <NUM_LIT:1> ] <EOL> elif '<STR_LIT>' in file_low and '<STR_LIT>' in file_low and '<STR_LIT>' not in file_low : <EOL> manual_seg_croped = subject_file <EOL> if t2star != '<STR_LIT>' and sc_seg != '<STR_LIT>' : <EOL> subject_path = path + '<STR_LIT:/>' + subject_dir + '<STR_LIT:/>' <EOL> os . chdir ( subject_path ) <EOL> ext = '<STR_LIT>' <EOL> try : <EOL> if seg_in == '<STR_LIT>' : <EOL> seg_in_name = t2star_name + '<STR_LIT>' <EOL> seg_in = seg_in_name + ext <EOL> sct . run ( '<STR_LIT>' + t2star + '<STR_LIT>' + sc_seg + '<STR_LIT>' + seg_in ) <EOL> if mask_box == '<STR_LIT>' : <EOL> mask_box = t2star_name + '<STR_LIT>' + ext <EOL> sct . run ( '<STR_LIT>' + seg_in + '<STR_LIT>' + sc_seg + '<STR_LIT>' + str ( box_size ) + '<STR_LIT>' + mask_box + '<STR_LIT>' ) <EOL> if seg_in_croped == '<STR_LIT>' : <EOL> seg_in_im = Image ( seg_in ) <EOL> mask_im = Image ( mask_box ) <EOL> seg_in_im . crop_and_stack ( mask_im , suffix = '<STR_LIT>' , save = True ) <EOL> seg_in_name += '<STR_LIT>' <EOL> seg_in_im . setFileName ( seg_in_name + ext ) <EOL> seg_in_im = set_orientation ( seg_in_im , '<STR_LIT>' ) <EOL> if manual_seg_croped == '<STR_LIT>' : <EOL> manual_seg_im = Image ( manual_seg ) <EOL> mask_im = Image ( mask_box ) <EOL> manual_seg_im . crop_and_stack ( mask_im , suffix = '<STR_LIT>' , save = True ) <EOL> manual_seg_name += '<STR_LIT>' <EOL> manual_seg_im . setFileName ( manual_seg_name + ext ) <EOL> manual_seg_im = set_orientation ( manual_seg_im , '<STR_LIT>' ) <EOL> except Exception , e : <EOL> sct . printv ( '<STR_LIT>' + str ( e ) , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> os . chdir ( '<STR_LIT:..>' ) <EOL> def crop_t2_star ( t2star , sc_seg , box_size = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> t2star_name = sct . extract_fname ( t2star ) [ <NUM_LIT:1> ] <EOL> sc_seg_name = sct . extract_fname ( sc_seg ) [ <NUM_LIT:1> ] <EOL> mask_box = None <EOL> try : <EOL> ext = '<STR_LIT>' <EOL> seg_in_name = t2star_name + '<STR_LIT>' <EOL> seg_in = seg_in_name + ext <EOL> sct . run ( '<STR_LIT>' + t2star + '<STR_LIT>' + sc_seg + '<STR_LIT>' + seg_in ) <EOL> mask_box = t2star_name + '<STR_LIT>' + ext <EOL> sct . run ( '<STR_LIT>' + seg_in + '<STR_LIT>' + sc_seg + '<STR_LIT>' + str ( box_size ) + '<STR_LIT>' + mask_box + '<STR_LIT>' ) <EOL> seg_in_im = Image ( seg_in ) <EOL> mask_im = Image ( mask_box ) <EOL> seg_in_im . crop_and_stack ( mask_im , suffix = '<STR_LIT>' , save = True ) <EOL> seg_in_name += '<STR_LIT>' <EOL> seg_in_im . setFileName ( seg_in_name + ext ) <EOL> seg_in_im = set_orientation ( seg_in_im , '<STR_LIT>' ) <EOL> fname_seg_in_IRP = seg_in_name + '<STR_LIT>' + ext <EOL> if t2star_name + '<STR_LIT>' in os . listdir ( '<STR_LIT:.>' ) : <EOL> mask_box = t2star_name + '<STR_LIT>' <EOL> except Exception , e : <EOL> sct . printv ( '<STR_LIT>' + t2star_name + '<STR_LIT>' + str ( e ) , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> return mask_box , fname_seg_in_IRP <EOL> def save_by_slice ( dic_dir ) : <EOL> """<STR_LIT>""" <EOL> if dic_dir [ - <NUM_LIT:1> ] == '<STR_LIT:/>' : <EOL> dic_by_slice_dir = dic_dir [ : - <NUM_LIT:1> ] + '<STR_LIT>' <EOL> else : <EOL> dic_by_slice_dir = dic_dir + '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + dic_by_slice_dir ) <EOL> for subject_dir in os . listdir ( dic_dir ) : <EOL> subject_path = dic_dir + '<STR_LIT:/>' + subject_dir <EOL> if os . path . isdir ( subject_path ) : <EOL> sct . run ( '<STR_LIT>' + dic_by_slice_dir + subject_dir ) <EOL> path_file_levels = None <EOL> label_by_slice = { } <EOL> 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> for file_name in os . listdir ( subject_path ) : <EOL> if '<STR_LIT>' in file_name : <EOL> path_file_levels = subject_path + '<STR_LIT:/>' + file_name <EOL> if '<STR_LIT>' not in file_name : <EOL> path_file_levels_IRP = sct . add_suffix ( path_file_levels , '<STR_LIT>' ) <EOL> sct . run ( '<STR_LIT>' + subject_path + '<STR_LIT:/>' + file_name + '<STR_LIT>' + path_file_levels_IRP ) <EOL> path_file_levels = path_file_levels_IRP <EOL> if path_file_levels is None and '<STR_LIT:label>' in os . listdir ( subject_path ) : <EOL> if '<STR_LIT>' not in sct . run ( '<STR_LIT>' + subject_path + '<STR_LIT>' ) [ <NUM_LIT:1> ] : <EOL> sct . run ( '<STR_LIT>' + subject_path + '<STR_LIT>' ) <EOL> path_file_levels = subject_path + '<STR_LIT>' <EOL> elif path_file_levels is not None : <EOL> im_levels = Image ( path_file_levels ) <EOL> nz_coord = im_levels . getNonZeroCoordinates ( ) <EOL> for i_level_slice , level_slice in enumerate ( im_levels . 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> label_by_slice [ i_level_slice ] = int ( round ( sum ( nz_val ) / len ( nz_val ) ) ) <EOL> except ZeroDivisionError : <EOL> sct . printv ( '<STR_LIT>' + str ( i_level_slice ) + '<STR_LIT>' + subject_dir ) <EOL> label_by_slice [ i_level_slice ] = <NUM_LIT:0> <EOL> for file_name in os . listdir ( subject_path ) : <EOL> if '<STR_LIT>' in file_name and '<STR_LIT>' in file_name and '<STR_LIT>' in file_name : <EOL> im = Image ( subject_path + '<STR_LIT:/>' + file_name ) <EOL> im_zooms = im . hdr . get_zooms ( ) <EOL> slice_zoom = ( im_zooms [ <NUM_LIT:1> ] , im_zooms [ <NUM_LIT:2> ] , im_zooms [ <NUM_LIT:0> ] ) <EOL> if path_file_levels is None : <EOL> for i_slice , im_slice in enumerate ( im . data ) : <EOL> if i_slice < <NUM_LIT:10> : <EOL> i_slice_str = str ( i_slice ) <EOL> i_slice_str = '<STR_LIT:0>' + i_slice_str <EOL> else : <EOL> i_slice_str = str ( i_slice ) <EOL> fname_slice = dic_by_slice_dir + subject_dir + '<STR_LIT:/>' + subject_dir + '<STR_LIT>' + i_slice_str + '<STR_LIT>' <EOL> im_slice = Image ( param = im_slice , absolutepath = fname_slice , hdr = im . hdr ) <EOL> if len ( im_slice . hdr . get_zooms ( ) ) == <NUM_LIT:3> : <EOL> im_slice . hdr . set_zooms ( slice_zoom ) <EOL> im_slice . save ( ) <EOL> else : <EOL> for i_slice , im_slice in enumerate ( im . data ) : <EOL> if i_slice < <NUM_LIT:10> : <EOL> i_slice_str = str ( i_slice ) <EOL> i_slice_str = '<STR_LIT:0>' + i_slice_str <EOL> else : <EOL> i_slice_str = str ( i_slice ) <EOL> fname_slice = dic_by_slice_dir + subject_dir + '<STR_LIT:/>' + subject_dir + '<STR_LIT>' + i_slice_str + '<STR_LIT:_>' + level_label [ label_by_slice [ i_slice ] ] + '<STR_LIT>' <EOL> im_slice = Image ( param = im_slice , absolutepath = fname_slice , hdr = im . hdr ) <EOL> if len ( im_slice . hdr . get_zooms ( ) ) == <NUM_LIT:3> : <EOL> im_slice . hdr . set_zooms ( slice_zoom ) <EOL> im_slice . save ( ) <EOL> if '<STR_LIT>' in file_name and '<STR_LIT>' in file_name and '<STR_LIT>' in file_name : <EOL> seg = Image ( dic_dir + '<STR_LIT:/>' + subject_dir + '<STR_LIT:/>' + file_name ) <EOL> seg_zooms = seg . hdr . get_zooms ( ) <EOL> slice_zoom = ( seg_zooms [ <NUM_LIT:1> ] , seg_zooms [ <NUM_LIT:2> ] , seg_zooms [ <NUM_LIT:0> ] ) <EOL> if path_file_levels is None : <EOL> for i_slice , seg_slice in enumerate ( seg . data ) : <EOL> if i_slice < <NUM_LIT:10> : <EOL> i_slice_str = str ( i_slice ) <EOL> i_slice_str = '<STR_LIT:0>' + i_slice_str <EOL> else : <EOL> i_slice_str = str ( i_slice ) <EOL> seg_slice = Image ( param = seg_slice , absolutepath = dic_by_slice_dir + subject_dir + '<STR_LIT:/>' + subject_dir + '<STR_LIT>' + i_slice_str + '<STR_LIT:_>' + level_label [ label_by_slice [ i_slice ] ] + '<STR_LIT>' , hdr = seg . hdr ) <EOL> if len ( seg_slice . hdr . get_zooms ( ) ) == <NUM_LIT:3> : <EOL> seg_slice . hdr . set_zooms ( slice_zoom ) <EOL> seg_slice . save ( ) <EOL> else : <EOL> for i_slice , seg_slice in enumerate ( seg . data ) : <EOL> if i_slice < <NUM_LIT:10> : <EOL> i_slice_str = str ( i_slice ) <EOL> i_slice_str = '<STR_LIT:0>' + i_slice_str <EOL> else : <EOL> i_slice_str = str ( i_slice ) <EOL> seg_slice = Image ( param = seg_slice , absolutepath = dic_by_slice_dir + subject_dir + '<STR_LIT:/>' + subject_dir + <EOL> '<STR_LIT>' + i_slice_str + '<STR_LIT:_>' + level_label [ label_by_slice [ i_slice ] ] + '<STR_LIT>' , hdr = seg . hdr ) <EOL> if len ( seg_slice . hdr . get_zooms ( ) ) == <NUM_LIT:3> : <EOL> seg_slice . hdr . set_zooms ( slice_zoom ) <EOL> seg_slice . save ( ) <EOL> def resample_image ( fname , suffix = '<STR_LIT>' , binary = False , npx = <NUM_LIT> , npy = <NUM_LIT> , thr = <NUM_LIT:0.0> , interpolation = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> im_in = Image ( fname ) <EOL> orientation = get_orientation_3d ( im_in ) <EOL> if orientation != '<STR_LIT>' : <EOL> im_in = set_orientation ( im_in , '<STR_LIT>' ) <EOL> im_in . save ( ) <EOL> fname = im_in . absolutepath <EOL> nx , ny , nz , nt , px , py , pz , pt = im_in . dim <EOL> if round ( px , <NUM_LIT:2> ) != round ( npx , <NUM_LIT:2> ) or round ( py , <NUM_LIT:2> ) != round ( npy , <NUM_LIT:2> ) : <EOL> name_resample = sct . extract_fname ( fname ) [ <NUM_LIT:1> ] + suffix <EOL> if binary : <EOL> interpolation = '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + fname + '<STR_LIT>' + str ( npx ) + '<STR_LIT:x>' + str ( npy ) + '<STR_LIT:x>' + str ( pz ) + '<STR_LIT>' + name_resample + '<STR_LIT>' + interpolation ) <EOL> if binary : <EOL> sct . run ( '<STR_LIT>' + name_resample + '<STR_LIT>' + str ( thr ) + '<STR_LIT>' + name_resample ) <EOL> sct . run ( '<STR_LIT>' + name_resample + '<STR_LIT>' + name_resample ) <EOL> if orientation != '<STR_LIT>' : <EOL> im_resample = Image ( name_resample ) <EOL> im_resample = set_orientation ( im_resample , orientation ) <EOL> im_resample . save ( ) <EOL> name_resample = im_resample . absolutepath <EOL> return name_resample <EOL> else : <EOL> if orientation != '<STR_LIT>' : <EOL> im_in = set_orientation ( im_in , orientation ) <EOL> im_in . save ( ) <EOL> fname = im_in . absolutepath <EOL> sct . printv ( '<STR_LIT>' + str ( npx ) + '<STR_LIT:x>' + str ( npy ) + '<STR_LIT>' ) <EOL> return fname <EOL> def dataset_preprocessing ( path_to_dataset , denoise = True ) : <EOL> """<STR_LIT>""" <EOL> axial_pix_dim = <NUM_LIT> <EOL> model_image_size = <NUM_LIT> <EOL> interpolation = '<STR_LIT>' <EOL> original_path = os . path . abspath ( '<STR_LIT:.>' ) <EOL> for subject_dir in os . listdir ( path_to_dataset ) : <EOL> if os . path . isdir ( path_to_dataset + '<STR_LIT:/>' + subject_dir ) : <EOL> os . chdir ( path_to_dataset + '<STR_LIT:/>' + subject_dir ) <EOL> t2star = '<STR_LIT>' <EOL> scseg = '<STR_LIT>' <EOL> gmseg = '<STR_LIT>' <EOL> for file_name in os . listdir ( '<STR_LIT:.>' ) : <EOL> if '<STR_LIT>' in file_name : <EOL> t2star = file_name <EOL> elif '<STR_LIT>' in file_name : <EOL> gmseg = file_name <EOL> elif '<STR_LIT>' in file_name and '<STR_LIT>' not in file_name : <EOL> scseg = file_name <EOL> new_names = [ ] <EOL> for f_name in [ t2star , scseg , gmseg ] : <EOL> im = Image ( f_name ) <EOL> orientation = get_orientation_3d ( im ) <EOL> if orientation != '<STR_LIT>' : <EOL> im = set_orientation ( im , '<STR_LIT>' ) <EOL> new_names . append ( im . absolutepath ) <EOL> im . save ( ) <EOL> else : <EOL> new_names . append ( f_name ) <EOL> t2star = new_names [ <NUM_LIT:0> ] <EOL> scseg = new_names [ <NUM_LIT:1> ] <EOL> gmseg = new_names [ <NUM_LIT:2> ] <EOL> t2star = resample_image ( t2star , npx = axial_pix_dim , npy = axial_pix_dim , interpolation = interpolation ) <EOL> scseg = resample_image ( scseg , npx = axial_pix_dim , npy = axial_pix_dim , binary = True ) <EOL> gmseg = resample_image ( gmseg , npx = axial_pix_dim , npy = axial_pix_dim , binary = True ) <EOL> if denoise : <EOL> from sct_maths import denoise_ornlm <EOL> t2star_im = Image ( t2star ) <EOL> t2star_im . data = denoise_ornlm ( t2star_im . data ) <EOL> t2star_im . save ( ) <EOL> mask_box , fname_seg_in_IRP = crop_t2_star ( t2star , scseg , box_size = model_image_size ) <EOL> gmseg_im = Image ( gmseg ) <EOL> mask_im = Image ( mask_box ) <EOL> gmseg_im . crop_and_stack ( mask_im , suffix = '<STR_LIT>' , save = True ) <EOL> sct . run ( '<STR_LIT>' + sct . extract_fname ( gmseg ) [ <NUM_LIT:1> ] + '<STR_LIT>' ) <EOL> os . chdir ( original_path ) <EOL> save_by_slice ( path_to_dataset ) <EOL> def compute_level_file ( t2star_fname , t2star_sc_seg_fname , t2_fname , t2_seg_fname , landmarks_fname ) : <EOL> """<STR_LIT>""" <EOL> cmd_register_template = '<STR_LIT>' + t2_fname + '<STR_LIT>' + t2_seg_fname + '<STR_LIT>' + landmarks_fname <EOL> sct . run ( cmd_register_template ) <EOL> cmd_warp_template = '<STR_LIT>' + t2_fname + '<STR_LIT>' <EOL> sct . run ( cmd_warp_template ) <EOL> cmd_register_multimodal = '<STR_LIT>' + t2star_fname + '<STR_LIT>' + t2star_sc_seg_fname + '<STR_LIT>' <EOL> sct . run ( cmd_register_multimodal ) <EOL> multimodal_warp_name = '<STR_LIT>' + t2star_fname <EOL> total_warp_name = '<STR_LIT>' <EOL> cmd_concat = '<STR_LIT>' + multimodal_warp_name + '<STR_LIT>' + t2star_fname + '<STR_LIT>' + total_warp_name <EOL> sct . run ( cmd_concat ) <EOL> cmd_warp = '<STR_LIT>' + t2star_fname + '<STR_LIT>' + total_warp_name + '<STR_LIT>' <EOL> sct . run ( cmd_warp ) <EOL> sct . run ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> def inverse_square_crop ( croped_image , square_mask ) : <EOL> if square_mask . orientation != '<STR_LIT>' : <EOL> square_mask = set_orientation ( square_mask , '<STR_LIT>' ) <EOL> nz_coord = square_mask . getNonZeroCoordinates ( ) <EOL> assert len ( nz_coord ) == croped_image . data . size <EOL> dim = len ( croped_image . data . shape ) <EOL> inverse_croped = square_mask . copy ( ) <EOL> if dim == <NUM_LIT:3> : <EOL> done_slices = [ ] <EOL> for coord in nz_coord : <EOL> if coord . x not in done_slices : <EOL> inverse_croped . data [ coord . x , coord . y : coord . y + croped_image . data . shape [ <NUM_LIT:1> ] , coord . z : coord . z + croped_image . data . shape [ <NUM_LIT:2> ] ] = croped_image . data [ coord . x ] <EOL> done_slices . append ( coord . x ) <EOL> elif dim == <NUM_LIT:2> : <EOL> coord = nz_coord [ <NUM_LIT:0> ] <EOL> inverse_croped . data [ coord . x : coord . x + croped_image . data . shape [ <NUM_LIT:0> ] , coord . y : coord . y + croped_image . data . shape [ <NUM_LIT:1> ] ] = croped_image . data <EOL> inverse_croped . setFileName ( croped_image . file_name + '<STR_LIT>' + croped_image . ext ) <EOL> return inverse_croped <EOL> def leave_one_out_by_subject ( dic_path , dic_3d , denoising = True , reg = '<STR_LIT>' , metric = '<STR_LIT>' , use_levels = True , weight = <NUM_LIT> , eq = <NUM_LIT:1> , mode_weighted_sim = False , weighted_label_fusion = False ) : <EOL> """<STR_LIT>""" <EOL> import time <EOL> from msct_multiatlas_seg import Model , SegmentationParam , SupervisedSegmentationMethod <EOL> from sct_segment_graymatter import FullGmSegmentation <EOL> init = time . time ( ) <EOL> wm_dice_file = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> gm_dice_file = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> wm_csa_file = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> gm_csa_file = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> hd_file = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> n_slices = <NUM_LIT:0> <EOL> e = None <EOL> 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>' , <NUM_LIT:10> : '<STR_LIT>' , <NUM_LIT:11> : '<STR_LIT>' , <NUM_LIT:12> : '<STR_LIT>' , <NUM_LIT> : '<STR_LIT>' } <EOL> gm_diff_by_level = { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } <EOL> wm_diff_by_level = { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } <EOL> for subject_dir in os . listdir ( dic_path ) : <EOL> subject_path = dic_path + '<STR_LIT:/>' + subject_dir <EOL> if os . path . isdir ( subject_path ) : <EOL> try : <EOL> tmp_dir = '<STR_LIT>' + subject_dir + '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + tmp_dir ) <EOL> tmp_dic_name = '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + dic_path + '<STR_LIT>' + tmp_dir + '<STR_LIT:/>' + tmp_dic_name + '<STR_LIT:/>' ) <EOL> sct . run ( '<STR_LIT>' + dic_3d + '<STR_LIT:/>' + subject_dir + '<STR_LIT>' + tmp_dir + '<STR_LIT:/>' ) <EOL> sct . run ( '<STR_LIT>' + tmp_dir + '<STR_LIT:/>' + tmp_dic_name + '<STR_LIT:/>' + subject_dir + '<STR_LIT>' + tmp_dir + '<STR_LIT:/>' + subject_dir + '<STR_LIT>' ) <EOL> os . chdir ( tmp_dir ) <EOL> model_param = SegmentationParam ( ) <EOL> model_param . path_model = tmp_dic_name <EOL> model_param . todo_model = '<STR_LIT>' <EOL> model_param . weight_gamma = float ( weight ) <EOL> model_param . use_levels = use_levels <EOL> model_param . res_type = '<STR_LIT>' <EOL> model_param . reg = reg . split ( '<STR_LIT::>' ) <EOL> model_param . reg_metric = metric <EOL> model_param . equation_id = eq <EOL> model_param . mode_weight_similarity = mode_weighted_sim <EOL> model_param . weight_label_fusion = weighted_label_fusion <EOL> model_param . target_denoising = denoising <EOL> model = Model ( model_param = model_param , k = <NUM_LIT> ) <EOL> n_slices_model = model . dictionary . J <EOL> target = '<STR_LIT>' <EOL> sc_seg = '<STR_LIT>' <EOL> ref_gm_seg_im = '<STR_LIT>' <EOL> level = '<STR_LIT>' <EOL> for file_name in os . listdir ( subject_dir ) : <EOL> if '<STR_LIT>' in file_name : <EOL> target = subject_dir + '<STR_LIT:/>' + file_name <EOL> elif '<STR_LIT>' in file_name : <EOL> level = subject_dir + '<STR_LIT:/>' + file_name <EOL> elif '<STR_LIT>' in file_name : <EOL> ref_gm_seg_im = subject_dir + '<STR_LIT:/>' + file_name <EOL> elif '<STR_LIT>' in file_name : <EOL> sc_seg = subject_dir + '<STR_LIT:/>' + file_name <EOL> full_gmseg = FullGmSegmentation ( target , sc_seg , None , level , ref_gm_seg = ref_gm_seg_im , model = model , param = model_param ) <EOL> subject_dice = open ( full_gmseg . dice_name , '<STR_LIT:r>' ) <EOL> dice_lines_list = subject_dice . readlines ( ) <EOL> subject_dice . close ( ) <EOL> gm_dices = None <EOL> wm_dices = None <EOL> n_subject_slices = len ( full_gmseg . gm_seg . target_seg_methods . target ) <EOL> n_slices += n_subject_slices <EOL> for i , line in enumerate ( dice_lines_list ) : <EOL> if '<STR_LIT>' in line : <EOL> gm_dices = dice_lines_list [ i + <NUM_LIT:10> : i + <NUM_LIT:10> + n_subject_slices ] <EOL> if '<STR_LIT>' in line : <EOL> wm_dices = dice_lines_list [ i + <NUM_LIT:10> : i + <NUM_LIT:10> + n_subject_slices ] <EOL> subject_slices_levels = { } <EOL> for slice_dice in wm_dices : <EOL> target_slice , wm_dice = slice_dice [ : - <NUM_LIT:1> ] . split ( '<STR_LIT:U+0020>' ) <EOL> if int ( target_slice ) < <NUM_LIT:10> : <EOL> target_slice = '<STR_LIT>' + target_slice <EOL> else : <EOL> target_slice = '<STR_LIT>' + target_slice <EOL> slice_level = '<STR_LIT>' <EOL> for file_name in os . listdir ( '<STR_LIT>' + subject_dir + '<STR_LIT>' ) : <EOL> if target_slice in file_name : <EOL> slice_level = file_name [ file_name . find ( target_slice ) + <NUM_LIT:8> : file_name . find ( target_slice ) + <NUM_LIT:10> ] <EOL> subject_slices_levels [ target_slice ] = slice_level <EOL> wm_dice_file . write ( subject_dir + '<STR_LIT:U+0020>' + target_slice + '<STR_LIT:U+0020>' + slice_level + '<STR_LIT>' + wm_dice + '<STR_LIT>' + str ( n_slices_model ) + '<STR_LIT:\n>' ) <EOL> for slice_dice in gm_dices : <EOL> target_slice , gm_dice = slice_dice [ : - <NUM_LIT:1> ] . split ( '<STR_LIT:U+0020>' ) <EOL> if int ( target_slice ) < <NUM_LIT:10> : <EOL> target_slice = '<STR_LIT>' + target_slice <EOL> else : <EOL> target_slice = '<STR_LIT>' + target_slice <EOL> slice_level = subject_slices_levels [ target_slice ] <EOL> gm_dice_file . write ( subject_dir + '<STR_LIT:U+0020>' + target_slice + '<STR_LIT:U+0020>' + slice_level + '<STR_LIT>' + gm_dice + '<STR_LIT>' + str ( n_slices_model ) + '<STR_LIT:\n>' ) <EOL> subject_hd = open ( full_gmseg . hausdorff_name , '<STR_LIT:r>' ) <EOL> hd_res_list = subject_hd . readlines ( ) [ <NUM_LIT:1> : - <NUM_LIT:4> ] <EOL> for line in hd_res_list : <EOL> n_slice , res_slice = line . split ( '<STR_LIT::>' ) <EOL> hd , med1 , med2 = res_slice . split ( '<STR_LIT:->' ) <EOL> n_slice = n_slice [ - <NUM_LIT:1> : ] <EOL> med1 = float ( med1 ) <EOL> med2 = float ( med2 [ : - <NUM_LIT:2> ] ) <EOL> if int ( n_slice ) < <NUM_LIT:10> : <EOL> target_slice = '<STR_LIT>' + n_slice <EOL> else : <EOL> target_slice = '<STR_LIT>' + n_slice <EOL> slice_level = subject_slices_levels [ target_slice ] <EOL> hd_file . write ( subject_dir + '<STR_LIT:U+0020>' + target_slice + '<STR_LIT:U+0020>' + slice_level + '<STR_LIT>' + str ( hd ) + '<STR_LIT>' + str ( max ( med1 , med2 ) ) + '<STR_LIT:\n>' ) <EOL> print '<STR_LIT>' <EOL> path_validation = full_gmseg . tmp_dir + '<STR_LIT>' <EOL> ref_gm_seg_im = Image ( path_validation + '<STR_LIT>' ) <EOL> ref_wm_seg_im = Image ( path_validation + '<STR_LIT>' ) <EOL> res_gm_seg_im = Image ( path_validation + '<STR_LIT>' ) <EOL> res_wm_seg_im = Image ( path_validation + '<STR_LIT>' ) <EOL> ref_gm_seg_im . change_orientation ( '<STR_LIT>' ) <EOL> ref_wm_seg_im . change_orientation ( '<STR_LIT>' ) <EOL> res_gm_seg_im . change_orientation ( '<STR_LIT>' ) <EOL> res_wm_seg_im . change_orientation ( '<STR_LIT>' ) <EOL> for i_slice in range ( len ( ref_gm_seg_im . data ) ) : <EOL> slice_gm_error = abs ( ref_gm_seg_im . data [ i_slice ] - res_gm_seg_im . data [ i_slice ] ) <EOL> slice_wm_error = abs ( ref_wm_seg_im . data [ i_slice ] - res_wm_seg_im . data [ i_slice ] ) <EOL> if int ( i_slice ) < <NUM_LIT:10> : <EOL> target_slice = '<STR_LIT>' + str ( i_slice ) <EOL> else : <EOL> target_slice = '<STR_LIT>' + str ( i_slice ) <EOL> slice_level = subject_slices_levels [ target_slice ] <EOL> gm_diff_by_level [ slice_level ] . append ( slice_gm_error ) <EOL> wm_diff_by_level [ slice_level ] . append ( slice_wm_error ) <EOL> sct . run ( '<STR_LIT>' + full_gmseg . res_names [ '<STR_LIT>' ] + '<STR_LIT>' ) <EOL> tmp_csa_file = open ( '<STR_LIT>' ) <EOL> csa_lines = tmp_csa_file . readlines ( ) <EOL> tmp_csa_file . close ( ) <EOL> for slice_csa in csa_lines : <EOL> target_slice , wm_csa = slice_csa . split ( '<STR_LIT:U+002C>' ) <EOL> if int ( target_slice ) < <NUM_LIT:10> : <EOL> target_slice = '<STR_LIT>' + target_slice <EOL> else : <EOL> target_slice = '<STR_LIT>' + target_slice <EOL> slice_level = subject_slices_levels [ target_slice ] <EOL> wm_csa_file . write ( subject_dir + '<STR_LIT:U+0020>' + target_slice + '<STR_LIT:U+0020>' + slice_level + '<STR_LIT>' + wm_csa [ : - <NUM_LIT:1> ] + '<STR_LIT:\n>' ) <EOL> sct . run ( '<STR_LIT>' ) <EOL> sct . run ( '<STR_LIT>' + full_gmseg . res_names [ '<STR_LIT>' ] + '<STR_LIT>' ) <EOL> tmp_csa_file = open ( '<STR_LIT>' ) <EOL> csa_lines = tmp_csa_file . readlines ( ) <EOL> tmp_csa_file . close ( ) <EOL> for slice_csa in csa_lines : <EOL> target_slice , gm_csa = slice_csa . split ( '<STR_LIT:U+002C>' ) <EOL> if int ( target_slice ) < <NUM_LIT:10> : <EOL> target_slice = '<STR_LIT>' + target_slice <EOL> else : <EOL> target_slice = '<STR_LIT>' + target_slice <EOL> slice_level = subject_slices_levels [ target_slice ] <EOL> gm_csa_file . write ( subject_dir + '<STR_LIT:U+0020>' + target_slice + '<STR_LIT:U+0020>' + slice_level + '<STR_LIT>' + gm_csa [ : - <NUM_LIT:1> ] + '<STR_LIT:\n>' ) <EOL> sct . run ( '<STR_LIT>' ) <EOL> os . chdir ( '<STR_LIT:..>' ) <EOL> except Exception , e : <EOL> sct . printv ( '<STR_LIT>' , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> print e <EOL> for l , level_error in gm_diff_by_level . items ( ) : <EOL> try : <EOL> n = len ( level_error ) <EOL> if n != <NUM_LIT:0> : <EOL> Image ( param = sum ( level_error ) / n , absolutepath = '<STR_LIT>' + str ( l ) + '<STR_LIT>' ) . save ( ) <EOL> except ZeroDivisionError : <EOL> sct . printv ( '<STR_LIT>' + str ( l ) , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> for l , level_error in wm_diff_by_level . items ( ) : <EOL> try : <EOL> n = len ( level_error ) <EOL> if n != <NUM_LIT:0> : <EOL> Image ( param = sum ( level_error ) / n , absolutepath = '<STR_LIT>' + str ( l ) + '<STR_LIT>' ) . save ( ) <EOL> except ZeroDivisionError : <EOL> sct . printv ( '<STR_LIT>' + str ( l ) , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> if e is None : <EOL> wm_dice_file . close ( ) <EOL> gm_dice_file . close ( ) <EOL> wm_csa_file . close ( ) <EOL> gm_csa_file . close ( ) <EOL> hd_file . close ( ) <EOL> t = time . time ( ) - init <EOL> print '<STR_LIT>' + str ( t ) + '<STR_LIT>' <EOL> def compute_error_map ( data_path ) : <EOL> error_map_sum = None <EOL> error_map_abs_sum = None <EOL> first = True <EOL> n_slices = <NUM_LIT:0> <EOL> os . chdir ( data_path ) <EOL> for file_name in os . listdir ( '<STR_LIT:.>' ) : <EOL> if os . path . isdir ( file_name ) and file_name != '<STR_LIT>' : <EOL> os . chdir ( file_name ) <EOL> res = '<STR_LIT>' <EOL> ref_wm_seg = '<STR_LIT>' <EOL> for slice_file in os . listdir ( '<STR_LIT:.>' ) : <EOL> if '<STR_LIT>' in slice_file : <EOL> res = slice_file <EOL> elif '<STR_LIT>' in slice_file : <EOL> ref_wm_seg = slice_file <EOL> res_im = Image ( res ) <EOL> ref_wm_seg_im = Image ( ref_wm_seg ) <EOL> if first : <EOL> error_map_sum = np . zeros ( ref_wm_seg_im . data . shape ) <EOL> error_map_abs_sum = np . zeros ( ref_wm_seg_im . data . shape ) <EOL> first = False <EOL> error_3d = ( ref_wm_seg_im . data - res_im . data ) + <NUM_LIT:1> <EOL> error_3d_abs = abs ( ref_wm_seg_im . data - res_im . data ) <EOL> error_map_sum += error_3d <EOL> error_map_abs_sum += error_3d_abs <EOL> n_slices += <NUM_LIT:1> <EOL> os . chdir ( '<STR_LIT:..>' ) <EOL> Image ( param = ( error_map_sum / n_slices ) - <NUM_LIT:1> , absolutepath = '<STR_LIT>' ) . save ( ) <EOL> Image ( param = error_map_abs_sum / n_slices , absolutepath = '<STR_LIT>' ) . save ( ) <EOL> '''<STR_LIT>''' <EOL> def compute_error_map_by_level ( data_path ) : <EOL> original_path = os . path . abspath ( '<STR_LIT:.>' ) <EOL> diff_by_level = { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } <EOL> error_map_abs_sum = None <EOL> first = True <EOL> n_slices = <NUM_LIT:0> <EOL> os . chdir ( data_path ) <EOL> for subject_dir in os . listdir ( '<STR_LIT:.>' ) : <EOL> if os . path . isdir ( subject_dir ) and subject_dir != '<STR_LIT>' and subject_dir != '<STR_LIT>' : <EOL> os . chdir ( subject_dir ) <EOL> subject = '<STR_LIT:_>' . join ( subject_dir . split ( '<STR_LIT:_>' ) [ <NUM_LIT:1> : <NUM_LIT:3> ] ) <EOL> if '<STR_LIT>' in subject : <EOL> subject = subject . split ( '<STR_LIT:_>' ) [ <NUM_LIT:0> ] <EOL> sq_mask = '<STR_LIT>' <EOL> level = '<STR_LIT>' <EOL> res_wm_seg = '<STR_LIT>' <EOL> res_dir = None <EOL> for fname in os . listdir ( '<STR_LIT:.>' ) : <EOL> if '<STR_LIT>' + subject in fname and os . path . isdir ( fname ) : <EOL> res_dir = fname <EOL> os . chdir ( res_dir ) <EOL> for fname in os . listdir ( '<STR_LIT:.>' ) : <EOL> if '<STR_LIT>' in fname and '<STR_LIT>' not in fname and '<STR_LIT>' in fname : <EOL> level = fname <EOL> elif '<STR_LIT>' in fname and '<STR_LIT>' in fname : <EOL> sq_mask = fname <EOL> elif '<STR_LIT>' in fname and '<STR_LIT>' in fname and '<STR_LIT>' not in fname : <EOL> res_wm_seg = fname <EOL> if res_wm_seg != '<STR_LIT>' and level != '<STR_LIT>' and sq_mask != '<STR_LIT>' : <EOL> ref_wm_seg = '<STR_LIT>' <EOL> status , ref_ori = sct . run ( '<STR_LIT>' + ref_wm_seg + '<STR_LIT>' ) <EOL> if ref_ori != '<STR_LIT>' : <EOL> sct . run ( '<STR_LIT>' + ref_wm_seg + '<STR_LIT>' ) <EOL> ref_wm_seg = sct . extract_fname ( ref_wm_seg ) [ <NUM_LIT:0> ] + sct . extract_fname ( ref_wm_seg ) [ <NUM_LIT:1> ] + '<STR_LIT>' + sct . extract_fname ( ref_wm_seg ) [ <NUM_LIT:2> ] <EOL> level_im = Image ( level ) <EOL> ref_wm_seg_im = Image ( ref_wm_seg ) <EOL> mask_im = Image ( sq_mask ) <EOL> ref_wm_seg_im . crop_and_stack ( mask_im , suffix = '<STR_LIT>' , save = True ) <EOL> ref_wm_seg = ref_wm_seg_im . absolutepath <EOL> res_wm_seg_im = Image ( res_wm_seg ) <EOL> label_by_slice = { } <EOL> 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> nz_coord = level_im . getNonZeroCoordinates ( ) <EOL> for i_level_slice , level_slice in enumerate ( level_im . 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> label_by_slice [ i_level_slice ] = int ( round ( sum ( nz_val ) / len ( nz_val ) ) ) <EOL> except ZeroDivisionError : <EOL> sct . printv ( '<STR_LIT>' + str ( i_level_slice ) + '<STR_LIT>' + subject_dir ) <EOL> label_by_slice [ i_level_slice ] = <NUM_LIT:0> <EOL> for i_slice in range ( len ( ref_wm_seg_im . data ) ) : <EOL> if first : <EOL> error_map_abs_sum = np . zeros ( ref_wm_seg_im . data [ i_slice ] . shape ) <EOL> first = False <EOL> error_3d_abs = abs ( ref_wm_seg_im . data [ i_slice ] - res_wm_seg_im . data [ i_slice ] ) <EOL> slice_level = level_label [ label_by_slice [ i_slice ] ] <EOL> diff_by_level [ slice_level ] . append ( error_3d_abs ) <EOL> error_map_abs_sum += error_3d_abs <EOL> n_slices += <NUM_LIT:1> <EOL> os . chdir ( '<STR_LIT>' ) <EOL> for l , level_error in diff_by_level . items ( ) : <EOL> try : <EOL> n = len ( level_error ) <EOL> if n != <NUM_LIT:0> : <EOL> Image ( param = sum ( level_error ) / n , absolutepath = '<STR_LIT>' + str ( l ) + '<STR_LIT>' ) . save ( ) <EOL> except ZeroDivisionError : <EOL> sct . printv ( '<STR_LIT>' + str ( l ) , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> Image ( param = error_map_abs_sum / n_slices , absolutepath = '<STR_LIT>' ) . save ( ) <EOL> os . chdir ( '<STR_LIT:..>' ) <EOL> os . chdir ( original_path ) <EOL> def compute_hausdorff_dist_on_loocv_results ( data_path ) : <EOL> import sct_compute_hausdorff_distance as hd <EOL> import xlsxwriter as xl <EOL> original_path = os . path . abspath ( '<STR_LIT:.>' ) <EOL> os . chdir ( data_path ) <EOL> workbook = xl . Workbook ( '<STR_LIT>' , { '<STR_LIT>' : True } ) <EOL> w1 = workbook . add_worksheet ( '<STR_LIT>' ) <EOL> w2 = workbook . add_worksheet ( '<STR_LIT>' ) <EOL> bold = workbook . add_format ( { '<STR_LIT>' : True , '<STR_LIT>' : True } ) <EOL> w1 . write ( <NUM_LIT:1> , <NUM_LIT:0> , '<STR_LIT>' , bold ) <EOL> w1 . write ( <NUM_LIT:1> , <NUM_LIT:1> , '<STR_LIT>' , bold ) <EOL> w1 . write ( <NUM_LIT:1> , <NUM_LIT:2> , '<STR_LIT>' , bold ) <EOL> w2 . write ( <NUM_LIT:1> , <NUM_LIT:0> , '<STR_LIT>' , bold ) <EOL> w2 . write ( <NUM_LIT:1> , <NUM_LIT:1> , '<STR_LIT>' , bold ) <EOL> w2 . write ( <NUM_LIT:1> , <NUM_LIT:2> , '<STR_LIT>' , bold ) <EOL> first = True <EOL> col1 = <NUM_LIT:3> <EOL> col2 = <NUM_LIT:3> <EOL> for modality in os . listdir ( '<STR_LIT:.>' ) : <EOL> if os . path . isdir ( modality ) and '<STR_LIT>' not in modality and '<STR_LIT>' not in modality : <EOL> os . chdir ( modality ) <EOL> w1 . write ( <NUM_LIT:0> , col1 , modality , bold ) <EOL> w2 . merge_range ( <NUM_LIT:0> , col2 , <NUM_LIT:0> , col2 + <NUM_LIT:1> , modality , bold ) <EOL> row = <NUM_LIT:2> <EOL> for subject_dir in os . listdir ( '<STR_LIT:.>' ) : <EOL> if os . path . isdir ( subject_dir ) and '<STR_LIT>' not in subject_dir and '<STR_LIT>' not in subject_dir : <EOL> os . chdir ( subject_dir ) <EOL> subject = '<STR_LIT:_>' . join ( subject_dir . split ( '<STR_LIT:_>' ) [ <NUM_LIT:1> : <NUM_LIT:3> ] ) <EOL> if '<STR_LIT>' in subject : <EOL> subject = subject . split ( '<STR_LIT:_>' ) [ <NUM_LIT:0> ] <EOL> sq_mask = '<STR_LIT>' <EOL> level = '<STR_LIT>' <EOL> res_gm_seg = '<STR_LIT>' <EOL> res_dir = None <EOL> for fname in os . listdir ( '<STR_LIT:.>' ) : <EOL> if '<STR_LIT>' + subject in fname and os . path . isdir ( fname ) : <EOL> res_dir = fname <EOL> os . chdir ( res_dir ) <EOL> for fname in os . listdir ( '<STR_LIT:.>' ) : <EOL> if '<STR_LIT>' in fname and '<STR_LIT>' not in fname and '<STR_LIT>' in fname : <EOL> level = fname <EOL> elif '<STR_LIT>' in fname and '<STR_LIT>' in fname : <EOL> sq_mask = fname <EOL> elif '<STR_LIT>' in fname and '<STR_LIT>' not in fname and '<STR_LIT>' not in fname : <EOL> res_gm_seg = fname <EOL> if res_gm_seg != '<STR_LIT>' and sq_mask != '<STR_LIT>' and level != '<STR_LIT>' : <EOL> ref_gm_seg = '<STR_LIT>' <EOL> status , ref_ori = sct . run ( '<STR_LIT>' + ref_gm_seg + '<STR_LIT>' ) <EOL> if ref_ori != '<STR_LIT>' : <EOL> sct . run ( '<STR_LIT>' + ref_gm_seg + '<STR_LIT>' ) <EOL> ref_gm_seg = sct . extract_fname ( ref_gm_seg ) [ <NUM_LIT:0> ] + sct . extract_fname ( ref_gm_seg ) [ <NUM_LIT:1> ] + '<STR_LIT>' + sct . extract_fname ( ref_gm_seg ) [ <NUM_LIT:2> ] <EOL> level_im = Image ( level ) <EOL> ref_gm_seg_im = Image ( ref_gm_seg ) <EOL> mask_im = Image ( sq_mask ) <EOL> ref_gm_seg_im . crop_and_stack ( mask_im , suffix = '<STR_LIT>' , save = True ) <EOL> ref_gm_seg = ref_gm_seg_im . absolutepath <EOL> sct . run ( '<STR_LIT>' + res_gm_seg + '<STR_LIT>' + res_gm_seg ) <EOL> resample_to = <NUM_LIT:0.1> <EOL> ref_gm_seg_im = Image ( resample_image ( ref_gm_seg , binary = True , thr = <NUM_LIT:0.5> , npx = resample_to , npy = resample_to ) ) <EOL> res_gm_seg_im = Image ( resample_image ( res_gm_seg , binary = True , thr = <NUM_LIT:0.5> , npx = resample_to , npy = resample_to ) ) <EOL> compute_param = hd . Param <EOL> compute_param . thinning = True <EOL> compute_param . verbose = <NUM_LIT:2> <EOL> comp = hd . ComputeDistances ( res_gm_seg_im , im2 = ref_gm_seg_im , param = compute_param ) <EOL> res_fic = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> res_fic . write ( comp . res ) <EOL> res_fic . write ( '<STR_LIT>' + res_gm_seg_im . file_name ) <EOL> res_fic . write ( '<STR_LIT>' + ref_gm_seg_im . file_name ) <EOL> res_fic . close ( ) <EOL> label_by_slice = { } <EOL> 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> nz_coord = level_im . getNonZeroCoordinates ( ) <EOL> for i_level_slice , level_slice in enumerate ( level_im . 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> label_by_slice [ i_level_slice ] = int ( round ( sum ( nz_val ) / len ( nz_val ) ) ) <EOL> except ZeroDivisionError : <EOL> sct . printv ( '<STR_LIT>' + str ( i_level_slice ) + '<STR_LIT>' + subject_dir ) <EOL> label_by_slice [ i_level_slice ] = <NUM_LIT:0> <EOL> for i_slice in range ( len ( ref_gm_seg_im . data ) ) : <EOL> slice_level = level_label [ label_by_slice [ i_slice ] ] <EOL> if first : <EOL> w1 . write ( row , <NUM_LIT:0> , subject ) <EOL> w1 . write ( row , <NUM_LIT:1> , i_slice ) <EOL> w1 . write ( row , <NUM_LIT:2> , slice_level ) <EOL> w2 . write ( row , <NUM_LIT:0> , subject ) <EOL> w2 . write ( row , <NUM_LIT:1> , i_slice ) <EOL> w2 . write ( row , <NUM_LIT:2> , slice_level ) <EOL> w1 . write ( row , col1 , comp . distances [ i_slice ] . H * comp . dim_pix ) <EOL> med1 = np . median ( comp . dist1_distribution [ i_slice ] ) <EOL> med2 = np . median ( comp . dist2_distribution [ i_slice ] ) <EOL> w2 . write ( row , col2 , med1 * comp . dim_pix ) <EOL> w2 . write ( row , col2 + <NUM_LIT:1> , med2 * comp . dim_pix ) <EOL> row += <NUM_LIT:1> <EOL> os . chdir ( '<STR_LIT>' ) <EOL> os . chdir ( '<STR_LIT:..>' ) <EOL> col1 += <NUM_LIT:1> <EOL> col2 += <NUM_LIT:2> <EOL> first = False <EOL> workbook . close ( ) <EOL> os . chdir ( '<STR_LIT>' ) <EOL> os . chdir ( original_path ) <EOL> def compute_level_similarities ( data_path ) : <EOL> similarity_sum = <NUM_LIT:0> <EOL> n_slices = <NUM_LIT:0> <EOL> os . chdir ( data_path ) <EOL> level_file = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> for file_name in os . listdir ( '<STR_LIT:.>' ) : <EOL> if os . path . isdir ( file_name ) and file_name != '<STR_LIT>' : <EOL> os . chdir ( file_name ) <EOL> for im_file in os . listdir ( '<STR_LIT:.>' ) : <EOL> if '<STR_LIT>' in im_file : <EOL> ref_seg = im_file <EOL> subject = ref_seg [ : <NUM_LIT:8> ] <EOL> n_slice = ref_seg [ <NUM_LIT> : <NUM_LIT:16> ] <EOL> slice_level = ref_seg [ - <NUM_LIT> : - <NUM_LIT:11> ] <EOL> selected_slices_levels = open ( '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> line_list = selected_slices_levels . read ( ) . split ( "<STR_LIT:'>" ) <EOL> selected_slices_levels . close ( ) <EOL> levels = [ ] <EOL> similar_levels = <NUM_LIT:0> <EOL> for s in line_list : <EOL> if s in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> levels . append ( s ) <EOL> for l in levels : <EOL> if l == slice_level : <EOL> similar_levels += <NUM_LIT:1> <EOL> slice_similarity = float ( similar_levels ) / len ( levels ) <EOL> similarity_sum += slice_similarity <EOL> level_file . write ( subject + '<STR_LIT>' + n_slice + '<STR_LIT>' + str ( slice_similarity * <NUM_LIT:100> ) + '<STR_LIT>' + str ( slice_level ) + '<STR_LIT>' ) <EOL> os . chdir ( '<STR_LIT:..>' ) <EOL> level_file . write ( '<STR_LIT>' + str ( similarity_sum / n_slices ) + '<STR_LIT>' ) <EOL> level_file . close ( ) <EOL> os . chdir ( '<STR_LIT:..>' ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> parser = get_parser ( ) <EOL> arguments = parser . parse ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> if "<STR_LIT>" in arguments : <EOL> crop_t2_star_pipeline ( arguments [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT>" in arguments : <EOL> dic_path , dic_3d , denoising , reg , metric , use_levels , weight , eq , mode_weighted_sim , weighted_label_fusion = arguments [ '<STR_LIT>' ] <EOL> leave_one_out_by_subject ( dic_path , dic_3d , denoising = bool ( int ( denoising ) ) , reg = reg , metric = metric , weight = float ( weight ) , eq = int ( eq ) , mode_weighted_sim = bool ( int ( mode_weighted_sim ) ) , weighted_label_fusion = bool ( int ( weighted_label_fusion ) ) ) <EOL> if "<STR_LIT>" in arguments : <EOL> compute_error_map_by_level ( arguments [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT>" in arguments : <EOL> compute_hausdorff_dist_on_loocv_results ( arguments [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT>" in arguments : <EOL> save_by_slice ( arguments [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT>" in arguments : <EOL> dataset_preprocessing ( arguments [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT>" in arguments : <EOL> gmseg = arguments [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> gmseg_im = Image ( gmseg ) <EOL> scseg = arguments [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> scseg_im = Image ( scseg ) <EOL> inverse_gmseg_to_wmseg ( gmseg_im , scseg_im , sct . extract_fname ( gmseg ) [ <NUM_LIT:1> ] ) </s>
