File size: 3,210 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
69
70
71
"""Deep mode, rule C, step 1: write the distinct patches of the tasks where the attempts disagree.

    python deep/make_candidates.py --attempts /data/train/mini/results/deep --n 8 \
        --out-patches /data/train/mini/pool_patches/cands-deep-n8 --out-subset /data/train/mini/results/review/deep-n8/subset.json

A task is left out when one patch has a majority of the attempts (rule A already decided it) or when no attempt made a
patch. For the others: up to six distinct patches, the most shared first, as <out-patches>/<instance>/c<k>.diff with a
README.txt. Nothing here reads the benchmark's tests or any score.
"""
import argparse
import collections
import json
import os
import shutil


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("--attempts", required=True)
    ap.add_argument("--n", type=int, required=True)
    ap.add_argument("--subset", default="/data/train/mini/src/eval/subset.json")
    ap.add_argument("--out-patches", required=True)
    ap.add_argument("--out-subset", required=True)
    a = ap.parse_args()
    ids = json.load(open(a.subset))["instance_ids"]
    preds = [json.load(open(f"{a.attempts}/att-{k}/preds.json")) for k in range(1, a.n + 1)]
    shutil.rmtree(a.out_patches, ignore_errors=True)
    os.makedirs(os.path.dirname(a.out_subset), exist_ok=True)
    chosen, record = [], {}
    for iid in ids:
        groups = collections.defaultdict(list)
        for k, p in enumerate(preds):
            patch = (p.get(iid) or {}).get("model_patch") or ""
            if patch.strip():
                groups[normal(patch)].append((k + 1, patch))
        made = sum(len(v) for v in groups.values())
        if not groups:
            continue
        ranked = sorted(groups.values(), key=lambda g: (-len(g), len(g[0][1])))
        if len(ranked[0]) * 2 > made or len(ranked) == 1:
            continue
        d = f"{a.out_patches}/{iid}"
        os.makedirs(d)
        lines = [f"{len(ranked)} distinct patches came out of {made} attempts that produced one ({a.n} attempts in all)."]
        for i, g in enumerate(ranked[:6], 1):
            text = g[0][1] if g[0][1].endswith("\n") else g[0][1] + "\n"
            open(f"{d}/c{i}.diff", "w").write(text)
            lines.append(f"c{i}.diff: produced by {len(g)} of the attempts")
        if len(ranked) > 6:
            lines.append(f"({len(ranked) - 6} more patches, each from one attempt, are not shown)")
        open(f"{d}/README.txt", "w").write("\n".join(lines) + "\n")
        chosen.append(iid)
        record[iid] = {"attempts_with_patch": made, "distinct": len(ranked), "shown": [[k for k, _ in g] for g in ranked[:6]]}
    json.dump({"instance_ids": chosen, "n": a.n, "candidates": record}, open(a.out_subset, "w"), indent=1)
    print(json.dumps({"n": a.n, "tasks_to_review": len(chosen), "of": len(ids)}))


if __name__ == "__main__":
    main()