patrickramos commited on
Commit
f576748
·
1 Parent(s): 3843738

Add shareable leaderboards

Browse files
Files changed (1) hide show
  1. daily_weekly_leaderboard.py +53 -33
daily_weekly_leaderboard.py CHANGED
@@ -4,6 +4,7 @@ import polars as pl
4
  import pandas as pd
5
  import matplotlib.pyplot as plt
6
  from plottable import Table, ColumnDefinition
 
7
 
8
  from data import df, game_df
9
  from gradio_function import *
@@ -37,8 +38,8 @@ def filter_pitcher_leaderboard_by_date(date, *args, **kwargs):
37
  daily_velos,
38
  weekly_whiffs,
39
  weekly_velos,
40
- plot_tables(daily_whiffs, daily_velos, 'Daily', f'{date.strftime("%B %d, %Y")}\n{date.strftime("%A")}'),
41
- plot_tables(weekly_whiffs, weekly_velos, 'Weekly', f'{monday.strftime("%B %d, %Y")} to {sunday.strftime("%B %d, %Y")}\n{monday.strftime("%A")} to {sunday.strftime("%A")}'),
42
  gr.update(interactive=True),
43
  gr.update(interactive=True),
44
  gr.update(interactive=True),
@@ -50,7 +51,7 @@ def compute_pitcher_leaderboards(df, top_players, strict, ignore_zero_whiffs, sh
50
  # _df = df.filter(pl.col('game_date') == date)
51
  _df = df
52
 
53
- other_cols = ['Name']
54
 
55
  if debug:
56
  other_cols = ['game_date'] + other_cols
@@ -109,25 +110,39 @@ def compute_pitcher_leaderboards(df, top_players, strict, ignore_zero_whiffs, sh
109
 
110
 
111
  def plot_tables(whiffs, velos, time_type, subheader):
112
- fig, (whiff_ax, velo_ax) = plt.subplots(ncols=2, constrained_layout=True, sharey=True)
113
 
114
- whiffs = whiffs.to_pandas()
 
 
 
 
 
115
  if 'Rank' in whiffs.columns:
116
  whiffs = whiffs.set_index('Rank')
117
  else:
118
  whiffs.index = pd.Series(range(1, len(whiffs)+1), name='Rank')
119
  Table(
120
- whiffs,
 
 
121
  column_definitions=[
122
- ColumnDefinition(name="Rank", title="Rank", width=0.25),
123
- ColumnDefinition(name="Name", title="Player", textprops={'ha': 'left'}),
124
- ColumnDefinition(name="Whiffs", title="#", width=0.25)
 
125
  ],
126
  ax=whiff_ax
127
  )
128
- whiff_ax.set_title('Whiffs')
129
 
130
- velos = velos.to_pandas()
 
 
 
 
 
 
131
  if 'Rank' in velos.columns:
132
  velos = velos.set_index('Rank')
133
  else:
@@ -135,16 +150,16 @@ def plot_tables(whiffs, velos, time_type, subheader):
135
  Table(
136
  velos,
137
  column_definitions=[
138
- ColumnDefinition(name="Rank", title="Rank", width=0.25),
139
- ColumnDefinition(name="Name", title="Player", textprops={'ha': 'left'}),
140
- ColumnDefinition(name="Velocity", title="MPH")
 
141
  ],
142
  ax=velo_ax
143
  )
144
- velo_ax.set_title('Velocity')
145
-
146
- fig.suptitle(f'{time_type} Leaderboard\n{subheader}')
147
- return fig
148
 
149
  def go_back_day(date):
150
  return date - datetime.timedelta(days=1)
@@ -194,29 +209,34 @@ def create_daily_pitcher_leaderboard():
194
  next_day_btn = gr.Button('Next Day', interactive=False)
195
  next_week_btn = gr.Button('Next Week', interactive=False)
196
 
 
 
 
 
 
197
 
198
- daily_header = gr.HTML('<center><h1>Daily Leaderboard<h1><h2 style="display: none;"></h2><h3 style="display: none;"></h3></center>')
199
- with gr.Row():
200
- daily_whiffs = gr.Dataframe(pl.DataFrame({'Name': [], 'Whiffs': []}), label='Whiffs', interactive=False, height=1000)
201
- daily_velos = gr.Dataframe(pl.DataFrame({'Name': [], 'Velocity': []}), label='Velocity', interactive=False, height=1000)
202
 
203
- weekly_header = gr.HTML('<center><h1>Weekly Leaderboard<h1><h2 style="display: none;"></h2><h3 style="display: none;"></h3></center>')
204
- with gr.Row():
205
- weekly_whiffs = gr.Dataframe(pl.DataFrame({'Name': [], 'Whiffs': []}), label='Whiffs', interactive=False, height=1000)
206
- weekly_velos = gr.Dataframe(pl.DataFrame({'Name': [], 'Velocity': []}), label='Velocity', interactive=False, height=1000)
207
 
208
- gr.Markdown('''# Plotted leaderboards
 
 
 
 
209
 
210
- For easier sharing
211
- ''')
212
- with gr.Row():
213
- daily_plot = gr.Plot(label='Daily plot')
214
- weekly_plot = gr.Plot(label='Weekly plot')
215
 
216
  search_kwargs = dict(
217
  fn=filter_pitcher_leaderboard_by_date,
218
  inputs=[date_picker, top_players, strict, ignore_zero_whiffs, show_rank, debug],
219
- outputs=[daily_header, weekly_header, daily_whiffs, daily_velos, weekly_whiffs, weekly_velos, daily_plot, weekly_plot, prev_day_btn, next_day_btn, prev_week_btn, next_week_btn]
220
  )
221
  search_btn.click(**search_kwargs)
222
  for btn, fn in (
 
4
  import pandas as pd
5
  import matplotlib.pyplot as plt
6
  from plottable import Table, ColumnDefinition
7
+ from plottable.plots import circled_image
8
 
9
  from data import df, game_df
10
  from gradio_function import *
 
38
  daily_velos,
39
  weekly_whiffs,
40
  weekly_velos,
41
+ *plot_tables(daily_whiffs, daily_velos, 'Daily', f'{date.strftime("%B %d, %Y")}\n{date.strftime("%A")}'),
42
+ *plot_tables(weekly_whiffs, weekly_velos, 'Weekly', f'{monday.strftime("%B %d, %Y")} to {sunday.strftime("%B %d, %Y")}\n{monday.strftime("%A")} to {sunday.strftime("%A")}'),
43
  gr.update(interactive=True),
44
  gr.update(interactive=True),
45
  gr.update(interactive=True),
 
51
  # _df = df.filter(pl.col('game_date') == date)
52
  _df = df
53
 
54
+ other_cols = ['Team', 'Name']
55
 
56
  if debug:
57
  other_cols = ['game_date'] + other_cols
 
110
 
111
 
112
  def plot_tables(whiffs, velos, time_type, subheader):
113
+ whiff_fig, whiff_ax = plt.subplots(figsize=(4, 6))
114
 
115
+ whiffs = (
116
+ whiffs
117
+ .with_columns(
118
+ pl.col('Team').map_elements(lambda team: f'assets/{team.lower()}.png', return_dtype=str)
119
+ )
120
+ ).to_pandas()
121
  if 'Rank' in whiffs.columns:
122
  whiffs = whiffs.set_index('Rank')
123
  else:
124
  whiffs.index = pd.Series(range(1, len(whiffs)+1), name='Rank')
125
  Table(
126
+ (
127
+ whiffs
128
+ ),
129
  column_definitions=[
130
+ ColumnDefinition(name="Rank", title="Rank", width=0.25),
131
+ ColumnDefinition(name='Team', title='Team', width=0.25, plot_fn=circled_image, textprops={'ha': 'center'}),
132
+ ColumnDefinition(name="Name", title="Player", textprops={'ha': 'left'}),
133
+ ColumnDefinition(name="Whiffs", title="#", width=0.25)
134
  ],
135
  ax=whiff_ax
136
  )
137
+ whiff_fig.suptitle(f'{time_type} Whiff Leaderboard\n{subheader}')
138
 
139
+ velo_fig, velo_ax = plt.subplots(figsize=(4, 6))
140
+ velos = (
141
+ velos
142
+ .with_columns(
143
+ pl.col('Team').map_elements(lambda team: f'assets/{team.lower()}.png', return_dtype=str)
144
+ )
145
+ ).to_pandas()
146
  if 'Rank' in velos.columns:
147
  velos = velos.set_index('Rank')
148
  else:
 
150
  Table(
151
  velos,
152
  column_definitions=[
153
+ ColumnDefinition(name="Rank", title="Rank", width=0.25),
154
+ ColumnDefinition(name='Team', title='Team', width=0.25, plot_fn=circled_image, textprops={'ha': 'center'}),
155
+ ColumnDefinition(name="Name", title="Player", textprops={'ha': 'left'}),
156
+ ColumnDefinition(name="Velocity", title="MPH", width=0.25)
157
  ],
158
  ax=velo_ax
159
  )
160
+ velo_fig.suptitle(f'{time_type} Velocity Leaderboard\n{subheader}')
161
+
162
+ return whiff_fig, velo_fig
 
163
 
164
  def go_back_day(date):
165
  return date - datetime.timedelta(days=1)
 
209
  next_day_btn = gr.Button('Next Day', interactive=False)
210
  next_week_btn = gr.Button('Next Week', interactive=False)
211
 
212
+ with gr.Tab('Tables for viewing'):
213
+ daily_header = gr.HTML('<center><h1>Daily Leaderboard<h1><h2 style="display: none;"></h2><h3 style="display: none;"></h3></center>')
214
+ with gr.Row():
215
+ daily_whiffs = gr.Dataframe(pl.DataFrame({'Name': [], 'Whiffs': []}), label='Whiffs', interactive=False, height=1000)
216
+ daily_velos = gr.Dataframe(pl.DataFrame({'Name': [], 'Velocity': []}), label='Velocity', interactive=False, height=1000)
217
 
218
+ weekly_header = gr.HTML('<center><h1>Weekly Leaderboard<h1><h2 style="display: none;"></h2><h3 style="display: none;"></h3></center>')
219
+ with gr.Row():
220
+ weekly_whiffs = gr.Dataframe(pl.DataFrame({'Name': [], 'Whiffs': []}), label='Whiffs', interactive=False, height=1000)
221
+ weekly_velos = gr.Dataframe(pl.DataFrame({'Name': [], 'Velocity': []}), label='Velocity', interactive=False, height=1000)
222
 
223
+ with gr.Tab('Tables for sharing'):
224
+ gr.Markdown('''# Plotted leaderboards
 
 
225
 
226
+ For easier sharing
227
+ ''')
228
+ with gr.Row():
229
+ daily_whiffs_plot = gr.Plot(label='Whiffs')
230
+ daily_velos_plot = gr.Plot(label='Velocity')
231
 
232
+ with gr.Row():
233
+ weekly_whiffs_plot = gr.Plot(label='Whiffs')
234
+ weekly_velos_plot = gr.Plot(label='Velocity')
 
 
235
 
236
  search_kwargs = dict(
237
  fn=filter_pitcher_leaderboard_by_date,
238
  inputs=[date_picker, top_players, strict, ignore_zero_whiffs, show_rank, debug],
239
+ outputs=[daily_header, weekly_header, daily_whiffs, daily_velos, weekly_whiffs, weekly_velos, daily_whiffs_plot, daily_velos_plot, weekly_whiffs_plot, weekly_velos_plot, prev_day_btn, next_day_btn, prev_week_btn, next_week_btn]
240
  )
241
  search_btn.click(**search_kwargs)
242
  for btn, fn in (