File size: 4,213 Bytes
5805ce8
 
 
 
 
 
 
 
 
 
 
 
 
c2fc1f0
 
5805ce8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Check the integer-literal property over the whole packaged corpus.

The response answers ssUK Q4 -- decimals in Figure 1 versus the integer parameter
claim -- with an audit of 156,500 sampled programs finding no non-integer literal,
and commits to running it "over the full released corpus so the property is
verifiable, not asserted". This is that run: all 4,006,182 programs, read straight
out of the parquet shards, no sampling.

Reports the counterexamples rather than a verdict. A property that holds on four
million programs is worth stating precisely; one that holds on 3,999,998 is worth
stating precisely too, and the difference only shows up if the failures are kept.

    python audit_integer_literals.py --parquet-root ./data \\
        --out ./integer_audit.json
"""

from __future__ import annotations

import argparse
import json
import re
import time
from collections import Counter
from pathlib import Path

import pyarrow.parquet as pq

OPS = ["extrude", "revolve", "sweep", "loft"]

# A float literal is a digit sequence with a decimal point that is not part of an
# attribute access. Scientific notation counts too: 1e-3 is not an integer either.
FLOAT_RE = re.compile(r"(?<![A-Za-z_.])\d+\.\d+|(?<![A-Za-z_.])\d+[eE][-+]?\d+")
INT_RE = re.compile(r"(?<![A-Za-z_.])(-?\d+)(?!\.)\b")


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("--parquet-root", required=True)
    ap.add_argument("--out", required=True)
    ap.add_argument("--examples", type=int, default=20,
                    help="how many offending programs to keep verbatim")
    args = ap.parse_args()

    root = Path(args.parquet_root)
    per_op, examples = {}, []
    lo_all, hi_all = 10 ** 9, -10 ** 9
    t0 = time.time()

    for op in OPS:
        files = sorted((root / op).glob("*.parquet"))
        n = n_float = 0
        lo, hi = 10 ** 9, -10 ** 9
        by_split = Counter()
        for f in files:
            # Only the program column: the mesh blobs are the bulk of the corpus
            # and reading them would turn a text scan into an I/O job.
            for batch in pq.ParquetFile(f).iter_batches(batch_size=4096,
                                                        columns=["id", "split", "program"]):
                for rid, split, code in zip(batch["id"].to_pylist(),
                                            batch["split"].to_pylist(),
                                            batch["program"].to_pylist()):
                    n += 1
                    if FLOAT_RE.search(code):
                        n_float += 1
                        by_split[split] += 1
                        if len(examples) < args.examples:
                            examples.append({"op": op, "id": rid, "split": split,
                                             "program": code})
                    v = INT_RE.findall(code)
                    if v:
                        iv = [int(x) for x in v]
                        lo, hi = min(lo, min(iv)), max(hi, max(iv))
            print(f"  {op} {f.name}: {n} programs, {n_float} with a float literal, "
                  f"{time.time() - t0:.0f}s", flush=True)
        per_op[op] = {"programs": n, "with_float_literal": n_float,
                      "by_split": dict(by_split),
                      "integer_range": [lo, hi] if n else None}
        lo_all, hi_all = min(lo_all, lo), max(hi_all, hi)
        print(f"{op}: {n} programs, {n_float} with a float literal, "
              f"integers in [{lo}, {hi}]", flush=True)

    total = sum(v["programs"] for v in per_op.values())
    bad = sum(v["with_float_literal"] for v in per_op.values())
    out = {"total_programs": total, "with_float_literal": bad,
           "pct_with_float_literal": 100.0 * bad / max(1, total),
           "integer_range": [lo_all, hi_all], "per_operation": per_op,
           "examples": examples, "elapsed_sec": round(time.time() - t0)}
    Path(args.out).write_text(json.dumps(out, indent=1))
    print(f"\n{total} programs, {bad} containing a float literal "
          f"({100.0 * bad / max(1, total):.6f}%), integers in [{lo_all}, {hi_all}]")
    print(f"wrote {args.out}")


if __name__ == "__main__":
    main()