File size: 3,969 Bytes
97ecad4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
"""Export Sudoku Extreme R0 train/test CSVs into this ``data/`` folder.

Run from anywhere::

    python data/export_sudoku_r0_csv.py

Protocol
--------
1. Read ``data/sudoku_extreme_raw/train.csv``
2. Keep rows with ``rating == 0``
3. Shuffle with seed 42
4. Take first 505_000 rows
5. Write:
   - first 500_000 -> ``data/sudoku_r0_train.csv``
   - remaining 5_000 -> ``data/sudoku_r0_test.csv``

Columns match simple Sudoku: ``quizzes,solutions``.

Note: the 5k test is the remainder of the 505k train-pool sample, not the
independent Extreme ``test.csv`` R0 pool.
"""

from __future__ import annotations

import csv
import random
from pathlib import Path

# This file lives in <repo>/data
DATA_DIR = Path(__file__).resolve().parent
RAW_TRAIN_CSV = DATA_DIR / "sudoku_extreme_raw" / "train.csv"
OUT_TRAIN_CSV = DATA_DIR / "sudoku_r0_train.csv"
OUT_TEST_CSV = DATA_DIR / "sudoku_r0_test.csv"

SEED = 42
POOL_SIZE = 505_000
TRAIN_SIZE = 500_000  # remainder of POOL_SIZE becomes test

csv.field_size_limit(10**7)


def load_r0(train_csv: Path) -> list[tuple[str, str]]:
    if not train_csv.is_file():
        raise FileNotFoundError(
            f"Missing Extreme train.csv: {train_csv}\n"
            "Put official Extreme train.csv under data/sudoku_extreme_raw/."
        )

    out: list[tuple[str, str]] = []
    with train_csv.open(newline="", encoding="utf-8") as f:
        reader = csv.DictReader(f)
        for row in reader:
            q = str(row["question"]).replace(".", "0")
            a = str(row["answer"])
            if len(q) != 81 or len(a) != 81:
                continue
            if float(row["rating"]) == 0:
                out.append((q, a))
    return out


def write_csv(path: Path, pairs: list[tuple[str, str]]) -> None:
    with path.open("w", encoding="utf-8", newline="") as f:
        w = csv.writer(f)
        w.writerow(["quizzes", "solutions"])
        w.writerows(pairs)


# ---------------------------------------------------------------------------
# Pull Sudoku Extreme raw CSVs (once) into data/sudoku_extreme_raw/, then run
# this script. From the repo root:
#
#   mkdir -p data/sudoku_extreme_raw
#   curl -L -o data/sudoku_extreme_raw/train.csv \
#     https://huggingface.co/zeyuzy/my_datasets/resolve/main/train.csv
#   curl -L -o data/sudoku_extreme_raw/test.csv \
#     https://huggingface.co/zeyuzy/my_datasets/resolve/main/test.csv
#
# Windows PowerShell (same two downloads):
#
#   New-Item -ItemType Directory -Force -Path data/sudoku_extreme_raw | Out-Null
#   curl.exe -L -o data/sudoku_extreme_raw/train.csv `
#     https://huggingface.co/zeyuzy/my_datasets/resolve/main/train.csv
#   curl.exe -L -o data/sudoku_extreme_raw/test.csv `
#     https://huggingface.co/zeyuzy/my_datasets/resolve/main/test.csv
# ---------------------------------------------------------------------------


def main() -> None:
    print(f"source: {RAW_TRAIN_CSV}")
    print(f"output: {OUT_TRAIN_CSV}")
    print(f"        {OUT_TEST_CSV}")
    print(f"seed={SEED} pool={POOL_SIZE} train={TRAIN_SIZE} test={POOL_SIZE - TRAIN_SIZE}")

    print("loading Extreme train.csv, filter rating == 0 ...")
    pool = load_r0(RAW_TRAIN_CSV)
    print(f"  R0 pool size: {len(pool)}")
    if len(pool) < POOL_SIZE:
        raise ValueError(f"R0 pool has only {len(pool)} rows, need {POOL_SIZE}")

    rng = random.Random(SEED)
    rng.shuffle(pool)
    selected = pool[:POOL_SIZE]
    train_pairs = selected[:TRAIN_SIZE]
    test_pairs = selected[TRAIN_SIZE:]

    train_q = {q for q, _ in train_pairs}
    test_q = {q for q, _ in test_pairs}
    overlap = len(train_q & test_q)
    if overlap:
        raise RuntimeError(f"train/test overlap: {overlap}")

    write_csv(OUT_TRAIN_CSV, train_pairs)
    write_csv(OUT_TEST_CSV, test_pairs)

    print(f"wrote {OUT_TRAIN_CSV} ({len(train_pairs)} rows)")
    print(f"wrote {OUT_TEST_CSV} ({len(test_pairs)} rows)")
    print("DONE")


if __name__ == "__main__":
    main()