Skip to content

Commit

Permalink
Test rebinXTo
Browse files Browse the repository at this point in the history
  • Loading branch information
APN-Pucky committed Oct 10, 2024
1 parent 4ec8d1b commit c61e317
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/babyyoda/grogu/histo1d_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ def binAt(self, x):
def binDim(self):
return 1

def xEdges(self):
return self.d_edges

def rebinXBy(self, factor: int, begin=1, end=None):
start = begin - 1
stop = end
Expand Down Expand Up @@ -244,8 +247,30 @@ def rebinXBy(self, factor: int, begin=1, end=None):
self.d_edges = list(set(new_edges))
self.d_bins = new_bins

def xEdges(self):
return self.d_edges
def xMid(self, i):
return (self.xEdges()[i] + self.xEdges()[i + 1]) / 2

def rebinXTo(self, edges: List[float]):
own_edges = self.xEdges()
for e in edges:
assert e in own_edges, f"Edge {e} not found in own edges {own_edges}"

new_bins = []
of = self.overflow()
uf = self.underflow()
for i in range(len(edges) - 1):
new_bins.append(GROGU_HISTO1D_V3.Bin())
for i, b in enumerate(self.bins()):
if self.xMid(i) < min(edges):
uf += b
elif self.xMid(i) > max(edges):
of += b
else:
for j in range(len(edges) - 1):
if edges[j] <= self.xMid(i) and self.xMid(i) <= edges[j + 1]:
new_bins[j] += b
self.d_bins = [uf] + new_bins + [of]
self.d_edges = edges

@classmethod
def from_string(cls, file_content: str, key: str = "") -> "GROGU_HISTO1D_V3":
Expand Down
25 changes: 25 additions & 0 deletions tests/babyyoda/test_histo1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,28 @@ def test_histos_rebinby(factory1, factory2):
h2.rebinBy(3, begin=2, end=7)

assert_equal_histo1d(h1, h2)


@pytest.mark.parametrize(
"factory1", [grogu.Histo1D, grogu.Histo1D_v2, grogu.Histo1D_v3, yoda.Histo1D]
)
@pytest.mark.parametrize(
"factory2", [grogu.Histo1D, grogu.Histo1D_v2, grogu.Histo1D_v3, yoda.Histo1D]
)
def test_histos_rebinto(factory1, factory2):
o1 = create_histo(factory1)
o2 = create_histo(factory2)

h1 = o1.clone()
h2 = o2.clone()

h1.rebinTo([0.0, 1.0, 2.0, 4.0, 5.0, 6.0, 9.0])
h2.rebinTo([0.0, 1.0, 2.0, 4.0, 5.0, 6.0, 9.0])

# check that modifications happen
with pytest.raises(AssertionError):
assert_equal_histo1d(o1, h1)
with pytest.raises(AssertionError):
assert_equal_histo1d(o2, h2)

assert_equal_histo1d(h1, h2)

0 comments on commit c61e317

Please sign in to comment.