Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import plotly.express as px
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Load datasets
|
6 |
+
file_paths = {
|
7 |
+
"Training Session 1": "./ZED 2005 Session 1-12-2024.csv",
|
8 |
+
"Training Session 2": "./ZED 2005 Session 2-12-2024.csv",
|
9 |
+
"Training Session 6": "./ZED 2005 Session 6-12-2024.csv"
|
10 |
+
}
|
11 |
+
|
12 |
+
# Load and clean datasets
|
13 |
+
def load_and_clean_data(file_path):
|
14 |
+
df = pd.read_csv(file_path)
|
15 |
+
|
16 |
+
# Converting relevant columns to numeric
|
17 |
+
for col in ['Played Time (min)', 'Top Speed (km/h)', 'Dist. Covered (m)', 'Dribbling Count (#)',
|
18 |
+
'Kick Power (km/h)', 'Session Intensity', 'High Intensity Run (#)']:
|
19 |
+
if col in df.columns:
|
20 |
+
df[col] = pd.to_numeric(df[col], errors='coerce')
|
21 |
+
|
22 |
+
# Dropping rows with missing values in key metrics
|
23 |
+
df = df.dropna(subset=['Played Time (min)', 'Top Speed (km/h)', 'Dist. Covered (m)',
|
24 |
+
'Session Intensity', 'High Intensity Run (#)'])
|
25 |
+
return df
|
26 |
+
|
27 |
+
dataframes = {session: load_and_clean_data(path) for session, path in file_paths.items()}
|
28 |
+
|
29 |
+
# Generate interactive visualizations
|
30 |
+
def generate_visualizations(session, metric):
|
31 |
+
df = dataframes[session]
|
32 |
+
|
33 |
+
if metric == "Played Time Distribution":
|
34 |
+
fig = px.histogram(df, x="Played Time (min)", nbins=20, title="Played Time Distribution",
|
35 |
+
labels={"Played Time (min)": "Played Time (min)"}, color_discrete_sequence=["skyblue"])
|
36 |
+
elif metric == "Top Speed Boxplot":
|
37 |
+
fig = px.box(df, y="Top Speed (km/h)", title="Top Speed of Players",
|
38 |
+
labels={"Top Speed (km/h)": "Top Speed (km/h)"}, color_discrete_sequence=["lightgreen"])
|
39 |
+
elif metric == "Distance Covered vs Played Time":
|
40 |
+
fig = px.scatter(df, x="Played Time (min)", y="Dist. Covered (m)", color="Main Possition",
|
41 |
+
title="Distance Covered vs Played Time",
|
42 |
+
labels={"Played Time (min)": "Played Time (min)", "Dist. Covered (m)": "Distance Covered (m)"},
|
43 |
+
color_discrete_sequence=px.colors.qualitative.Vivid)
|
44 |
+
elif metric == "Dribbling Contribution by Position":
|
45 |
+
fig = px.bar(df, x="Main Possition", y="Dribbling Count (#)", title="Dribbling Contribution by Position",
|
46 |
+
labels={"Main Possition": "Position", "Dribbling Count (#)": "Dribbling Count (#)"},
|
47 |
+
color="Main Possition", color_discrete_sequence=px.colors.qualitative.Vivid)
|
48 |
+
elif metric == "Kick Power Distribution":
|
49 |
+
fig = px.histogram(df, x="Kick Power (km/h)", nbins=10, title="Kick Power Distribution",
|
50 |
+
labels={"Kick Power (km/h)": "Kick Power (km/h)"}, color_discrete_sequence=["orange"])
|
51 |
+
elif metric == "Session Intensity by Position":
|
52 |
+
fig = px.box(df, x="Main Possition", y="Session Intensity", title="Session Intensity by Position",
|
53 |
+
labels={"Main Possition": "Position", "Session Intensity": "Session Intensity"},
|
54 |
+
color="Main Possition", color_discrete_sequence=px.colors.qualitative.Vivid)
|
55 |
+
elif metric == "High-Intensity Runs":
|
56 |
+
fig = px.scatter(df, x="High Intensity Run (#)", y="High Intensity Run (m)", color="Main Possition",
|
57 |
+
title="High-Intensity Runs: Distance vs Frequency",
|
58 |
+
labels={"High Intensity Run (#)": "High Intensity Runs (#)",
|
59 |
+
"High Intensity Run (m)": "High Intensity Distance (m)"},
|
60 |
+
size="Session Intensity", color_discrete_sequence=px.colors.qualitative.Vivid)
|
61 |
+
else:
|
62 |
+
fig = px.scatter(df, x="Max Acceleration (m/s\u00b2)", y="Max Deceleration (m/s\u00b2)", color="Main Possition",
|
63 |
+
title="Max Intensity: Acceleration vs Deceleration",
|
64 |
+
labels={"Max Acceleration (m/s\u00b2)": "Max Acceleration (m/s\u00b2)",
|
65 |
+
"Max Deceleration (m/s\u00b2)": "Max Deceleration (m/s\u00b2)"},
|
66 |
+
color_discrete_sequence=px.colors.qualitative.Vivid)
|
67 |
+
return fig
|
68 |
+
|
69 |
+
# Gradio Interface
|
70 |
+
def visualize_data(session, metric):
|
71 |
+
fig = generate_visualizations(session, metric)
|
72 |
+
return fig
|
73 |
+
|
74 |
+
sessions = list(file_paths.keys())
|
75 |
+
metrics = ["Played Time Distribution", "Top Speed Boxplot", "Distance Covered vs Played Time",
|
76 |
+
"Dribbling Contribution by Position", "Kick Power Distribution", "Session Intensity by Position",
|
77 |
+
"High-Intensity Runs", "Max Intensity: Acceleration vs Deceleration"]
|
78 |
+
|
79 |
+
gr.Interface(
|
80 |
+
fn=visualize_data,
|
81 |
+
inputs=[gr.Dropdown(choices=sessions, label="Select Session"),
|
82 |
+
gr.Dropdown(choices=metrics, label="Select Metric")],
|
83 |
+
outputs=gr.Plot(label="Visualization"),
|
84 |
+
title="Football Analytics: Interactive Visualizations"
|
85 |
+
).launch()
|