analysis.desp#

pyleem.analysis.desp.preprocess_image(image, gaussian_kernel=3, gaussian_sigma=0, use_morph=True, morph_kernel=3)[source]#

Preprocess DESP image for disk pattern detection.

Applies Gaussian blur, normalization, Otsu thresholding, and morphological operations. The morphological operations are optional.

Parameters:
  • image (ndarray) – Input image array.

  • gaussian_kernel (int) – Kernel size for Gaussian blur (must be odd).

  • gaussian_sigma (float) – Sigma for Gaussian blur.

  • use_morph (bool) – Whether to use morphological operations.

  • morph_kernel (int) – Kernel size for morphological operations.

Returns:

Processed image as uint8 in 0-255 range.

Return type:

ndarray

pyleem.analysis.desp.get_radius(image)[source]#

Detect and measure the disk pattern in a DESP image.

In this process we assume that there is only one disk pattern in the image. Uses contour detection and minimum enclosing circle to determine center and radius.

Parameters:

image (ndarray) – Preprocessed DESP image (uint8).

Returns:

Disk center (x, y) and radius in pixels.

Return type:

tuple(float, float, float)

pyleem.analysis.desp.parabola_fit(voltages, radii, window=None)[source]#

Fit a parabola to the voltages and radii.

Parameters:
  • voltages (list) – List of voltages.

  • radii (list) – List of radii.

  • window (int) – Window size for the fit.

Returns:

Fit result.

Return type:

lmfit.ModelResult

pyleem.analysis.desp.disk_kernel(radius)[source]#

Build a zero-mean filled-disk kernel.

pyleem.analysis.desp.match_score(im, radius)[source]#

Compute the match score for a given radius.

The correlation map is computed using the FFT.

pyleem.analysis.desp.eval_radii(image, radii, use_threads=True, max_workers=None)[source]#

Find the best match over radii using multiple threads.

pyleem.analysis.desp.get_radius_convolve(image, r_min, r_max, step_size, use_threads=True, max_workers=None)[source]#

Get the radius of the disk pattern in the image.

The image is processed minimally by subtracting a background.

Parameters:
  • image (ndarray) – Input image array.

  • r_min (int) – Minimum radius.

  • r_max (int) – Maximum radius.

  • step_size (int) – Step size.

  • use_threads (bool) – Whether to use threads.

  • max_workers (int) – Maximum number of workers.

Returns:

Best x, y, and radius.

Return type:

tuple(float, float, float)

class pyleem.analysis.desp.DESPAnalyzerBase(readers, roi=None, onset=0, gaussian_kernel=3, gaussian_sigma=0, use_morph=True, morph_kernel=3)[source]#

Bases: Analyzer

Base class for DESP analyzer.

get_processed_image(index)[source]#

Return the processed image.

get_energy_convert_function(params)[source]#

Return the energy conversion function.

class pyleem.analysis.desp.DESPCalibration(readers, roi=None, onset=0, gaussian_kernel=3, gaussian_sigma=0, use_morph=True, morph_kernel=3)[source]#

Bases: DESPAnalyzerBase

Calibration analyzer for DESP patterns.

analyze(window)[source]#

Calibrate radius to potential using multiple images.

Uses linear interpolation to map pattern radii to electron energies. The disk detection defaults to contour detection. The radius of the pattern is related to the square of the voltage. The relationship is fitted to a parabola.

Parameters:
  • analyzers (list) – List of Analyzer objects.

  • metadata (dict) – Optional dictionary with a ‘Start Voltage’ key whose value is a list of voltages. If None, the start voltage is read from each analyzer’s metadata.

  • window (int) – Window size for the fit.

Returns:

Interpolation function mapping radius to potential (radius_to_energy_func).

Return type:

dict

class pyleem.analysis.desp.DESPAnalyzer(readers, parabola_params, roi=None, **parameters)[source]#

Bases: DESPAnalyzerBase

Analyzer for amorphous DESP patterns.

Analyzes DESP micrographs to detect circular diffraction patterns and measure charging effects through pattern radius changes.

Parameters:
  • readers (list) – List of readers.

  • parabola_params (dict) – Parabola parameters.

Variables:
  • x_array (list) – List of X-coordinates of circle centers.

  • y_array (list) – List of Y-coordinates of circle centers.

  • radii_array (list) – List of circle radii in pixels.

  • energy_array (list) – List of electron energies.

annotate_image(index, ax)[source]#

Annotate DESP radius and energy on an image axes.

plot_energy(ax=None)[source]#

Plot the energy vs. time.

Parameters:

ax (matplotlib.axes.Axes) – Matplotlib axes object.