File size: 2,488 Bytes
eb131c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
# inp_path = "/Users/zkcpku/Documents/seke/mywork/分块ROPE/openai_src/gen_out/openai_default_codesymbols_outputless100.jsonl.renew"
# inp_path is the parameter for script
import sys
# /Users/zkcpku/Documents/seke/pretrain/envAnalysis/
# python extract_funcs.py
# test_renew_dataset
def eval_codesymbols(inp_path):
    with open(inp_path,'r') as f:
        lines = f.readlines()
        lines = [json.loads(x) for x in lines]
    print("len(lines):",len(lines))

    rst = {}
    for i,l in enumerate(lines):
        this_rst = {"true": [], "false": [], "acc": 0.0}
        ground_output = l['output']
        preds = l["out_str"]
        metadata = l['metadata']
        symbol_dict = metadata['symbol_dict']
        symbol2location = {e['symbol']: e['location'] for e in symbol_dict}
        for e in ground_output:
            if e in preds:
                this_rst["true"].append((e, symbol2location[e]))
            else:
                this_rst["false"].append((e, symbol2location[e]))
        acc = len(this_rst["true"]) / (len(this_rst["true"]) + len(this_rst["false"]))
        this_rst["acc"] = acc
        rst[i] = this_rst
    total_acc = sum([rst[i]["acc"] for i in rst]) / len(rst)
    print(f"total acc: {total_acc}")
    all_true = [rst[i]['true'] for i in range(len(rst))]
    all_true = [e for ee in all_true for e in ee]
    all_true_location = [e[1] for e in all_true]

    all_false = [rst[i]['false'] for i in range(len(rst))]
    all_false = [e for ee in all_false for e in ee]
    all_false_location = [e[1] for e in all_false]
    # print([len(rst[i]['false']) for i in range(len(rst))])
    # import ipdb; ipdb.set_trace()

    print(max(all_true_location), min(all_true_location), max(all_false_location), min(all_false_location))

    # each 1000 is a range, count acc
    # 0-1000 1000-2000 2000-3000 3000-4000 4000-5000 5000-6000 6000-7000 7000-8000 8000-9000 9000-10000
    for i in range(20):
        this_true = [e for e in all_true_location if e >= i*1000 and e < (i+1)*1000]
        this_false = [e for e in all_false_location if e >= i*1000 and e < (i+1)*1000]
        if len(this_true) + len(this_false) == 0:
            this_acc = 0
        else:
            this_acc = len(this_true) / (len(this_true) + len(this_false))
        print(f"range {i*1000}-{(i+1)*1000} acc: {this_acc} true: {len(this_true)} false: {len(this_false)}")


if __name__ == "__main__":
    args = sys.argv
    inp_path = args[1]
    eval_codesymbols(inp_path)