-
Notifications
You must be signed in to change notification settings - Fork 13
/
chart.py
331 lines (273 loc) · 12 KB
/
chart.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# A Graphical Visualization of Chess Openings
# April 2020
# Provides a colorful multi-level pie chart which shows the popularity of openings after moves
# For more info, go to www.github.com/Destaq/chess_graph
import plotly.graph_objects as go
from collections import Counter
import new_parser
import find_opening
import pandas as pd
import numpy as np
def form_values(gammme, depth, fragmentation_percentage, should_defragment, custom_branching, color, name):
"""Create parent, id, labels, and values """
lst, ratios, kick_depth = new_parser.parse_games(gammme, depth, custom_branching, color, name) #whether or not to implement custom branching
firstx = [
lst[i][:depth+kick_depth] for i in range(len(lst))
] # probably unneeded but for safety's sake...
all_level_moves, exclude_first_moves = [], [] # for parent/labels later
counter = kick_depth
holder = [firstx[i][0] for i in range(len(firstx))]
holder = dict(Counter(holder))
percentage_holder, firstmove = [], []
while counter < depth + kick_depth:
counter += 1
othermove = list(
Counter([tuple(firstx[i][kick_depth:counter]) for i in range(len(lst))]).items()
)
all_level_moves.append(othermove)
exclude_first_moves.append(
othermove
) # obviously excluding first moves (for parent creation)
pz = []
true_ids = [] # the ids, that is
parents = []
labels = []
for i in range(len(all_level_moves)):
if i == 0:
true_ids = [
all_level_moves[0][r][0] for r in range(len(all_level_moves[0]))
] # special ids for original parents
true_ids = [
item for sublist in true_ids for item in sublist
] # functions perfectly
labels += [
all_level_moves[i][f][0][0] for f in range(len(all_level_moves[i]))
] # similar to hackerrank diagonal
firstcount = len(labels)
else:
labels += [
all_level_moves[i][f][0][i] for f in range(len(all_level_moves[i]))
] # similar to hackerrank diagonal
true_ids += [
all_level_moves[i][r][0] for r in range(len(all_level_moves[i]))
]
parents += [
all_level_moves[i][r][0][: len(all_level_moves[i][r][0]) - 1]
for r in range(len(all_level_moves[i]))
]
pz += [z[0][:i] for ply_depth in exclude_first_moves for z in ply_depth]
parents = [""] * firstcount + parents # flattening
ids = true_ids
values = [i[1] for i in firstmove] + [
i[1] for move in exclude_first_moves for i in move
]
game_count = 0
for i in range(len(values)):
if parents[i] == "":
game_count += values[i]
for i in range(len(values)): # e.g e6 has 50, parent e4, with value 100
if parents[i] != "": # each child has parent, this one is e4 so yes
parent_id = parents[i] # parent id = e4
aka = list(parent_id)
if len(aka) >= 2:
aka = tuple(aka)
else:
aka = "".join(aka)
parent_value = ids.index(aka)
parent_value = values[parent_value]
complete_value = round((values[i] / parent_value) * 100, 2)
percentage_holder.append(complete_value)
else:
complete_value = round((values[i] / game_count) * 100, 2)
percentage_holder.append(complete_value)
num_games = 0
for i in range(len(parents)):
if parents[i] == '':
num_games+=values[i]
if should_defragment == True:
del_list = []
for i in range(len(values)):
if values[i]/num_games <= fragmentation_percentage:
del_list.append(i)
percentage_holder = [percentage_holder[i] for i in range(len(percentage_holder)) if i not in del_list]
ids = [ids[i] for i in range(len(ids)) if i not in del_list]
labels = [labels[i] for i in range(len(labels)) if i not in del_list]
parents = [parents[i] for i in range(len(parents)) if i not in del_list]
values = [values[i] for i in range(len(values)) if i not in del_list]
return ids, labels, parents, values, percentage_holder, lst, ratios, kick_depth
# fig = form(ids, labels, parents, values, full_ratios, full_ratios, percentage_everything, hovertip_openings, shade)
def form(ids, labels, parents, values, colors, ratios, percentage_everything, hovertip_openings, shade):
if shade:
fig = go.Figure(
go.Sunburst(
ids=ids,
labels=labels,
parents=parents,
values=values,
marker = dict(
colors = colors,
colorscale = 'Greys_r',
colorbar = dict(
thickness = 20,
title = dict(
text = 'White/Black Winning Percentage',
side = 'right'
)
)
),
leaf = {
'opacity': 1.0
},
branchvalues="total", # if children exceed parent, graph will crash and not show
insidetextorientation="horizontal", # text displays PP
hovertext=[
str(percentage_everything[i])
+ "% of Parent<br>Game Count: "
+ str(values[i])
+ '<br>Opening: '
+ hovertip_openings[i]
+ '<br>W/B Winning Percentage: ' # Note: this is wins+1/2draws/wins+losses+draws
+ str(ratios[i])
for i in range(len(percentage_everything))
],
hovertemplate="%{hovertext}<extra></extra>",
)
)
fig.update_layout(
margin=dict(t=30, l=30, r=30, b=0),
title = {
'text': "The Chess Opening Graph",
'xanchor': 'center',
'y':0.995,
'x':0.4715,
'yanchor': 'top',
'font': {
'size': 25
}
})
else:
fig = go.Figure(
go.Sunburst(
ids=ids,
labels=labels,
parents=parents,
values=values,
leaf = {
'opacity': 1.0
},
branchvalues="total", # if children exceed parent, graph will crash and not show
insidetextorientation="horizontal", # text displays PP
hovertext=[
str(percentage_everything[i])
+ "% of Parent<br>Game Count: "
+ str(values[i])
+ '<br>Opening: '
+ hovertip_openings[i]
+ '<br>W/B Winning Percentage: ' # Note: this is wins+1/2draws/wins+losses+draws
+ str(ratios[i])
for i in range(len(percentage_everything))
],
hovertemplate="%{hovertext}<extra></extra>",
)
)
fig.update_layout(
margin=dict(t=30, l=30, r=30, b=0),
title = {
'text': "The Chess Opening Graph",
'xanchor': 'center',
'y':0.995,
'x':0.50,
'yanchor': 'top',
'font': {
'size': 25
}
})
return fig #nasty thing should be fixed by autopep8
def find_colors(ids, ratios, lst, kick_depth):
holder = []
for i in range(len(ids)):
if type(ids[i]) != str:
holder.append(list(ids[i]))
else:
holder.append(list(ids[i].split(' ')))
lst = [lst[i][kick_depth:] for i in range(len(lst))]
white_list = [0]*len(holder)
black_list = [0]*len(holder)
draw_list = [0]*len(holder)
for i in range(len(holder)):
a = len(holder[i])
for r in range(len(lst)):
if lst[r][:a] == holder[i]:
if ratios[r] == '1-0':
white_list[i] += 1
elif ratios[r] == '0-1':
black_list[i] += 1
else:
draw_list[i] += 1
full_ratios = []
for i in range(len(white_list)):
result = round((white_list[i]+draw_list[i])/(black_list[i]+white_list[i]+draw_list[i]+draw_list[i]), 3)
full_ratios.append(result)
return full_ratios
def best_worst(ids, labels, parents, values, percentage_everything, full_ratios, min_games_best_lines):
df = pd.DataFrame()
df['ids'] = ids
df['labels'] = labels
df['parents'] = parents
df['values'] = values
df['percentage_everything'] = percentage_everything
df['full_ratios'] = full_ratios
def semimove_number(l):
a=len(l.parents)+1
return a
tmp = df.apply(semimove_number, axis=1)
tmp = tmp.to_frame()
tmp.columns = ['semimove']
df['semimove'] = tmp['semimove']
best_worst = pd.DataFrame(columns=['move', 'Best', 'b_score', 'b_games', 'Worst', 'w_score', 'w_games'])
for i in np.unique(df.semimove):
semimove_df = df[(df.semimove == i) & (df['values'] >= min_games_best_lines)]
if (len(semimove_df) > 0):
semimove_df = semimove_df.sort_values('full_ratios', ascending=False)
best = semimove_df.head(1)
worst = semimove_df.tail(1)
move = semimove_df.semimove.values[0]
Best = best['ids'].values[0]
b_score = best['full_ratios'].values[0]
b_games = best['values'].values[0]
Worst = worst['ids'].values[0]
w_score = worst['full_ratios'].values[0]
w_games = worst['values'].values[0]
best_worst.loc[len(best_worst)] = [move, Best, b_score, b_games, Worst, w_score, w_games]
else:
out_warning = 'No lines at move ' + str(i) + ' with at least ' + str(min_games_best_lines) + ' games'
print(out_warning)
print('\n')
best_worst.set_index('move')
print(best_worst)
print('\n')
def graph(database, depth=5, shade = True, fragmentation_percentage=0.0032, should_defragment=False, custom_branching=False, should_download = False, download_format = 'png', download_name = 'fig1', color = 'both', name = '', print_best_lines=False, min_games_best_lines=1): # need file path, depth,
ids, labels, parents, values, percentage_everything, lst, ratios, kick_depth = form_values(database, depth, fragmentation_percentage, should_defragment, custom_branching, color, name) # a good value is about 10x the smallest value
full_ratios = find_colors(ids, ratios, lst, kick_depth)
eco_codes, eco_names, eco_positions = find_opening.create_openings()
hovertip_openings = []
for i in range(len(ids)):
if ids[i] in eco_positions:
analyzed = eco_positions.index(ids[i])
hovertip_openings.append(eco_names[analyzed])
else:
hovertip_openings.append('Non-ECO Opening')
fig = form(ids, labels, parents, values, full_ratios, full_ratios, percentage_everything, hovertip_openings, shade)
fig.show()
def download(format, name = 'fig1'):
fig.write_image(name+'.'+format)
def download_html(name = 'fig1'):
fig.write_html(name+'.html')
static_download_formats = ['png', 'jpeg', 'svg', 'pdf', 'jpg', 'webp']
if should_download == True:
if download_format in static_download_formats:
download(download_format, download_name)
else:
download_html(download_name)
if print_best_lines == True:
best_worst(ids, labels, parents, values, percentage_everything, full_ratios, min_games_best_lines)