From ba7b4e23ae0fe13473e51887122b679c877fbcd4 Mon Sep 17 00:00:00 2001 From: Thibault Barnouin Date: Tue, 13 May 2025 15:06:31 +0200 Subject: [PATCH] fit background with gaussion+polynomial --- package/lib/background.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/package/lib/background.py b/package/lib/background.py index 99a09f2..89240c8 100755 --- a/package/lib/background.py +++ b/package/lib/background.py @@ -93,8 +93,10 @@ def display_bkg(data, background, std_bkg, headers, histograms=None, binning=Non max(xmax, np.max(np.array(bins)[np.array(hist) > 5e1]) * convert_flux[0]), ) if coeff is not None: - # ax_h.plot(bins*convert_flux[i], gausspol(bins, *coeff[i]), '--', color="C{0:d}".format(i), alpha=0.8) - ax_h.plot(bins * convert_flux[i], gauss(bins, *coeff[i]), "--", color="C{0:d}".format(i), alpha=0.8) + if len(coeff[i]) == 7: + ax_h.plot(bins * convert_flux[i], gausspol(bins, *coeff[i]), "--", color="C{0:d}".format(i), alpha=0.8) + elif len(coeff[i]) == 3: + ax_h.plot(bins * convert_flux[i], gauss(bins, *coeff[i]), "--", color="C{0:d}".format(i), alpha=0.8) ax_h.set_xscale("log") ax_h.set_yscale("log") ax_h.set_ylim([5e1, np.max([hist.max() for hist in histograms])]) @@ -363,10 +365,10 @@ def bkg_hist(data, error, mask, headers, sub_type=None, subtract_error=True, dis # Fit a gaussian to the log-intensity histogram bins_stdev = binning[-1][hist > hist.max() / 2.0] stdev = bins_stdev[-1] - bins_stdev[0] - # p0 = [hist.max(), binning[-1][np.argmax(hist)], stdev, 1e-3, 1e-3, 1e-3, 1e-3] - p0 = [hist.max(), binning[-1][np.argmax(hist)], stdev] - # popt, pcov = curve_fit(gausspol, binning[-1], hist, p0=p0) - popt, pcov = curve_fit(gauss, binning[-1], hist, p0=p0) + p0 = [hist.max(), binning[-1][np.argmax(hist)], stdev, 1e-3, 1e-3, 1e-3, 1e-3] + popt, pcov = curve_fit(gausspol, binning[-1], hist, p0=p0) + # p0 = [hist.max(), binning[-1][np.argmax(hist)], stdev] + # popt, pcov = curve_fit(gauss, binning[-1], hist, p0=p0) coeff.append(popt) bkg = popt[1] + np.abs(popt[2]) * subtract_error