Exemple #1
0
def test_get_centerline_nurbs(img_ctl, expected, params):
    """Test centerline fitting using nurbs"""
    img, img_sub = [img_ctl[0].copy(), img_ctl[1].copy()]
    img_out, arr_out, arr_deriv_out, fit_results = get_centerline(
        img_sub, ParamCenterline(algo_fitting='nurbs', minmax=False), verbose=VERBOSE)
    assert np.median(find_and_sort_coord(img) - find_and_sort_coord(img_out)) == expected['median']
    assert fit_results.laplacian_max < expected['laplacian']
def test_get_centerline_linear(img_ctl, expected):
    """Test centerline fitting using linear interpolation"""
    deg = 3
    img, img_sub = [img_ctl[0].copy(), img_ctl[1].copy()]
    img_out, arr_out, _ = get_centerline(img_sub, algo_fitting='linear', param=ParamCenterline(degree=deg),
                                         minmax=False, verbose=VERBOSE)
    assert np.linalg.norm(find_and_sort_coord(img) - find_and_sort_coord(img_out)) < expected
def test_get_centerline_optic():
    """Test extraction of metrics aggregation across slices: All slices by default"""
    fname_t2 = os.path.join(__sct_dir__, 'sct_testing_data/t2/t2.nii.gz'
                            )  # install: sct_download_data -d sct_testing_data
    img_t2 = Image(fname_t2)
    # Add non-numerical values at the top corner of the image for testing purpose
    img_t2.change_type('float32')
    img_t2.data[0, 0, 0] = np.nan
    img_t2.data[1, 0, 0] = np.inf
    img_out, arr_out, _, _ = get_centerline(img_t2,
                                            ParamCenterline(
                                                algo_fitting='optic',
                                                contrast='t2',
                                                minmax=False),
                                            verbose=VERBOSE)
    # Open ground truth segmentation and compare
    fname_t2_seg = os.path.join(__sct_dir__,
                                'sct_testing_data/t2/t2_seg.nii.gz')
    img_seg_out, arr_seg_out, _, _ = get_centerline(Image(fname_t2_seg),
                                                    ParamCenterline(
                                                        algo_fitting='bspline',
                                                        minmax=False),
                                                    verbose=VERBOSE)
    assert np.linalg.norm(
        find_and_sort_coord(img_seg_out) - find_and_sort_coord(img_out)) < 3.5
def test_get_centerline_nurbs(img_ctl, expected):
    """Test centerline fitting using nurbs"""
    img, img_sub = [img_ctl[0].copy(), img_ctl[1].copy()]
    # Here we need a try/except because nurbs crashes with too few points.
    try:
        img_out, arr_out, _ = get_centerline(img_sub, algo_fitting='nurbs', minmax=False, verbose=VERBOSE)
        assert np.linalg.norm(find_and_sort_coord(img) - find_and_sort_coord(img_out)) < expected
    except ArithmeticError as e:
        print(e)
Exemple #5
0
def test_get_centerline_polyfit(img_ctl, expected, params):
    """Test centerline fitting using polyfit"""
    img, img_sub = [img_ctl[0].copy(), img_ctl[1].copy()]
    img_out, arr_out, arr_deriv_out, fit_results = get_centerline(
        img_sub, ParamCenterline(algo_fitting='polyfit', minmax=False), verbose=VERBOSE)
    assert np.median(find_and_sort_coord(img) - find_and_sort_coord(img_out)) == expected['median']
    assert np.max(np.absolute(np.diff(arr_deriv_out))) < expected['laplacian']
    # check arr_out only if input orientation is RPI (because the output array is always in RPI)
    if img.orientation == 'RPI':
        assert np.linalg.norm(find_and_sort_coord(img) - arr_out) < expected['norm']
def test_get_centerline_nurbs(img_ctl, expected):
    """Test centerline fitting using nurbs"""
    img, img_sub = [img_ctl[0].copy(), img_ctl[1].copy()]
    # Here we need a try/except because nurbs crashes with too few points.
    try:
        img_out, arr_out, arr_deriv_out = get_centerline(img_sub, algo_fitting='nurbs', minmax=False, verbose=VERBOSE)
        assert np.median(find_and_sort_coord(img) - find_and_sort_coord(img_out)) == expected['median']
        assert np.max(np.absolute(np.diff(arr_deriv_out))) < expected['laplacian']
    except ArithmeticError as e:
        print(e)
def test_get_centerline_linear(img_ctl, expected, params):
    """Test centerline fitting using linear interpolation"""
    img, img_sub = [img_ctl[0].copy(), img_ctl[1].copy()]
    if params:
        img_out, arr_out, arr_deriv_out = get_centerline(img_sub, algo_fitting='polyfit', minmax=False,
                                                         degree=params['degree'], verbose=VERBOSE)
    else:
        img_out, arr_out, arr_deriv_out = get_centerline(img_sub, algo_fitting='polyfit', minmax=False, verbose=VERBOSE)
    assert np.median(find_and_sort_coord(img) - find_and_sort_coord(img_out)) == expected['median']
    assert np.max(np.absolute(np.diff(arr_deriv_out))) < expected['laplacian']
def test_get_centerline_polyfit(img_ctl, expected):
    """Test centerline fitting using polyfit"""
    deg = 3
    img, img_sub = [img_ctl[0].copy(), img_ctl[1].copy()]
    img_out, arr_out, _ = get_centerline(img_sub, algo_fitting='polyfit', param=ParamCenterline(degree=deg),
                                         minmax=False, verbose=VERBOSE)

    assert np.linalg.norm(find_and_sort_coord(img) - find_and_sort_coord(img_out)) < expected
    # check arr_out and arr_out_deriv only if input orientation is RPI (because the output array is always in RPI)
    if img.orientation == 'RPI':
        assert np.linalg.norm(find_and_sort_coord(img) - arr_out) < expected
def test_get_centerline_bspline(img_ctl, expected, params):
    """Test centerline fitting using bspline"""
    img, img_sub = [img_ctl[0].copy(), img_ctl[1].copy()]
    if params:
        img_out, arr_out, arr_deriv_out = get_centerline(img_sub, algo_fitting='bspline', minmax=False,
                                                         smooth=params['smooth'], verbose=VERBOSE)
    else:
        img_out, arr_out, arr_deriv_out = get_centerline(img_sub, algo_fitting='bspline', minmax=False, verbose=VERBOSE)

    assert np.median(find_and_sort_coord(img) - find_and_sort_coord(img_out)) == expected['median']
    assert np.max(np.absolute(np.diff(arr_deriv_out))) < expected['laplacian']
def test_get_centerline_polyfit(img_ctl, expected, params):
    """Test centerline fitting using polyfit"""
    img, img_sub = [img_ctl[0].copy(), img_ctl[1].copy()]
    if params:
        img_out, arr_out, arr_deriv_out = get_centerline(img_sub, algo_fitting='polyfit', minmax=False,
                                                         degree=params['degree'], verbose=VERBOSE)
    else:
        img_out, arr_out, arr_deriv_out = get_centerline(img_sub, algo_fitting='polyfit', minmax=False, verbose=VERBOSE)
    assert np.median(find_and_sort_coord(img) - find_and_sort_coord(img_out)) == expected['median']
    assert np.max(np.absolute(np.diff(arr_deriv_out))) < expected['laplacian']
    # check arr_out only if input orientation is RPI (because the output array is always in RPI)
    if img.orientation == 'RPI':
        assert np.linalg.norm(find_and_sort_coord(img) - arr_out) < expected['norm']
def test_get_centerline_optic():
    """Test extraction of metrics aggregation across slices: All slices by default"""
    fname_t2 = os.path.join(sct.__sct_dir__, 'sct_testing_data/t2/t2.nii.gz')  # install: sct_download_data -d sct_testing_data
    img_t2 = Image(fname_t2)
    # Add non-numerical values at the top corner of the image for testing purpose
    img_t2.change_type('float32')
    img_t2.data[0, 0, 0] = np.nan
    img_t2.data[1, 0, 0] = np.inf
    img_out, arr_out, _ = get_centerline(img_t2, algo_fitting='optic', contrast='t2', minmax=False, verbose=VERBOSE)
    # Open ground truth segmentation and compare
    fname_t2_seg = os.path.join(sct.__sct_dir__, 'sct_testing_data/t2/t2_seg.nii.gz')
    img_seg_out, arr_seg_out, _ = get_centerline(Image(fname_t2_seg), algo_fitting='bspline', minmax=False,
                                                 verbose=VERBOSE)
    assert np.linalg.norm(find_and_sort_coord(img_seg_out) - find_and_sort_coord(img_out)) < 3.5
def test_get_centerline_nurbs(img_ctl, expected):
    """Test centerline fitting using nurbs"""
    img, img_sub = [img_ctl[0].copy(), img_ctl[1].copy()]
    # Here we need a try/except because nurbs crashes with too few points.
    try:
        img_out, arr_out, arr_deriv_out = get_centerline(img_sub,
                                                         algo_fitting='nurbs',
                                                         minmax=False,
                                                         verbose=VERBOSE)
        assert np.median(
            find_and_sort_coord(img) -
            find_and_sort_coord(img_out)) == expected['median']
        assert np.max(np.absolute(
            np.diff(arr_deriv_out))) < expected['laplacian']
    except ArithmeticError as e:
        print(e)
def test_get_centerline_linear(img_ctl, expected, params):
    """Test centerline fitting using linear interpolation"""
    img, img_sub = [img_ctl[0].copy(), img_ctl[1].copy()]
    if params:
        img_out, arr_out, arr_deriv_out = get_centerline(
            img_sub,
            algo_fitting='polyfit',
            minmax=False,
            degree=params['degree'],
            verbose=VERBOSE)
    else:
        img_out, arr_out, arr_deriv_out = get_centerline(
            img_sub, algo_fitting='polyfit', minmax=False, verbose=VERBOSE)
    assert np.median(find_and_sort_coord(img) -
                     find_and_sort_coord(img_out)) == expected['median']
    assert np.max(np.absolute(np.diff(arr_deriv_out))) < expected['laplacian']
def test_get_centerline_bspline(img_ctl, expected, params):
    """Test centerline fitting using bspline"""
    img, img_sub = [img_ctl[0].copy(), img_ctl[1].copy()]
    if params:
        img_out, arr_out, arr_deriv_out = get_centerline(
            img_sub,
            algo_fitting='bspline',
            minmax=False,
            smooth=params['smooth'],
            verbose=VERBOSE)
    else:
        img_out, arr_out, arr_deriv_out = get_centerline(
            img_sub, algo_fitting='bspline', minmax=False, verbose=VERBOSE)

    assert np.median(find_and_sort_coord(img) -
                     find_and_sort_coord(img_out)) == expected['median']
    assert np.max(np.absolute(np.diff(arr_deriv_out))) < expected['laplacian']
Exemple #15
0
def test_find_and_sort_coord(img_ctl, expected):
    img = img_ctl[0].copy()
    centermass = find_and_sort_coord(img)
    assert centermass.shape == (3, 9)
    assert np.linalg.norm(centermass - img_ctl[2]) == 0
def test_find_and_sort_coord(img_ctl, expected):
    img = img_ctl[0].copy()
    centermass = find_and_sort_coord(img)
    assert centermass.shape == (3, 9)
    assert np.linalg.norm(centermass - img_ctl[2]) == 0