forked from microsoft/hash-modulo-alpha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
241 lines (197 loc) · 7.4 KB
/
plot.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
230
231
232
233
234
235
236
237
238
239
240
241
import math
from typing import Dict
import argparse
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
def make_plot(filename: str, descr: str, bert_plot: bool, results: Dict[str, str]):
# Some elements may benefit from slightly larger font sizes - define these here.
LEGEND_FONT_SIZE = 14
AXIS_LABEL_FONT_SIZE = 15
# Get colors from the default color map.
prop_cycle = plt.rcParams["axes.prop_cycle"]
colors = prop_cycle.by_key()["color"]
# Mapping from algorithms to colors.
color_map = {
"LN": colors[0],
"Ours": colors[2],
"DeBruijn": colors[1],
"Structural": colors[3],
}
# Mapping from algorithms to marker shapes.
marker_map = {"LN": "+", "Ours": "o", "DeBruijn": "^", "Structural": "s"}
# Mapping from algorithms to how they are shown in the legend.
display_name = {
"LN": "Locally Nameless",
"Ours": "Ours",
"DeBruijn": "De Bruijn*",
"Structural": "Structural*",
}
for algo, data_file in results.items():
lines = [line.rstrip().split() for line in open(data_file, "rt")]
lines = sorted([[float(x) for x in line] for line in lines])
[xs, ys] = zip(*lines)
marker = marker_map[algo]
color = color_map[algo]
if bert_plot:
# For BERT examples, use markers connected by line segments.
plt.plot(
xs,
ys,
label=display_name[algo],
markersize=6.0,
color=color,
markerfacecolor="none" if marker not in ["+", "x"] else color,
marker=marker,
markeredgewidth=0.6,
linewidth=0.3,
)
else:
# Since we generate too much data, take every 4th result.
xs = [xs[i] for i in range(0, len(xs), 4)]
ys = [ys[i] for i in range(0, len(ys), 4)]
# Non-BERT examples have a bit of noise, so leave the markers unconnected.
plt.scatter(
xs,
ys,
label=display_name[algo],
s=30.0,
edgecolors=color,
facecolors="none" if marker not in ["+", "x"] else color,
marker=marker,
linewidth=0.6,
)
# Define axis limits, anchor point for the arrows, and hand-picked shifts (explained later).
if bert_plot:
xlim = (1470, 24500)
ylim = (9e-7, 1.0)
anchor_x, anchor_y = 2700, 3e-3
complexity_annotations_x_length = 8
shifts_x = [1.44, 1.1, 1.11]
shifts_y = [2.2, 1.2, 1.0]
else:
xlim = (1, 2e7)
ylim = (1e-8, 1)
anchor_x, anchor_y = 1.5, 1e-5
complexity_annotations_x_length = 150.0
shifts_x = [0.87, 0.29, 0.46]
shifts_y = [1.1, 0.9, 1.05]
plt.xlim(xlim)
plt.ylim(ylim)
# Define the functions we wish to show, together with their LaTeX renderings.
funcs = [
(lambda x: x, "n"),
(lambda x: x * math.log2(x) ** 2, "n \log^2 n"),
(lambda x: x ** 2 * math.log2(x), "n^2 \log n"),
]
for (func, func_latex), shift_x, shift_y in zip(funcs, shifts_x, shifts_y):
# To gauge how `func` grows, we evaluate it in two points `x_start` and `x_end`. Functions
# of the form `x^k` are a straight line on a log-log plot, so the exact evaluation positions
# do not matter. For other functions, what the arrows show is *approximate* growth.
x_start = 1e1
x_end = x_start * complexity_annotations_x_length
xs = [x_start, x_end]
ys = [func(x) for x in xs]
x_ratio = anchor_x / xs[0]
y_ratio = anchor_y / ys[0]
xs = [x * x_ratio for x in xs]
ys = [y * y_ratio for y in ys]
# Use a function that plots text and an arrow, but set text to empty, to get just the arrow.
plt.annotate(
"",
xy=(xs[1], ys[1]),
xytext=(xs[0], ys[0]),
arrowprops=dict(
width=0.1, headwidth=5, headlength=5, color="grey", shrink=0.0
),
)
# Compute the slope of the arrow in logspace.
ratio = (math.log(ys[1]) - math.log(ys[0])) / (
math.log(xs[1]) - math.log(xs[0])
)
ratio /= (math.log(ylim[1]) - math.log(ylim[0])) / (
math.log(xlim[1]) - math.log(xlim[0])
)
# Even though we did so much work computing the exact angle above, matplotlib ends up
# rendering the text at an angle which is just a tiny bit off. Adjusting this manually here.
ratio *= 0.74
# Convert slope to angle.
angle = math.atan(ratio) / math.pi * 180.0
# Compute where the text should start. Again, theoretically correct positioning ends up off,
# so apply hand-designed offsets `shift_x` and `shift_y` to fix things up.
text_xpos = x_end * (complexity_annotations_x_length ** -0.32) * shift_x
text_ypos = func(text_xpos) * math.exp(ratio * shift_y)
text_xpos *= x_ratio
text_ypos *= y_ratio
# Finally, render the LaTeX.
plt.text(
text_xpos,
text_ypos,
"$\mathcal{O}({" + func_latex + "})$",
rotation=angle,
color="grey",
)
plt.xscale("log")
plt.yscale("log")
plt.xlabel(f"Number of nodes ({descr})", fontsize=AXIS_LABEL_FONT_SIZE)
plt.ylabel(
"Time taken to hash all subexpressions (s)", fontsize=AXIS_LABEL_FONT_SIZE
)
legend = plt.legend(loc=4, fontsize=LEGEND_FONT_SIZE)
if not bert_plot:
for handle in legend.legendHandles:
handle.set_sizes([60.0])
plt.savefig(filename, bbox_inches="tight")
plt.clf()
def main():
parser = argparse.ArgumentParser(
description="Generate nice plots from *.dat files."
)
parser.add_argument("PLOT_TYPE", choices=["balanced", "unbalanced", "bert"])
parser.add_argument(
"--ln", type=str, required=True, help="Path to measurements for Locally Nameless.",
)
parser.add_argument(
"--ours", type=str, required=True, help="Path to measurements for Ours."
)
parser.add_argument(
"--db", type=str, required=True, help="Path to measurements for de Bruijn."
)
parser.add_argument(
"--struct", type=str, required=True, help="Path to measurements for Structural."
)
# Enable LaTeX, and set the default font size.
matplotlib.rcParams["text.usetex"] = True
matplotlib.rcParams["font.size"] = 13
args = parser.parse_args()
results = {
"LN": args.ln,
"Ours": args.ours,
"DeBruijn": args.db,
"Structural": args.struct,
}
if args.PLOT_TYPE == "balanced":
make_plot(
filename="benchmark-balanced.pdf",
descr="balanced expressions",
bert_plot=False,
results=results,
)
elif args.PLOT_TYPE == "unbalanced":
make_plot(
filename="benchmark-unbalanced.pdf",
descr="unbalanced expressions",
bert_plot=False,
results=results,
)
elif args.PLOT_TYPE == "bert":
make_plot(
filename="benchmark-bert.pdf",
descr="BERT",
bert_plot=True,
results=results,
)
else:
raise ValueError(f"Unrecognized plot type {args.PLOT_TYPE}.")
if __name__ == "__main__":
main()