-
Notifications
You must be signed in to change notification settings - Fork 62
/
plots.py
229 lines (202 loc) · 6.58 KB
/
plots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import matplotlib.pyplot as plt
import pandas as pd
import re
benchmark_pattern = "(?P<system>(MonadBayes|Anglican|WebPPL))_(?P<model>(LR|HMM|LDA))(?P<length>[0-9]+)_(?P<alg>(SMC(?P<smcparam>[0-9]+$)|MH(?P<mhparam>[0-9]+$)|RMSMC(?P<rmsmcparam>[0-9]+-[0-9]+$)))"
benchmark_reg = re.compile(benchmark_pattern)
rmsmc_pattern = "(?P<particles>[0-9]+)-(?P<steps>[0-9]+)"
rmsmc_reg = re.compile(rmsmc_pattern)
def unpack_name(benchmark_name):
m = benchmark_reg.match(benchmark_name)
if m is None:
return None
def lookup(property_name):
return m.expand("\g<" + property_name + ">")
system = lookup("system")
model = lookup("model")
length = int(lookup("length"))
alg = lookup("alg")
if alg[:3] == "SMC":
alg_name = "SMC"
particles = int(lookup("smcparam"))
steps = 0
elif alg[:2] == "MH":
alg_name = "MH"
particles = 0
steps = int(lookup("mhparam"))
elif alg[:5] == "RMSMC":
alg_name = "RMSMC"
t = rmsmc_reg.match(lookup("rmsmcparam"))
particles = int(t.expand("\g<particles>"))
steps = int(t.expand("\g<steps>"))
else:
raise ValueError("Unrecognized algorithm: " + alg)
return system, model, length, alg_name, particles, steps
def unpack_names(series):
x = list(filter(lambda y: y is not None, [unpack_name(name) for name in series]))
systems = [y[0] for y in x]
models = [y[1] for y in x]
lengths = [y[2] for y in x]
algs = [y[3] for y in x]
particless = [y[4] for y in x]
stepss = [y[5] for y in x]
return pd.DataFrame(
{
"system": systems,
"model": models,
"length": lengths,
"alg": algs,
"particles": particless,
"steps": stepss,
}
)
def style(system):
if system == "MonadBayes":
return "ro"
elif system == "Anglican":
return "bs"
else:
return "gX"
models = ["LR", "HMM", "LDA"]
algs = ["MH", "SMC", "RMSMC"]
systems = ["MonadBayes", "Anglican", "WebPPL"]
# plot execution time vs. dataset size
benchmarks = pd.read_csv("speed-length.csv")
results = unpack_names(benchmarks["Name"])
results["time"] = benchmarks["Mean"]
results["timeLB"] = benchmarks["MeanLB"]
results["timeUB"] = benchmarks["MeanUB"]
mhsteps = 100
smcsize = 100
rmsize, rmsteps = 10, 1
fig, subplots = plt.subplots(nrows=len(models), ncols=len(algs), figsize=(12, 8))
lines = []
for i in range(len(models)):
model = models[i]
for j in range(len(algs)):
alg = algs[j]
subplot = subplots[i, j]
data = results.loc[(results["model"] == model) & (results["alg"] == alg)]
if alg == "MH":
data = data.loc[data["steps"] == mhsteps]
elif alg == "SMC":
data = data.loc[data["particles"] == smcsize]
else:
data = data.loc[(data["steps"] == rmsteps) & (data["particles"] == rmsize)]
for system in systems:
t = data.loc[data["system"] == system]
xs = t["length"]
ys = t["time"]
if model == "LDA":
# LDA has 5 documents
xs = xs * 5
(line,) = subplot.plot(xs, ys, style(system), label=system)
lines.append((line, system))
if i == len(models) - 1:
subplot.set_xlabel("Dataset size")
if j == 0:
subplot.set_ylabel("Execution time [s]")
pad = 5
algnames = [
"MH" + str(mhsteps),
"SMC" + str(smcsize),
"RMSMC" + str(rmsize) + "-" + str(rmsteps),
]
for ax, col in zip(subplots[0], algnames):
ax.annotate(
col,
xy=(0.5, 1),
xytext=(0, pad),
xycoords="axes fraction",
textcoords="offset points",
size="large",
ha="center",
va="baseline",
)
for ax, row in zip(subplots[:, 0], models):
ax.annotate(
row,
xy=(0, 0.5),
xytext=(-ax.yaxis.labelpad - pad, 0),
xycoords=ax.yaxis.label,
textcoords="offset points",
size="large",
ha="right",
va="center",
)
a, b = zip(*lines[:3])
b = ("Ours", b[1], b[2])
plt.figlegend(a, b, "upper right")
plt.savefig("length.pdf")
# plot execution time vs. # samples
benchmarks = pd.read_csv("speed-samples.csv")
results = unpack_names(benchmarks["Name"])
results["time"] = benchmarks["Mean"]
results["timeLB"] = benchmarks["MeanLB"]
results["timeUB"] = benchmarks["MeanUB"]
lrlength = 50
hmmlength = 20
ldalength = 10
rmparticles = 10
fig, subplots = plt.subplots(nrows=len(models), ncols=len(algs), figsize=(12, 8))
lines = []
for i in range(len(models)):
model = models[i]
for j in range(len(algs)):
alg = algs[j]
subplot = subplots[i, j]
data = results.loc[(results["model"] == model) & (results["alg"] == alg)]
if model == "LR":
data = data.loc[data["length"] == lrlength]
elif model == "HMM":
data = data.loc[data["length"] == hmmlength]
else:
data = data.loc[data["length"] == ldalength]
for system in systems:
t = data.loc[data["system"] == system]
if alg == "MH":
xs = t["steps"]
if i == len(models) - 1:
subplot.set_xlabel("Number of steps")
elif alg == "SMC":
xs = t["particles"]
if i == len(models) - 1:
subplot.set_xlabel("Number of particles")
else:
t = t.loc[t["particles"] == rmparticles]
xs = t["steps"]
if i == len(models) - 1:
subplot.set_xlabel("Number of rejuvenation steps")
ys = t["time"]
(line,) = subplot.plot(xs, ys, style(system), label=system)
lines.append((line, system))
if j == 0:
subplot.set_ylabel("Execution time [s]")
pad = 5
algnames = ["MH", "SMC", "RMSMC" + str(rmsize)]
for ax, col in zip(subplots[0], algnames):
ax.annotate(
col,
xy=(0.5, 1),
xytext=(0, pad),
xycoords="axes fraction",
textcoords="offset points",
size="large",
ha="center",
va="baseline",
)
modelnames = ["LR" + str(lrlength), "HMM" + str(hmmlength), "LDA" + str(ldalength * 5)]
for ax, row in zip(subplots[:, 0], modelnames):
ax.annotate(
row,
xy=(0, 0.5),
xytext=(-ax.yaxis.labelpad - pad, 0),
xycoords=ax.yaxis.label,
textcoords="offset points",
size="large",
ha="right",
va="center",
)
a, b = zip(*lines[:3])
b = ("Ours", b[1], b[2])
plt.figlegend(a, b, "upper right")
plt.savefig("samples.pdf")