| |
| """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 |
|
|
| |
| 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 |
|
|
| 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) |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| 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() |
|
|