-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval_one.py
88 lines (66 loc) · 2.3 KB
/
eval_one.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import chess.pgn
import chess
import pystockfish
import os, StringIO
import boto
from boto.s3.key import Key
from djeval import *
def get_game_from_s3(game_number):
s3conn = boto.connect_s3()
gamesbucket = s3conn.get_bucket('bc-games')
key_name = "kaggle/%s.pgn" % game_number
msg("Retrieving %s" % key_name)
k = gamesbucket.get_key(key_name)
game_pgn_string = k.get_contents_as_string()
game_fd = StringIO.StringIO(game_pgn_string)
game = chess.pgn.read_game(game_fd)
return game
def describe_movescores(ms):
# https://github.com/ornicar/lila/blob/master/modules/analyse/src/main/Advice.scala#L44-L47
print "Avg cp loss: ", sum(ms) / len(ms)
print "Inaccuracies: ", sum((ms > -100) & (ms <= -50))
print "Mistakes: ", sum((ms > -300) & (ms <= -100))
print "Blunders: ", sum( (ms <= -300))
print ms.describe()
def describe_position_scores(ps):
ds = []
print "POSITION SCORES"
sps = Series(ps)
print sps.describe()
clipped_sps = sps.clip(-999,999)
print "WHITE"
describe_movescores((clipped_sps.diff())[1::2])
print "BLACK"
describe_movescores((-1 * clipped_sps.diff())[2::2])
DEBUG = ('DEBUG' in os.environ)
if 'THREADS' in os.environ:
threads = int(os.environ['THREADS'])
else:
threads = 1
if 'HASH' in os.environ:
hash = int(os.environ['HASH'])
else:
hash = 100
backwards = ('BACKWARDS' in os.environ)
game_number = int(os.environ['GAMENUM'])
depth = None
if 'DEPTH' in os.environ:
depth = int(os.environ['DEPTH'])
movetime = None
if 'MOVETIME' in os.environ:
movetime = int(os.environ['MOVETIME'])
depth = None
movenum=None
if 'MOVENUM' in os.environ:
movenum = int(os.environ['MOVENUM'])
msg("Hi! Analyzing %i. Depth=%s, Movetime=%s" % (game_number, str(depth), str(movetime)))
engine = pystockfish.Engine(depth=depth, param={'Threads':threads, 'Hash':hash}, movetime=movetime)
game = get_game_from_s3(game_number)
print 'YO game is %s' % game
if backwards:
result_struct = do_it_backwards(engine=engine, game=game, debug=DEBUG, movenum=movenum)
else:
result_struct = do_it(engine=engine, game=game, depth=depth, debug=DEBUG)
print result_struct
#describe_position_scores(result_struct['position_scores'])
#describe_position_scores(result_struct['massaged_position_scores'])