File size: 2,097 Bytes
082a76b | 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 | """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)
# Check for valid fen
board = chess.Board(fen)
assert board.is_valid(), f"Invalid FEN: {fen}"
# Check for valid move
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):
# If mate is "#" or an integer, win_prob should be 1.0 or 0.0
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 |