Skip to content

Commit

Permalink
Transition to OOP
Browse files Browse the repository at this point in the history
Closes: #24
Closes: #19
Closes: #23
  • Loading branch information
APN-Pucky authored Oct 13, 2024
2 parents 9a23a4c + cc9a77b commit c898a72
Show file tree
Hide file tree
Showing 52 changed files with 1,180 additions and 1,038 deletions.
8 changes: 8 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[flake8]
# E501: line too long
# E203: E203 whitespace before ':'
extend-ignore = E501, E203
max-line-length = 88
max-complexity = 18
count = True
statistics = True
14 changes: 8 additions & 6 deletions .github/workflows/test-yoda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,26 @@ jobs:
runs-on: ubuntu-latest
container: apnpucky/gentoo-yoda:${{ matrix.yoda-version }}-latest
strategy:
fail-fast: false
matrix:
yoda-version: ['1.9.10', '2.0.1']
yoda-version: ['1.9.10',
# '2.0.1', # This version is faulty!!!
'9999']

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: astral-sh/setup-uv@v3

- name: Install dependencies
run: |
pip install '.[test]' --break-system-packages
pip list
PYTHON_TARGETS="python3_12" emerge -q dev-python/pip
python3.12 -m pip install '.[test]' --break-system-packages
python3.12 -m pip list
- name: Test with pytest
run: |
pytest -r sx tests/
python3.12 -m pytest -r sx tests/
# - name: Report core project coverage with Codecov
# if: >-
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ For a less feature complete version, `babyyoda.grogu` is a simpler python drop-i
import babyyoda.grogu as yoda
```

or force yoda use with

```python
import babyyoda.yoda as yoda
```

## License

`babyyoda` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
7 changes: 4 additions & 3 deletions debug/Untitled.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"metadata": {},
"outputs": [],
"source": [
"import babyyoda as by\n",
"import yoda as yd"
"import yoda as yd\n",
"\n",
"import babyyoda as by"
]
},
{
Expand Down Expand Up @@ -166,8 +167,8 @@
"metadata": {},
"outputs": [],
"source": [
"from babyyoda.histo2D import Histo2D\n",
"from babyyoda.grogu.histo2d_v2 import GROGU_HISTO2D_V2\n",
"from babyyoda.histo2D import Histo2D\n",
"from babyyoda.util import loc"
]
},
Expand Down
5 changes: 3 additions & 2 deletions debug/YODA2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"metadata": {},
"outputs": [],
"source": [
"from babyyoda.Histo1D import HISTO1D\n",
"import yoda\n",
"\n",
"from babyyoda.Histo1D import HISTO1D\n",
"\n",
"\n",
"def get_histo1d():\n",
" h = yoda.Histo1D(10, 0, 10, title=\"test\")\n",
Expand Down Expand Up @@ -101,7 +102,7 @@
"metadata": {},
"outputs": [],
"source": [
"import babyyoda.grogu as grogu"
"from babyyoda import grogu"
]
},
{
Expand Down
5 changes: 3 additions & 2 deletions debug/histo1d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"import babyyoda as grogu\n",
"from babyyoda import loc, rebin\n",
"import matplotlib.pyplot as plt"
"from babyyoda import loc, rebin"
]
},
{
Expand Down
21 changes: 11 additions & 10 deletions debug/histo2d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
"metadata": {},
"outputs": [],
"source": [
"import yoda as yd\n",
"from matplotlib import pyplot as plt\n",
"\n",
"import babyyoda as grogu\n",
"from matplotlib import pyplot as plot\n",
"from babyyoda import loc\n",
"import yoda as yd"
"from babyyoda import loc"
]
},
{
Expand Down Expand Up @@ -127,7 +128,7 @@
],
"source": [
"h[:, :].plot()\n",
"plot.xscale(\"log\")"
"plt.xscale(\"log\")"
]
},
{
Expand Down Expand Up @@ -156,7 +157,7 @@
],
"source": [
"h[:, loc(0) : loc(5)].plot()\n",
"plot.xscale(\"log\")"
"plt.xscale(\"log\")"
]
},
{
Expand Down Expand Up @@ -185,7 +186,7 @@
],
"source": [
"h[loc(10**-5) : loc(10**-2), :].plot()\n",
"plot.xscale(\"log\")"
"plt.xscale(\"log\")"
]
},
{
Expand Down Expand Up @@ -214,7 +215,7 @@
],
"source": [
"h[loc(10**-5) : loc(10**-2), loc(0) : loc(5)].plot()\n",
"plot.xscale(\"log\")"
"plt.xscale(\"log\")"
]
},
{
Expand Down Expand Up @@ -352,7 +353,7 @@
"source": [
"hists[\"/ALICE_2020_I1797621/GGPdfXQ\"].plot()\n",
"# plot.yscale(\"log\")\n",
"plot.xscale(\"log\")"
"plt.xscale(\"log\")"
]
},
{
Expand Down Expand Up @@ -391,8 +392,8 @@
"source": [
"hists = grogu.read_yoda(\"../tests/test_histo2d_v2.yoda\")\n",
"hists[\"/ALICE_2020_I1797621/GGPdfXQ\"].plot()\n",
"# plot.yscale(\"log\")\n",
"plot.xscale(\"log\")"
"# plt.yscale(\"log\")\n",
"plt.xscale(\"log\")"
]
},
{
Expand Down
33 changes: 33 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,36 @@ local_scheme = "no-local-version"

[tool.hatch.build.hooks.vcs]
version-file = "src/babyyoda/_version.py"


[tool.ruff.lint]
extend-select = [
"B", # flake8-bugbear
"I", # isort
"ARG", # flake8-unused-arguments
"C4", # flake8-comprehensions
"EM", # flake8-errmsg
"ICN", # flake8-import-conventions
"G", # flake8-logging-format
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # pylint
"PT", # flake8-pytest-style
"RET", # flake8-return
"RUF", # Ruff-specific
"SIM", # flake8-simplify
"UP", # pyupgrade
"YTT", # flake8-2020
"EXE", # flake8-executable
"NPY", # NumPy specific rules
"PD", # pandas-vet
"FURB", # refurb
"PYI", # flake8-pyi
]
ignore = [
"PLR09", # Too many <...>
"PLR2004", # Magic value used in comparison
"ISC001", # Conflicts with formatter
"RUF012", # TODO: mutable class attributes
"SIM115", # TODO: use context manager for opening files
]
12 changes: 5 additions & 7 deletions src/babyyoda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
# SPDX-License-Identifier: MIT

from ._version import version as __version__

from .histo1d import UHIHisto1D
from .histo2d import UHIHisto2D
from .read import read, read_grogu, read_yoda
from .util import loc, overflow, rebin, underflow
from .write import write, write_grogu, write_yoda
from .util import loc, overflow, underflow, rebin

from .histo1D import Histo1D
from .histo2D import Histo2D

__all__ = [
"__version__",
"Histo1D",
"Histo2D",
"UHIHisto1D",
"UHIHisto2D",
"read",
"loc",
"overflow",
Expand Down
13 changes: 7 additions & 6 deletions src/babyyoda/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import matplotlib
import babyyoda
import argparse
import re

import matplotlib as mpl
from histoprint import print_hist

from babyyoda.histo1D import Histo1D
from babyyoda.histo2D import Histo2D
import babyyoda
from babyyoda.histo1d import UHIHisto1D
from babyyoda.histo2d import UHIHisto2D


def main():
Expand Down Expand Up @@ -35,10 +36,10 @@ def main():
):
continue

if isinstance(v, Histo1D) or isinstance(v, Histo2D):
if isinstance(v, (UHIHisto1D, UHIHisto2D)):
if args.operation == "print":
print(k)
print_hist(v, summary=True, title=v.title())
if args.operation == "plot":
v.plot()
matplotlib.pyplot.show()
mpl.pyplot.show()
4 changes: 2 additions & 2 deletions src/babyyoda/grogu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from babyyoda.grogu.histo1d_v3 import GROGU_HISTO1D_V3
from babyyoda.grogu.histo2d_v2 import GROGU_HISTO2D_V2
from babyyoda.grogu.histo2d_v3 import GROGU_HISTO2D_V3

from .histo1d_v2 import GROGU_HISTO1D_V2
from .read import read
from .write import write
from .histo1d_v2 import GROGU_HISTO1D_V2


__all__ = ["read", "write"]

Expand Down
42 changes: 17 additions & 25 deletions src/babyyoda/grogu/histo1d_v2.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import re
from typing import List, Optional
from dataclasses import dataclass, field
from typing import Optional

from babyyoda.grogu.analysis_object import GROGU_ANALYSIS_OBJECT
from babyyoda.histo1d import UHIHisto1D


@dataclass
class GROGU_HISTO1D_V2(GROGU_ANALYSIS_OBJECT):
class GROGU_HISTO1D_V2(GROGU_ANALYSIS_OBJECT, UHIHisto1D):
@dataclass
class Bin:
d_xmin: Optional[float] = None
Expand Down Expand Up @@ -56,7 +57,7 @@ def set_bin(self, bin):
self.d_sumwx2 = bin.sumWX2()
self.d_numentries = bin.numEntries()

def set(self, numEntries: float, sumW: List[float], sumW2: List[float]):
def set(self, numEntries: float, sumW: list[float], sumW2: list[float]):
assert len(sumW) == 2
assert len(sumW2) == 2
self.d_sumw = sumW[0]
Expand Down Expand Up @@ -136,13 +137,6 @@ def numEntries(self):

def __add__(self, other):
assert isinstance(other, GROGU_HISTO1D_V2.Bin)
## combine if the bins are adjacent
# if self.d_xmax == other.d_xmin:
# nxlow = self.d_xmin
# nxhigh = other.d_xmax
# elif self.d_xmin == other.d_xmax:
# nxlow = other.d_xmin
# nxhigh = self.d_xmax
return GROGU_HISTO1D_V2.Bin(
self.d_xmin,
self.d_xmax,
Expand All @@ -167,25 +161,23 @@ def from_string(cls, line: str) -> "GROGU_HISTO1D_V2.Bin":
float(values[5]),
float(values[6]),
)
else:
return cls(
float(values[0]),
float(values[1]),
float(values[2]),
float(values[3]),
float(values[4]),
float(values[5]),
float(values[6]),
)
return cls(
float(values[0]),
float(values[1]),
float(values[2]),
float(values[3]),
float(values[4]),
float(values[5]),
float(values[6]),
)

def to_string(bin, label=None) -> str:
"""Convert a Histo1DBin object to a formatted string."""
if label is None:
return f"{bin.d_xmin:.6e}\t{bin.d_xmax:.6e}\t{bin.d_sumw:.6e}\t{bin.d_sumw2:.6e}\t{bin.d_sumwx:.6e}\t{bin.d_sumwx2:.6e}\t{bin.d_numentries:.6e}"
else:
return f"{label}\t{label}\t{bin.d_sumw:.6e}\t{bin.d_sumw2:.6e}\t{bin.d_sumwx:.6e}\t{bin.d_sumwx2:.6e}\t{bin.d_numentries:.6e}"
return f"{label}\t{label}\t{bin.d_sumw:.6e}\t{bin.d_sumw2:.6e}\t{bin.d_sumwx:.6e}\t{bin.d_sumwx2:.6e}\t{bin.d_numentries:.6e}"

d_bins: List[Bin] = field(default_factory=list)
d_bins: list[Bin] = field(default_factory=list)
d_overflow: Optional[Bin] = None
d_underflow: Optional[Bin] = None

Expand Down Expand Up @@ -229,7 +221,7 @@ def xMin(self):

def bins(self, includeFlows=False):
if includeFlows:
return [self.d_underflow] + self.d_bins + [self.d_overflow]
return [self.d_underflow, *self.d_bins, self.d_overflow]
# TODO sorted needed here?
return sorted(self.d_bins, key=lambda b: b.d_xmin)

Expand All @@ -248,7 +240,7 @@ def binDim(self):
def xEdges(self):
return [b.xMin() for b in self.d_bins] + [self.xMax()]

def rebinXTo(self, edges: List[float]):
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}"
Expand Down
Loading

0 comments on commit c898a72

Please sign in to comment.