When using bq.rebin or bq.Spectrum.rebin, the default behavior is to silently include all data outside the new binning scheme in the leftmost bin (if less than the leftmost bin edge) or in the rightmost bin (if greater than the rightmost bin edge). This behavior is unexpected and not documented. I think it should be controlled by a kwarg that defaults to disabling the behavior.
This behavior has been noted in #148 and #209, and #209 in particular notes the needed workaround: to add an extra bin at each end and then later remove them.
I just ran into a situation where I rebinned a spectrum but it still included the problematic data that I was trying to exclude, which was unhelpful and confusing.
Here is a MWE demonstrating the behavior:
import numpy as np
import becquerel as bq
counts = 1000 * np.ones(10)
spec = bq.Spectrum(counts=counts)
cal = bq.Calibration("p[0] * x", [1.0])
spec.apply_calibration(cal)
print("Original spectrum:")
print("edges: ", spec.bin_edges_kev)
print("values:", spec.counts_vals)
for edges in [
np.linspace(2, 9, num=8),
np.linspace(2.5, 9.5, num=8),
]:
for method in ["interpolation", "listmode"]:
spec2 = spec.rebin(edges, method=method)
print("")
print(f"Rebinned spectrum ({method}):")
print("edges: ", spec2.bin_edges_kev)
print("values:", spec2.counts_vals)
When using
bq.rebin
orbq.Spectrum.rebin
, the default behavior is to silently include all data outside the new binning scheme in the leftmost bin (if less than the leftmost bin edge) or in the rightmost bin (if greater than the rightmost bin edge). This behavior is unexpected and not documented. I think it should be controlled by a kwarg that defaults to disabling the behavior.This behavior has been noted in #148 and #209, and #209 in particular notes the needed workaround: to add an extra bin at each end and then later remove them.
I just ran into a situation where I rebinned a spectrum but it still included the problematic data that I was trying to exclude, which was unhelpful and confusing.
Here is a MWE demonstrating the behavior:
The output is:
Ideally all of the outputs of the rebinned spectra would be
or approximately those values. (Randomness will affect the final line.)