#NEWLINE# def test( path_data , parameters = '' ) : #NEWLINE# #INDENT# if ( not parameters ) : #NEWLINE# #INDENT# parameters = '-i mt/mt0.nii.gz -s mt/mt0_seg.nii.gz -vertfile mt/label/template/MNI-Poly-AMU_level.nii.gz -normalize 1 -ref mt/mt0_manual_gmseg.nii.gz -qc 0' #NEWLINE# #UNINDENT# parser = sct_segment_graymatter . get_parser ( ) #NEWLINE# dict_param = parser . parse ( parameters . split ( ) , check_file_exist = False ) #NEWLINE# dict_param_with_path = parser . add_path_to_file ( dict_param , path_data , input_file = True ) #NEWLINE# if ( '-model' in dict_param_with_path . keys ( ) ) : #NEWLINE# #INDENT# dict_param_with_path [ '-model' ] = dict_param_with_path [ '-model' ] [ len ( path_data ) : ] #NEWLINE# #UNINDENT# param_with_path = parser . dictionary_to_string ( dict_param_with_path ) #NEWLINE# if ( not ( os . path . isfile ( dict_param_with_path [ '-i' ] ) and os . path . isfile ( dict_param_with_path [ '-s' ] ) and os . path . isfile ( dict_param_with_path [ '-vertfile' ] ) and os . path . isfile ( dict_param_with_path [ '-ref' ] ) ) ) : #NEWLINE# #INDENT# status = 200 #NEWLINE# output = ( 'ERROR: the file(s) provided to test function do not exist in folder: ' + path_data ) #NEWLINE# return ( status , output , DataFrame ( data = { #NEWLINE# #INDENT# 'status' : status , #NEWLINE# 'output' : output , #NEWLINE# 'dice_gm' : float ( 'nan' ) , #NEWLINE# 'dice_wm' : float ( 'nan' ) , #NEWLINE# 'hausdorff' : float ( 'nan' ) , #NEWLINE# 'med_dist' : float ( 'nan' ) , #NEWLINE# 'duration_[s]' : float ( 'nan' ) , #NEWLINE# #UNINDENT# } , index = [ path_data ] ) ) #NEWLINE# #UNINDENT# import time , random #NEWLINE# subject_folder = path_data . split ( '/' ) #NEWLINE# if ( ( subject_folder [ ( - 1 ) ] == '' ) and ( len ( subject_folder ) > 1 ) ) : #NEWLINE# #INDENT# subject_folder = subject_folder [ ( - 2 ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# subject_folder = subject_folder [ ( - 1 ) ] #NEWLINE# #UNINDENT# path_output = sct . slash_at_the_end ( ( ( ( ( ( 'sct_segment_graymatter_' + subject_folder ) + '_' ) + time . strftime ( '%y%m%d%H%M%S' ) ) + '_' ) + str ( random . randint ( 1 , 1000000 ) ) ) , slash = 1 ) #NEWLINE# param_with_path += ( ' -ofolder ' + path_output ) #NEWLINE# cmd = ( 'sct_segment_graymatter ' + param_with_path ) #NEWLINE# time_start = time . time ( ) #NEWLINE# ( status , output ) = sct . run ( cmd , 0 ) #NEWLINE# duration = ( time . time ( ) - time_start ) #NEWLINE# ( result_dice_gm , result_dice_wm , result_hausdorff , result_median_dist ) = ( float ( 'nan' ) , float ( 'nan' ) , float ( 'nan' ) , float ( 'nan' ) ) #NEWLINE# if ( status == 0 ) : #NEWLINE# #INDENT# target_name = sct . extract_fname ( dict_param_with_path [ '-i' ] ) [ 1 ] #NEWLINE# dice_fname = ( ( ( ( ( path_output + 'dice_' ) + target_name ) + '_' ) + dict_param_with_path [ '-res-type' ] ) + '.txt' ) #NEWLINE# hausdorff_fname = ( ( ( ( ( path_output + 'hd_' ) + target_name ) + '_' ) + dict_param_with_path [ '-res-type' ] ) + '.txt' ) #NEWLINE# dice = open ( dice_fname , 'r' ) #NEWLINE# dice_lines = dice . readlines ( ) #NEWLINE# dice . close ( ) #NEWLINE# gm_start = dice_lines . index ( 'Dice coefficient on the Gray Matter segmentation:\n' ) #NEWLINE# wm_start = dice_lines . index ( 'Dice coefficient on the White Matter segmentation:\n' ) #NEWLINE# gm_dice_lines = dice_lines [ gm_start : ( wm_start - 1 ) ] #NEWLINE# gm_dice_lines = gm_dice_lines [ ( gm_dice_lines . index ( '2D Dice coefficient by slice:\n' ) + 1 ) : ( - 1 ) ] #NEWLINE# null_slices = [ ] #NEWLINE# gm_dice = [ ] #NEWLINE# for line in gm_dice_lines : #NEWLINE# #INDENT# ( n_slice , dc ) = line . split ( ' ' ) #NEWLINE# dc = dc [ : ( - 1 ) ] #NEWLINE# dc = ( dc [ : ( - 4 ) ] if ( '[0m' in dc ) else dc ) #NEWLINE# if ( ( dc == '0' ) or ( dc == 'nan' ) ) : #NEWLINE# #INDENT# null_slices . append ( n_slice ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# gm_dice . append ( float ( dc ) ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# gm_dice . append ( float ( dc [ : ( - 4 ) ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# result_dice_gm = mean ( gm_start ) #NEWLINE# wm_dice_lines = dice_lines [ wm_start : ] #NEWLINE# wm_dice_lines = wm_dice_lines [ ( wm_dice_lines . index ( '2D Dice coefficient by slice:\n' ) + 1 ) : ] #NEWLINE# wm_dice = [ ] #NEWLINE# for line in wm_dice_lines : #NEWLINE# #INDENT# ( n_slice , dc ) = line . split ( ' ' ) #NEWLINE# if ( line is not wm_dice_lines [ ( - 1 ) ] ) : #NEWLINE# #INDENT# dc = dc [ : ( - 1 ) ] #NEWLINE# #UNINDENT# if ( n_slice not in null_slices ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# wm_dice . append ( float ( dc ) ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# wm_dice . append ( float ( dc [ : ( - 4 ) ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# result_dice_wm = mean ( wm_dice ) #NEWLINE# hd = open ( hausdorff_fname , 'r' ) #NEWLINE# hd_lines = hd . readlines ( ) #NEWLINE# hd . close ( ) #NEWLINE# hd_lines = hd_lines [ 1 : ( - 4 ) ] #NEWLINE# hausdorff = [ ] #NEWLINE# max_med = [ ] #NEWLINE# for line in hd_lines : #NEWLINE# #INDENT# ( slice_id , res ) = line . split ( ':' ) #NEWLINE# ( slice , n_slice ) = slice_id . split ( ' ' ) #NEWLINE# if ( n_slice not in null_slices ) : #NEWLINE# #INDENT# ( hd , med1 , med2 ) = res [ : ( - 1 ) ] . split ( ' - ' ) #NEWLINE# ( hd , med1 , med2 ) = ( float ( hd ) , float ( med1 ) , float ( med2 ) ) #NEWLINE# hausdorff . append ( hd ) #NEWLINE# max_med . append ( max ( med1 , med2 ) ) #NEWLINE# #UNINDENT# #UNINDENT# result_hausdorff = mean ( hausdorff ) #NEWLINE# result_median_dist = mean ( max_med ) #NEWLINE# hd_threshold = 1.5 #NEWLINE# wm_dice_threshold = 0.8 #NEWLINE# if ( ( result_hausdorff > hd_threshold ) or ( result_dice_wm < wm_dice_threshold ) ) : #NEWLINE# #INDENT# status = 99 #NEWLINE# output += ( ( ( ( '\nResulting segmentation is too different from manual segmentation:\nWM dice: ' + str ( result_dice_wm ) ) + '\nHausdorff distance: ' ) + str ( result_hausdorff ) ) + '\n' ) #NEWLINE# #UNINDENT# #UNINDENT# results = DataFrame ( data = { #NEWLINE# #INDENT# 'status' : status , #NEWLINE# 'output' : output , #NEWLINE# 'dice_gm' : result_dice_gm , #NEWLINE# 'dice_wm' : result_dice_wm , #NEWLINE# 'hausdorff' : result_hausdorff , #NEWLINE# 'med_dist' : result_median_dist , #NEWLINE# 'duration_[s]' : duration , #NEWLINE# #UNINDENT# } , index = [ path_data ] ) #NEWLINE# return ( status , output , results )