inspect_result.py 11.8 KB
Newer Older
CSDN-Ada助手's avatar
CSDN-Ada助手 已提交
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
import os
import sys
import fire
import json
import glob
import numpy as np
import pandas as pd

from collections import defaultdict
from metric import estimate_pass_at_k

error_types = {
    "python"    : [
        "accepted",
        "assertion error",
        "undefined error",
        "runtime error",
        "syntax error",
        "timeout error",
        "type error",
    ],
    "java"      : [
        "accepted",
        "compilation error",
        "assertion error",
        "timeout error",
        "index error",
        "class cast error",
        "stack overflow",
        "null pointer error",
        "unsupported operation",
        "number format error",
        "no such element",
        "illegal argument",
        "out of memory",
        "arithmetic error",
        "others",
    ],
    "cpp"       : [
        "accepted",
        "compilation error",
        "assertion error",
        "range error",
        "invalid argument",
        "pointer error",
        "out of memory",
        "package error",
        "others",
    ],
    "javascript": [
        "accepted",
        "assertion error",
        "undefined error",
        "runtime error",
        "syntax error",
        "timeout error",
        "range error",
        "type error",
    ],
    "go"        : [
        "accepted",
        "assertion error",
        "undefined error",
        "runtime error",
        "syntax error",
        "timeout error",
        "type error",
        "notused error",
    ],
}


def inspect_result(
        input_dir: str = None,
        input_file: str = None,
        output_dir: str = None,
        pass_at_k_outpath: str = None,
):
    if input_dir is not None:
        input_files = glob.glob(input_dir + "/*_results.jsonl")
    else:
        input_files = [input_file]

    if output_dir is not None:
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)
    else:
        output_dir = "/"

    pass_at_k_outs = []
    for input_file in input_files:
        result_stats = defaultdict(dict)
        df = None
        incompleted = False
        with open(input_file, "r") as f:
            for i, line in enumerate(f):
                obj = json.loads(line)
                task_id = obj["task_id"]
                language_type = task_id.split("/")[0].lower()
                if language_type not in error_types:
                    if language_type == "humaneval":
                        language_type = "python"
                    elif language_type == "C++":
                        language_type = "cpp"
                    else:
                        incompleted = True
                        break
                if task_id not in result_stats.keys():
                    default_stats = {}
                    default_stats["task_id"] = task_id
                    default_stats["n_sample"] = 0
                    for k in error_types[language_type]:
                        default_stats[k] = 0

                    result_stats[task_id] = default_stats.copy()
                if df is None:
                    df = pd.DataFrame(columns=error_types[language_type])
                result_stats[task_id]["n_sample"] += 1

                if "failed" in obj["result"]:
                    error = obj["result"]
                    if language_type == "python":
                        if "assertionerror" in error.lower():
                            result_stats[task_id]["assertion error"] += 1
                        elif "syntax" in error.lower() or "indent" in error.lower() or "literal" in error.lower():
                            result_stats[task_id]["syntax error"] += 1
                        elif "not defined" in error.lower():
                            result_stats[task_id]["undefined error"] += 1
                        elif "timeout" in error.lower():
                            result_stats[task_id]["timeout error"] += 1
                        elif "type" in error.lower():
                            result_stats[task_id]["type error"] += 1
                        else:
                            result_stats[task_id]["runtime error"] += 1

                    elif language_type == "java":
                        if "wrong answer" in error:
                            result_stats[task_id]["assertion error"] += 1
                        elif "compilation error" in error:
                            result_stats[task_id]["compilation error"] += 1
                        elif "time out" in error:
                            result_stats[task_id]["timeout error"] += 1
                        elif "IndexOutOfBounds" in error:
                            result_stats[task_id]["index error"] += 1
                        elif "UnsupportedOperation" in error:
                            result_stats[task_id]["unsupported operation"] += 1
                        elif "ClassCast" in error:
                            result_stats[task_id]["class cast error"] += 1
                        elif "NullPointer" in error:
                            result_stats[task_id]["null pointer error"] += 1
                        elif "NumberFormat" in error:
                            result_stats[task_id]["number format error"] += 1
                        elif "NoSuchElement" in error:
                            result_stats[task_id]["no such element"] += 1
                        elif "StackOverflow" in error:
                            result_stats[task_id]["stack overflow"] += 1
                        elif "Arithmetic" in error:
                            result_stats[task_id]["arithmetic error"] += 1
                        elif "OutOfMemory" in error:
                            result_stats[task_id]["out of memory"] += 1
                        elif "IllegalArgument" in error:
                            result_stats[task_id]["illegal argument"] += 1
                        else:
                            result_stats[task_id]["others"] += 1

                    elif language_type == "cpp":
                        if "compilation error" in error.lower():
                            result_stats[task_id]["compilation error"] += 1
                        elif "int main(): assertion" in error.lower():
                            result_stats[task_id]["assertion error"] += 1
                        elif "out_of_range" in error.lower():
                            result_stats[task_id]['range error'] += 1
                        elif "corrupted top size" in error.lower():
                            result_stats[task_id]['range error'] += 1
                        elif "length_error" in error.lower():
                            result_stats[task_id]['range error'] += 1
                        elif "invalid_argument" in error.lower():
                            result_stats[task_id]['invalid argument'] += 1
                        elif "invalid pointer" in error.lower():
                            result_stats[task_id]['pointer error'] += 1
                        elif "double free" in error.lower():
                            result_stats[task_id]['pointer error'] += 1
                        elif "free()" in error.lower():
                            result_stats[task_id]['pointer error'] += 1
                        elif "logic_error" in error.lower():
                            result_stats[task_id]['pointer error'] += 1
                        elif "sysmalloc: assertion" in error.lower():
                            result_stats[task_id]['pointer error'] += 1
                        elif "stack smashing" in error.lower():
                            result_stats[task_id]['out of memory'] += 1
                        elif "bad_alloc" in error.lower():
                            result_stats[task_id]['out of memory'] += 1
                        elif "terminate called after throwing an instance of" in error.lower():
                            result_stats[task_id]['package error'] += 1
                        else:
                            result_stats[task_id]["others"] += 1

                    elif language_type == "javascript":
                        if "Assertion failed" in error:
                            result_stats[task_id]["assertion error"] += 1
                        elif "SyntaxError" in error:
                            result_stats[task_id]["syntax error"] += 1
                        elif "ReferenceError" in error:
                            result_stats[task_id]["undefined error"] += 1
                        elif "timed out" in error:
                            result_stats[task_id]["timeout error"] += 1
                        elif "TypeError" in error:
                            result_stats[task_id]["type error"] += 1
                        elif "RangeError" in error:
                            result_stats[task_id]["range error"] += 1
                        else:
                            result_stats[task_id]["runtime error"] += 1

                    elif language_type == "go":
                        if "Error:      \tNot equal:" in error:
                            result_stats[task_id]["assertion error"] += 1
                        elif "undefined" in error:
                            result_stats[task_id]["undefined error"] += 1
                        elif "expected" in error and "found" in error:
                            result_stats[task_id]["syntax error"] += 1
                        elif "illegal" in error:
                            result_stats[task_id]["syntax error"] += 1
                        elif "unexpected" in error:
                            result_stats[task_id]["syntax error"] += 1
                        elif "FAIL" in error:
                            result_stats[task_id]["runtime error"] += 1
                        elif "timed out" in error:
                            result_stats[task_id]["timeout error"] += 1
                        elif "not used" in error:
                            result_stats[task_id]['notused error'] += 1
                        elif "type" in error:
                            result_stats[task_id]['type error'] += 1
                    else:
                        incompleted = True
                        break
                else:
                    if obj["passed"]:
                        result_stats[task_id]["accepted"] += 1

        if incompleted:
            print(f"Language not supported, aborted. {input_file}")
        else:
            try:
                total, correct = [], []
                for k, res in result_stats.items():
                    total.append(res["n_sample"])
                    correct.append(res["accepted"])
                    df_res = pd.DataFrame(res, index=[int(k.split("/")[-1])])
                    df = pd.concat([df, df_res], axis=0)

                total = np.array(total)
                correct = np.array(correct)

                ks = [1, 10, 100, 1000]
                pass_at_k = {f"pass@{k}": estimate_pass_at_k(total, correct, k).mean()
                            for k in ks if (total >= k).all()}

                print(pass_at_k)
                pass_at_k["file"] = input_file
                pass_at_k["n"] = res["n_sample"]
                pass_at_k_outs.append(pass_at_k)

                output_prefix = input_file.split("/")[-1].split(".jsonl")[0]
                output_file = os.path.join(output_dir, output_prefix + "_stats.xlsx")
                df = df.sort_index(ascending=True)
                df.to_excel(output_file)

                print(f"Stats saved in {output_file}")
            except Exception as e:
                print(e)
                print(f"Data incompleted, aborted. {input_file}")
                
    if pass_at_k_outpath is not None:
        jsonl_path = os.path.join(output_dir, pass_at_k_outpath)
        with open(jsonl_path, "w") as f_out:
            for p in pass_at_k_outs:
                f_out.write(json.dumps(p) + "\n")
        print(f"Pass at k saved in {jsonl_path}")


def main():
    fire.Fire(inspect_result)


if __name__ == "__main__":
    sys.exit(main())