def fit_skewedgauss(led_esp, led): model = SkewedGaussianModel() params = model.make_params(amplitude=1, center=400, sigma=5, gamma=1) result = model.fit(led_esp[led][:, 1], params, x=led_esp[led][:, 0], nan_policy='propagate') return result
def SkewedGaussianFit(x, y, plot=True): from lmfit.models import SkewedGaussianModel model = SkewedGaussianModel() params = model.make_params(amplitude=1, center=60, sigma=30, gamma=0) # adjust parameters to best fit data. result = model.fit(y, params, x=x) v = result.values plt.plot(x, result.best_fit) return v['center'], v['gamma']
def skewed_gaussian_fit(x, y, title_name): mod = SkewedGaussianModel() params = mod.make_params(amplitude=-2000, center=6562.801, sigma=1, gamma=0) result = mod.fit(y, params, x=x) print(result.fit_report()) #out = mod.fit(y, pars, x=x) plt.figure() plt.plot(x, y) plt.plot(x, result.best_fit) plt.title(title_name) plt.show()
def fit_GC_residual__(x, y, peak, peak_center): mod = SkewedGaussianModel(prefix='peak_') + LinearModel(prefix='bkg_') pars = mod.make_params() pars['peak_amplitude'].value = 1e6 pars['peak_center'].value = peak_center pars['peak_gamma'].value = 4 pars['peak_sigma'].value = 0.4 pars['bkg_intercept'].value = 1e5 # pars['bkg_slope'].value = 500 out = mod.fit(y, pars, x=x) #, iter_cb=per_iteration) if peak == 'H2': #print('Nfev = ', out.nfev) print(out.fit_report()) #print(out.pars['peak_amplitude'].value) return out
def clean(spectra, sigma=2.6): md = np.median(spectra[1]) n = int(len(spectra[0])*0.8) offset = (len(spectra[0])-n)/2 absor = md - min(spectra[1][offset:n-offset]) freq, bin = np.histogram(spectra[1], bins=50, range=(md-absor, md+absor)) rebin = [(bin[b+1]+bin[b])/2 for b in range(len(bin)-1)] gauss = SkewedGaussianModel() pars = gauss.make_params() pars['center'].set(value=md, vary=True) pars['amplitude'].set(vary=True) pars['sigma'].set(vary=True) pars['gamma'].set(vary=True) out = gauss.fit(freq, pars, x=rebin) var = sigma*out.best_values['sigma'] xrbn = np.linspace(rebin[0], rebin[-1], num=100) yrbn = list(out.eval(x=xrbn)) mode = xrbn[yrbn.index(max(yrbn))] xn = np.copy(spectra[0]) yn = np.copy(spectra[1]) ist=0 pts=[] errflg = False for i in range(len(xn)): if (yn[i] > mode+var) and (errflg == False): cnt = 0 errflg = True ist = np.copy(i) cnt += 1 if (yn[i] > mode+var) and (errflg == True): cnt += 1 if (yn[i] < mode+var) and (errflg == True): pts = np.linspace(yn[ist-1], yn[i], cnt+2)[1:-1] for p in range(ist, i): yn[p] = pts[p-ist] errflg = False return np.array([xn, yn])
def clean(spectra, sigma=4.5): rej = [] for i, w in enumerate(spectra[1]): if w <= 0: rej.append(i) spectra[0] = np.delete(spectra[0], rej) spectra[1] = np.delete(spectra[1], rej) md = np.median(spectra[1]) n = int(len(spectra[0]) * 0.8) offset = (len(spectra[0]) - n) / 2 absor = md - min(spectra[1][offset:n - offset]) freq, bin = np.histogram(spectra[1], bins=30, range=(md - absor, md + absor)) rebin = [(bin[b + 1] + bin[b]) / 2 for b in range(len(bin) - 1)] gauss = SkewedGaussianModel() pars = gauss.make_params() pars['center'].set(vary=True) pars['amplitude'].set(vary=True) pars['sigma'].set(vary=True) pars['gamma'].set(vary=True) out = gauss.fit(freq, pars, x=rebin) var = sigma * out.best_values['sigma'] xrbn = np.linspace(rebin[0], rebin[-1], num=100) yrbn = list(out.eval(x=xrbn)) mode = xrbn[yrbn.index(max(yrbn))] xn = np.copy(spectra[0]) yn = np.copy(spectra[1]) var = mode for i, w in enumerate(yn): if w > mode + var: rej.append(i) xn = np.delete(xn, rej) yn = np.delete(yn, rej) return np.array([xn, yn])
def fit(spectra, obj, sigma=2.0, ord=4, iter=4): poly = PolynomialModel(3) pars = poly.make_params() for p in range(4): label = 'c'+str(p) pars[label].set(value=1., vary=True) wkcopy = np.copy(spectra[1]) truesp = [i for i in wkcopy if i > 5] truex = [spectra[0][i] for i in range(len(spectra[1])) if spectra[1][i] > 5] outcont = poly.fit(truesp, pars, x=truex) firstcont = outcont.eval(x=spectra[0]) xn = np.copy(spectra[0]) yn = np.copy(spectra[1])/firstcont pl1=plt.subplot((iter+1)*100+11) pl1.plot(xn, spectra[1], 'k-', linewidth=0.3) pl1.plot(xn, firstcont, 'r-', linewidth=0.6) pl1.set_ylim([0, np.mean(firstcont)*1.5]) for i in range(iter): i_=np.copy(i) niter=str(i_+1) sigma = sigma-i*0.21*sigma md = np.median(yn) n = len([i for i in yn if i > 0.1]) offset = (len(xn)-n)/2 absor = md - min(yn[offset:n-offset]) freq, bin = np.histogram(yn, bins=50, range=(md-absor, md+absor)) rebin = [(bin[b+1]+bin[b])/2 for b in range(len(bin)-1)] gauss = SkewedGaussianModel() pars = gauss.make_params() pars['center'].set(value=md, vary=True) pars['amplitude'].set(vary=True) pars['sigma'].set(vary=True) pars['gamma'].set(vary=True) out = gauss.fit(freq, pars, x=rebin) var = sigma*out.best_values['sigma'] xrbn = np.linspace(rebin[0], rebin[-1], num=100) yrbn = list(out.eval(x=xrbn)) mode = xrbn[yrbn.index(max(yrbn))] ync = np.copy(spectra[1]) xnc = np.copy(spectra[0]) mask = [] for j in range(len(yn)): if (yn[j] > mode+var/2) or (yn[j] < mode-var/2): mask.append(False) else: mask.append(True) mask = np.array(mask) ync = ync[mask] xnc = xnc[mask] poly2 = PolynomialModel(ord) pars2 = poly2.make_params() for p in range(ord+1): label = 'c'+str(p) pars2[label].set(value=1., vary=True) outcont2 = poly2.fit(ync, pars2, x=xnc) contf = outcont2.eval(x=xn) yn = spectra[1]/contf err = spectra[2]/contf pln=plt.subplot(int((iter+1)*100+10+(i_+2))) pln.plot(xn, yn*(np.mean(contf)*0.8), 'k-', linewidth=0.3) pln.plot(xnc, ync, 'r-', linewidth=0.3) pln.plot(xn, contf, 'b-', linewidth=0.6) pln.set_ylim([0, np.mean(contf)*1.2]) plt.savefig(obj[0]+'_fit.png', dpi=300) plt.clf() return np.array([xn, yn, err])
def fit(spectra, sigma=4.0, ptreg=8., ord=4, iter=2): rej = [] for i, w in enumerate(spectra[1]): if w <= 0: rej.append(i) spectra[0] = np.delete(spectra[0], rej) spectra[1] = np.delete(spectra[1], rej) # prepare first kick poly = PolynomialModel(3) pars = poly.make_params() for p in range(4): label = 'c' + str(p) pars[label].set(value=1., vary=True) wkcopy = np.copy(spectra[1]) truesp = [i for i in wkcopy if i >= 0] truex = [ spectra[0][i] for i in range(len(spectra[1])) if spectra[1][i] >= 0 ] outcont = poly.fit(truesp, pars, x=truex) firstcont = outcont.eval(x=spectra[0]) xn = np.copy(spectra[0]) yn = np.copy(spectra[1]) / firstcont # start cont. cleaning iterations for i in range(iter): i_ = np.copy(i) niter = str(i_ + 1) sigma = sigma - i * 0.21 * sigma md = np.median(yn) n = len([i for i in yn if i > 0.1]) offset = (len(xn) - n) / 2 absor = md - min(yn[offset:n - offset]) freq, bin = np.histogram(yn, bins=50, range=(md - absor, md + absor)) rebin = [(bin[b + 1] + bin[b]) / 2 for b in range(len(bin) - 1)] gauss = SkewedGaussianModel() pars = gauss.make_params() pars['center'].set(vary=True) pars['amplitude'].set(vary=True) pars['sigma'].set(vary=True) pars['gamma'].set(vary=True) out = gauss.fit(freq, pars, x=rebin) var = sigma * out.best_values['sigma'] xrbn = np.linspace(rebin[0], rebin[-1], num=100) yrbn = list(out.eval(x=xrbn)) mode = xrbn[yrbn.index(max(yrbn))] # clean cont. ync = np.copy(spectra[1]) xnc = np.copy(spectra[0]) mask = [] for j in range(len(yn)): if (yn[j] > mode + var / 2) or (yn[j] < mode - var / 2): mask.append(False) else: mask.append(True) mask = np.array(mask) ync = ync[mask] xnc = xnc[mask] # re-fitting poly2 = PolynomialModel(ord) pars2 = poly2.make_params() for p in range(ord + 1): label = 'c' + str(p) pars2[label].set(value=1., vary=True) try: outcont2 = poly2.fit(ync, pars2, x=xnc) except: plt.plot(xn, yn, 'k-') plt.plot([xn[0], xn[-1]], [mode, mode], 'b-') plt.plot([xn[0], xn[-1]], [mode + var / 2, mode + var / 2], 'r-') plt.plot([xn[0], xn[-1]], [mode - var / 2, mode - var / 2], 'r-') plt.show() contf = outcont2.eval(x=xn) yn = spectra[1] / contf clspec = [xnc, ync] # start slicing firstv = clspec[0][0] wavrange = clspec[0][-1] - firstv sliceno = wavrange / ptreg slisize = wavrange / sliceno points = [[], []] # continuum point definition for s in range(int(sliceno)): i = bissec(clspec[0], firstv + s * slisize) f = bissec(clspec[0], firstv + (s + 1) * slisize) slc = [clspec[0][i:f], clspec[1][i:f]] if len(slc[1]) > 2.: md = np.median(slc[1]) absor = min(slc[1]) high = max(slc[1]) freq, bin = np.histogram(slc[1], bins=20, range=(absor, high)) rebin = [(bin[b + 1] + bin[b]) / 2 for b in range(len(bin) - 1)] fmode = rebin[list(freq).index(max(freq))] fsigma = rebin[-1] - rebin[0] gauss = GaussianModel() pars = gauss.make_params() pars['center'].set(value=fmode, vary=True) pars['amplitude'].set(value=max(freq), vary=True) pars['sigma'].set(value=fsigma, vary=True) out = gauss.fit(freq, pars, x=rebin) xrbn = np.linspace(rebin[0], rebin[-1], num=100) yrbn = list(out.eval(x=xrbn)) mode = xrbn[yrbn.index(max(yrbn))] xp = slc[0][len(slc[0]) / 2] points[0].append(xp) points[1].append(mode) spline = splrep(points[0], points[1], k=3) contx = splev(clspec[0], spline) continuum = splev(spectra[0], spline) return [spectra[0], spectra[1] / continuum]