hyperspy._signals.eds module

class hyperspy._signals.eds.EDSSpectrum(*args, **kwards)

Bases: Signal1D

General 1D signal class for EDS spectra.

Create a Signal from a numpy array.

Parameters:
  • data (numpy.ndarray) – The signal data. It can be an array of any dimensions.

  • axes ([dict/axes], optional) – List of either dictionaries or axes objects to define the axes (see the documentation of the AxesManager class for more details).

  • attributes (dict, optional) – A dictionary whose items are stored as attributes.

  • metadata (dict, optional) – A dictionary containing a set of parameters that will to stores in the metadata attribute. Some parameters might be mandatory in some cases.

  • original_metadata (dict, optional) – A dictionary containing a set of parameters that will to stores in the original_metadata attribute. It typically contains all the parameters that has been imported from the original data file.

  • ragged (bool or None, optional) – Define whether the signal is ragged or not. Overwrite the ragged value in the attributes dictionary. If None, it does nothing. Default is None.

_add_background_windows_markers(windows_position, render_figure=True)

Plot the background windows associated with each X-ray lines.

For X-ray lines, a black line links the left and right window with the average value in each window.

Parameters:

windows_position (2D array of float) – The position of the windows in energy. Each line corresponds to a X-ray lines. In a line, the two first value corresponds to the limit of the left window and the two last values corresponds to the limit of the right window.

_add_vertical_lines_groups(position, render_figure=True, **kwargs)

Add vertical markers for each group that shares the color.

Parameters:
  • position (2D array of float) – The position on the signal axis. Each row corresponds to a group.

  • kwargs – keywords argument for markers.vertical_line

_get_beam_energy()

Get the beam energy.

The return value is in the same units than the signal axis

_get_line_energy(Xray_line, FWHM_MnKa=None)

Get the line energy and the energy resolution of a Xray line.

The return values are in the same units than the signal axis

Parameters:
  • Xray_line (strings) – Valid element X-ray lines e.g. Fe_Kb

  • FWHM_MnKa ({None, float, 'auto'}) – The energy resolution of the detector in eV if ‘auto’, used the one in ‘self.metadata.Acquisition_instrument.SEM.Detector.EDS.energy_resolution_MnKa’

Returns:

  • float (the line energy, if FWHM_MnKa is None)

  • (float,float) (the line energy and the energy resolution, if FWHM_MnKa)

  • is not None

_get_lines_from_elements(elements, only_one=False, only_lines=('a',))

Returns the X-ray lines of the given elements in spectral range of the data.

Parameters:
  • elements (list of strings) – A list containing the symbol of the chemical elements.

  • only_one (bool) – If False, add all the lines of each element in the data spectral range. If True only add the line at the highest energy above an overvoltage of 2 (< beam energy / 2).

  • only_lines ({None, list of strings}) – If not None, only the given lines will be returned.

Return type:

list of X-ray lines alphabetically sorted

_get_xray_lines_in_spectral_range(xray_lines)

Return the lines in the energy range

Parameters:

xray_lines (List of string) – The xray_lines

Return type:

The list of xray_lines in the energy range

add_elements(elements)

Add elements and the corresponding X-ray lines.

The list of elements is stored in metadata.Sample.elements

Parameters:

elements (list of strings) – The symbol of the elements.

Examples

>>> s = hs.datasets.example_signals.EDS_SEM_Spectrum()
>>> print(s.metadata.Sample.elements)
>>> s.add_elements(['Ar'])
>>> print(s.metadata.Sample.elements)
['Al' 'C' 'Cu' 'Mn' 'Zr']
['Al', 'Ar', 'C', 'Cu', 'Mn', 'Zr']
add_lines(lines=(), only_one=True, only_lines=('a',))

Add X-rays lines to the internal list.

Although most functions do not require an internal list of X-ray lines because they can be calculated from the internal list of elements, ocassionally it might be useful to customize the X-ray lines to be use by all functions by default using this method. The list of X-ray lines is stored in metadata.Sample.xray_lines

Parameters:
  • lines (list of strings) – A list of valid element X-ray lines to add e.g. Fe_Kb. Additionally, if metadata.Sample.elements is defined, add the lines of those elements that where not given in this list. If the list is empty (default), and metadata.Sample.elements is defined, add the lines of all those elements.

  • only_one (bool) – If False, add all the lines of each element in metadata.Sample.elements that has not line defined in lines. If True (default), only add the line at the highest energy above an overvoltage of 2 (< beam energy / 2).

  • only_lines ({None, list of strings}) – If not None, only the given lines will be added.

Examples

>>> s = hs.datasets.example_signals.EDS_SEM_Spectrum()
>>> s.add_lines()
>>> print(s.metadata.Sample.xray_lines)
['Al_Ka', 'C_Ka', 'Cu_La', 'Mn_La', 'Zr_La']
>>> s = hs.datasets.example_signals.EDS_SEM_Spectrum()
>>> s.set_microscope_parameters(beam_energy=30)
>>> s.add_lines()
>>> print(s.metadata.Sample.xray_lines)
['Al_Ka', 'C_Ka', 'Cu_Ka', 'Mn_Ka', 'Zr_La']
>>> s = hs.datasets.example_signals.EDS_SEM_Spectrum()
>>> s.add_lines()
>>> print(s.metadata.Sample.xray_lines)
>>> s.add_lines(['Cu_Ka'])
>>> print(s.metadata.Sample.xray_lines)
['Al_Ka', 'C_Ka', 'Cu_La', 'Mn_La', 'Zr_La']
['Al_Ka', 'C_Ka', 'Cu_Ka', 'Cu_La', 'Mn_La', 'Zr_La']
add_xray_lines_markers(xray_lines, render_figure=True)

Add marker on a spec.plot() with the name of the selected X-ray lines

Parameters:

xray_lines (list of string) – A valid list of X-ray lines

estimate_background_windows(line_width=[2, 2], windows_width=1, xray_lines=None)

Estimate two windows around each X-ray line containing only the background.

Parameters:
  • line_width (list of two floats) – The position of the two windows around the X-ray line is given by the line_width (left and right) times the calculated FWHM of the line.

  • windows_width (float) – The width of the windows is is the windows_width times the calculated FWHM of the line.

  • xray_lines (None or list of string) – If None, use metadata.Sample.elements.xray_lines. Else, provide an iterable containing a list of valid X-ray lines symbols.

Returns:

windows_position – The position of the windows in energy. Each line corresponds to a X-ray line. In a line, the two first values correspond to the limits of the left window and the two last values correspond to the limits of the right window.

Return type:

2D array of float

Examples

>>> s = hs.datasets.example_signals.EDS_TEM_Spectrum()
>>> s.add_lines()
>>> bw = s.estimate_background_windows(line_width=[5.0, 2.0])
>>> s.plot(background_windows=bw)
>>> s.get_lines_intensity(background_windows=bw, plot_result=True)
Fe_Ka at 6.4039 keV : Intensity = 2754.00
Pt_La at 9.4421 keV : Intensity = 15090.00
estimate_integration_windows(windows_width=2.0, xray_lines=None)

Estimate a window of integration for each X-ray line.

Parameters:
  • windows_width (float) – The width of the integration windows is the ‘windows_width’ times the calculated FWHM of the line.

  • xray_lines (None or list of string) – If None, use ‘metadata.Sample.elements.xray_lines’. Else, provide an iterable containing a list of valid X-ray lines symbols.

Returns:

integration_windows – The positions of the windows in energy. Each row corresponds to a X-ray line. Each row contains the left and right value of the window.

Return type:

2D array of float

Examples

>>> s = hs.datasets.example_signals.EDS_TEM_Spectrum()
>>> s.add_lines()
>>> iw = s.estimate_integration_windows()
>>> s.plot(integration_windows=iw)
>>> s.get_lines_intensity(integration_windows=iw, plot_result=True)
Fe_Ka at 6.4039 keV : Intensity = 3710.00
Pt_La at 9.4421 keV : Intensity = 15872.00
get_lines_intensity(xray_lines=None, integration_windows=2.0, background_windows=None, plot_result=False, only_one=True, only_lines=('a',), **kwargs)

Return the intensity map of selected Xray lines.

The intensities, the number of X-ray counts, are computed by suming the spectrum over the different X-ray lines. The sum window width is calculated from the energy resolution of the detector as defined in ‘energy_resolution_MnKa’ of the metadata. Backgrounds average in provided windows can be subtracted from the intensities.

Parameters:
  • xray_lines ({None, Iterable* of strings}) – If None, if metadata.Sample.elements.xray_lines contains a list of lines use those. If metadata.Sample.elements.xray_lines is undefined or empty but metadata.Sample.elements is defined, use the same syntax as add_line to select a subset of lines for the operation. Alternatively, provide an iterable containing a list of valid X-ray lines symbols. * Note that while dictionaries and strings are iterable, their use is ambiguous and specifically not allowed.

  • integration_windows (Float or array) – If float, the width of the integration windows is the ‘integration_windows_width’ times the calculated FWHM of the line. Else provide an array for which each row corresponds to a X-ray line. Each row contains the left and right value of the window.

  • background_windows (None or 2D array of float) – If None, no background subtraction. Else, the backgrounds average in the windows are subtracted from the return intensities. ‘background_windows’ provides the position of the windows in energy. Each line corresponds to a X-ray line. In a line, the two first values correspond to the limits of the left window and the two last values correspond to the limits of the right window.

  • plot_result (bool) – If True, plot the calculated line intensities. If the current object is a single spectrum it prints the result instead.

  • only_one (bool) – If False, use all the lines of each element in the data spectral range. If True use only the line at the highest energy above an overvoltage of 2 (< beam energy / 2).

  • only_lines ({None, list of strings}) – If not None, use only the given lines.

  • kwargs – The extra keyword arguments for plotting. See utils.plot.plot_signals

Returns:

intensities – A list containing the intensities as BaseSignal subclasses.

Return type:

list

Examples

>>> s = hs.datasets.example_signals.EDS_SEM_Spectrum()
>>> s.get_lines_intensity(['Mn_Ka'], plot_result=True)
Mn_La at 0.63316 keV : Intensity = 96700.00
>>> s = hs.datasets.example_signals.EDS_SEM_Spectrum()
>>> s.plot(['Mn_Ka'], integration_windows=2.1)
>>> s.get_lines_intensity(['Mn_Ka'],
>>>                       integration_windows=2.1, plot_result=True)
Mn_Ka at 5.8987 keV : Intensity = 53597.00
>>> s = hs.datasets.example_signals.EDS_SEM_Spectrum()
>>> s.set_elements(['Mn'])
>>> s.set_lines(['Mn_Ka'])
>>> bw = s.estimate_background_windows()
>>> s.plot(background_windows=bw)
>>> s.get_lines_intensity(background_windows=bw, plot_result=True)
Mn_Ka at 5.8987 keV : Intensity = 46716.00
get_take_off_angle()

Calculate the take-off-angle (TOA).

TOA is the angle with which the X-rays leave the surface towards the detector. Parameters are read in ‘SEM.Stage.tilt_alpha’, ‘Acquisition_instrument.SEM.Detector.EDS.azimuth_angle’ and ‘SEM.Detector.EDS.elevation_angle’ and ‘SEM.Stage.tilt_beta in ‘metadata’.

Returns:

take_off_angle – in Degree

Return type:

float

Examples

>>> s = hs.datasets.example_signals.EDS_SEM_Spectrum()
>>> s.get_take_off_angle()
37.0
>>> s.set_microscope_parameters(tilt_stage=20.)
>>> s.get_take_off_angle()
57.0

See also

hs.eds.take_off_angle

plot(xray_lines=False, only_lines=('a', 'b'), only_one=False, background_windows=None, integration_windows=None, navigator='auto', plot_markers=True, autoscale='v', norm='auto', axes_manager=None, navigator_kwds={}, **kwargs)

Plot the EDS spectrum. The following markers can be added

  • The position of the X-ray lines and their names.

  • The background windows associated with each X-ray lines. A black line links the left and right window with the average value in each window.

Parameters:
  • xray_lines ({False, True, 'from_elements', list of string}) – If not False, indicate the position and the name of the X-ray lines. If True, if metadata.Sample.elements.xray_lines contains a list of lines use those. If metadata.Sample.elements.xray_lines is undefined or empty or if xray_lines equals ‘from_elements’ and metadata.Sample.elements is defined, use the same syntax as add_line to select a subset of lines for the operation. Alternatively, provide an iterable containing a list of valid X-ray lines symbols.

  • only_lines (None or list of strings) – If not None, use only the given lines (eg. (‘a’,’Kb’)). If None, use all lines.

  • only_one (bool) – If False, use all the lines of each element in the data spectral range. If True use only the line at the highest energy above an overvoltage of 2 (< beam energy / 2).

  • background_windows (None or 2D array of float) – If not None, add markers at the position of the windows in energy. Each line corresponds to a X-ray lines. In a line, the two first value corresponds to the limit of the left window and the two last values corresponds to the limit of the right window.

  • integration_windows (None or 'auto' or float or 2D array of float) – If not None, add markers at the position of the integration windows. If ‘auto’ (or float), the width of the integration windows is 2.0 (or float) times the calculated FWHM of the line. see ‘estimate_integration_windows’. Else provide an array for which each row corresponds to a X-ray line. Each row contains the left and right value of the window.

  • navigator (str, None, or BaseSignal (or subclass). Allowed string values are 'auto', 'slider', and 'spectrum'.) –

    If 'auto':

    • If navigation_dimension > 0, a navigator is provided to explore the data.

    • If navigation_dimension is 1 and the signal is an image the navigator is a sum spectrum obtained by integrating over the signal axes (the image).

    • If navigation_dimension is 1 and the signal is a spectrum the navigator is an image obtained by stacking all the spectra in the dataset horizontally.

    • If navigation_dimension is > 1, the navigator is a sum image obtained by integrating the data over the signal axes.

    • Additionally, if navigation_dimension > 2, a window with one slider per axis is raised to navigate the data.

    • For example, if the dataset consists of 3 navigation axes X, Y, Z and one signal axis, E, the default navigator will be an image obtained by integrating the data over E at the current Z index and a window with sliders for the X, Y, and Z axes will be raised. Notice that changing the Z-axis index changes the navigator in this case.

    • For lazy signals, the navigator will be calculated using the compute_navigator() method.

    If 'slider':

    • If navigation dimension > 0 a window with one slider per axis is raised to navigate the data.

    If 'spectrum':

    • If navigation_dimension > 0 the navigator is always a spectrum obtained by integrating the data over all other axes.

    • Not supported for lazy signals, the 'auto' option will be used instead.

    If None, no navigator will be provided.

    Alternatively a BaseSignal (or subclass) instance can be provided. The navigation or signal shape must match the navigation shape of the signal to plot or the navigation_shape + signal_shape must be equal to the navigator_shape of the current object (for a dynamic navigator). If the signal dtype is RGB or RGBA this parameter has no effect and the value is always set to 'slider'.

  • axes_manager (None or AxesManager) – If None, the signal’s axes_manager attribute is used.

  • plot_markers (bool, default True) – Plot markers added using s.add_marker(marker, permanent=True). Note, a large number of markers might lead to very slow plotting.

  • navigator_kwds (dict) – Only for image navigator, additional keyword arguments for matplotlib.pyplot.imshow().

  • norm (str, optional) – The function used to normalize the data prior to plotting. Allowable strings are: 'auto', 'linear', 'log'. (default value is 'auto'). If 'auto', intensity is plotted on a linear scale except when power_spectrum=True (only for complex signals).

  • autoscale (str) – The string must contain any combination of the ‘x’ and ‘v’ characters. If ‘x’ or ‘v’ (for values) are in the string, the corresponding horizontal or vertical axis limits are set to their maxima and the axis limits will reset when the data or the navigation indices are changed. Default is ‘v’.

Examples

>>> s = hs.datasets.example_signals.EDS_SEM_Spectrum()
>>> s.plot()
>>> s = hs.datasets.example_signals.EDS_SEM_Spectrum()
>>> s.plot(True)
>>> s = hs.datasets.example_signals.EDS_TEM_Spectrum()
>>> s.add_lines()
>>> bw = s.estimate_background_windows()
>>> s.plot(background_windows=bw)
>>> s = hs.datasets.example_signals.EDS_SEM_Spectrum()
>>> s.plot(['Mn_Ka'], integration_windows='auto')
>>> s = hs.datasets.example_signals.EDS_TEM_Spectrum()
>>> s.add_lines()
>>> bw = s.estimate_background_windows()
>>> s.plot(background_windows=bw, integration_windows=2.1)
rebin(new_shape=None, scale=None, crop=True, dtype=None, out=None)

Rebin the signal into a smaller or larger shape, based on linear interpolation. Specify either new_shape or scale. Scale of 1 means no binning and scale less than one results in up-sampling.

Parameters:
  • new_shape (list (of floats or integer) or None) – For each dimension specify the new_shape. This will internally be converted into a scale parameter.

  • scale (list (of floats or integer) or None) – For each dimension, specify the new:old pixel ratio, e.g. a ratio of 1 is no binning and a ratio of 2 means that each pixel in the new spectrum is twice the size of the pixels in the old spectrum. The length of the list should match the dimension of the Signal’s underlying data array. Note : Only one of `scale` or `new_shape` should be specified, otherwise the function will not run

  • crop (bool) – Whether or not to crop the resulting rebinned data (default is True). When binning by a non-integer number of pixels it is likely that the final row in each dimension will contain fewer than the full quota to fill one pixel. For example, a 5*5 array binned by 2.1 will produce two rows containing 2.1 pixels and one row containing only 0.8 pixels. Selection of crop=True or crop=False determines whether or not this “black” line is cropped from the final binned array or not. Please note that if ``crop=False`` is used, the final row in each dimension may appear black if a fractional number of pixels are left over. It can be removed but has been left to preserve total counts before and after binning.

  • dtype ({None, numpy.dtype, "same"}) – Specify the dtype of the output. If None, the dtype will be determined by the behaviour of numpy.sum(), if “same”, the dtype will be kept the same. Default is None.

  • out (BaseSignal (or subclasses) or None) – If None, a new Signal is created with the result of the operation and returned (default). If a Signal is passed, it is used to receive the output of the operation, and nothing is returned.

Returns:

s – The resulting cropped signal.

Return type:

BaseSignal (or subclass)

Raises:

NotImplementedError – If trying to rebin over a non-uniform axis.

Examples

>>> spectrum = hs.signals.EDSTEMSpectrum(np.ones([4, 4, 10]))
>>> spectrum.data[1, 2, 9] = 5
>>> print(spectrum)
<EDXTEMSpectrum, title: dimensions: (4, 4|10)>
>>> print ('Sum = ', sum(sum(sum(spectrum.data))))
Sum = 164.0
>>> scale = [2, 2, 5]
>>> test = spectrum.rebin(scale)
>>> print(test)
<EDSTEMSpectrum, title: dimensions (2, 2|2)>
>>> print('Sum = ', sum(sum(sum(test.data))))
Sum =  164.0
>>> s = hs.signals.Signal1D(np.ones((2, 5, 10), dtype=np.uint8)
>>> print(s)
<Signal1D, title: , dimensions: (5, 2|10)>
>>> print(s.data.dtype)
uint8

Use dtype=np.unit16 to specify a dtype

>>> s2 = s.rebin(scale=(5, 2, 1), dtype=np.uint16)
>>> print(s2.data.dtype)
uint16

Use dtype=”same” to keep the same dtype

>>> s3 = s.rebin(scale=(5, 2, 1), dtype="same")
>>> print(s3.data.dtype)
uint8

By default dtype=None, the dtype is determined by the behaviour of numpy.sum, in this case, unsigned integer of the same precision as the platform interger

>>> s4 = s.rebin(scale=(5, 2, 1))
>>> print(s4.data.dtype)
uint64
remove_xray_lines_markers(xray_lines, render_figure=True)

Remove marker previosuly added on a spec.plot() with the name of the selected X-ray lines

Parameters:

xray_lines (list of string) – A valid list of X-ray lines to remove

set_elements(elements)

Erase all elements and set them.

Parameters:

elements (list of strings) – A list of chemical element symbols.

Examples

>>> s = hs.datasets.example_signals.EDS_SEM_Spectrum()
>>> print(s.metadata.Sample.elements)
>>> s.set_elements(['Al'])
>>> print(s.metadata.Sample.elements)
['Al' 'C' 'Cu' 'Mn' 'Zr']
['Al']
set_lines(lines, only_one=True, only_lines=('a',))

Erase all Xrays lines and set them.

See add_lines for details.

Parameters:
  • lines (list of strings) – A list of valid element X-ray lines to add e.g. Fe_Kb. Additionally, if metadata.Sample.elements is defined, add the lines of those elements that where not given in this list.

  • only_one (bool) – If False, add all the lines of each element in metadata.Sample.elements that has not line defined in lines. If True (default), only add the line at the highest energy above an overvoltage of 2 (< beam energy / 2).

  • only_lines ({None, list of strings}) – If not None, only the given lines will be added.

Examples

>>> s = hs.datasets.example_signals.EDS_SEM_Spectrum()
>>> s.add_lines()
>>> print(s.metadata.Sample.xray_lines)
>>> s.set_lines(['Cu_Ka'])
>>> print(s.metadata.Sample.xray_lines)
['Al_Ka', 'C_Ka', 'Cu_La', 'Mn_La', 'Zr_La']
['Al_Ka', 'C_Ka', 'Cu_Ka', 'Mn_La', 'Zr_La']
sum(axis=None, out=None)

Sum the data over the given axes.

Parameters:
  • axis (int, str, DataAxis, tuple (of DataAxis) or None) – Either one on its own, or many axes in a tuple can be passed. In both cases the axes can be passed directly, or specified using the index in axes_manager or the name of the axis. Any duplicates are removed. If None, the operation is performed over all navigation axes (default).

  • out (BaseSignal (or subclasses) or None) – If None, a new Signal is created with the result of the operation and returned (default). If a Signal is passed, it is used to receive the output of the operation, and nothing is returned.

  • rechunk (bool) – Only has effect when operating on lazy signal. If True (default), the data may be automatically rechunked before performing this operation.

Returns:

s – A new Signal containing the sum of the provided Signal along the specified axes.

Return type:

BaseSignal (or subclasses)

Note

If you intend to calculate the numerical integral of an unbinned signal, please use the integrate1D() function instead. To avoid erroneous misuse of the sum function as integral, it raises a warning when working with an unbinned, non-uniform axis.

See also

max, min, mean, std, var, indexmax, indexmin, valuemax, valuemin

Examples

>>> import numpy as np
>>> s = BaseSignal(np.random.random((64,64,1024)))
>>> s.data.shape
(64,64,1024)
>>> s.sum(-1).data.shape
(64,64)
class hyperspy._signals.eds.LazyEDSSpectrum(*args, **kwards)

Bases: EDSSpectrum, LazySignal1D

Create a Signal from a numpy array.

Parameters:
  • data (numpy.ndarray) – The signal data. It can be an array of any dimensions.

  • axes ([dict/axes], optional) – List of either dictionaries or axes objects to define the axes (see the documentation of the AxesManager class for more details).

  • attributes (dict, optional) – A dictionary whose items are stored as attributes.

  • metadata (dict, optional) – A dictionary containing a set of parameters that will to stores in the metadata attribute. Some parameters might be mandatory in some cases.

  • original_metadata (dict, optional) – A dictionary containing a set of parameters that will to stores in the original_metadata attribute. It typically contains all the parameters that has been imported from the original data file.

  • ragged (bool or None, optional) – Define whether the signal is ragged or not. Overwrite the ragged value in the attributes dictionary. If None, it does nothing. Default is None.