pypty.tcbf¶
run_tcbf_alignment
¶
Align and fit the beam contrast transfer function (CTF) using 4D-STEM data.
This function estimates beam aberrations by aligning individual pixel images using cross-correlation and fitting a CTF model. It supports iterative fitting with various binning levels and options for low-frequency drift compensation.
| PARAMETER | DESCRIPTION |
|---|---|
params
|
Dictionary containing PyPTY experimental and reconstruction settings.
|
binning_for_fit
|
List (for integers) of binning factors for each iteration of the CTF fit.
TYPE:
|
save
|
Whether to save intermediate tcBF images and shift estimates.
TYPE:
|
optimize_angle
|
Whether to include probe rotation angle in the fit.
TYPE:
|
aberrations
|
Initial guess for aberrations. If None, n_aberrations_to_fit zeros will be used.
TYPE:
|
n_aberrations_to_fit
|
Number of aberrations to fit if no initial guess is provided.
TYPE:
|
reference_type
|
Reference used for cross-correlation ("bf" or "zero").
TYPE:
|
ctf_sign_flip
|
Boolean flag, helps to align the images.
DEFAULT:
|
refine_box_dim
|
Radius (in pixels) of the interpolation box for sub-pixel shift refinement.
TYPE:
|
upsample
|
Factor for refining cross-correlation to estimate sub-pixel shifts.
TYPE:
|
cross_corr_type
|
Type of cross-correlation to use ("phase" or "classical").
TYPE:
|
cancel_large_shifts
|
Threshold (0–1) to ignore large shift outliers in the fit.
TYPE:
|
pattern_blur_width
|
Radius for optional circular blur mask applied to patterns.
TYPE:
|
scan_pad
|
Number of scan pixels to pad around the dataset (auto if None).
TYPE:
|
aperture
|
Aperture mask. If None, attempts to extract from parameters.
TYPE:
|
subscan_region
|
Subregion for CTF fitting: [left, top, right, bottom].
TYPE:
|
compensate_lowfreq_drift
|
Whether to compensate for pattern drift in large FOVs.
TYPE:
|
append_lowfreq_shifts_to_params
|
Whether to store low-frequency drift corrections in
TYPE:
|
interpolate_scan_factor
|
Factor to upsample the scan via interpolation (experimental).
TYPE:
|
binning_cross_corr
|
Binning factor before peak detection in cross-correlation.
TYPE:
|
phase_cross_corr_formula
|
Use analytical peak refinement formula for phase correlation.
TYPE:
|
f_scale_lsq
|
Scaling factor for residuals in
TYPE:
|
x_scale_lsq
|
Scaling for initial step size in
TYPE:
|
loss_lsq
|
Loss function for
TYPE:
|
tol_ctf
|
Tolerance (
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Updated parameter dictionary with fitted aberrations, defocus, and potentially updated scan positions and rotation. |
upsampled_tcbf
¶
Perform an upsampled tcBF (transmission coherent Bright Field) reconstruction.
This function reconstructs a tcBF image on an upsampled scan grid from 4D-STEM data.
It applies Fourier-based shifts to align the bright field pixel images and combines them into a final image.
Prior to calling this function, it is recommended to run the tcBF alignment routine to update pypty_params.
| PARAMETER | DESCRIPTION |
|---|---|
pypty_params
|
Dictionary containing experimental parameters and reconstruction settings. This should include keys such as 'data_path', 'scan_size', 'aperture_mask', 'acc_voltage', etc.
TYPE:
|
upsample
|
Upsampling factor for the scan grid. Default is 5.
TYPE:
|
pad
|
Number of additional scan positions to pad on each side to avoid wrap-around artifacts. Default is 10.
TYPE:
|
compensate_lowfreq_drift
|
If True, compensates for low-frequency drift of the aperture. Requires that run_tcbf_alignment has been executed to provide drift corrections. Default is False.
TYPE:
|
ctf_sign_flig
|
Boolean flag, helps to compensate artifacts
|
default_float
|
Precision for floating point computations. Use 64 for higher precision or 32 for lower memory usage. Default is 64.
TYPE:
|
round_shifts
|
If True, shifts are rounded and alignment is performed using array roll operations. If False, FFT-based subpixel shifting is used. Default is False.
TYPE:
|
xp
|
Backend array module (e.g., numpy or cupy). Default is cupy.
TYPE:
|
save
|
Flag to save the output image. If True, the image is saved to disk.
Ignored if 'save_preprocessing_files' is set in
TYPE:
|
max_parallel_fft
|
Maximum number of FFTs to perform in parallel for vectorized processing. Default is 100.
TYPE:
|
bin_fac
|
Binning factor for the data in reciprocal space. Default is 1 (no binning).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Real-valued tcBF image reconstructed on the upsampled grid. |
float
|
Final pixel size in Ångströms after upsampling. |
run_tcbf_compressed_alignment
¶
Perform a CTF alignment using compressed 4D-STEM data and masked bright-field regions.
This function fits the beam CTF to the shifts between the individual pixel images of the 4d-stem dataset. It's the same as run_tcbf_alignment, but for compressed data. The shift estimation is done via cross-correaltion.
| PARAMETER | DESCRIPTION |
|---|---|
params
|
Dictionary containing experimental and reconstruction settings.
TYPE:
|
num_iterations
|
Number of fitting iterations to perform.
TYPE:
|
save
|
Whether to save intermediate tcBF images and shift maps. Default is True.
TYPE:
|
optimize_angle
|
Whether to include probe rotation angle in the CTF fit. Default is True.
TYPE:
|
aberrations
|
Initial guess for the aberration coefficients. If None, it will be inferred or zero-initialized.
TYPE:
|
n_aberrations_to_fit
|
Number of aberration coefficients to fit if
TYPE:
|
reference_type
|
"bf" to use the tcBF image as a reference, "zero" to use the central pixel. Default is "bf".
TYPE:
|
ctf_sign_flip
|
Boolean flag, helps to compensate artifacts
DEFAULT:
|
refine_box_dim
|
Size of the cropped region around the cross-correlation peak for sub-pixel refinement. Default is 10.
TYPE:
|
upsample
|
Upsampling factor for sub-pixel interpolation. Default is 3.
TYPE:
|
cross_corr_type
|
Cross-correlation method: "phase" (recommended) or "classic". Default is "phase".
TYPE:
|
cancel_large_shifts
|
Threshold to reject large shift outliers during fitting. Value between 0 and 1. Default is None.
TYPE:
|
pattern_blur_width
|
Width of blur kernel for patterns prior to analysis. Default is None.
TYPE:
|
scan_pad
|
Number of scan pixels to pad around the scan to prevent wrap-around. Default is auto.
TYPE:
|
aperture
|
Aperture mask defining pixels to analyze. If None, it will be loaded from
TYPE:
|
subscan_region
|
Optional subregion [left, top, right, bottom] on which to perform the alignment. Default is None.
TYPE:
|
compensate_lowfreq_drift
|
Whether to compute and correct for slow drifting of the aperture over time. Default is False.
TYPE:
|
append_lowfreq_shifts_to_params
|
If True, saves the low-frequency correction back into
TYPE:
|
interpolate_scan_factor
|
Experimental: interpolate scan grid by this factor (e.g., 2 for 2x upsampled grid). Default is 1.
TYPE:
|
binning_cross_corr
|
Binning factor applied to cross-correlation maps before refinement. Default is 1.
TYPE:
|
phase_cross_corr_formula
|
If True, uses analytical subpixel peak estimation for phase correlation. Default is False.
TYPE:
|
f_scale_lsq
|
Scaling factor for least squares residuals (
TYPE:
|
x_scale_lsq
|
Initial step scaling (
TYPE:
|
loss_lsq
|
Loss type for least squares optimizer. E.g., "linear", "huber". Default is "linear".
TYPE:
|
tol_ctf
|
Tolerance for optimizer convergence (
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Updated dictionary of reconstruction parameters including fitted aberrations and scan rotation. |