Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions examples/1d_histograms/1d_comparison_asymmetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# --8<-- [start:imports]
import hist
import numpy as np
from hist import Hist

import mplhep as mh

Expand All @@ -21,11 +20,9 @@
x1 = np.r_[np.random.normal(0.4, 0.1, 5000), np.random.normal(0.7, 0.1, 5000)]
x2 = np.r_[np.random.normal(0.4, 0.1, 1000), np.random.normal(0.7, 0.11, 7000)]

# Create and fill histograms"
h1 = Hist(hist.axis.Regular(50, 0, 1), storage=hist.storage.Weight()) # Long interface
h2 = hist.new.Regular(50, 0, 1).Weight() # Shorthand interface
h1.fill(x1)
h2.fill(x2)
# Create and fill histograms
h1 = hist.new.Regular(50, 0, 1).Weight().fill(x1)
h2 = hist.new.Regular(50, 0, 1).Weight().fill(x2)
# --8<-- [end:setup]

# --8<-- [start:plot_body]
Expand Down
15 changes: 5 additions & 10 deletions examples/1d_histograms/1d_comparison_difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# --8<-- [start:imports]
import hist
import numpy as np
from hist import Hist

import mplhep as mh

Expand All @@ -21,17 +20,13 @@
x1 = np.r_[np.random.normal(0.4, 0.1, 5000), np.random.normal(0.7, 0.1, 5000)]
x2 = np.r_[np.random.normal(0.4, 0.1, 1000), np.random.normal(0.7, 0.11, 7000)]

# Create and fill histograms"
h1 = Hist(hist.axis.Regular(50, 0, 1), storage=hist.storage.Weight()) # Long interface
h2 = hist.new.Regular(50, 0, 1).Weight() # Shorthand interface
h1.fill(x1)
h2.fill(x2)
# Create and fill histograms
h1 = hist.new.Regular(50, 0, 1).Weight().fill(x1)
h2 = hist.new.Regular(50, 0, 1).Weight().fill(x2)
# --8<-- [end:setup]


# --8<-- [start:plot_body]
from mplhep import add_text

fig, ax_main, ax_comparison = mh.comp.hists(
h1,
h2,
Expand All @@ -42,13 +37,13 @@
comparison="difference", # <--
)

add_text(
mh.add_text(
"Comparison of two hist with difference plot",
ax=ax_main,
fontsize="small",
loc="over left",
)
add_text("Difference ax", ax=ax_comparison, loc="over right", fontsize="small")
mh.add_text("Difference ax", ax=ax_comparison, loc="over right", fontsize="small")

# --8<-- [end:plot_body]
# --8<-- [end:full_code]
Expand Down
10 changes: 2 additions & 8 deletions examples/1d_histograms/1d_comparison_efficiency.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# --8<-- [start:imports]
import hist
import numpy as np
from hist import Hist

import mplhep as mh

Expand All @@ -22,16 +21,11 @@
x_sample = x_total[:7500] # 75% subset

# Create and fill histograms
h_sample = Hist(
hist.axis.Regular(50, 0, 1), storage=hist.storage.Weight()
) # Long interface
h_total = hist.new.Regular(50, 0, 1).Weight() # Shorthand interface
h_sample.fill(x_sample)
h_total.fill(x_total)
h_sample = hist.new.Regular(50, 0, 1).Weight().fill(x_sample)
h_total = hist.new.Regular(50, 0, 1).Weight().fill(x_total)
# --8<-- [end:setup]

# --8<-- [start:plot_body]
###
fig, ax_main, ax_comparison = mh.comp.hists(
h_sample,
h_total,
Expand Down
10 changes: 2 additions & 8 deletions examples/1d_histograms/1d_comparison_only_efficiency.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import hist
import matplotlib.pyplot as plt
import numpy as np
from hist import Hist

import mplhep as mh

Expand All @@ -23,16 +22,11 @@
x_sample = x_total[:7500] # 75% subset

# Create and fill histograms
h_sample = Hist(
hist.axis.Regular(50, 0, 1), storage=hist.storage.Weight()
) # Long interface
h_total = hist.new.Regular(50, 0, 1).Weight() # Shorthand interface
h_sample.fill(x_sample)
h_total.fill(x_total)
h_sample = hist.new.Regular(50, 0, 1).Weight().fill(x_sample)
h_total = hist.new.Regular(50, 0, 1).Weight().fill(x_total)
# --8<-- [end:setup]

# --8<-- [start:plot_body]
# Plot comparison using efficiency
fig, ax = plt.subplots()
mh.comp.comparison(h_sample, h_total, ax=ax, xlabel="Variable", comparison="efficiency")

Expand Down
8 changes: 2 additions & 6 deletions examples/1d_histograms/1d_comparison_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# --8<-- [start:imports]
import hist
import numpy as np
from hist import Hist

import mplhep as mh

Expand All @@ -22,14 +21,11 @@
x2 = np.r_[np.random.normal(0.4, 0.1, 1000), np.random.normal(0.7, 0.11, 7000)]

# Create and fill histograms
h1 = Hist(hist.axis.Regular(50, 0, 1), storage=hist.storage.Weight()) # Long interface
h2 = hist.new.Regular(50, 0, 1).Weight() # Shorthand interface
h1.fill(x1)
h2.fill(x2)
h1 = hist.new.Regular(50, 0, 1).Weight().fill(x1)
h2 = hist.new.Regular(50, 0, 1).Weight().fill(x2)
# --8<-- [end:setup]

# --8<-- [start:plot_body]
###
fig, ax_main, ax_comparison = mh.comp.hists(
h1,
h2,
Expand Down
8 changes: 2 additions & 6 deletions examples/1d_histograms/1d_comparison_ratio.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# --8<-- [start:imports]
import hist
import numpy as np
from hist import Hist

import mplhep as mh

Expand All @@ -22,14 +21,11 @@
x2 = np.r_[np.random.normal(0.4, 0.1, 1000), np.random.normal(0.7, 0.11, 7000)]

# Create and fill histograms
h1 = Hist(hist.axis.Regular(50, 0, 1), storage=hist.storage.Weight()) # Long interface
h2 = hist.new.Regular(50, 0, 1).Weight() # Shorthand interface
h1.fill(x1)
h2.fill(x2)
h1 = hist.new.Regular(50, 0, 1).Weight().fill(x1)
h2 = hist.new.Regular(50, 0, 1).Weight().fill(x2)
# --8<-- [end:setup]

# --8<-- [start:plot_body]
###
fig, ax_main, ax_comparison = mh.comp.hists(
h1,
h2,
Expand Down
8 changes: 2 additions & 6 deletions examples/1d_histograms/1d_comparison_relative_difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# --8<-- [start:imports]
import hist
import numpy as np
from hist import Hist

import mplhep as mh

Expand All @@ -22,14 +21,11 @@
x2 = np.r_[np.random.normal(0.4, 0.1, 1000), np.random.normal(0.7, 0.11, 7000)]

# Create and fill histograms
h1 = Hist(hist.axis.Regular(50, 0, 1), storage=hist.storage.Weight()) # Long interface
h2 = hist.new.Regular(50, 0, 1).Weight() # Shorthand interface
h1.fill(x1)
h2.fill(x2)
h1 = hist.new.Regular(50, 0, 1).Weight().fill(x1)
h2 = hist.new.Regular(50, 0, 1).Weight().fill(x2)
# --8<-- [end:setup]

# --8<-- [start:plot_body]
###
fig, ax_main, ax_comparison = mh.comp.hists(
h1,
h2,
Expand Down
8 changes: 2 additions & 6 deletions examples/1d_histograms/1d_comparison_split_ratio.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# --8<-- [start:imports]
import hist
import numpy as np
from hist import Hist

import mplhep as mh

Expand All @@ -22,14 +21,11 @@
x2 = np.r_[np.random.normal(0.4, 0.1, 1000), np.random.normal(0.7, 0.11, 7000)]

# Create and fill histograms
h1 = Hist(hist.axis.Regular(50, 0, 1), storage=hist.storage.Weight()) # Long interface
h2 = hist.new.Regular(50, 0, 1).Weight() # Shorthand interface
h1.fill(x1)
h2.fill(x2)
h1 = hist.new.Regular(50, 0, 1).Weight().fill(x1)
h2 = hist.new.Regular(50, 0, 1).Weight().fill(x2)
# --8<-- [end:setup]

# --8<-- [start:plot_body]
###
fig, ax_main, ax_comparison = mh.comp.hists(
h1,
h2,
Expand Down
101 changes: 39 additions & 62 deletions examples/model_ex/model_all_comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,58 @@

# --8<-- [start:full_code]
# --8<-- [start:imports]
from plothist_utils import get_dummy_data

df = get_dummy_data()

import hist
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

import mplhep as mh

np.random.seed(42)
# --8<-- [end:imports]

# --8<-- [start:setup]
# Define the histograms

key = "variable_1"
x_range = (-9, 12)
category = "category"

# Define masks
signal_mask = df[category] == 7
data_mask = df[category] == 8

background_categories = [0, 1, 2]
background_categories_labels = [f"c{i}" for i in background_categories]
background_categories_colors = sns.color_palette(
"cubehelix", len(background_categories)
# Create data histogram with mixed components
data_hist = hist.new.Regular(50, -8, 8).Weight()
data_hist.fill(
np.concatenate(
[
np.random.normal(0, 2, 3000),
np.random.normal(-3, 0.8, 1500),
np.random.normal(-2, 1.5, 1200),
np.random.normal(0, 0.5, 300),
]
)
)

background_masks = [df[category] == p for p in background_categories]

# Make histograms
import hist
from hist import Hist
# Create background component histograms
background_hists = [
hist.new.Regular(50, -8, 8).Weight().fill(np.random.normal(0, 2, 3500)),
hist.new.Regular(50, -8, 8).Weight().fill(np.random.normal(-3, 0.8, 1800)),
hist.new.Regular(50, -8, 8).Weight().fill(np.random.normal(-2, 1.5, 1400)),
]

axis = hist.axis.Regular(50, x_range[0], x_range[1])

data_hist = Hist(axis, storage=hist.storage.Weight())
signal_hist = Hist(axis, storage=hist.storage.Weight())
background_hists = []

data_hist.fill(df[key][data_mask])
signal_hist.fill(df[key][signal_mask])

for mask in background_masks:
h_bkg = Hist(axis, storage=hist.storage.Weight())
h_bkg.fill(df[key][mask])
background_hists.append(h_bkg)

# Optional: scale to data
background_scaling_factor = data_hist.sum().value / sum(background_hists).sum().value
background_hists = [background_scaling_factor * h for h in background_hists]
# Scale backgrounds to match data
scale = data_hist.sum().value / sum(background_hists).sum().value
background_hists = [scale * h for h in background_hists]
# --8<-- [end:setup]

# --8<-- [start:plot_body]
###
import matplotlib.pyplot as plt

from mplhep import comp

fig, axes = plt.subplots(
nrows=6,
figsize=(6, 13),
gridspec_kw={"height_ratios": [3, 1, 1, 1, 1, 1]},
nrows=6, figsize=(6, 13), gridspec_kw={"height_ratios": [3, 1, 1, 1, 1, 1]}
)
fig.subplots_adjust(hspace=0.3)

# Hide x-axis labels for all but the bottom plot
for ax in axes[:-1]:
ax.xaxis.set_ticklabels([])
ax.set_xlabel(" ")
background_sum = sum(background_hists)

comp.data_model(
mh.comp.data_model(
data_hist=data_hist,
stacked_components=background_hists,
stacked_labels=background_categories_labels,
stacked_colors=background_categories_colors,
stacked_labels=["c0", "c1", "c2"],
stacked_colors=sns.color_palette("cubehelix", 3),
xlabel="",
ylabel="Entries",
comparison="ratio",
Expand All @@ -99,28 +77,27 @@
r' $\mathbf{→}$ comparison = "ratio"', ax=axes[1], loc="over left", fontsize=13
)

for k_comp, comparison in enumerate(
# Add remaining comparison types
for k, comp in enumerate(
["split_ratio", "pull", "relative_difference", "difference"], start=2
):
ax_comparison = axes[k_comp]

comp.comparison(
mh.comp.comparison(
data_hist,
background_sum,
ax=ax_comparison,
comparison=comparison,
sum(background_hists),
ax=axes[k],
comparison=comp,
xlabel="",
h1_label="Data",
h2_label="MC",
h1_w2method="poisson",
)
mh.add_text(
rf' $\mathbf{{→}}$ comparison = "{comparison}"',
ax=ax_comparison,
rf' $\mathbf{{→}}$ comparison = "{comp}"',
ax=axes[k],
fontsize=13,
loc="over left",
)
mh.set_fitting_ylabel_fontsize(ax_comparison)
mh.set_fitting_ylabel_fontsize(axes[k])

axes[-1].set_xlabel("Observable")
# --8<-- [end:plot_body]
Expand Down
6 changes: 2 additions & 4 deletions examples/model_ex/model_all_comparisons_no_model_unc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from hist import Hist

import mplhep as mh

Expand All @@ -35,14 +34,13 @@
np.random.normal(0, 2.5, 3500), # Less background here
np.random.normal(3, 1.2, 1800), # Similar background
np.random.normal(-1, 1.8, 1400), # Similar background
np.random.normal(5, 0.8, 800), # Clear signal peak
np.random.normal(0, 0.8, 500), # Clear signal peak
np.random.normal(-3, 0.5, 200), # Some deficit here (under-predicted)
]
)

# Create histograms
data_hist = Hist(hist.axis.Regular(50, -8, 8), storage=hist.storage.Weight())
data_hist.fill(data_data)
data_hist = hist.new.Regular(50, -8, 8).Weight().fill(data_data)

background_hists = [
hist.new.Regular(50, -8, 8).Weight().fill(bkg1_data),
Expand Down
Loading
Loading