<s> from msct_parser import Parser <EOL> from msct_image import Image <EOL> from msct_multiatlas_seg import SegmentationParam <EOL> import sct_utils as sct <EOL> import os <EOL> import sys <EOL> import time <EOL> class MultimodalRegistrationParam : <EOL> def __init__ ( self ) : <EOL> self . target = '<STR_LIT>' <EOL> self . anat = '<STR_LIT>' <EOL> self . target_seg = '<STR_LIT>' <EOL> self . anat_seg = '<STR_LIT>' <EOL> self . output = '<STR_LIT>' <EOL> self . warp_template2anat = '<STR_LIT>' <EOL> self . p = '<STR_LIT>' <EOL> class WmRegistrationParam : <EOL> def __init__ ( self ) : <EOL> self . debug = False <EOL> self . fname_fixed = '<STR_LIT>' <EOL> self . fname_moving = '<STR_LIT>' <EOL> self . fname_seg_fixed = '<STR_LIT>' <EOL> self . fname_seg_moving = '<STR_LIT>' <EOL> self . transformation = '<STR_LIT>' <EOL> self . metric = '<STR_LIT>' <EOL> self . gradient_step = '<STR_LIT>' <EOL> self . radius = '<STR_LIT:2>' <EOL> self . interpolation = '<STR_LIT>' <EOL> self . iteration = '<STR_LIT>' <EOL> self . fname_output = '<STR_LIT>' <EOL> self . padding = '<STR_LIT>' <EOL> def reg_multimodal_warp ( anat , target , anat_seg , target_seg , warp_template2anat , param ) : <EOL> status , output = sct . run ( '<STR_LIT>' + anat + '<STR_LIT>' + target + '<STR_LIT>' + anat_seg + '<STR_LIT>' + target_seg + '<STR_LIT>' + param . p ) <EOL> if status != <NUM_LIT:0> : <EOL> sct . printv ( '<STR_LIT>' , verbose , '<STR_LIT>' ) <EOL> sct . printv ( output , verbose , '<STR_LIT>' ) <EOL> return None <EOL> else : <EOL> warp_template2target = '<STR_LIT>' <EOL> warp_anat2target = '<STR_LIT>' + sct . extract_fname ( anat ) [ <NUM_LIT:1> ] + '<STR_LIT:2>' + sct . extract_fname ( target ) [ <NUM_LIT:1> ] + '<STR_LIT>' <EOL> warp_target2anat = '<STR_LIT>' + sct . extract_fname ( target ) [ <NUM_LIT:1> ] + '<STR_LIT:2>' + sct . extract_fname ( anat ) [ <NUM_LIT:1> ] + '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + warp_template2anat + '<STR_LIT:U+002C>' + warp_anat2target + '<STR_LIT>' + target + '<STR_LIT>' + warp_template2target ) <EOL> sct . run ( '<STR_LIT>' + target + '<STR_LIT>' + warp_template2target ) <EOL> label_folder = '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + label_folder ) <EOL> return warp_anat2target , warp_target2anat , label_folder <EOL> def segment_gm ( target_fname = '<STR_LIT>' , sc_seg_fname = '<STR_LIT>' , path_to_label = '<STR_LIT>' , param = None ) : <EOL> from sct_segment_graymatter import FullGmSegmentation <EOL> level_fname = path_to_label + '<STR_LIT>' <EOL> gmsegfull = FullGmSegmentation ( target_fname , sc_seg_fname , None , level_fname , param = param ) <EOL> return gmsegfull . res_names [ '<STR_LIT>' ] , gmsegfull . res_names [ '<STR_LIT>' ] <EOL> def wm_registration ( param , smooth = False , init = False ) : <EOL> path , fixed_name , ext = sct . extract_fname ( param . fname_fixed ) <EOL> path_moving , moving_name , ext = sct . extract_fname ( param . fname_moving ) <EOL> path , fixed_seg_name , ext = sct . extract_fname ( param . fname_seg_fixed ) <EOL> path_moving_seg , moving_seg_name , ext = sct . extract_fname ( param . fname_seg_moving ) <EOL> fixed_name_temp = fixed_name + "<STR_LIT>" <EOL> cmd = "<STR_LIT>" + fixed_name + ext + "<STR_LIT>" + fixed_name_temp + ext + "<STR_LIT>" + fixed_seg_name + ext + "<STR_LIT>" <EOL> sct . run ( cmd ) <EOL> fixed_name = fixed_name_temp <EOL> fixed_seg_name_temp = fixed_seg_name + "<STR_LIT>" <EOL> sct . run ( "<STR_LIT>" + fixed_seg_name + ext + "<STR_LIT>" + fixed_seg_name_temp + ext + "<STR_LIT>" + fixed_seg_name + ext + "<STR_LIT>" ) <EOL> fixed_seg_name = fixed_seg_name_temp <EOL> moving_name_pad = moving_name + "<STR_LIT>" <EOL> fixed_name_pad = fixed_name + "<STR_LIT>" <EOL> sct . run ( '<STR_LIT>' + path_moving + moving_name + ext + '<STR_LIT>' + moving_name_pad + ext + '<STR_LIT>' + str ( param . padding ) ) <EOL> sct . run ( '<STR_LIT>' + fixed_name + ext + '<STR_LIT>' + fixed_name_pad + ext + '<STR_LIT>' + str ( param . padding ) ) <EOL> moving_name = moving_name_pad <EOL> fixed_name = fixed_name_pad <EOL> moving_seg_name_pad = moving_seg_name + "<STR_LIT>" <EOL> sct . run ( '<STR_LIT>' + path_moving_seg + moving_seg_name + ext + '<STR_LIT>' + moving_seg_name_pad + ext + '<STR_LIT>' + str ( param . padding ) ) <EOL> moving_seg_name = moving_seg_name_pad <EOL> fixed_seg_name_pad = fixed_seg_name + "<STR_LIT>" <EOL> sct . run ( '<STR_LIT>' + fixed_seg_name + ext + '<STR_LIT>' + fixed_seg_name_pad + ext + '<STR_LIT>' + str ( param . padding ) ) <EOL> fixed_seg_name = fixed_seg_name_pad <EOL> if param . metric == '<STR_LIT>' : <EOL> old_min = <NUM_LIT:0> <EOL> old_max = <NUM_LIT:1> <EOL> new_min = <NUM_LIT:100> <EOL> new_max = <NUM_LIT:200> <EOL> fixed_im = Image ( fixed_name + ext ) <EOL> fixed_im . data = ( fixed_im . data - old_min ) * ( new_max - new_min ) / ( old_max - old_min ) + new_min <EOL> fixed_im . save ( ) <EOL> moving_im = Image ( moving_name + ext ) <EOL> moving_im . data = ( moving_im . data - old_min ) * ( new_max - new_min ) / ( old_max - old_min ) + new_min <EOL> moving_im . save ( ) <EOL> if smooth : <EOL> sct . printv ( '<STR_LIT>' , verbose , '<STR_LIT>' ) <EOL> fixed_smooth = fixed_name + '<STR_LIT>' <EOL> moving_smooth = moving_name + '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + fixed_name + ext + '<STR_LIT>' + fixed_smooth + ext ) <EOL> sct . run ( '<STR_LIT>' + moving_name + ext + '<STR_LIT>' + moving_smooth + ext ) <EOL> fixed_name = fixed_smooth <EOL> moving_name = moving_smooth <EOL> sct . printv ( '<STR_LIT>' , verbose , '<STR_LIT>' ) <EOL> moving_name_reg = moving_name + "<STR_LIT>" <EOL> param_multimodal_reg = '<STR_LIT>' <EOL> if param . transformation == '<STR_LIT>' : <EOL> transfo_params = '<STR_LIT>' <EOL> elif param . transformation == '<STR_LIT>' : <EOL> transfo_params = '<STR_LIT>' <EOL> else : <EOL> transfo_params = '<STR_LIT>' <EOL> cmd = '<STR_LIT>' + moving_name + ext + '<STR_LIT>' + fixed_name + ext + '<STR_LIT>' + moving_seg_name + ext + '<STR_LIT>' + fixed_seg_name + ext + '<STR_LIT>' + param_multimodal_reg + '<STR_LIT>' + moving_name_reg <EOL> ext_multimodal = '<STR_LIT>' <EOL> '''<STR_LIT>''' <EOL> sct . run ( cmd ) <EOL> if init : <EOL> warp0_mat = moving_name_reg + '<STR_LIT>' <EOL> warp0 , warp0_inv = transform_mat_to_warp ( warp0_mat , moving_name_reg + ext , fixed_name + ext ) <EOL> if param . transformation == '<STR_LIT>' : <EOL> warp_tot = warp0 <EOL> inverse_warp_tot = warp0_inv <EOL> else : <EOL> warp1 = moving_name_reg + '<STR_LIT>' <EOL> warp1_inv = moving_name_reg + '<STR_LIT>' <EOL> warp_tot = moving_name_reg + '<STR_LIT>' + ext <EOL> inverse_warp_tot = moving_name_reg + '<STR_LIT>' + ext <EOL> sct . run ( '<STR_LIT>' + warp0 + '<STR_LIT:U+002C>' + warp1 + '<STR_LIT>' + fixed_name + ext + '<STR_LIT>' + warp_tot ) <EOL> sct . run ( '<STR_LIT>' + warp1_inv + '<STR_LIT:U+002C>' + warp0_inv + '<STR_LIT>' + moving_name + ext + '<STR_LIT>' + inverse_warp_tot ) <EOL> else : <EOL> warp_tot = moving_name_reg + '<STR_LIT>' <EOL> inverse_warp_tot = moving_name_reg + '<STR_LIT>' <EOL> if param . metric == '<STR_LIT>' : <EOL> fixed_im = Image ( fixed_name + ext ) <EOL> fixed_im . data = ( fixed_im . data - new_min ) * ( old_max - old_min ) / ( new_max - new_min ) + old_min <EOL> fixed_im . save ( ) <EOL> moving_im = Image ( moving_name_reg + ext_multimodal ) <EOL> moving_im . data = ( moving_im . data - new_min ) * ( old_max - old_min ) / ( new_max - new_min ) + old_min <EOL> moving_im . save ( ) <EOL> moving_name_unpad = moving_name_reg + "<STR_LIT>" <EOL> fixed_name_unpad = fixed_name + "<STR_LIT>" <EOL> sct . run ( "<STR_LIT>" + moving_name_reg + ext + "<STR_LIT>" + str ( int ( param . padding ) ) + "<STR_LIT>" + param . padding + "<STR_LIT>" + moving_name_unpad + ext ) <EOL> sct . run ( "<STR_LIT>" + fixed_name + ext + "<STR_LIT>" + str ( int ( param . padding ) ) + "<STR_LIT>" + param . padding + "<STR_LIT>" + fixed_name_unpad + ext ) <EOL> '''<STR_LIT>''' <EOL> path_output , file_output , ext_output = sct . extract_fname ( param . fname_output ) <EOL> warp_output = file_output + "<STR_LIT>" + ext_output <EOL> inverse_warp_output = file_output + "<STR_LIT>" + ext_output <EOL> sct . run ( "<STR_LIT>" + moving_name + "<STR_LIT:2>" + fixed_name + "<STR_LIT>" + warp_output ) <EOL> sct . run ( "<STR_LIT>" + fixed_name + "<STR_LIT:2>" + moving_name + "<STR_LIT>" + inverse_warp_output ) <EOL> '''<STR_LIT>''' <EOL> moving_name_out = file_output + ext_output <EOL> sct . run ( '<STR_LIT>' + fixed_name_unpad + ext + '<STR_LIT:U+002C>' + moving_name_unpad + ext + '<STR_LIT>' + moving_name_out + '<STR_LIT>' ) <EOL> return warp_output , inverse_warp_output <EOL> def transform_mat_to_warp ( warp_mat_name , src , dest , dim = <NUM_LIT:3> ) : <EOL> from msct_register_regularized import generate_warping_field <EOL> path_warp , name_warp , ext_warp = sct . extract_fname ( warp_mat_name ) <EOL> new_ext = '<STR_LIT>' <EOL> name_output_warp = name_warp + new_ext <EOL> name_output_warp_inverse = name_warp + '<STR_LIT>' + new_ext <EOL> name_warp_null = '<STR_LIT>' <EOL> name_warp_null_inv = '<STR_LIT>' <EOL> nx , ny , nz , nt , px , py , pz , pt = Image ( src ) . dim <EOL> nx_d , ny_d , nz_d , nt_d , px_d , py_d , pz_d , pt_d = Image ( dest ) . dim <EOL> x_trans = [ <NUM_LIT:0> for i in range ( nz ) ] <EOL> x_trans_d = [ <NUM_LIT:0> for i in range ( nz_d ) ] <EOL> y_trans = [ <NUM_LIT:0> for i in range ( nz ) ] <EOL> y_trans_d = [ <NUM_LIT:0> for i in range ( nz_d ) ] <EOL> generate_warping_field ( src , x_trans = x_trans , y_trans = y_trans , fname = name_warp_null , verbose = <NUM_LIT:0> ) <EOL> generate_warping_field ( dest , x_trans = x_trans_d , y_trans = y_trans_d , fname = name_warp_null_inv , verbose = <NUM_LIT:0> ) <EOL> sct . run ( '<STR_LIT>' + str ( dim ) + '<STR_LIT:U+0020>' + name_output_warp + '<STR_LIT>' + src + '<STR_LIT:U+0020>' + name_warp_null + '<STR_LIT:U+0020>' + warp_mat_name ) <EOL> sct . run ( '<STR_LIT>' + str ( dim ) + '<STR_LIT:U+0020>' + name_output_warp_inverse + '<STR_LIT>' + dest + '<STR_LIT:U+0020>' + name_warp_null_inv + '<STR_LIT>' + warp_mat_name ) <EOL> return name_output_warp , name_output_warp_inverse <EOL> def main ( ) : <EOL> sct . printv ( '<STR_LIT>' , verbose , '<STR_LIT>' ) <EOL> path_tmp = '<STR_LIT>' + time . strftime ( "<STR_LIT>" ) + '<STR_LIT:/>' <EOL> sct . run ( '<STR_LIT>' + path_tmp ) <EOL> target = '<STR_LIT>' <EOL> target_seg = '<STR_LIT>' <EOL> anat = '<STR_LIT>' <EOL> anat_seg = '<STR_LIT>' <EOL> warp_template2anat = '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + multimodal_reg_param . anat + '<STR_LIT:U+0020>' + path_tmp + anat ) <EOL> sct . run ( '<STR_LIT>' + multimodal_reg_param . target + '<STR_LIT:U+0020>' + path_tmp + target ) <EOL> sct . run ( '<STR_LIT>' + multimodal_reg_param . anat_seg + '<STR_LIT:U+0020>' + path_tmp + anat_seg ) <EOL> sct . run ( '<STR_LIT>' + multimodal_reg_param . target_seg + '<STR_LIT:U+0020>' + path_tmp + target_seg ) <EOL> sct . run ( '<STR_LIT>' + multimodal_reg_param . warp_template2anat + '<STR_LIT:U+0020>' + path_tmp + warp_template2anat ) <EOL> os . chdir ( path_tmp ) <EOL> warp_anat2target , warp_target2anat , label_original = reg_multimodal_warp ( anat , target , anat_seg , target_seg , warp_template2anat , multimodal_reg_param ) <EOL> gm_seg_param . res_type = '<STR_LIT>' <EOL> wm_fname , gm_fname = segment_gm ( target_fname = target , sc_seg_fname = target_seg , path_to_label = label_original , param = gm_seg_param ) <EOL> wm_seg_out = sct . extract_fname ( multimodal_reg_param . target ) [ <NUM_LIT:1> ] + '<STR_LIT>' <EOL> gm_seg_out = sct . extract_fname ( multimodal_reg_param . target ) [ <NUM_LIT:1> ] + '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + wm_fname + '<STR_LIT>' + wm_seg_out ) <EOL> sct . run ( '<STR_LIT>' + gm_fname + '<STR_LIT>' + gm_seg_out ) <EOL> wm_reg_param . fname_fixed = wm_fname <EOL> wm_reg_param . fname_moving = label_original + '<STR_LIT>' <EOL> wm_reg_param . fname_seg_fixed = target_seg <EOL> wm_reg_param . fname_seg_moving = label_original + '<STR_LIT>' <EOL> if gm_seg_param . res_type == '<STR_LIT>' : <EOL> template_wm = Image ( wm_reg_param . fname_moving ) <EOL> template_wm . data = ( template_wm . data > <NUM_LIT:0.5> ) . astype ( int ) <EOL> template_wm . save ( ) <EOL> warp , inverse_warp = wm_registration ( wm_reg_param ) <EOL> warp_anat2target_corrected = '<STR_LIT>' + sct . extract_fname ( multimodal_reg_param . anat ) [ <NUM_LIT:1> ] + '<STR_LIT:2>' + sct . extract_fname ( multimodal_reg_param . target ) [ <NUM_LIT:1> ] + '<STR_LIT>' <EOL> warp_target2anat_corrected = '<STR_LIT>' + sct . extract_fname ( multimodal_reg_param . target ) [ <NUM_LIT:1> ] + '<STR_LIT:2>' + sct . extract_fname ( multimodal_reg_param . anat ) [ <NUM_LIT:1> ] + '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + warp_anat2target + '<STR_LIT:U+002C>' + warp + '<STR_LIT>' + target + '<STR_LIT>' + warp_anat2target_corrected ) <EOL> sct . run ( '<STR_LIT>' + inverse_warp + '<STR_LIT:U+002C>' + warp_target2anat + '<STR_LIT>' + anat + '<STR_LIT>' + warp_target2anat_corrected ) <EOL> target_reg = sct . extract_fname ( multimodal_reg_param . target ) [ <NUM_LIT:1> ] + '<STR_LIT>' <EOL> anat_reg = sct . extract_fname ( multimodal_reg_param . anat ) [ <NUM_LIT:1> ] + '<STR_LIT>' <EOL> sct . run ( '<STR_LIT>' + target + '<STR_LIT>' + anat + '<STR_LIT>' + warp_target2anat_corrected + '<STR_LIT>' + target_reg ) <EOL> sct . run ( '<STR_LIT>' + anat + '<STR_LIT>' + target + '<STR_LIT>' + warp_anat2target_corrected + '<STR_LIT>' + anat_reg ) <EOL> sct . run ( '<STR_LIT>' + target_reg + '<STR_LIT>' + target_reg ) <EOL> sct . run ( '<STR_LIT>' + anat_reg + '<STR_LIT>' + anat_reg ) <EOL> sct . run ( '<STR_LIT>' + warp_anat2target_corrected + '<STR_LIT>' + warp_anat2target_corrected ) <EOL> sct . run ( '<STR_LIT>' + warp_target2anat_corrected + '<STR_LIT>' + warp_target2anat_corrected ) <EOL> sct . printv ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' + multimodal_reg_param . warp_template2anat + '<STR_LIT:U+002C>' + warp_anat2target_corrected + '<STR_LIT>' + multimodal_reg_param . target + '<STR_LIT>' + sct . extract_fname ( multimodal_reg_param . target ) [ <NUM_LIT:1> ] + '<STR_LIT>' <EOL> '<STR_LIT>' + multimodal_reg_param . target + '<STR_LIT>' + sct . extract_fname ( multimodal_reg_param . target ) [ <NUM_LIT:1> ] + '<STR_LIT>' , verbose , '<STR_LIT:info>' ) <EOL> os . chdir ( '<STR_LIT:..>' ) <EOL> if remove : <EOL> sct . printv ( '<STR_LIT>' , verbose , '<STR_LIT>' ) <EOL> sct . run ( "<STR_LIT>" + path_tmp ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> multimodal_reg_param = MultimodalRegistrationParam ( ) <EOL> wm_reg_param = WmRegistrationParam ( ) <EOL> gm_seg_param = SegmentationParam ( ) <EOL> remove = True <EOL> verbose = <NUM_LIT:1> <EOL> if wm_reg_param . debug : <EOL> print '<STR_LIT>' <EOL> else : <EOL> param_default = WmRegistrationParam ( ) <EOL> parser = Parser ( __file__ ) <EOL> parser . usage . set_description ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . usage . addSection ( '<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:file>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = True , <EOL> example = '<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:file>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = True , <EOL> example = '<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 . usage . addSection ( '<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>' , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> default_value = '<STR_LIT>' , <EOL> example = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> parser . usage . addSection ( "<STR_LIT>" ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = '<STR_LIT>' , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> default_value = <NUM_LIT:1> , <EOL> example = [ '<STR_LIT:0>' , '<STR_LIT:1>' ] ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = '<STR_LIT>' , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> default_value = <NUM_LIT:1> , <EOL> example = [ '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:2>' ] ) <EOL> arguments = parser . parse ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> multimodal_reg_param . target = arguments [ "<STR_LIT>" ] <EOL> multimodal_reg_param . target_seg = arguments [ "<STR_LIT>" ] <EOL> multimodal_reg_param . anat = arguments [ "<STR_LIT>" ] <EOL> multimodal_reg_param . anat_seg = arguments [ "<STR_LIT>" ] <EOL> multimodal_reg_param . warp_template2anat = arguments [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in arguments : <EOL> wm_reg_param . transformation = arguments [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in arguments : <EOL> wm_reg_param . metric = arguments [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in arguments : <EOL> remove = bool ( int ( arguments [ "<STR_LIT>" ] ) ) <EOL> if "<STR_LIT>" in arguments : <EOL> verbose = arguments [ "<STR_LIT>" ] <EOL> main ( ) </s>
