summaryrefslogtreecommitdiff
path: root/src/split_ep.py
blob: 557dbec0a50df33f92eb219de3e1e59c78c7dff0 (plain)
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
#!/usr/bin/env python3
"""Split a script-format .ep_ file (setboard/solution/id/go blocks, as
used by typhoon's `script` command) into two disjoint, shuffled halves
for a train/holdout threshold sweep.

Usage: split_ep.py <input.ep_> <out_a.ep_> <out_b.ep_> [--seed N]

Splits by whole position blocks (never mid-position), shuffled with a
fixed seed so the split is reproducible but not biased by the file's
original ordering (ECM.ep_ is not randomly ordered).
"""
import argparse
import random
import sys


def read_blocks(path):
    """Each block is the lines from one 'setboard' up to and including
    the following 'go' line."""
    blocks = []
    current = []
    with open(path) as f:
        for line in f:
            stripped = line.rstrip("\n")
            if stripped.startswith("setboard") and current:
                blocks.append(current)
                current = []
            current.append(stripped)
    if current:
        blocks.append(current)
    return blocks


def main():
    ap = argparse.ArgumentParser(description=__doc__)
    ap.add_argument("input")
    ap.add_argument("out_a")
    ap.add_argument("out_b")
    ap.add_argument("--seed", type=int, default=0)
    args = ap.parse_args()

    blocks = read_blocks(args.input)
    if not blocks:
        print(f"ERROR: no position blocks found in {args.input}", file=sys.stderr)
        sys.exit(1)

    rng = random.Random(args.seed)
    order = list(range(len(blocks)))
    rng.shuffle(order)

    mid = len(order) // 2
    half_a = sorted(order[:mid])
    half_b = sorted(order[mid:])

    with open(args.out_a, "w") as f:
        for i in half_a:
            f.write("\n".join(blocks[i]) + "\n")

    with open(args.out_b, "w") as f:
        for i in half_b:
            f.write("\n".join(blocks[i]) + "\n")

    print(f"{len(blocks)} positions in {args.input} -> "
          f"{len(half_a)} in {args.out_a}, {len(half_b)} in {args.out_b} "
          f"(seed={args.seed})")


if __name__ == "__main__":
    main()