From 0eff84910bc7d4f69bdc94d9c778a3a7c33b92b9 Mon Sep 17 00:00:00 2001 From: robrobo Date: Thu, 17 Jul 2025 18:44:20 +0200 Subject: [PATCH] simplified quick non gaussian fit function so that it is more robust --- src/mdevaluate/utils.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/mdevaluate/utils.py b/src/mdevaluate/utils.py index 9bdbf2e..16baea4 100644 --- a/src/mdevaluate/utils.py +++ b/src/mdevaluate/utils.py @@ -357,14 +357,15 @@ def quick1etau(t: ArrayLike, C: ArrayLike, n: int = 7) -> float: return tau_est -def quicknongaussfit(t, C, width=4): +def quicknongaussfit(t, C, width=2): """ Estimates the time and height of the peak in the non-Gaussian function. C is C(t) the correlation function """ - # TODO this is a very experimental interpolation, can fail - def ppoly(x,a,b,c,d,e,A,mu,sig): - return A*np.exp(-(x - mu)**2 / (2 * sig**2))+a+(b*x+e)*1/(1+np.exp(c*(x-d))) + def ffunc(t,y0,A_main,log_tau_main,sig_main): + main_peak = A_main*np.exp(-(t - log_tau_main)**2 / (2 * sig_main**2)) + return y0 + main_peak + # first rough estimate, the closest time. This is returned if the interpolation fails! tau_est = t[np.argmax(C)] nG_max = np.amax(C) @@ -375,13 +376,11 @@ def quicknongaussfit(t, C, width=4): tau = time[np.argmax(corr)] mask = (time>tau-width/2) & (time 0].min() + guess = [nG_min, nG_max-nG_min, tau, 0.6] + popt = curve_fit(ffunc, time, corr, p0=guess, maxfev=10000)[0] + tau_est = 10**popt[-2] + nG_max = popt[0] + popt[1] except: pass if np.isnan(tau_est):