Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,19 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
| 4 |
-
import seaborn as sns
|
| 5 |
import plotly.express as px
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
from plotly.subplots import make_subplots
|
| 8 |
-
from
|
| 9 |
-
import numpy as np
|
| 10 |
|
| 11 |
# --------------------------
|
| 12 |
-
# 1.
|
| 13 |
# --------------------------
|
| 14 |
st.set_page_config(
|
| 15 |
page_title="🏏 Advanced Cricket Analytics",
|
| 16 |
page_icon="🏏",
|
| 17 |
layout="wide",
|
| 18 |
-
initial_sidebar_state="expanded"
|
| 19 |
-
menu_items={
|
| 20 |
-
'Get Help': 'https://github.com/yourusername/cricket-analytics',
|
| 21 |
-
'Report a bug': "https://github.com/yourusername/cricket-analytics/issues",
|
| 22 |
-
'About': """
|
| 23 |
-
## 🏏 Advanced Cricket Analytics Dashboard
|
| 24 |
-
Comprehensive player performance analysis, team comparisons, and predictive modeling.
|
| 25 |
-
"""
|
| 26 |
-
}
|
| 27 |
)
|
| 28 |
|
| 29 |
# --------------------------
|
|
@@ -35,231 +25,314 @@ def load_data():
|
|
| 35 |
|
| 36 |
# Create derived metrics
|
| 37 |
for fmt in ['Test', 'ODI', 'T20', 'IPL']:
|
|
|
|
| 38 |
df[f'batting_Boundary%_{fmt}'] = (
|
| 39 |
(df[f'batting_Fours_{fmt}'] * 4 + df[f'batting_Sixes_{fmt}'] * 6) /
|
| 40 |
df[f'batting_Runs_{fmt}'].replace(0, 1)
|
| 41 |
) * 100
|
| 42 |
|
|
|
|
| 43 |
df[f'bowling_Dots%_{fmt}'] = (
|
| 44 |
(df[f'bowling_{fmt}_Maidens'] * 6) /
|
| 45 |
df[f'bowling_{fmt}_Balls'].replace(0, 1)
|
| 46 |
) * 100
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
return df
|
| 49 |
|
| 50 |
df = load_data()
|
| 51 |
|
| 52 |
# --------------------------
|
| 53 |
-
# 3.
|
| 54 |
# --------------------------
|
| 55 |
-
def
|
| 56 |
"""Radar chart showing player's performance across formats"""
|
| 57 |
-
metrics = ['
|
| 58 |
formats = ['Test', 'ODI', 'T20', 'IPL']
|
| 59 |
|
| 60 |
fig = go.Figure()
|
| 61 |
|
| 62 |
for fmt in formats:
|
| 63 |
values = [
|
| 64 |
-
df.loc[df['Player'] == player, f'Matches_{fmt}'].values[0],
|
| 65 |
df.loc[df['Player'] == player, f'batting_Runs_{fmt}'].values[0],
|
| 66 |
df.loc[df['Player'] == player, f'batting_Average_{fmt}'].values[0],
|
| 67 |
df.loc[df['Player'] == player, f'batting_SR_{fmt}'].values[0],
|
| 68 |
-
df.loc[df['Player'] == player, f'bowling_{fmt}_Wickets'].values[0]
|
|
|
|
| 69 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
fig.add_trace(go.Scatterpolar(
|
| 71 |
-
r=
|
| 72 |
theta=metrics,
|
| 73 |
fill='toself',
|
| 74 |
-
name=fmt
|
|
|
|
| 75 |
))
|
| 76 |
|
| 77 |
fig.update_layout(
|
| 78 |
-
polar=dict(radialaxis=dict(visible=True)),
|
| 79 |
-
title=f"{player}'s Performance Across Formats",
|
| 80 |
-
height=600
|
|
|
|
| 81 |
)
|
| 82 |
-
|
| 83 |
|
| 84 |
-
def
|
| 85 |
-
"""
|
| 86 |
-
fig = make_subplots(specs=[[{
|
| 87 |
|
| 88 |
-
# Batting
|
| 89 |
-
for i, player in enumerate(
|
| 90 |
fig.add_trace(go.Bar(
|
| 91 |
-
x=['Runs', 'Average', '
|
| 92 |
y=[
|
| 93 |
df.loc[df['Player'] == player, f'batting_Runs_{format_type}'].values[0],
|
| 94 |
df.loc[df['Player'] == player, f'batting_Average_{format_type}'].values[0],
|
| 95 |
df.loc[df['Player'] == player, f'batting_SR_{format_type}'].values[0],
|
| 96 |
df.loc[df['Player'] == player, f'batting_100s_{format_type}'].values[0]
|
| 97 |
],
|
| 98 |
-
name=
|
| 99 |
-
marker_color=[
|
| 100 |
opacity=0.7
|
| 101 |
-
),
|
| 102 |
|
| 103 |
-
# Bowling
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
],
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
fig.update_layout(
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
)
|
| 123 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 124 |
-
|
| 125 |
-
def plot_team_strength_analysis(team):
|
| 126 |
-
"""Sunburst chart showing team composition"""
|
| 127 |
-
team_df = df[df['Player_Team'] == team]
|
| 128 |
-
|
| 129 |
-
# Classify players
|
| 130 |
-
team_df['Player_Type'] = np.where(
|
| 131 |
-
(team_df[f'batting_Runs_Test'] > 1000) & (team_df[f'bowling_Test_Wickets'] > 50),
|
| 132 |
-
'All-Rounder',
|
| 133 |
-
np.where(
|
| 134 |
-
team_df[f'batting_Runs_Test'] > 1000,
|
| 135 |
-
'Batsman',
|
| 136 |
-
'Bowler'
|
| 137 |
-
)
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
fig = px.sunburst(
|
| 141 |
-
team_df,
|
| 142 |
-
path=['Player_Type', 'Player'],
|
| 143 |
-
values=f'batting_Runs_Test',
|
| 144 |
-
color=f'batting_Average_Test',
|
| 145 |
-
title=f"{team} Team Composition"
|
| 146 |
)
|
| 147 |
-
|
| 148 |
|
| 149 |
-
def
|
| 150 |
-
"""
|
| 151 |
runs = df.loc[df['Player'] == player, f'batting_Runs_{format_type}'].values[0]
|
| 152 |
fours = df.loc[df['Player'] == player, f'batting_Fours_{format_type}'].values[0]
|
| 153 |
sixes = df.loc[df['Player'] == player, f'batting_Sixes_{format_type}'].values[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
| 156 |
|
| 157 |
-
|
|
|
|
| 158 |
labels=['Fours', 'Sixes', 'Other Runs'],
|
| 159 |
-
values=[fours*4, sixes*6,
|
| 160 |
hole=0.4,
|
| 161 |
marker_colors=['#1f77b4', '#ff7f0e', '#2ca02c'],
|
| 162 |
-
|
| 163 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
fig.update_layout(
|
| 166 |
-
|
| 167 |
-
height=
|
| 168 |
)
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
# --------------------------
|
| 172 |
-
# 4.
|
| 173 |
# --------------------------
|
| 174 |
-
def
|
| 175 |
-
st.header("🌟
|
| 176 |
|
| 177 |
col1, col2 = st.columns([1, 3])
|
| 178 |
with col1:
|
| 179 |
player = st.selectbox("Select Player", df['Player'].unique())
|
| 180 |
format_type = st.selectbox("Select Format", ["Test", "ODI", "T20", "IPL"])
|
| 181 |
analysis_type = st.radio("Analysis Type",
|
| 182 |
-
|
| 183 |
|
| 184 |
with col2:
|
| 185 |
-
if analysis_type == "
|
| 186 |
-
|
| 187 |
-
elif analysis_type == "Batting Breakdown":
|
| 188 |
-
plot_boundary_analysis(player, format_type)
|
| 189 |
|
| 190 |
-
#
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
df.loc[df['Player'] == player, f'batting_Ducks_{format_type}'].values[0]
|
| 198 |
-
],
|
| 199 |
-
title=f"{player}'s Milestones in {format_type}"
|
| 200 |
-
)
|
| 201 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 202 |
-
else:
|
| 203 |
-
# Bowling analysis
|
| 204 |
-
fig = go.Figure()
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
value=df.loc[df['Player'] == player, f'bowling_{format_type}_Wickets'].values[0],
|
| 209 |
-
title={'text': "Total Wickets"},
|
| 210 |
-
domain={'x': [0.1, 0.3], 'y': [0.7, 0.9]},
|
| 211 |
-
gauge={'axis': {'range': [0, 500]}}
|
| 212 |
-
))
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
))
|
| 229 |
|
| 230 |
-
fig.update_layout(
|
|
|
|
|
|
|
|
|
|
| 231 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
-
def
|
| 234 |
st.header("🔥 Head-to-Head Player Comparison")
|
| 235 |
|
| 236 |
col1, col2 = st.columns(2)
|
| 237 |
with col1:
|
| 238 |
player1 = st.selectbox("Player 1", df['Player'].unique(), key='p1')
|
| 239 |
-
show_batting = st.checkbox("Show Batting Stats", True, key='b1')
|
| 240 |
with col2:
|
| 241 |
player2 = st.selectbox("Player 2", df['Player'].unique(), index=1, key='p2')
|
| 242 |
-
show_bowling = st.checkbox("Show Bowling Stats", True, key='b2')
|
| 243 |
|
| 244 |
-
format_type = st.selectbox("Compare in Format", ["Test", "ODI", "T20", "IPL"]
|
| 245 |
|
| 246 |
-
|
| 247 |
-
|
| 248 |
|
| 249 |
-
#
|
| 250 |
-
st.subheader("Detailed Comparison
|
| 251 |
|
| 252 |
comparison_data = []
|
| 253 |
for metric, label in [
|
| 254 |
-
('Matches', 'Matches
|
| 255 |
-
('batting_Runs', 'Runs
|
| 256 |
-
('batting_Average', '
|
| 257 |
('batting_SR', 'Strike Rate'),
|
| 258 |
-
('
|
| 259 |
-
('
|
| 260 |
-
('
|
|
|
|
| 261 |
]:
|
| 262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
p1_val = df.loc[df['Player'] == player1, key].values[0]
|
| 264 |
p2_val = df.loc[df['Player'] == player2, key].values[0]
|
| 265 |
comparison_data.append([label, p1_val, p2_val])
|
|
@@ -267,49 +340,81 @@ def enhanced_player_comparison():
|
|
| 267 |
comp_df = pd.DataFrame(comparison_data, columns=['Metric', player1, player2])
|
| 268 |
st.dataframe(comp_df.set_index('Metric'), use_container_width=True)
|
| 269 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
# --------------------------
|
| 271 |
# 5. MAIN APP FUNCTION
|
| 272 |
# --------------------------
|
| 273 |
def main():
|
| 274 |
st.title("🏏 Advanced Cricket Analytics Dashboard")
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
.
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
""", unsafe_allow_html=True)
|
| 284 |
-
|
| 285 |
-
# Sidebar with enhanced options
|
| 286 |
-
with st.sidebar:
|
| 287 |
-
st.header("Navigation")
|
| 288 |
-
app_mode = st.selectbox("Choose Analysis Mode", [
|
| 289 |
-
"🌟 Player Analysis",
|
| 290 |
-
"🔥 Player Comparison",
|
| 291 |
-
"🏆 Team Analysis",
|
| 292 |
-
"📈 Trend Analysis",
|
| 293 |
-
"🔮 Predictive Models"
|
| 294 |
-
])
|
| 295 |
-
|
| 296 |
-
st.header("Settings")
|
| 297 |
-
viz_style = st.selectbox("Visualization Style", ["Plotly", "Seaborn", "Matplotlib"])
|
| 298 |
-
st.checkbox("Show Advanced Metrics", True)
|
| 299 |
-
st.checkbox("Enable Predictive Features", True)
|
| 300 |
|
| 301 |
# Page routing
|
| 302 |
if app_mode == "🌟 Player Analysis":
|
| 303 |
-
|
| 304 |
elif app_mode == "🔥 Player Comparison":
|
| 305 |
-
|
| 306 |
elif app_mode == "🏆 Team Analysis":
|
| 307 |
-
|
| 308 |
-
plot_team_strength_analysis(team)
|
| 309 |
-
elif app_mode == "📈 Trend Analysis":
|
| 310 |
-
st.warning("Trend Analysis section under development")
|
| 311 |
-
elif app_mode == "🔮 Predictive Models":
|
| 312 |
-
st.warning("Predictive Models section under development")
|
| 313 |
|
| 314 |
if __name__ == "__main__":
|
| 315 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import numpy as np
|
|
|
|
| 4 |
import plotly.express as px
|
| 5 |
import plotly.graph_objects as go
|
| 6 |
from plotly.subplots import make_subplots
|
| 7 |
+
from sklearn.preprocessing import MinMaxScaler
|
|
|
|
| 8 |
|
| 9 |
# --------------------------
|
| 10 |
+
# 1. PAGE CONFIGURATION
|
| 11 |
# --------------------------
|
| 12 |
st.set_page_config(
|
| 13 |
page_title="🏏 Advanced Cricket Analytics",
|
| 14 |
page_icon="🏏",
|
| 15 |
layout="wide",
|
| 16 |
+
initial_sidebar_state="expanded"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
# --------------------------
|
|
|
|
| 25 |
|
| 26 |
# Create derived metrics
|
| 27 |
for fmt in ['Test', 'ODI', 'T20', 'IPL']:
|
| 28 |
+
# Batting metrics
|
| 29 |
df[f'batting_Boundary%_{fmt}'] = (
|
| 30 |
(df[f'batting_Fours_{fmt}'] * 4 + df[f'batting_Sixes_{fmt}'] * 6) /
|
| 31 |
df[f'batting_Runs_{fmt}'].replace(0, 1)
|
| 32 |
) * 100
|
| 33 |
|
| 34 |
+
# Bowling metrics
|
| 35 |
df[f'bowling_Dots%_{fmt}'] = (
|
| 36 |
(df[f'bowling_{fmt}_Maidens'] * 6) /
|
| 37 |
df[f'bowling_{fmt}_Balls'].replace(0, 1)
|
| 38 |
) * 100
|
| 39 |
|
| 40 |
+
# Player type classification
|
| 41 |
+
df[f'Player_Type_{fmt}'] = np.where(
|
| 42 |
+
(df[f'batting_Runs_{fmt}'] > 1000) & (df[f'bowling_{fmt}_Wickets'] > 50),
|
| 43 |
+
'All-Rounder',
|
| 44 |
+
np.where(
|
| 45 |
+
df[f'batting_Runs_{fmt}'] > 1000,
|
| 46 |
+
'Batsman',
|
| 47 |
+
np.where(
|
| 48 |
+
df[f'bowling_{fmt}_Wickets'] > 50,
|
| 49 |
+
'Bowler',
|
| 50 |
+
'Other'
|
| 51 |
+
)
|
| 52 |
+
)
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
return df
|
| 56 |
|
| 57 |
df = load_data()
|
| 58 |
|
| 59 |
# --------------------------
|
| 60 |
+
# 3. VISUALIZATION FUNCTIONS
|
| 61 |
# --------------------------
|
| 62 |
+
def plot_player_radar(player):
|
| 63 |
"""Radar chart showing player's performance across formats"""
|
| 64 |
+
metrics = ['Runs', 'Average', 'Strike Rate', 'Wickets', 'Economy']
|
| 65 |
formats = ['Test', 'ODI', 'T20', 'IPL']
|
| 66 |
|
| 67 |
fig = go.Figure()
|
| 68 |
|
| 69 |
for fmt in formats:
|
| 70 |
values = [
|
|
|
|
| 71 |
df.loc[df['Player'] == player, f'batting_Runs_{fmt}'].values[0],
|
| 72 |
df.loc[df['Player'] == player, f'batting_Average_{fmt}'].values[0],
|
| 73 |
df.loc[df['Player'] == player, f'batting_SR_{fmt}'].values[0],
|
| 74 |
+
df.loc[df['Player'] == player, f'bowling_{fmt}_Wickets'].values[0],
|
| 75 |
+
df.loc[df['Player'] == player, f'bowling_{fmt}_Eco'].values[0]
|
| 76 |
]
|
| 77 |
+
|
| 78 |
+
# Normalize values for radar chart
|
| 79 |
+
scaler = MinMaxScaler()
|
| 80 |
+
values_normalized = scaler.fit_transform(np.array(values).reshape(-1, 1)).flatten()
|
| 81 |
+
|
| 82 |
fig.add_trace(go.Scatterpolar(
|
| 83 |
+
r=values_normalized,
|
| 84 |
theta=metrics,
|
| 85 |
fill='toself',
|
| 86 |
+
name=fmt,
|
| 87 |
+
hovertext=[f"{metrics[i]}: {values[i]}" for i in range(len(metrics))]
|
| 88 |
))
|
| 89 |
|
| 90 |
fig.update_layout(
|
| 91 |
+
polar=dict(radialaxis=dict(visible=True, range=[0, 1])),
|
| 92 |
+
title=f"{player}'s Performance Across Formats (Normalized)",
|
| 93 |
+
height=600,
|
| 94 |
+
hovermode="closest"
|
| 95 |
)
|
| 96 |
+
return fig
|
| 97 |
|
| 98 |
+
def plot_format_comparison(players, format_type):
|
| 99 |
+
"""Compare multiple players in a specific format"""
|
| 100 |
+
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'xy'}, {'type':'polar'}]])
|
| 101 |
|
| 102 |
+
# Batting comparison (bar chart)
|
| 103 |
+
for i, player in enumerate(players):
|
| 104 |
fig.add_trace(go.Bar(
|
| 105 |
+
x=['Runs', 'Average', 'SR', '100s'],
|
| 106 |
y=[
|
| 107 |
df.loc[df['Player'] == player, f'batting_Runs_{format_type}'].values[0],
|
| 108 |
df.loc[df['Player'] == player, f'batting_Average_{format_type}'].values[0],
|
| 109 |
df.loc[df['Player'] == player, f'batting_SR_{format_type}'].values[0],
|
| 110 |
df.loc[df['Player'] == player, f'batting_100s_{format_type}'].values[0]
|
| 111 |
],
|
| 112 |
+
name=player,
|
| 113 |
+
marker_color=px.colors.qualitative.Plotly[i],
|
| 114 |
opacity=0.7
|
| 115 |
+
), row=1, col=1)
|
| 116 |
|
| 117 |
+
# Bowling comparison (radar chart)
|
| 118 |
+
bowling_metrics = ['Wickets', 'Avg', 'Eco', 'SR', '4w', '5w']
|
| 119 |
+
for i, player in enumerate(players):
|
| 120 |
+
values = [
|
| 121 |
+
df.loc[df['Player'] == player, f'bowling_{format_type}_Wickets'].values[0],
|
| 122 |
+
df.loc[df['Player'] == player, f'bowling_{format_type}_Avg'].values[0],
|
| 123 |
+
df.loc[df['Player'] == player, f'bowling_{format_type}_Eco'].values[0],
|
| 124 |
+
df.loc[df['Player'] == player, f'bowling_{format_type}_SR'].values[0],
|
| 125 |
+
df.loc[df['Player'] == player, f'bowling_{format_type}_4w'].values[0],
|
| 126 |
+
df.loc[df['Player'] == player, f'bowling_{format_type}_5w'].values[0]
|
| 127 |
+
]
|
| 128 |
+
|
| 129 |
+
fig.add_trace(go.Scatterpolar(
|
| 130 |
+
r=values,
|
| 131 |
+
theta=bowling_metrics,
|
| 132 |
+
fill='toself',
|
| 133 |
+
name=player,
|
| 134 |
+
line_color=px.colors.qualitative.Plotly[i],
|
| 135 |
+
opacity=0.7
|
| 136 |
+
), row=1, col=2)
|
| 137 |
|
| 138 |
fig.update_layout(
|
| 139 |
+
title_text=f"Player Comparison - {format_type} Format",
|
| 140 |
+
height=500,
|
| 141 |
+
showlegend=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
)
|
| 143 |
+
return fig
|
| 144 |
|
| 145 |
+
def plot_run_breakdown(player, format_type):
|
| 146 |
+
"""Detailed batting composition visualization"""
|
| 147 |
runs = df.loc[df['Player'] == player, f'batting_Runs_{format_type}'].values[0]
|
| 148 |
fours = df.loc[df['Player'] == player, f'batting_Fours_{format_type}'].values[0]
|
| 149 |
sixes = df.loc[df['Player'] == player, f'batting_Sixes_{format_type}'].values[0]
|
| 150 |
+
dots = df.loc[df['Player'] == player, f'batting_Balls_{format_type}'].values[0] - (
|
| 151 |
+
df.loc[df['Player'] == player, f'batting_Fours_{format_type}'].values[0] +
|
| 152 |
+
df.loc[df['Player'] == player, f'batting_Sixes_{format_type}'].values[0]
|
| 153 |
+
)
|
| 154 |
|
| 155 |
+
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'pie'}, {'type':'bar'}]])
|
| 156 |
|
| 157 |
+
# Pie chart - run composition
|
| 158 |
+
fig.add_trace(go.Pie(
|
| 159 |
labels=['Fours', 'Sixes', 'Other Runs'],
|
| 160 |
+
values=[fours*4, sixes*6, runs - (fours*4 + sixes*6)],
|
| 161 |
hole=0.4,
|
| 162 |
marker_colors=['#1f77b4', '#ff7f0e', '#2ca02c'],
|
| 163 |
+
name="Run Composition"
|
| 164 |
+
), row=1, col=1)
|
| 165 |
+
|
| 166 |
+
# Bar chart - scoring distribution
|
| 167 |
+
fig.add_trace(go.Bar(
|
| 168 |
+
x=['Dots', '1s', '2s', '3s', '4s', '6s'],
|
| 169 |
+
y=[dots, runs/4, runs/8, runs/16, fours, sixes], # Simplified distribution
|
| 170 |
+
marker_color=['#636efa', '#ef553b', '#00cc96', '#ab63fa', '#19d3f3', '#ff6692'],
|
| 171 |
+
name="Scoring Distribution"
|
| 172 |
+
), row=1, col=2)
|
| 173 |
|
| 174 |
fig.update_layout(
|
| 175 |
+
title_text=f"{player}'s Batting Breakdown - {format_type}",
|
| 176 |
+
height=400
|
| 177 |
)
|
| 178 |
+
return fig
|
| 179 |
+
|
| 180 |
+
def plot_team_composition(team):
|
| 181 |
+
"""Visualize team's player types and strengths"""
|
| 182 |
+
team_df = df[df['Player_Team'] == team]
|
| 183 |
+
|
| 184 |
+
# Create a sunburst chart
|
| 185 |
+
fig1 = px.sunburst(
|
| 186 |
+
team_df,
|
| 187 |
+
path=['Player_Type_Test', 'Player'],
|
| 188 |
+
values='batting_Runs_Test',
|
| 189 |
+
color='batting_Average_Test',
|
| 190 |
+
title=f"{team} Player Composition"
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
# Create a parallel categories diagram
|
| 194 |
+
fig2 = px.parallel_categories(
|
| 195 |
+
team_df,
|
| 196 |
+
dimensions=['Player_Type_Test', 'Player_Type_ODI', 'Player_Type_T20'],
|
| 197 |
+
color=team_df['batting_Runs_Test'],
|
| 198 |
+
title="Player Role Across Formats"
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
return fig1, fig2
|
| 202 |
+
|
| 203 |
+
def plot_career_timeline(player):
|
| 204 |
+
"""Simulated career timeline (would need actual time-series data)"""
|
| 205 |
+
formats = ['Test', 'ODI', 'T20', 'IPL']
|
| 206 |
+
milestones = []
|
| 207 |
+
|
| 208 |
+
for fmt in formats:
|
| 209 |
+
milestones.append({
|
| 210 |
+
'Format': fmt,
|
| 211 |
+
'Debut': 2005 + np.random.randint(0, 5),
|
| 212 |
+
'Matches': df.loc[df['Player'] == player, f'Matches_{fmt}'].values[0],
|
| 213 |
+
'Runs': df.loc[df['Player'] == player, f'batting_Runs_{fmt}'].values[0],
|
| 214 |
+
'Wickets': df.loc[df['Player'] == player, f'bowling_{fmt}_Wickets'].values[0]
|
| 215 |
+
})
|
| 216 |
+
|
| 217 |
+
timeline_df = pd.DataFrame(milestones)
|
| 218 |
+
|
| 219 |
+
fig = px.scatter(
|
| 220 |
+
timeline_df,
|
| 221 |
+
x='Debut',
|
| 222 |
+
y='Format',
|
| 223 |
+
size='Matches',
|
| 224 |
+
color='Runs',
|
| 225 |
+
hover_name='Format',
|
| 226 |
+
hover_data=['Runs', 'Wickets'],
|
| 227 |
+
title=f"{player}'s Career Timeline"
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
fig.update_traces(
|
| 231 |
+
marker=dict(line=dict(width=2, color='DarkSlateGrey')),
|
| 232 |
+
selector=dict(mode='markers')
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
return fig
|
| 236 |
|
| 237 |
# --------------------------
|
| 238 |
+
# 4. PAGE COMPONENTS
|
| 239 |
# --------------------------
|
| 240 |
+
def player_analysis():
|
| 241 |
+
st.header("🌟 Player Performance Analysis")
|
| 242 |
|
| 243 |
col1, col2 = st.columns([1, 3])
|
| 244 |
with col1:
|
| 245 |
player = st.selectbox("Select Player", df['Player'].unique())
|
| 246 |
format_type = st.selectbox("Select Format", ["Test", "ODI", "T20", "IPL"])
|
| 247 |
analysis_type = st.radio("Analysis Type",
|
| 248 |
+
["Overview", "Batting", "Bowling", "Career"])
|
| 249 |
|
| 250 |
with col2:
|
| 251 |
+
if analysis_type == "Overview":
|
| 252 |
+
st.plotly_chart(plot_player_radar(player), use_container_width=True)
|
|
|
|
|
|
|
| 253 |
|
| 254 |
+
# Key metrics cards
|
| 255 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 256 |
+
col1.metric("Matches", df.loc[df['Player'] == player, f'Matches_{format_type}'].values[0])
|
| 257 |
+
col2.metric("Runs", df.loc[df['Player'] == player, f'batting_Runs_{format_type}'].values[0])
|
| 258 |
+
col3.metric("Wickets", df.loc[df['Player'] == player, f'bowling_{format_type}_Wickets'].values[0])
|
| 259 |
+
col4.metric("Player Type",
|
| 260 |
+
df.loc[df['Player'] == player, f'Player_Type_{format_type}'].values[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
+
elif analysis_type == "Batting":
|
| 263 |
+
st.plotly_chart(plot_run_breakdown(player, format_type), use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
+
# Batting milestones
|
| 266 |
+
st.subheader("Batting Milestones")
|
| 267 |
+
col1, col2, col3 = st.columns(3)
|
| 268 |
+
col1.metric("50s", df.loc[df['Player'] == player, f'batting_50s_{format_type}'].values[0])
|
| 269 |
+
col2.metric("100s", df.loc[df['Player'] == player, f'batting_100s_{format_type}'].values[0])
|
| 270 |
+
col3.metric("Highest", df.loc[df['Player'] == player, f'batting_Highest_{format_type}'].values[0])
|
| 271 |
+
|
| 272 |
+
elif analysis_type == "Bowling":
|
| 273 |
+
# Bowling metrics visualization
|
| 274 |
+
fig = go.Figure()
|
| 275 |
|
| 276 |
+
metrics = ['Wickets', 'Avg', 'Eco', 'SR', '4w', '5w']
|
| 277 |
+
values = [
|
| 278 |
+
df.loc[df['Player'] == player, f'bowling_{format_type}_Wickets'].values[0],
|
| 279 |
+
df.loc[df['Player'] == player, f'bowling_{format_type}_Avg'].values[0],
|
| 280 |
+
df.loc[df['Player'] == player, f'bowling_{format_type}_Eco'].values[0],
|
| 281 |
+
df.loc[df['Player'] == player, f'bowling_{format_type}_SR'].values[0],
|
| 282 |
+
df.loc[df['Player'] == player, f'bowling_{format_type}_4w'].values[0],
|
| 283 |
+
df.loc[df['Player'] == player, f'bowling_{format_type}_5w'].values[0]
|
| 284 |
+
]
|
| 285 |
+
|
| 286 |
+
fig.add_trace(go.Bar(
|
| 287 |
+
x=metrics,
|
| 288 |
+
y=values,
|
| 289 |
+
marker_color=px.colors.sequential.Viridis
|
| 290 |
))
|
| 291 |
|
| 292 |
+
fig.update_layout(
|
| 293 |
+
title=f"{player}'s Bowling Metrics - {format_type}",
|
| 294 |
+
height=400
|
| 295 |
+
)
|
| 296 |
st.plotly_chart(fig, use_container_width=True)
|
| 297 |
+
|
| 298 |
+
else: # Career
|
| 299 |
+
st.plotly_chart(plot_career_timeline(player), use_container_width=True)
|
| 300 |
|
| 301 |
+
def player_comparison():
|
| 302 |
st.header("🔥 Head-to-Head Player Comparison")
|
| 303 |
|
| 304 |
col1, col2 = st.columns(2)
|
| 305 |
with col1:
|
| 306 |
player1 = st.selectbox("Player 1", df['Player'].unique(), key='p1')
|
|
|
|
| 307 |
with col2:
|
| 308 |
player2 = st.selectbox("Player 2", df['Player'].unique(), index=1, key='p2')
|
|
|
|
| 309 |
|
| 310 |
+
format_type = st.selectbox("Compare in Format", ["Test", "ODI", "T20", "IPL"])
|
| 311 |
|
| 312 |
+
st.plotly_chart(plot_format_comparison([player1, player2], format_type),
|
| 313 |
+
use_container_width=True)
|
| 314 |
|
| 315 |
+
# Detailed comparison table
|
| 316 |
+
st.subheader("Detailed Comparison")
|
| 317 |
|
| 318 |
comparison_data = []
|
| 319 |
for metric, label in [
|
| 320 |
+
('Matches', 'Matches'),
|
| 321 |
+
('batting_Runs', 'Runs'),
|
| 322 |
+
('batting_Average', 'Bat Avg'),
|
| 323 |
('batting_SR', 'Strike Rate'),
|
| 324 |
+
('batting_100s', '100s'),
|
| 325 |
+
('bowling_Wickets', 'Wickets'),
|
| 326 |
+
('bowling_Avg', 'Bowl Avg'),
|
| 327 |
+
('bowling_Eco', 'Economy')
|
| 328 |
]:
|
| 329 |
+
if metric == 'Matches':
|
| 330 |
+
key = f"{metric}_{format_type}"
|
| 331 |
+
elif metric.startswith('batting'):
|
| 332 |
+
key = f"{metric}_{format_type}"
|
| 333 |
+
else:
|
| 334 |
+
key = f"bowling_{format_type}_{metric.split('_')[1]}"
|
| 335 |
+
|
| 336 |
p1_val = df.loc[df['Player'] == player1, key].values[0]
|
| 337 |
p2_val = df.loc[df['Player'] == player2, key].values[0]
|
| 338 |
comparison_data.append([label, p1_val, p2_val])
|
|
|
|
| 340 |
comp_df = pd.DataFrame(comparison_data, columns=['Metric', player1, player2])
|
| 341 |
st.dataframe(comp_df.set_index('Metric'), use_container_width=True)
|
| 342 |
|
| 343 |
+
def team_analysis():
|
| 344 |
+
st.header("🏆 Team Composition Analysis")
|
| 345 |
+
|
| 346 |
+
team = st.selectbox("Select Team", df['Player_Team'].unique())
|
| 347 |
+
|
| 348 |
+
fig1, fig2 = plot_team_composition(team)
|
| 349 |
+
st.plotly_chart(fig1, use_container_width=True)
|
| 350 |
+
st.plotly_chart(fig2, use_container_width=True)
|
| 351 |
+
|
| 352 |
+
# Team strengths visualization
|
| 353 |
+
st.subheader("Team Strengths by Format")
|
| 354 |
+
|
| 355 |
+
format_type = st.selectbox("Select Format for Analysis", ["Test", "ODI", "T20"])
|
| 356 |
+
|
| 357 |
+
team_df = df[df['Player_Team'] == team]
|
| 358 |
+
|
| 359 |
+
# Batting strength
|
| 360 |
+
batting_strength = team_df[f'batting_Runs_{format_type}'].sum()
|
| 361 |
+
|
| 362 |
+
# Bowling strength
|
| 363 |
+
bowling_strength = team_df[f'bowling_{format_type}_Wickets'].sum()
|
| 364 |
+
|
| 365 |
+
# All-round strength
|
| 366 |
+
allround_strength = team_df[
|
| 367 |
+
(team_df[f'batting_Runs_{format_type}'] > 500) &
|
| 368 |
+
(team_df[f'bowling_{format_type}_Wickets'] > 20)
|
| 369 |
+
].shape[0]
|
| 370 |
+
|
| 371 |
+
fig = go.Figure()
|
| 372 |
+
|
| 373 |
+
fig.add_trace(go.Indicator(
|
| 374 |
+
mode="number",
|
| 375 |
+
value=batting_strength,
|
| 376 |
+
title={"text": "Batting Strength<br><span style='font-size:0.8em'>Total Runs</span>"},
|
| 377 |
+
domain={'x': [0, 0.3], 'y': [0, 1]}
|
| 378 |
+
))
|
| 379 |
+
|
| 380 |
+
fig.add_trace(go.Indicator(
|
| 381 |
+
mode="number",
|
| 382 |
+
value=bowling_strength,
|
| 383 |
+
title={"text": "Bowling Strength<br><span style='font-size:0.8em'>Total Wickets</span>"},
|
| 384 |
+
domain={'x': [0.35, 0.65], 'y': [0, 1]}
|
| 385 |
+
))
|
| 386 |
+
|
| 387 |
+
fig.add_trace(go.Indicator(
|
| 388 |
+
mode="number",
|
| 389 |
+
value=allround_strength,
|
| 390 |
+
title={"text": "All-Rounders<br><span style='font-size:0.8em'>Count</span>"},
|
| 391 |
+
domain={'x': [0.7, 1], 'y': [0, 1]}
|
| 392 |
+
))
|
| 393 |
+
|
| 394 |
+
fig.update_layout(height=200)
|
| 395 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 396 |
+
|
| 397 |
# --------------------------
|
| 398 |
# 5. MAIN APP FUNCTION
|
| 399 |
# --------------------------
|
| 400 |
def main():
|
| 401 |
st.title("🏏 Advanced Cricket Analytics Dashboard")
|
| 402 |
+
|
| 403 |
+
# Sidebar navigation
|
| 404 |
+
st.sidebar.header("Navigation")
|
| 405 |
+
app_mode = st.sidebar.selectbox("Choose Analysis Mode", [
|
| 406 |
+
"🌟 Player Analysis",
|
| 407 |
+
"🔥 Player Comparison",
|
| 408 |
+
"🏆 Team Analysis"
|
| 409 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
|
| 411 |
# Page routing
|
| 412 |
if app_mode == "🌟 Player Analysis":
|
| 413 |
+
player_analysis()
|
| 414 |
elif app_mode == "🔥 Player Comparison":
|
| 415 |
+
player_comparison()
|
| 416 |
elif app_mode == "🏆 Team Analysis":
|
| 417 |
+
team_analysis()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
|
| 419 |
if __name__ == "__main__":
|
| 420 |
main()
|