# define the initial parameters fit_params = Parameters() if lineshape == 'pseudovoigt': cen = newbin_axis[np.unravel_index(newhist.argmax(), newhist.shape)] fit_params.add('amp_1', value=50000, min=100, max=1000000) fit_params.add('cen_1', value=cen, min=cen - 0.2, max=cen + 0.2) fit_params.add('sig_1', value=0.1, min=0.01, max=0.5) fit_params.add('ratio_1', value=0.5, min=0, max=1) # run the fit result = minimize(util.objective_lmfit, fit_params, args=(fit_axis, fit_hist, lineshape)) report_fit(result.params) y_fit = util.function_lmfit(params=result.params, iterator=0, x_axis=newbin_axis, distribution=lineshape) else: y_fit = None result = None ################################## # plot the histogram and the fit # ################################## fig, ax = plt.subplots(1, 1) plt.plot(bin_axis, hist, 'o', newbin_axis, newhist, '-') if histogram_Yaxis == 'log': ax.set_yscale('log') if fit: if histogram_Yaxis == 'linear': ax.plot(newbin_axis, y_fit, '-')
line, = axes_dict[idx][0].plot( linecuts[idx, width_length // 2 - roi_width:width_length // 2 + roi_width], # x axis linecuts[idx + 3, width_length // 2 - roi_width:width_length // 2 + roi_width], # cut 'ro', fillstyle='none') line.set_label(f'cut along {axes_dict[idx][1]}') fit_x_axis = np.linspace( linecuts[idx, width_length // 2 - roi_width:width_length // 2 + roi_width].min(), linecuts[idx, width_length // 2 - roi_width:width_length // 2 + roi_width].max(), num=200) y_fit = util.function_lmfit(params=minimization.params, iterator=idx, x_axis=fit_x_axis, distribution='gaussian') line, = axes_dict[idx][0].plot(fit_x_axis, y_fit, 'b-') sig_name = f'sig_{idx+1}' line.set_label( f'fit, FWHM={2*np.sqrt(2*np.log(2))*minimization.params[sig_name].value:.2f}nm' ) gu.savefig(savedir=savedir, figure=fig, axes=(ax0, ax1, ax2), tick_width=tick_width, tick_length=tick_length, tick_labelsize=16, xlabels='width (nm)', ylabels='psf (a.u.)',
fit_params, args=(combined_xaxis, combined_data, lineshape)) report_fit(result.params) ##################### # plot data and fit # ##################### fig, ax = plt.subplots(1, 1) if scale == 'linear': ax.plot(distances, average, 'r') else: ax.plot(distances, np.log10(average), 'r') plt.legend(['data']) for idx in range(nb_ranges): y_fit = util.function_lmfit(params=result.params, iterator=idx, x_axis=distances, distribution=lineshape) if scale == 'linear': ax.plot(distances, y_fit, '-') else: ax.plot(distances, np.log10(y_fit), '-') ax.set_xlim(xlim[0], xlim[1]) ax.set_ylim(ylim[0], ylim[1]) ax.set_xlabel('q (1/nm)') ax.set_ylabel('Angular average (A.U.)') try: fig.text(0.15, 0.95, 'cen_1 = ' + str('{:.5f}'.format(result.params['cen_1'].value)) + '+/-' + str('{:.5f}'.format(result.params['cen_1'].stderr)) + ' sig_1 = ' +
fit_params.add('loc_1', value=0, min=-0.1, max=0.1) fit_params.add('sig_1', value=0.0005, min=0.0000001, max=0.1) fit_params.add('alpha_1', value=0, min=-10, max=10) else: # 'pseudovoigt' fit_params.add('amp_1', value=0.0005, min=0.000001, max=10000) fit_params.add('cen_1', value=0, min=-0.1, max=0.1) fit_params.add('sig_1', value=0.0005, min=0.0000001, max=0.1) fit_params.add('ratio_1', value=0.5, min=0, max=1) # run the global fit to all the data sets result = minimize(util.objective_lmfit, fit_params, args=(x_axis, hist, fit_pdf)) report_fit(result.params) strain_fit = util.function_lmfit(params=result.params, x_axis=x_axis, distribution=fit_pdf) if fit_pdf == 'skewed_gaussian': # find the position of the mode (maximum of the pdf) x_mode = np.unravel_index(strain_fit.argmax(), x_axis.shape) step = x_axis[x_mode] - x_axis[x_mode[0] - 1] fine_x = np.copy(x_axis) for idx in range(2): fine_x = np.linspace(fine_x[x_mode] - step, fine_x[x_mode] + step, endpoint=True, num=1000) fine_y = util.function_lmfit(params=result.params, x_axis=fine_x, distribution=fit_pdf) diff_fit = np.gradient(fine_y, fine_x[1] - fine_x[0])