def forOnePoint_ecal_neq_0(e, h): # the neighbours of the point (ecal,hcal) = (e,h) dist, ind = self.neigh_ecal_neq_0.kneighbors([[e, h]]) true = z[ind][0] hcal = y[ind][0] ecal = x[ind][0] binwidth = 1 nbins = np.arange(min(true), max(true) + binwidth, binwidth) with warnings.catch_warnings(): try: #we create the histogram warnings.simplefilter("error", OptimizeWarning) entries, bin_edges = np.histogram(true, bins=nbins) bin_middles = 0.5 * (bin_edges[1:] + bin_edges[:-1]) bin_middles = bin_middles[entries != 0] entries = entries[entries != 0] reduced = math.nan # we fit the histogram p0 = np.sqrt(np.std(entries)), bin_middles[np.argmax( entries)], max(entries) error = np.sqrt(entries) parameters, cov_matrix = curve_fit(gaussian_param, bin_middles, entries, sigma=error, p0=p0) res = parameters[1] chi2 = np.sum( ((gaussian_param(bin_middles, *parameters) - entries) / error)**2) reduced = chi2 / (len(bin_middles) - len(parameters)) if reduced > 10: raise OptimizeWarning except (OptimizeWarning, RuntimeError): parameters = p0 res = parameters[1] if e + h < self.lim: print("calibration issue for ecal = ", e, " hcal = ", h) finally: # we save the values in the attributs self.evaluatedPoint_neighbours_ecal.append(ecal) self.evaluatedPoint_neighbours_hcal.append(hcal) self.evaluatedPoint_neighbours_true.append(true) self.evaluatedPoint_entries.append(entries) self.evaluatedPoint_bin_middles.append(bin_middles) self.evaluatedPoint_ecal.append(e) self.evaluatedPoint_hcal.append(h) self.evaluatedPoint_true.append(res) self.evaluatedPoint_parameters.append(parameters) self.evaluatedPoint_reducedchi2.append(reduced) return res
def predictSingleValue(ecal, hcal): if ecal + hcal > self.lim: return math.nan reduced = math.nan if ecal == 0: dist, ind = self.neigh_ecal_eq_0.kneighbors(X=hcal) dist = dist[0] ind = ind[0] dlim = hcal - self.hcal_train_ecal_eq_0_min + 0.1 if hcal < self.hcal_train_ecal_eq_0_min: dlim = self.hcal_train_ecal_eq_0_min + 0.1 ind = ind[dist <= dlim] true_neigh = self.true_train_ecal_eq_0[ind] binwidth = 1 nbins = np.arange(min(true_neigh), max(true_neigh) + binwidth, binwidth) with warnings.catch_warnings(): try: #we create the histogram warnings.simplefilter("error", OptimizeWarning) entries, bin_edges = np.histogram(true_neigh, bins=nbins) bin_middles = 0.5 * (bin_edges[1:] + bin_edges[:-1]) bin_middles = bin_middles[entries != 0] entries = entries[entries != 0] # we fit the histogram p0 = np.sqrt(np.std(entries)), bin_middles[np.argmax( entries)], max(entries) error = np.sqrt(entries) parameters, cov_matrix = curve_fit(gaussian_param, bin_middles, entries, sigma=error, p0=p0) res = parameters[1] chi2 = np.sum( ((gaussian_param(bin_middles, *parameters) - entries) / error)**2) reduced = chi2 / (len(bin_middles) - len(parameters)) if reduced > 10: raise OptimizeWarning except (OptimizeWarning, RuntimeError): parameters = p0 res = parameters[1] print("calibration issue for ecal = 0, hcal = ", hcal, "reduced chi2 = ", reduced) finally: return res else: dist, ind = self.neigh_ecal_neq_0.kneighbors(X=[[ecal, hcal]]) dist = dist[0] ind = ind[0] true_neigh = self.true_train_ecal_neq_0[ind] binwidth = 1 nbins = np.arange(min(true_neigh), max(true_neigh) + binwidth, binwidth) try: #we create the histogram warnings.simplefilter("error", OptimizeWarning) entries, bin_edges = np.histogram(true_neigh, bins=nbins) bin_middles = 0.5 * (bin_edges[1:] + bin_edges[:-1]) bin_middles = bin_middles[entries != 0] entries = entries[entries != 0] # we fit the histogram p0 = np.sqrt(np.std(entries)), bin_middles[np.argmax( entries)], max(entries) error = np.sqrt(entries) parameters, cov_matrix = curve_fit(gaussian_param, bin_middles, entries, sigma=error, p0=p0) res = parameters[1] chi2 = np.sum( ((gaussian_param(bin_middles, *parameters) - entries) / error)**2) reduced = chi2 / (len(bin_middles) - len(parameters)) if reduced > 10: raise OptimizeWarning except (OptimizeWarning, RuntimeError): parameters = p0 res = parameters[1] print("calibration issue for ecal = ", ecal, ", hcal = ", hcal, "reduced chi2 = ", reduced, "remained neighbours = ", len(true_neigh)) finally: return res
entries1 = KNNGF.evaluatedPoint_entries_ecal_eq_0[i1] bin_middles1 = KNNGF.evaluatedPoint_bin_middles_ecal_eq_0[i1] error1 = np.sqrt(entries1) params1 = KNNGF.evaluatedPoint_parameters_ecal_eq_0[i1] e2 = KNNGF.evaluatedPoint_ecal[i2] h2 = KNNGF.evaluatedPoint_hcal[i2] entries2 = KNNGF.evaluatedPoint_entries[i2] bin_middles2 = KNNGF.evaluatedPoint_bin_middles[i2] error2 = np.sqrt(entries2) params2 = KNNGF.evaluatedPoint_parameters[i2] fig = plt.figure(figsize=(10, 4)) plt.subplot(1, 2, 1) plt.errorbar(bin_middles1, entries1, yerr=error1, fmt='o') xplot = np.arange(min(bin_middles1), max(bin_middles1), 1) plt.plot(xplot, gaussian_param(xplot, *params1), lw=3) plt.xlabel(r"$e_{true}$", fontsize=12) plt.title(r"histogram of $e_{true}$ for $(e_{cal}=" + str(np.around(e1, 2)) + ",h_{cal}=" + str(np.around(h1, 2)) + ")$", fontsize=12) plt.subplot(1, 2, 2) plt.errorbar(bin_middles2, entries2, yerr=error2, fmt='o') plt.plot(xplot, gaussian_param(xplot, *params2), lw=3) xplot = np.arange(min(bin_middles2), max(bin_middles2), 1) plt.xlabel(r"$e_{true}$", fontsize=12) plt.title(r"histogram of $e_{true}$ for $(e_{cal}=" + str(np.around(e2, 2)) + ",h_{cal}=" + str(np.around(h2, 2)) + ")$", fontsize=12) plt.tight_layout() plt.show() savefig(fig, directory, classname + "_hist_calib.png")
def forOnePoint_ecal_eq_0(h): # the neighbours of the point (ecal,hcal) = (0,h) dist, ind = self.neigh_ecal_eq_0.kneighbors(X=h) dist = dist[0] ind = ind[0] dlim = h - self.hcal_train_ecal_eq_0_min + 0.1 ind = ind[dist <= dlim] true = z[ind] hcal = y[ind] binwidth = 1 reduced = math.nan with warnings.catch_warnings(): try: if len(true) == 0: raise UserWarning nbins = np.arange(min(true), max(true) + binwidth, binwidth) #we create the histogram warnings.simplefilter("error", OptimizeWarning) entries, bin_edges = np.histogram(true, bins=nbins) bin_middles = 0.5 * (bin_edges[1:] + bin_edges[:-1]) bin_middles = bin_middles[entries != 0] entries = entries[entries != 0] # we fit the histogram p0 = np.sqrt(np.std(entries)), bin_middles[np.argmax( entries)], max(entries) error = np.sqrt(entries) parameters, cov_matrix = curve_fit(gaussian_param, bin_middles, entries, sigma=error, p0=p0) res = parameters[1] chi2 = np.sum( ((gaussian_param(bin_middles, *parameters) - entries) / error)**2) reduced = chi2 / (len(bin_middles) - len(parameters)) if reduced > 10: raise OptimizeWarning except UserWarning: parameters = math.nan entries = math.nan res = math.nan bin_middles = math.nan print("calibration issue for ecal = 0, hcal = ", h, "reduced chi2 = ", reduced) except (OptimizeWarning, RuntimeError): parameters = p0 res = parameters[1] print("calibration issue for ecal = 0, hcal = ", h, "reduced chi2 = ", reduced) finally: # we save the values in the attributs self.evaluatedPoint_hcal_ecal_eq_0.append(h) self.evaluatedPoint_true_ecal_eq_0.append(res) self.evaluatedPoint_parameters_ecal_eq_0.append(parameters) self.evaluatedPoint_neighbours_hcal_ecal_eq_0.append(hcal) self.evaluatedPoint_neighbours_true_ecal_eq_0.append(true) self.evaluatedPoint_entries_ecal_eq_0.append(entries) self.evaluatedPoint_bin_middles_ecal_eq_0.append( bin_middles) self.evaluatedPoint_reducedchi2_ecal_eq_0.append(reduced) return res