From 11218377cae9864891ae5f8ddebe3f4e65926539 Mon Sep 17 00:00:00 2001
From: Martin Teichmann <martin.teichmann@xfel.eu>
Date: Tue, 13 Dec 2022 15:51:02 +0100
Subject: [PATCH] using bincount instead of histogram for integration

this removes more code from the inner loop. It is more efficient,
but is it also more readable? Probably not.
---
 src/toolbox_scs/detectors/hrixs.py | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/src/toolbox_scs/detectors/hrixs.py b/src/toolbox_scs/detectors/hrixs.py
index 11d409c..5889812 100644
--- a/src/toolbox_scs/detectors/hrixs.py
+++ b/src/toolbox_scs/detectors/hrixs.py
@@ -480,18 +480,17 @@ class hRIXS:
         x = np.arange(images.shape[1])[:, None]
         y = np.arange(images.shape[2])[None, :] - self.parabola(x)
         quo, rem = divmod(y, 1)
-        quo = np.array([[[0]], [[1]]]) + quo
+        quo = np.array([[[0]], [[1]]]) + quo - margin
         rem = rem * np.array([[[1]], [[-1]]]) + np.array([[[0]], [[1]]])
+        wrong = (quo < 0) | (quo >= bins - 2 * margin)
+        quo[wrong] = rem[wrong] = 0
+        quo = quo.astype(int).ravel()
 
         for image, r in zip(images, ret):
             if self.USE_DARK:
                 image = image - dark_image
-            r[:], _ = np.histogram(quo.ravel(), weights=(rem * image).ravel(),
-                                   bins=bins - 2 * margin,
-                                   range=(margin, bins - margin))
-        ret /= np.histogram(quo.ravel(), weights=rem.ravel(),
-                            bins=bins - 2 * margin,
-                            range=(margin, bins - margin))[0]
+            r[:] = np.bincount(quo, weights=(rem * image).ravel())
+        ret /= np.bincount(quo, weights=rem.ravel())
         data.coords["energy"] = (
             np.arange(self.Y_RANGE.start + margin, self.Y_RANGE.stop - margin)
             * self.ENERGY_SLOPE + self.ENERGY_INTERCEPT)
-- 
GitLab