def test_rebin_uniform_same(): bin_edges_old = np.linspace(0.0, 50.0, 51) bin_edges_new = np.linspace(-1.0, 52.0, 54) data = np.random.normal(25.0, 7.0, size=1000) hist = np.histogram(data, bin_edges_old)[0] hist_new = rebin_uniform(hist, bin_edges_old, bin_edges_new) assert (hist == hist_new[1:-2]).all()
def test_rebin_uniform(): bin_edges_old = np.linspace(0.0, 50.0, 51) bin_edges_new = np.linspace(0.0, 60.0, 21) data = np.random.normal(25.0, 7.0, size=1000) hist = np.histogram(data, bin_edges_old)[0] hist_new = rebin_uniform(hist, bin_edges_old, bin_edges_new) assert np.sum(hist) == np.sum(hist_new)
def test_rebin_uniform_non_integer(): bin_edges_old = np.linspace(0.0, 50.0, 51) bin_edges_new = np.linspace(0.1, 50.1, 51) data = np.random.normal(25.0, 7.0, size=1000) hist = np.histogram(data, bin_edges_old)[0] hist = 2.2 * hist.astype("float") with pytest.raises(ValueError): hist_new = rebin_uniform(hist, bin_edges_old, bin_edges_new)
def test_rebin_uniform_negative(): bin_edges_old = np.linspace(0.0, 50.0, 51) bin_edges_new = np.linspace(0.1, 50.1, 51) data = np.random.normal(25.0, 7.0, size=1000) hist = np.histogram(data, bin_edges_old)[0] hist[0] = -1 with pytest.raises(ValueError): hist_new = rebin_uniform(hist, bin_edges_old, bin_edges_new)
def read_rebin_spectrum( spectrum: Path, bin_edges_rebin: np.ndarray, histname: Optional[str] = None, cal_bin_centers: Optional[List[float]] = None, cal_bin_edges: Optional[List[float]] = None, ) -> Tuple[np.ndarray, np.ndarray]: spectrum, spectrum_bin_edges = read_pos_int_spectrum(spectrum, histname) if spectrum_bin_edges is not None and (cal_bin_centers or cal_bin_edges): logger.warning("Ignoring calibration, binnig already provided") if cal_bin_centers is not None: spectrum_bin_edges = np.poly1d( np.array(cal_bin_centers)[::-1])(np.arange(spectrum.size + 1) - 0.5) elif cal_bin_edges is not None: spectrum_bin_edges = np.poly1d(np.array(cal_bin_edges)[::-1])( np.arange(spectrum.size + 1)) if spectrum_bin_edges is not None: spectrum = rebin_uniform(spectrum, spectrum_bin_edges, bin_edges_rebin) return spectrum, spectrum_bin_edges