def fit_individual_ions(self, img_1d, ion_edges): from lmfit.models1d import GaussianModel centers = [] for i, (xmin, xmax) in enumerate(ion_edges): model = GaussianModel() x = np.arange(xmin, xmax, 1) y = img_1d[xmin:xmax] model.guess_starting_values(y, x=x) init_fit = model.model(x=x) model.fit(y, x=x) final_fit = model.model(x=x) centers.append(model.params['center'].value) return centers
""" Example using the built-in Peak-like models """ import numpy as np from lmfit.models1d import GaussianModel, LorentzianModel, VoigtModel import matplotlib.pyplot as plt x = np.linspace(0, 10, 101) sca = 1./(2.0*np.sqrt(2*np.pi)) noise = 5e-2*np.random.randn(len(x)) dat = 2.60 -0.04*x + 7.5 * np.exp(-(x-4.0)**2 / (2*0.35)**2) + noise mod = GaussianModel(background='linear') # mod = VoigtModel(background='linear') # mod = LorentzianModel(background='linear') mod.guess_starting_values(dat, x) plt.plot(x, dat) # initial guess plt.plot(x, mod.model(x=x) + mod.calc_background(x), 'r+') mod.fit(dat, x=x) print mod.fit_report() # best fit plt.plot(x, mod.model(x=x) + mod.calc_background(x))
model = VoigtModel(background='linear') # get default starting values, but then alter them model.guess_starting_values(y, x=x) model.params['amplitude'].value = 2.0 init_fit = model.model(x=x) # the actual fit model.fit(y, x=x) print model.fit_report(min_correl=0.25) vfit = model.model(x=x) mod2 = GaussianModel(background='linear') mod2.fit(y, x=x) gfit = mod2.model(x=x) print mod2.fit_report(min_correl=0.25) print 'Voigt Sum of Squares: ', ((vfit - y)**2).sum() print 'Gaussian Sum of Squares: ', ((gfit - y)**2).sum() plt.plot(x, vfit, 'r-') plt.plot(x, gfit, 'b-') plt.plot(x, y, 'bo') plt.show()
import numpy as np from lmfit.models1d import GaussianModel import matplotlib.pyplot as plt data = np.loadtxt('model1d_gauss.dat') x = data[:, 0] y = data[:, 1] model = GaussianModel() model.guess_starting_values(y, x=x) # model.params['amplitude'].value=6.0 init_fit = model.model(x=x) model.fit(y, x=x) print model.fit_report(min_correl=0.25) final_fit = model.model(x=x) plt.plot(x, final_fit, 'r-') plt.plot(x, init_fit, 'k--') plt.plot(x, y, 'bo') plt.show()
sig = 0.47 amp = 12.00 cen = 5.66 eps = 0.15 off = 9 slo = 0.2 sca = 1.0 / (2.0 * np.sqrt(2 * np.pi)) / sig noise = eps * np.random.randn(len(x)) dat = off + slo * x + amp * sca * np.exp(-(x - cen) ** 2 / (2 * sig) ** 2) + noise # mod = ExponentialModel(background='linear') # mod = LinearModel() mod = GaussianModel(background="quad") mod = VoigtModel(background="quad") mod = LorenztianModel(background="quad") mod.guess_starting_values(dat, x, negative=False) mod.params["bkg_offset"].value = min(dat) init = mod.model(x=x) + mod.calc_background(x) mod.fit(dat, x=x) print mod.fit_report() fit = mod.model(x=x) + mod.calc_background(x) plt.plot(x, dat) plt.plot(x, init)
sig = 0.47 amp = 12.00 cen = 5.66 eps = 0.15 off = 9 slo = 0.0012 sca = 1. / (2.0 * np.sqrt(2 * np.pi)) / sig noise = eps * np.random.randn(len(x)) dat = off + slo * x + amp * sca * np.exp(-(x - cen)**2 / (2 * sig)**2) + noise # mod = ExponentialModel(background='linear') # mod = LinearModel() mod = GaussianModel(background='quad') mod = VoigtModel(background='quad') mod = LorenztianModel(background='quad') mod.guess_starting_values(dat, x, negative=False) mod.params['bkg_offset'].value = min(dat) init = mod.model(x=x) + mod.calc_background(x) mod.fit(dat, x=x) print mod.fit_report() fit = mod.model(x=x) + mod.calc_background(x) plt.plot(x, dat) plt.plot(x, init) plt.plot(x, fit)
import numpy as np from lmfit.models1d import GaussianModel import matplotlib.pyplot as plt data = np.loadtxt('model1d_gauss.dat') x = data[:, 0] y = data[:, 1] model = GaussianModel() # background='linear' # model.guess_starting_values(y, x, negative=False) # model.params['bkg_offset'].value=min(y) init_fit = model.model(x=x) + model.calc_background(x) model.fit(y, x=x) print model.fit_report() final_fit = model.model(x=x) plt.plot(x, y) plt.plot(x, init_fit) plt.plot(x, final_fit) plt.show()