def get_costh_phi_in_bins(hist_3d): """Get all costh phi histograms in each bin of the 3rd variable""" arr = get_array(hist_3d) binning = np.array([get_binning(hist_3d, 'X'), get_binning(hist_3d, 'Y')]) err = get_array(hist_3d, errors=True) return [from_array(arr[:,:,i], binning, errors=err[:,:,i]) for i in xrange(arr.shape[2])]
def to_bw_hist(hist): """Fill all filled bins with value 1 and all empty ones with 0""" arr = get_array(hist) # TODO: generalize and put into hist_utils binning = np.array([get_binning(hist, 0), get_binning(hist, 1)]) arr = arr != 0 return from_array(arr, binning)
def get_pt_bin(amap, pt_val): """ Get the pt bin costh-phi map from the passed (3d) acceptance map """ pt_bin = find_bin(get_binning(amap, 2), np.array([pt_val]))[0] val, err = get_array(amap), get_array(amap, errors=True) ctp_binning = np.array([get_binning(amap, i) for i in [0, 1]]) return from_array(val[:, :, pt_bin], ctp_binning, errors=err[:, :, pt_bin])
def shift_by_median(ppd, use_val=None): """ Shift the ppd by the median to center it around 0 """ if use_val is None: return ppd else: med = use_val binning = get_binning(ppd) return from_array(get_array(ppd), binning - med, errors=get_array(ppd, errors=True))
def get_combined_ppd_2d(inputfiles, var1, var2): """ Get the combined 2d ppd from all inputfiles """ ppds = [get_scaled_ppd_2d(f, var1, var2, 100, 100) for f in inputfiles] ppd_binning = np.array([get_binning(ppds[0], 0), get_binning(ppds[0], 1)]) ppd_vals = np.array([get_array(p) for p in ppds]) # TODO: at some point find out how argmax works in multiple dimensions return from_array(np.max(ppd_vals, axis=0), ppd_binning)
def _test_from_array_nd_w_overflow(self, n_dim): hist = _get_hist(n_dim) arr = hu.get_array(hist, overflow=True) axes = 'X' if n_dim == 2: axes = 'XY' if n_dim == 3: axes = 'XYZ' binning = np.array([hu.get_binning(hist, ax) for ax in axes]) arr_hist = hu.from_array(arr, binning) npt.assert_equal(hu.get_array(arr_hist, overflow=True), arr) npt.assert_equal(hu.get_binning(arr_hist, 'X'), hu.get_binning(hist, 'X')) if n_dim > 1: npt.assert_equal(hu.get_binning(arr_hist, 'Y'), hu.get_binning(hist, 'Y')) if n_dim > 2: npt.assert_equal(hu.get_binning(arr_hist, 'Z'), hu.get_binning(hist, 'Z')) err = hu.get_array(hist, errors=True, overflow=True) arr_err_hist = hu.from_array(arr, binning, errors=err) npt.assert_equal(hu.get_array(arr_err_hist, overflow=True), arr) npt.assert_equal(hu.get_array(arr_err_hist, overflow=True, errors=True), err)
def make_plot_min_chi2(data, var_x, var_y, tf_x, tf_y, gf_only=False): """Make a plot showing the min chi2 from the scan for each scan point, where the fit was OK""" data.loc[:, 'delta_chi2'] = 2 * (data.llh - data.llh.min()) bin_data = apply_selections(data, lambda d: d.delta_chi2 != 0) # Cleanup the data frame by dropping duplicates which can stem from # overlapping scanning when done in parallel bin_data = bin_data.drop_duplicates() if gf_only: bin_data = apply_selections(bin_data, lambda d: d.goodFit > 0) # Get the binning trans_f_x = globals()[tf_x] trans_f_y = globals()[tf_y] x_vals = trans_f_x(bin_data.loc[:, var_x]) y_vals = trans_f_y(bin_data.loc[:, var_y]) x_binning = get_variable_binning(x_vals) y_binning = get_variable_binning(y_vals) arr = np.zeros((len(x_binning) - 1, len(y_binning) - 1)) x_bin = find_bin(x_binning, x_vals) y_bin = find_bin(y_binning, y_vals) dchi2 = bin_data.delta_chi2.values arr[x_bin, y_bin] = dchi2[:] hist = from_array(arr, np.array([x_binning, y_binning])) can = _setup_canvas(None) can.SetRightMargin(0.12) mkplot(hist, can=can, drawOpt='colz', xRange=[x_binning[0], x_binning[-1]], yRange=[y_binning[0], y_binning[-1]], xLabel=get_var_name(var_x, tf_x), yLabel=get_var_name(var_y, tf_y)) hist.GetZaxis().SetRangeUser(0, 25) can.Update() return can
def get_combined_ppd(inputfiles, var): """ Get the combined ppd from all inputfiles """ ppds = [get_scaled_ppd(f, var) for f in inputfiles] # PPDs all have the same binning ppd_binning = get_binning(ppds[0]) ppd_vals = np.array([get_array(p) for p in ppds]) ppd_errs = np.array([get_array(p, errors=True) for p in ppds]) # Get the maximum value in each gin and its uncertainty max_idx = np.argmax(ppd_vals, axis=0) # Necessary for 2d indexing. There might be an easier way for this idx = np.arange(0, len(ppd_vals[0])) max_ppd = ppd_vals[max_idx, idx] max_err = ppd_errs[max_idx, idx] return from_array(max_ppd, ppd_binning, errors=max_err)
def shift_by_median(ppd, median): """Shift the ppd by the passed median""" return from_array(get_array(ppd), get_binning(ppd) - median, errors=get_array(ppd, errors=True))
def test_handles_binning(self): # Test if binning arrays are handled correctly (type conversion for # ROOT to understand) hist = hu.from_array(np.random.uniform(0, 1, 10), np.arange(0, 11, 1)) npt.assert_equal(hu.get_binning(hist, 'X'), np.arange(0, 11, 1))
def test_raises_errors(self): # binning, array mismatch with self.assertRaises(ValueError): hu.from_array(np.random.uniform(0, 1, 10), np.linspace(0, 1, 30)) with self.assertRaises(ValueError): hu.from_array(np.random.uniform(0, 1, (10, 10)), np.array([np.linspace(0, 1, 11), np.linspace(0, 1, 12)])) with self.assertRaises(ValueError): hu.from_array(np.random.uniform(0, 1, (10, 10, 10)), np.array([np.linspace(0, 1, 11), np.linspace(0, 1, 11), np.linspace(0, 1, 12)])) with self.assertRaises(ValueError): hu.from_array(np.random.uniform(0, 1, (10, 2)), np.linspace(0, 1, 30)) # wrong error dimension with self.assertRaises(ValueError): hu.from_array(np.random.uniform(0, 1, 10), np.linspace(0, 1, 11), errors=np.random.uniform(0, 1, 11)) with self.assertRaises(ValueError): hu.from_array(np.random.uniform(0, 1, (10, 10)), np.array([np.linspace(0, 1, 11), np.linspace(0, 1, 11)]), errors=np.random.uniform(0, 1, (11, 2))) # unhandleable array dimension with self.assertRaises(ValueError): hu.from_array(np.random.uniform(0, 1, (10, 4, 4, 5)), np.random.uniform(0, 1, 10))