From 27215b7463e3e80fbca6653102eba07baa82a983 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Le=20Guyader?= <loic.le.guyader@xfel.eu>
Date: Fri, 23 Aug 2019 18:19:15 +0200
Subject: [PATCH] linear slope estimation

---
 knife_edge.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/knife_edge.py b/knife_edge.py
index 83d0d8d..10659ce 100644
--- a/knife_edge.py
+++ b/knife_edge.py
@@ -46,7 +46,10 @@ def knife_edge(nrun, axisKey='scannerX', signalKey='FastADC4peaks', p0=None, ful
     sortIdx = np.argsort(positions)
     positions = positions[sortIdx]
     intensities = nrun[signalKey].values.flatten()[sortIdx]
-    if intensities[0] > intensities[-1]:
+
+    # estimate a linear slope fitting the data to determine which function to fit
+    slope = np.cov(positions, intensities)[0][1]/np.var(positions) 
+    if slope < 0:
         func = integPowerDown
         funcStr = 'a*erfc(np.sqrt(2)*(x-x0)/w0)'
     else:
@@ -76,4 +79,4 @@ def knife_edge(nrun, axisKey='scannerX', signalKey='FastADC4peaks', p0=None, ful
     if full:
         return popt, pcov
     else:
-        return np.array([popt[1], pcov[1,1]**0.5])
\ No newline at end of file
+        return np.array([popt[1], pcov[1,1]**0.5])
-- 
GitLab