KeithG33 commited on
Commit
9393083
·
1 Parent(s): 813df99

decompress .pgn.zst dataset

Browse files
Files changed (1) hide show
  1. ChessBot-Dataset.py +24 -16
ChessBot-Dataset.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import logging
2
  from datasets import (
3
  GeneratorBasedBuilder,
@@ -10,6 +11,9 @@ from datasets import (
10
  Array2D,
11
  )
12
  from adversarial_gym.chess_env import ChessEnv
 
 
 
13
 
14
  class ChessPGNDataset(GeneratorBasedBuilder):
15
  VERSION = "0.0.1"
@@ -55,19 +59,23 @@ class ChessPGNDataset(GeneratorBasedBuilder):
55
  uid = 0
56
 
57
  for path in shards:
58
- with open(path, "r") as f:
59
- while (game := chess.pgn.read_game(f)) is not None:
60
- board = game.board()
61
- base = {"1-0":1,"0-1":-1}.get(game.headers["Result"], 0)
62
- for move in game.mainline_moves():
63
- state = ChessEnv.get_piece_configuration(board)
64
- state = state if board.turn else -state
65
- action = ChessEnv.move_to_action(move)
66
- result = base * (-1 if board.turn == 0 else 1)
67
- yield uid, {
68
- "state": state.astype("int8"),
69
- "action": int(action),
70
- "result": int(result),
71
- }
72
- uid += 1
73
- board.push(move)
 
 
 
 
 
1
+ import io
2
  import logging
3
  from datasets import (
4
  GeneratorBasedBuilder,
 
11
  Array2D,
12
  )
13
  from adversarial_gym.chess_env import ChessEnv
14
+ import zstandard as zstd
15
+
16
+
17
 
18
  class ChessPGNDataset(GeneratorBasedBuilder):
19
  VERSION = "0.0.1"
 
59
  uid = 0
60
 
61
  for path in shards:
62
+ decompressor = zstd.ZstdDecompressor()
63
+ with open(path, "rb") as compressed:
64
+ with decompressor.stream_reader(compressed) as reader:
65
+ text_stream = io.TextIOWrapper(reader, encoding='utf-8')
66
+ while (game := chess.pgn.read_game(text_stream)) is not None:
67
+ # while (game := chess.pgn.read_game(f)) is not None:
68
+ board = game.board()
69
+ base = {"1-0":1,"0-1":-1}.get(game.headers["Result"], 0)
70
+ for move in game.mainline_moves():
71
+ state = ChessEnv.get_piece_configuration(board)
72
+ state = state if board.turn else -state
73
+ action = ChessEnv.move_to_action(move)
74
+ result = base * (-1 if board.turn == 0 else 1)
75
+ yield uid, {
76
+ "state": state.astype("int8"),
77
+ "action": int(action),
78
+ "result": int(result),
79
+ }
80
+ uid += 1
81
+ board.push(move)