forked from benediktwerner/wcc_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.py
executable file
·74 lines (54 loc) · 1.98 KB
/
analysis.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
#! /usr/bin/env python3
import chess
import chess.pgn
import itertools
from statistics import mean
import os
import json
def pgnProcess(f, event):
def pairwise(iterable):
# pairwise('ABCDEFG') --> AB BC CD DE EF FG
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
output = {event: {}}
count = 0
while True:
game = chess.pgn.read_game(f)
if not game:
break
count += 1
output[event][str(count)] = dict()
print(event, count)
w = game.headers['White']
b = game.headers['Black']
evals = []
if sum(1 for _ in game.mainline()) < 10:
continue # skip games shorter than 10 ply
for node in itertools.islice(game.mainline(), 200): # no analysis after ply 200
if node.board().is_game_over():
continue # no eval for mate played
evals.append(max(min(node.eval().white(), chess.engine.Cp(1000)), chess.engine.Cp(-1000)).score()) # blunders above 1000 don't count
evals[0] = 35 # initial eval is 0 but should not be
def difference(n, evals):
a, b = evals
if n % 2 != 0:
return max((a - b), 0)
return max((b - a), 0)
cpls = []
for n, pair in enumerate(pairwise(evals)):
cpls.append(difference(n, pair))
white_evals = cpls[1::2]
black_evals = cpls[::2]
output[event][str(count)][w] = ('w', mean(white_evals), len(white_evals))
output[event][str(count)][b] = ('b', mean(black_evals), len(black_evals))
return output
full_output = []
events = os.listdir('./analysed_pgns')
for event in events:
#print(event)
with open(f'analysed_pgns/{event}') as f:
event_output = pgnProcess(f, event)
full_output.append(event_output)
with open('analysis.json', 'w') as f:
f.write(json.dumps(full_output))