File size: 2,987 Bytes
741ca6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Deep mode, rule C, step 3: the final predictions.

    python deep/merge_review.py --base <rule A>/preds.json --review <review run>/preds.json --subset <review subset.json> \
        --cands /data/train/mini/pool_patches/cands-deep-n8 --out /data/train/mini/results/agent/deepC-n8 --name rightnow-mini-deep-reviewed-n8

For a task the reviewing session handled and returned a patch for, that patch; for every other task, rule A's patch.
select.json records, per reviewed task, whether the session returned one of the candidates unchanged or a new patch.
"""
import argparse
import glob
import json
import os


def normal(patch):
    keep = []
    for line in (patch or "").splitlines():
        if line.startswith(("index ", "diff --git", "new file mode", "old mode", "new mode", "similarity index")):
            continue
        if line.startswith("@@"):
            line = "@@"
        keep.append(line.rstrip())
    return "\n".join(keep).strip()


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--base", required=True)
    ap.add_argument("--review", required=True)
    ap.add_argument("--subset", required=True)
    ap.add_argument("--cands", required=True)
    ap.add_argument("--out", required=True)
    ap.add_argument("--name", required=True)
    a = ap.parse_args()
    os.makedirs(a.out, exist_ok=True)
    base = json.load(open(a.base))
    rev = json.load(open(a.review)) if os.path.exists(a.review) else {}
    sub = json.load(open(a.subset))
    out, log = {}, []
    for iid, v in base.items():
        out[iid] = {"instance_id": iid, "model_name_or_path": a.name, "model_patch": v.get("model_patch") or ""}
    kinds = {"candidate": 0, "new patch": 0, "no patch, rule A kept": 0}
    for iid in sub["instance_ids"]:
        patch = (rev.get(iid) or {}).get("model_patch") or ""
        info = {"instance_id": iid, "distinct": sub["candidates"][iid]["distinct"]}
        if not patch.strip():
            info["how"] = "no patch, rule A kept"
        else:
            which = None
            for f in sorted(glob.glob(f"{a.cands}/{iid}/c*.diff")):
                if normal(open(f).read()) == normal(patch):
                    which = os.path.basename(f)
            info["how"] = "candidate" if which else "new patch"
            info["candidate"] = which
            info["same_as_rule_a"] = normal(patch) == normal(out.get(iid, {}).get("model_patch", ""))
            out[iid] = {"instance_id": iid, "model_name_or_path": a.name, "model_patch": patch}
        kinds[info["how"]] += 1
        log.append(info)
    json.dump(out, open(a.out + "/preds.json", "w"))
    summary = {"n": sub["n"], "mode": "reviewing session with tools", "reviewed": len(sub["instance_ids"]), "returned": kinds,
               "changed_from_rule_a": sum(1 for x in log if x.get("same_as_rule_a") is False)}
    json.dump(dict(summary, tasks=log), open(a.out + "/select.json", "w"), indent=1)
    print(json.dumps(summary))


if __name__ == "__main__":
    main()