Example #1
0
    def fit_background(self,
                       start_energy=None,
                       end_energy=None,
                       windows_sigma=(4., 3.),
                       kind='single',
                       **kwargs):
        """
        Fit the background in the energy range containing no X-ray line.

        After the fit, the background is fixed.

        Parameters
        ----------
        start_energy : {float, None}
            If float, limit the range of energies from the left to the
            given value.
        end_energy : {float, None}
            If float, limit the range of energies from the right to the
            given value.
        windows_sigma: tuple of two float
            The (lower, upper) bounds around each X-ray line, each as a float,
            to define the energy range free of X-ray lines.
        kind : {'single', 'multi'}
            If 'single' fit only the current location. If 'multi'
            use multifit.
        **kwargs : extra key word arguments
            All extra key word arguments are passed to fit or

        See also
        --------
        free_background
        """

        if end_energy is None:
            end_energy = self.end_energy
        if start_energy is None:
            start_energy = self.start_energy

        # disactivate line
        self.free_background()
        with stash_active_state(self):
            self.disable_xray_lines()
            self.set_signal_range(start_energy, end_energy)
            for component in self:
                if component.isbackground is False:
                    self.remove_signal_range(
                        component.centre.value -
                        windows_sigma[0] * component.sigma.value,
                        component.centre.value +
                        windows_sigma[1] * component.sigma.value)
            if kind == 'single':
                self.fit(**kwargs)
            if kind == 'multi':
                self.multifit(**kwargs)
            self.reset_signal_range()
        self.fix_background()
Example #2
0
    def fit_background(self,
                       start_energy=None,
                       end_energy=None,
                       windows_sigma=(4., 3.),
                       kind='single',
                       **kwargs):
        """
        Fit the background in the energy range containing no X-ray line.

        After the fit, the background is fixed.

        Parameters
        ----------
        start_energy : {float, None}
            If float, limit the range of energies from the left to the
            given value.
        end_energy : {float, None}
            If float, limit the range of energies from the right to the
            given value.
        windows_sigma: tuple of two float
            The (lower, upper) bounds around each X-ray line, each as a float,
            to define the energy range free of X-ray lines.
        kind : {'single', 'multi'}
            If 'single' fit only the current location. If 'multi'
            use multifit.
        **kwargs : extra key word arguments
            All extra key word arguments are passed to fit or

        See also
        --------
        free_background
        """

        if end_energy is None:
            end_energy = self.end_energy
        if start_energy is None:
            start_energy = self.start_energy

        # disactivate line
        self.free_background()
        with stash_active_state(self):
            self.disable_xray_lines()
            self.set_signal_range(start_energy, end_energy)
            for component in self:
                if component.isbackground is False:
                    self.remove_signal_range(
                        component.centre.value -
                        windows_sigma[0] * component.sigma.value,
                        component.centre.value +
                        windows_sigma[1] * component.sigma.value)
            if kind == 'single':
                self.fit(**kwargs)
            if kind == 'multi':
                self.multifit(**kwargs)
            self.reset_signal_range()
        self.fix_background()
 def test_stash_array(self):
     g = self.gaussian
     g.active_is_multidimensional = True
     g._create_arrays()
     g._active_array[2, 0] = False
     g._active_array[0, 0] = False
     with stash_active_state([g]):
         g.active_is_multidimensional = False
         assert not g._active_is_multidimensional
         np.testing.assert_array_equal(
             g.A.as_signal('values').data, np.zeros((3, 3)))
         assert g._active_array is None
     assert g._active_is_multidimensional
     np.testing.assert_allclose(
         g._active_array,
         np.array([[0, 1, 1], [1, 1, 1], [0, 1, 1]], dtype=bool))
     assert np.isnan(g.A.as_signal('values').data[[0, 2], [0]]).all()
 def test_stash_array(self):
     g = self.gaussian
     g.active_is_multidimensional = True
     g._create_arrays()
     g._active_array[2, 0] = False
     g._active_array[0, 0] = False
     with stash_active_state([g]):
         g.active_is_multidimensional = False
         assert not g._active_is_multidimensional
         np.testing.assert_array_equal(g.A.as_signal('values').data,
                                       np.zeros((3, 3)))
         assert g._active_array is None
     assert g._active_is_multidimensional
     np.testing.assert_allclose(
         g._active_array, np.array([[0, 1, 1], [1, 1, 1], [0, 1, 1]],
                                   dtype=bool))
     assert np.isnan(g.A.as_signal('values').data[[0, 2], [0]]).all()
 def test_stash_array(self):
     g = self.gaussian
     g.active_is_multidimensional = True
     g._create_arrays()
     g._active_array[2, 0] = False
     g._active_array[0, 0] = False
     with stash_active_state([g]):
         g.active_is_multidimensional = False
         nt.assert_false(g._active_is_multidimensional)
         nt.assert_true(
             np.all(g.A.as_signal('values').data == np.zeros((3, 3))))
         nt.assert_equal(g._active_array, None)
     nt.assert_true(g._active_is_multidimensional)
     np.testing.assert_almost_equal(
         g._active_array,
         np.array([[0, 1, 1], [1, 1, 1], [0, 1, 1]], dtype=bool))
     nt.assert_true(
         np.isnan(g.A.as_signal('values').data[[0, 2], [0]]).all())
 def test_stash_array(self):
     g = self.gaussian
     g.active_is_multidimensional = True
     g._create_arrays()
     g._active_array[2, 0] = False
     g._active_array[0, 0] = False
     with stash_active_state([g]):
         g.active_is_multidimensional = False
         nt.assert_false(g._active_is_multidimensional)
         nt.assert_true(
             np.all(
                 g.A.as_signal('values').data == np.zeros(
                     (3, 3))))
         nt.assert_equal(g._active_array, None)
     nt.assert_true(g._active_is_multidimensional)
     np.testing.assert_almost_equal(
         g._active_array, np.array([[0, 1, 1], [1, 1, 1], [0, 1, 1]],
                                   dtype=bool))
     nt.assert_true(
         np.isnan(g.A.as_signal('values').data[[0, 2], [0]]).all())