def find(self, back=False):
     if ((self.index == len(self.coordinates) - 1 and back is False)
     or (back is True and self.index == 0)):
         messages.information('End of dataset reached')
         return
     if self.interpolated_line is not None:
         self.interpolated_line.close()
         self.interpolated_line = None
         self.reset_span_selector()
     
     if self.detect_spike() is False:
         if back is False:
             self.index += 1
         else:
             self.index -= 1
         self.find(back=back)
     else:
         minimum = max(0,self.argmax - 50)
         maximum = min(len(self.signal()) - 1, self.argmax + 50)
         self.ax.set_xlim(
             self.signal.axes_manager.signal_axes[0].index2value(
                 minimum),
             self.signal.axes_manager.signal_axes[0].index2value(
                 maximum))
         self.update_plot()
         self.create_interpolation_line()
    def find(self, back=False):
        self._reset_line()
        ncoordinates = len(self.coordinates)
        spike = self.detect_spike()
        while not spike and (
                (self.index < ncoordinates - 1 and back is False) or
                (self.index > 0 and back is True)):
            if back is False:
                self.index += 1
            else:
                self.index -= 1
            spike = self.detect_spike()

        if spike is False:
            messages.information('End of dataset reached')
            self.index = 0
            self._reset_line()
            return
        else:
            minimum = max(0, self.argmax - 50)
            maximum = min(len(self.signal()) - 1, self.argmax + 50)
            thresh_label = DerivativeTextParameters(
                text="$\mathsf{\delta}_\mathsf{max}=$",
                color="black")
            self.ax.legend([thresh_label], [repr(int(self.derivmax))],
                           handler_map={DerivativeTextParameters:
                                        DerivativeTextHandler()},
                           loc='best')
            self.ax.set_xlim(
                self.signal.axes_manager.signal_axes[0].index2value(
                    minimum),
                self.signal.axes_manager.signal_axes[0].index2value(
                    maximum))
            self.update_plot()
            self.create_interpolation_line()
Exemple #3
0
 def _right_value_changed(self, old, new):
     if self.span_selector.range is None:
         messages.information('Please select a range in the spectrum figure'
                              'by dragging the mouse over it')
         return
     else:
         self._update_calibration()
    def find(self, back=False):
        self._reset_line()
        ncoordinates = len(self.coordinates)
        spike = self.detect_spike()
        while not spike and (
                (self.index < ncoordinates -1 and back is False) or 
                (self.index > 0 and back is True)):
            if back is False:
                self.index += 1
            else:
                self.index -= 1
            spike = self.detect_spike()

        if spike is False:
            messages.information('End of dataset reached')
            self.index = 0
            self._reset_line()
            return
        else:
            minimum = max(0,self.argmax - 50)
            maximum = min(len(self.signal()) - 1, self.argmax + 50)
            self.ax.set_xlim(
                self.signal.axes_manager.signal_axes[0].index2value(
                    minimum),
                self.signal.axes_manager.signal_axes[0].index2value(
                    maximum))
            self.update_plot()
            self.create_interpolation_line()
Exemple #5
0
    def find(self, back=False):
        self._reset_line()
        ncoordinates = len(self.coordinates)
        spike = self.detect_spike()
        while not spike and ((self.index < ncoordinates - 1 and back is False)
                             or (self.index > 0 and back is True)):
            if back is False:
                self.index += 1
            else:
                self.index -= 1
            spike = self.detect_spike()

        if spike is False:
            messages.information('End of dataset reached')
            self.index = 0
            self._reset_line()
            return
        else:
            minimum = max(0, self.argmax - 50)
            maximum = min(len(self.signal()) - 1, self.argmax + 50)
            thresh_label = DerivativeTextParameters(
                text="$\mathsf{\delta}_\mathsf{max}=$", color="black")
            self.ax.legend([thresh_label], [repr(int(self.derivmax))],
                           handler_map={
                               DerivativeTextParameters:
                               DerivativeTextHandler()
                           },
                           loc='best')
            self.ax.set_xlim(
                self.signal.axes_manager.signal_axes[0].index2value(minimum),
                self.signal.axes_manager.signal_axes[0].index2value(maximum))
            self.update_plot()
            self.create_interpolation_line()
    def find(self, back=False):
        self._reset_line()
        ncoordinates = len(self.coordinates)
        spike = self.detect_spike()
        while not spike and ((self.index < ncoordinates - 1 and back is False)
                             or (self.index > 0 and back is True)):
            if back is False:
                self.index += 1
            else:
                self.index -= 1
            spike = self.detect_spike()

        if spike is False:
            messages.information('End of dataset reached')
            self.index = 0
            self._reset_line()
            return
        else:
            minimum = max(0, self.argmax - 50)
            maximum = min(len(self.signal()) - 1, self.argmax + 50)
            self.ax.set_xlim(
                self.signal.axes_manager.signal_axes[0].index2value(minimum),
                self.signal.axes_manager.signal_axes[0].index2value(maximum))
            self.update_plot()
            self.create_interpolation_line()
Exemple #7
0
 def find(self, back=False):
     if ((self.index == len(self.coordinates) - 1 and back is False)
     or (back is True and self.index == 0)):
         messages.information('End of dataset reached')
         return
     if self.interpolated_line is not None:
         self.interpolated_line.close()
         self.interpolated_line = None
         self.reset_span_selector()
     
     if self.detect_spike() is False:
         if back is False:
             self.index += 1
         else:
             self.index -= 1
         self.find(back=back)
     else:
         minimum = max(0,self.argmax - 50)
         maximum = min(len(self.signal()) - 1, self.argmax + 50)
         self.ax.set_xlim(
             self.signal.axes_manager.signal_axes[0].index2value(
                 minimum),
             self.signal.axes_manager.signal_axes[0].index2value(
                 maximum))
         self.update_plot()
         self.create_interpolation_line()
Exemple #8
0
 def _right_value_changed(self, old, new):
     if self.span_selector.range is None:
         messages.information('Please select a range in the spectrum figure' 
         'by dragging the mouse over it')
         return
     else:
         self._update_calibration()
Exemple #9
0
 def _are_microscope_parameters_missing(self):
     """Check if the EELS parameters necessary to calculate the GOS
     are defined in metadata. If not, in interactive mode
     raises an UI item to fill the values"""
     must_exist = (
         'Acquisition_instrument.TEM.convergence_angle',
         'Acquisition_instrument.TEM.beam_energy',
         'Acquisition_instrument.TEM.Detector.EELS.collection_angle',
     )
     missing_parameters = []
     for item in must_exist:
         exists = self.metadata.has_item(item)
         if exists is False:
             missing_parameters.append(item)
     if missing_parameters:
         if preferences.General.interactive is True:
             par_str = "The following parameters are missing:\n"
             for par in missing_parameters:
                 par_str += '%s\n' % par
             par_str += 'Please set them in the following wizard'
             is_ok = messagesui.information(par_str)
             if is_ok:
                 self._set_microscope_parameters()
             else:
                 return True
         else:
             return True
     else:
         return False
Exemple #10
0
    def _are_microscope_parameters_missing(self):
        """Check if the EDS parameters necessary for quantification
        are defined in metadata. Raise in interactive mode
         an UI item to fill or cahnge the values"""
        must_exist = ("Acquisition_instrument.TEM.beam_energy", "Acquisition_instrument.TEM.Detector.EDS.live_time")

        missing_parameters = []
        for item in must_exist:
            exists = self.metadata.has_item(item)
            if exists is False:
                missing_parameters.append(item)
        if missing_parameters:
            if preferences.General.interactive is True:
                par_str = "The following parameters are missing:\n"
                for par in missing_parameters:
                    par_str += "%s\n" % par
                par_str += "Please set them in the following wizard"
                is_ok = messagesui.information(par_str)
                if is_ok:
                    self._set_microscope_parameters()
                else:
                    return True
            else:
                return True
        else:
            return False
Exemple #11
0
    def _are_microscope_parameters_missing(self):
        """Check if the EDS parameters necessary for quantification
        are defined in metadata. Raise in interactive mode
         an UI item to fill or cahnge the values"""
        must_exist = (
            'Acquisition_instrument.TEM.beam_energy',
            'Acquisition_instrument.TEM.Detector.EDS.live_time',
        )

        missing_parameters = []
        for item in must_exist:
            exists = self.metadata.has_item(item)
            if exists is False:
                missing_parameters.append(item)
        if missing_parameters:
            if preferences.General.interactive is True:
                par_str = "The following parameters are missing:\n"
                for par in missing_parameters:
                    par_str += '%s\n' % par
                par_str += 'Please set them in the following wizard'
                is_ok = messagesui.information(par_str)
                if is_ok:
                    self._set_microscope_parameters()
                else:
                    return True
            else:
                return True
        else:
            return False
Exemple #12
0
    def _are_microscope_parameters_missing(self):
        """Check if the EDS parameters necessary for quantification
        are defined in mapped_parameters. If not, in interactive mode 
        raises an UI item to fill the values
        
        """
        import hyperspy.gui.messages as messagesui
        must_exist = (
            'SEM.beam_energy',
            'SEM.EDS.live_time',
        )

        missing_parameters = []
        for item in must_exist:
            exists = self.mapped_parameters.has_item(item)
            if exists is False:
                missing_parameters.append(item)
        if missing_parameters:
            if preferences.General.interactive is True:
                par_str = "The following parameters are missing:\n"
                for par in missing_parameters:
                    par_str += '%s\n' % par
                par_str += 'Please set them in the following wizard'
                is_ok = messagesui.information(par_str)
                if is_ok:
                    self._set_microscope_parameters()
                else:
                    return True
            else:
                return True
        else:
            return False
Exemple #13
0
    def _are_microscope_parameters_missing(self):
        """Check if the EDS parameters necessary for quantification
        are defined in metadata. If not, in interactive mode
        raises an UI item to fill the values

        """
        import hyperspy.gui.messages as messagesui
        must_exist = (
            'Acquisition_instrument.SEM.beam_energy',
            'Acquisition_instrument.SEM.Detector.EDS.live_time', )

        missing_parameters = []
        for item in must_exist:
            exists = self.metadata.has_item(item)
            if exists is False:
                missing_parameters.append(item)
        if missing_parameters:
            if preferences.General.interactive is True:
                par_str = "The following parameters are missing:\n"
                for par in missing_parameters:
                    par_str += '%s\n' % par
                par_str += 'Please set them in the following wizard'
                is_ok = messagesui.information(par_str)
                if is_ok:
                    self._set_microscope_parameters()
                else:
                    return True
            else:
                return True
        else:
            return False
Exemple #14
0
 def _are_microscope_parameters_missing(self):
     """Check if the EELS parameters necessary to calculate the GOS
     are defined in metadata. If not, in interactive mode
     raises an UI item to fill the values"""
     must_exist = (
         'Acquisition_instrument.TEM.convergence_angle',
         'Acquisition_instrument.TEM.beam_energy',
         'Acquisition_instrument.TEM.Detector.EELS.collection_angle',)
     missing_parameters = []
     for item in must_exist:
         exists = self.metadata.has_item(item)
         if exists is False:
             missing_parameters.append(item)
     if missing_parameters:
         if preferences.General.interactive is True:
             par_str = "The following parameters are missing:\n"
             for par in missing_parameters:
                 par_str += '%s\n' % par
             par_str += 'Please set them in the following wizard'
             is_ok = messagesui.information(par_str)
             if is_ok:
                 self._set_microscope_parameters()
             else:
                 return True
         else:
             return True
     else:
         return False
Exemple #15
0
 def check_eels_parameters(self):
     must_exist = ("TEM.convergence_angle", "TEM.beam_energy", "TEM.EELS.collection_angle")
     missing_parameters = []
     for item in must_exist:
         exists = self.spectrum.mapped_parameters.has_item(item)
         if exists is False:
             missing_parameters.append(item)
     if missing_parameters:
         if preferences.General.interactive is True:
             par_str = "The following parameters are missing:\n"
             for par in missing_parameters:
                 par_str += "%s\n" % par
             par_str += "Please set them in the following wizard"
             is_ok = messagesui.information(par_str)
             if is_ok:
                 self.define_eels_parameters()
             else:
                 raise MissingParametersError(missing_parameters)
         else:
             raise MissingParametersError(missing_parameters)
Exemple #16
0
def overwrite(fname):
    """ If file exists 'fname', ask for overwriting and return True or False,
    else return True.
    
    """
    if os.path.isfile(fname):
        message = "Overwrite '%s' (y/n)?\n" % fname
        try:
            answer = raw_input(message)
            answer = answer.lower()
            while (answer != 'y') and (answer != 'n'):
                print('Please answer y or n.')
                answer = raw_input(message)
            if answer.lower() == 'y':
                return True
            elif answer.lower() == 'n':
                # print('Operation canceled.')
                return False
        except:
            # We are running in the IPython notebook that does not
            # support raw_input
            return information(message)
    else:
        return True
Exemple #17
0
def estimate_variance_parameters(
        noisy_signal,
        clean_signal,
        mask=None,
        pol_order=1,
        higher_than=None,
        return_results=False,
        plot_results=True,
        weighted=False):
    """Find the scale and offset of the Poissonian noise

    By comparing an SI with its denoised version (i.e. by PCA),
    this plots an
    estimation of the variance as a function of the number of counts
    and fits a
    polynomy to the result.

    Parameters
    ----------
    noisy_SI, clean_SI : spectrum.Spectrum instances
    mask : numpy bool array
        To define the channels that will be used in the calculation.
    pol_order : int
        The order of the polynomy.
    higher_than: float
        To restrict the fit to counts over the given value.

    return_results : Bool

    plot_results : Bool

    Returns
    -------
    Dictionary with the result of a linear fit to estimate the offset
    and scale factor

    """
    fold_back_noisy = unfold_if_multidim(noisy_signal)
    fold_back_clean = unfold_if_multidim(clean_signal)

    # The rest of the code assumes that the first data axis
    # is the navigation axis. We transpose the data if that is not the
    # case.
    ns = (noisy_signal.data.copy()
          if noisy_signal.axes_manager[0].index_in_array == 0
          else noisy_signal.data.T.copy())
    cs = (clean_signal.data.copy()
          if clean_signal.axes_manager[0].index_in_array == 0
          else clean_signal.data.T.copy())

    if mask is not None:
        _slice = [slice(None), ] * len(ns.shape)
        _slice[noisy_signal.axes_manager.signal_axes[0].index_in_array]\
            = ~mask
        ns = ns[_slice]
        cs = cs[_slice]

    results0 = _estimate_gain(ns, cs, weighted=weighted,
                              higher_than=higher_than, plot_results=plot_results, binning=0,
                              pol_order=pol_order)

    results2 = _estimate_gain(ns, cs, weighted=weighted,
                              higher_than=higher_than, plot_results=False, binning=2,
                              pol_order=pol_order)

    c = _estimate_correlation_factor(results0['fit'][0],
                                     results2['fit'][0], 4)

    message = ("Gain factor: %.2f\n" % results0['fit'][0] +
               "Gain offset: %.2f\n" % results0['fit'][1] +
               "Correlation factor: %.2f\n" % c)
    is_ok = True
    if hyperspy.defaults_parser.preferences.General.interactive is True:
        is_ok = messagesui.information(
            message + "Would you like to store the results?")
    else:
        print message
    if is_ok:
        noisy_signal.metadata.set_item("Signal.Noise_properties.Variance_linear_model.gain_factor",
                                       results0['fit'][0])
        noisy_signal.metadata.set_item("Signal.Noise_properties.Variance_linear_model.gain_offset",
                                       results0['fit'][1])
        noisy_signal.metadata.set_item("Signal.Noise_properties.Variance_linear_model.correlation_factor",
                                       c)
        noisy_signal.metadata.set_item("Signal.Noise_properties.Variance_linear_model.parameters_estimation_method",
                                       'HyperSpy')

    if fold_back_noisy is True:
        noisy_signal.fold()
    if fold_back_clean is True:
        clean_signal.fold()

    if return_results is True:
        return results0
Exemple #18
0
def estimate_variance_parameters(noisy_signal,
                                 clean_signal,
                                 mask=None,
                                 pol_order=1,
                                 higher_than=None,
                                 return_results=False,
                                 plot_results=True,
                                 weighted=False):
    """Find the scale and offset of the Poissonian noise

    By comparing an SI with its denoised version (i.e. by PCA),
    this plots an
    estimation of the variance as a function of the number of counts
    and fits a
    polynomy to the result.

    Parameters
    ----------
    noisy_SI, clean_SI : spectrum.Spectrum instances
    mask : numpy bool array
        To define the channels that will be used in the calculation.
    pol_order : int
        The order of the polynomy.
    higher_than: float
        To restrict the fit to counts over the given value.

    return_results : Bool

    plot_results : Bool

    Returns
    -------
    Dictionary with the result of a linear fit to estimate the offset
    and scale factor

    """
    fold_back_noisy = unfold_if_multidim(noisy_signal)
    fold_back_clean = unfold_if_multidim(clean_signal)

    # The rest of the code assumes that the first data axis
    # is the navigation axis. We transpose the data if that is not the
    # case.
    ns = (noisy_signal.data.copy()
          if noisy_signal.axes_manager[0].index_in_array == 0 else
          noisy_signal.data.T.copy())
    cs = (clean_signal.data.copy()
          if clean_signal.axes_manager[0].index_in_array == 0 else
          clean_signal.data.T.copy())

    if mask is not None:
        _slice = [
            slice(None),
        ] * len(ns.shape)
        _slice[noisy_signal.axes_manager.signal_axes[0].index_in_array]\
            = ~mask
        ns = ns[_slice]
        cs = cs[_slice]

    results0 = _estimate_gain(ns,
                              cs,
                              weighted=weighted,
                              higher_than=higher_than,
                              plot_results=plot_results,
                              binning=0,
                              pol_order=pol_order)

    results2 = _estimate_gain(ns,
                              cs,
                              weighted=weighted,
                              higher_than=higher_than,
                              plot_results=False,
                              binning=2,
                              pol_order=pol_order)

    c = _estimate_correlation_factor(results0['fit'][0], results2['fit'][0], 4)

    message = ("Gain factor: %.2f\n" % results0['fit'][0] +
               "Gain offset: %.2f\n" % results0['fit'][1] +
               "Correlation factor: %.2f\n" % c)
    is_ok = True
    if hyperspy.defaults_parser.preferences.General.interactive is True:
        is_ok = messagesui.information(message +
                                       "Would you like to store the results?")
    else:
        print message
    if is_ok:
        noisy_signal.metadata.set_item(
            "Signal.Noise_properties.Variance_linear_model.gain_factor",
            results0['fit'][0])
        noisy_signal.metadata.set_item(
            "Signal.Noise_properties.Variance_linear_model.gain_offset",
            results0['fit'][1])
        noisy_signal.metadata.set_item(
            "Signal.Noise_properties.Variance_linear_model.correlation_factor",
            c)
        noisy_signal.metadata.set_item(
            "Signal.Noise_properties.Variance_linear_model.parameters_estimation_method",
            'HyperSpy')

    if fold_back_noisy is True:
        noisy_signal.fold()
    if fold_back_clean is True:
        clean_signal.fold()

    if return_results is True:
        return results0
Exemple #19
0
def estimate_variance_parameters(noisy_signal, clean_signal, mask = None,
    pol_order = 1, higher_than = None, return_results = False,
    plot_results = True, weighted = False):
    """Find the scale and offset of the Poissonian noise

    By comparing an SI with its denoised version (i.e. by PCA), this plots an
    estimation of the variance as a function of the number of counts and fits a
    polynomy to the result.

    Parameters
    ----------
    noisy_SI, clean_SI : Spectrum instances
    mask : numpy bool array
        To define the channels that will be used in the calculation.
    pol_order : int
        The order of the polynomy.
    higher_than: float
        To restrict the fit to counts over the given value.
        
    return_results : Bool
    
    plot_results : Bool

    Returns
    -------
    Dictionary with the result of a linear fit to estimate the offset and
    scale factor
    """
    fold_back_noisy =  unfold_if_multidim(noisy_signal)
    fold_back_clean =  unfold_if_multidim(clean_signal)
    ns = noisy_signal.data.copy()
    cs = clean_signal.data.copy()

    if mask is not None:
        ns = ns[mask]
        cs = cs[mask]

    results0 = _estimate_gain(ns, cs, weighted = weighted, 
        higher_than = higher_than, plot_results = plot_results, binning = 0,
        pol_order = pol_order)
        
    results2 = _estimate_gain(ns, cs, weighted = weighted, 
        higher_than = higher_than, plot_results = False, binning = 2,
        pol_order = pol_order)
        
    c = _estimate_correlation_factor(results0['fit'][0], results2['fit'][0],
        4)
    
    message = ("Gain factor: %.2f\n" % results0['fit'][0] +
               "Gain offset: %.2f\n" % results0['fit'][1] +
               "Correlation factor: %.2f\n" % c )
    is_ok = True
    if hyperspy.defaults_parser.preferences.General.interactive is True:
        is_ok = messagesui.information(message +
                                       "Would you like to store the results?")
    else:
        print message
    if is_ok:
        if not noisy_signal.mapped_parameters.has_item('Variance_estimation'):
            noisy_signal.mapped_parameters.add_node('Variance_estimation')
        noisy_signal.mapped_parameters.Variance_estimation.gain_factor = \
            results0['fit'][0]
        noisy_signal.mapped_parameters.Variance_estimation.gain_offset = \
            results0['fit'][1]
        noisy_signal.mapped_parameters.Variance_estimation.correlation_factor =\
            c
        noisy_signal.mapped_parameters.Variance_estimation.\
        parameters_estimation_method = 'Hyperspy'

    if fold_back_noisy is True:
        noisy_signal.fold()
    if fold_back_clean is True:
        clean_signal.fold()
        
    if return_results is True:
        return results0