|
import streamlit as st |
|
import pandas as pd |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
import seaborn as sns |
|
from datetime import date, timedelta |
|
import random |
|
|
|
|
|
|
|
|
|
|
|
def team_workload_analysis(schedule_df): |
|
"""Generate a bar chart showing the number of matches each team has per week.""" |
|
schedule_df['Week'] = schedule_df['Date'].dt.isocalendar().week |
|
team_counts = schedule_df.groupby(['Week', 'Team 1']).size().unstack().fillna(0) |
|
|
|
|
|
team_counts.plot(kind='bar', stacked=True, figsize=(15, 7), cmap='Oranges') |
|
plt.title('Team Workload Analysis') |
|
plt.ylabel('Number of Matches') |
|
plt.xlabel('Week Number') |
|
plt.tight_layout() |
|
plt.legend(title='Teams', bbox_to_anchor=(1.05, 1), loc='upper left') |
|
plt.show() |
|
|
|
|
|
def match_distribution(schedule_df): |
|
"""Generate a histogram showing match distribution across months.""" |
|
schedule_df['Month'] = schedule_df['Date'].dt.month_name() |
|
month_order = ['November', 'December', 'January', 'February', 'March'] |
|
|
|
|
|
plt.figure(figsize=(10, 6)) |
|
sns.countplot(data=schedule_df, x='Month', order=month_order, palette='Oranges_r') |
|
plt.title('Match Distribution Across Months') |
|
plt.ylabel('Number of Matches') |
|
plt.xlabel('Month') |
|
plt.tight_layout() |
|
plt.show() |
|
|
|
|
|
def inter_conference_analysis(schedule_df): |
|
"""Generate a heatmap showing inter-conference match frequencies.""" |
|
|
|
schedule_df['Conference 1'] = schedule_df['Team 1'].str[0] |
|
schedule_df['Conference 2'] = schedule_df['Team 2'].str[0] |
|
|
|
|
|
inter_conference_df = schedule_df[schedule_df['Conference 1'] != schedule_df['Conference 2']] |
|
|
|
|
|
heatmap_data = pd.crosstab(inter_conference_df['Conference 1'], inter_conference_df['Conference 2']) |
|
|
|
|
|
all_conferences = schedule_df['Conference 1'].unique() |
|
for conf in all_conferences: |
|
if conf not in heatmap_data.columns: |
|
heatmap_data[conf] = 0 |
|
if conf not in heatmap_data.index: |
|
heatmap_data.loc[conf] = 0 |
|
|
|
heatmap_data = heatmap_data.sort_index().sort_index(axis=1) |
|
|
|
|
|
plt.figure(figsize=(8, 6)) |
|
sns.heatmap(heatmap_data, annot=True, cmap='Oranges', linewidths=.5, cbar_kws={'label': 'Number of Matches'}) |
|
plt.title('Inter-Conference Match Analysis') |
|
plt.ylabel('Conference 1') |
|
plt.xlabel('Conference 2') |
|
plt.show() |
|
|
|
|
|
def commissioner_analytics(schedule_df, commissioners): |
|
"""Generate a bar chart showing matches overseen by each commissioner.""" |
|
|
|
comm_dict = {f"Conference {chr(65+i)}": comm for i, comm in enumerate(commissioners)} |
|
schedule_df['Commissioner'] = schedule_df['Conference 1'].map(comm_dict) |
|
|
|
|
|
commissioner_counts = schedule_df['Commissioner'].value_counts() |
|
|
|
|
|
plt.figure(figsize=(10, 6)) |
|
plt.bar(commissioner_counts.index, commissioner_counts.values, color='orange') |
|
plt.title('Matches Overseen by Each Commissioner') |
|
plt.ylabel('Number of Matches') |
|
plt.xlabel('Commissioner') |
|
plt.xticks(rotation=45) |
|
plt.tight_layout() |
|
plt.show() |
|
|
|
|
|
|
|
|
|
|
|
st.title("Basketball Game Schedule Generator") |
|
|
|
|
|
if 'schedule_df' not in st.session_state: |
|
st.session_state.schedule_df = None |
|
|
|
|
|
st.header("Configuration") |
|
|
|
num_teams = st.number_input("Number of teams per conference:", min_value=2, value=10) |
|
num_conferences = st.number_input("Number of conferences:", min_value=2, value=4) |
|
num_inter_games = st.number_input("Number of inter-conference games per team:", min_value=1, value=3) |
|
|
|
commissioners = st.multiselect("Add commissioners:", options=[], default=[]) |
|
|
|
add_commissioner = st.text_input("New commissioner name:") |
|
if add_commissioner: |
|
commissioners.append(add_commissioner) |
|
|
|
|
|
if st.button("Generate Schedule"): |
|
st.session_state.schedule_df = create_schedule(num_teams, num_conferences, num_inter_games) |
|
|
|
|
|
st.header("View Schedule") |
|
conference_selector = st.selectbox("Select conference to view schedule:", options=["All"] + [f"Conference {chr(65+i)}" for i in range(num_conferences)]) |
|
if st.session_state.schedule_df is not None: |
|
if conference_selector == "All": |
|
st.dataframe(st.session_state.schedule_df) |
|
else: |
|
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))] |
|
st.dataframe(filtered_schedule) |
|
|
|
|
|
st.header("Analytics & Comparisons") |
|
analytics_option = st.selectbox("Choose an analysis type:", ["Team Workload Analysis", "Match Distribution", "Inter-Conference Match Analysis", "Commissioner Analytics"]) |
|
historical_data = generate_mock_historical_data(num_teams, num_conferences, num_inter_games, date(2022, 11, 6), date(2023, 3, 1)) |
|
|
|
if analytics_option == "Team Workload Analysis": |
|
st.subheader("Historical Data") |
|
st.pyplot(team_workload_analysis(historical_data)) |
|
st.subheader("Current Data") |
|
st.pyplot(team_workload_analysis(st.session_state.schedule_df)) |
|
|
|
elif analytics_option == "Match Distribution": |
|
st.subheader("Historical Data") |
|
st.pyplot(match_distribution(historical_data)) |
|
st.subheader("Current Data") |
|
st.pyplot(match_distribution(st.session_state.schedule_df)) |
|
|
|
elif analytics_option == "Inter-Conference Match Analysis": |
|
st.subheader("Historical Data") |
|
st.pyplot(inter_conference_analysis(historical_data)) |
|
st.subheader("Current Data") |
|
st.pyplot(inter_conference_analysis(st.session_state.schedule_df)) |
|
|
|
elif analytics_option == "Commissioner Analytics": |
|
st.subheader("Historical Data") |
|
st.pyplot(commissioner_analytics(historical_data, commissioners)) |
|
st.subheader("Current Data") |
|
st.pyplot(commissioner_analytics(st.session_state.schedule_df, commissioners)) |
|
|
|
|
|
|