aminnj / yahist

1D and 2D histogram objects
10 stars 3 forks source link

Overflow bins from "restrict" method #12

Closed jkguiang closed 3 years ago

jkguiang commented 3 years ago

When a user calls the restrict method for a histogram, it would be nice to add an option to have the clipped bins be consolidated into overflow bins. This could be accomplished with some numpy in the backend and an additional kwarg:

h.restrict(low, high, overflow=True/False)

where the backend implementation would be

h2 = h1.restrict(low, high)
h2._counts = h1.counts[0:len(h2.counts)]
h2._counts[-1] += h1.counts[len(h2.counts):-1].sum()

or "something like that."

aminnj commented 3 years ago

Thanks, will look into this. In the meantime, it's probably best to remake the histogram with the restricted binning. That way the error is propagated properly for the overflow entries.

aminnj commented 3 years ago

This is in version 1.14 now.

from yahist import Hist1D

h = Hist1D.from_random(bins="30,-3,3")
h.plot(label="orig", lw=5)
h.restrict(-2, 2).plot(label="restricted", lw=3)
h.restrict(-2, 2, overflow=True).plot(label="restricted with overflow=True", lw=1)

gives image