Example #1
0
def test_function():
    g = Voigt(legacy=False)
    g.area.value = 5
    g.sigma.value = 0.5
    g.gamma.value = 0.2
    g.centre.value = 1
    assert_allclose(g.function(0), 0.78853024)
    assert_allclose(g.function(1), 2.97832092)
Example #2
0
def test_legacy():
    """Legacy test, to be removed in v2.0."""
    with pytest.warns(
            VisibleDeprecationWarning,
            match="API of the `Voigt` component will change",
    ):
        g = Voigt(legacy=True)
        g.area.value = 5
        g.FWHM.value = 0.5
        g.gamma.value = 0.2
        g.centre.value = 1
        np.testing.assert_allclose(g.function(0), 0.35380168)
        np.testing.assert_allclose(g.function(1), 5.06863535)
Example #3
0
    def get_background_model(self, saturation_radius):
        """Creates a model for the background of the signal.

        The mean radial profile is fitted with the following three components:

        * Voigt profile for the central beam
        * Exponential profile for the diffuse scatter
        * Linear profile for the background offset and to improve the fit

        Using the exponential profile and the linear profile, an
        ElectronDiffraction signal is produced representing the mean background
        of the signal. This may be used for background subtraction.

        Parameters
        ----------
        saturation_radius : int
            The radius of the region about the central beam in which pixels are
            saturated.

        Returns
        -------
        ElectronDiffraction
            The mean background of the signal.

        """
        # TODO: get this done without taking the mean
        profile = self.get_radial_profile().mean()
        model = profile.create_model()
        e1 = saturation_radius * profile.axes_manager.signal_axes[0].scale
        model.set_signal_range(e1)

        direct_beam = Voigt()
        direct_beam.centre.value = 0
        direct_beam.centre.free = False
        direct_beam.FWHM.value = 0.1
        direct_beam.area.bmin = 0
        model.append(direct_beam)

        diffuse_scatter = Exponential()
        diffuse_scatter.A.value = 0
        diffuse_scatter.A.bmin = 0
        diffuse_scatter.tau.value = 0
        diffuse_scatter.tau.bmin = 0
        model.append(diffuse_scatter)

        linear_decay = Polynomial(1)
        model.append(linear_decay)

        model.fit(bounded=True)

        x_axis = self.axes_manager.signal_axes[0].axis
        y_axis = self.axes_manager.signal_axes[1].axis
        xs, ys = np.meshgrid(x_axis, y_axis)
        rs = (xs ** 2 + ys ** 2) ** 0.5
        bg = ElectronDiffraction(
            diffuse_scatter.function(rs) + linear_decay.function(rs))
        for i in (0, 1):
            bg.axes_manager.signal_axes[i].update_from(
                self.axes_manager.signal_axes[i])
        return bg
Example #4
0
def test_estimate_parameters_binned(only_current, binned, lazy, uniform):
    s = Signal1D(np.empty((200, )))
    s.axes_manager.signal_axes[0].is_binned = binned
    axis = s.axes_manager.signal_axes[0]
    axis.scale = .05
    axis.offset = -5
    g1 = Voigt(centre=1, area=5, gamma=0.001, sigma=0.5, legacy=False)
    s.data = g1.function(axis.axis)
    if lazy:
        s = s.as_lazy()
    g2 = Voigt(legacy=False)
    if not uniform:
        axis.convert_to_non_uniform_axis()
    if binned and uniform:
        factor = axis.scale
    elif binned:
        factor = np.gradient(axis.axis)
    else:
        factor = 1
    assert g2.estimate_parameters(s,
                                  axis.low_value,
                                  axis.high_value,
                                  only_current=only_current)
    assert g2._axes_manager[-1].is_binned == binned
    np.testing.assert_allclose(g2.sigma.value, 0.5, 0.01)
    np.testing.assert_allclose(g1.area.value, g2.area.value * factor, 0.01)
    np.testing.assert_allclose(g2.centre.value, 1, 1e-3)
Example #5
0
def test_function_nd(binned, lazy):
    s = Signal1D(np.empty((200, )))
    s.metadata.Signal.binned = binned
    axis = s.axes_manager.signal_axes[0]
    axis.scale = .05
    axis.offset = -5
    g1 = Voigt(centre=1, area=5, gamma=0, sigma=0.5, legacy=False)
    s.data = g1.function(axis.axis)
    s2 = stack([s] * 2)
    if lazy:
        s2 = s2.as_lazy()
    g2 = Voigt(legacy=False)
    factor = axis.scale if binned else 1
    g2.estimate_parameters(s2, axis.low_value, axis.high_value, False)
    assert g2.binned == binned
    assert_allclose(g2.function_nd(axis.axis) * factor, s2.data)
Example #6
0
def test_estimate_parameters_binned(only_current, binned, lazy):
    s = Signal1D(np.empty((200, )))
    s.metadata.Signal.binned = binned
    axis = s.axes_manager.signal_axes[0]
    axis.scale = .05
    axis.offset = -5
    g1 = Voigt(centre=1, area=5, gamma=0.001, sigma=0.5, legacy=False)
    s.data = g1.function(axis.axis)
    if lazy:
        s = s.as_lazy()
    g2 = Voigt(legacy=False)
    factor = axis.scale if binned else 1
    assert g2.estimate_parameters(s,
                                  axis.low_value,
                                  axis.high_value,
                                  only_current=only_current)
    assert g2.binned == binned
    assert_allclose(g2.sigma.value, 0.5, 0.01)
    assert_allclose(g1.area.value, g2.area.value * factor, 0.01)
    assert_allclose(g2.centre.value, 1, 1e-3)
Example #7
0
def test_util_lwidth_getset():
    g1 = Voigt(legacy=False)
    g1.lwidth = 3.0
    assert_allclose(g1.lwidth, 3.0)
Example #8
0
def test_util_lwidth_get():
    g1 = Voigt(legacy=False)
    g1.gamma.value = 3.0
    assert_allclose(g1.lwidth / 2, g1.gamma.value)
Example #9
0
def test_util_FWHM_getset():
    g1 = Voigt(legacy=False)
    g1.FWHM = 1.0
    assert_allclose(g1.FWHM, 1.0)
Example #10
0
def test_util_FWHM_get():
    g1 = Voigt(legacy=False)
    g1.sigma.value = 1.0
    assert_allclose(g1.FWHM, 1.0 * (2 * np.sqrt(2 * np.log(2))))
Example #11
0
def test_util_gwidth_getset():
    g1 = Voigt(legacy=False)
    g1.gwidth = 1.0
    assert_allclose(g1.gwidth, 1.0)
Example #12
0
def test_util_gwidth_set():
    g1 = Voigt(legacy=False)
    g1.gwidth = 1.0
    assert_allclose(g1.sigma.value, 1.0 / (2 * np.sqrt(2 * np.log(2))))
Example #13
0
def test_util_lwidth_set():
    g1 = Voigt(legacy=False)
    g1.lwidth = 3.0
    np.testing.assert_allclose(g1.lwidth / 2, g1.gamma.value)
Example #14
0
def test_util_FWHM_set():
    g1 = Voigt(legacy=False)
    g1.FWHM = 1.0
    np.testing.assert_allclose(g1.sigma.value,
                               1.0 / (2 * np.sqrt(2 * np.log(2))))
Example #15
0
def test_util_gwidth_get():
    g1 = Voigt(legacy=False)
    g1.sigma.value = 1.0
    np.testing.assert_allclose(g1.gwidth, 1.0 * (2 * np.sqrt(2 * np.log(2))))