def SPErough(data, n_channel_s): fit_input, bins, check = SPE_input(data) amplitude = get_amp(data) gauss = GaussianModel(prefix='g_') s, bins = np.histogram(amplitude, bins=bins) topnoise = fit_input[0] valley = fit_input[1] endvalley = fit_input[2] spebump = fit_input[3] endbin = fit_input[4] idx_1 = endvalley idx_2 = spebump + (spebump-endvalley) if idx_1 < endvalley: idx_1 = endvalley if idx_2 > endbin: idx_2 = endbin if check == 1: gauss.set_param_hint('g_height', value=s[spebump], min=s[spebump]-30, max=s[spebump]+30) gauss.set_param_hint('g_center', value=bins[spebump], min=bins[spebump]-30, max=bins[spebump]+30) gauss.set_param_hint('g_sigma', value=idx_2-idx_1, max=(idx_2-idx_1)+30) result = gauss.fit(s[idx_1:idx_2], x=bins[idx_1:idx_2], weights=1.0/np.sqrt(s[idx_1:idx_2])) else: gauss = 0 result = 0 return gauss, result
def gaussian_fit_2(self, y_data, x_data=None, x_peak_indexes=None, y_peak_values=None): if x_data is None: x_data = np.arange(np.max(y_data)) else: pass if x_peak_indexes is None: x_peak_indexes = np.array(np.where(y_data == np.max(y_data))) else: pass if y_peak_values is None: y_peak_values = np.array([np.max(y_data)]) else: pass global multi_gauss_a for peak_num in range(len(x_peak_indexes)): if peak_num == 0: multi_gauss_a = GaussianModel(prefix='g' + str(peak_num) + '_') else: multi_gauss_a += GaussianModel(prefix='g' + str(peak_num) + '_') multi_gauss_a.set_param_hint('g' + str(peak_num) + '_center', value=x_peak_indexes[peak_num], min=x_peak_indexes[peak_num] - 200, max=x_peak_indexes[peak_num] + 200) multi_gauss_a.set_param_hint('g' + str(peak_num) + '_sigma', value=50) multi_gauss_a.set_param_hint('g' + str(peak_num) + '_amplitude', value=50.0 * np.sqrt(2 * np.pi) * y_peak_values[peak_num]) # FIT ACTUALLY OUTPUTS AREA INSTEAD OF AMPLITUDE multi_gauss_fit_a = multi_gauss_a.fit( y_data, x=x_data) # THIS IS THE LINE ACTUALLY DOING THE FITTING return multi_gauss_fit_a
def fit_to_gaussian(self, x, y): gmodel = GaussianModel() params = gmodel.guess(y, x=x) c = params['center'].value n = len(y) q3 = ((np.max(x) - c) / 2) + c min_x = np.min(x) q1 = ((params['center'].value - min_x) / 2) + min_x s = params['sigma'].value h = params['height'].value max_y = np.max(y) if np.max([h, max_y]) < 0.5: amp = 1 / n diff_h = 0.6 - h gmodel.set_param_hint('amplitude', value=amp) gmodel.set_param_hint('amplitude', max=amp * (1 + diff_h)) gmodel.set_param_hint('amplitude', min=amp * diff_h) gmodel.set_param_hint('center', value=c) gmodel.set_param_hint('center', max=q3) gmodel.set_param_hint('center', min=q1) gmodel.set_param_hint('sigma', value=s) gmodel.set_param_hint('sigma', min=s / 2) gmodel.set_param_hint('sigma', max=s * 1.5) gmodel.set_param_hint('height', min=0.6) result = gmodel.fit(y, x=x) # gmodel.print_param_hints() report = result.fit_report() chi_re = re.compile(r'chi-square\s+=\s+([0-9.]+)') cor_re = re.compile(r'C\(sigma, amplitude\)\s+=\s+([0-9.-]+)') chis = np.float32(chi_re.findall(report)) cors = np.float32(cor_re.findall(report)) coeffs = np.concatenate((chis, cors)) mse_model = self.assess_fit(y, result.init_fit - result.best_fit) mse_yhat = self.assess_fit(y, result.residual) return mse_model, mse_yhat, result, report, coeffs