| | """Validation functions for the Chessbench dataset. |
| | |
| | This script validates the integrity of the Chessbench dataset by checking that each record adheres to expected formats and value ranges. |
| | It ensures that FEN strings and move UCI strings are valid, and that win probabilities and mate labels are consistent. |
| | """ |
| |
|
| | import chess |
| | from tqdm import tqdm |
| |
|
| | from athena.datasets.chessbenchmate.dataset import ChessbenchDataset |
| |
|
| |
|
| | def validate_record(record): |
| | fen, move, win_prob, mate = record |
| | assert isinstance(fen, str) |
| | assert isinstance(move, str) |
| |
|
| | |
| | board = chess.Board(fen) |
| | assert board.is_valid(), f"Invalid FEN: {fen}" |
| |
|
| | |
| | uci_move = chess.Move.from_uci(move) |
| | assert uci_move in board.legal_moves, f"Illegal move {move} for board {fen}" |
| |
|
| | if mate == "#": |
| | assert win_prob == 1.0, f"Unexpected win_prob {win_prob} for mate {mate}" |
| | elif isinstance(mate, int): |
| | |
| | assert mate != 0, "Mate cannot be zero" |
| | assert win_prob in (0.0, 1.0), f"Unexpected win_prob {win_prob} for mate {mate}" |
| | if mate > 0: |
| | assert win_prob == 1.0, f"Unexpected win_prob {win_prob} for mate {mate}" |
| | else: |
| | assert win_prob == 0.0, f"Unexpected win_prob {win_prob} for mate {mate}" |
| | elif mate == "-": |
| | assert isinstance(win_prob, float) and 0.0 < win_prob < 1.0 |
| |
|
| |
|
| | test_dataset = ChessbenchDataset(dir="src/athena/datasets/chessbenchmate/data", mode="test") |
| | train_dataset = ChessbenchDataset(dir="src/athena/datasets/chessbenchmate/data", mode="train") |
| |
|
| |
|
| | for i in tqdm(range(len(test_dataset))): |
| | record = test_dataset[i] |
| | try: |
| | validate_record(record) |
| | except AssertionError as e: |
| | print(f"Validation error in test dataset at index {i}: {e}") |
| | raise |
| |
|
| | for i in tqdm(range(len(train_dataset))): |
| | record = train_dataset[i] |
| | try: |
| | validate_record(record) |
| | except AssertionError as e: |
| | print(f"Validation error in train dataset at index {i}: {e}") |
| | raise |