def fit_composit(seq, *params, report=False): x = np.arange(len(seq)) y = seq amp, cen, wid = params cmodel = CompositeModel(Model(gaussian), Model(LognormalModel), ExponentialGaussianModel) pars = cmodel.make_params(amplitude=amp, center=cen, sigma=wid, mid=cen) # 'mid' and 'center' should be completely correlated, and 'mid' is # used as an integer index, so a very poor fit variable: pars['mid'].vary = False # fit this model to data array y result = cmodel.fit(y, params=pars, x=x) # limit the amplitude to be in 0-256 cmodel.set_param_hint('amp', min=0) cmodel.set_param_hint('amp', max=256) result = gmodel.fit(y, x=x, amp=amp, cen=cen, wid=wid) if result.redchi >= 1e3: # if report: plt.figure() plt.plot(x, y, 'bo') # plt.plot(x, result.init_fit, 'k--') plt.plot(x, np.ceil(result.best_fit), 'r-') plt.title('chi2: {0:.2e} - red_chi2: {0:.2e}'.format( result.chisqr, result.redchi)) plt.show() return result
def fit_gaussian_peak_step_background_2(x, y): # create data from broadened step npts = 201 x = np.linspace(0, 10, npts) y = step(x, amplitude=12.5, center=4.5, sigma=0.88, form='erf') y = y + np.random.normal(size=npts, scale=0.35) # create Composite Model using the custom convolution operator mod = CompositeModel(Model(jump), Model(gaussian), convolve) pars = mod.make_params(amplitude=1, center=3.5, sigma=1.5, mid=5.0) # 'mid' and 'center' should be completely correlated, and 'mid' is # used as an integer index, so a very poor fit variable: pars['mid'].vary = False # fit this model to data array y result = mod.fit(y, params=pars, x=x) print(result.fit_report()) plot_components = True # plot results plt.plot(x, y, 'bo') if plot_components: # generate components comps = result.eval_components(x=x) plt.plot(x, 10 * comps['jump'], 'k--') plt.plot(x, 10 * comps['gaussian'], 'r-') else: plt.plot(x, result.init_fit, 'k--') plt.plot(x, result.best_fit, 'r-') plt.show() # #<end examples/model_doc3.py> return fit_fwhm, fit_center, fwhm_err
# Create model for the fit gmodel = CompositeModel(Model(irf_gate), Model(model_2lorentzians), convolve) print('Names of parameters:', gmodel.param_names) print('Independent variable(s):', gmodel.independent_vars) # Load reference data - extract x and y values two_lorentzians_iris = np.loadtxt(path_to_data + 'data_2lorentzians.dat') xx = two_lorentzians_iris[:, 0] yy = two_lorentzians_iris[:, 1] # Fit result = gmodel.fit(yy, x=xx, scale1=1., center1=0., hwhm1=0.25, scale2=1., center2=1., hwhm2=0.25) fig1, ax1 = plt.subplots() ax1.plot(xx, yy, '+', label='experimental data') ax1.plot(xx, result.init_fit, 'k--', label='model with initial guesses') ax1.legend() ax1.set( xlabel='Energy transfer (meV)', title='Plot before fitting: experimental data and mode with initial guesses' ) ax1.grid() # display result
tmp = np.concatenate((pad*arr[0], arr, pad*arr[-1])) out = np.convolve(tmp, kernel, mode='valid') noff = int((len(out) - npts)/2) return out[noff:noff+npts] # # create Composite Model using the custom convolution operator mod = CompositeModel(Model(jump), Model(gaussian), convolve) pars = mod.make_params(amplitude=1, center=3.5, sigma=1.5, mid=5.0) # 'mid' and 'center' should be completely correlated, and 'mid' is # used as an integer index, so a very poor fit variable: pars['mid'].vary = False # fit this model to data array y result = mod.fit(y, params=pars, x=x) print(result.fit_report()) plot_components = False # plot results plt.plot(x, y, 'bo') if plot_components: # generate components comps = result.eval_components(x=x) plt.plot(x, 10*comps['jump'], 'k--') plt.plot(x, 10*comps['gaussian'], 'r-') else: plt.plot(x, result.init_fit, 'k--') plt.plot(x, result.best_fit, 'r-')
radius=ini_values['radius'], DR=ini_values['DR']) # Q-independent parameters if i == 0: D_value = params['D'].value resTime_value = params['resTime'].value radius_value = params['radius'].value DR_value = params['DR'].value else: params['D'].set(value=D_value) params['resTime'].set(value=resTime_value) params['radius'].set(value=radius_value) params['DR'].set(value=DR_value) result_fit[i] = model.fit(f_5A['y'][i], params, w=f_5A['x'][i]) # display result for i in range(nb_q_values): print(f'Result of fit {i}:\n', result_fit[i].fit_report()) # plot results using lmfit's features for i in range(nb_q_values): result_fit[i].plot() # other option to plot: experimental data, initial fitting model and fitted model for each spectrum for indx in range(nb_q_values): fig1, ax1 = plt.subplots() ax1.plot(f_5A['x'][indx], f_5A['y'][indx], 'bo', label='exp') ax1.plot(f_5A['x'][indx], result_fit[indx].init_fit, 'k--', label='ini')
noff = int((len(out) - npts) / 2) return out[noff:noff + npts] # # create Composite Model using the custom convolution operator mod = CompositeModel(Model(jump), Model(gaussian), convolve) pars = mod.make_params(amplitude=1, center=3.5, sigma=1.5, mid=5.0) # 'mid' and 'center' should be completely correlated, and 'mid' is # used as an integer index, so a very poor fit variable: pars['mid'].vary = False # fit this model to data array y result = mod.fit(y, params=pars, x=x) print(result.fit_report()) plot_components = False # plot results plt.plot(x, y, 'bo') if plot_components: # generate components comps = result.eval_components(x=x) plt.plot(x, 10 * comps['jump'], 'k--') plt.plot(x, 10 * comps['gaussian'], 'r-') else: plt.plot(x, result.init_fit, 'k--') plt.plot(x, result.best_fit, 'r-')