This repository has been archived by the owner on Jun 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
138 lines (99 loc) · 5.46 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import chess
import chess.pgn
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
import seaborn as sns
import os
from pandasgui import show
from aggregation import aggregate
from processing import process_game, in_range
from plotting import *
def main():
filename = "./Dataframes/" + "The big one" + ".csv"
patterns = ["World", "Champ", "Candidates", "Interzonal", "PCA"]
# patterns = ["2000"]
# patterns = ["World"]
# patterns = ["Dae"]
directory = r'/home/jake/Downloads/pgn/'
# directory = r'./pgn/'
# directory = r'./pgn/FISC'
dfs = []
list_of_games = []
for entry in os.scandir(directory):
if (entry.path.endswith(".pgn") and entry.is_file()) and any(True for pattern in patterns \
if pattern in entry.name[:-4]):
print(entry.path)
df, games = aggregate(entry.path)
rows = []
for game in games:
lost_pieces = process_game(game)
rows.append(lost_pieces)
df["Lost pieces"] = rows
dfs.append(df)
list_of_games.extend(games)
df = pd.concat(dfs, ignore_index=True)
# filename = "./pgn/DeLaBourdonnais.pgn"
# filename = "./pgn/testing.pgn"
# meta data and game list
# df.to_csv(filename)
# pieces = [chess.ROOK, chess.KNIGHT]
pieces = list(chess.PIECE_TYPES)[:-1][::-1]
# Type casting and filsimilar to others surrounding it. If objects are atering
df_elo = df[df["WhiteElo"].apply(lambda x: x not in ["", "?", None, np.nan]) & \
df["BlackElo"].apply(lambda x: x not in ["", "?", None, np.nan])] \
.astype({"WhiteElo": 'int', "BlackElo": 'int'}) # type casts elo to ints and filters out the various non-int things
df["Date"] = pd.to_datetime(df["Date"].apply(lambda x: x.replace("?", "")[:4].replace(".", ""))) # type cast dates to dates, assumes year is present in all data
# plt.rcParams['figure.figsize'] = (10, 8)
plt.rcParams.update({'font.size': 14})
# ELO
minimum, low, low_mid, mid, mid_high, high, maximum = df_elo["WhiteElo"].quantile([0, 0.028, 0.1587, 0.5, 0.8413, 0.9772, 1])
bins_ranges = [minimum, low, low_mid, mid, mid_high, high, maximum]
bins = []
col_labels = []
for lower, upper in zip(bins_ranges[:-1], bins_ranges[1:]):
print(f"Elo in range {lower:.0f}-{upper:.0f}")
bins.append(df_elo[in_range(lower, df_elo["WhiteElo"], upper)])
col_labels.append(f"{lower:.0f} - {upper:.0f}\n{len(bins)-4 if len(bins) != 1 else '-∞'}σ to {len(bins)-3 if len(bins) != 6 else '∞'}σ")
bintype = "ELO"
# fig, axs = plot_heatmap_grid(bins, pieces, col_labels, chess.BLACK, bintype, username="DaenaliaEvandruile")
# fig, axs = plot_heatmap_grid(bins, pieces, col_labels, chess.WHITE, bintype, username="DaenaliaEvandruile")
# fig, axs = plot_hist_grid(bins, pieces, col_labels, chess.BLACK, bintype, username="DaenaliaEvandruile")
# fig, axs = plot_hist_grid(bins, pieces, col_labels, chess.WHITE, bintype, username="DaenaliaEvandruile")
fig, axs = plot_heatmap_grid(bins, pieces, col_labels, chess.BLACK, bintype)
fig, axs = plot_heatmap_grid(bins, pieces, col_labels, chess.WHITE, bintype)
fig, axs = plot_hist_grid(bins, pieces, col_labels, chess.BLACK, bintype)
fig, axs = plot_hist_grid(bins, pieces, col_labels, chess.WHITE, bintype)
# DATE
minimum, low, mid, high, maximum = df["Date"].quantile([0, 0.25, 0.5, 0.75, 1])
bins_ranges = [minimum, low, mid, high, maximum]
# bins_ranges = [pd.to_datetime("1700"), pd.to_datetime("1900"), pd.to_datetime("1980"), pd.to_datetime("2010"), pd.to_datetime("2025")]
bins = []
col_labels = []
for lower, upper in zip(bins_ranges[:-1], bins_ranges[1:]):
print(f"Date in range {lower.year}-{upper.year}")
bins.append(df[in_range(lower, df["Date"], upper)])
col_labels.append(f"{lower.year} to {upper.year}\nQ{len(bins)-1} - Q{len(bins)}")
bintype = "DATE"
# fig, axs = plot_heatmap_grid(bins, pieces, col_labels, chess.BLACK, bintype, username="DaenaliaEvandruile")
# fig, axs = plot_heatmap_grid(bins, pieces, col_labels, chess.WHITE, bintype, username="DaenaliaEvandruile")
# fig, axs = plot_hist_grid(bins, pieces, col_labels, chess.BLACK, bintype, username="DaenaliaEvandruile")
# fig, axs = plot_hist_grid(bins, pieces, col_labels, chess.WHITE, bintype, username="DaenaliaEvandruile")
fig, axs = plot_heatmap_grid(bins, pieces, col_labels, chess.BLACK, bintype)
fig, axs = plot_heatmap_grid(bins, pieces, col_labels, chess.WHITE, bintype)
fig, axs = plot_hist_grid(bins, pieces, col_labels, chess.BLACK, bintype)
fig, axs = plot_hist_grid(bins, pieces, col_labels, chess.WHITE, bintype)
# fig, axs = plot_heatmap_single_piece(df, [chess.PAWN], username="DaenaliaEvandruile", cmap=sns.color_palette("viridis", as_cmap=True))
# fig, axs = plot_heatmap_single_piece(df, [chess.PAWN], bintype)
for piece in chess.PIECE_TYPES[:-1]:
# fig, axs = plot_heatmap_single_piece(df, [piece], username="DaenaliaEvandruile")
fig, axs = plot_heatmap_single_piece(df, [piece])
# fig, axs = plot_hist_single_piece(df, [piece], username="DaenaliaEvandruile")
fig, axs = plot_hist_single_piece(df, [piece])
# plt.show()
# show(df)
print(len(df))
return df, list_of_games
if __name__ == "__main__":
df, games = main()