Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,105 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
|
|
|
|
3 |
from datetime import date, timedelta
|
4 |
import random
|
5 |
|
6 |
-
#
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
conference_name = chr(65 + i) # 'A', 'B', 'C', 'D', ...
|
15 |
-
combined_schedule = combine_schedules(conference_name, num_teams, num_inter_games)
|
16 |
-
schedule_with_dates = assign_dates_to_matches_v2(combined_schedule)
|
17 |
-
|
18 |
-
for match in schedule_with_dates:
|
19 |
-
full_schedule.append({
|
20 |
-
"Team 1": match[0],
|
21 |
-
"Team 2": match[1],
|
22 |
-
"Date": match[2]
|
23 |
-
})
|
24 |
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Streamlit App
|
28 |
|
29 |
st.title("Basketball Game Schedule Generator")
|
30 |
|
|
|
|
|
|
|
|
|
31 |
# Configuration UI
|
32 |
st.header("Configuration")
|
33 |
|
@@ -43,16 +115,45 @@ if add_commissioner:
|
|
43 |
|
44 |
# Schedule Generation
|
45 |
if st.button("Generate Schedule"):
|
46 |
-
|
47 |
-
schedule_df = create_schedule(num_teams, num_conferences, num_inter_games)
|
48 |
|
49 |
# Schedule Viewing
|
50 |
st.header("View Schedule")
|
51 |
conference_selector = st.selectbox("Select conference to view schedule:", options=["All"] + [f"Conference {chr(65+i)}" for i in range(num_conferences)])
|
52 |
-
if
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# Export functionality can be added later
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import seaborn as sns
|
6 |
from datetime import date, timedelta
|
7 |
import random
|
8 |
|
9 |
+
# [All the scheduling functions and analytics functions here]
|
10 |
|
11 |
+
|
12 |
+
# Team Workload Analysis
|
13 |
+
def team_workload_analysis(schedule_df):
|
14 |
+
"""Generate a bar chart showing the number of matches each team has per week."""
|
15 |
+
schedule_df['Week'] = schedule_df['Date'].dt.isocalendar().week
|
16 |
+
team_counts = schedule_df.groupby(['Week', 'Team 1']).size().unstack().fillna(0)
|
17 |
+
|
18 |
+
# Plot
|
19 |
+
team_counts.plot(kind='bar', stacked=True, figsize=(15, 7), cmap='Oranges')
|
20 |
+
plt.title('Team Workload Analysis')
|
21 |
+
plt.ylabel('Number of Matches')
|
22 |
+
plt.xlabel('Week Number')
|
23 |
+
plt.tight_layout()
|
24 |
+
plt.legend(title='Teams', bbox_to_anchor=(1.05, 1), loc='upper left')
|
25 |
+
plt.show()
|
26 |
+
|
27 |
+
# Match Distribution
|
28 |
+
def match_distribution(schedule_df):
|
29 |
+
"""Generate a histogram showing match distribution across months."""
|
30 |
+
schedule_df['Month'] = schedule_df['Date'].dt.month_name()
|
31 |
+
month_order = ['November', 'December', 'January', 'February', 'March']
|
32 |
+
|
33 |
+
# Plot
|
34 |
+
plt.figure(figsize=(10, 6))
|
35 |
+
sns.countplot(data=schedule_df, x='Month', order=month_order, palette='Oranges_r')
|
36 |
+
plt.title('Match Distribution Across Months')
|
37 |
+
plt.ylabel('Number of Matches')
|
38 |
+
plt.xlabel('Month')
|
39 |
+
plt.tight_layout()
|
40 |
+
plt.show()
|
41 |
+
|
42 |
+
# Inter-Conference Match Analysis
|
43 |
+
def inter_conference_analysis(schedule_df):
|
44 |
+
"""Generate a heatmap showing inter-conference match frequencies."""
|
45 |
+
# Extract the conference from the team names
|
46 |
+
schedule_df['Conference 1'] = schedule_df['Team 1'].str[0]
|
47 |
+
schedule_df['Conference 2'] = schedule_df['Team 2'].str[0]
|
48 |
|
49 |
+
# Filter out intra-conference matches
|
50 |
+
inter_conference_df = schedule_df[schedule_df['Conference 1'] != schedule_df['Conference 2']]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
# Create a crosstab for the heatmap
|
53 |
+
heatmap_data = pd.crosstab(inter_conference_df['Conference 1'], inter_conference_df['Conference 2'])
|
54 |
+
|
55 |
+
# Ensure every conference combination has a value
|
56 |
+
all_conferences = schedule_df['Conference 1'].unique()
|
57 |
+
for conf in all_conferences:
|
58 |
+
if conf not in heatmap_data.columns:
|
59 |
+
heatmap_data[conf] = 0
|
60 |
+
if conf not in heatmap_data.index:
|
61 |
+
heatmap_data.loc[conf] = 0
|
62 |
+
|
63 |
+
heatmap_data = heatmap_data.sort_index().sort_index(axis=1)
|
64 |
+
|
65 |
+
# Plot
|
66 |
+
plt.figure(figsize=(8, 6))
|
67 |
+
sns.heatmap(heatmap_data, annot=True, cmap='Oranges', linewidths=.5, cbar_kws={'label': 'Number of Matches'})
|
68 |
+
plt.title('Inter-Conference Match Analysis')
|
69 |
+
plt.ylabel('Conference 1')
|
70 |
+
plt.xlabel('Conference 2')
|
71 |
+
plt.show()
|
72 |
+
|
73 |
+
# Commissioner Analytics
|
74 |
+
def commissioner_analytics(schedule_df, commissioners):
|
75 |
+
"""Generate a bar chart showing matches overseen by each commissioner."""
|
76 |
+
# Assuming each commissioner oversees a specific conference
|
77 |
+
comm_dict = {f"Conference {chr(65+i)}": comm for i, comm in enumerate(commissioners)}
|
78 |
+
schedule_df['Commissioner'] = schedule_df['Conference 1'].map(comm_dict)
|
79 |
+
|
80 |
+
# Count matches overseen by each commissioner
|
81 |
+
commissioner_counts = schedule_df['Commissioner'].value_counts()
|
82 |
+
|
83 |
+
# Plot using matplotlib
|
84 |
+
plt.figure(figsize=(10, 6))
|
85 |
+
plt.bar(commissioner_counts.index, commissioner_counts.values, color='orange')
|
86 |
+
plt.title('Matches Overseen by Each Commissioner')
|
87 |
+
plt.ylabel('Number of Matches')
|
88 |
+
plt.xlabel('Commissioner')
|
89 |
+
plt.xticks(rotation=45)
|
90 |
+
plt.tight_layout()
|
91 |
+
plt.show()
|
92 |
+
|
93 |
+
|
94 |
|
95 |
# Streamlit App
|
96 |
|
97 |
st.title("Basketball Game Schedule Generator")
|
98 |
|
99 |
+
# Initialize session state for schedule_df
|
100 |
+
if 'schedule_df' not in st.session_state:
|
101 |
+
st.session_state.schedule_df = None
|
102 |
+
|
103 |
# Configuration UI
|
104 |
st.header("Configuration")
|
105 |
|
|
|
115 |
|
116 |
# Schedule Generation
|
117 |
if st.button("Generate Schedule"):
|
118 |
+
st.session_state.schedule_df = create_schedule(num_teams, num_conferences, num_inter_games)
|
|
|
119 |
|
120 |
# Schedule Viewing
|
121 |
st.header("View Schedule")
|
122 |
conference_selector = st.selectbox("Select conference to view schedule:", options=["All"] + [f"Conference {chr(65+i)}" for i in range(num_conferences)])
|
123 |
+
if st.session_state.schedule_df is not None:
|
124 |
+
if conference_selector == "All":
|
125 |
+
st.dataframe(st.session_state.schedule_df)
|
126 |
+
else:
|
127 |
+
filtered_schedule = st.session_state.schedule_df[(st.session_state.schedule_df["Team 1"].str.startswith(conference_selector)) | (st.session_state.schedule_df["Team 2"].str.startswith(conference_selector))]
|
128 |
+
st.dataframe(filtered_schedule)
|
129 |
+
|
130 |
+
# Analytics & Comparisons
|
131 |
+
st.header("Analytics & Comparisons")
|
132 |
+
analytics_option = st.selectbox("Choose an analysis type:", ["Team Workload Analysis", "Match Distribution", "Inter-Conference Match Analysis", "Commissioner Analytics"])
|
133 |
+
historical_data = generate_mock_historical_data(num_teams, num_conferences, num_inter_games, date(2022, 11, 6), date(2023, 3, 1))
|
134 |
+
|
135 |
+
if analytics_option == "Team Workload Analysis":
|
136 |
+
st.subheader("Historical Data")
|
137 |
+
st.pyplot(team_workload_analysis(historical_data))
|
138 |
+
st.subheader("Current Data")
|
139 |
+
st.pyplot(team_workload_analysis(st.session_state.schedule_df))
|
140 |
+
|
141 |
+
elif analytics_option == "Match Distribution":
|
142 |
+
st.subheader("Historical Data")
|
143 |
+
st.pyplot(match_distribution(historical_data))
|
144 |
+
st.subheader("Current Data")
|
145 |
+
st.pyplot(match_distribution(st.session_state.schedule_df))
|
146 |
+
|
147 |
+
elif analytics_option == "Inter-Conference Match Analysis":
|
148 |
+
st.subheader("Historical Data")
|
149 |
+
st.pyplot(inter_conference_analysis(historical_data))
|
150 |
+
st.subheader("Current Data")
|
151 |
+
st.pyplot(inter_conference_analysis(st.session_state.schedule_df))
|
152 |
+
|
153 |
+
elif analytics_option == "Commissioner Analytics":
|
154 |
+
st.subheader("Historical Data")
|
155 |
+
st.pyplot(commissioner_analytics(historical_data, commissioners))
|
156 |
+
st.subheader("Current Data")
|
157 |
+
st.pyplot(commissioner_analytics(st.session_state.schedule_df, commissioners))
|
158 |
|
159 |
# Export functionality can be added later
|