Ejemplo n.º 1
0
    def align_zero_loss_peak(
            self,
            calibrate=True,
            also_align=[],
            print_stats=True,
            subpixel=True,
            mask=None,
            **kwargs):
        """Align the zero-loss peak.

        This function first aligns the spectra using the result of
        `estimate_zero_loss_peak_centre` and afterward, if subpixel is True,
        proceeds to align with subpixel accuracy using `align1D`. The offset
        is automatically correct if `calibrate` is True.

        Parameters
        ----------
        calibrate : bool
            If True, set the offset of the spectral axis so that the
            zero-loss peak is at position zero.
        also_align : list of signals
            A list containing other spectra of identical dimensions to
            align using the shifts applied to the current spectrum.
            If `calibrate` is True, the calibration is also applied to
            the spectra in the list.
        print_stats : bool
            If True, print summary statistics the ZLP maximum before
            the aligment.
        subpixel : bool
            If True, perform the alignment with subpixel accuracy
            using cross-correlation.
        mask : Signal of bool data type.
            It must have signal_dimension = 0 and navigation_shape equal to the
            current signal. Where mask is True the shift is not computed
            and set to nan.

        See Also
        --------
        estimate_zero_loss_peak_centre, align1D, estimate_shift1D.

        Notes
        -----
        Any extra keyword arguments are passed to `align1D`. For
        more information read its docstring.

        """
        def substract_from_offset(value, signals):
            for signal in signals:
                signal.axes_manager[-1].offset -= value

        zlpc = self.estimate_zero_loss_peak_centre(mask=mask)
        mean_ = without_nans(zlpc.data).mean()
        if print_stats is True:
            print
            print(underline("Initial ZLP position statistics"))
            zlpc.print_summary_statistics()

        for signal in also_align + [self]:
            signal.shift1D(-zlpc.data + mean_)

        if calibrate is True:
            zlpc = self.estimate_zero_loss_peak_centre(mask=mask)
            substract_from_offset(without_nans(zlpc.data).mean(),
                                  also_align + [self])

        if subpixel is False:
            return
        left, right = -3., 3.
        if calibrate is False:
            mean_ = without_nans(self.estimate_zero_loss_peak_centre(
                mask=mask).data).mean()
            left += mean_
            right += mean_

        left = (left if left > self.axes_manager[-1].axis[0]
                else self.axes_manager[-1].axis[0])
        right = (right if right < self.axes_manager[-1].axis[-1]
                 else self.axes_manager[-1].axis[-1])
        self.align1D(left, right, also_align=also_align, **kwargs)
        zlpc = self.estimate_zero_loss_peak_centre(mask=mask)
        if calibrate is True:
            substract_from_offset(without_nans(zlpc.data).mean(),
                                  also_align + [self])
Ejemplo n.º 2
0
    def align_zero_loss_peak(self,
                             calibrate=True,
                             also_align=[],
                             print_stats=True,
                             subpixel=True,
                             mask=None,
                             **kwargs):
        """Align the zero-loss peak.

        This function first aligns the spectra using the result of
        `estimate_zero_loss_peak_centre` and afterward, if subpixel is True,
        proceeds to align with subpixel accuracy using `align1D`. The offset
        is automatically correct if `calibrate` is True.

        Parameters
        ----------
        calibrate : bool
            If True, set the offset of the spectral axis so that the
            zero-loss peak is at position zero.
        also_align : list of signals
            A list containing other spectra of identical dimensions to
            align using the shifts applied to the current spectrum.
            If `calibrate` is True, the calibration is also applied to
            the spectra in the list.
        print_stats : bool
            If True, print summary statistics the ZLP maximum before
            the aligment.
        subpixel : bool
            If True, perform the alignment with subpixel accuracy
            using cross-correlation.
        mask : Signal of bool data type.
            It must have signal_dimension = 0 and navigation_shape equal to the
            current signal. Where mask is True the shift is not computed
            and set to nan.

        See Also
        --------
        estimate_zero_loss_peak_centre, align1D, estimate_shift1D.

        Notes
        -----
        Any extra keyword arguments are passed to `align1D`. For
        more information read its docstring.

        """
        def substract_from_offset(value, signals):
            for signal in signals:
                signal.axes_manager[-1].offset -= value

        zlpc = self.estimate_zero_loss_peak_centre(mask=mask)
        mean_ = without_nans(zlpc.data).mean()
        if print_stats is True:
            print
            print(underline("Initial ZLP position statistics"))
            zlpc.print_summary_statistics()

        for signal in also_align + [self]:
            signal.shift1D(-zlpc.data + mean_)

        if calibrate is True:
            zlpc = self.estimate_zero_loss_peak_centre(mask=mask)
            substract_from_offset(
                without_nans(zlpc.data).mean(), also_align + [self])

        if subpixel is False:
            return
        left, right = -3., 3.
        if calibrate is False:
            mean_ = without_nans(
                self.estimate_zero_loss_peak_centre(mask=mask).data).mean()
            left += mean_
            right += mean_

        left = (left if left > self.axes_manager[-1].axis[0] else
                self.axes_manager[-1].axis[0])
        right = (right if right < self.axes_manager[-1].axis[-1] else
                 self.axes_manager[-1].axis[-1])
        self.align1D(left, right, also_align=also_align, **kwargs)
        zlpc = self.estimate_zero_loss_peak_centre(mask=mask)
        if calibrate is True:
            substract_from_offset(
                without_nans(zlpc.data).mean(), also_align + [self])