API Summary

Plotting

fit_peak(sig, lower_bound, upper_bound[, …]) Fit a Gaussian peak in a range to a signal
add_colored_outlines(fig, signal, num_images) Add outlines to a matplotlib figure to make it easy to visualize with the plotted spectra
add_custom_colorbars(fig[, tick_list]) Add custom colorbars with ticks at specified values
plot_dm3_survey_with_markers(fname[, …]) Plot a hyperspy signal with the markers from digital micrograph enabled

Line Scan Shifting

hyperspy_tools.shifting_lines.get_shifts_from_area_stem
hyperspy_tools.shifting_lines.shift_area_stem
hyperspy_tools.shifting_lines.determine_shifts
hyperspy_tools.shifting_lines._interp_spectrum
hyperspy_tools.shifting_lines.shift_line_scan
hyperspy_tools.shifting_lines.shift_lines
hyperspy_tools.shifting_lines.crop_area_scan
hyperspy_tools.shifting_lines.shift_area_eels
hyperspy_tools.shifting_lines.smooth_scan
hyperspy_tools.shifting_lines.smooth_scans
hyperspy_tools.shifting_lines.normalize_lines
hyperspy_tools.shifting_lines.get_scans_and_eels_fnames
hyperspy_tools.shifting_lines.load_shift_and_build_area
hyperspy_tools.shifting_lines._natural_sort
hyperspy_tools.shifting_lines.gui_fnames

EELS Methods

extract_ZLP(signal[, range_factor, …]) Extracts the ZLP of an EELS spectrum and returns it and the inelastic spectrum.
align_energy_vertical(signal[, start, end, …]) Align the energy (signal) axis of a spectrum image, based off of the spectrum in the first column of each row.

Plotting API

hyperspy_tools.plotting.fit_peak(sig, lower_bound, upper_bound, factor_num=None)[source]

Fit a Gaussian peak in a range to a signal

Parameters:
  • sig (Signal) – Signal to fit
  • lower_bound (float) – Lower bound of values in which to fit
  • upper_bound (float) – Upper bound of values in which to fit
  • factor_num (int) – if given, the fit will be performed on a decomposition component of the signal (given by factor_num), rather than the signal itself
Returns:

centre – center of the fitted Gaussian

Return type:

float

hyperspy_tools.plotting.add_colored_outlines(fig, signal, num_images, color_palette=<Mock name='mock.color_palette()' id='140223390467536'>, border=0.0, lw=15)[source]

Add outlines to a matplotlib figure to make it easy to visualize with the plotted spectra

Parameters:
  • fig (matplotlib.figure) – figure to which to add outlines (this should not have colorbars, add them later)
  • signal (Signal) – signal that has calibrated axes (in order to set bounds of rectangle)
  • num_images (int) – number of images in figure
  • color_palette (list) – list of colors to use for outlines
  • border (float) – offset of rectangle from edge of data
  • lw (float) – width of rectangle lines to plot
hyperspy_tools.plotting.add_custom_colorbars(fig, tick_list=None)[source]

Add custom colorbars with ticks at specified values

Parameters:
  • fig (matplotlib.figure) – figure to which to add colorbars (should not currently have colorbars)
  • tick_list (list) –

    nested list with the position of ticks to be added to colorbars; should have a length equal to the number of images in the figure Example for a four plot figure:

    >>> tick_list =  [[120,200,280],
    ...               [4,20,36],
    ...               [0,8,16],
    ...               [0,22,44]]
    
hyperspy_tools.plotting.plot_dm3_survey_with_markers(fname, add_text=True, plot_beam=True, plot_si=True, plot_drift=True, x_offset=1.0, y_offset=1.0, im_scale=1.0, text_size='xx-small', **kwargs)[source]

Plot a hyperspy signal with the markers from digital micrograph enabled

Parameters:
  • fname (str) – Name of .dm3 file to load
  • add_text (bool) – Switch to control if labels for the markers are added to the plot
  • plot_beam (bool) – Switch to control if beam point is plotted (if present)
  • plot_si (bool) – Switch to control if spectrum image box or line is plotted (if present)
  • plot_drift (bool) – Switch to control if spatial drift box is plotted (if present)
  • x_offset (float) – multiplier to control how far the text will be offset from its default position (in the x-direction)
  • y_offset (float) – multiplier to control how far the text will be offset from its default position (in the y-direction)
  • im_scale (float) – will scale the survey image by a given factor (useful for correcting image scale bars if the calibration is incorrect)
  • text_size (str or float) – size of the text that will be written on the image (follows same convention as the Text matplotlib Artist
  • **kwargs – Other keyword arguments are passed to hs.signal.plot()
Returns:

im – HyperSpy signal with markers added. While the figure is open, the figure can be saved with a call such as im._plot.signal_plot.figure.savefig()

Return type:

hyperspy._signals.image.Image

Line Scan Shifting API

EELS API

hyperspy_tools.eels.extract_ZLP(signal, range_factor=(-10, 6), return_inelastic=True, print_output=True)[source]

Extracts the ZLP of an EELS spectrum and returns it and the inelastic spectrum.

ZLP is fit with the sum of a Gaussian and Lorentzian function. Fit range is set by first fitting a gaussian and calculating HWHM based off that. This is the method used by Gatan’s DigitalMicrograph software

Parameters:
  • signal (Signal) – Low-loss signal from which to extract the zero-loss
  • range_factor (tuple) – This parameter must be a tuple of length two, with the first value less than the second. The values here correspond to the range over which the ZLP will be fit. The HWHM of the initial ZLP fit will be multiplied by these factors to obtain the fit range.
  • return_inelastic (bool) – This switch controls whether the method will return the inelastic signal as well, which is calculated by subtracting the ZLP from the original signal and then “cleaning up” by removing negative values
  • print_output (bool) – This switch controls whether or not the method will output any results, besides the return.
Returns:

  • zlp (Signal instance representing the ZLP of the spectrum)
  • inelastic (Signal instant representing the inelastic portion of) – the spectrum (only returned if return_inelastic is set)

hyperspy_tools.eels.align_energy_vertical(signal, start=None, end=None, column=0, smoothing_parameter=0.05, number_of_iterations=1, print_output=True, plot_deriv=False, plot_shifts=False)[source]

Align the energy (signal) axis of a spectrum image, based off of the spectrum in the first column of each row. This is useful for SI of vertically aligned interfaces.

If the spectrum image is uniform in the left-most column, this method should help to reduce the effect of energy drift that occurs during acquisition (if you cannot acquire the zero-loss at the same time). It will not correct for any drift that occurs within each row, but that is a much trickier problem, considering the SI likely covers different phases.

A note on the method: the first column is extracted as a line scan. This data is smoothed using a Lowess filter (to reduce the impact of noise), and then the signal-dimension derivative is taken. This signal is passed to hyperspy.signal.Signal1DTools.estimate_shift1D in order to figure out what shift is necessary to keep the energy axis aligned. Each column of the original spectrum image is shifted by this same amount, and then the overall spectrum image is cropped in the signal dimension so there are no blank pixels. If the results are not quite as expected, try increasing the smoothing parameter, as noise in the derivative is the most likely reason for failure.

Parameters:
  • signal (Signal) – 2D spectrum image to align in energy dimension
  • start ({int | float | None}) – The limits of the interval in which to align. If int they are taken as the axis index. If float they are taken as the axis value.
  • end ({int | float | None}) – The limits of the interval in which to align. If int they are taken as the axis index. If float they are taken as the axis value.
  • column (int) – The column of data to use for shifting. By default, the left-most ( 0) is used, but if there is no edge in this area, a different column should be used.
  • smoothing_parameter (float) – Degree of smoothing used to smooth the original spectral data (necessary before taking the derivative). This parameter is passed to smooth_lowess()
  • number_of_iterations (int) – Number of Lowess iterations used to smooth the original spectral data (necessary before taking the derivative). This parameter is passed to smooth_lowess()
  • print_output (bool) – Whether or not to show output during calculation.
  • plot_deriv (bool) – Whether or not to plot the derivative output. Useful if results are not as expected, and can show if more smoothing is needed
  • plot_shifts (bool) – Whether or not to show a plot illustrating the shifts that were found
Returns:

aligned_signal – 2D spectrum image with signal axes aligned and cropped

Return type:

Signal