Spaces:
Sleeping
Sleeping
Update pages/2player_comparison.py
Browse files- pages/2player_comparison.py +44 -32
pages/2player_comparison.py
CHANGED
@@ -17,9 +17,9 @@ def load_model_and_encoder():
|
|
17 |
# Load player dataset
|
18 |
@st.cache_data
|
19 |
def load_data():
|
20 |
-
|
21 |
-
return df
|
22 |
|
|
|
23 |
def get_matching_player(name_from_file, player_list):
|
24 |
name_lower = name_from_file.lower()
|
25 |
for player in player_list:
|
@@ -27,12 +27,13 @@ def get_matching_player(name_from_file, player_list):
|
|
27 |
return player
|
28 |
return None
|
29 |
|
|
|
30 |
def plot_horizontal_bar(df, player1, player2):
|
31 |
st.subheader("π Stat Comparison - Horizontal Bar Chart")
|
32 |
num_cols = df.select_dtypes(include='number').columns
|
33 |
df_num = df[num_cols].T
|
34 |
df_num.columns = [player1, player2]
|
35 |
-
df_num = df_num.sort_values(by=player1, ascending=False).head(15)
|
36 |
|
37 |
fig, ax = plt.subplots(figsize=(10, 7))
|
38 |
df_num.plot(kind='barh', ax=ax)
|
@@ -42,31 +43,42 @@ def plot_horizontal_bar(df, player1, player2):
|
|
42 |
ax.legend(loc="lower right")
|
43 |
st.pyplot(fig)
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
values1 = [player1_data[cat] for cat in categories]
|
51 |
-
values2 = [player2_data[cat] for cat in categories]
|
52 |
-
|
53 |
-
angles = np.linspace(0, 2 * np.pi, len(categories), endpoint=False).tolist()
|
54 |
-
values1 += values1[:1]
|
55 |
-
values2 += values2[:1]
|
56 |
-
angles += angles[:1]
|
57 |
-
|
58 |
-
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))
|
59 |
-
ax.plot(angles, values1, label=player1, linewidth=2)
|
60 |
-
ax.fill(angles, values1, alpha=0.25)
|
61 |
-
|
62 |
-
ax.plot(angles, values2, label=player2, linewidth=2)
|
63 |
-
ax.fill(angles, values2, alpha=0.25)
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
st.pyplot(fig)
|
69 |
|
|
|
70 |
def main():
|
71 |
st.set_page_config(layout="wide")
|
72 |
st.title("Cricket Player Comparison Tool π")
|
@@ -101,21 +113,21 @@ def main():
|
|
101 |
with col4:
|
102 |
st.image(img2, caption=player2_name, use_container_width=True)
|
103 |
|
104 |
-
#
|
105 |
comparison_df = pd.DataFrame([player1_data, player2_data])
|
106 |
comparison_df.set_index('Player', inplace=True)
|
107 |
-
|
108 |
-
st.subheader("π Detailed Stats")
|
109 |
st.dataframe(comparison_df.T)
|
110 |
|
111 |
-
#
|
112 |
plot_horizontal_bar(comparison_df, player1_name, player2_name)
|
113 |
-
|
|
|
114 |
|
115 |
else:
|
116 |
-
st.error("Player names from image files don't match or are the same. Please check file names.")
|
117 |
else:
|
118 |
-
st.info("Please upload two player images to continue.")
|
119 |
|
120 |
if __name__ == "__main__":
|
121 |
main()
|
|
|
17 |
# Load player dataset
|
18 |
@st.cache_data
|
19 |
def load_data():
|
20 |
+
return pd.read_csv('Reduced_final_teams.csv')
|
|
|
21 |
|
22 |
+
# Fuzzy match player name
|
23 |
def get_matching_player(name_from_file, player_list):
|
24 |
name_lower = name_from_file.lower()
|
25 |
for player in player_list:
|
|
|
27 |
return player
|
28 |
return None
|
29 |
|
30 |
+
# Horizontal bar chart
|
31 |
def plot_horizontal_bar(df, player1, player2):
|
32 |
st.subheader("π Stat Comparison - Horizontal Bar Chart")
|
33 |
num_cols = df.select_dtypes(include='number').columns
|
34 |
df_num = df[num_cols].T
|
35 |
df_num.columns = [player1, player2]
|
36 |
+
df_num = df_num.sort_values(by=player1, ascending=False).head(15)
|
37 |
|
38 |
fig, ax = plt.subplots(figsize=(10, 7))
|
39 |
df_num.plot(kind='barh', ax=ax)
|
|
|
43 |
ax.legend(loc="lower right")
|
44 |
st.pyplot(fig)
|
45 |
|
46 |
+
# Pie chart comparison
|
47 |
+
def plot_pie_charts(player1_data, player2_data, player1, player2):
|
48 |
+
st.subheader("π₯§ Batting vs Bowling Contribution")
|
49 |
+
col1, col2 = st.columns(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
for col, player_data, player_name in zip([col1, col2], [player1_data, player2_data], [player1, player2]):
|
52 |
+
batting_total = player_data.get('Runs_ODI', 0) + player_data.get('Runs_T20', 0) + player_data.get('Runs_Test', 0)
|
53 |
+
bowling_total = player_data.get('Wickets_ODI', 0) + player_data.get('Wickets_T20', 0) + player_data.get('Wickets_Test', 0)
|
54 |
+
|
55 |
+
labels = ['Batting', 'Bowling']
|
56 |
+
sizes = [batting_total, bowling_total]
|
57 |
+
|
58 |
+
fig, ax = plt.subplots()
|
59 |
+
ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90, colors=['#4CAF50', '#2196F3'])
|
60 |
+
ax.axis('equal')
|
61 |
+
col.pyplot(fig)
|
62 |
+
col.caption(f"{player_name}'s Batting vs Bowling")
|
63 |
+
|
64 |
+
# Bowling metric bar chart
|
65 |
+
def plot_bowling_comparison(df, player1, player2):
|
66 |
+
st.subheader("π― Bowling Metrics Comparison")
|
67 |
+
|
68 |
+
bowling_cols = [col for col in df.columns if 'Wickets' in col or 'Economy' in col or 'Bowling_Average' in col]
|
69 |
+
df_bowling = df[bowling_cols].T
|
70 |
+
df_bowling.columns = [player1, player2]
|
71 |
+
df_bowling = df_bowling.dropna().sort_values(by=player1, ascending=False)
|
72 |
+
|
73 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
74 |
+
df_bowling.plot(kind='bar', ax=ax)
|
75 |
+
ax.set_title("Bowling Stats")
|
76 |
+
ax.set_ylabel("Value")
|
77 |
+
ax.set_xticklabels(df_bowling.index, rotation=45, ha='right')
|
78 |
+
ax.legend(loc="upper right")
|
79 |
st.pyplot(fig)
|
80 |
|
81 |
+
# Main app
|
82 |
def main():
|
83 |
st.set_page_config(layout="wide")
|
84 |
st.title("Cricket Player Comparison Tool π")
|
|
|
113 |
with col4:
|
114 |
st.image(img2, caption=player2_name, use_container_width=True)
|
115 |
|
116 |
+
# Display comparison table
|
117 |
comparison_df = pd.DataFrame([player1_data, player2_data])
|
118 |
comparison_df.set_index('Player', inplace=True)
|
119 |
+
st.subheader("π Full Stats Table")
|
|
|
120 |
st.dataframe(comparison_df.T)
|
121 |
|
122 |
+
# Visualizations
|
123 |
plot_horizontal_bar(comparison_df, player1_name, player2_name)
|
124 |
+
plot_pie_charts(player1_data, player2_data, player1_name, player2_name)
|
125 |
+
plot_bowling_comparison(comparison_df, player1_name, player2_name)
|
126 |
|
127 |
else:
|
128 |
+
st.error("β Player names from image files don't match or are the same. Please check file names.")
|
129 |
else:
|
130 |
+
st.info("πΈ Please upload two player images to continue.")
|
131 |
|
132 |
if __name__ == "__main__":
|
133 |
main()
|