diff --git a/cfelpyutils/peakfinding/peakfinder8.py b/cfelpyutils/peakfinding/peakfinder8.py index 6a32ab6e215ab072691eafaaf221bc4c808fa2e4..8d80fada3f7ea2afb6a68dea3e2c33280a58078b 100644 --- a/cfelpyutils/peakfinding/peakfinder8.py +++ b/cfelpyutils/peakfinding/peakfinder8.py @@ -18,20 +18,31 @@ Peakfinder8 algorithm for detection of Bragg peaks in detector frame data. """ import sys -from typing import Type, List, Union +from collections import namedtuple +from typing import Union import h5py # type: ignore import numpy # type: ignore -from mypy_extensions import TypedDict from .lib import peakfinder_8 +PeakList = namedtuple("PeakList", ["num_peaks", "fs", "ss", "intensity"]) +""" +Information about Bragg peaks found in a detector data frame (list of peaks). + +Arguments: -TypePeakList = TypedDict( - "TypePeakList", - {"num_peaks": int, "fs": List[float], "ss": List[float], "intensity": List[float]}, - total=True, -) + num_peaks(int): number of peaks that were detected in the data frame. + + fs (List[float]): a list of fractional fs indexes locating the detected peaks + in the detector data frame. + + ss (List[float]): a list of fractional ss indexes locating the detected peaks + in the detector data frame. + + intensity (List[float]): a list of integrated intensities for the detected + peaks. +""" class Peakfinder8PeakDetection: @@ -167,7 +178,7 @@ class Peakfinder8PeakDetection: else: self._mask = None - def find_peaks(self, data: numpy.ndarray) -> TypePeakList: + def find_peaks(self, data: numpy.ndarray) -> PeakList: """ Finds peaks in a detector data frame. @@ -181,19 +192,19 @@ class Peakfinder8PeakDetection: Returns: - TypePeakList: a dictionary with information about the Bragg peaks - detected in a data frame. The dictionary has the following keys: + PeakList: a namedtuple with information about the Bragg peaks + detected in a data frame. The object has the following attributes: - - A key named "num_peaks" whose value is the number of peaks that were + - An attribute named "num_peaks" whose value is the number of peaks that were detected in the data frame. - - A key named 'fs' whose value is a list of fractional fs indexes locating + - An attribute named 'fs' whose value is a list of fractional fs indexes locating the detected peaks in the data frame. - - A key named 'ss' whose value is a list of fractional ss indexes locating + - An attribute named 'ss' whose value is a list of fractional ss indexes locating the detected peaks in the data frame. - - A key named 'intensity' whose value is a list of integrated intensities + - An attribute named 'intensity' whose value is a list of integrated intensities for the detected peaks. """ if not self._mask_initialized: @@ -223,9 +234,9 @@ class Peakfinder8PeakDetection: self._local_bg_radius, ) - return { - "num_peaks": len(peak_list[0]), - "fs": peak_list[0], - "ss": peak_list[1], - "intensity": peak_list[2], - } + return PeakList( + num_peaks=len(peak_list[0]), + fs=peak_list[0], + ss=peak_list[1], + intensity=peak_list[2], + ) diff --git a/tests/peakfinding/test_peakfinder8.py b/tests/peakfinding/test_peakfinder8.py index 49d5fac4278018c83c5f958fc138b6d4155d7ade..9a95bd608b80f1ea43e06606cdf93dcd19c5a040 100644 --- a/tests/peakfinding/test_peakfinder8.py +++ b/tests/peakfinding/test_peakfinder8.py @@ -16,7 +16,7 @@ import numpy import pytest -from cfelpyutils.geometry import compute_pix_maps, load_crystfel_geometry +from cfelpyutils.geometry import compute_pix_maps from cfelpyutils.peakfinding.peakfinder8 import Peakfinder8PeakDetection from tests.fixtures import detector_geometry @@ -82,7 +82,7 @@ class TestPeakfinder8PeakDetection: def test_find_peak(self, peakfinder_config, detector_image_with_4_peaks): peakfinder = Peakfinder8PeakDetection(**peakfinder_config) peak_list = peakfinder.find_peaks(detector_image_with_4_peaks) - assert peak_list["num_peaks"] == 4 - assert len(peak_list["intensity"]) == 4 - assert len(peak_list["fs"]) == 4 - assert len(peak_list["ss"]) == 4 + assert peak_list.num_peaks == 4 + assert len(peak_list.intensity) == 4 + assert len(peak_list.fs) == 4 + assert len(peak_list.ss) == 4