Tutorial 3: Peak Fitting

Peak fitting relies on pygidFIT package. First, create the mlgidBASE class instance and run detection:

from mlgidbase import mlgidBASE
filename = '../../example/BA2PbI4.h5'
analysis = mlgidBASE(filename=filename)
analysis.run_detection()
2026-05-26 13:55:20.766714561 [W:onnxruntime:Default, device_discovery.cc:283 GetGpuDevices] Failed to detect devices under "/sys/class/drm/card0": device_discovery.cc:93 ReadFileContents Failed to open file: "/sys/class/drm/card0/device/vendor"
INFO - Loading model
INFO - Saved detected peaks to file: ../../example/BA2PbI4.h5, entry: entry_0000, frame: 0

Then run fitting:


Minimal Code Example

analysis.run_fitting()
INFO - Saved fitted peaks to file: ../../example/BA2PbI4.h5, entry: entry_0000, frame: 0

Parameters

  • entry (str) — Data file entry to process. Defaults to None (all entries). OPTIONAL

  • frame_num (int/List[int]) — Frame number in each entry for detection / list of frames. Defaults to None (all frames).

  • clustering_distance_peaks (float) — Distance threshold for clustering peaks. Defaults to 10.

  • clustering_distance_rings (float) — Distance threshold for clustering rings. Defaults to 10.

  • clustering_extend (float) — Cluster extension factor. Defaults to 2.

  • theta_fixed (bool) - Whether to fix Gaussian tilt angle to 0° (azimuthal direction) during fitting. Defaults to True

  • crit_angle (float) — Critical angle used in fitting. Defaults to 0.

  • use_pool (bool) — Enable multiprocessing. Defaults to False.

  • debug (bool) — Enable debug output. Defaults to False.


Decription

The user can specify a single entry or process all entries in the file by setting entry=None. The frame_num parameter accepts either a single int or a list of frame indices to process.

Clustering is controlled by a distance threshold that defines how close detected boxes need to be to form a cluster. The clustering_extend parameter adds extra pixels to the cluster size and can be useful for broad peaks.

In many cases, peaks are azimuthally oriented. However, in GID geometry, peaks can also appear along truncation rods (i.e., along (q_z)). In this case, the tilt angle theta of the 2D Gaussian should be included in the fit. To enable this, set theta_fixed=False.

The crit_angle parameter (in degrees) can be used to account for the sample horizon. The region below this angle will be masked.

For time series, peak pooling can be enabled with use_pool=True. This reuses fitted parameters from previous frames as initial guesses and can speed up the fitting.

To visualize and print fitting details, set debug=True.

analysis = mlgidBASE(filename=filename)
analysis.run_fitting(
    entry='entry_0000',
    frame_num=0,
    clustering_distance_peaks=10,
    clustering_distance_rings=10,
    clustering_extend=2,
    crit_angle=1,
    theta_fixed=False,
    use_pool=True,
    debug=False,
)
INFO - Saved fitted peaks to file: ../../example/BA2PbI4.h5, entry: entry_0000, frame: 0

The results can be visualized using silx view or loaded from the saved file, as shown in Tutorial 8.