Example #1
0
def test_expected_absolute_value_1d():
    """
    Tests that expected absolute value works for the one dimensional case
    """
    np.random.seed(0)
    stddevs = np.array([0.0], dtype=np.float64)

    stddevs[0] = 0.0
    eav = e2d.expected_absolute_value(stddevs)

    assert eav == 0.0
    assert eav == _eav_by_brute_force(stddevs)

    stddevs[0] = 1.0
    eav = e2d.expected_absolute_value(stddevs)

    arith_eav = np.linalg.norm(stddevs)
    arith_eav *= arith_eav
    assert np.isclose(eav, arith_eav, atol=0.0, rtol=0.0001)
    #absolute(a - b) <= (atol + rtol * absolute(b))
    #so rtol = 0.1 is around 10% rtol = 0.01 is 1 %
    assert np.isclose(eav, _eav_by_brute_force(stddevs), atol=0.0, rtol=0.15)

    for _ in range(10):
        stddevs[0] = np.absolute(np.random.normal() * 10.0)
        eav = e2d.expected_absolute_value(stddevs)

        arith_eav = np.linalg.norm(stddevs)
        arith_eav *= arith_eav
        assert np.isclose(eav, arith_eav, atol=0.0, rtol=0.0001)
        assert np.isclose(eav,
                          _eav_by_brute_force(stddevs),
                          atol=0.0,
                          rtol=0.15)
Example #2
0
def test_expected_absolute_value_3d():
    """
    Tests that expected absolute value works for the three dimensional case
    """
    np.random.seed(0)
    stddevs = np.array([0.0, 0.0, 0.0], dtype=np.float64)

    eav = e2d.expected_absolute_value(stddevs)

    assert eav == 0.0
    assert eav == _eav_by_brute_force(stddevs)

    stddevs = np.array([1.0, 1.0, 1.0], dtype=np.float64)
    eav = e2d.expected_absolute_value(stddevs)

    arith_eav = np.linalg.norm(stddevs)
    arith_eav *= arith_eav
    assert np.isclose(eav, arith_eav, atol=0.0, rtol=0.0001)
    assert np.isclose(eav, _eav_by_brute_force(stddevs), atol=0.0, rtol=0.15)

    for _ in range(10):
        stddevs = np.absolute(np.random.normal((1, 1, 1)) * 10.0)
        eav = e2d.expected_absolute_value(stddevs)
        arith_eav = np.linalg.norm(stddevs)
        arith_eav *= arith_eav
        assert np.isclose(eav, arith_eav, atol=0.0, rtol=0.0001)
        assert np.isclose(eav,
                          _eav_by_brute_force(stddevs),
                          atol=0.0,
                          rtol=0.15)
Example #3
0
    def init_reg(self):
        """
        sets up the registration
        """
        img = skimage.io.imread(self.image_file_name)
        outline, _initial_guess = find_outer_contour(img)
        target_point = make_target_point(outline)

        self.plotter.initialise_new_reg(img, target_point, outline)

        fle_sd = np.random.uniform(low=0.5, high=5.0)
        moving_fle = np.zeros((1, 3), dtype=np.float64)

        fixed_fle = np.array([fle_sd, fle_sd, fle_sd], dtype=np.float64)
        fixed_fle_eavs = expected_absolute_value(fixed_fle)
        moving_fle_eavs = expected_absolute_value(moving_fle)

        if self.pbr is None:
            self.pbr = PointBasedRegistration(target_point, fixed_fle_eavs,
                                              moving_fle_eavs)
        else:
            self.pbr.reinit(target_point, fixed_fle_eavs, moving_fle_eavs)

        if self.mouse_int is None:
            self.mouse_int = AddFiducialMarker(self.fig, self.plotter,
                                               self.pbr, self.logger,
                                               fixed_fle, moving_fle)

        self.mouse_int.reset_fiducials(fixed_fle_eavs)
        return target_point
Example #4
0
def test_pbr_3_fids():
    """
    Tests for tre_from_fle_2d
    """
    fixed_fle_std_dev = np.array([1.0, 1.0, 1.0], dtype=np.float64)
    moving_fle_std_dev = np.array([0.0, 0.0, 0.0], dtype=np.float64)

    fixed_fle_easv = expected_absolute_value(fixed_fle_std_dev)
    moving_fle_easv = expected_absolute_value(moving_fle_std_dev)

    target = np.array([[0.0, 0.0, 0.0]], dtype=np.float64)

    pbr = pbreg.PointBasedRegistration(target, fixed_fle_easv, moving_fle_easv)

    centre = np.array([0.0, 0.0, 0.0], dtype=np.float64)
    radius = 20.0

    expected_tre_squared = 0
    expected_fre = 0
    repeats = 100
    no_fids = 3

    ave_tresq, ave_fresq,  expected_tre_squared, expected_fre, p_value = \
                    _run_registrations(pbr, no_fids, centre, radius,
                                       fixed_fle_std_dev,
                                       moving_fle_std_dev, repeats)

    assert np.isclose(ave_tresq, expected_tre_squared, atol=0.0, rtol=0.10)
    assert np.isclose(ave_fresq, expected_fre, atol=0.0, rtol=0.05)
    assert p_value > 0.05
Example #5
0
def test_init_with_moving_fle():
    """
    Init pbr with moving fle should yield non implemented error
    """
    fixed_fle_std_dev = np.array([1.0, 1.0, 1.0], dtype=np.float64)
    moving_fle_std_dev = np.array([1.0, 1.0, 1.0], dtype=np.float64)

    fixed_fle_easv = expected_absolute_value(fixed_fle_std_dev)
    moving_fle_easv = expected_absolute_value(moving_fle_std_dev)

    target = np.array([[0.0, 0.0, 0.0]], dtype=np.float64)

    with pytest.raises(NotImplementedError):
        pbreg.PointBasedRegistration(target, fixed_fle_easv, moving_fle_easv)
Example #6
0
def test_pbr_20_fids_offset_target():
    """
    Tests for tre_from_fle_2d
    """

    fixed_fle_std_dev = np.array([1.0, 1.0, 1.0], dtype=np.float64)
    moving_fle_std_dev = np.array([0.0, 0.0, 0.0], dtype=np.float64)

    fixed_fle_easv = expected_absolute_value(fixed_fle_std_dev)
    moving_fle_easv = expected_absolute_value(moving_fle_std_dev)

    target = np.array([[2.0, 1.0, 0.0]], dtype=np.float64)

    pbr = pbreg.PointBasedRegistration(target, fixed_fle_easv, moving_fle_easv)

    centre = np.array([0.0, 0.0, 0.0], dtype=np.float64)
    radius = 20.0

    repeats = 200
    no_fids = 20

    #test get transformed target before registration
    status, transformed_target = pbr.get_transformed_target()
    assert not status
    assert transformed_target is None

    ave_tresq, ave_fresq,  expected_tre_squared, expected_fre, p_value = \
                    _run_registrations(pbr, no_fids, centre, radius,
                                       fixed_fle_std_dev,
                                       moving_fle_std_dev, repeats)

    assert np.isclose(ave_tresq, expected_tre_squared, atol=0.0, rtol=0.10)
    assert np.isclose(ave_fresq, expected_fre, atol=0.0, rtol=0.05)
    assert p_value > 0.05

    #test get transformed target after registration
    status, transformed_target = pbr.get_transformed_target()
    assert status
    assert np.allclose(np.transpose(transformed_target), target, atol=1.0)
Example #7
0
def getfle():
    """
    Returns values for fiducial localisation errors
    Values are randomly selected from a uniform
    distribution from 0.5 to 5.0 pixels
    """
    fle_sd = np.random.uniform(low=0.5, high=5.0)
    #change fle_ratio if you want anisotropic fle
    fle_ratio = np.array([1.0, 1.0, 1.0], dtype=np.float64)
    anis_scale = math.sqrt(3.0 / (np.linalg.norm(fle_ratio)**2))
    fixed_fle = fle_ratio * fle_sd * anis_scale

    moving_fle = np.array([0., 0., 0.], dtype=np.float64)
    fixed_fle_eavs = expected_absolute_value(fixed_fle)
    moving_fle_eavs = expected_absolute_value(moving_fle)

    returnjson = jsonify({
        'fixed_fle_sd': fixed_fle.tolist(),
        'moving_fle_sd': moving_fle.tolist(),
        'fixed_fle_eav': fixed_fle_eavs.tolist(),
        'moving_fle_eav': moving_fle_eavs.tolist()
    })
    return returnjson