lbl-anp / becquerel

Becquerel is a Python package for analyzing nuclear spectroscopic measurements.
Other
43 stars 16 forks source link

Include or exclude "overflow" events when rebinning #341

Closed markbandstra closed 2 years ago

markbandstra commented 2 years ago

This PR allows the user to specify whether "overflow" events are included in the leftmost and rightmost bins when rebinning. Changes the default to not including overflow events since it can lead to confusion (see #334).

The following MWE demonstrates the different functionality:

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"]:
        for include_overflows in [True, False]:
            spec2 = spec.rebin(edges, method=method, include_overflows=include_overflows)
            print("")
            print(f"Rebinned spectrum (method={method}, include_overflows={include_overflows}):")
            print("edges: ", spec2.bin_edges_kev)
            print("values:", spec2.counts_vals)

The output is:

Original spectrum:
edges:  [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]
values: [1000. 1000. 1000. 1000. 1000. 1000. 1000. 1000. 1000. 1000.]

Rebinned spectrum (method=interpolation, include_overflows=True):
edges:  [2. 3. 4. 5. 6. 7. 8. 9.]
values: [3000. 1000. 1000. 1000. 1000. 1000. 2000.]

Rebinned spectrum (method=interpolation, include_overflows=False):
edges:  [2. 3. 4. 5. 6. 7. 8. 9.]
values: [1000. 1000. 1000. 1000. 1000. 1000. 1000.]

Rebinned spectrum (method=listmode, include_overflows=True):
edges:  [2. 3. 4. 5. 6. 7. 8. 9.]
values: [3000. 1000. 1000. 1000. 1000. 1000. 2000.]

Rebinned spectrum (method=listmode, include_overflows=False):
edges:  [2. 3. 4. 5. 6. 7. 8. 9.]
values: [1000. 1000. 1000. 1000. 1000. 1000. 1000.]

Rebinned spectrum (method=interpolation, include_overflows=True):
edges:  [2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5]
values: [3500. 1000. 1000. 1000. 1000. 1000. 1500.]

Rebinned spectrum (method=interpolation, include_overflows=False):
edges:  [2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5]
values: [1000. 1000. 1000. 1000. 1000. 1000. 1000.]

Rebinned spectrum (method=listmode, include_overflows=True):
edges:  [2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5]
values: [3519.  991. 1001.  971. 1027. 1018. 1473.]

Rebinned spectrum (method=listmode, include_overflows=False):
edges:  [2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5]
values: [1017.  992. 1015. 1000.  996. 1017.  972.]

Fixes #334

markbandstra commented 2 years ago

From our discussion: add a specific numerical example to test for exact equality, just use (one of?) the cases shown here.