-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvisualization.py
More file actions
212 lines (170 loc) · 6.24 KB
/
visualization.py
File metadata and controls
212 lines (170 loc) · 6.24 KB
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
import plotly.express as px
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
import numpy as np
import plotly.graph_objects as go
from scipy import stats
# Input is a dataframe containing all of the players, output scaled df
def scale_visualization_data(player_df):
player_df = player_df.astype("float")
scaler = MinMaxScaler(copy=True)
player_df_scaled = player_df.copy()
player_df_scaled[player_df.columns] = scaler.fit_transform(player_df)
return player_df_scaled
def scale_player(player_name, player_cap_dict):
player_max = {"Control": 5, "Defense": 115, "Impact": 0.15, "Offense": 140, "Scoring": 0.65}
player_min = {"Control": 0, "Defense": 90, "Impact": 0, "Offense": 45, "Scoring": 0}
index1 = [player_name, 'player_max', 'player_min']
player_df = pd.DataFrame([player_cap_dict, player_max, player_min], index=index1)
player_df = player_df.astype("float")
feature_list = list(player_df.columns)
scaler = MinMaxScaler(copy=True)
player_df_scaled = player_df.copy()
player_df_scaled[player_df.columns] = scaler.fit_transform(player_df)
player_result = player_df_scaled.loc[player_name, :]
for i in range(len(player_result)):
if player_result[i] > 1:
player_result[i] = 0.7
if player_result[i] < 0:
player_result[i] = 0
return player_result, feature_list
def scale_team(team_name, team_cap_dict):
team_max = {"Offense": 58, "Defense": 48, "Scoring": 55, "Control": 25, "Impact": 47, "Tacit": 66, "Shooting": 66}
team_min = {"Offense": 35, "Defense": 20, "Scoring": 30, "Control": 7, "Impact": 6, "Tacit": 15, "Shooting": 40}
index1 = [team_name, 'team_max', 'team_min']
team_df = pd.DataFrame([team_cap_dict, team_max, team_min], index=index1)
team_df = team_df.astype("float")
feature_list = list(team_df.columns)
scaler = MinMaxScaler(feature_range=(0, 100), copy=True)
team_df_scaled = team_df.copy()
team_df_scaled[team_df.columns] = scaler.fit_transform(team_df)
team_result = team_df_scaled.loc[team_name, :]
for i in range(len(team_result)):
if team_result[i] > 100:
team_result[i] = 100
if team_result[i] < 0:
team_result[i] = 0
return dict(team_result)
def radar_capability(feature_list, player_value, playername):
fig = go.Figure()
player_value = list(player_value)
player_value.append(player_value[0])
feature_list.append(feature_list[0])
fig.add_trace(go.Scatterpolar(
r=player_value,
theta=feature_list,
fill='toself',
name=playername
))
fig.update_layout(
template="plotly_dark",
polar=dict(
radialaxis=dict(range=[-0.1, 1.1], showticklabels=False, ticks='')
),
title=playername,
font_size=20
)
return fig
# Input two player info to compare the two player capability
def radar_capability_comparison(feature_list, player1_value, player2_value, playername_list):
player1_value = list(player1_value)
player1_value.append(player1_value[0])
player2_value = list(player2_value)
player2_value.append(player2_value[0])
feature_list.append(feature_list[0])
fig = go.Figure()
fig.add_trace(go.Scatterpolar(
r=player1_value,
theta=feature_list,
fill='toself',
name=playername_list[0]
))
fig.add_trace(go.Scatterpolar(
r=player2_value,
theta=feature_list,
fill='toself',
name=playername_list[1]
))
fig.update_layout(
template="plotly_dark",
polar=dict(
radialaxis=dict(range=[-0.1, 1.1], showticklabels=False, ticks='')
),
font_size=20
)
return fig
# Calculate the Percentile of a value in a list
def calculate_pecentile(arr, x):
percentile = np.around(stats.percentileofscore(arr, x), decimals = 0)
return percentile
# mean = {"Defensive Mean:": np.mean(test_od["Defensive"]), "Offensive mean": np.mean(test_od["Offensive"])}
def off_def_plot(player_name, off_def_df):
off_def_df = off_def_df.astype("float")
DEF = list(off_def_df["Defense"])
OFF = list(off_def_df["Offense"])
player_od = off_def_df.loc[player_name]
player_def = player_od["Defense"]
player_off = player_od["Offense"]
off_perc = calculate_pecentile(OFF, player_off)
def_perc = calculate_pecentile(DEF, player_def)
fig = go.Figure()
fig.add_trace(go.Bar(
y=['Offensive Stats Ranking', 'Defensive Stats Ranking'],
x=[off_perc, def_perc],
name='name5',
orientation='h',
marker=dict(
color='rgba(122, 120, 168, 0.8)',
line=dict(color='rgb(248, 248, 249)', width=0.1)
)
))
fig.add_trace(go.Bar(
y=['Offensive Stats Ranking', 'Defensive Stats Ranking'],
x=[100 - off_perc, 100 - def_perc],
name='All NBA Players',
orientation='h',
marker=dict(
color='rgba(190, 192, 213, 1)',
line=dict(color='rgb(248, 248, 249)', width=0.1)
)
))
fig.update_layout(barmode='stack', template="plotly_dark",
width=500,
height=280,
xaxis=dict(
showgrid=True,
showline=False,
showticklabels=True,
zeroline=False,
domain=[0.15, 1]
),
title=player_name,
showlegend=False,
bargap=0.5
)
return player_off, player_def, fig
# For visualizing the
def team_summary(team_dict, team_name):
feature = list(team_dict.keys())
value = list(team_dict.values())
value = list(value)
value.append(value[0])
feature.append(feature[0])
fig = go.Figure()
fig.add_trace(go.Scatterpolar(
r=value,
theta=feature,
fill='toself',
name=team_name,
line_color='deepskyblue'
))
fig.update_layout(
template="plotly_dark",
polar=dict(
radialaxis=dict(range=[-5, 105], showticklabels=False)
),
title=team_name,
font_size=20
)
return fig