-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_statistics.py
99 lines (75 loc) · 3.46 KB
/
load_statistics.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
# © 2021 Joseph Craig <[email protected]>
# This code is not released under a standard OSS license. Please read README.md.
from typing import List, Tuple
import numpy as np
import pandas as pd
import re
import sys
def load_performances_new(analysis_filename: str) -> dict:
loaded = load(analysis_filename)
if not loaded:
print(f'Failed to load {analysis_filename}')
sys.exit(4)
date, game_name, dataframe = loaded
return {
'Game': dataframe['Delta'].to_numpy(),
'B': dataframe[dataframe['Player'] == 'B']['Delta'].to_numpy(),
'W': dataframe[dataframe['Player'] == 'W']['Delta'].to_numpy()
}
def load_performances(analysis_filename, use_rounded=True, minimum_moves=21):
performances = []
def add_performance(player, mistakes):
nonlocal performances
mean = np.mean(mistakes)
standard_deviation = np.std(mistakes)
performances.append((game_name, player, mistakes, mean, standard_deviation))
loaded = load(analysis_filename)
if not loaded:
print(f'Failed to load {analysis_filename}')
sys.exit(4)
date, game_name, dataframe = loaded
black_name = 'B'
black_mistakes = get_mistakes(dataframe, 'B', use_rounded=use_rounded)
if len(black_mistakes) >= minimum_moves:
add_performance(black_name, black_mistakes)
else:
print(f'Rejected ({game_name}, {black_name}) for being too short.')
white_name = 'W'
white_mistakes = get_mistakes(dataframe, 'W', use_rounded=use_rounded)
if len(white_mistakes) >= minimum_moves:
add_performance(white_name, white_mistakes)
else:
print(f'Rejected ({game_name}, {white_name}) for being too short.')
return performances
def load(analysis_filename):
matcher = re.search(r'^(?:[^/\\]*[/\\])*(?P<date>\d{4,}-\d{2}-\d{2})__.*__(?P<name>.+).csv$', analysis_filename)
if not matcher:
return None
return (
matcher.group('date'),
matcher.group('name'),
pd.read_csv(analysis_filename)
)
def get_mistakes(dataframe, player, use_rounded=True):
player_slice = dataframe[dataframe['Player'] == player]
if use_rounded:
result = player_slice['Mistake'].to_numpy()
else:
result = player_slice['Before'].to_numpy() - player_slice['After'].to_numpy()
return result
def get_expected_result(analysis_filename: str) -> List[float]:
_1, _2, dataframe = load(analysis_filename)
subset = zip(dataframe['Player'], dataframe['After'])
return [np.round(x if p == 'B' else -x, 1) for p, x in subset]
def get_worst_moves(analysis_filename: str) -> Tuple[List[int], np.array]:
_1, _2, dataframe = load(analysis_filename)
black_mistakes = np.array([x if p == 'B' else np.NaN for p, x in zip(dataframe['Player'], dataframe['Delta'])])
worst_black_mistake_indices = np.sort((-black_mistakes).argsort()[:10])
worst_black_mistakes = np.array([black_mistakes[i] for i in worst_black_mistake_indices])
white_mistakes = np.array([-x if p == 'W' else np.NaN for p, x in zip(dataframe['Player'], dataframe['Delta'])])
worst_white_mistake_indices = np.sort(white_mistakes.argsort()[:10])
worst_white_mistakes = np.array([white_mistakes[i] for i in worst_white_mistake_indices])
# indices are one off move numbers
indices = np.concatenate((worst_black_mistake_indices, worst_white_mistake_indices)) + 1
worst = np.concatenate((worst_black_mistakes, worst_white_mistakes))
return indices, worst