Tutorial 2: Peak Detection
Peak detection relies on mlgidDETECT package.
First, create the mlgidBASE class instance:
from mlgidbase import mlgidBASE
filename = '../../example/BA2PbI4.h5'
analysis = mlgidBASE(filename=filename)
INFO - Failed to extract font properties from /usr/share/fonts/truetype/noto/NotoColorEmoji.ttf: Non-scalable fonts are not supported
INFO - generated new fontManager
2026-07-21 15:46:56.129599394 [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"
Then run detection:
Minimal Code Example
analysis.run_detection()
INFO - Model file does not exist.
INFO - Starting download of model file to /home/docs/.local/share/mlgiddetect/dino.onnx
[--------------------------------------------------] 0.0% 0.0/968.4 MB 0.0 KB/s
[--------------------------------------------------] 0.0% 0.0/968.4 MB 14.6 KB/s
[--------------------------------------------------] 0.2% 1.7/968.4 MB 3.31 MB/s
[--------------------------------------------------] 0.4% 3.5/968.4 MB 3.53 MB/s
[--------------------------------------------------] 0.5% 5.1/968.4 MB 3.05 MB/s
[--------------------------------------------------] 0.7% 6.7/968.4 MB 2.96 MB/s
[--------------------------------------------------] 0.9% 8.2/968.4 MB 3.09 MB/s
[--------------------------------------------------] 1.0% 9.9/968.4 MB 3.15 MB/s
[--------------------------------------------------] 1.2% 11.9/968.4 MB 3.68 MB/s
[--------------------------------------------------] 1.4% 13.5/968.4 MB 2.94 MB/s
[--------------------------------------------------] 1.5% 14.9/968.4 MB 2.85 MB/s
[--------------------------------------------------] 1.8% 17.0/968.4 MB 4.04 MB/s
[--------------------------------------------------] 1.9% 18.6/968.4 MB 3.03 MB/s
[#-------------------------------------------------] 2.1% 20.2/968.4 MB 3.02 MB/s
[#-------------------------------------------------] 2.2% 21.7/968.4 MB 2.94 MB/s
[#-------------------------------------------------] 2.4% 23.3/968.4 MB 3.10 MB/s
[#-------------------------------------------------] 2.6% 25.0/968.4 MB 3.21 MB/s
[#-------------------------------------------------] 2.7% 26.6/968.4 MB 3.06 MB/s
[#-------------------------------------------------] 2.9% 28.4/968.4 MB 3.34 MB/s
[#-------------------------------------------------] 3.1% 30.1/968.4 MB 3.19 MB/s
[##------------------------------------------------] 4.7% 45.7/968.4 MB 29.49 MB/s
[##------------------------------------------------] 4.9% 47.4/968.4 MB 3.14 MB/s
[##------------------------------------------------] 5.0% 48.7/968.4 MB 2.47 MB/s
[##------------------------------------------------] 5.2% 50.2/968.4 MB 2.82 MB/s
[##------------------------------------------------] 5.3% 51.6/968.4 MB 2.81 MB/s
[#####---------------------------------------------] 10.1% 97.9/968.4 MB 91.80 MB/s
[#####---------------------------------------------] 10.7% 104.0/968.4 MB 12.18 MB/s
[#####---------------------------------------------] 11.2% 108.3/968.4 MB 8.45 MB/s
[#####---------------------------------------------] 11.6% 112.7/968.4 MB 8.88 MB/s
[######--------------------------------------------] 12.5% 120.7/968.4 MB 15.67 MB/s
[######--------------------------------------------] 13.0% 126.0/968.4 MB 10.48 MB/s
[#######-------------------------------------------] 14.0% 135.8/968.4 MB 19.54 MB/s
[#######-------------------------------------------] 14.6% 141.6/968.4 MB 11.47 MB/s
[#######-------------------------------------------] 15.5% 150.3/968.4 MB 17.20 MB/s
[#######-------------------------------------------] 15.9% 154.2/968.4 MB 7.54 MB/s
[########------------------------------------------] 17.1% 166.0/968.4 MB 22.95 MB/s
[########------------------------------------------] 17.4% 168.1/968.4 MB 4.22 MB/s
[########------------------------------------------] 17.9% 173.2/968.4 MB 10.13 MB/s
[#########-----------------------------------------] 18.5% 178.9/968.4 MB 11.21 MB/s
[#########-----------------------------------------] 18.9% 183.3/968.4 MB 8.54 MB/s
[#########-----------------------------------------] 19.7% 191.2/968.4 MB 15.72 MB/s
[##########----------------------------------------] 20.4% 197.2/968.4 MB 11.49 MB/s
[###########---------------------------------------] 22.6% 219.1/968.4 MB 43.54 MB/s
[###########---------------------------------------] 23.7% 229.4/968.4 MB 20.24 MB/s
[############--------------------------------------] 24.7% 238.9/968.4 MB 18.76 MB/s
[############--------------------------------------] 24.9% 241.0/968.4 MB 4.10 MB/s
[############--------------------------------------] 25.1% 243.5/968.4 MB 5.05 MB/s
[############--------------------------------------] 25.4% 245.6/968.4 MB 3.93 MB/s
[############--------------------------------------] 25.6% 248.2/968.4 MB 4.96 MB/s
[############--------------------------------------] 26.0% 251.6/968.4 MB 6.80 MB/s
[#############-------------------------------------] 26.3% 254.4/968.4 MB 5.52 MB/s
[#############-------------------------------------] 26.5% 257.1/968.4 MB 5.40 MB/s
[#############-------------------------------------] 26.8% 259.6/968.4 MB 4.84 MB/s
[#############-------------------------------------] 27.0% 261.2/968.4 MB 3.22 MB/s
[#############-------------------------------------] 27.3% 264.6/968.4 MB 6.73 MB/s
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
File ~/checkouts/readthedocs.org/user_builds/mlgidbase/envs/latest/lib/python3.11/site-packages/mlgidbase/mlgiddetect_functions.py:68, in load_inference(analysis)
67 try:
---> 68 analysis.imp_detect = Inference(analysis.config_detect)
69 except:
File ~/checkouts/readthedocs.org/user_builds/mlgidbase/envs/latest/lib/python3.11/site-packages/mlgiddetect/inference/inference.py:12, in Inference.__init__(self, config)
11 self.config = config
---> 12 model_path = path_utils.get_model_path(config)
13 if model_path is None:
File ~/checkouts/readthedocs.org/user_builds/mlgidbase/envs/latest/lib/python3.11/site-packages/mlgiddetect/utils/path_utils.py:134, in get_model_path(config, model_name)
133 if not check_onnx_filepath(config, onnx_dir) or config.MODEL_REDOWNLOAD:
--> 134 return download(config, model_name, destination=onnx_dir)
135 return str(onnx_dir)
File ~/checkouts/readthedocs.org/user_builds/mlgidbase/envs/latest/lib/python3.11/site-packages/mlgiddetect/utils/path_utils.py:110, in download(config, model_name, source, destination)
108 sys.stdout.flush()
--> 110 urllib.request.urlretrieve(source, destination, reporthook=_progress_hook)
111 total_size = os.path.getsize(destination)
File ~/.asdf/installs/python/3.11.15/lib/python3.11/urllib/request.py:270, in urlretrieve(url, filename, reporthook, data)
269 while True:
--> 270 block = fp.read(bs)
271 if not block:
File ~/.asdf/installs/python/3.11.15/lib/python3.11/http/client.py:478, in HTTPResponse.read(self, amt)
477 amt = self.length
--> 478 s = self.fp.read(amt)
479 if not s and amt:
480 # Ideally, we would raise IncompleteRead if the content-length
481 # wasn't satisfied, but it might break compatibility.
File ~/.asdf/installs/python/3.11.15/lib/python3.11/socket.py:718, in SocketIO.readinto(self, b)
717 try:
--> 718 return self._sock.recv_into(b)
719 except timeout:
File ~/.asdf/installs/python/3.11.15/lib/python3.11/ssl.py:1314, in SSLSocket.recv_into(self, buffer, nbytes, flags)
1311 raise ValueError(
1312 "non-zero flags not allowed in calls to recv_into() on %s" %
1313 self.__class__)
-> 1314 return self.read(nbytes, buffer)
1315 else:
File ~/.asdf/installs/python/3.11.15/lib/python3.11/ssl.py:1166, in SSLSocket.read(self, len, buffer)
1165 if buffer is not None:
-> 1166 return self._sslobj.read(len, buffer)
1167 else:
KeyboardInterrupt:
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
Cell In[2], line 1
----> 1 analysis.run_detection()
File ~/checkouts/readthedocs.org/user_builds/mlgidbase/envs/latest/lib/python3.11/site-packages/mlgidbase/main.py:194, in mlgidBASE.run_detection(self, entry, frame_num, config_detect, model_type)
179 def run_detection(self, entry=None, frame_num=None, config_detect=None, model_type=None):
180 """
181 Run peak detection on the dataset.
182
(...) 192 Type of detection model to use (e.g., 'faster_rcnn', 'dino').
193 """
--> 194 _run_detection(self, entry, frame_num, config_detect, model_type)
File ~/checkouts/readthedocs.org/user_builds/mlgidbase/envs/latest/lib/python3.11/site-packages/mlgidbase/mlgiddetect_functions.py:48, in _run_detection(analysis, entry, frame_num, config_detect, model_type)
43 # if model_type is not None:
44 # if analysis.config_detect.MODEL_TYPE != model_type:
45 # analysis.config_detect.MODEL_TYPE = model_type
46 # analysis.imp_detect = None
47 if analysis.imp_detect is None:
---> 48 load_inference(analysis)
50 if not analysis.from_nexus:
51 if frame_num != 1 and not frame_num is None:
File ~/checkouts/readthedocs.org/user_builds/mlgidbase/envs/latest/lib/python3.11/site-packages/mlgidbase/mlgiddetect_functions.py:70, in load_inference(analysis)
68 analysis.imp_detect = Inference(analysis.config_detect)
69 except:
---> 70 raise ValueError("Detection failed. Couldn't load the model.")
ValueError: Detection failed. Couldn't load the model.
Parameters
entry(str) — Data file entry to process. Defaults toNone(all entries). OPTIONALframe_num(int/List[int]) — Frame number in each entry for detection / list of frames. Defaults toNone(all frames).config_file(str) — Path to detection configuration file. Defaults toNone(default parameters). OPTIONALmodel_type(str) — Name of the modeldinoorfaster_rcnn. OPTIONAL
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.
The detection configuration can be loaded from a YAML file. Additionally, the detection model can be selected via the model_type parameter. Currently, two models are supported: dino and faster_rcnn, as described in the recent work by C. Völter.
Informational logs indicate whether the detection model is being loaded. After the initial load, both the model and its configuration remain fixed; any subsequent configuration inputs will be ignored. To apply a different configuration, a new mlgidBASE instance must be created with the desired settings.
analysis = mlgidBASE(filename=filename)
analysis.run_detection(
entry='entry_0000',
frame_num=0,
config_detect='../../example/dino.yaml',
model_type = 'dino')
INFO:root:Loading model
INFO:mlgidBASE:Saved detected 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.