File size: 1,851 Bytes
42862cd |
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 59 60 61 62 63 64 65 66 67 |
#!/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 <data_dir>")
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
|