def test_integrity(param_test): """ Test integrity of function """ # apply transformation to binary mask: template --> anat sct_apply_transfo.main(args=[ '-i', param_test.fname_gt, '-d', param_test.dict_args_with_path['-s'], '-w', 'warp_template2anat.nii.gz', '-o', 'test_template2anat.nii.gz', '-x', 'nn', '-v', '0' ]) # apply transformation to binary mask: anat --> template sct_apply_transfo.main(args=[ '-i', param_test.dict_args_with_path['-s'], '-d', param_test.fname_gt, '-w', 'warp_anat2template.nii.gz', '-o', 'test_anat2template.nii.gz', '-x', 'nn', '-v', '0' ]) # compute dice coefficient between template segmentation warped to anat and segmentation from anat im_seg = Image(param_test.dict_args_with_path['-s']) im_template_seg_reg = Image('test_template2anat.nii.gz') dice_template2anat = compute_dice(im_seg, im_template_seg_reg, mode='3d', zboundaries=True) # check param_test.output += 'Dice[seg,template_seg_reg]: ' + str( dice_template2anat) if dice_template2anat > param_test.dice_threshold: param_test.output += '\n--> PASSED' else: param_test.status = 99 param_test.output += '\n--> FAILED' # compute dice coefficient between anat segmentation warped to template and segmentation from template im_seg_reg = Image('test_anat2template.nii.gz') im_template_seg = Image(param_test.fname_gt) dice_anat2template = compute_dice(im_seg_reg, im_template_seg, mode='3d', zboundaries=True) # check param_test.output += '\n\nDice[seg_reg,template_seg]: ' + str( dice_anat2template) if dice_anat2template > param_test.dice_threshold: param_test.output += '\n--> PASSED' else: param_test.status = 99 param_test.output += '\n--> FAILED' # update Panda structure param_test.results['dice_template2anat'] = dice_template2anat param_test.results['dice_anat2template'] = dice_anat2template return param_test
def test_integrity(param_test): """ Test integrity of function """ dice_segmentation = float('nan') # extract name of output segmentation: data_seg.nii.gz file_seg = os.path.join(param_test.path_output, sct.add_suffix(param_test.file_input, '_seg')) # open output segmentation im_seg = Image(file_seg) # open ground truth im_seg_manual = Image(param_test.fname_gt) # compute dice coefficient between generated image and image from database dice_segmentation = compute_dice(im_seg, im_seg_manual, mode='3d', zboundaries=False) # display param_test.output += 'Computed dice: ' + str(dice_segmentation) param_test.output += '\nDice threshold (if computed Dice smaller: fail): ' + str( param_test.dice_threshold) if dice_segmentation < param_test.dice_threshold: param_test.status = 99 param_test.output += '\n--> FAILED' else: param_test.output += '\n--> PASSED' # update Panda structure param_test.results['dice'] = dice_segmentation return param_test
def test_integrity(param_test): """ Test integrity of function """ path_data = os.path.join(param_test.path_data, param_test.contrast, param_test.file_input) integrity_value = 1.0 # open output segmentation try: im_seg_manual = Image(path_data) except: param_test.output += 'ERROR: Cannot open ground truth segmentation: ' + path_data param_test.status = 99 return param_test # compute dice coefficient between generated image and image from database dice_segmentation = compute_dice(im_seg_manual, im_seg_manual, mode='3d', zboundaries=False) param_test.output += 'Computed dice: ' + str(dice_segmentation) if dice_segmentation != integrity_value: param_test.output += '\nERROR: Dice coefficient should be : ' + str( integrity_value) param_test.status = 99 return param_test
def test(path_data='', parameters=''): # initialization verbose = 0 output = '' # check if isct_propseg compatibility status_isct_propseg, output_isct_propseg = commands.getstatusoutput('isct_propseg') isct_propseg_version = output_isct_propseg.split('\n')[0] if isct_propseg_version != 'sct_propseg - Version 1.1 (2015-03-24)': status = 99 output += '\nERROR: isct_propseg does not seem to be compatible with your system or is no up-to-date... Please contact SCT administrators.' return status, output, DataFrame(data={'status': status, 'output': output}, index=[path_data]) # parameters if not parameters: parameters = '-i t2/t2.nii.gz -c t2' dice_threshold = 0.9 parser = sct_propseg.get_parser() dict_param = parser.parse(parameters.split(), check_file_exist=False) dict_param_with_path = parser.add_path_to_file(deepcopy(dict_param), path_data, input_file=True) param_with_path = parser.dictionary_to_string(dict_param_with_path) # Extract contrast contrast = '' input_filename = '' if dict_param['-i'][0] == '/': dict_param['-i'] = dict_param['-i'][1:] input_split = dict_param['-i'].split('/') if len(input_split) == 2: contrast = input_split[0] input_filename = input_split[1] else: input_filename = input_split[0] if not contrast: # if no contrast folder, send error. status = 1 output += '\nERROR: when extracting the contrast folder from input file in command line: ' + dict_param['-i'] + ' for ' + path_data return status, output, DataFrame(data={'status': status, 'output': output, 'dice_segmentation': float('nan')}, index=[path_data]) import time, random subject_folder = path_data.split('/') if subject_folder[-1] == '' and len(subject_folder) > 1: subject_folder = subject_folder[-2] else: subject_folder = subject_folder[-1] path_output = sct.slash_at_the_end('sct_propseg_' + subject_folder + '_' + time.strftime("%y%m%d%H%M%S") + '_' + str(random.randint(1, 1000000)), slash=1) param_with_path += ' -ofolder ' + path_output sct.create_folder(path_output) # log file import sys fname_log = path_output + 'output.log' stdout_log = file(fname_log, 'w') # redirect to log file stdout_orig = sys.stdout sys.stdout = stdout_log # Check if input files exist if not (os.path.isfile(dict_param_with_path['-i'])): status = 200 output += '\nERROR: the file(s) provided to test function do not exist in folder: ' + path_data write_to_log_file(fname_log, output, 'w') return status, output, DataFrame( data={'status': status, 'output': output, 'dice_segmentation': float('nan')}, index=[path_data]) # Check if ground truth files exist if not os.path.isfile(path_data + contrast + '/' + contrast + '_seg_manual.nii.gz'): status = 201 output += '\nERROR: the file *_labeled_center_manual.nii.gz does not exist in folder: ' + path_data write_to_log_file(fname_log, output, 'w') return status, output, DataFrame(data={'status': int(status), 'output': output}, index=[path_data]) # run command cmd = 'sct_propseg ' + param_with_path output += '\n====================================================================================================\n'\ + cmd + \ '\n====================================================================================================\n\n' # copy command time_start = time.time() try: status, o = sct.run(cmd, 0) except: status, o = 1, 'ERROR: Function crashed!' output += o duration = time.time() - time_start # extract name of manual segmentation # by convention, manual segmentation are called inputname_seg_manual.nii.gz where inputname is the filename # of the input image segmentation_filename = path_output + sct.add_suffix(input_filename, '_seg') manual_segmentation_filename = path_data + contrast + '/' + sct.add_suffix(input_filename, '_seg_manual') dice_segmentation = float('nan') # if command ran without error, test integrity if status == 0: # compute dice coefficient between generated image and image from database dice_segmentation = compute_dice(Image(segmentation_filename), Image(manual_segmentation_filename), mode='3d', zboundaries=False) if dice_segmentation < dice_threshold: status = 99 # transform results into Pandas structure results = DataFrame(data={'status': status, 'output': output, 'dice_segmentation': dice_segmentation, 'duration [s]': duration}, index=[path_data]) sys.stdout.close() sys.stdout = stdout_orig # write log file write_to_log_file(fname_log, output, mode='r+', prepend=True) return status, output, results
def test(path_data='', parameters=''): verbose = 0 # check if isct_propseg compatibility status_isct_propseg, output_isct_propseg = commands.getstatusoutput('isct_propseg') isct_propseg_version = output_isct_propseg.split('\n')[0] if isct_propseg_version != 'sct_propseg - Version 1.1 (2015-03-24)': status = 99 output = 'ERROR: isct_propseg does not seem to be compatible with your system or is no up-to-date... Please contact SCT administrators.' return status, output, DataFrame(data={'status': status, 'output': output}, index=[path_data]) # parameters if not parameters: parameters = '-i t2/t2.nii.gz -c t2' dice_threshold = 0.9 parser = sct_propseg.get_parser() dict_param = parser.parse(parameters.split(), check_file_exist=False) dict_param_with_path = parser.add_path_to_file(deepcopy(dict_param), path_data, input_file=True) param_with_path = parser.dictionary_to_string(dict_param_with_path) # Extract contrast contrast = '' input_filename = '' if dict_param['-i'][0] == '/': dict_param['-i'] = dict_param['-i'][1:] input_split = dict_param['-i'].split('/') if len(input_split) == 2: contrast = input_split[0] input_filename = input_split[1] else: input_filename = input_split[0] if not contrast: # if no contrast folder, send error. status = 1 output = 'ERROR: when extracting the contrast folder from input file in command line: ' + dict_param['-i'] + ' for ' + path_data return status, output, DataFrame(data={'status': status, 'output': output, 'dice_segmentation': float('nan')}, index=[path_data]) # Check if input files exist if not (os.path.isfile(dict_param_with_path['-i'])): status = 200 output = 'ERROR: the file(s) provided to test function do not exist in folder: ' + path_data return status, output, DataFrame( data={'status': status, 'output': output, 'dice_segmentation': float('nan')}, index=[path_data]) # Check if ground truth files exist if not os.path.isfile(path_data + contrast + '/' + contrast + '_seg_manual.nii.gz'): status = 200 output = 'ERROR: the file *_labeled_center_manual.nii.gz does not exist in folder: ' + path_data return status, output, DataFrame(data={'status': int(status), 'output': output}, index=[path_data]) import time, random subject_folder = path_data.split('/') if subject_folder[-1] == '' and len(subject_folder) > 1: subject_folder = subject_folder[-2] else: subject_folder = subject_folder[-1] path_output = sct.slash_at_the_end('sct_propseg_' + subject_folder + '_' + time.strftime("%y%m%d%H%M%S") + '_' + str(random.randint(1, 1000000)), slash=1) param_with_path += ' -ofolder ' + path_output # run command cmd = 'sct_propseg ' + param_with_path output = '\n====================================================================================================\n'\ + cmd + \ '\n====================================================================================================\n\n' # copy command time_start = time.time() status, o = sct.run(cmd, verbose) output += o duration = time.time() - time_start # extract name of manual segmentation # by convention, manual segmentation are called inputname_seg_manual.nii.gz where inputname is the filename # of the input image segmentation_filename = path_output + sct.add_suffix(input_filename, '_seg') manual_segmentation_filename = path_data + contrast + '/' + sct.add_suffix(input_filename, '_seg_manual') dice_segmentation = float('nan') # if command ran without error, test integrity if status == 0: # compute dice coefficient between generated image and image from database dice_segmentation = compute_dice(Image(segmentation_filename), Image(manual_segmentation_filename), mode='3d', zboundaries=False) if dice_segmentation < dice_threshold: status = 99 # transform results into Pandas structure results = DataFrame(data={'status': status, 'output': output, 'dice_segmentation': dice_segmentation, 'duration [s]': duration}, index=[path_data]) return status, output, results