From 78547b5ad423c194b056df813b4bf9f92a068ece Mon Sep 17 00:00:00 2001
From: Florian Lauck <florian.lauck@cfel.de>
Date: Wed, 22 Sep 2021 16:42:15 +0200
Subject: [PATCH] Fix return type in peakfinder

---
 cfelpyutils/peakfinding/peakfinder8.py | 51 ++++++++++++++++----------
 tests/peakfinding/test_peakfinder8.py  | 10 ++---
 2 files changed, 36 insertions(+), 25 deletions(-)

diff --git a/cfelpyutils/peakfinding/peakfinder8.py b/cfelpyutils/peakfinding/peakfinder8.py
index 6a32ab6..8d80fad 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 49d5fac..9a95bd6 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
-- 
GitLab