forked from bird-bench/mini_dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation_ves.py
251 lines (226 loc) · 7.8 KB
/
evaluation_ves.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
import sys
import json
import numpy as np
import argparse
import multiprocessing as mp
from func_timeout import func_timeout, FunctionTimedOut
from evaluation_utils import (
load_json,
execute_sql,
package_sqls,
sort_results,
print_data,
connect_db,
)
import time
import math
def result_callback(result):
exec_result.append(result)
def clean_abnormal(input):
input = np.asarray(input)
processed_list = []
mean = np.mean(input, axis=0)
std = np.std(input, axis=0)
for x in input:
if x < mean + 3 * std and x > mean - 3 * std:
processed_list.append(x)
return processed_list
def execute_sql(sql, db_path, sql_dialect, return_time=False):
# Connect to the database
conn = connect_db(sql_dialect, db_path)
start_time = time.time()
cursor = conn.cursor()
cursor.execute(sql)
res = cursor.fetchall()
conn.close() # Don't forget to close the connection!
exec_time = time.time() - start_time
if return_time:
return exec_time
return res
def iterated_execute_sql(
predicted_sql, ground_truth, db_path, iterate_num, sql_dialect
):
diff_list = []
predicted_res = execute_sql(predicted_sql, db_path, sql_dialect)
ground_truth_res = execute_sql(ground_truth, db_path, sql_dialect)
reward = 0
time_ratio = 0
if set(predicted_res) == set(ground_truth_res):
for _ in range(iterate_num):
predicted_time = execute_sql(
predicted_sql, db_path, sql_dialect, return_time=True
)
ground_truth_time = execute_sql(
ground_truth, db_path, sql_dialect, return_time=True
)
diff_list.append(ground_truth_time / predicted_time)
processed_diff_list = clean_abnormal(diff_list)
time_ratio = sum(processed_diff_list) / len(processed_diff_list)
if time_ratio == 0:
reward = 0
elif time_ratio >= 2:
reward = 1.25
elif time_ratio >= 1 and time_ratio < 2:
reward = 1
elif time_ratio >= 0.5 and time_ratio < 1:
reward = 0.75
elif time_ratio >= 0.25 and time_ratio < 0.5:
reward = 0.5
else:
reward = 0.25
# return time_ratio
return reward
def execute_model(
predicted_sql, ground_truth, db_place, idx, iterate_num, meta_time_out, sql_dialect
):
try:
# you can personalize the total timeout number
# larger timeout leads to more stable ves
# while it needs more your patience....
reward = func_timeout(
meta_time_out * iterate_num,
iterated_execute_sql,
args=(predicted_sql, ground_truth, db_place, iterate_num, sql_dialect),
)
except KeyboardInterrupt:
sys.exit(0)
except FunctionTimedOut:
result = [(f"timeout",)]
reward = 0
except Exception as e:
result = [(f"error",)] # possibly len(query) > 512 or not executable
reward = 0
result = {"sql_idx": idx, "reward": reward}
return result
def run_sqls_parallel(
sqls,
db_places,
num_cpus=1,
iterate_num=100,
meta_time_out=30.0,
sql_dialect="SQLite",
):
pool = mp.Pool(processes=num_cpus)
for i, sql_pair in enumerate(sqls):
predicted_sql, ground_truth = sql_pair
pool.apply_async(
execute_model,
args=(
predicted_sql,
ground_truth,
db_places[i],
i,
iterate_num,
meta_time_out,
sql_dialect,
),
callback=result_callback,
)
pool.close()
pool.join()
def compute_ves(exec_results):
num_queries = len(exec_results)
total_reward = 0
count = 0
for i, result in enumerate(exec_results):
if result["reward"] != 0:
count += 1
total_reward += math.sqrt(result["reward"]) * 100
ves = total_reward / num_queries
return ves
def compute_ves_by_diff(exec_results, diff_json_path):
num_queries = len(exec_results)
contents = load_json(diff_json_path)
simple_results, moderate_results, challenging_results = [], [], []
for i, content in enumerate(contents):
if content["difficulty"] == "simple":
simple_results.append(exec_results[i])
if content["difficulty"] == "moderate":
moderate_results.append(exec_results[i])
if content["difficulty"] == "challenging":
challenging_results.append(exec_results[i])
simple_ves = compute_ves(simple_results)
moderate_ves = compute_ves(moderate_results)
challenging_ves = compute_ves(challenging_results)
all_ves = compute_ves(exec_results)
count_lists = [
len(simple_results),
len(moderate_results),
len(challenging_results),
num_queries,
]
return simple_ves, moderate_ves, challenging_ves, all_ves, count_lists
def print_reward_category(exec_results, engine, sql_dialect):
res = {
"engine": engine,
"sql_dialect": sql_dialect,
"distribution": exec_results,
}
file_path = "results.json"
try:
with open(file_path, "r") as file:
data = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
data = [] # Start with an empty list if file doesn't exist or is empty
# Append the new data
data.append(res)
# Write the updated data back to the file
with open(file_path, "w") as file:
json.dump(data, file, indent=4)
if __name__ == "__main__":
args_parser = argparse.ArgumentParser()
args_parser.add_argument(
"--predicted_sql_path", type=str, required=True, default=""
)
args_parser.add_argument("--ground_truth_path", type=str, required=True, default="")
args_parser.add_argument("--data_mode", type=str, required=True, default="dev")
args_parser.add_argument("--db_root_path", type=str, required=True, default="")
args_parser.add_argument("--num_cpus", type=int, default=1)
args_parser.add_argument("--meta_time_out", type=float, default=30.0)
args_parser.add_argument("--mode_gt", type=str, default="gt")
args_parser.add_argument("--mode_predict", type=str, default="gpt")
args_parser.add_argument("--diff_json_path", type=str, default="")
args_parser.add_argument("--engine", type=str, default="")
args_parser.add_argument("--sql_dialect", type=str, default="SQLite")
args = args_parser.parse_args()
exec_result = []
pred_queries, db_paths = package_sqls(
args.predicted_sql_path,
args.db_root_path,
args.engine,
sql_dialect=args.sql_dialect,
mode=args.mode_predict,
data_mode=args.data_mode,
)
# generate ground truth sqls:
gt_queries, db_paths_gt = package_sqls(
args.ground_truth_path,
args.db_root_path,
args.engine,
sql_dialect=args.sql_dialect,
mode="gt",
data_mode=args.data_mode,
)
query_pairs = list(zip(pred_queries, gt_queries))
run_sqls_parallel(
query_pairs,
db_places=db_paths,
num_cpus=args.num_cpus,
meta_time_out=args.meta_time_out,
sql_dialect=args.sql_dialect,
)
exec_result = sort_results(exec_result)
# print_reward_category(exec_result, args.engine, args.sql_dialect)
print("start calculate")
simple_ves, moderate_ves, challenging_ves, ves, count_lists = compute_ves_by_diff(
exec_result, args.diff_json_path
)
score_lists = [simple_ves, moderate_ves, challenging_ves, ves]
print(f"VES for {args.engine} on {args.sql_dialect} set")
print("start calculate")
print_data(score_lists, count_lists, metric="VES")
print(
"==========================================================================================="
)
print(f"Finished VES evaluation for {args.engine} on {args.sql_dialect} set")
print("\n\n")