From db7b8ad0e2b69a95e86fbeadc8708a7976b518f6 Mon Sep 17 00:00:00 2001
From: Yannick Ulrich <yannick.ulrich@psi.ch>
Date: Sun, 9 Feb 2020 11:39:40 +0100
Subject: [PATCH] 10: follow-up to de61042, forgot mergebins

---
 pymule/errortools.py | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/pymule/errortools.py b/pymule/errortools.py
index 2cbd25a..acb36c1 100644
--- a/pymule/errortools.py
+++ b/pymule/errortools.py
@@ -91,3 +91,30 @@ def combineNplots(func, plots):
     for plot in plots[1:]:
         accum = func(accum, plot)
     return accum
+
+
+def mergebins(p, n):
+    if p[0,0] == -np.inf:
+        return np.concatenate([
+            [p[0,:]],
+            mergebins(p[1:,:], n)
+        ])
+    if p[-1,0] == +np.inf:
+        return np.concatenate([
+            mergebins(p[:-1,:], n),
+            [p[-1,:]]
+        ])
+
+    # Make sure the length fits
+    if len(p) % n:
+        short = p[:-(len(p) % n)]
+    else:
+        short = p[:]
+    part = np.split(short, len(short)/n)
+    return np.array([
+        (
+            sum(i[:,0])/n,
+            sum(i[:,1])/n,
+            np.sqrt(sum(i[:,2]**2))/n
+        ) for i in part
+    ])
-- 
GitLab