(exec_results, query_pairs, diff_json_path, sample=True, test_id=None, fold="1", whether_print=False, print_file_name="")
| 122 | |
| 123 | |
| 124 | def compute_acc_by_diff_sample(exec_results, query_pairs, diff_json_path, sample=True, test_id=None, fold="1", whether_print=False, print_file_name=""): |
| 125 | num_queries = len(exec_results) |
| 126 | results = [res['res'] for res in exec_results] |
| 127 | contents = load_json(diff_json_path) |
| 128 | if sample: |
| 129 | import sys |
| 130 | sys.path.append("..") |
| 131 | import os |
| 132 | from sample import get_sample_idx |
| 133 | idx = get_sample_idx(fold, "bird", root_dir=os.path.realpath("..")) |
| 134 | contents = [contents[i] for i in idx] |
| 135 | if test_id is not None: |
| 136 | contents = [load_json(diff_json_path)[test_id]] |
| 137 | |
| 138 | # xzjin add |
| 139 | if whether_print: |
| 140 | details = open(print_file_name, mode='r').readlines() |
| 141 | # xzjin add |
| 142 | |
| 143 | simple_results, moderate_results, challenging_results = [], [], [] |
| 144 | |
| 145 | for i, content in enumerate(contents): |
| 146 | if content['difficulty'] == 'simple': |
| 147 | simple_results.append(exec_results[i]) |
| 148 | |
| 149 | if content['difficulty'] == 'moderate': |
| 150 | moderate_results.append(exec_results[i]) |
| 151 | |
| 152 | if content['difficulty'] == 'challenging': |
| 153 | challenging_results.append(exec_results[i]) |
| 154 | |
| 155 | # xzjin add |
| 156 | if whether_print: |
| 157 | dirname, basename = os.path.dirname(print_file_name), os.path.basename(print_file_name) |
| 158 | file_name = os.path.join(dirname, f"processed_{basename}") |
| 159 | p_str, g_str = query_pairs[i] |
| 160 | with open(file_name, mode='a') as f: |
| 161 | data = json.loads(details[i]) |
| 162 | for k, v in data.items(): |
| 163 | if k != "init_table_infos": |
| 164 | f.write(f"{k}: {v}\n") |
| 165 | if k == 'query': |
| 166 | f.write(f"-----------------------------------------------\n") |
| 167 | if k == "init_table_infos": |
| 168 | f.write(f"{k}: {v}-----------------------------------------------\n") |
| 169 | if k == "table_info": |
| 170 | f.write(f"-----------------------------------------------\n") |
| 171 | if k == "features": |
| 172 | f.write(f"-----------------------------------------------\n") |
| 173 | # f.write(f"{k}: {v}\n") |
| 174 | f.write(f"pred: {p_str}\ngroud_truth: {g_str}\nresult: {exec_results[i]['res']}\n\n\n") |
| 175 | f.write(f"################################################################\n") |
| 176 | # xzjin add |
| 177 | |
| 178 | # try except is for debug |
| 179 | try: |
| 180 | simple_acc = sum([res['res'] for res in simple_results]) / len(simple_results) |
| 181 | except: |
no test coverage detected