def test_integrity(param_test):
    """
    Test integrity of function
    """
    # fetch index of the test being performed
    index_args = param_test.default_args.index(param_test.args)

    # apply transformation to binary mask: template --> anat
    sct_apply_transfo.main(args=[
        '-i', param_test.fname_gt[index_args], '-d', param_test.file_seg, '-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.file_seg, '-d', param_test.fname_gt[index_args], '-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.file_seg)
    im_template_seg_reg = Image('test_template2anat.nii.gz')
    dice_template2anat = msct_image.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[index_args])
    dice_anat2template = msct_image.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
예제 #2
0
def test_integrity(param_test):
    """
    Test integrity of function
    """
    # open output segmentation
    im_seg = Image(param_test.fname_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
    """
    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
예제 #4
0
def test_integrity(param_test):
    """
    Test integrity of function
    """

    # open output segmentation
    try:
        im_seg_manual = Image(param_test.fname_data)
    except:
        param_test.output += 'ERROR: Cannot open ground truth segmentation: ' + param_test.fname_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 != param_test.dice_value:
        param_test.output += '\nERROR: Dice coefficient should be : ' + str(
            param_test.dice_value)
        param_test.status = 99

    return param_test
예제 #5
0
def test_sct_deepseg_gm_check_dice_coefficient_against_groundtruth():
    """Run the CLI script and verify that dice (computed against ground truth) is within a certain threshold."""
    fname_out = 'output.nii.gz'
    fname_gt = 't2s/t2s_uncropped_gmseg_manual.nii.gz'
    sct_deepseg_gm.main(argv=['-i', 't2s/t2s_uncropped.nii.gz', '-o', fname_out, '-qc', 'testing-qc'])
    dice_segmentation = compute_dice(Image(fname_out), Image(fname_gt), mode='3d', zboundaries=False)
    assert dice_segmentation >= 0.85
예제 #6
0
def test_segment_2d():
    from keras import backend as K
    K.set_image_data_format(
        "channels_last"
    )  # Set at channels_first in test_deepseg_lesion.test_segment()

    contrast_test = 't2'
    model_path = os.path.join(sct.__sct_dir__, 'data', 'deepseg_sc_models',
                              '{}_sc.h5'.format(contrast_test))

    fname_t2 = os.path.join(sct.__sct_dir__, 'sct_testing_data/t2/t2.nii.gz'
                            )  # install: sct_download_data -d sct_testing_data
    fname_t2_seg = os.path.join(
        sct.__sct_dir__, 'sct_testing_data/t2/t2_seg.nii.gz'
    )  # install: sct_download_data -d sct_testing_data

    img, gt = _preprocess_segment(fname_t2, fname_t2_seg, contrast_test)

    seg = deepseg_sc.segment_2d(model_fname=model_path,
                                contrast_type=contrast_test,
                                input_size=(64, 64),
                                im_in=img)
    assert seg.dtype == np.dtype('float32')

    seg_im = img.copy()
    seg_im.data = (seg > 0.5).astype(np.uint8)
    assert msct_image.compute_dice(seg_im, gt) > 0.80
예제 #7
0
def test_segment_3d():
    from keras import backend as K
    K.set_image_data_format(
        "channels_last"
    )  # Set at channels_first in test_deepseg_lesion.test_segment()

    contrast_test = 't2'
    model_path = os.path.join(sct.__sct_dir__, 'data', 'deepseg_sc_models',
                              '{}_sc_3D.h5'.format(contrast_test))

    fname_t2 = os.path.join(sct.__sct_dir__, 'sct_testing_data/t2/t2.nii.gz'
                            )  # install: sct_download_data -d sct_testing_data
    fname_t2_seg = os.path.join(
        sct.__sct_dir__, 'sct_testing_data/t2/t2_seg.nii.gz'
    )  # install: sct_download_data -d sct_testing_data

    img, gt = _preprocess_segment(fname_t2,
                                  fname_t2_seg,
                                  contrast_test,
                                  dim_3=True)

    seg_im = deepseg_sc.segment_3d(model_fname=model_path,
                                   contrast_type=contrast_test,
                                   im_in=img)

    assert msct_image.compute_dice(seg_im, gt) > 0.80
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
예제 #10
0
def test_sct_label_vertebrae_clean_labels(tmp_path):
    command = '-i sct_testing_data/t2/t2.nii.gz -s sct_testing_data/t2/t2_seg-manual.nii.gz -c t2 -initz 40,3 -clean-labels 1 -ofolder ' + str(os.path.join(str(tmp_path), 'clean'))
    sct_label_vertebrae.main(command.split())
    command = '-i sct_testing_data/t2/t2.nii.gz -s sct_testing_data/t2/t2_seg-manual.nii.gz -c t2 -initz 40,3 -ofolder ' + str(os.path.join(str(tmp_path), 'no_clean'))
    sct_label_vertebrae.main(command.split())
    image_clean = Image(os.path.join(str(tmp_path), 'clean', 't2_seg-manual_labeled.nii.gz'))
    image_no_clean = Image(os.path.join(str(tmp_path), 'no_clean', 't2_seg-manual_labeled.nii.gz'))
    image_seg = Image(os.path.join('sct_testing_data', 't2', 't2_seg-manual.nii.gz'))
    # binarization (because label are between 3 and 6)
    image_clean.data = image_clean.data > 0.5
    image_no_clean.data = image_no_clean.data > 0.5
    dice_clean = compute_dice(image_clean, image_seg)
    dice_no_clean = compute_dice(image_no_clean, image_seg)
    # The cleaned version should be closer to the segmentation
    assert dice_clean >= dice_no_clean

    # Files created in root directory by sct_label_vertebrae
    os.unlink('straightening.cache')
예제 #11
0
def test_sct_dice_coefficient_check_output_against_groundtruth():
    """Run the CLI script and verify its output matches a known ground truth value."""
    # FIXME: The results of "sct_dice_coefficient" are not actually verified. Instead, the "compute_dice" function
    #        is called, and THOSE results are verified instead.
    # This was copied as-is from the existing 'sct_testing' test, but should be fixed at a later date.
    path_data = 't2/t2_seg-manual.nii.gz'
    sct_dice_coefficient.main(argv=['-i', path_data, '-d', path_data])
    im_seg_manual = Image(path_data)
    dice_segmentation = compute_dice(im_seg_manual, im_seg_manual, mode='3d', zboundaries=False)
    assert dice_segmentation == 1.0
def test_sct_propseg_check_dice_coefficient_against_groundtruth():
    """Run the CLI script and verify that dice (computed against ground truth) is within a certain threshold."""
    sct_propseg.main(argv=['-i', 't2/t2.nii.gz', '-c', 't2', '-qc', 'testing-qc'])

    # open output segmentation
    im_seg = Image('t2_seg.nii.gz')
    # open ground truth
    im_seg_manual = Image('t2/t2_seg-manual.nii.gz')
    # compute dice coefficient between generated image and image from database
    dice_segmentation = compute_dice(im_seg, im_seg_manual, mode='3d', zboundaries=False)

    # note: propseg does *not* produce the same results across platforms, hence the 0.9 Dice threshold.
    # For more details, see: https://github.com/neuropoly/spinalcordtoolbox/issues/2769
    assert dice_segmentation > 0.9
def test_sct_register_to_template_dice_coefficient_against_groundtruth(
        fname_gt, remaining_args):
    """Run the CLI script and verify transformed images have expected attributes."""
    fname_seg = 't2/t2_seg-manual.nii.gz'
    dice_threshold = 0.9
    sct_register_to_template.main(
        argv=['-i', 't2/t2.nii.gz', '-s', fname_seg] + remaining_args)

    # apply transformation to binary mask: template --> anat
    sct_apply_transfo.main(argv=[
        '-i', fname_gt, '-d', fname_seg, '-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(argv=[
        '-i', fname_seg, '-d', 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(fname_seg)
    im_template_seg_reg = Image('test_template2anat.nii.gz')
    dice_template2anat = compute_dice(im_seg,
                                      im_template_seg_reg,
                                      mode='3d',
                                      zboundaries=True)
    assert dice_template2anat > dice_threshold

    # 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(fname_gt)
    dice_anat2template = compute_dice(im_seg_reg,
                                      im_template_seg,
                                      mode='3d',
                                      zboundaries=True)
    assert dice_anat2template > dice_threshold
예제 #14
0
def test_segment_3d():
    from keras import backend as K
    K.set_image_data_format("channels_last")  # Set at channels_first in test_deepseg_lesion.test_segment()

    contrast_test = 't2'
    model_path = os.path.join(sct.__sct_dir__, 'data', 'deepseg_sc_models', '{}_sc_3D.h5'.format(contrast_test))   

    fname_t2 = os.path.join(sct.__sct_dir__, 'sct_testing_data/t2/t2.nii.gz')  # install: sct_download_data -d sct_testing_data
    fname_t2_seg = os.path.join(sct.__sct_dir__, 'sct_testing_data/t2/t2_seg.nii.gz')  # install: sct_download_data -d sct_testing_data

    img, gt = _preprocess_segment(fname_t2, fname_t2_seg, contrast_test, dim_3=True)

    seg = deepseg_sc.segment_3d(model_fname=model_path, contrast_type=contrast_test, im_in=img)
    seg_im = img.copy()
    seg_im.data = seg

    assert msct_image.compute_dice(seg_im, gt) > 0.80
def test_integrity(param_test):
    """
    Test integrity of function
    """

    if param_test.args.startswith(default_args[1]):
        return param_test  # no integrity test

    # 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',
        os.path.join(param_test.path_output,
                     'warp_template2anat.nii.gz'), '-o',
        os.path.join(param_test.path_output,
                     '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',
        os.path.join(param_test.path_output,
                     'warp_anat2template.nii.gz'), '-o',
        os.path.join(param_test.path_output,
                     '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(
        os.path.join(param_test.path_output, 'test_template2anat.nii.gz'))
    dice_template2anat = msct_image.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(
        os.path.join(param_test.path_output, 'test_anat2template.nii.gz'))
    im_template_seg = Image(param_test.fname_gt)
    dice_anat2template = msct_image.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