def single_peak_fit(self, hist, lower_threshold, upper_threshold, mean, zero_peak_gaussian=None): lower_slot = 0 for slot in hist.slot_centers(): lower_slot += 1 if slot > lower_threshold: break upper_slot = 0 for slot in hist.slot_centers(): upper_slot += 1 if slot > upper_threshold: break x = hist.slot_centers() y = hist.slots().as_double() starting_gaussians = [curve_fitting.gaussian( a=flex.max(y[lower_slot:upper_slot]), b=mean, c=3)] # print starting_gaussians #mamin: fit gaussian will take the maximum between starting point (lower_slot) and ending (upper_slot) as a if zero_peak_gaussian is not None: y -= zero_peak_gaussian(x) if 1: fit = curve_fitting.lbfgs_minimiser( starting_gaussians, x[lower_slot:upper_slot], y[lower_slot:upper_slot]) sigma = abs(fit.functions[0].params[2]) if sigma < 1 or sigma > 10: if flex.sum(y[lower_slot:upper_slot]) < 15: #mamin I changed 15 to 5 # No point wasting time attempting to fit a gaussian if there aren't any counts #raise PixelFitError("Not enough counts to fit gaussian") return fit print "using cma_es:", sigma fit = curve_fitting.cma_es_minimiser( starting_gaussians, x[lower_slot:upper_slot], y[lower_slot:upper_slot]) else: fit = curve_fitting.cma_es_minimiser( starting_gaussians, x[lower_slot:upper_slot], y[lower_slot:upper_slot]) return fit
def interpolate(x, y, half_window=10): perm = flex.sort_permutation(x) x = x.select(perm) y = y.select(perm) x_all = flex.double() y_all = flex.double() for i in range(x.size()): x_all.append(x[i]) y_all.append(y[i]) if i < x.size()-1 and (x[i+1] - x[i]) > 1: window_left = min(half_window, i) window_right = min(half_window, x.size() - i) x_ = x[i-window_left:i+window_right] y_ = y[i-window_left:i+window_right] from scitbx.math import curve_fitting # fit a 2nd order polynomial through the missing points polynomial = curve_fitting.univariate_polynomial(1, 1) fit = curve_fitting.lbfgs_minimiser([polynomial], x_,y_).functions[0] missing_x = flex.double(range(int(x[i]+1), int(x[i+1]))) x_all.extend(missing_x) y_all.extend(fit(missing_x)) perm = flex.sort_permutation(x_all) x_all = x_all.select(perm) y_all = y_all.select(perm) return x_all, y_all
def single_peak_fit(self, hist, lower_threshold, upper_threshold, mean, zero_peak_gaussian=None): lower_slot = 0 for slot in hist.slot_centers(): lower_slot += 1 if slot > lower_threshold: break upper_slot = 0 for slot in hist.slot_centers(): upper_slot += 1 if slot > upper_threshold: break x = hist.slot_centers() y = hist.slots().as_double() starting_gaussians = [ curve_fitting.gaussian(a=flex.max(y[lower_slot:upper_slot]), b=mean, c=3) ] # print starting_gaussians #mamin: fit gaussian will take the maximum between starting point (lower_slot) and ending (upper_slot) as a if zero_peak_gaussian is not None: y -= zero_peak_gaussian(x) if 1: fit = curve_fitting.lbfgs_minimiser(starting_gaussians, x[lower_slot:upper_slot], y[lower_slot:upper_slot]) sigma = abs(fit.functions[0].params[2]) if sigma < 1 or sigma > 10: if flex.sum(y[lower_slot:upper_slot] ) < 15: #mamin I changed 15 to 5 # No point wasting time attempting to fit a gaussian if there aren't any counts #raise PixelFitError("Not enough counts to fit gaussian") return fit print "using cma_es:", sigma fit = curve_fitting.cma_es_minimiser(starting_gaussians, x[lower_slot:upper_slot], y[lower_slot:upper_slot]) else: fit = curve_fitting.cma_es_minimiser(starting_gaussians, x[lower_slot:upper_slot], y[lower_slot:upper_slot]) return fit