def gaussFit(data): # Create x-axis xaxis = np.arange(data.size) # Estimate parameters params_estimate = GaussFit.gauss_params_estimate(data) print "Estimate:", params_estimate print "Fitting" fit_params, fit_cov = GaussFit.curve_fit(GaussFit.gauss, xaxis, data, params_estimate) fit_params_error = np.sqrt(np.diag(fit_cov)) #print "Final Fit(errors):",zip(fit_params,fit_params_error) #print "Covariance Matrix:",fit_cov combinedvals = zip(fit_params, fit_params_error) print "Fit Values(errors):" print "mean:", combinedvals[0], print "sigma:", combinedvals[1], print "Height:", combinedvals[2], print "Pedestal:", combinedvals[3] return fit_params, fit_params_error
def gaussFit(data): # Create x-axis xaxis = np.arange(data.size) # Estimate parameters params_estimate = GaussFit.gauss_params_estimate(data) print "Estimate:", params_estimate print "Fitting" fit_params, fit_cov = GaussFit.curve_fit(GaussFit.gauss, xaxis, data, params_estimate) fit_params_error = np.sqrt(np.diag(fit_cov)) fit_results = { 'mean': fit_params[0], 'sigma': fit_params[1], 'height': fit_params[2], 'pedestal': fit_params[3], 'mean_error': fit_params_error[0], 'sigma_error': fit_params_error[1], 'height_error': fit_params_error[2], 'pedestal_error': fit_params_error[3] } return fit_results
def image(*arg): print "calling custom image func" print len(arg), arg ncol = 0 nrow = 0 if len(arg) == 2: ncol = arg[0] nrow = arg[1] elif len(arg[0]) == 2: ncol = arg[0][0] nrow = arg[0][1] elif len(arg[0][0]) == 2: ncol = arg[0][0][0] nrow = arg[0][0][1] # ncol = arg[0][0][0] # nrow = arg[0][0][1] gauss_data = GaussFit.gauss(np.arange(ncol), ncol / 2.0, ncol / 10.0, 100.0, 10.0) return np.tile(gauss_data, (nrow, 1))
def projection(mirror_pos, xvals): """ Function to emulate projection from EXS_OPAL when mono is in first order Simulate FEL spectrum as sum of Gaussians spread over CCD, whose width is dictated by the mirror position, with random mean and height. Number of gaussians is also random """ # get length of xvals len_xvals = len(xvals) # Generate random number of spikes n_spikes = int(spike_number * ((np.random.random(1) * 2.0 )-1.0) * 0.1) \ + (spike_number/2.0) # Create random position of the spikes of length n_spikes mean_list = np.random.random(n_spikes) * len_xvals # Create random heights of the spikes height_list = np.random.random(n_spikes) * spike_height sim_projection = np.zeros(len_xvals) for mean, height in zip(mean_list, height_list): sim_projection += (GaussFit.gauss(xvals, mean, width(mirror_pos), height, spike_pedestal)) # Add noise sim_projection += np.random.normal(0.0, 1.0, len_xvals) * spike_noise return sim_projection
def fit_focus_quality_vs_mirror(self): self.__logger.info("Fit measured sharpness vs mirror position") #self.__logger.info("Fit to a quadratic, and find maximum") #coeff = np.polyfit(self.mirror_pos_log, self.focus_quality_log,2) # Find zero gradiant -- optimial mirror position #optimalPos = -coeff[1] / (2.0 * coeff[0]) # Check this is a maxima #if (coeff[0] > 0.0) : # self.__logger.error("FOUND A MINIMA RATHER THAN MAXIMA !!") self.__logger.info("Fit to a gaussian and find maximum") # Fit data to gaussian fit_params = GaussFit.GaussFit(np.array(self.focus_quality_log), np.array(self.mirror_pos_log)) print fit_params # Update plot fitplot = GaussFit.gauss(self.mirror_pos_log, mean=fit_params['mean'], sigma=fit_params['sigma'], height=fit_params['height'], pedestal=fit_params['pedestal']) self.ax.plot(self.mirror_pos_log, fitplot, "b") self.fig.canvas.draw() # Use mean as optimal position optimalPos = fit_params['mean'] # Calculate optimal grating position gradient = \ (self.grating_pos_log[-1]-self.grating_pos_log[0]) / (self.mirror_pos_log[-1]-self.mirror_pos_log[0]) delta_mirrorpos = optimalPos - self.mirror_pos_log[0] optimal_grating = self.grating_pos_log[0] + (gradient * delta_mirrorpos) # Set the optimal values self.best_mirror_pos = optimalPos self.best_grating_pos = optimal_grating
def fit_gauss(mirrorpos, sharpness): """ Fit mirror position vs sharpness to a Gaussian to find optimal mirror position """ # Estimate parameters params_estimate = GaussFit.gauss_params_estimate(sharpness) # Translate mean and sigma from array index units to mirror # position units gradient = (mirrorpos[-1] - mirrorpos[0]) / float(mirrorpos.size) params_estimate[0] = mirrorpos[0] + (params_estimate[0] * gradient) params_estimate[1] = params_estimate[1] * gradient # Fit to Gaussian fit_params, fit_cov = GaussFit.curve_fit(GaussFit.gauss, mirrorpos, sharpness, params_estimate) fit_params_error = np.sqrt(np.diag(fit_cov)) # Return the fitted mean position return fit_params[0], GaussFit.gaus(mirrorpos, *fit_params)
def __measure_fwhm(self,data) : """ Fit data to Gaussian+pedestal and return FWHM """ self.__logger.info("Fit data and measure FWHM") fit_results = GaussFit.GaussFit(data) self.__logger.info("Fit Results: %s"%fit_results) fit_plot = GaussFit.gauss(np.arange(data.size), mean=fit_results['mean'], sigma=fit_results['sigma'], height=fit_results['height'], pedestal=fit_results['pedestal']) self.__fitplot.set_ydata(fit_plot) self.__fig.canvas.draw() fwhm = 2.35 * fit_results['sigma'] self.__logger.info("FWHM: %f"%fwhm) return fwhm
def projection(mirror_pos, xvals): """ Function to emulate projection from EXS_OPAL Use a gaussian + noise """ # Create gaussian to simulate projection gauss_data = GaussFit.gauss(xvals, mean, width(mirror_pos), height, pedestal) # Add noise to data gauss_data += np.random.normal(0.0, 1.0, len(xvals)) * noise return gauss_data
print projections_store.shape # Average first 100 to define cuts for self-seeding self_seeded = np.sum(projections_store[:100], axis=0) / 100.0 print self_seeded.shape # Now fit --> should pick up largest peak, which should be the # self-seeded peak print "Fitting average of first 100 shots" self_seed_params = gaussFit(self_seeded) print self_seed_params xaxis = np.arange(1024) plt.plot(self_seeded) plt.plot( GaussFit.gauss(xaxis, self_seed_params['mean'], self_seed_params['sigma'], self_seed_params['height'], self_seed_params['pedestal'])) plt.show() # Use mean+/-3*sigma to define cut self_seed_lower_cut = self_seed_params['mean'] - (2.35 * self_seed_params['sigma']) if self_seed_lower_cut < 0.0: self_seed_lower_cut = 0.0 self_seed_upper_cut = self_seed_params['mean'] + (2.35 * self_seed_params['sigma']) if self_seed_upper_cut > 1023.0: self_seed_upper_cut = 1023.0 print self_seed_lower_cut print self_seed_upper_cut # Use self-seeded height to define minimum height to cut off SASE peaks
mirror_pos = np.arange(-10.0, 10.0, 0.5) avg_sharpness = [] std_sharpness = [] for m in mirror_pos: theSharpness = [] for i in range(10): spectrum = projection(m, xvals) theSharpness.append(sharpness(spectrum)) avg_sharpness.append(np.mean(theSharpness)) std_sharpness.append(np.std(theSharpness)) # Fit gaussian to avg_sharpness fit_params = GaussFit.GaussFit(np.array(avg_sharpness), mirror_pos) print fit_params fit_plot = GaussFit.gauss(mirror_pos, mean=fit_params['mean'], sigma=fit_params['sigma'], height=fit_params['height'], pedestal=fit_params['pedestal']) #plt.clf() #plt.plot(spectrum) #plt.draw() #print proj_list #plt.clf()