-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
295 lines (259 loc) · 9.43 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import numpy as np
import matplotlib.pyplot as plt
nq = 1_000_000
topk = 100
ef = 800
bf_threshold = 0.05
category_index_threshold = 0.05
strategy = 'incr_build'
def get_query_stats():
stats = []
with open('query_stats.bin', 'rb') as f:
for _ in range(nq):
stats.append(np.frombuffer(f.read(4 * 3), dtype=np.float32))
return stats
def get_recall():
recall = []
with open('recall1.bin', 'rb') as f:
for _ in range(nq):
recall.append(np.frombuffer(f.read(4), dtype=np.int32))
return recall
stats = get_query_stats()
recall = get_recall()
def plot_selectivity_recall(stats, recall):
x = []
y = []
for i in range(nq):
x.append(stats[i][1])
y.append(recall[i])
plt.scatter(x, y, s=1)
plt.xlabel('Selectivity')
plt.ylabel('Recall@100')
plt.title('Selectivity vs Recall@100')
plt.ylim(80,100)
plt.savefig(f'selectivity_recall_ef{ef}_sel{bf_threshold}_cat_{category_index_threshold}.png')
plt.clf()
def plot_bruteforce_recall(stats, recall, threshold=bf_threshold):
# 统计一下在selectivity<0.2情况下,每个recall(1,0.99)的query个数
counter = {}
for i in range(nq):
if stats[i][1] < threshold:
r = recall[i].item()
if r in counter:
counter[r] += 1
else:
counter[r] = 1
# plot
x = list(counter.keys())
y = list(counter.values())
plt.bar(x, y)
plt.xlim(90, 100)
# 标出频数
for a, b in zip(x, y):
plt.text(a, b, '%d' % b, ha='center', va='bottom', fontsize=10)
plt.xlabel('Recall')
plt.ylabel('Query Count')
plt.title('Bruteforce Recall')
plt.savefig(f'bruteforce_recall_sel{bf_threshold}_cat{category_index_threshold}.png')
plt.clf()
def plot_graph_recall(stats, recall, threshold=bf_threshold):
# 统计一下在selectivity<0.2情况下,每个recall(1,0.99)的query个数
counter = {}
for i in range(nq):
if stats[i][1] >= threshold:
r = recall[i].item()
if r in counter:
counter[r] += 1
else:
counter[r] = 1
# plot
x = list(counter.keys())
y = list(counter.values())
plt.bar(x, y)
plt.xlim(90, 100)
for a,b in zip(x, y):
plt.text(a, b, '%d' % b, ha='center', va='bottom', fontsize=10)
plt.xlabel('Recall')
plt.ylabel('Query Count')
plt.title('Graph Search Recall')
plt.savefig(f'graph_recall_ef_{ef}_sel{bf_threshold}_cat_{category_index_threshold}.png')
plt.clf()
def plot_selectivity_time(stats):
x = []
y = []
for i in range(nq):
x.append(stats[i][1])
y.append(stats[i][2])
plt.scatter(x, y, s=1)
plt.xlabel('Selectivity')
plt.ylabel('Time (ms)')
plt.title('Selectivity vs Time')
plt.savefig(f'selectivity_time_ef{ef}_sel{bf_threshold}_cat_{category_index_threshold}.png')
plt.clf()
def plot_selectivity(stats):
# plot distribution of selectivity
# [0,0.1] [0.1,0.2] ... [0.9,1
# 假设selectivity是一个包含选择性值的列表
selectivity = [stat[1] for stat in stats]
# 创建直方图
plt.hist(selectivity, bins=[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1])
# 设置标题和标签
plt.title('Distribution of Selectivity')
plt.xlabel('Selectivity')
plt.ylabel('Frequency')
# 显示纵坐标的值
plt.yticks(range(0, 100000, 10000))
# 显示图形
plt.savefig('selectivity.png')
plt.clf()
def plot_type_selectivity_frequency(stats):
s1 = [stat[1] for i, stat in enumerate(stats) if stat[0] == 1]
s2 = [stat[1] for i, stat in enumerate(stats) if stat[0] == 2]
s3 = [stat[1] for i, stat in enumerate(stats) if stat[0] == 3]
counts, bins, patches = plt.hist(s1, bins=[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1], alpha=0.5, label='Type 1')
plt.title('Distribution of Selectivity of Type 1')
plt.xlabel('Selectivity')
plt.ylabel('Frequency')
plt.legend(loc='upper right')
# 给出每个直方的频数
for i in range(len(counts)):
plt.text(bins[i], counts[i], str(counts[i]), ha='center', va='bottom', fontsize=10)
plt.savefig('type1_selectivity.png')
plt.clf()
counts, bins, patches = plt.hist(s2, bins=[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1], alpha=0.5, label='Type 2')
plt.title('Distribution of Selectivity of Type 2')
plt.xlabel('Selectivity')
plt.ylabel('Frequency')
plt.legend(loc='upper right')
for i in range(len(counts)):
plt.text(bins[i], counts[i], str(counts[i]), ha='center', va='bottom', fontsize=10)
plt.savefig('type2_selectivity.png')
plt.clf()
counts, bins, patches = plt.hist(s3, bins=[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1], alpha=0.5, label='Type 3')
plt.title('Distribution of Selectivity of Type 3')
plt.xlabel('Selectivity')
plt.ylabel('Frequency')
plt.legend(loc='upper right')
for i in range(len(counts)):
plt.text(bins[i], counts[i], str(counts[i]), ha='center', va='bottom', fontsize=10)
plt.savefig('type3_selectivity.png')
plt.clf()
def plot_type3_selectivity_under_category():
# 输出type3的query在对category首先partition后的selectivity
s3 = [stat[1] for i, stat in enumerate(stats) if stat[0] == 3]
counts, bins, patches = plt.hist(s3, bins=[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1], alpha=0.5, label='Type 3')
plt.title('Distribution of Selectivity of Type 3 under category')
plt.xlabel('Selectivity')
plt.ylabel('Frequency')
plt.legend(loc='upper right')
for i in range(len(counts)):
plt.text(bins[i], counts[i], str(counts[i]), ha='center', va='bottom', fontsize=10)
plt.savefig('type3_selectivity_under_category.png')
def plot_query_type_selectivity_recall():
# type = 0
freq = {}
for i in range(nq):
if stats[i][0] == 0:
r = recall[i].item()
if r in freq:
freq[r] += 1
else:
freq[r] = 1
x = list(freq.keys())
y = list(freq.values())
plt.bar(x, y)
plt.xlim(90, 100)
for a,b in zip(x, y):
plt.text(a, b, '%d' % b, ha='center', va='bottom', fontsize=10)
plt.xlabel('Recall')
plt.ylabel('Query Count')
plt.title('Type 0 Recall')
plt.savefig(f'type0_recall_{strategy}_ef_{ef}_sel{bf_threshold}.png')
plt.clf()
# type = 1
freq = {}
for i in range(nq):
if stats[i][0] == 1:
r = recall[i].item()
if r in freq:
freq[r] += 1
else:
freq[r] = 1
x = list(freq.keys())
y = list(freq.values())
plt.bar(x, y)
plt.xlim(90, 100)
for a,b in zip(x, y):
plt.text(a, b, '%d' % b, ha='center', va='bottom', fontsize=10)
plt.xlabel('Recall')
plt.ylabel('Query Count')
plt.title('Type 1 Recall')
plt.savefig(f'type1_recall_{strategy}_ef_{ef}_sel{bf_threshold}.png')
plt.clf()
# type = 2
freq = {}
for i in range(nq):
if stats[i][0] == 2:
r = recall[i].item()
if r in freq:
freq[r] += 1
else:
freq[r] = 1
x = list(freq.keys())
y = list(freq.values())
plt.bar(x, y)
plt.xlim(90, 100)
for a,b in zip(x, y):
plt.text(a, b, '%d' % b, ha='center', va='bottom', fontsize=10)
plt.xlabel('Recall')
plt.ylabel('Query Count')
plt.title('Type 2 Recall')
plt.savefig(f'type2_recall_{strategy}_ef_{ef}_sel{bf_threshold}.png')
plt.clf()
# type = 3
freq = {}
for i in range(nq):
if stats[i][0] == 3:
r = recall[i].item()
if r in freq:
freq[r] += 1
else:
freq[r] = 1
x = list(freq.keys())
y = list(freq.values())
plt.bar(x, y)
plt.xlim(90, 100)
for a,b in zip(x, y):
plt.text(a, b, '%d' % b, ha='center', va='bottom', fontsize=10)
plt.xlabel('Recall')
plt.ylabel('Query Count')
plt.title('Type 3 Recall')
plt.savefig(f'type3_recall_{strategy}_ef_{ef}_sel{bf_threshold}.png')
plt.clf()
def plot_range_filter_selectivity_recall():
# 统计type=2时,在selectivity为[0, 0.1, 0.2, 0.3, ..., 1.0]下,recall大于等于0.95的query个数的百分比
recall2 = [recall[i] for i, stat in enumerate(stats) if stat[0] == 2]
selectivity2 = [stat[1] for i, stat in enumerate(stats) if stat[0] == 2]
threshold = 95
ratios = np.zeros(10, dtype=np.float32)
for i in range(10):
start = i * 0.1
end = (i + 1) * 0.1
sel_recall = [recall2[i].item() for i, sel in enumerate(selectivity2) if start <= sel < end and sel>=bf_threshold]
ratios[i] = len([r for r in sel_recall if r >= threshold]) / len(sel_recall)
plt.plot(np.arange(0, 1.0, 0.1), ratios)
plt.xlabel('Selectivity')
plt.ylabel(f'Recall Ratio(>{threshold})')
plt.title(f'Range Filter Selectivity vs Recall Ratio (>{threshold})')
for i in range(10):
plt.text(i * 0.1, ratios[i], f'{ratios[i]:.4f}', ha='center', va='bottom', fontsize=10)
plt.savefig(f'type2_sel_rec_{strategy}_ef{ef}_{threshold}.png')
# plot_selectivity_recall(stats, recall)
# plot_selectivity_time(stats)
# plot_selectivity(stats)
# plot_bruteforce_recall(stats, recall)
# plot_graph_recall(stats, recall)
# plot_type_selectivity_frequency(stats)
# plot_type3_selectivity_under_category()
plot_query_type_selectivity_recall()
plot_range_filter_selectivity_recall()