Example #1
0
def test_properties():
    g = Gaussian2D(add_rotation=True)
    angle = np.radians(20)
    g.rotation_angle.value = angle
    np.testing.assert_allclose(g.rotation_angle_wrapped, angle)

    angle = np.radians(380)
    g.rotation_angle.value = angle
    np.testing.assert_allclose(g.rotation_angle_wrapped,
                               math.fmod(angle, 2 * np.pi))

    g = Gaussian2D(add_rotation=True)
    g.sigma_x.value = 0.5
    g.sigma_y.value = 0.1
    assert g.ellipticity == 5.0
    assert g.rotation_angle.value == 0
    assert g.sigma_major == 0.5
    assert g.sigma_minor == 0.1
    angle = np.radians(20)
    g.rotation_angle.value = angle
    np.testing.assert_allclose(g.rotation_angle_wrapped, angle)
    np.testing.assert_allclose(g.rotation_major_axis, angle)

    g = Gaussian2D(add_rotation=True)
    g.sigma_x.value = 0.1
    g.sigma_y.value = 0.5
    assert g.ellipticity == 5.0
    assert g.rotation_angle.value == 0
    assert g.sigma_major == 0.5
    assert g.sigma_minor == 0.1
    angle = np.radians(20)
    g.rotation_angle.value = angle
    np.testing.assert_allclose(g.rotation_angle_wrapped, angle)
    np.testing.assert_allclose(g.rotation_major_axis, angle - np.pi / 2)
Example #2
0
    def test_model2D_linear_many_gaussians(self, nav2d):
        mesh, x, y = self.mesh, self.x, self.y
        gausslow, gausshigh = -8, 8
        gauss_step = 8
        X, Y = mesh
        z = np.zeros(X.shape)
        g = Gaussian2D()
        for i in np.arange(gausslow, gausshigh + 1, gauss_step):
            for j in np.arange(gausslow, gausshigh + 1, gauss_step):
                g.centre_x.value = i
                g.centre_y.value = j
                g.A.value = 10
                z += g.function(X, Y)

        s = Signal2D(z)
        s.axes_manager[-2].offset = x[0]
        s.axes_manager[-1].offset = y[0]

        s.axes_manager[-2].scale = x[1] - x[0]
        s.axes_manager[-1].scale = y[1] - y[0]

        if nav2d:
            s = hs.stack([s] * 2)
            s = hs.stack([s] * 3)

        m = s.create_model()
        for i in np.arange(gausslow, gausshigh + 1, gauss_step):
            for j in np.arange(gausslow, gausshigh + 1, gauss_step):
                g = Gaussian2D(centre_x=i, centre_y=j)
                g.set_parameters_not_free()
                g.A.free = True
                m.append(g)

        m.fit(optimizer='lstsq')
        np.testing.assert_allclose(s.data, m.as_signal().data)
Example #3
0
def test_util_fwhm_set():
    g1 = Gaussian2D()
    g1.fwhm_x = 0.33
    g1.fwhm_y = 0.33
    g1.A.value = 1.0
    np.testing.assert_allclose(g1.fwhm_x, g1.sigma_x.value * sigma2fwhm)
    np.testing.assert_allclose(g1.fwhm_y, g1.sigma_y.value * sigma2fwhm)
Example #4
0
    def test_model2D_one_component(self, nav2d):
        mesh, x, y = self.mesh, self.x, self.y
        G1 = Gaussian2D(30, 5.0, 4.0, 0, 0)

        data = G1.function(*mesh)
        s = Signal2D(data)
        s.axes_manager[-2].offset = x[0]
        s.axes_manager[-1].offset = y[0]

        s.axes_manager[-2].scale = x[1] - x[0]
        s.axes_manager[-1].scale = y[1] - y[0]

        if nav2d:
            s = hs.stack([s] * 2)
            s = hs.stack([s] * 3)

        m = s.create_model()
        m.append(G1)

        G1.set_parameters_not_free()
        G1.A.free = True

        m.multifit(optimizer='lstsq', calculate_errors=True)
        diff = (s - m.as_signal(show_progressbar=False))
        np.testing.assert_allclose(diff.data, 0.0, atol=1E-7)
        np.testing.assert_allclose(m.p_std[0], 0.0, atol=1E-7)
Example #5
0
def test_height_value():
    g = Gaussian2D()
    g.sigma_x.value = 0.1
    g.sigma_y.value = 0.5
    g.A.value = 99
    x = np.arange(-2, 2, 0.01)
    y = np.arange(-2, 2, 0.01)
    xx, yy = np.meshgrid(x, y)
    g_image = g.function(xx, yy)
    assert approx(g_image.max()) == g.height
Example #6
0
def test_function():
    g = Gaussian2D()
    g.A.value = 14
    g.sigma_x.value = 1.
    g.sigma_y.value = 2.
    g.centre_x.value = -5.
    g.centre_y.value = -5.
    np.testing.assert_allclose(g.function(-5, -5), 1.1140846)
    np.testing.assert_allclose(g.function(-2, -3), 0.007506643)
    assert g._is2D
    assert g._position_x == g.centre_x
    assert g._position_y == g.centre_y
Example #7
0
def test_util_height_getset():
    g = Gaussian2D()
    g.height = 0.165
    assert g.height == 0.165
Example #8
0
def test_util_height_get():
    g = Gaussian2D(A=55)
    np.testing.assert_allclose(
        g.height, g.A.value / (2 * np.pi * g.sigma_x.value * g.sigma_y.value))
Example #9
0
def test_util_fwhm_getset():
    g1 = Gaussian2D()
    g1.fwhm_x = 0.33
    g1.fwhm_y = 0.33
    assert g1.fwhm_x == 0.33
    assert g1.fwhm_y == 0.33
Example #10
0
def test_util_fwhm_get():
    g1 = Gaussian2D(sigma_x=0.33, sigma_y=0.33)
    g1.A.value = 1.0
    assert_allclose(g1.fwhm_x, g1.sigma_x.value * sigma2fwhm)
    assert_allclose(g1.fwhm_y, g1.sigma_y.value * sigma2fwhm)