File size: 1,567 Bytes
5f0b693 | 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 | """Convert the RRN `sudoku-hard` CSVs (Palm et al., 2018) to this repository's arrays.
Each CSV line is `puzzle,solution`, 81 digits each, 0 for a blank. The output is
`<split>_puz.npy` / `<split>_sol.npy`, int64 of shape (N, 81), the layout that
`src/train_sudoku.py --hard` and `ncm/eval_ours.py --data` read.
curl -L -o sudoku-hard.zip "https://www.dropbox.com/s/rp3hbjs91xiqdgc/sudoku-hard.zip?dl=1"
unzip sudoku-hard.zip
python ncm/convert_rrn.py --src sudoku-hard --out data/rrn
"""
import argparse
from pathlib import Path
import numpy as np
def main():
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("--src", type=Path, required=True, help="folder holding train.csv, valid.csv, test.csv")
ap.add_argument("--out", type=Path, required=True)
a = ap.parse_args()
a.out.mkdir(parents=True, exist_ok=True)
for split in ("train", "valid", "test"):
rows = [l.strip().split(",") for l in open(a.src / f"{split}.csv") if l.strip()]
puz = np.array([[int(c) for c in p] for p, _ in rows], dtype=np.int64)
sol = np.array([[int(c) for c in s] for _, s in rows], dtype=np.int64)
assert puz.shape[1] == sol.shape[1] == 81 and sol.min() >= 1
assert ((puz == 0) | (puz == sol)).all(), "a clue disagrees with its solution"
np.save(a.out / f"{split}_puz.npy", puz)
np.save(a.out / f"{split}_sol.npy", sol)
clues = (puz > 0).sum(1)
print(f"{split}: {len(puz):,} puzzles, {clues.min()}-{clues.max()} clues")
if __name__ == "__main__":
main()
|