Create validate_data.py
Browse files- validate_data.py +58 -0
validate_data.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Validation functions for the Chessbench dataset.
|
| 2 |
+
|
| 3 |
+
This script validates the integrity of the Chessbench dataset by checking that each record adheres to expected formats and value ranges.
|
| 4 |
+
It ensures that FEN strings and move UCI strings are valid, and that win probabilities and mate labels are consistent.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import chess
|
| 8 |
+
from tqdm import tqdm
|
| 9 |
+
|
| 10 |
+
from athena.datasets.chessbenchmate.dataset import ChessbenchDataset
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def validate_record(record):
|
| 14 |
+
fen, move, win_prob, mate = record
|
| 15 |
+
assert isinstance(fen, str)
|
| 16 |
+
assert isinstance(move, str)
|
| 17 |
+
|
| 18 |
+
# Check for valid fen
|
| 19 |
+
board = chess.Board(fen)
|
| 20 |
+
assert board.is_valid(), f"Invalid FEN: {fen}"
|
| 21 |
+
|
| 22 |
+
# Check for valid move
|
| 23 |
+
uci_move = chess.Move.from_uci(move)
|
| 24 |
+
assert uci_move in board.legal_moves, f"Illegal move {move} for board {fen}"
|
| 25 |
+
|
| 26 |
+
if mate == "#":
|
| 27 |
+
assert win_prob == 1.0, f"Unexpected win_prob {win_prob} for mate {mate}"
|
| 28 |
+
elif isinstance(mate, int):
|
| 29 |
+
# If mate is "#" or an integer, win_prob should be 1.0 or 0.0
|
| 30 |
+
assert mate != 0, "Mate cannot be zero"
|
| 31 |
+
assert win_prob in (0.0, 1.0), f"Unexpected win_prob {win_prob} for mate {mate}"
|
| 32 |
+
if mate > 0:
|
| 33 |
+
assert win_prob == 1.0, f"Unexpected win_prob {win_prob} for mate {mate}"
|
| 34 |
+
else:
|
| 35 |
+
assert win_prob == 0.0, f"Unexpected win_prob {win_prob} for mate {mate}"
|
| 36 |
+
elif mate == "-":
|
| 37 |
+
assert isinstance(win_prob, float) and 0.0 < win_prob < 1.0
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
test_dataset = ChessbenchDataset(dir="src/athena/datasets/chessbenchmate/data", mode="test")
|
| 41 |
+
train_dataset = ChessbenchDataset(dir="src/athena/datasets/chessbenchmate/data", mode="train")
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
for i in tqdm(range(len(test_dataset))):
|
| 45 |
+
record = test_dataset[i]
|
| 46 |
+
try:
|
| 47 |
+
validate_record(record)
|
| 48 |
+
except AssertionError as e:
|
| 49 |
+
print(f"Validation error in test dataset at index {i}: {e}")
|
| 50 |
+
raise
|
| 51 |
+
|
| 52 |
+
for i in tqdm(range(len(train_dataset))):
|
| 53 |
+
record = train_dataset[i]
|
| 54 |
+
try:
|
| 55 |
+
validate_record(record)
|
| 56 |
+
except AssertionError as e:
|
| 57 |
+
print(f"Validation error in train dataset at index {i}: {e}")
|
| 58 |
+
raise
|