Tutorial 7: Operations with peaks

The peak list can be modified using the mlgidGUI. This interactive interface allows users to visualize, adjust, and save peak selections directly to a NeXus file.

In addition, mlgidBASE provides basic functionality to delete and add peaks. First, run the analysis:

from mlgidbase import mlgidBASE
filename = '../../example/BA2PbI4.h5'
analysis = mlgidBASE(filename=filename)
analysis.run_detection()
analysis.run_fitting()
analysis.run_matching(
    cif_prepr = r'../../example/prepr_cifs.pickle',
    peaks_type='segments')

analysis.run_matching(
    cif_prepr = r'../../example/prepr_cifs.pickle',
    peaks_type='rings')
2026-05-26 13:57:52.920004243 [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
INFO - Saved fitted peaks to file: ../../example/BA2PbI4.h5, entry: entry_0000, frame: 0
INFO - Saved matched peaks to file: ../../example/BA2PbI4.h5, entry: entry_0000, frame: 0
INFO - cif_prepr is already set. The previous cif_prepr is to be used
INFO - No solutions for (../../example/BA2PbI4.h5, entry: entry_0000, frame: 0) was found. Try to decrease threshold

Segments

Visualize the matched peaks

# Visualization settings for matched peaks (multiple structures in solution)
matched_params = {
    'solution': None,              # list of structures to display (None = all)
    'plot_segments': True,         # show matched peak positions
    'marker': ['s', 's', 's'],     # marker shapes for each structure
    'marker_size': [50, 50, 50],   # marker sizes for each structure
    'marker_facecolor': ["none", "none", "none"],  # marker fill colors
    'marker_edgecolor': ["winter", 'blue', 'green'], # marker edge colors per structure
    'plot_rings': False,            # draw matched rings
    'line_width': [1, 1, 1],       # line widths for each structure
    'line_style': ["--", "--", "--"], # line styles for each structure
    'line_color': ["bone", 'blue', 'bone'], # line colors per structure
    'plot_id': True,              # display peak IDs
    'text_color': 'white',         # color of ID labels
    'text_size': 8,                # font size of ID labels
    'legend': True,                # show legend for multiple structures
    'plot': True,                 # enable/disable plotting of matched peaks
}

# Plot analysis results with selected matched peak visualization
analysis.plot_analysis_results(
    matched_params=matched_params,
    entry='entry_0000',  # select entry
    frame_num=0,          # select frame
    clims=(50, 1e4),     # color map
)
../_images/654c0dda8e46fd165c21617e8b53841f07c23561031fd6e6275a8a0206984307.png

Delete the peak #94

analysis.delete_peak(
    entry='entry_0000',
    frame_num=0,
    peak_id=94 # peak number
)

The peak dissapears from all: detected, fitted and matched (all solutions and structures) peak lists.

analysis.plot_analysis_results(
    matched_params=matched_params,
    entry='entry_0000',  # select entry
    frame_num=0,          # select frame
    clims=(50, 1e4),     # color map
)
../_images/728444390f2124e477478bf6f06c1bb59f34e50073f44430dc47da28427792a5.png

Add a box for the missing peak at q_xy = 2.25, q_z=2.5. NOTE: it appears only in detected peaks

analysis.add_peak(
    entry='entry_0000',
    frame_num=0,
    q_xy = 2.25, # box center in horizontal direction (Å⁻¹)
    dq_xy=0.1,   # box wight in horizontal direction (Å⁻¹)
    q_z=2.5,     # box center in vertical direction (Å⁻¹)
    dq_z=0.06    # box wight in vertical direction (Å⁻¹)
)
INFO - Peak id#95 has been added

Run fitting and matching:

analysis.run_fitting()
analysis.run_matching(
    cif_prepr = r'../../example/prepr_cifs.pickle',
    peaks_type='segments')

analysis.run_matching(
    cif_prepr = r'../../example/prepr_cifs.pickle',
    peaks_type='rings')
INFO - Saved fitted peaks to file: ../../example/BA2PbI4.h5, entry: entry_0000, frame: 0
INFO - cif_prepr is already set. The previous cif_prepr is to be used
INFO - Saved matched peaks to file: ../../example/BA2PbI4.h5, entry: entry_0000, frame: 0
INFO - cif_prepr is already set. The previous cif_prepr is to be used
INFO - No solutions for (../../example/BA2PbI4.h5, entry: entry_0000, frame: 0) was found. Try to decrease threshold
analysis.plot_analysis_results(
    matched_params=matched_params,
    entry='entry_0000',  # select entry
    frame_num=0,          # select frame
    clims=(50, 1e4),     # color map
)
../_images/700e1ee84e37376bed33285d9d6f7451ad1ade7217496c989e4a6f04280b2ebb.png

Note: The datasets in the NeXus file are modified when peaks are added or deleted.

Rings

Repeat the procedure for rings. Visualize the matched rings:

# Visualization settings for matched peaks (multiple structures in solution)
matched_params = {
    'solution': None,              # list of structures to display (None = all)
    'plot_segments': False,         # show matched peak positions
    'marker': ['s', 's', 's'],     # marker shapes for each structure
    'marker_size': [50, 50, 50],   # marker sizes for each structure
    'marker_facecolor': ["none", "none", "none"],  # marker fill colors
    'marker_edgecolor': ["winter", 'blue', 'green'], # marker edge colors per structure
    'plot_rings': True,            # draw matched rings
    'line_width': [1, 1, 1],       # line widths for each structure
    'line_style': ["--", "--", "--"], # line styles for each structure
    'line_color': ["bone", 'blue', 'bone'], # line colors per structure
    'plot_id': True,              # display peak IDs
    'text_color': 'black',         # color of ID labels
    'text_size': 12,                # font size of ID labels
    'legend': True,                # show legend for multiple structures
    'plot': True,                 # enable/disable plotting of matched peaks
}

# Plot analysis results with selected matched peak visualization
analysis.plot_analysis_results(
    matched_params=matched_params,
    entry='entry_0000',  # select entry
    frame_num=0,          # select frame
    clims=(50, 1e4),     # color map
)

Delete the peak #17

analysis.delete_peak(
    entry='entry_0000',
    frame_num=0,
    peak_id=17
)

Plot:

analysis.plot_analysis_results(
    matched_params=matched_params,
    entry='entry_0000',  # select entry
    frame_num=0,          # select frame
    clims=(50, 1e4),     # color map
)

Add a ring box:

analysis.add_peak(
    entry='entry_0000',
    frame_num=0,
    angle=45,       # angular center of the box (in deg). 45 for rings.
    angle_width=90, # angular width of the box (in deg). 90 for rings.
    radius=2.5,     # radial center of the box (Å⁻¹).
    radius_width=0.05, # radial width of the box (Å⁻¹)
)
INFO - Peak id#95 has been added

Run fitting and matching:

analysis.run_fitting()
analysis.run_matching(
    cif_prepr = r'../../example/prepr_cifs.pickle',
    peaks_type='segments')

analysis.run_matching(
    cif_prepr = r'../../example/prepr_cifs.pickle',
    peaks_type='rings')
INFO - Saved fitted peaks to file: ../../example/BA2PbI4.h5, entry: entry_0000, frame: 0
INFO - cif_prepr is already set. The previous cif_prepr is to be used
INFO - Saved matched peaks to file: ../../example/BA2PbI4.h5, entry: entry_0000, frame: 0
INFO - cif_prepr is already set. The previous cif_prepr is to be used
INFO - No solutions for (../../example/BA2PbI4.h5, entry: entry_0000, frame: 0) was found. Try to decrease threshold

Plot:

analysis.plot_analysis_results(
    matched_params=matched_params,
    entry='entry_0000',  # select entry
    frame_num=0,          # select frame
    clims=(50, 1e4),     # color map
)