Skip to content

refactor: refactor internal plottable histogram and allow comparision of plottable histograms (for pulls, ratio, ...) #573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ src/mplhep/_version.py
*.root
result_images/
test/
.nox/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
41 changes: 41 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from __future__ import annotations

import nox

nox.options.sessions = ["lint", "tests"]
nox.needs_version = ">=2025.2.9"
nox.options.default_venv_backend = "uv|venv"

PYTHON_ALL_VERSIONS = ["3.9", "3.13"]


@nox.session(reuse_venv=True)
def lint(session: nox.Session) -> None:
"""
Run the linter.
"""
session.install("pre-commit")
session.run("pre-commit", "run", "--all-files", *session.posargs)


@nox.session(python=PYTHON_ALL_VERSIONS)
def tests(session: nox.Session) -> None:
"""
Run the unit and regular tests.
"""
pyproject = nox.project.load_toml("pyproject.toml")
session.install("-e", ".")
session.install(*pyproject["project"]["optional-dependencies"]["test"])
session.run("pytest", *session.posargs)


@nox.session(venv_backend="conda", reuse_venv=True)
def root_tests(session):
"""
Test ROOT histograms. Note: a conda installation is needed to run this test.
"""
pyproject = nox.project.load_toml("pyproject.toml")
session.conda_install("--channel=conda-forge", "ROOT")
session.install("-e", ".")
session.install(*pyproject["project"]["optional-dependencies"]["test"])
session.run("pytest", "tests/test_make_plottable_histogram.py", *session.posargs)
10 changes: 9 additions & 1 deletion src/mplhep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
yscale_legend,
)
from .styles import set_style
from .utils import get_plottables
from .utils import (
EnhancedPlottableHistogram,
_check_counting_histogram,
get_plottables,
make_plottable_histogram,
)

# Configs
rcParams = Config(
Expand Down Expand Up @@ -78,4 +83,7 @@
"save_variations",
"set_style",
"get_plottables",
"EnhancedPlottableHistogram",
"make_plottable_histogram",
"_check_counting_histogram",
]
19 changes: 11 additions & 8 deletions src/mplhep/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ def iterable_not_string(arg):
if "step" in histtype:
for i in range(len(plottables)):
do_errors = yerr is not False and (
(yerr is not None or w2 is not None) or plottables[i]._has_variances
(yerr is not None or w2 is not None)
or plottables[i].variances() is not None
)

_kwargs = _chunked_kwargs[i]
Expand Down Expand Up @@ -378,7 +379,7 @@ def iterable_not_string(arg):

_b = ax.bar(
plottables[i].centers + _shift[i],
plottables[i].values,
plottables[i].values(),
width=_full_bin_width / len(plottables),
label=_step_label,
align="center",
Expand Down Expand Up @@ -423,7 +424,7 @@ def iterable_not_string(arg):

_b = ax.bar(
plottables[i].centers + _shift[i],
plottables[i].values,
plottables[i].values(),
width=_full_bin_width / len(plottables),
label=_labels[i],
align="center",
Expand Down Expand Up @@ -575,7 +576,7 @@ def iterable_not_string(arg):
xticklabels = _xticklabels

lw = ax.spines["bottom"].get_linewidth()
_edges = plottables[0].edges
_edges = plottables[0].edges_1d()
_centers = plottables[0].centers
_marker_size = (
20
Expand Down Expand Up @@ -818,7 +819,7 @@ def hist2dplot(
except TypeError as error:
if "got an unexpected keyword argument 'flow'" in str(error):
msg = (
f"The histograms value method {h!r} does not take a 'flow' argument. UHI Plottable doesn't require this to have, but it is required for this function."
f"The histograms value method {h!r} does not take a 'flow' argument. UHI PlottableHistogram doesn't require this to have, but it is required for this function."
f" Implementations like hist/boost-histogram support this argument."
)
raise TypeError(msg) from error
Expand Down Expand Up @@ -1009,9 +1010,11 @@ def hist2dplot(
ax.text(
xc,
yc,
_labels[iy, ix].round(labels_round)
if labels_round is not None
else _labels[iy, ix], # type: ignore[arg-type]
(
_labels[iy, ix].round(labels_round) # type: ignore[arg-type]
if labels_round is not None
else _labels[iy, ix]
),
ha="center",
va="center",
color=color,
Expand Down
Loading
Loading