-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscouting_app.py
1401 lines (1258 loc) · 53.5 KB
/
scouting_app.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import streamlit as st
import streamlit.components.v1 as components
import pandas as pd
import numpy as np
import math
import seaborn as sns
from scipy import stats
import requests
import json
from urllib.parse import unquote
from urllib.request import urlopen
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
import streamlit_shadcn_ui as ui
from streamlit_extras.colored_header import colored_header
from st_keyup import st_keyup
from streamlit_extras.add_vertical_space import add_vertical_space
import streamlit_antd_components as sac
from streamlit_bokeh_events import streamlit_bokeh_events
from streamlit_vertical_slider import vertical_slider
from streamlit_lottie import st_lottie
from mplsoccer.pitch import Pitch, VerticalPitch
import matplotlib as mpl
import matplotlib.font_manager as fm
from highlight_text import fig_text
import plotly.graph_objects as go
from mplsoccer import PyPizza, add_image, FontManager
st.set_page_config(page_title='BotolaScout',
page_icon='⚽',
layout='wide',
initial_sidebar_state="expanded",)
def example():
st.write("## Notice how the output doesn't update until you hit enter")
out = st.text_input("Normal text input")
st.write(out)
st.write("## Notice how the output updates with every key you press")
out2 = st_keyup("Keyup input")
st.write(out2)
@st.cache_data
def load_data(path: str):
data = pd.read_csv(path)
return data
def convert_market_value(value):
# Ensure value is treated as string
if isinstance(value, float):
return value
if value == '-' or value == '':
return None
value = value.replace('€', '')
if 'k' in value:
return float(value.replace('k', '')) * 1e3
if 'm' in value:
return float(value.replace('m', '')) * 1e6
return float(value)
position_mapping = {
'GK': ['Goalkeeper'],
'DF': ['Defender', 'Right-Back', 'Centre-Back', 'Left-Back'],
'MID': ['Right Midfield', 'Left Midfield', 'Midfielder', 'Central Midfield', 'Defensive Midfield', 'Attacking Midfield'],
'FWD': ['Striker', 'Second Striker', 'Forward', 'Left Winger', 'Right Winger', 'Centre-Forward']
}
stats_mapping = {
'GK': ['saves', 'aerialDuelsWonPercentage', 'accurateLongBallsPercentage', 'cleanSheet', 'goalsConceded'],
'DF': ['accurateCrossesPercentage','accurateFinalThirdPasses',
'accuratePassesPercentage','accurateLongBallsPercentage','totalDuelsWonPercentage',
'ballRecovery', 'interceptions', 'aerialDuelsWonPercentage', 'clearances','blockedShots',
'errorLeadToShot', 'dribbledPast', ],
'MID': ['goalsAssistsSum','keyPasses','goalConversionPercentage','accurateCrossesPercentage',
'accuratePassesPercentage', 'accurateFinalThirdPasses', 'successfulDribblesPercentage', 'accurateLongBallsPercentage',
'ballRecovery', 'aerialDuelsWonPercentage', 'totalDuelsWonPercentage',
'dribbledPast'],
'FWD':['goalsAssistsSum','shotsOnTarget','goalConversionPercentage','keyPasses', 'accurateCrossesPercentage',
'successfulDribblesPercentage', 'accurateFinalThirdPasses','accuratePassesPercentage', 'wasFouled',
'aerialDuelsWonPercentage',
'offsides', 'scoringFrequency'],
}
spaces = ' '
pos = {'GK':'Goalkeepers', 'DF':'Defenders', 'MID':'Midfielders', 'FWD':'Forwards'}
def get_position_group(position):
for group, positions in position_mapping.items():
if position in positions:
return group
return None
def filter_positions(user_choices):
selected_positions = []
for choice in user_choices:
selected_positions.extend(position_mapping.get(choice, []))
return selected_positions
exclude_columns = ['Player',
'Nationality',
'Team_x',
'Position',
'Age',
'Height',
'Preferred Foot',
'Shirt Number',
'Market Value',
'Player Image',
'Team Logo',] # Columns to exclude from conversion
@st.cache_data
def load_lottieurl(url: str):
r = requests.get(url)
if r.status_code != 200:
return None
return r.json()
def convert_to_readable(name):
words = []
current_word = name[0]
for char in name[1:]:
if char.isupper() or char.isdigit():
words.append(current_word)
current_word = char
else:
current_word += char
words.append(current_word)
readable_name = ' '.join(word.capitalize() for word in words)
readable_name = readable_name.replace("Percentage", "%")
if readable_name == 'Accurate Final Third Passes':
readable_name = 'Accurate Final\nThird Passes'
if readable_name == 'Accurate Long Balls %':
readable_name = 'Accurate Long\nBalls %'
return readable_name
def get_color_category(value, df, stat, reverse_stats):
q1, q2, q3 = df[stat].quantile([0.25, 0.5, 0.75])
reverse = stat in reverse_stats
if not reverse:
if value < q1:
return '#fe003e' # Bottom 25%
elif value < q2:
return '#ffa500' # 25-50%
elif value < q3:
return '#ffef01' # 50-75%
else:
return '#00ff1d' # Top 25%
else:
if value > q3:
return '#fe003e' # Top 25%
elif value > q2:
return '#ffa500' # 50-75%
elif value > q1:
return '#ffef01' # 25-50%
else:
return '#00ff1d' # Bottom 25%
@st.cache_data
def display_stat(label, value, df, stat):
reverse_stats = ['goalsConceded', 'errorLeadToShot', 'dribbledPast', 'offsides', 'scoringFrequency']
color = get_color_category(value, df, stat, reverse_stats)
min_value = df[stat].min()
max_value = df[stat].max()
percentage = ((value - min_value) / (max_value - min_value)) * 100
if percentage == 0:
percentage = 2
if stat in reverse_stats:
color = '#00ff1d'
else:
color = '#fe003e'
if isinstance(value, float):
value = f"{value:.2f}"
else:
value = str(value)
if value == '0' or value == 'nan':
percentage = 0
if value == 'nan':
value = '--- '
st.markdown(f"""
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 5px;">
<span style="color: white;">{label}</span>
<span style="color: white;">{value + " min" if label == 'Scoring Frequency' else value}</span>
</div>
<div style="background-color: #1E2A38; height: 5px; width: 100%; margin-bottom: 15px;">
<div style="background-color: {color}; height: 100%; width: {percentage}%;"></div>
</div>
""", unsafe_allow_html=True)
def get_image_output(URL):
img = Image.open(urlopen(URL))
# Create a mask
mask = Image.new('L', img.size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + img.size, fill=255)
# Apply the mask to the image
output = Image.new('RGBA', img.size, (0, 0, 0, 0))
output.paste(img, (0, 0), mask)
return output
@st.cache_data
def pizza_plot(player_data, params_1, values, output):
position_group = get_position_group(player_data['Position'])
# Color palette
background_color = "#0c1421" # Dark blue background
main_color = "#00CED1" # Bright turquoise for the pizza slices
accent_color = "#FF6B6B" # Coral red for emphasis
text_color = "#E0E0E0" # Light gray for most text
highlight_color = "#FFD700" # Gold for important text/numbers
# Slice colors (adjust as needed)
colors_mapping = {'DF':[2,3,5], 'MID':[4,4,3], 'FWD':[5,4,1]}
if position_group == 'GK':
slice_colors = ["#11C9AF"] * 4
else:
num_colors = colors_mapping[position_group]
slice_colors = ["#11C9AF"] * num_colors[0] + ["#FFA500"] * num_colors[1] + ["#D70232"] * num_colors[2]
# Instantiate PyPizza class
baker = PyPizza(
params=params_1,
straight_line_color="#2A3F5F", # Slightly lighter than background for subtle lines
straight_line_lw=1,
last_circle_color="#4A6491", # Highlighted outer circle
last_circle_lw=2,
other_circle_color="#2A3F5F", # Same as straight lines
other_circle_lw=1,
other_circle_ls="-.",
inner_circle_size=20
)
# Plot pizza
fig, ax = baker.make_pizza(
values,
figsize=(8, 8),
param_location=110,
slice_colors=slice_colors, # Color for individual slices
#value_colors=text_colors, # Color for the value-text
#value_bck_colors=slice_colors, # Color for the blank spaces
blank_alpha=0.4, # Alpha for blank-space colors
kwargs_slices=dict(
edgecolor="#FFFFFF", zorder=2, linewidth=1, alpha=0.8
),
kwargs_params=dict(
color=text_color, fontsize=11,
va="center", fontweight='bold'
),
kwargs_values=dict(
color=highlight_color, fontsize=11,
zorder=3, fontweight='bold',
bbox=dict(
edgecolor=accent_color, facecolor=background_color,
boxstyle="round,pad=0.2", lw=1, alpha=0.8
)
)
)
# Set background color
fig.patch.set_facecolor(background_color)
ax.set_facecolor(background_color)
# Add title
fig.text(
0.515, 0.98, f"{player_data['Player']} - {player_data['Team_x']}", size=18,
ha="center", color=highlight_color, fontweight='bold'
)
# Add subtitle
fig.text(
0.515, 0.94,
f" ┆ Percentile Rank vs Botola Pro {pos[position_group]} | Season 2023-24 ┆",
size=14,
ha="center", color=text_color
)
# Add image
ax_image = add_image(
output, fig, left=0.4478, bottom=0.4315, width=0.13, height=0.127
)
# Add rectangles for slice categories
if position_group != 'GK':
fig.text(
0.99, 0.005, f"Attacking\nPossession\nDefending", size=14,
color=text_color,
ha="center"
)
fig.patches.extend([
plt.Rectangle(
(0.89, 0.06), 0.025, 0.021, fill=True, color="#11C9AF",
transform=fig.transFigure, figure=fig
),
plt.Rectangle(
(0.89, 0.03), 0.025, 0.021, fill=True, color="#FFA500",
transform=fig.transFigure, figure=fig
),
plt.Rectangle(
(0.89, 0.00), 0.025, 0.021, fill=True, color="#d70232",
transform=fig.transFigure, figure=fig
),
])
return fig
@st.cache_data
def pizza_plot_comparison(params_1, values, values_2, player_data, player_name_2, output, output2):
position_group = get_position_group(player_data['Position'])
# Color palette
background_color = "#0c1421" # Dark blue background
text_color = "#E0E0E0" # Light gray for most text
highlight_color = "#FFD700" # Gold for important text/numbers
# Player colors
player1_color = "#11C9AF" # Turquoise for player 1
player2_color = "#FFA500" # Orange for player 2
params_offset = []
for v1, v2 in zip(values, values_2):
distance = abs(v1 - v2)
params_offset.append(distance < 10)
# instantiate PyPizza class
baker = PyPizza(
params=params_1,
straight_line_color="#2A3F5F", # Slightly lighter than background for subtle lines
straight_line_lw=1,
last_circle_color="#4A6491", # Highlighted outer circle
last_circle_lw=2,
other_circle_color="#2A3F5F", # Same as straight lines
other_circle_lw=1,
other_circle_ls="-.",
inner_circle_size=20
)
# plot pizza
fig, ax = baker.make_pizza(
values, # list of values
compare_values=values_2, # comparison values
figsize=(8, 8), # adjust figsize according to your need
blank_alpha=0.4,
kwargs_slices=dict(
facecolor=player1_color, edgecolor="#FFFFFF", zorder=2, linewidth=1, alpha=1
),
kwargs_compare=dict(
facecolor=player2_color, edgecolor="#FFFFFF", zorder=2, linewidth=1, alpha=1
),
kwargs_params=dict(
color=text_color, fontsize=11,
va="center", fontweight='bold'
),
kwargs_values=dict(
color='#000000', fontsize=11,
zorder=3, fontweight='bold',
bbox=dict(
edgecolor='#000000', facecolor=player1_color,
boxstyle="round,pad=0.2", lw=1, alpha=0.8
)
),
kwargs_compare_values=dict(
color='#000000', fontsize=11, zorder=3, fontweight='bold',
bbox=dict(
edgecolor='#000000', facecolor=player2_color,
boxstyle="round,pad=0.2", lw=1, alpha=0.8
)
)
)
# Set background color
fig.patch.set_facecolor(background_color)
ax.set_facecolor(background_color)
# adjust text for comparison-values-text
baker.adjust_texts(params_offset, offset=-0.17, adj_comp_values=True)
# add title
fig_text(
0.515, 1.01, f"<{player_data['Player']}> vs <{player_name_2}>", size=17, fig=fig,
highlight_textprops=[{"color": player1_color}, {"color": player2_color}],
ha="center", color=text_color, fontweight='bold'
)
# Add subtitle
fig.text(
0.515, 0.94,
f" ┆ Percentile Rank vs Botola Pro {pos[position_group]} | Season 2023-24 ┆",
size=14,
ha="center", color=text_color
)
# Add player images and names
# Player 1
add_image(
output, fig, left=-0.03, bottom=0.9, width=0.13, height=0.13
)
# Player 2
add_image(
output2, fig, left=0.95, bottom=0.9, width=0.13, height=0.13
)
return fig
@st.cache_data
def display_player_card(player):
st.markdown("""
<style>
body {
color: white;
background-color: #0E1117;
}
.player-card {
background-color: #1E2A38;
border-radius: 15px;
padding: 25px;
color: white;
display: flex;
flex-direction: column;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
border: 2px solid #FFA500;
}
.player-header {
display: flex;
align-items: center;
margin-bottom: 25px;
}
.player-image {
width: 120px;
height: 120px;
border-radius: 50%;
margin-right: 25px;
border: 3px solid #11C9AF;
}
.player-info {
flex-grow: 1;
}
.player-name {
font-family:cursive;
font-size: 30px;
font-weight: bold;
margin: 0;
color: white;
}
.player-team {
font-size: 18px;
margin: 5px 0;
color: white;
}
.team-logo {
width: 35px;
height: 35px;
margin-right: 10px;
vertical-align: middle;
}
.stats {
display: flex;
justify-content: space-between;
margin-bottom: 25px;
background-color: #263238;
border-radius: 10px;
padding: 15px;
border: 2px solid #11C9AF;
}
.stat {
text-align: center;
}
.stat-value {
font-size: 24px;
font-weight: bold;
color: white;
}
.stat-label {
font-size: 16px;
color: #BDD0DA;
}
.player-bio {
display: flex;
flex-wrap: wrap;
background-color: #263238;
border-radius: 10px;
padding: 15px;
border: 2px solid #11C9AF;
}
.bio-item {
width: 50%;
margin-bottom: 12px;
}
.bio-label {
font-weight: bold;
color: #BDD0DA;
}
.bio-value {
font-size: 18px;
font-weight: bold;
color: white;
}
h3 {
color: #FFA500;
margin-top: 20px;
}
""", unsafe_allow_html=True)
html = f"""
<div class="player-card">
<div class="player-header">
<img src="{player['Player Image']}" class="player-image">
<div class="player-info">
<h2 class="player-name">{player['Player']}</h2>
<p class="player-team"><img src="{player['Team Logo']}" class="team-logo">{player['Team_x']}</p>
</div>
</div>
<div class="stats">
<div class="stat">
<div class="stat-value">{player['appearances']}</div>
<div class="stat-label">Appearances</div>
</div>
<div class="stat">
<div class="stat-value">{player['minutesPlayed']}</div>
<div class="stat-label">Minutes Played</div>
</div>
<div class="stat">
<div class="stat-value">{player['rating']:.2f}</div>
<div class="stat-label">Rating</div>
</div>
</div>
<h3>Player Bio</h3>
<div class="player-bio">
<div class="bio-item"><span class="bio-label">Nationality:</span><span class="bio-value">{spaces*2} {player['Nationality']}</span></div>
<div class="bio-item"><span class="bio-label">Position:</span><span class="bio-value">{spaces*2} {player['Position']}</span></div>
<div class="bio-item"><span class="bio-label">Age:</span><span class="bio-value">{spaces*2} {player['Age']}</span></div>
<div class="bio-item"><span class="bio-label">Height:</span><span class="bio-value">{spaces*2} {player['Height']}</span></div>
<div class="bio-item"><span class="bio-label">Market Value:</span><span class="bio-value">{spaces*2} {player['Market Value']}</span></div>
<div class="bio-item"><span class="bio-label">Preferred Foot:</span><span class="bio-value">{spaces*2} {player['Preferred Foot']}</span></div>
</div>
</div>
"""
st.markdown(html, unsafe_allow_html=True)
@st.cache_data
def heatmap(df, player_name):
player_df = df[df['Player'] == player_name]
# Set up the pitch
pitch = Pitch(pitch_type='opta',
pitch_color='#0f0c2b',#22312b
line_color='#c7d5cc',#c7d5cc
corner_arcs=True,
line_zorder=2,
)
fig, ax = pitch.draw(figsize=(8, 6), constrained_layout=True, tight_layout=False)
fig.set_facecolor('#0f0c2b')#22312b
fig.patch.set_edgecolor('#FFA500')
fig.patch.set_linewidth(2)
pitch.kdeplot(player_df['x'], player_df['y'], ax=ax,
cmap='magma',#magma
fill=True,
n_levels=10,
zorder=1,
shade_lowest=True,
bw_adjust=0.3,
)
plt.tight_layout(pad=0)
return fig
@st.cache_data
def shotmap(df, player):
df_goal = df[(df["shotType"] == "goal") & (df["player_name"] == player)].copy()
df_miss = df[(df["shotType"] == "miss") & (df["player_name"] == player)].copy()
df_save = df[(df["shotType"] == "save") & (df["player_name"] == player)].copy()
df_block = df[(df["shotType"] == "block") & (df["player_name"] == player)].copy()
df_post = df[(df["shotType"] == "post") & (df["player_name"] == player)].copy()
# Set up the pitch
pitch = VerticalPitch(pitch_type='opta',
pitch_color='#0c1421',#22312b
line_color='#c7d5cc',#c7d5cc
corner_arcs=True,
#line_zorder=5,
half=True
)
fig, ax = pitch.draw(figsize=(8, 6))
fig.set_facecolor('#22312b')#22312b
fig.patch.set_edgecolor('#FFA500')
fig.patch.set_linewidth(2)
# goals:
sc_g = pitch.scatter(df_goal["new_x"],
df_goal["new_y"],
marker='football',
#s=50,
s=df_goal["xg"]*500+100,
ax=ax,
label="Goals",
zorder=4)
# missed
sc_m = pitch.scatter(df_miss["new_x"],
df_miss["new_y"],
c='red',
#s=100,
s=df_miss["xg"]*500+100,
ax=ax,
label="Missed",
zorder=1)
# saved
sc_s = pitch.scatter(df_save["new_x"],
df_save["new_y"],
c='yellow',
#s=100,
s=df_save["xg"]*500+100,
ax=ax,
label="Saved",
zorder=3)
# blocked
sc_b = pitch.scatter(df_block["new_x"],
df_block["new_y"],
c='#2463AD',
#s=100,
s=df_block["xg"]*500+100,
ax=ax,
label="Blocked",
zorder=2)
# post
sc_p = pitch.scatter(df_post["new_x"],
df_post["new_y"],
c='#1C8714',
#s=100,
s=df_post["xg"]*500+100,
ax=ax,
label="Post",
zorder=2)
ax.legend(loc='lower left', bbox_to_anchor=(0.05, 0.05), ncol=2, fontsize=10.2, frameon=True, edgecolor='black', markerscale=0.8)
plt.tight_layout(pad=0)
return fig
@st.cache_data
def beeswarmplot(players_df, player_data, stat):
defend_positions = [
'Defender', 'Right-Back', 'Centre-Back', 'Left-Back'
]
midfield_positions = [
'Right Midfield', 'Left Midfield', 'Midfielder',
'Central Midfield', 'Defensive Midfield', 'Attacking Midfield'
]
forward_positions = [
'Striker', 'Second Striker', 'Forward',
'Left Winger', 'Right Winger', 'Centre-Forward'
]
goalkeepers = players_df[players_df['Position'] == 'Goalkeeper']
defenders = players_df[players_df['Position'].isin(defend_positions)]
midfielders = players_df[players_df['Position'].isin(midfield_positions)]
forwards = players_df[players_df['Position'].isin(forward_positions)]
position = player_data['Position']
#set default colors
text_color = '#FFFFFF'
background = '#0c1421'
if position == 'Goalkeeper':
df = goalkeepers.copy()
pos = 'Goalkeepers'
elif position in defend_positions:
df = defenders.copy()
pos = 'Defenders'
elif position in midfield_positions:
df = midfielders.copy()
pos = 'Midfielders'
elif position in forward_positions:
df = forwards.copy()
pos = 'Forwards'
# Remove players with played less than 900 min
df['minutesPlayed'] = df['minutesPlayed'].replace('-', np.nan)
df['90s'] = (df['minutesPlayed'] / 90).round(1)
df = df[df['90s'] >= 10]
# Remove players with 0 value for the stat
df = df[df[stat] > 0]
# Calculate quartiles
q1, q2, q3 = df[stat].quantile([0.25, 0.5, 0.75])
# Create color categories
df['color_category'] = pd.cut(df[stat],
bins=[-np.inf, q1, q2, q3, np.inf],
labels=['Bottom 25%', '25-50%', '50-75%', 'Top 25%'])
fig, ax = plt.subplots(figsize=(6,4))
fig.set_facecolor(background)
ax.patch.set_facecolor(background)
#fig.patch.set_edgecolor('#FFA500')
#fig.patch.set_linewidth(2)
#set up our base layer
mpl.rcParams['xtick.color'] = text_color
mpl.rcParams['ytick.color'] = text_color
spines = ['top','bottom','left','right']
for x in spines:
if x in spines:
ax.spines[x].set_visible(False)
reverse_stats = ['goalsConceded', 'errorLeadToShot', 'dribbledPast', 'offsides', 'scoringFrequency']
if stat in reverse_stats:
custom_palette={'Bottom 25%': '#00ff1d', '25-50%': '#ffef01', '50-75%': '#ffa500', 'Top 25%': '#fe003e'}
else:
custom_palette={'Bottom 25%': '#fe003e', '25-50%': '#ffa500', '50-75%': '#ffef01', 'Top 25%': '#00ff1d'}
sns.swarmplot(x=stat, data=df,
hue='color_category',
palette=custom_palette,
size=15, zorder=1,
legend=False)
# Remove the legend if you don't need it
#plt.legend([],[], frameon=False)
# Set x-axis ticks based on data range
x_min = df[stat].min()
x_max = df[stat].max()
x_range = x_max - x_min
if x_range <= 30:
step = 2
elif x_range <= 50:
step = 5
elif x_range <= 100:
step = 10
else:
step = 20
x_ticks = np.arange(np.floor(x_min), np.ceil(x_max) + step, step)
plt.xticks(x_ticks)
#plot player
if player_data['Player'] in df['Player'].values:
value = df.loc[df['Player'] == player_data['Player'], stat].values[0]
plt.scatter(x=value,y=0,c=text_color,s=250,zorder=2)
title_text = f"Distribution of {stat} Among Botola Pro {pos}"
# Get a cursive font
cursive_fonts = [f for f in fm.fontManager.ttflist if 'cursive' in f.name.lower()]
if cursive_fonts:
cursive_font = cursive_fonts[0].fname
else:
cursive_font = 'Arial' # Fallback to Arial if no cursive font is found
ax.text(0.5, 0.93, title_text,
horizontalalignment='center',
verticalalignment='center',
transform=ax.transAxes,
fontsize=12,
fontfamily='cursive',
fontweight='bold',
color='white',
wrap=True)
plt.xlabel(stat,c=text_color)
#plt.tight_layout(pad=-0.5)
return fig
@st.cache_data
def player_radar_chart(player_name, metrics, values, avg_values):
# Ensure values are integers
numeric_values = [int(v) for v in values if str(v).isdigit()]
numeric_avg_values = [int(v) for v in avg_values if str(v).isdigit()]
if not numeric_values or not numeric_avg_values:
print("Error: No valid integer values provided.")
return
# Ensure the first value is repeated at the end to close the polygon
values_plot = numeric_values + [numeric_values[0]]
avg_values_plot = numeric_avg_values + [numeric_avg_values[0]]
metrics_plot = metrics + [metrics[0]]
# Create the trace for the average values
avg_trace = go.Scatterpolar(
r=avg_values_plot,
theta=metrics_plot,
name='GK Average',
line=dict(color='#FFA500', width=2),
hoverinfo='text',
text=[f'GK Average {metric}: {value}' for metric, value in zip(metrics, numeric_avg_values)] + [f'GK Average {metrics[0]}: {numeric_avg_values[0]}']
)
# Create the trace for the player
player_trace = go.Scatterpolar(
r=values_plot,
theta=metrics_plot,
fill='toself',
name=player_name,
fillcolor='rgba(17, 201, 175, 0.5)', # Semi-transparent fill
line=dict(color='#11C9AF', width=2),
hoverinfo='text',
text=[f'{player_name} {metric}: {value}' for metric, value in zip(metrics, numeric_values)] + [f'{player_name} {metrics[0]}: {numeric_values[0]}']
)
# Create the layout
grid_color = '#BDD0DA'
layout = go.Layout(
polar=dict(
radialaxis=dict(
visible=True,
range=[0, 70],
color='white',
gridcolor=grid_color,
tickfont=dict(size=13), # Increased tick font size
tickmode='array',
tickvals=[0, 10, 20, 30, 40, 50, 60, 70],
),
angularaxis=dict(
gridcolor=grid_color,
linecolor='white',
tickfont=dict(color='white', size=14) # Increased angular tick font size
),
bgcolor='#0c1421'
),
showlegend=True,
legend=dict(font=dict(color='white', size=14), bgcolor='rgba(12, 20, 33, 0.8)'), # Increased legend font size
width=600,
height=500,
paper_bgcolor='#0c1421',
plot_bgcolor='#0c1421',
font=dict(color='white', size=14) # Increased global font size
)
# Create the figure and display it
fig = go.Figure(data=[player_trace, avg_trace], layout=layout)
# Update the radial axis to ensure consistent scaling
fig.update_layout(
polar=dict(
radialaxis=dict(
range=[0, 70],
tickmode='array',
tickvals=[0, 10, 20, 30, 40, 50, 60, 70],
ticktext=['0', '10', '20', '30', '40', '50', '60', '70']
)
)
)
st.plotly_chart(fig, use_container_width=True)
def player_details(df, player_data):
heatmap_df = pd.read_csv('https://raw.githubusercontent.com/MS3B09/Botola-Scout/main/Datasets/Botola%20Players%20HeatMaps.csv')
shotmap_df = pd.read_csv('https://raw.githubusercontent.com/MS3B09/Botola-Scout/main/Datasets/Botola%20Players%20ShotMaps.csv')
spaces = ' '
st.set_option('deprecation.showPyplotGlobalUse', False)
st.markdown("""
<style>
.colored-line {
height: 3px;
width: 1370px;
background: linear-gradient(to right, #11C9AF, #FFA500, #9b59b6);
margin: -10px 0;
}
.title {
font-family:cursive;
font-size: 40px;
font-weight: bold;
text-align: center;
margin-bottom: 30px;
background-color: #082630;
padding: 20px;
border-radius: 10px;
border: 2px solid #11C9AF;
}
</style>
""", unsafe_allow_html=True)
col1, col2 = st.columns((1,8))
with col1:
lottie_animation = load_lottieurl("https://lottie.host/a79be04b-d18c-4f2b-8341-9bd28d0ab5d5/ke10ceU8Pg.json")
st_lottie(lottie_animation, loop=False, height=150, width=150, speed=1)
with col2:
add_vertical_space(1)
#st.title('Pape Badji Stats for Moghreb Tetouan 2023/24')
st.markdown(f"<p class='title'>{player_data['Player']} Stats for {player_data['Team_x']} 2023/24</p>", unsafe_allow_html=True)
st.markdown('<div class="colored-line"></div>', unsafe_allow_html=True)
col1, col2 = st.columns(2)
with col1:
st.markdown("<h1><span style='color:#FFA500; font-size:40px; font-family:cursive'>Player Profile </span></h1>", unsafe_allow_html=True)
display_player_card(player_data)
with col2:
add_vertical_space(5)
st.markdown(f"<h1>{spaces*3}<span style='color:#FFA500; font-size:40px; font-family:cursive'>Season HeatMap </span></h1>", unsafe_allow_html=True)
fig = heatmap(heatmap_df, player_data['Player'])
st.pyplot(fig, bbox_inches='tight', pad_inches=0.00, use_container_width=True)
col1, col2 = st.columns(2)
with col1:
add_vertical_space(4)
st.markdown(f"<h1>{spaces}<span style='color:#FFA500; font-size:40px; font-family:cursive'>Key Stats </span></h1>", unsafe_allow_html=True)
add_vertical_space(4)
position_group = get_position_group(player_data['Position'])
if position_group:
# Get the stats for the position group
key_stats = stats_mapping[position_group]
stats_df = df[df['Position'].isin(position_mapping[position_group])]
# Remove players with played less than 900 min
stats_df['minutesPlayed'] = stats_df['minutesPlayed'].replace('-', np.nan)
stats_df['90s'] = (stats_df['minutesPlayed'] / 90).round(1)
if player_data['minutesPlayed'] >= 900:
stats_df = stats_df[stats_df['90s'] >= 10]
midpoint = math.ceil(len(key_stats) / 2)
col01,col02 = st.columns(2)
# Display stats in two columns
for i, stat in enumerate(key_stats):
stats_df[stat] = stats_df[stat].replace('-', np.nan)
pos_stats_df = stats_df[stats_df[stat] > 0]
# Choose the column based on the index
with col01 if i < midpoint else col02:
stat_name = convert_to_readable(stat)
display_stat(stat_name, player_data[stat], pos_stats_df, stat)
with col2:
add_vertical_space(1)
st.markdown(f"<h1>{spaces}<span style='color:#FFA500; font-size:40px; font-family:cursive'>Key Stats Distribution</span></h1>", unsafe_allow_html=True)
stat = st.selectbox(
label=' ╰┈➤ Select a Stat : ❗(For Players who Played >= 900 mins)',
options=key_stats,
index=0,
format_func=convert_to_readable,
#label_visibility="collapsed",
)
fig = beeswarmplot(df, player_data, stat)
st.pyplot(fig, bbox_inches='tight', pad_inches=0)
#col1, col2, col3 = st.columns((1,2,1))
col1, col2 = st.columns((1,1.2))
with col1:
add_vertical_space(2)
if position_group == 'GK':
st.markdown(f"<h1>{spaces}<span style='color:#FFA500; font-size:40px; font-family:cursive'>Attribute Overview </span></h1>", unsafe_allow_html=True)
gk_df = pd.read_csv('https://raw.githubusercontent.com/MS3B09/Botola-Scout/main/Datasets/GK%20Attributes.csv')
params = list(gk_df.columns)
params = params[2:]
if player_data['Player'] in gk_df['Player'].values:
player = gk_df.loc[gk_df['Player']==player_data['Player']].reset_index()
player = list(player.loc[0])
values = player[3:]
else:
values = [0,0,0,0,0]
avg_values = [53, 64, 47, 63, 61]
player_radar_chart(player_data['Player'], params, values, avg_values)
else:
st.markdown(f"<h1>{spaces}<span style='color:#FFA500; font-size:40px; font-family:cursive'>Season ShotMap </span></h1>", unsafe_allow_html=True)
add_vertical_space(1)
fig = shotmap(shotmap_df, player_data['Player'])
st.pyplot(fig, bbox_inches='tight', pad_inches=0)
with col2:
add_vertical_space(2)
st.markdown(f"<h1>{spaces}<span style='color:#FFA500; font-size:30px; font-family:cursive'>╰┈➤ Compare with other {pos[position_group]}: </span></h1>", unsafe_allow_html=True)
col01,col02 = st.columns(2)
with col01:
add_vertical_space(1)
clubs = df['Team_x'].unique()
clubs = np.insert(clubs, 0, '- - -')
team_name_2 = st.selectbox(
label=f'Team :',
options=clubs,
index=0,
#label_visibility="collapsed",
)
with col02:
add_vertical_space(1)
players_pos_df = df[
(df['Position'].isin(position_mapping[position_group])) &
(df['Team_x'] == team_name_2) &
(df['rating'] != '-')]
players_pos = ['- - -'] + players_pos_df['Player'].tolist()
player_name_2 = st.selectbox(
label='Player :',
options=players_pos,
index=0,
#label_visibility="collapsed",
)
pizza_df = df[df['Position'].isin(position_mapping[position_group])]
params = stats_mapping[position_group]
reverse_stats = ['goalsConceded', 'errorLeadToShot', 'dribbledPast', 'offsides', 'scoringFrequency']
# Create a new list to store the filtered params
filtered_params = []
params_1 = []
for param in params:
pizza_df[param] = pizza_df[param].replace('-', 0.0)
pizza_df[param] = pizza_df[param].replace(np.nan, 0.0)
if param not in reverse_stats:
filtered_params.append(param)
params_1.append(convert_to_readable(param))
player = [player_data[param] for param in filtered_params]
values = []
for x in range(len(filtered_params)):
values.append(math.floor(stats.percentileofscore(pizza_df[filtered_params[x]],player[x])))
output = get_image_output(player_data['Player Image'])
if player_name_2 == '- - -':
fig = pizza_plot(player_data, params_1, values, output)
st.pyplot(fig, bbox_inches='tight', pad_inches=0)
else:
player2_data = df[df['Player'] == player_name_2].iloc[0]