Пример #1
0
def test_get_scaling_parameter(is_binned, non_uniform, dim):
    shape = [10 + i for i in range(dim)]
    signal = Signal1D(np.arange(np.prod(shape)).reshape(shape[::-1]))
    axis = signal.axes_manager.signal_axes[0]
    axis.is_binned = is_binned
    axis.scale = 0.5
    if non_uniform:
        axis.convert_to_non_uniform_axis()
    centre = np.ones(shape[::-2])
    scaling_factor = _get_scaling_factor(signal, axis, centre)

    if is_binned:
        assert np.all(scaling_factor == 0.5)
    else:
        assert scaling_factor == 1
Пример #2
0
    def estimate_parameters(self, signal, x1, x2, only_current=False):
        """Estimate the Donach by calculating the median (centre) and the
        variance parameter (sigma).

        Note that an insufficient range will affect the accuracy of this
        method and that this method doesn't estimate the asymmetry parameter
        (alpha).

        Parameters
        ----------
        signal : Signal1D instance
        x1 : float
            Defines the left limit of the spectral range to use for the
            estimation.
        x2 : float
            Defines the right limit of the spectral range to use for the
            estimation.

        only_current : bool
            If False estimates the parameters for the full dataset.

        Returns
        -------
        bool
            Returns True when the parameters estimation is successful

        Examples
        --------

        >>> g = hs.model.components1D.Lorentzian()
        >>> x = np.arange(-10, 10, 0.01)
        >>> data = np.zeros((32, 32, 2000))
        >>> data[:] = g.function(x).reshape((1, 1, 2000))
        >>> s = hs.signals.Signal1D(data)
        >>> s.axes_manager[-1].offset = -10
        >>> s.axes_manager[-1].scale = 0.01
        >>> g.estimate_parameters(s, -10, 10, False)
        """

        super()._estimate_parameters(signal)
        axis = signal.axes_manager.signal_axes[0]
        centre, height, sigma = _estimate_gaussian_parameters(
            signal, x1, x2, only_current)
        scaling_factor = _get_scaling_factor(signal, axis, centre)

        if only_current is True:
            self.centre.value = centre
            self.sigma.value = sigma
            self.A.value = height * 1.3
            if is_binned(signal):
                # in v2 replace by
                #if axis.is_binned:
                self.A.value /= scaling_factor
            return True
        else:
            if self.A.map is None:
                self._create_arrays()
            self.A.map['values'][:] = height * 1.3
            if is_binned(signal):
                # in v2 replace by
                #if axis.is_binned:
                self.A.map['values'][:] /= scaling_factor
            self.A.map['is_set'][:] = True
            self.sigma.map['values'][:] = sigma
            self.sigma.map['is_set'][:] = True
            self.centre.map['values'][:] = centre
            self.centre.map['is_set'][:] = True
            self.fetch_stored_values()
            return True
Пример #3
0
    def estimate_parameters(self, signal, x1, x2, only_current=False):
        """Estimate the Lorentzian by calculating the median (centre) and half
        the interquartile range (gamma).

        Note that an insufficient range will affect the accuracy of this
        method.

        Parameters
        ----------
        signal : Signal1D instance
        x1 : float
            Defines the left limit of the spectral range to use for the
            estimation.
        x2 : float
            Defines the right limit of the spectral range to use for the
            estimation.

        only_current : bool
            If False estimates the parameters for the full dataset.

        Returns
        -------
        bool

        Notes
        -----
        Adapted from gaussian.py and
        https://en.wikipedia.org/wiki/Cauchy_distribution

        Examples
        --------

        >>> g = hs.model.components1D.Lorentzian()
        >>> x = np.arange(-10, 10, 0.01)
        >>> data = np.zeros((32, 32, 2000))
        >>> data[:] = g.function(x).reshape((1, 1, 2000))
        >>> s = hs.signals.Signal1D(data)
        >>> s.axes_manager[-1].offset = -10
        >>> s.axes_manager[-1].scale = 0.01
        >>> g.estimate_parameters(s, -10, 10, False)
        """

        super()._estimate_parameters(signal)
        axis = signal.axes_manager.signal_axes[0]
        centre, height, gamma = _estimate_lorentzian_parameters(signal, x1, x2,
                                                              only_current)
        scaling_factor = _get_scaling_factor(signal, axis, centre)

        if only_current is True:
            self.centre.value = centre
            self.gamma.value = gamma
            self.A.value = height * gamma * np.pi
            if is_binned(signal):
            # in v2 replace by
            #if axis.is_binned:
                self.A.value /= scaling_factor
            return True
        else:
            if self.A.map is None:
                self._create_arrays()
            self.A.map['values'][:] = height * gamma * np.pi
            if is_binned(signal):
            # in v2 replace by
            #if axis.is_binned:
                self.A.map['values'] /= scaling_factor
            self.A.map['is_set'][:] = True
            self.gamma.map['values'][:] = gamma
            self.gamma.map['is_set'][:] = True
            self.centre.map['values'][:] = centre
            self.centre.map['is_set'][:] = True
            self.fetch_stored_values()
            return True
Пример #4
0
    def estimate_parameters(self, signal, x1, x2, only_current=False):
        """Estimate the gaussian by calculating the momenta.

        Parameters
        ----------
        signal : Signal1D instance
        x1 : float
            Defines the left limit of the spectral range to use for the
            estimation.
        x2 : float
            Defines the right limit of the spectral range to use for the
            estimation.
        only_current : bool
            If False estimates the parameters for the full dataset.

        Returns
        -------
        bool

        Notes
        -----
        Adapted from http://www.scipy.org/Cookbook/FittingData

        Examples
        --------

        >>> g = hs.model.components1D.GaussianHF()
        >>> x = np.arange(-10, 10, 0.01)
        >>> data = np.zeros((32, 32, 2000))
        >>> data[:] = g.function(x).reshape((1, 1, 2000))
        >>> s = hs.signals.Signal1D(data)
        >>> s.axes_manager[-1].offset = -10
        >>> s.axes_manager[-1].scale = 0.01
        >>> g.estimate_parameters(s, -10, 10, False)
        """

        super()._estimate_parameters(signal)
        axis = signal.axes_manager.signal_axes[0]
        centre, height, sigma = _estimate_gaussian_parameters(
            signal, x1, x2, only_current)
        scaling_factor = _get_scaling_factor(signal, axis, centre)

        if only_current is True:
            self.centre.value = centre
            self.fwhm.value = sigma * sigma2fwhm
            self.height.value = float(height)
            if is_binned(signal):
                # in v2 replace by
                #if axis.is_binned:
                self.height.value /= scaling_factor
            return True
        else:
            if self.height.map is None:
                self._create_arrays()
            self.height.map['values'][:] = height
            if is_binned(signal):
                # in v2 replace by
                #if axis.is_binned:
                self.height.map['values'][:] /= scaling_factor
            self.height.map['is_set'][:] = True
            self.fwhm.map['values'][:] = sigma * sigma2fwhm
            self.fwhm.map['is_set'][:] = True
            self.centre.map['values'][:] = centre
            self.centre.map['is_set'][:] = True
            self.fetch_stored_values()
            return True
Пример #5
0
    def estimate_parameters(self, signal, x1, x2, only_current=False):
        """Estimate the skew normal distribution by calculating the momenta.

        Parameters
        ----------
        signal : Signal1D instance
        x1 : float
            Defines the left limit of the spectral range to use for the
            estimation.
        x2 : float
            Defines the right limit of the spectral range to use for the
            estimation.

        only_current : bool
            If False estimates the parameters for the full dataset.

        Returns
        -------
        bool

        Notes
        -----
        Adapted from Lin, Lee and Yen, Statistica Sinica 17, 909-927 (2007)
        https://www.jstor.org/stable/24307705

        Examples
        --------

        >>> g = hs.model.components1D.SkewNormal()
        >>> x = np.arange(-10, 10, 0.01)
        >>> data = np.zeros((32, 32, 2000))
        >>> data[:] = g.function(x).reshape((1, 1, 2000))
        >>> s = hs.signals.Signal1D(data)
        >>> s.axes_manager._axes[-1].offset = -10
        >>> s.axes_manager._axes[-1].scale = 0.01
        >>> g.estimate_parameters(s, -10, 10, False)
        """

        super()._estimate_parameters(signal)
        axis = signal.axes_manager.signal_axes[0]
        x0, height, scale, shape = _estimate_skewnormal_parameters(
            signal, x1, x2, only_current
            )
        scaling_factor = _get_scaling_factor(signal, axis, x0)

        if only_current is True:
            self.x0.value = x0
            self.A.value = height * sqrt2pi
            self.scale.value = scale
            self.shape.value = shape
            if is_binned(signal):
            # in v2 replace by
            #if axis.is_binned:
                self.A.value /= scaling_factor
            return True
        else:
            if self.A.map is None:
                self._create_arrays()
            self.A.map['values'][:] = height * sqrt2pi

            if is_binned(signal):
            # in v2 replace by
            #if axis.is_binned:
                self.A.map['values'] /= scaling_factor
            self.A.map['is_set'][:] = True
            self.x0.map['values'][:] = x0
            self.x0.map['is_set'][:] = True
            self.scale.map['values'][:] = scale
            self.scale.map['is_set'][:] = True
            self.shape.map['values'][:] = shape
            self.shape.map['is_set'][:] = True
            self.fetch_stored_values()
            return True