#!/usr/bin/env python3 """Dataset loader for ChessBenchmate Aggregated dataset.""" import msgpack from pathlib import Path from typing import Iterator, Dict, Any def stream_positions(data_dir: str, shard_range: tuple = None) -> Iterator[Dict[str, Any]]: """Stream positions from all shards. Args: data_dir: Directory containing the .msgpack files shard_range: Optional (start, end) tuple to limit shards Yields: Dict with 'fen' and 'moves' keys """ data_path = Path(data_dir) shard_files = sorted(data_path.glob('train-*.msgpack')) if shard_range: start, end = shard_range shard_files = shard_files[start:end] for shard_file in shard_files: with open(shard_file, 'rb') as f: unpacker = msgpack.Unpacker(f, raw=False) for record in unpacker: yield record def count_positions(data_dir: str) -> tuple: """Count total positions and moves in the dataset. Returns: Tuple of (num_positions, num_moves) """ total_positions = 0 total_moves = 0 for record in stream_positions(data_dir): total_positions += 1 total_moves += len(record['moves']) return total_positions, total_moves if __name__ == '__main__': import sys if len(sys.argv) < 2: print("Usage: python dataset.py ") sys.exit(1) data_dir = sys.argv[1] print(f"Sampling positions from {data_dir}...") for i, record in enumerate(stream_positions(data_dir)): print(f"\nPosition {i+1}:") print(f" FEN: {record['fen']}") print(f" Moves: {len(record['moves'])}") for move, eval in list(record['moves'].items())[:3]: print(f" {move}: win_prob={eval['win_prob']:.3f}, mate={eval['mate']}") if i >= 4: break