Exemplo n.º 1
0
def test_em_3d_demons():
    r'''
    Register a stack of circles ('cylinder') before and after warping them with
    a synthetic diffeomorphism. This test
    is intended to detect regressions only: we saved the energy profile (the
    sequence of energy values at each iteration) of a working version of EM in
    3D, and this test checks that the current energy profile matches the saved
    one. The validation of the "working version" was
    done by registering the 18 manually annotated T1 brain MRI database IBSR
    with each other and computing the jaccard index for all 31 common anatomical
    regions. The "working version" of EM in 3D obtains very similar results as
    those reported for ANTS on the same database. Any modification that produces
    a change in the energy profile should be carefully validated to ensure no
    accuracy loss.
    '''
    moving, static = get_synthetic_warped_circle(30)
    moving[...,:8] = 0
    moving[...,-1:-9:-1] = 0
    static[...,:8] = 0
    static[...,-1:-9:-1] = 0

    #Create the EM metric
    smooth=25.0
    inner_iter=20
    step_length=0.25
    q_levels=256
    double_gradient=True
    iter_type='demons'
    similarity_metric = metrics.EMMetric(
        3, smooth, inner_iter, q_levels, double_gradient, iter_type)

    #Create the optimizer
    level_iters = [10, 5]
    opt_tol = 1e-4
    inv_iter = 20
    inv_tol = 1e-3
    ss_sigma_factor = 0.5
    optimizer = imwarp.SymmetricDiffeomorphicRegistration(similarity_metric,
        level_iters, step_length, ss_sigma_factor, opt_tol, inv_iter, inv_tol)
    optimizer.verbosity = VerbosityLevels.DEBUG
    mapping = optimizer.optimize(static, moving, None)
    m = optimizer.get_map()
    assert_equal(mapping, m)
    energy_profile = np.array(optimizer.full_energy_profile)
    if floating is np.float32:
        expected_profile = \
            np.array([144.0369470764622, 122.39543007604394, 112.43783718421119,
                      85.46819602604248, 101.15549228031932, 119.62429965589826,
                      124.00100190950647, 118.94404608675168, 112.57666071129853,
                      117.84424645441413, 4470.2719430621, 4138.201850019068,
                      4007.225585554024, 4074.8853855654797, 3833.6272345908865])
    else:
        expected_profile = \
            np.array([144.03695786872666, 121.73922862297613, 107.41132697303448,
                      90.70731102557508, 97.4295175632117, 112.78404966709469,
                      103.29910157963684, 111.83865866152108, 121.26265581989485,
                      118.19913094423933, 4222.003181977351, 4418.042311441615,
                      4508.671160627819, 4761.251133428944, 4292.8507317299245])
    assert_array_almost_equal(energy_profile, expected_profile, decimal=6)
Exemplo n.º 2
0
def test_em_3d_demons():
    r'''
    Register a stack of circles ('cylinder') before and after warping them with
    a synthetic diffeomorphism. This test
    is intended to detect regressions only: we saved the energy profile (the
    sequence of energy values at each iteration) of a working version of EM in
    3D, and this test checks that the current energy profile matches the saved
    one. The validation of the "working version" was
    done by registering the 18 manually annotated T1 brain MRI database IBSR
    with each other and computing the jaccard index for all 31 common
    anatomical regions. The "working version" of EM in 3D obtains very similar
    results as those reported for ANTS on the same database. Any modification
    that produces a change in the energy profile should be carefully validated
    to ensure no accuracy loss.
    '''
    moving, static = get_synthetic_warped_circle(30)
    moving[...,:8] = 0
    moving[...,-1:-9:-1] = 0
    static[...,:8] = 0
    static[...,-1:-9:-1] = 0

    #Create the EM metric
    smooth=25.0
    inner_iter=20
    step_length=0.25
    q_levels=256
    double_gradient=True
    iter_type='demons'
    similarity_metric = metrics.EMMetric(
        3, smooth, inner_iter, q_levels, double_gradient, iter_type)

    #Create the optimizer
    level_iters = [10, 5]
    opt_tol = 1e-4
    inv_iter = 20
    inv_tol = 1e-3
    ss_sigma_factor = 0.5
    optimizer = imwarp.SymmetricDiffeomorphicRegistration(similarity_metric,
        level_iters, step_length, ss_sigma_factor, opt_tol, inv_iter, inv_tol)
    optimizer.verbosity = VerbosityLevels.DEBUG
    mapping = optimizer.optimize(static, moving, None)
    m = optimizer.get_map()
    assert_equal(mapping, m)
    energy_profile = subsample_profile(
        optimizer.full_energy_profile, 10)
    print(energy_profile)

    if USING_VC_SSE2:
        expected_profile = \
            np.array([144.03694708, 122.39512307, 111.31925381, 90.9100989,
                      93.93705232, 104.22993997, 110.57817867, 140.45262039,
                      133.87804571, 119.20794977])
    elif USING_GCC_SSE2:
        expected_profile = \
            np.array([144.03694708, 122.39512227, 111.31924572, 90.91010482,
                      93.93707059, 104.22996918, 110.57822649, 140.45298465,
                      133.87831302, 119.20826433])

    assert_array_almost_equal(energy_profile, expected_profile, decimal=4)
Exemplo n.º 3
0
def test_em_3d_gauss_newton():
    r'''
    Register a stack of circles ('cylinder') before and after warping them with
    a synthetic diffeomorphism. This test
    is intended to detect regressions only: we saved the energy profile (the
    sequence of energy values at each iteration) of a working version of EM in
    3D, and this test checks that the current energy profile matches the saved
    one. The validation of the "working version" was
    done by registering the 18 manually annotated T1 brain MRI database IBSR
    with each other and computing the jaccard index for all 31 common
    anatomical regions. The "working version" of EM in 3D obtains very similar
    results as those reported for ANTS on the same database. Any modification
    that produces a change in the energy profile should be carefully validated
    to ensure no accuracy loss.
    '''
    moving, static = get_synthetic_warped_circle(30)
    moving[...,:8] = 0
    moving[...,-1:-9:-1] = 0
    static[...,:8] = 0
    static[...,-1:-9:-1] = 0

    #Create the EM metric
    smooth=25.0
    inner_iter=20
    step_length=0.25
    q_levels=256
    double_gradient=True
    iter_type='gauss_newton'
    similarity_metric = metrics.EMMetric(
        3, smooth, inner_iter, q_levels, double_gradient, iter_type)

    #Create the optimizer
    level_iters = [10, 5]
    opt_tol = 1e-4
    inv_iter = 20
    inv_tol = 1e-3
    ss_sigma_factor = 0.5
    optimizer = imwarp.SymmetricDiffeomorphicRegistration(similarity_metric,
        level_iters, step_length, ss_sigma_factor, opt_tol, inv_iter, inv_tol)
    optimizer.verbosity = VerbosityLevels.DEBUG
    mapping = optimizer.optimize(static, moving, None)
    m = optimizer.get_map()
    assert_equal(mapping, m)
    energy_profile = subsample_profile(
        optimizer.full_energy_profile, 10)
    print(energy_profile)

    if USING_VC_SSE2:
        expected_profile = \
            np.array([144.03694724, 63.06874155, 51.84694887, 39.6374044,
                      31.84981429, 44.3778833, 37.84961761, 38.00509734,
                      38.67423812, 38.47003306])
    elif USING_GCC_SSE2:
        expected_profile = \
            np.array([144.03694724, 63.06874148, 51.84694881, 39.63740417,
                      31.84981481, 44.37788414, 37.84961844, 38.00509881,
                      38.67423954, 38.47003339])

    assert_array_almost_equal(energy_profile, expected_profile, decimal=4)
Exemplo n.º 4
0
def test_em_3d_gauss_newton():
    r'''
    Register a stack of circles ('cylinder') before and after warping them with
    a synthetic diffeomorphism. This test
    is intended to detect regressions only: we saved the energy profile (the
    sequence of energy values at each iteration) of a working version of EM in
    3D, and this test checks that the current energy profile matches the saved
    one. The validation of the "working version" was
    done by registering the 18 manually annotated T1 brain MRI database IBSR
    with each other and computing the jaccard index for all 31 common anatomical
    regions. The "working version" of EM in 3D obtains very similar results as
    those reported for ANTS on the same database. Any modification that produces
    a change in the energy profile should be carefully validated to ensure no
    accuracy loss.
    '''
    moving, static = get_synthetic_warped_circle(30)
    moving[...,:8] = 0
    moving[...,-1:-9:-1] = 0
    static[...,:8] = 0
    static[...,-1:-9:-1] = 0

    #Create the EM metric
    smooth=25.0
    inner_iter=20
    step_length=0.25
    q_levels=256
    double_gradient=True
    iter_type='gauss_newton'
    similarity_metric = metrics.EMMetric(
        3, smooth, inner_iter, q_levels, double_gradient, iter_type)

    #Create the optimizer
    level_iters = [10, 5]
    opt_tol = 1e-4
    inv_iter = 20
    inv_tol = 1e-3
    ss_sigma_factor = 0.5
    optimizer = imwarp.SymmetricDiffeomorphicRegistration(similarity_metric,
        level_iters, step_length, ss_sigma_factor, opt_tol, inv_iter, inv_tol)
    optimizer.verbosity = VerbosityLevels.DEBUG
    mapping = optimizer.optimize(static, moving, None)
    m = optimizer.get_map()
    assert_equal(mapping, m)
    energy_profile = np.array(optimizer.full_energy_profile)
    if floating is np.float32:
        expected_profile = \
            np.array([144.03694724, 63.06898905, 51.84577681, 39.75409677,
                      32.10342869, 44.84663951, 38.48587153, 36.64351228, 
                      37.14853803, 40.07766093, 1686.24351443, 1500.19633766,
                      1302.04852831, 1148.19549508, 1032.820053])
    else:
        expected_profile = \
            np.array([144.03695787, 63.06894122, 51.84575143, 39.75308705,
                      32.13062096, 44.15214831, 40.71952511, 37.26523679,
                      37.86654915, 34.92844873, 1644.56890565, 1408.15872151,
                      1274.1339093, 1131.38037004, 1004.71854514])
    assert_array_almost_equal(energy_profile, expected_profile, decimal=6)
Exemplo n.º 5
0
def test_em_3d_demons():
    r""" Test 3D SyN with EM metric, demons-like optimizer

    Register a volume created by stacking copies of a coronal slice from
    a T1w brain MRI before and after warping it under a synthetic
    invertible map. We verify that the final registration is of good
    quality.
    """
    fname = get_fnames('t1_coronal_slice')
    nslices = 21
    b = 0.1
    m = 4

    image = np.load(fname)
    moving, static = get_warped_stacked_image(image, nslices, b, m)

    # Create the EM metric
    smooth = 2.0
    inner_iter = 20
    step_length = 0.25
    q_levels = 256
    double_gradient = True
    iter_type = 'demons'
    similarity_metric = metrics.EMMetric(
        3, smooth, inner_iter, q_levels, double_gradient, iter_type)

    # Create the optimizer
    level_iters = [20, 5]
    opt_tol = 1e-4
    inv_iter = 20
    inv_tol = 1e-3
    ss_sigma_factor = 1.0
    optimizer = imwarp.SymmetricDiffeomorphicRegistration(
        similarity_metric,
        level_iters,
        step_length,
        ss_sigma_factor,
        opt_tol,
        inv_iter,
        inv_tol)
    optimizer.verbosity = VerbosityLevels.DEBUG
    mapping = optimizer.optimize(static, moving, None)
    m = optimizer.get_map()
    assert_equal(mapping, m)

    warped = mapping.transform(moving)
    starting_energy = np.sum((static - moving)**2)
    final_energy = np.sum((static - warped)**2)
    reduced = 1.0 - final_energy / starting_energy

    assert(reduced > 0.9)
Exemplo n.º 6
0
def test_em_2d_demons():
    r'''
    Register a circle to itself after warping it under a synthetic invertible
    map. This test is intended to detect regressions only: we saved the energy
    profile (the sequence of energy values at each iteration) of a working
    version of EM in 2D, and this test checks that the current energy profile
    matches the saved one.
    '''

    moving, static = get_synthetic_warped_circle(1)

    #Configure the metric
    smooth=25.0
    inner_iter=20
    q_levels=256
    double_gradient=False
    iter_type='demons'
    metric = metrics.EMMetric(
        2, smooth, inner_iter, q_levels, double_gradient, iter_type)

    #Configure and run the Optimizer
    level_iters = [40, 20, 10]
    optimizer = imwarp.SymmetricDiffeomorphicRegistration(metric, level_iters)
    optimizer.verbosity = VerbosityLevels.DEBUG
    mapping = optimizer.optimize(static, moving, None)
    m = optimizer.get_map()
    assert_equal(mapping, m)
    energy_profile = subsample_profile(
        optimizer.full_energy_profile, 10)
    print(energy_profile)

    if USING_VC_SSE2:
        expected_profile = \
            [2.50773393, 3.26942324, 1.81684393, 5.44878881, 40.0195918,
             31.87030788, 25.15710409, 29.82206485, 196.33114499, 213.86419995]
    elif USING_GCC_SSE2:
        expected_profile = \
            [2.50773393, 3.26942352, 1.8168445, 5.44879264, 40.01956373,
             31.65616398, 32.43115903, 35.24130742, 192.89072697, 195.456909]

    assert_array_almost_equal(energy_profile, np.array(expected_profile),
                              decimal=5)
Exemplo n.º 7
0
def test_em_2d_gauss_newton():
    r'''
    Register a circle to itself after warping it under a synthetic invertible
    map. This test is intended to detect regressions only: we saved the energy
    profile (the sequence of energy values at each iteration) of a working
    version of EM in 2D, and this test checks that the current energy profile
    matches the saved one.
    '''

    moving, static = get_synthetic_warped_circle(1)

    #Configure the metric
    smooth=25.0
    inner_iter=20
    q_levels=256
    double_gradient=False
    iter_type='gauss_newton'
    metric = metrics.EMMetric(
        2, smooth, inner_iter, q_levels, double_gradient, iter_type)

    #Configure and run the Optimizer
    level_iters = [40, 20, 10]
    optimizer = imwarp.SymmetricDiffeomorphicRegistration(metric, level_iters)
    optimizer.verbosity = VerbosityLevels.DEBUG
    mapping = optimizer.optimize(static, moving, None)
    m = optimizer.get_map()
    assert_equal(mapping, m)
    energy_profile = subsample_profile(
        optimizer.full_energy_profile, 10)
    print(energy_profile)

    if USING_VC_SSE2:
        expected_profile = \
            [2.50773392, 0.41762978, 0.30900322, 0.14818498, 0.44620725,
             1.53134054, 1.42115728, 1.66358267, 1.184265, 46.13635772]
    elif USING_GCC_SSE2:
        expected_profile = \
            [2.50773392, 0.41763383, 0.30908578, 0.06241115, 0.11573476,
             2.48475885, 1.10053769, 0.9270271, 49.37186785, 44.72643467]

    assert_array_almost_equal(energy_profile, np.array(expected_profile),
                              decimal=5)
Exemplo n.º 8
0
def test_em_2d_demons():
    r'''
    Register a circle to itself after warping it under a synthetic invertible
    map. This test is intended to detect regressions only: we saved the energy
    profile (the sequence of energy values at each iteration) of a working
    version of EM in 2D, and this test checks that the current energy profile
    matches the saved one.
    '''

    moving, static = get_synthetic_warped_circle(1)

    #Configure the metric
    smooth=25.0
    inner_iter=20
    q_levels=256
    double_gradient=False
    iter_type='demons'
    metric = metrics.EMMetric(
        2, smooth, inner_iter, q_levels, double_gradient, iter_type)

    #Configure and run the Optimizer
    level_iters = [40, 20, 10]
    optimizer = imwarp.SymmetricDiffeomorphicRegistration(metric, level_iters)
    optimizer.verbosity = VerbosityLevels.DEBUG
    mapping = optimizer.optimize(static, moving, None)
    m = optimizer.get_map()
    assert_equal(mapping, m)
    energy_profile = np.array(optimizer.full_energy_profile)[::2]
    if floating is np.float32:
        expected_profile = \
            [2.50773393, 4.59842633, 3.94307794, 3.09777134, 2.57982865,
             3.24937725, 0.42507437, 2.59523238, 29.8114103, 34.94621044,
             27.49480758, 38.64567224, 28.14442977, 25.34123425, 36.95076494,
             192.36444764, 202.90168694, 188.44310016, 199.73662253, 193.81159141]
    else:
        expected_profile = \
            [2.50773436, 4.59843299, 3.94307817, 3.09777401, 2.57983375,
             3.24936765, 0.42506361, 2.5952175, 29.81143768, 33.42148555,
             29.04341476, 29.44541313, 27.39435491, 27.62029669, 187.34889413,
             206.57998934, 198.48724278, 188.65410869, 177.83943006]
    assert_array_almost_equal(energy_profile, np.array(expected_profile))
Exemplo n.º 9
0
def test_em_2d_gauss_newton():
    r'''
    Register a circle to itself after warping it under a synthetic invertible
    map. This test is intended to detect regressions only: we saved the energy
    profile (the sequence of energy values at each iteration) of a working
    version of EM in 2D, and this test checks that the current energy profile
    matches the saved one.
    '''

    moving, static = get_synthetic_warped_circle(1)

    #Configure the metric
    smooth=25.0
    inner_iter=20
    q_levels=256
    double_gradient=False
    iter_type='gauss_newton'
    metric = metrics.EMMetric(
        2, smooth, inner_iter, q_levels, double_gradient, iter_type)

    #Configure and run the Optimizer
    level_iters = [40, 20, 10]
    optimizer = imwarp.SymmetricDiffeomorphicRegistration(metric, level_iters)
    optimizer.verbosity = VerbosityLevels.DEBUG
    mapping = optimizer.optimize(static, moving, None)
    m = optimizer.get_map()
    assert_equal(mapping, m)
    energy_profile = np.array(optimizer.full_energy_profile)[::4]
    if floating is np.float32:
        expected_profile = \
            [2.50773392e+00, 1.19082175e+00, 3.44192871e-01, 4.26320783e-01,
             3.77910892e-02, 3.34404847e-01, 3.00400618e+00, 2.56292691e+00,
             2.10458398e+00, 2.45479897e+00, 6.14513257e+01, 5.38091115e+01,
             5.27868250e+01]
    else:
        expected_profile = \
            [2.50773436, 1.19082577, 0.34422934, 0.19543193, 0.23659461,
             0.41145348, 3.56414698, 3.02325691, 1.74649377, 1.8172007,
             2.09930208, 53.06513917, 49.4088898 ]
    assert_array_almost_equal(energy_profile, np.array(expected_profile))
Exemplo n.º 10
0
def test_em_2d_gauss_newton():
    r""" Test 2D SyN with EM metric, Gauss-Newton optimizer

    Register a coronal slice from a T1w brain MRI before and after warping
    it under a synthetic invertible map. We verify that the final
    registration is of good quality.
    """

    fname = get_fnames('t1_coronal_slice')
    nslices = 1
    b = 0.1
    m = 4

    image = np.load(fname)
    moving, static = get_warped_stacked_image(image, nslices, b, m)

    # Configure the metric
    smooth = 5.0
    inner_iter = 20
    q_levels = 256
    double_gradient = False
    iter_type = 'gauss_newton'
    metric = metrics.EMMetric(2, smooth, inner_iter, q_levels, double_gradient,
                              iter_type)

    # Configure and run the Optimizer
    level_iters = [40, 20, 10]
    optimizer = imwarp.SymmetricDiffeomorphicRegistration(metric, level_iters)
    optimizer.verbosity = VerbosityLevels.DEBUG
    mapping = optimizer.optimize(static, moving, None)
    m = optimizer.get_map()
    assert_equal(mapping, m)

    warped = mapping.transform(moving)
    starting_energy = np.sum((static - moving)**2)
    final_energy = np.sum((static - warped)**2)
    reduced = 1.0 - final_energy / starting_energy

    assert (reduced > 0.9)