예제 #1
0
 def test_ab(self):
     x, y, r, nt = 10, 20, 0, 9999
     a0, b0, a1, b1 = 10, 5, 8, 15
     data0 = ret.make_ellipse_data_points(x, y, a0, b0, r, nt=nt, use_focus=False)
     assert np.allclose(data0.min(axis=0), (x - a0, y - b0), atol=10e-5)
     data1 = ret.make_ellipse_data_points(x, y, a1, b1, r, nt=nt, use_focus=False)
     assert np.allclose(data1.min(axis=0), (x - a1, y - b1), atol=10e-5)
예제 #2
0
 def test_r(self):
     x, y, a, b, nt = 10, 20, 15, 5, 9999
     r0, r1 = 0, math.pi / 2
     data0 = ret.make_ellipse_data_points(x, y, a, b, r0, nt=nt, use_focus=False)
     assert np.allclose(data0.min(axis=0), (x - a, y - b), atol=10e-5)
     data1 = ret.make_ellipse_data_points(x, y, a, b, r1, nt=nt, use_focus=False)
     assert np.allclose(data1.min(axis=0), (x - b, y - a), atol=10e-5)
예제 #3
0
 def test_xy_use_focus_false(self):
     x0, y0, x1, y1 = 10, -5, -20, 30
     a, b, r = 10, 8, 0
     data0 = ret.make_ellipse_data_points(x0, y0, a, b, r, nt=99, use_focus=False)
     assert np.allclose(data0.mean(axis=0), (x0, y0))
     data1 = ret.make_ellipse_data_points(x1, y1, a, b, r, nt=99, use_focus=False)
     assert np.allclose(data1.mean(axis=0), (x1, y1))
예제 #4
0
 def test_xy_use_focus_true(self):
     x0, y0, x1, y1 = 10, -5, -20, 30
     a, b, r = 10, 8, 0
     data0 = ret.make_ellipse_data_points(x0, y0, a, b, r, nt=99, use_focus=True)
     xc0, yc0 = data0.mean(axis=0)
     f0 = ret._get_closest_focus(x0, y0, xc0, yc0, a, b, r)
     assert approx(f0) == (x0, y0)
     data1 = ret.make_ellipse_data_points(x1, y1, a, b, r, nt=99, use_focus=True)
     xc1, yc1 = data1.mean(axis=0)
     f1 = ret._get_closest_focus(x1, y1, xc1, yc1, a, b, r)
     assert approx(f1) == (x1, y1)
예제 #5
0
    def test_semi_len_max(self):
        xf, yf, a, b, r = 50, 55, 21, 20, 2
        data = ret.make_ellipse_data_points(xf, yf, a, b, r, nt=25)
        model_ransac0, inliers0 = ret.get_ellipse_model_ransac_single_frame(
            data,
            xf=50,
            yf=55,
            rf_lim=2,
            semi_len_min=None,
            semi_len_max=25,
            semi_len_ratio_lim=2,
            min_samples=6,
            residual_threshold=10,
            max_trails=100,
        )
        params0f = (xf, yf, a, b, r)
        params1f = ret._ellipse_model_centre_to_focus(*model_ransac0.params, xf, yf)
        assert inliers0.all()
        compare_model_params(params0f, params1f, abs=0.1)

        model_ransac1, inliers1 = ret.get_ellipse_model_ransac_single_frame(
            data,
            xf=50,
            yf=55,
            rf_lim=2,
            semi_len_min=None,
            semi_len_max=15,
            semi_len_ratio_lim=2,
            min_samples=6,
            residual_threshold=10,
            max_trails=100,
        )
        assert model_ransac1 is None
예제 #6
0
 def test_min_samples(self):
     xf, yf, a, b, r = 50, 55, 21, 20, 0
     data = ret.make_ellipse_data_points(xf, yf, a, b, r, nt=20)
     data = np.vstack((data, [70, 55]))
     model_ransac0, inliers0 = ret.get_ellipse_model_ransac_single_frame(
         data,
         xf=50,
         yf=55,
         rf_lim=5,
         semi_len_min=15,
         semi_len_max=None,
         semi_len_ratio_lim=None,
         min_samples=20,
         residual_threshold=3,
         max_trails=100,
     )
     assert inliers0[:-1].all()
     assert not inliers0[-1]
     model_ransac1, inliers1 = ret.get_ellipse_model_ransac_single_frame(
         data,
         xf=50,
         yf=55,
         rf_lim=5,
         semi_len_min=15,
         semi_len_max=None,
         semi_len_ratio_lim=None,
         min_samples=20,
         residual_threshold=6,
         max_trails=100,
     )
     assert inliers1.all()
예제 #7
0
def test_full_ellipse_ransac_processing():
    xf, yf, a, b, r, nt = 100, 115, 45, 35, 0, 15
    data_points = ret.make_ellipse_data_points(xf, yf, a, b, r, nt)
    image = np.zeros(shape=(200, 210), dtype=np.float32)
    for x, y in data_points:
        image[int(round(x)), int(round(y))] = 100
    disk = morphology.disk(5, np.uint16)
    image = convolve2d(image, disk, mode="same")
    data = np.zeros((2, 3, 210, 200), dtype=np.float32)
    data[:, :] = image.T

    s = Diffraction2D(data)
    s_t = s.template_match_disk(disk_r=5)
    peak_array = s_t.find_peaks_lazy(lazy_result=False)

    c = math.sqrt(math.pow(a, 2) - math.pow(b, 2))
    xc, yc = xf - c * math.cos(r), yf - c * math.sin(r)

    for iy, ix in np.ndindex(peak_array.shape):
        peaks = peak_array[iy, ix]
        assert len(peaks) == 15
        assert approx(peaks[:, 1].mean(), abs=2) == xc
        assert approx(peaks[:, 0].mean(), abs=2) == yc
        assert approx(peaks[:, 1].max(), abs=2) == xc + a
        assert approx(peaks[:, 0].max(), abs=2) == yc + b
        assert approx(peaks[:, 1].min(), abs=2) == xc - a
        assert approx(peaks[:, 0].min(), abs=2) == yc - b

    ellipse_array, inlier_array = ret.get_ellipse_model_ransac(
        peak_array,
        yf=yf,
        xf=xf,
        rf_lim=15,
        semi_len_min=min(a, b) - 5,
        semi_len_max=max(a, b) + 5,
        semi_len_ratio_lim=5,
        max_trails=50,
        min_samples=10,
    )
    s.add_ellipse_array_as_markers(ellipse_array)

    for iy, ix in np.ndindex(ellipse_array.shape):
        ycf, xcf, bf, af, rf = ellipse_array[iy, ix]
        if af < bf:
            rf += math.pi / 2
            af, bf = bf, af
        assert approx((xcf, ycf, af, bf, rf), abs=0.1) == [xc, yc, a, b, r]
        assert inlier_array[iy, ix].all()

    s.add_ellipse_array_as_markers(ellipse_array)
    x_list, y_list = [], []
    for _, marker in list(s.metadata.Markers):
        x_list.append(marker.data["x1"][()][0][0])
        y_list.append(marker.data["y1"][()][0][0])
    assert approx(np.mean(x_list), abs=1) == xc
    assert approx(np.mean(y_list), abs=1) == yc
    assert approx(np.max(x_list), abs=1) == xc + a
    assert approx(np.max(y_list), abs=1) == yc + b
    assert approx(np.min(x_list), abs=1) == xc - a
    assert approx(np.min(y_list), abs=1) == yc - b
예제 #8
0
 def test_use_focus(self):
     x, y, a, b, r = 5, 9, 10, 8, 0
     data0 = ret.make_ellipse_data_points(x,
                                          y,
                                          a,
                                          b,
                                          r,
                                          nt=99,
                                          use_focus=False)
     assert np.allclose(data0.mean(axis=0), (x, y))
     data1 = ret.make_ellipse_data_points(x,
                                          y,
                                          a,
                                          b,
                                          r,
                                          nt=99,
                                          use_focus=True)
     assert not np.allclose(data1.mean(axis=0), (x, y))
예제 #9
0
    def test_yf(self):
        xf, yf0, yf1, a, b, r = 50, 55, 140, 21, 20, 2
        data0 = ret.make_ellipse_data_points(xf, yf0, a, b, r, nt=25)
        data1 = ret.make_ellipse_data_points(xf, yf1, a, b, r, nt=25)
        data = np.vstack((data0, data1))
        model_ransac0, inliers0 = ret.get_ellipse_model_ransac_single_frame(
            data,
            xf=50,
            yf=yf0,
            rf_lim=2,
            semi_len_min=19,
            semi_len_max=None,
            semi_len_ratio_lim=2,
            min_samples=5,
            residual_threshold=10,
            max_trails=300,
        )
        params00f = (xf, yf0, a, b, r)
        params01f = ret._ellipse_model_centre_to_focus(*model_ransac0.params,
                                                       xf, yf0)
        compare_model_params(params00f, params01f, abs=0.1)

        model_ransac1, inliers1 = ret.get_ellipse_model_ransac_single_frame(
            data,
            xf=50,
            yf=yf1,
            rf_lim=2,
            semi_len_min=19,
            semi_len_max=None,
            semi_len_ratio_lim=2,
            min_samples=5,
            residual_threshold=10,
            max_trails=300,
        )
        params10f = (xf, yf1, a, b, r)
        params11f = ret._ellipse_model_centre_to_focus(*model_ransac1.params,
                                                       xf, yf1)
        compare_model_params(params10f, params11f, abs=0.1)
예제 #10
0
def _make_4d_peak_array_test_data(xf, yf, semi0, semi1, rot, nt=20):
    """Get a 4D NumPy array with peak_array test data.

    Parameters
    ----------
    xf, yf : scalar, 2D NumPy array
        Centre position of the ellipse. The size of the xf array gives the
        size of the peak_array.
    semi0, semi1 : scalar, 2D NumPy array
        Semi length of the ellipse, of rot is 0 semi0 is the x-direction
        semi length, and semi1 the y-direction.
    rot : scalar, 2D NumPy array
        Rotation in radians.
    nt : scalar
        Number of points in the ellipse.

    Returns
    -------
    peak_array : NumPy 4D array

    Examples
    --------
    >>> import pyxem as pxm
    >>> from pyxem.dummy_data import make_diffraction_test_data as mdtd
    >>> xf = np.random.randint(65, 70, size=(4, 5))
    >>> yf = np.random.randint(115, 120, size=(4, 5))
    >>> semi0 = np.random.randint(35, 40, size=(4, 5))
    >>> semi1 = np.random.randint(45, 50, size=(4, 5))
    >>> rot = np.random.random(size=(4, 5)) * 0.2
    >>> peak_array = mdtd._make_4d_peak_array_test_data(
    ...        xf, yf, semi0, semi1, rot)
    >>> s = pxm.signals.Diffraction2D(np.zeros(shape=(4, 5, 200, 210)))
    >>> import pyxem.utils.marker_tools as mt
    >>> mt.add_peak_array_to_signal_as_markers(s, peak_array)

    """
    peak_array = np.empty_like(xf, dtype=np.object)
    for iy, ix in np.ndindex(peak_array.shape):
        params = (xf[iy, ix], yf[iy, ix], semi0[iy,
                                                ix], semi1[iy,
                                                           ix], rot[iy,
                                                                    ix], nt)
        ellipse_points = ret.make_ellipse_data_points(*params)
        peak_array[iy, ix] = np.fliplr(ellipse_points)
    return peak_array
예제 #11
0
 def test_simple(self):
     data = ret.make_ellipse_data_points(50, 55, 20, 16, 2, nt=15)
     ret.get_ellipse_model_ransac_single_frame(data)
예제 #12
0
 def test_nt(self):
     data0 = ret.make_ellipse_data_points(5, 2, 9, 5, 0, nt=10)
     assert data0.shape == (10, 2)
     data1 = ret.make_ellipse_data_points(5, 2, 9, 5, 0, nt=29)
     assert data1.shape == (29, 2)
예제 #13
0
 def test_simple(self):
     data = ret.make_ellipse_data_points(5, 2, 9, 5, 0)
     assert data.size > 0
예제 #14
0
 def test_min_samples_smaller_than_data(self):
     data = ret.make_ellipse_data_points(50, 55, 20, 16, 2, nt=15)
     ret.get_ellipse_model_ransac_single_frame(data, min_samples=25)