Skip to content

Commit 4e078c1

Browse files
0ctagoncyrrazpre-commit-ci[bot]andrzejnovak
authored
feat: add comparison plotters (#580)
* feat: add comparison plotter functions Co-authored-by: cyrraz <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * feat: added model and data/model comparison plotting functions Co-authored-by: cyrraz <[email protected]> * test: add examples of 1d histogram comparisons and test them --------- Co-authored-by: Tristan Fillinger <[email protected]> * test: fix path to image examples Co-authored-by: Tristan Fillinger <[email protected]> * chore: silence some ruff errors Co-authored-by: Tristan Fillinger <[email protected]> * test: add examples of data model comparisons and test them Co-authored-by: cyrraz <[email protected]> * refactor: improve code formatting and organization in comparison scripts * ci: update macOS-14 Python version to 3.9 to resolve conflicts with plothist_utils * fix: switch print to logging in docs --------- Co-authored-by: cyrraz <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Andrzej Novak <[email protected]>
1 parent 8a3ab97 commit 4e078c1

File tree

49 files changed

+2597
-13
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2597
-13
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ jobs:
1818
fail-fast: false
1919
matrix:
2020
runs-on: [ ubuntu-latest ]
21-
python-version: [ "3.8", "3.10", "3.11", "3.12", "3.13"]
21+
python-version: ["3.10", "3.11", "3.12", "3.13"] # "3.8" conflicts with plothist_utils for now
2222
include: # testing the last "reasonable supported" version
2323
- runs-on: macOS-13
2424
python-version: "3.9"
2525
- runs-on: macOS-14
26-
python-version: "3.8" # min version available for Apple silicon
26+
python-version: "3.9" # min version available for Apple silicon # changed to 3.9 as conflicts with plothist_utils for now
2727
- runs-on: windows-latest
2828
python-version: "3.9"
2929

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ src/mplhep/_version.py
44
result_images/
55
test/
66
.nox/
7+
.vscode/
78

89
# Byte-compiled / optimized / DLL files
910
__pycache__/

docs/source/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# -- Path setup --------------------------------------------------------------
1010
import importlib
1111
import inspect
12+
import logging
1213
import os
1314
import subprocess
1415
import sys
@@ -152,7 +153,7 @@ def linkcode_resolve(domain, info):
152153

153154
with Path(here / "_static/bkg_sig_plot.yaml").resolve().open() as f:
154155
plotdata = yaml.load(f, Loader=yaml.FullLoader)
155-
print("Creating gallery plots on the fly... (this takes a minute or two)")
156+
logging.info("Creating gallery plots on the fly... (this takes a minute or two)")
156157
for style in allstyle:
157158
plt.style.use("default") # make sure it's reset
158159
plt.style.use(getattr(mplhep.style, style))
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Asymmetry
3+
=========
4+
5+
Compare two 1D histograms using the asymmetry comparison [(h1-h2)/(h1+h2)].
6+
"""
7+
8+
from plothist_utils import get_dummy_data
9+
10+
df = get_dummy_data()
11+
12+
name = "variable_1"
13+
14+
x1 = df[name][df["category"] == 2]
15+
x2 = df[name][df["category"] == 3]
16+
17+
x_range = (min(*x1, *x2), max(*x1, *x2))
18+
19+
import hist
20+
from hist import Hist
21+
22+
h1 = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
23+
h2 = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
24+
25+
h1.fill(x1)
26+
h2.fill(x2)
27+
28+
###
29+
from mplhep import plot_two_hist_comparison
30+
31+
fig, ax_main, ax_comparison = plot_two_hist_comparison(
32+
h1,
33+
h2,
34+
xlabel=name,
35+
ylabel="Entries",
36+
h1_label=r"$\mathbfit{h}_1$",
37+
h2_label=r"$\mathbfit{h}_2$",
38+
comparison="asymmetry", # <--
39+
)
40+
41+
fig.savefig("1d_comparison_asymmetry.svg", bbox_inches="tight")
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Difference
3+
==========
4+
5+
Compare two 1D histograms using the difference [h1-h2].
6+
"""
7+
8+
from plothist_utils import get_dummy_data
9+
10+
df = get_dummy_data()
11+
12+
name = "variable_1"
13+
14+
x1 = df[name][df["category"] == 2]
15+
x2 = df[name][df["category"] == 3]
16+
17+
x_range = (min(*x1, *x2), max(*x1, *x2))
18+
19+
import hist
20+
from hist import Hist
21+
22+
h1 = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
23+
h2 = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
24+
25+
h1.fill(x1)
26+
h2.fill(x2)
27+
28+
###
29+
from mplhep import add_text, plot_two_hist_comparison
30+
31+
fig, ax_main, ax_comparison = plot_two_hist_comparison(
32+
h1,
33+
h2,
34+
xlabel=name,
35+
ylabel="Entries",
36+
h1_label=r"$\mathcal{H}_{1}$",
37+
h2_label=r"$\mathcal{H}_{2}$",
38+
comparison="difference", # <--
39+
)
40+
41+
add_text("Comparison of two hist with difference plot", ax=ax_main)
42+
add_text("Difference ax", x="right", ax=ax_comparison)
43+
44+
fig.savefig("1d_comparison_difference.svg", bbox_inches="tight")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Efficiency
3+
==========
4+
5+
Compare the ratio between two histograms h1 and h2 when the entries of h1 are a subset of the entries of h2.
6+
"""
7+
8+
from plothist_utils import get_dummy_data
9+
10+
df = get_dummy_data()
11+
12+
name = "variable_1"
13+
14+
x_total = df[name][df["category"] == 2]
15+
x_sample = x_total[: int(len(x_total) * 0.75)]
16+
17+
x_range = (min(x_total), max(x_total))
18+
19+
import hist
20+
from hist import Hist
21+
22+
h_sample = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
23+
h_total = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
24+
25+
h_sample.fill(x_sample)
26+
h_total.fill(x_total)
27+
28+
###
29+
from mplhep import plot_two_hist_comparison
30+
31+
fig, ax_main, ax_comparison = plot_two_hist_comparison(
32+
h_sample,
33+
h_total,
34+
xlabel=name,
35+
ylabel="Entries",
36+
h1_label=r"$\mathit{H}_{Sample}$",
37+
h2_label=r"$\mathit{H}_{Total}$",
38+
comparison="efficiency", # <--
39+
)
40+
41+
fig.savefig("1d_comparison_efficiency.svg", bbox_inches="tight")
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Comparison
3+
==========
4+
5+
Plot the comparison between two 1D histograms.
6+
"""
7+
8+
from plothist_utils import get_dummy_data
9+
10+
df = get_dummy_data()
11+
12+
name = "variable_1"
13+
14+
x_total = df[name][df["category"] == 2]
15+
x_sample = x_total[: int(len(x_total) * 0.75)]
16+
17+
x_range = (min(x_total), max(x_total))
18+
19+
import hist
20+
from hist import Hist
21+
22+
h_sample = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
23+
h_total = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
24+
25+
h_sample.fill(x_sample)
26+
h_total.fill(x_total)
27+
28+
###
29+
import matplotlib.pyplot as plt
30+
31+
from mplhep import plot_comparison
32+
33+
fig, ax = plt.subplots()
34+
35+
plot_comparison(h_sample, h_total, ax=ax, xlabel=name, comparison="efficiency")
36+
37+
fig.savefig("1d_comparison_only_efficiency.svg", bbox_inches="tight")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Pull
3+
====
4+
5+
Compare two 1D histograms using the pull method.
6+
"""
7+
8+
from plothist_utils import get_dummy_data
9+
10+
df = get_dummy_data()
11+
12+
name = "variable_1"
13+
14+
x1 = df[name][df["category"] == 2]
15+
x2 = df[name][df["category"] == 3]
16+
17+
x_range = (min(*x1, *x2), max(*x1, *x2))
18+
19+
import hist
20+
from hist import Hist
21+
22+
h1 = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
23+
h2 = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
24+
25+
h1.fill(x1)
26+
h2.fill(x2)
27+
28+
###
29+
from mplhep import plot_two_hist_comparison
30+
31+
fig, ax_main, ax_comparison = plot_two_hist_comparison(
32+
h1,
33+
h2,
34+
xlabel=name,
35+
ylabel="Entries",
36+
h1_label=r"$h_1$",
37+
h2_label=r"$h_2$",
38+
comparison="pull", # <---
39+
)
40+
41+
fig.savefig("1d_comparison_pull.svg", bbox_inches="tight")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Ratio
3+
=====
4+
5+
Compare two 1D histograms using the ratio [h1/h2].
6+
"""
7+
8+
from plothist_utils import get_dummy_data
9+
10+
df = get_dummy_data()
11+
12+
name = "variable_1"
13+
14+
x1 = df[name][df["category"] == 2]
15+
x2 = df[name][df["category"] == 3]
16+
17+
x_range = (min(*x1, *x2), max(*x1, *x2))
18+
19+
import hist
20+
from hist import Hist
21+
22+
h1 = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
23+
h2 = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
24+
25+
h1.fill(x1)
26+
h2.fill(x2)
27+
28+
###
29+
from mplhep import plot_two_hist_comparison
30+
31+
# Default comparison is ratio
32+
fig, ax_main, ax_comparison = plot_two_hist_comparison(
33+
h1,
34+
h2,
35+
xlabel=name,
36+
ylabel="Entries",
37+
h1_label="h1",
38+
h2_label="h2",
39+
)
40+
41+
fig.savefig("1d_comparison_ratio.svg", bbox_inches="tight")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Relative difference
3+
===================
4+
5+
Compare two 1D histograms using the relative difference [(h1-h2)/h2].
6+
"""
7+
8+
from plothist_utils import get_dummy_data
9+
10+
df = get_dummy_data()
11+
12+
name = "variable_1"
13+
14+
x1 = df[name][df["category"] == 2]
15+
x2 = df[name][df["category"] == 3]
16+
17+
x_range = (min(*x1, *x2), max(*x1, *x2))
18+
19+
import hist
20+
from hist import Hist
21+
22+
h1 = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
23+
h2 = Hist(hist.axis.Regular(50, x_range[0], x_range[1]))
24+
25+
h1.fill(x1)
26+
h2.fill(x2)
27+
28+
###
29+
from mplhep import plot_two_hist_comparison
30+
31+
fig, ax_main, ax_comparison = plot_two_hist_comparison(
32+
h1,
33+
h2,
34+
xlabel=name,
35+
ylabel="Entries",
36+
h1_label=r"$\mathbf{H\,\,1}$",
37+
h2_label=r"$\mathbf{H\,\,2}$",
38+
comparison="relative_difference", # <--
39+
)
40+
41+
fig.savefig("1d_comparison_relative_difference.svg", bbox_inches="tight")

0 commit comments

Comments
 (0)