Skip to content

Commit

Permalink
Add examples and some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
APN-Pucky committed Oct 17, 2024
1 parent 9f3e2c6 commit 38d0da1
Show file tree
Hide file tree
Showing 19 changed files with 3,490 additions and 67 deletions.
60 changes: 30 additions & 30 deletions debug/histo2d.ipynb

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions examples/.ipynb_checkpoints/Untitled-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
385 changes: 385 additions & 0 deletions examples/.ipynb_checkpoints/grogu_histo1d-checkpoint.ipynb

Large diffs are not rendered by default.

456 changes: 456 additions & 0 deletions examples/.ipynb_checkpoints/grogu_histo2d-checkpoint.ipynb

Large diffs are not rendered by default.

385 changes: 385 additions & 0 deletions examples/.ipynb_checkpoints/yoda_histo1d-checkpoint.ipynb

Large diffs are not rendered by default.

456 changes: 456 additions & 0 deletions examples/.ipynb_checkpoints/yoda_histo2d-checkpoint.ipynb

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions examples/Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "a7d4ce29-47aa-43c6-b5ac-a3bc3ec47a98",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "",
"name": ""
},
"language_info": {
"name": ""
}
},
"nbformat": 4,
"nbformat_minor": 5
}
384 changes: 384 additions & 0 deletions examples/grogu_histo1d.ipynb

Large diffs are not rendered by default.

457 changes: 457 additions & 0 deletions examples/grogu_histo2d.ipynb

Large diffs are not rendered by default.

383 changes: 383 additions & 0 deletions examples/yoda_histo1d.ipynb

Large diffs are not rendered by default.

456 changes: 456 additions & 0 deletions examples/yoda_histo2d.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/babyyoda/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def plot(self, *args, **kwargs):
# Remove x-axis labels and ticks
plt.gca().get_xaxis().set_visible(False)
# Show the plot
plt.show()
# plt.show()

def _ipython_display_(self):
with contextlib.suppress(ImportError):
Expand Down
2 changes: 1 addition & 1 deletion src/babyyoda/grogu/histo1d_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def Histo1D_v2(*args, title=None, **kwargs):
nbins = args[0]
start = float(args[1])
end = float(args[2])
edges = [i * (end - start) / nbins for i in range(nbins + 1)]
edges = [start + i * (end - start) / nbins for i in range(nbins + 1)]
else:
err = "Invalid arguments"
raise ValueError(err)
Expand Down
2 changes: 1 addition & 1 deletion src/babyyoda/grogu/histo1d_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def Histo1D_v3(*args, title=None, **kwargs):
nbins = args[0]
start = float(args[1])
end = float(args[2])
edges = [i * (end - start) / nbins for i in range(nbins + 1)]
edges = [start + i * (end - start) / nbins for i in range(nbins + 1)]
else:
err = "Invalid arguments"
raise ValueError(err)
Expand Down
4 changes: 2 additions & 2 deletions src/babyyoda/grogu/histo2d_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def Histo2D_v2(
nybins = args[3]
ystart = float(args[4])
yend = float(args[5])
xedges = [i * (xend - xstart) / nxbins for i in range(nxbins + 1)]
yedges = [i * (yend - ystart) / nybins for i in range(nybins + 1)]
xedges = [xstart + i * (xend - xstart) / nxbins for i in range(nxbins + 1)]
yedges = [ystart + i * (yend - ystart) / nybins for i in range(nybins + 1)]

return GROGU_HISTO2D_V2(
d_bins=[
Expand Down
6 changes: 3 additions & 3 deletions src/babyyoda/grogu/histo2d_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def Histo2D_v3(
nybins = args[3]
ystart = float(args[4])
yend = float(args[5])
xedges = [i * (xend - xstart) / nxbins for i in range(nxbins + 1)]
yedges = [i * (yend - ystart) / nybins for i in range(nybins + 1)]
xedges = [xstart + i * (xend - xstart) / nxbins for i in range(nxbins + 1)]
yedges = [ystart + i * (yend - ystart) / nybins for i in range(nybins + 1)]
return GROGU_HISTO2D_V3(
d_edges=[
xedges,
Expand Down Expand Up @@ -251,7 +251,7 @@ def bins(self, includeOverflows=False):
)

def rebinXYTo(self, xedges: list[float], yedges: list[float]):
print(f"rebinXYTo : {self.xEdges()} -> {xedges}, {self.yEdges()} -> {yedges}")
# print(f"rebinXYTo : {self.xEdges()} -> {xedges}, {self.yEdges()} -> {yedges}")
own_xedges = self.xEdges()
for e in xedges:
assert e in own_xedges, f"Edge {e} not found in own edges {own_xedges}"
Expand Down
9 changes: 9 additions & 0 deletions src/babyyoda/histo1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,18 @@ class UHIHisto1D(UHIAnalysisObject):
def to_grogu_v2(self):
from babyyoda.grogu.histo1d_v2 import GROGU_HISTO1D_V2

tot = GROGU_HISTO1D_V2.Bin()
for b in self.bins():
tot.d_sumw += b.sumW()
tot.d_sumw2 += b.sumW2()
tot.d_sumwx += b.sumWX()
tot.d_sumwx2 += b.sumWX2()
tot.d_numentries += b.numEntries()

return GROGU_HISTO1D_V2(
d_key=self.key(),
d_annotations=self.annotationsDict(),
d_total=tot,
d_bins=[
GROGU_HISTO1D_V2.Bin(
d_xmin=self.xEdges()[i],
Expand Down
62 changes: 33 additions & 29 deletions src/babyyoda/histo2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
import numpy as np

from babyyoda.analysisobject import UHIAnalysisObject
from babyyoda.util import loc, overflow, project, rebin, rebinBy_to_rebinTo, underflow
from babyyoda.util import (
loc,
overflow,
project,
rebin,
rebinBy_to_rebinTo,
shift_rebinby,
shift_rebinto,
underflow,
)


def set_bin2d(target, source):
Expand Down Expand Up @@ -42,22 +51,34 @@ class UHIHisto2D(UHIAnalysisObject):
def to_grogu_v2(self):
from babyyoda.grogu.histo2d_v2 import GROGU_HISTO2D_V2

tot = GROGU_HISTO2D_V2.Bin()
for b in self.bins():
tot.d_sumw += b.sumW()
tot.d_sumw2 += b.sumW2()
tot.d_sumwx += b.sumWX()
tot.d_sumwx2 += b.sumWX2()
tot.d_sumwy += b.sumWY()
tot.d_sumwy2 += b.sumWY2()
tot.d_sumwxy += b.crossTerm(0, 1)
tot.d_numentries += b.numEntries()

return GROGU_HISTO2D_V2(
d_key=self.key(),
d_annotations=self.annotationsDict(),
d_total=tot,
d_bins=[
GROGU_HISTO2D_V2.Bin(
d_xmin=self.xEdges()[i % len(self.xEdges())],
d_xmax=self.xEdges()[i % len(self.xEdges()) + 1],
d_ymin=self.yEdges()[i // len(self.xEdges())],
d_ymax=self.yEdges()[i // len(self.xEdges()) + 1],
d_xmin=self.xEdges()[i % (len(self.xEdges()) - 1)],
d_xmax=self.xEdges()[i % (len(self.xEdges()))],
d_ymin=self.yEdges()[i // (len(self.xEdges()) - 1)],
d_ymax=self.yEdges()[i // (len(self.xEdges()))],
d_sumw=b.sumW(),
d_sumw2=b.sumW2(),
d_sumwx=b.sumWX(),
d_sumwx2=b.sumWX2(),
d_sumwy=b.sumWY(),
d_sumwy2=b.sumWY2(),
d_sumwxy=b.sumWXY(),
d_sumwxy=b.crossTerm(0, 1),
d_numentries=b.numEntries(),
)
for i, b in enumerate(self.bins())
Expand Down Expand Up @@ -251,23 +272,6 @@ def __get_indices(self, slices):
def __setitem__(self, slices, value):
set_bin2d(self.__getitem__(slices), value)

def _shift_rebinby(ystart, ystop):
# weird yoda default
if ystart is None:
ystart = 1
else:
ystart += 1
if ystop is None:
ystop = sys.maxsize
else:
ystop += 1
return ystart, ystop

def _shift_rebinto(xstart, xstop):
if xstop is not None:
xstop += 1
return xstart, xstop

def __getitem__(self, slices):
# integer index
if slices is underflow:
Expand Down Expand Up @@ -299,29 +303,29 @@ def __getitem__(self, slices):
)

if isinstance(ystep, rebin):
ystart, ystop = self._shift_rebinby(ystart, ystop)
ystart, ystop = shift_rebinby(ystart, ystop)
sc.rebinYBy(ystep.factor, ystart, ystop)
elif ystep is project:
ystart, ystop = self._shift_rebinby(ystart, ystop)
ystart, ystop = shift_rebinto(ystart, ystop)
sc.rebinYTo(sc.yEdges()[ystart:ystop])
sc = sc.projectY()
# sc = sc[:, ystart:ystop].projectY()
else:
ystart, ystop = self._shift_rebinby(ystart, ystop)
ystart, ystop = shift_rebinto(ystart, ystop)
sc.rebinYTo(self.yEdges()[ystart:ystop])

if isinstance(xstep, rebin):
# weird yoda default
xstart, xstop = self._shift_rebinby(xstart, xstop)
xstart, xstop = shift_rebinby(xstart, xstop)
sc.rebinXBy(xstep.factor, xstart, xstop)
elif xstep is project:
xstart, xstop = self._shift_rebinto(xstart, xstop)
xstart, xstop = shift_rebinto(xstart, xstop)
sc.rebinXTo(sc.xEdges()[xstart:xstop])
# project defaults to projectX, but since we might have already projected Y
# we use the generic project that also exists for 1D
sc = sc.project()
else:
xstart, xstop = self._shift_rebinto(xstart, xstop)
xstart, xstop = shift_rebinto(xstart, xstop)
sc.rebinXTo(self.xEdges()[xstart:xstop])

return sc
Expand Down
19 changes: 19 additions & 0 deletions src/babyyoda/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,22 @@ def rebinBy_to_rebinTo(edges: list[float], factor: int, begin=1, end=sys.maxsize
new_edges.append(edges[j + 1])
# no duplicates
return list(set(new_edges))


def shift_rebinby(ystart, ystop):
# weird yoda default
if ystart is None:
ystart = 1
else:
ystart += 1
if ystop is None:
ystop = sys.maxsize
else:
ystop += 1
return ystart, ystop


def shift_rebinto(xstart, xstop):
if xstop is not None:
xstop += 1
return xstart, xstop

0 comments on commit 38d0da1

Please sign in to comment.