this cell takes the average spectrum from a map and performs a baseline subtraction; prepatory step for gaussian fits this is also a useful step to check the arpls parameters and how they'll construct a baseline ''' import sys sys.path.append(r'C:\Users\triton\xrays\python') from baseline_algorithms import arpls from scipy import optimize # construct and subtract baseline from average spectrum #for Au side spectra, 0hr baseline_arpls = arpls(z_average, 1e5, 0.01) zaverage_arpls = z_average - baseline_arpls # check subtraction plt.plot(energy, z_average) plt.plot(energy, zaverage_arpls) #%% ''' this cell fits average spectrum of Au side with single gaussian and plot the results ''' def gaussian_fit(x, *pars): offset = pars[0]
def multi_gaussian(x, *pars): offset = pars[0] g1 = gaussian2(x, pars[1], pars[2], pars[3]) g2 = gaussian2(x, pars[4], pars[5], pars[6]) return g1 + g2 + offset import sys sys.path.append(r'C:\Users\triton\xrays\python') from baseline_algorithms import arpls from scipy import optimize # construct and subtract baseline from average spectrum # for glass side spectra baseline_arpls = arpls(z_average, 1e7, 0.1) zaverage_arpls = z_average - baseline_arpls # check subtraction plt.plot(energy, z_average) plt.plot(energy, zaverage_arpls) #%% ''' this cell fits average spectrum of glass side with single gaussian and plot the results ''' def gaussian_plot(x, A, x0, sig): return A * np.exp(-(x - x0)**2 / (2 * sig**2))
y = np.shape(spectra)[0] x = np.shape(spectra)[1] spectra_ravel = spectra.reshape((x*y),z) if aborted_map == 1: spectra_ravel = spectra.copy() # construct array to store gaussian fit parameters # the no. of rows (4) correspond to the number of optimization parameters # from the optimize.curve_fit function: offset, A, x0, sig # the no. of columns correspond to the number of pixels or spectra measured pixels = np.shape(spectra_ravel)[0] stored_gauss_params = np.zeros((pixels,7)) for pix, spectrum in enumerate(spectra_ravel): # construct and subtract baseline from pixel spectrum baseline_arpls = arpls(spectrum, 1e6, 0.01) spectrum_arpls = spectrum - baseline_arpls # fit gaussian to background-subtracted spectrum popt, pcov = optimize.curve_fit(multi_gaussian_fit, energy, spectrum_arpls, guess) # store gaussian fit values; popt needs to be transposed # since optimize.curve_fit outputs as (4,1) array stored_gauss_params[pix,:] = popt.T end = time.time() t = end-start t_str = "%.3f" % t message = 'time to complete: {i}s'.format(i=str(t_str)) print(message) #%% SAVE = 0 if SAVE == 1: