From 7e1f3f54c2d0470b58d7e79afeb7263d31af5885 Mon Sep 17 00:00:00 2001 From: gac-x10da Date: Tue, 26 Feb 2019 10:45:24 +0100 Subject: [PATCH] Startup --- script/cpython/gfitoff.py | 30 ++++++++++++++++++++++++++++++ script/cpython/wrapper.py | 9 +++++++++ 2 files changed, 39 insertions(+) create mode 100644 script/cpython/gfitoff.py create mode 100644 script/cpython/wrapper.py diff --git a/script/cpython/gfitoff.py b/script/cpython/gfitoff.py new file mode 100644 index 0000000..31a1e1b --- /dev/null +++ b/script/cpython/gfitoff.py @@ -0,0 +1,30 @@ +import numpy as np +import scipy.optimize + + +def gfitoff(x, y, off=None, amp=None, com=None, sigma=None): + if off is None: + off = y.min() # good enough starting point for offset + + if com is None: + com = x[y.argmax()] + + if amp is None: + amp = y.max() - off + + # For normalised gauss curve sigma=1/(amp*sqrt(2*pi)) + if sigma is None: + surface = np.trapz((y-off), x=x) + sigma = surface / (amp * np.sqrt(2 * np.pi)) + try: + popt, pcov = scipy.optimize.curve_fit(gauss_fn, x, y, p0=[off, amp, com, sigma], method='lm') + popt[3] = abs(popt[3]) # sigma should be returned as positive + except Exception as e: + print("Gauss fitting not successful.\n" + str(e)) + popt = [off, amp, com, abs(sigma)] + + return popt + + +def gauss_fn(x, a, b, c, d): + return a + b * np.exp(-(np.power((x - c), 2) / (2 * np.power(d, 2)))) \ No newline at end of file diff --git a/script/cpython/wrapper.py b/script/cpython/wrapper.py new file mode 100644 index 0000000..c7f6f7c --- /dev/null +++ b/script/cpython/wrapper.py @@ -0,0 +1,9 @@ +from jeputils import * + + +def gfitoff(x, y, off=None, amp=None, com=None, sigma=None): + ret = call_jep("cpython/gfitoff", "gfitoff", [to_npa(x), to_npa(y), off, amp, com, sigma]) + return ret if ret is None or is_list(ret) else ret.data + + +