Skip to content

Commit f838649

Browse files
committed
Make RooFit AD comparison plot with ROOT
1 parent add25d5 commit f838649

1 file changed

Lines changed: 107 additions & 30 deletions

File tree

root/roofit/roofit/benchCodeSquashAD_make_plot.py

Lines changed: 107 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99

1010
import numpy as np
11-
import matplotlib.pyplot as plt
1211
import pandas as pd
1312

1413

@@ -110,50 +109,128 @@ def line_to_seconds(l):
110109
df = pd.DataFrame(datas)
111110
df = df.query("nparams > 10 and nparams < 2500 and backend != 'legacy'")
112111

113-
114-
t = np.arange(0.01, 5.0, 0.01)
115-
s1 = np.sin(2 * np.pi * t)
116-
s2 = np.exp(-t)
117-
s3 = np.sin(4 * np.pi * t)
118-
119-
f, (a0, a1) = plt.subplots(2, 1, gridspec_kw={"height_ratios": [2, 1]})
120-
121112
dfs = dict()
122113

123-
124114
for backend, df_g in df.groupby("backend"):
125115
dfs[backend] = df_g
126116
nparams = df_g["nparams"]
127117
vals = df_g.eval("migrad + seeding")
128118
# vals = df_g.eval("fval")
129119
if backend == "codegen_no_grad":
130120
continue
131-
if backend == "RooFit AD":
132-
a0.plot(nparams, vals + 0 * df_g["jitting"], label=backend)
133-
a0.plot(nparams, df_g["jitting"], label="JIT time", color="k", linewidth=1, linestyle="--")
134-
else:
135-
a0.plot(nparams, vals, label=backend)
136-
# plt.plot(nparams, df_g["jitting"], label=backend + "_jit")
137121

138122
nparams = dfs["RooFit"]["nparams"]
139123

140-
a0.legend(loc="upper left")
124+
# Plotting with ROOT
125+
126+
import ROOT
127+
128+
# ----------------------------------------------------------------------
129+
# ROOT setup
130+
# ----------------------------------------------------------------------
131+
ROOT.gROOT.SetBatch(True)
132+
ROOT.gStyle.SetOptStat(0)
133+
134+
# c = ROOT.TCanvas("c", "Scaling study", 800, 900)
135+
c = ROOT.TCanvas("c", "Scaling study", 800, 700)
136+
# c = ROOT.TCanvas("c", "Scaling study", int(0.75 * 800), int(0.75 * 700))
137+
c.Divide(1, 2)
138+
139+
pad_top = c.cd(1)
140+
pad_top.SetPad(0.0, 0.32, 1.0, 1.0)
141+
pad_top.SetBottomMargin(0.15)
142+
143+
pad_bottom = c.cd(2)
144+
pad_bottom.SetPad(0.0, 0.0, 1.0, 0.32)
145+
pad_bottom.SetTopMargin(0.05)
146+
pad_bottom.SetBottomMargin(0.25)
147+
148+
# ----------------------------------------------------------------------
149+
# Convert grouped data to ROOT TGraphs
150+
# ----------------------------------------------------------------------
151+
graphs = {}
152+
jit_graphs = {}
153+
154+
colors = {
155+
"RooFit": ROOT.kBlue + 1,
156+
"Hardcoded": ROOT.kRed + 1,
157+
"RooFit AD": ROOT.kGreen + 2,
158+
}
159+
160+
for backend, df_g in df.groupby("backend"):
161+
x = df_g["nparams"].to_numpy()
162+
y = df_g.eval("migrad + seeding").to_numpy()
163+
164+
g = ROOT.TGraph(len(x), x.astype("float64"), y.astype("float64"))
165+
g.SetLineColor(colors.get(backend, ROOT.kBlack))
166+
g.SetLineWidth(3)
167+
# g.SetMarkerStyle(20)
168+
g.SetTitle("")
169+
graphs[backend] = g
170+
171+
if backend == "RooFit AD":
172+
yjit = df_g["jitting"].to_numpy()
173+
gj = ROOT.TGraph(len(x), x.astype("float64"), yjit.astype("float64"))
174+
gj.SetLineColor(ROOT.kBlack)
175+
gj.SetLineStyle(2)
176+
gj.SetLineWidth(2)
177+
jit_graphs[backend] = gj
178+
179+
# ----------------------------------------------------------------------
180+
# Draw top pad (timing)
181+
# ----------------------------------------------------------------------
182+
pad_top.cd()
183+
frame = pad_top.DrawFrame(0, 0, df["nparams"].max(), 30, ";Number of parameters;Minimization time [s]")
184+
185+
frame.GetXaxis().SetLabelSize(0.05)
186+
frame.GetXaxis().SetTitleSize(0.06)
187+
frame.GetYaxis().SetLabelSize(0.05)
188+
frame.GetYaxis().SetTitleSize(0.06)
189+
190+
legend = ROOT.TLegend(0.15, 0.65, 0.55, 0.88)
191+
legend.SetTextSize(0.05)
192+
193+
for backend, g in graphs.items():
194+
g.Draw("LP SAME")
195+
legend.AddEntry(g, backend, "LP")
196+
197+
if backend == "RooFit AD":
198+
jit_graphs[backend].Draw("L SAME")
199+
legend.AddEntry(jit_graphs[backend], "JIT time", "L")
200+
201+
legend.Draw()
202+
203+
# ----------------------------------------------------------------------
204+
# Compute speedup curve
205+
# ----------------------------------------------------------------------
206+
df_R = dfs["RooFit"]
207+
df_AD = dfs["RooFit AD"]
208+
209+
x = df_R["nparams"].to_numpy()
210+
speedup = df_R["migrad"].values / df_AD["migrad"].values
141211

142-
plt.tick_params("x", labelsize=6)
212+
g_speed = ROOT.TGraph(len(x), x.astype("float64"), speedup.astype("float64"))
213+
g_speed.SetLineWidth(2)
214+
g_speed.SetLineColor(ROOT.kBlack)
143215

144-
# make these tick labels invisible
145-
# plt.tick_params('x', labelbottom=False)
146-
vals = dfs["RooFit"]["migrad"].values / dfs["RooFit AD"]["migrad"].values
216+
# ----------------------------------------------------------------------
217+
# Draw bottom pad (speedup)
218+
# ----------------------------------------------------------------------
219+
pad_bottom.cd()
220+
frame2 = pad_bottom.DrawFrame(0, 1, df["nparams"].max(), 4.2, ";Number of parameters;AD speedup")
147221

148-
a1.plot(nparams, vals, color="k")
222+
# Make font sizes match the top panel
223+
frame2.GetXaxis().SetLabelSize(0.09)
224+
frame2.GetXaxis().SetTitleSize(0.10)
225+
frame2.GetYaxis().SetLabelSize(0.09)
226+
frame2.GetYaxis().SetTitleSize(0.10)
149227

150-
a1.set_xlabel("Number of parameters")
151-
a0.set_ylabel("Minimization time [s]")
152-
a0.set_ylim(0, 30)
153-
a0.set_xlim(0, nparams.to_numpy()[-1])
228+
frame2.GetYaxis().SetTitleOffset(0.4) # closer to the axis
154229

155-
a1.set_ylabel("AD speedup")
156-
a1.set_ylim(1, 4.2)
157-
a1.set_xlim(0, nparams.to_numpy()[-1])
230+
g_speed.Draw("L SAME")
158231

159-
plt.savefig("scaling_study.png")
232+
# ----------------------------------------------------------------------
233+
# Save result
234+
# ----------------------------------------------------------------------
235+
c.SaveAs("scaling_study_root.png")
236+
c.SaveAs("scaling_study_root.pdf")

0 commit comments

Comments
 (0)