-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatestats.py
113 lines (87 loc) · 2.56 KB
/
updatestats.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
updatestats.py
~~~~
Tool to combine several stats file into one
"""
import json
from argparse import ArgumentParser
from collections import Counter, defaultdict
from datetime import datetime as dt
from pathlib import Path
from typing import Dict, List
def calculate_material_diff(pieces: str) -> int:
"""
Calculate material difference between two sides.
:param pieces: EGTB name
"""
piece_values = {
'K': 0, # always present, hence, irrelevant to the result
'P': 1,
'N': 3,
'B': 3,
'R': 5,
'Q': 9,
}
# EGTB name format: <pieces>v<pieces>
a, b = pieces.split('v')
mat_a = sum(piece_values[p] for p in a)
mat_b = sum(piece_values[p] for p in b)
return abs(mat_a - mat_b)
def material_diff_sort(egtbs: Dict[str, int]) -> Dict[str, int]:
"""
Sort EGTB dict by material difference and number of games.
:param egtbs: dict to sort
"""
def keyfunc(pair):
name, cnt = pair
# Negate count so the bigger number is first
return calculate_material_diff(name), -cnt
return dict(sorted(egtbs.items(), key=keyfunc))
def combine(files: List[Path], outfile: Path):
"""
Calculate cumulative statistics from several JSON files.
:param files: list with filepaths
:param outfile: output file to save to
"""
result = {
'created': dt.isoformat(dt.now()),
'total_games': 0,
'timecontrol': defaultdict(int),
}
most_games = defaultdict(int)
for file in files:
with open(file) as f:
data = json.load(f)
# Total games
result['total_games'] += data['total_games']
# Timecontrol
for k, v in data['timecontrol'].items():
result['timecontrol'][k] += v
# EGTBs
for k, v in data['EGTB_most_games'].items():
most_games[k] += v
# Material diff
result['EGTB_material_diff'] = material_diff_sort(most_games)
# Most games, sorted by number of games
result['EGTB_most_games'] = dict(Counter(most_games).most_common())
# Save
with open(outfile, 'w') as f:
json.dump(result, f)
def main():
ap = ArgumentParser()
ap.add_argument(
'files',
nargs='*',
default=[],
help='List of files to combine',
)
ap.add_argument(
'--outfile',
type=Path,
default=Path.cwd().joinpath('cumulative.json'),
help='Output file',
)
args = ap.parse_args()
combine(args.files, args.outfile)
if __name__ == '__main__':
main()