Skip to content

Commit

Permalink
Working tests
Browse files Browse the repository at this point in the history
  • Loading branch information
APN-Pucky committed Oct 9, 2024
1 parent 9847a89 commit 279f3da
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 43 deletions.
314 changes: 300 additions & 14 deletions debug/histo1d.ipynb

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions debug/histo2d.ipynb

Large diffs are not rendered by default.

25 changes: 17 additions & 8 deletions src/babyyoda/Histo1D_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ def __call__(self, *args, **kwargs):
########################################################

def overflow(self):
# This is a YODA-1 feature that is not present in YODA-2
return self.bins()[-1]
# if target has overflow method, call it
if hasattr(self.target, "overflow"):
return self.target.overflow()
return self.bins(includeOverflows=True)[-1]

def underflow(self):
# This is a YODA-1 feature that is not present in YODA-2
return self.bins()[0]
# if target has underflow method, call it
if hasattr(self.target, "underflow"):
return self.target.underflow()
return self.bins(includeOverflows=True)[0]

def xMins(self):
return np.array([b.xMin() for b in self.bins()])
Expand All @@ -64,6 +68,9 @@ def xMaxs(self):
def sumWs(self):
return np.array([b.sumW() for b in self.bins()])

def sumW2s(self):
return np.array([b.sumW2() for b in self.bins()])

########################################################
# Generic UHI code
########################################################
Expand All @@ -74,7 +81,7 @@ def axes(self):

@property
def kind(self):
return "COUNT"
return "MEAN"

def counts(self):
return np.array([b.numEntries() for b in self.bins()])
Expand All @@ -83,7 +90,7 @@ def values(self):
return np.array([b.sumW() for b in self.bins()])

def variances(self):
return np.array([b.sumW2() for b in self.bins()])
return np.array([(b.sumW2()) for b in self.bins()])

def __setitem__(self, slices, value):
# integer index
Expand Down Expand Up @@ -150,10 +157,12 @@ def __get_index(self, slices):
index = overflow
return index

def plot(self, *args, w2method="sqrt", **kwargs):
def plot(self, *args, binwnorm=1.0, **kwargs):
import mplhep as hep

hep.histplot(self, w2=self.variances(), *args, w2method=w2method, **kwargs)
hep.histplot(
self, *args, yerr=self.variances() ** 0.5, binwnorm=binwnorm, **kwargs
)

def _ipython_display_(self):
try:
Expand Down
29 changes: 23 additions & 6 deletions src/babyyoda/Histo2D_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,20 @@ def __call__(self, *args, **kwargs):
# YODA compatibility code (dropped legacy code?)
########################################################

def overflow(self):
# This is a YODA-1 feature that is not present in YODA-2
return self.bins(includeOverflows=True)[-1]
def bins(self):
# fix order
return np.array(sorted(self.target.bins(), key=lambda b: (b.xMin(), b.yMin())))

def underflow(self):
# This is a YODA-1 feature that is not present in YODA-2
return self.bins(includeOverflows=True)[0]
def bin(self, *indices):
return self.bins()[indices]

# def overflow(self):
# # This is a YODA-1 feature that is not present in YODA-2
# return self.bins(includeOverflows=True)[-1]

# def underflow(self):
# # This is a YODA-1 feature that is not present in YODA-2
# return self.bins(includeOverflows=True)[0]

def xMins(self):
return np.array(sorted(list(set([b.xMin() for b in self.bins()]))))
Expand Down Expand Up @@ -141,7 +148,17 @@ def __getitem__(self, slices):
def plot(self, *args, **kwargs):
import mplhep as hep

# Hack in the temporary division by dVol
saved_values = self.values

def temp_values():
return np.array([b.sumW() / b.dVol() for b in self.bins()]).reshape(
(len(self.axes[0]), len(self.axes[1]))
)

self.values = temp_values
hep.hist2dplot(self, *args, **kwargs)
self.values = saved_values

def _ipython_display_(self):
try:
Expand Down
33 changes: 33 additions & 0 deletions src/babyyoda/grogu/histo1d_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,39 @@ def sumWX(self):
def sumWX2(self):
return self.d_sumwx2

def variance(self):
if self.d_sumw**2 - self.d_sumw2 == 0:
return 0
return abs(
(self.d_sumw2 * self.d_sumw - self.d_sumw**2)
/ (self.d_sumw**2 - self.d_sumw2)
)
# return self.d_sumw2/self.d_numentries - (self.d_sumw/self.d_numentries)**2

def errW(self):
return self.d_sumw2**0.5

def stdDev(self):
return self.variance() ** 0.5

def effNumEntries(self):
return self.sumW() ** 2 / self.sumW2()

def stdErr(self):
return self.stdDev() / self.effNumEntries() ** 0.5

def dVol(self):
return self.d_xmax - self.d_xmin

def xVariance(self):
# return self.d_sumwx2/self.d_sumw - (self.d_sumwx/self.d_sumw)**2
if self.d_sumw**2 - self.d_sumw2 == 0:
return 0
return abs(
(self.d_sumwx2 * self.d_sumw - self.d_sumwx**2)
/ (self.d_sumw**2 - self.d_sumw2)
)

def numEntries(self):
return self.d_numentries

Expand Down
3 changes: 3 additions & 0 deletions src/babyyoda/grogu/histo2d_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def sumWY2(self):
def sumWXY(self):
return self.d_sumwxy

def dVol(self):
return (self.d_xmax - self.d_xmin) * (self.d_ymax - self.d_ymin)

def crossTerm(self, x, y):
assert (x == 0 and y == 1) or (x == 1 and y == 0)
return self.sumWXY()
Expand Down
18 changes: 8 additions & 10 deletions tests/test_histo1d_v2.yoda
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ Total Total 6.600000e+01 6.600000e+01 4.400000e+02 3.410000e+03 6.600000e+
Underflow Underflow 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
Overflow Overflow 1.100000e+01 1.100000e+01 1.100000e+02 1.100000e+03 1.100000e+01
# xlow xhigh sumw sumw2 sumwx sumwx2 numEntries
0.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00
1.000000e+00 2.000000e+00 2.000000e+00 2.000000e+00 2.000000e+00 2.000000e+00 2.000000e+00
2.000000e+00 3.000000e+00 3.000000e+00 3.000000e+00 6.000000e+00 1.200000e+01 3.000000e+00
3.000000e+00 4.000000e+00 4.000000e+00 4.000000e+00 1.200000e+01 3.600000e+01 4.000000e+00
4.000000e+00 5.000000e+00 5.000000e+00 5.000000e+00 2.000000e+01 8.000000e+01 5.000000e+00
5.000000e+00 6.000000e+00 6.000000e+00 6.000000e+00 3.000000e+01 1.500000e+02 6.000000e+00
6.000000e+00 7.000000e+00 7.000000e+00 7.000000e+00 4.200000e+01 2.520000e+02 7.000000e+00
7.000000e+00 8.000000e+00 8.000000e+00 8.000000e+00 5.600000e+01 3.920000e+02 8.000000e+00
8.000000e+00 9.000000e+00 9.000000e+00 9.000000e+00 7.200000e+01 5.760000e+02 9.000000e+00
9.000000e+00 1.000000e+01 1.000000e+01 1.000000e+01 9.000000e+01 8.100000e+02 1.000000e+01
0.000000e+00 2.000000e+00 3.000000e+00 9.500000e+00 2.000000e+00 6.000000e+00 5.000000e+00
2.000000e+00 3.000000e+00 3.000000e+00 3.500000e+00 6.000000e+00 1.200000e+03 3.000000e+00
3.000000e+00 4.000000e+00 4.000000e+00 4.500000e+00 1.200000e+01 3.600000e+03 4.000000e+00
4.000000e+00 5.000000e+00 5.000000e+00 5.500000e+00 2.000000e+01 8.000000e+03 5.000000e+00
5.000000e+00 6.000000e+00 6.000000e+00 6.500000e+00 3.000000e+01 1.500000e+03 6.000000e+00
6.000000e+00 7.000000e+00 7.000000e+00 7.500000e+00 4.200000e+01 2.520000e+03 7.000000e+00
7.000000e+00 8.000000e+00 8.000000e+00 8.500000e+00 5.600000e+01 3.920000e+03 8.000000e+00
8.000000e+00 1.000000e+01 8.000000e+00 1.500000e+01 9.000000e+01 8.100000e+03 1.000000e+01
END YODA_HISTO1D_V2
2 changes: 1 addition & 1 deletion tests/yoda/uhi/test_yd_histo2d_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_access_index():
assert_bin2d(h[0, 2], h.bin(2))

assert_bin2d(g[1, 0], g.bin(10))
assert_bin2d(g[0, 2], h.bin(2))
assert_bin2d(g[0, 2], g.bin(2))


def test_access_loc():
Expand Down

0 comments on commit 279f3da

Please sign in to comment.