Skip to content

Commit

Permalink
Add explicit forward functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Neuwirth committed Nov 6, 2024
1 parent e4a59de commit 7afb993
Show file tree
Hide file tree
Showing 17 changed files with 446 additions and 310 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ __pycache__
test.yoda
test.yoda.gz
docs/build
.coverage
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,3 @@ addopts = [
"--strict-markers",
"--strict-config",
]
filterwarnings = ["error"]
17 changes: 10 additions & 7 deletions src/babyyoda/analysisobject.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
class UHIAnalysisObject:
def key(self) -> str:
return self.path()

########
# Basic needed functions for UHI+YODA
########
def path(self) -> str:
err = "UHIAnalysisObject.path() must be implemented by subclass"
raise NotImplementedError(err)

def setAnnotationsDict(self, d: dict[str, str]) -> None:
for k, v in d.items():
self.setAnnotation(k, v)

def setAnnotation(self, key: str, value: str) -> None:
err = "UHIAnalysisObject.setAnnotation() must be implemented by subclass"
raise NotImplementedError(err)

def key(self) -> str:
return self.path()

def setAnnotationsDict(self, d: dict[str, str]) -> None:
for k, v in d.items():
self.setAnnotation(k, v)
4 changes: 2 additions & 2 deletions src/babyyoda/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def to_yoda_v3(self) -> Any:
def to_string(self) -> str:
# Now we need to map YODA to grogu and then call to_string
# TODO do we want to hardcode v3 here?
return self.to_grogu_v3().to_string()
return str(self.to_grogu_v3().to_string())

########################################################
# YODA compatibility code (dropped legacy code?)
Expand Down Expand Up @@ -138,7 +138,7 @@ def plot(self, *args: Any, **kwargs: Any) -> None:
# Show the plot
# plt.show()

def _ipython_display_(self) -> None:
def _ipython_display_(self) -> "UHICounter":
with contextlib.suppress(ImportError):
self.plot()
return self
44 changes: 22 additions & 22 deletions src/babyyoda/grogu/counter_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ class Bin:
# YODA compatibility code
########################################################

def clone(self):
def clone(self) -> "GROGU_COUNTER_V2.Bin":
return GROGU_COUNTER_V2.Bin(
d_sumw=self.d_sumw,
d_sumw2=self.d_sumw2,
d_numentries=self.d_numentries,
)

def fill(self, weight: float = 1.0, fraction: float = 1.0) -> bool:
def fill(self, weight: float = 1.0, fraction: float = 1.0) -> None:
sf = fraction * weight
self.d_sumw += sf
self.d_sumw2 += sf * weight
self.d_numentries += fraction

def set_bin(self, bin):
def set_bin(self, bin: Any) -> None:
self.d_sumw = bin.sumW()
self.d_sumw2 = bin.sumW2()
self.d_numentries = bin.numEntries()
Expand All @@ -49,7 +49,7 @@ def set(
numEntries: float,
sumW: Union[list[float], float],
sumW2: Union[list[float], float],
):
) -> None:
if isinstance(sumW, float):
sumW = [sumW]
if isinstance(sumW2, float):
Expand All @@ -60,13 +60,13 @@ def set(
self.d_sumw2 = sumW2[0]
self.d_numentries = numEntries

def sumW(self):
def sumW(self) -> float:
return self.d_sumw

def sumW2(self):
def sumW2(self) -> float:
return self.d_sumw2

def variance(self):
def variance(self) -> float:
if self.d_sumw**2 - self.d_sumw2 == 0:
return 0
return abs(
Expand All @@ -75,30 +75,30 @@ def variance(self):
)
# return self.d_sumw2/self.d_numentries - (self.d_sumw/self.d_numentries)**2

def errW(self):
def errW(self) -> Any:
return self.d_sumw2**0.5

def stdDev(self):
def stdDev(self) -> Any:
return self.variance() ** 0.5

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

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

def numEntries(self):
def numEntries(self) -> float:
return self.d_numentries

def __eq__(self, other):
def __eq__(self, other: object) -> bool:
return (
isinstance(other, GROGU_COUNTER_V2.Bin)
and self.d_sumw == other.d_sumw
and self.d_sumw2 == other.d_sumw2
and self.d_numentries == other.d_numentries
)

def __add__(self, other):
def __add__(self, other: Any) -> "GROGU_COUNTER_V2.Bin":
assert isinstance(other, GROGU_COUNTER_V2.Bin)
return GROGU_COUNTER_V2.Bin(
self.d_sumw + other.d_sumw,
Expand Down Expand Up @@ -128,30 +128,30 @@ def __post_init__(self) -> None:
# YODA compatibility code
############################################

def sumW(self):
def sumW(self) -> float:
return self.d_bins[0].sumW()

def sumW2(self):
def sumW2(self) -> float:
return self.d_bins[0].sumW2()

def numEntries(self):
def numEntries(self) -> float:
return self.d_bins[0].numEntries()

def clone(self):
def clone(self) -> "GROGU_COUNTER_V2":
return GROGU_COUNTER_V2(
d_key=self.d_key,
d_annotations=self.annotationsDict(),
d_bins=[b.clone() for b in self.d_bins],
)

def fill(self, weight=1.0, fraction=1.0):
def fill(self, weight: float = 1.0, fraction: float = 1.0) -> None:
for b in self.bins():
b.fill(weight=weight, fraction=fraction)

def set(self, *args, **kwargs):
def set(self, *args: Any, **kwargs: Any) -> None:
self.d_bins[0].set(*args, **kwargs)

def bins(self):
def bins(self) -> list[Bin]:
return self.d_bins

@classmethod
Expand Down Expand Up @@ -191,7 +191,7 @@ def from_string(cls, file_content: str) -> "GROGU_COUNTER_V2":
d_bins=bins,
)

def to_string(self):
def to_string(self) -> str:
"""Convert a YODA_COUNTER_V2 object to a formatted string."""
header = (
f"BEGIN YODA_COUNTER_V2 {self.d_key}\n"
Expand Down
32 changes: 16 additions & 16 deletions src/babyyoda/grogu/counter_v3.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import re
from dataclasses import dataclass, field
from typing import Optional, Union
from typing import Any, Optional, Union

from babyyoda.counter import UHICounter
from babyyoda.grogu.analysis_object import GROGU_ANALYSIS_OBJECT


def Counter_v3(title: Optional[str] = None, **kwargs) -> "GROGU_COUNTER_V3":
def Counter_v3(title: Optional[str] = None, **kwargs: Any) -> "GROGU_COUNTER_V3":
return GROGU_COUNTER_V3(
d_bins=[GROGU_COUNTER_V3.Bin()],
d_annotations={"Title": title} if title else {},
Expand All @@ -26,20 +26,20 @@ class Bin:
# YODA compatibility code
########################################################

def clone(self):
def clone(self) -> "GROGU_COUNTER_V3.Bin":
return GROGU_COUNTER_V3.Bin(
d_sumw=self.d_sumw,
d_sumw2=self.d_sumw2,
d_numentries=self.d_numentries,
)

def fill(self, weight: float = 1.0, fraction: float = 1.0) -> bool:
def fill(self, weight: float = 1.0, fraction: float = 1.0) -> None:
sf = fraction * weight
self.d_sumw += sf
self.d_sumw2 += sf * weight
self.d_numentries += fraction

def set_bin(self, bin):
def set_bin(self, bin: Any) -> None:
self.d_sumw = bin.sumW()
self.d_sumw2 = bin.sumW2()
self.d_numentries = bin.numEntries()
Expand All @@ -49,7 +49,7 @@ def set(
numEntries: float,
sumW: Union[list[float], float],
sumW2: Union[list[float], float],
):
) -> None:
if isinstance(sumW, float):
sumW = [sumW]
if isinstance(sumW2, float):
Expand All @@ -75,30 +75,30 @@ def variance(self) -> float:
)
# return self.d_sumw2/self.d_numentries - (self.d_sumw/self.d_numentries)**2

def errW(self) -> float:
def errW(self) -> Any:
return self.d_sumw2**0.5

def stdDev(self) -> float:
def stdDev(self) -> Any:
return self.variance() ** 0.5

def effNumEntries(self) -> float:
def effNumEntries(self) -> Any:
return self.sumW() ** 2 / self.sumW2()

def stdErr(self) -> float:
def stdErr(self) -> Any:
return self.stdDev() / self.effNumEntries() ** 0.5

def numEntries(self) -> float:
return self.d_numentries

def __eq__(self, other):
def __eq__(self, other: object) -> bool:
return (
isinstance(other, GROGU_COUNTER_V3.Bin)
and self.d_sumw == other.d_sumw
and self.d_sumw2 == other.d_sumw2
and self.d_numentries == other.d_numentries
)

def __add__(self, other):
def __add__(self, other: Any) -> "GROGU_COUNTER_V3.Bin":
assert isinstance(other, GROGU_COUNTER_V3.Bin)
return GROGU_COUNTER_V3.Bin(
self.d_sumw + other.d_sumw,
Expand Down Expand Up @@ -137,21 +137,21 @@ def sumW2(self) -> float:
def numEntries(self) -> float:
return self.d_bins[0].numEntries()

def clone(self):
def clone(self) -> "GROGU_COUNTER_V3":
return GROGU_COUNTER_V3(
d_key=self.d_key,
d_annotations=self.annotationsDict(),
d_bins=[b.clone() for b in self.d_bins],
)

def fill(self, weight=1.0, fraction=1.0):
def fill(self, weight: float = 1.0, fraction: float = 1.0) -> None:
for b in self.bins():
b.fill(weight=weight, fraction=fraction)

def set(self, *args, **kwargs):
def set(self, *args: Any, **kwargs: Any) -> None:
self.d_bins[0].set(*args, **kwargs)

def bins(self):
def bins(self) -> list[Bin]:
return self.d_bins

@classmethod
Expand Down
Loading

0 comments on commit 7afb993

Please sign in to comment.