pyextal.optimize

Classes

CoarseOptimize

Class for coarse optimization of thickness, orientation, and gl.

FineOptimize

Class for fine optimization of the structure factor.

Functions

voigt_2d(sigma, gamma)

Generates a 2D Voigt profile.

Module Contents

class pyextal.optimize.CoarseOptimize(dinfo: pyextal.dinfo.BaseDiffractionInfo, roi: pyextal.roi.BaseROI, searchRadius: float = None, nx: int = None)

Class for coarse optimization of thickness, orientation, and gl.

dinfo

Diffraction information object.

Type:

BaseDiffractionInfo

param

Bloch simulation from dinfo.

Type:

SimParams

roi

The region of interest object.

Type:

BaseROI

thickness

The sample thickness from dinfo.

Type:

float

gl

geometric scaling factor from dinfo.

Type:

float

indices

List of indices matching reflections to the exitwave.

Type:

List[int]

exitwave

The simulated exitwave.

Type:

np.ndarray

side

The side length of the simulation used for tilt mapping.

Type:

float

scale_factor

The scale factor from LARBED calibration between simulation and experiment.

Type:

float

templates

The experimental templates extracted from the ROI.

Type:

np.ndarray

dinfo
param
roi
thickness
gl
indices = []
exitwave
templates
optimizeOrientationGeometry(target: Callable, targetValue: float) float

Optimizes a single parameter (thickness or gl) and the sample orientation.

This method uses scipy.optimize.minimize_scalar to find the optimal value for a given target function (e.g., thickness or gl). After finding the best parameter value, it determines the optimal sample tilt by finding the location of the maximum correlation in the template matching result.

Parameters:
  • target (Callable) – The target function to minimize (e.g., thicknessTarget or glTarget).

  • targetValue (float) – The initial guess for the parameter being optimized.

Returns:

The optimized value for the target parameter.

Return type:

float

optimizeOrientationThickness(filter=None, threshold=None) None

Optimizes the thickness and orientation of the experimental pattern.

optimizeOrientationGL(filter=None, threshold=None) None

Optimizes the gl and orientation of the experimental pattern.

static thicknessTarget(x0, *args)

Error function for thickness optimization.

Parameters:
  • x0 (float) – Thickness value.

  • *args – CoarseOptimize instance.

Returns:

The optimization error, calculated as 1 - max_correlation.

Return type:

float

static glTarget(x0, *args)

Error function for gl optimization.

Parameters:
  • x0 (float) – gl value.

  • *args – CoarseOptimize instance.

Returns:

The optimization error, calculated as 1 - max_correlation.

Return type:

float

optimizeDWF() None

Optimizes the Debye-Waller factors (DWFs) for all atoms.

This method uses scipy.optimize.minimize to find the optimal DWF values that maximize the correlation between simulated and experimental patterns. It assumes isotropic DWFs.

static callbackDWF(y0)
static correlationTargetDWF(y0, *args)

Error function for DWF optimization.

Parameters:
  • y0 (np.ndarray) – Debye-Waller factors.

  • *args – a CoarseOptimize instance.

Returns:

The optimization error, calculated as 1 - max_correlation.

Return type:

float

optimizeXYZ() None

Optimizes the atomic (x, y, z) coordinates for all atoms.

This method uses scipy.optimize.minimize to find the optimal atomic positions that maximize the correlation between simulated and experimental patterns.

static callbackXYZ(y0)
static correlationTargetXYZ(y0, *args)

Error function for XYZ coordinate optimization.

Parameters:
  • y0 (np.ndarray) – XYZ coordinates for each atom.

  • *args – a CoarseOptimize instance.

Returns:

The optimization error, calculated as 1 - max_correlation.

Return type:

float

optimizeCell() None

Optimizes the cell parameters.

Currently, this method optimizes ‘a’ and ‘c’ for a tetragonal/hexagonal system, assuming a=b. It uses scipy.optimize.minimize to find the optimal cell parameters that maximize the correlation.

static callbackCell(y0)
static correlationTargetCell(y0, *args)

Error function for cell parameter optimization.

Parameters:
  • y0 (np.ndarray) – Cell parameters.

  • *args – a CoarseOptimize instance.

Returns:

The optimization error, calculated as 1 - max_correlation.

Return type:

float

optimizeHV(filter=None, threshold=None) None

Optimizes the high voltage (HV).

static HVTarget(x0, *args)

Error function for HV optimization.

Parameters:
  • x0 (float) – High voltage value.

  • *args – a CoarseOptimize instance.

Returns:

The correlation error.

Return type:

float

displayCoarseSearch(filter=None, threshold=None)

Displays the result of the coarse search.

class pyextal.optimize.FineOptimize(dinfo: pyextal.dinfo.BaseDiffractionInfo, roi: pyextal.roi.BaseROI, reflections: List[Tuple[int, int, int]], sfMask: numpy.ndarray, noncentro: bool = False, errorFunc: pyextal.gof.BaseGOF = None, perturbROI: bool = False, shiftDisk: bool = False, symUpdate=False, probe: Tuple[float, float] = None, refineProbe: bool = True)

Class for fine optimization of the structure factor.

dinfo

The main diffraction information object.

Type:

BaseDiffractionInfo

thickness

The thickness of the sample in Angstroms from dinfo.

Type:

float

lastParam

The last used simulation parameters.

Type:

SimParams

roi

The region of interest object for refinement.

Type:

BaseROI

refineProbe

Flag to indicate if probe parameters should be refined.

Type:

bool

reflections

List of (h, k, l) Miller indices for the reflections being optimized.

Type:

List[Tuple[int, int, int]]

sfMask

Mask to select which structure factor components to optimize.

Type:

np.ndarray

noncentro

Flag for non-centrosymmetric structures.

Type:

bool

errorFunc

The goodness-of-fit function used for optimization.

Type:

BaseGOF

symUpdate

Flag to update Ugh matrix with symmetry equivalents.

Type:

bool

history

A list to store the optimization history.

Type:

List

dinfo
thickness
lastParam
roi
_probe = None
refineProbe = True
reflections
sfMask
noncentro = False
errorFunc = None
_perturbROI = False
_shiftDisk = False
symUpdate = False
history = []
_nfit = 0
property perturbROI

Flag to indicate if ROI parameters (thickness, gl, rotation) should be refined.

Type:

bool

property probe

Probe parameters (sigma, gamma) for convolution.

Type:

tuple[float, float] | None

property shiftDisk

Flag to indicate if individual disk shifts should be refined.

Type:

bool

calDOF() int

Calculates the degrees of freedom for the optimization.

Returns:

the degree of freedom for the optimization.

Return type:

int

getx0(x0=None) None

Gets the initial guess for the optimization.

Parameters:

x0 (np.ndarray, optional) – Initial guess for structure factors. Shape (len(reflections), 4) for non-centro, (len(reflections), 2) for centro. Defaults to None.

getRange(x0Range=None) None

Sets the range to normalize the structure factor.

normalizeX0()

Normalizes the structure factor and applies the mask.

denormalizeX0(x0)

Denormalizes the structure factor and removes the mask.

optimize(x0: numpy.ndarray = None, x0Range: numpy.ndarray = None) None

Runs the fine optimization.

Parameters:
  • x0 (np.ndarray, optional) – Initial guess for structure factors. Defaults to None.

  • x0Range (np.ndarray, optional) – Range for structure factor normalization. Defaults to None.

evaluateSF(x0: numpy.ndarray) None

Evaluates the structure factor and solve for the eigenvector/values.

Parameters:

x0 (np.ndarray) – Structure factor values.

evaluateParam(thickness=None)

Evaluates CBED intensity based on Bloch simulation results and geometry.

Parameters:

thickness (float, optional) – Sample thickness. Defaults to None.

Returns:

Simulated CBED pattern.

Return type:

np.ndarray

display(lines, savedir=None)

Displays the optimization result.

Parameters:
  • lines (List[int]) – List of line indices to plot.

  • savedir (str, optional) – Directory to save the plot. Defaults to None.

Returns:

Experimental and simulated

regional patterns.

Return type:

Tuple[List[np.ndarray], List[np.ndarray]]

static SFtarget(x0, *args)

Error function for structure factor optimization.

Parameters:
  • x0 (np.ndarray) – Structure factor values.

  • *args – Variable length argument list, expects a FineOptimize instance.

Returns:

The optimization error.

Return type:

float

static ROItarget(x0, *args)

Error function for ROI optimization.

Parameters:
  • x0 (np.ndarray) – ROI parameters (thickness, gl, rotation, allshift).

  • *args – a FineOptimize instance.

Returns:

The goodness of fit.

Return type:

float

static ShiftTarget(x0, *args)

Error function for disk shift optimization.

Parameters:
  • x0 (np.ndarray) – Disk shift values.

  • *args – a FineOptimize instance and the simulated CBED pattern.

Returns:

The optimization error.

Return type:

float

static callback(intermediate_result, self=None)

Callback function for optimization visualization.

pyextal.optimize.voigt_2d(sigma, gamma)

Generates a 2D Voigt profile.

Parameters:
  • sigma (float) – Standard deviation for the Gaussian component.

  • gamma (float) – Half-width at half-maximum for the Lorentzian component.

Returns:

A 2D array representing the Voigt profile.

Return type:

np.ndarray