patrickramos commited on
Commit
f1f0527
·
1 Parent(s): 2929fa3

Change implementation of player- and league-level stats

Browse files
Files changed (3) hide show
  1. daily_pitcher_leaderboard.py +2 -2
  2. data.py +51 -30
  3. gradio_function.py +18 -9
daily_pitcher_leaderboard.py CHANGED
@@ -103,8 +103,8 @@ def create_daily_pitcher_leaderboard():
103
  min_width=50
104
  )
105
  top_players = gr.Number(10, label='# Top players', scale=1, min_width=100)
106
- strict = gr.Checkbox(False, label='Strict', info='Ignore ties and restrict to # top players', scale=1.5, min_width=100)
107
- ignore_zero_whiffs = gr.Checkbox(False, label='Ignore zero whiffs', info='Ignore zero whiff players if in top ranked', scale=1.5, min_width=100)
108
  show_rank = gr.Checkbox(False, label='Show rank', scale=1, min_width=100)
109
  debug = gr.Checkbox(False, label='Debug', info='Show dates', scale=1, min_width=100)
110
  search_btn = gr.Button('Search', scale=1, min_width=100)
 
103
  min_width=50
104
  )
105
  top_players = gr.Number(10, label='# Top players', scale=1, min_width=100)
106
+ strict = gr.Checkbox(False, label='Strict', info='Ignore ties and restrict to # top players', scale=2, min_width=100)
107
+ ignore_zero_whiffs = gr.Checkbox(False, label='Ignore zero whiffs', info='Ignore zero whiff players if in top ranked', scale=2, min_width=100)
108
  show_rank = gr.Checkbox(False, label='Show rank', scale=1, min_width=100)
109
  debug = gr.Checkbox(False, label='Debug', info='Show dates', scale=1, min_width=100)
110
  search_btn = gr.Button('Search', scale=1, min_width=100)
data.py CHANGED
@@ -2,7 +2,7 @@
2
  # import pandas as pd
3
  import polars as pl
4
  import numpy as np
5
- from gradio_client import Client
6
  from tqdm.auto import tqdm
7
 
8
  import os
@@ -159,32 +159,53 @@ df = (
159
  # unfortunately we have pas that don't show up in the pitch data, so this would be useful for
160
  pa_df = pa_df.join(player_df.rename({'player_id': 'pitcher'}), on='pitcher', how='inner')
161
 
162
- pitch_stats, rhb_pitch_stats, lhb_pitch_stats = [
163
- (
164
- _df
165
- .group_by(['name', 'pitch_name'])
166
- .agg(
167
- ((pl.col('whiff').sum() / pl.col('swing').sum()) * 100).round(1).alias('Whiff%'),
168
- ((pl.col('csw').sum() / pl.col('normal_pitch').sum()) * 100).round(1).alias('CSW%'),
169
- pl.col('release_speed').mean().round(1).alias('Velocity'),
170
- pl.len().alias('Count')
171
- )
172
- .sort(['name', 'Count'], descending=[False, True])
173
- # .rename({'name': 'Player', 'pitch_name': 'Pitch'})
174
- )
175
- for _df
176
- in (
177
- df,
178
- df.filter(pl.col('stand') == 'R'),
179
- df.filter(pl.col('stand') == 'L'),
180
- )
181
- ]
182
- league_pitch_stats, rhb_league_pitch_stats, lhb_league_pitch_stats = [
183
- _df.group_by('pitch_name').agg(pl.col('release_speed').mean().round(1).alias('Velocity'))
184
- for _df
185
- in (
186
- df,
187
- df.filter(pl.col('stand') == 'R'),
188
- df.filter(pl.col('stand') == 'L'),
189
- )
190
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  # import pandas as pd
3
  import polars as pl
4
  import numpy as np
5
+ # from gradio_client import Client
6
  from tqdm.auto import tqdm
7
 
8
  import os
 
159
  # unfortunately we have pas that don't show up in the pitch data, so this would be useful for
160
  pa_df = pa_df.join(player_df.rename({'player_id': 'pitcher'}), on='pitcher', how='inner')
161
 
162
+ # pitch_stats, rhb_pitch_stats, lhb_pitch_stats = [
163
+ # (
164
+ # _df
165
+ # .group_by(['name', 'pitch_name'])
166
+ # .agg(
167
+ # ((pl.col('whiff').sum() / pl.col('swing').sum()) * 100).round(1).alias('Whiff%'),
168
+ # ((pl.col('csw').sum() / pl.col('normal_pitch').sum()) * 100).round(1).alias('CSW%'),
169
+ # pl.col('release_speed').mean().round(1).alias('Velocity'),
170
+ # pl.len().alias('Count')
171
+ # )
172
+ # .sort(['name', 'Count'], descending=[False, True])
173
+ # # .rename({'name': 'Player', 'pitch_name': 'Pitch'})
174
+ # )
175
+ # for _df
176
+ # in (
177
+ # df,
178
+ # df.filter(pl.col('stand') == 'R'),
179
+ # df.filter(pl.col('stand') == 'L'),
180
+ # )
181
+ # ]
182
+ # league_pitch_stats, rhb_league_pitch_stats, lhb_league_pitch_stats = [
183
+ # _df.group_by('pitch_name').agg(pl.col('release_speed').mean().round(1).alias('Velocity'))
184
+ # for _df
185
+ # in (
186
+ # df,
187
+ # df.filter(pl.col('stand') == 'R'),
188
+ # df.filter(pl.col('stand') == 'L'),
189
+ # )
190
+ # ]
191
+
192
+ def compute_pitch_stats(df):
193
+ pitch_stats = (
194
+ df
195
+ .group_by(['name', 'pitch_name'])
196
+ .agg(
197
+ ((pl.col('whiff').sum() / pl.col('swing').sum()) * 100).round(1).alias('Whiff%'),
198
+ ((pl.col('csw').sum() / pl.col('normal_pitch').sum()) * 100).round(1).alias('CSW%'),
199
+ pl.col('release_speed').mean().round(1).alias('Velocity'),
200
+ pl.len().alias('Count')
201
+ )
202
+ .sort(['name', 'Count'], descending=[False, True])
203
+ )
204
+ return pitch_stats
205
+
206
+ pitch_stats = compute_pitch_stats(df)
207
+
208
+ def compute_league_pitch_stats(df):
209
+ return df.group_by('pitch_name').agg(pl.col('release_speed').mean().round(1).alias('Velocity'))
210
+
211
+ league_pitch_stats = compute_league_pitch_stats(df)
gradio_function.py CHANGED
@@ -12,8 +12,9 @@ from math import ceil
12
  from translate import max_pitch_types, jp_pitch_to_en_pitch
13
  from data import (
14
  df,
15
- pitch_stats, rhb_pitch_stats,lhb_pitch_stats,
16
- league_pitch_stats, rhb_league_pitch_stats, lhb_league_pitch_stats
 
17
  )
18
 
19
 
@@ -315,19 +316,27 @@ def plot_velo_summary(df, league_df, player):
315
  def update_dfs(player, handedness, df):
316
  if handedness == 'Both':
317
  handedness_filter = pl.col('stand').is_in(['R', 'L'])
318
- _pitch_stats = pitch_stats
319
- _league_pitch_stats = league_pitch_stats
320
  elif handedness == 'Right':
321
  handedness_filter = pl.col('stand') == 'R'
322
- _pitch_stats = rhb_pitch_stats
323
- _league_pitch_stats = rhb_league_pitch_stats
324
  elif handedness == 'Left':
325
  handedness_filter = pl.col('stand') == 'L'
326
- _pitch_stats = lhb_pitch_stats
327
- _league_pitch_stats = lhb_league_pitch_stats
328
  player_filter = pl.col('name') == player
329
  final_filter = player_filter & handedness_filter
330
- return df.filter(final_filter), df.filter(handedness_filter), _pitch_stats.filter(player_filter), _league_pitch_stats,
 
 
 
 
 
 
 
 
331
 
332
  def create_set_download_file_fn(filepath):
333
  def set_download_file(df):
 
12
  from translate import max_pitch_types, jp_pitch_to_en_pitch
13
  from data import (
14
  df,
15
+ # pitch_stats, rhb_pitch_stats,lhb_pitch_stats,
16
+ # league_pitch_stats, rhb_league_pitch_stats, lhb_league_pitch_stats
17
+ compute_pitch_stats, compute_league_pitch_stats
18
  )
19
 
20
 
 
316
  def update_dfs(player, handedness, df):
317
  if handedness == 'Both':
318
  handedness_filter = pl.col('stand').is_in(['R', 'L'])
319
+ # _pitch_stats = pitch_stats
320
+ # _league_pitch_stats = league_pitch_stats
321
  elif handedness == 'Right':
322
  handedness_filter = pl.col('stand') == 'R'
323
+ # _pitch_stats = rhb_pitch_stats
324
+ # _league_pitch_stats = rhb_league_pitch_stats
325
  elif handedness == 'Left':
326
  handedness_filter = pl.col('stand') == 'L'
327
+ # _pitch_stats = lhb_pitch_stats
328
+ # _league_pitch_stats = lhb_league_pitch_stats
329
  player_filter = pl.col('name') == player
330
  final_filter = player_filter & handedness_filter
331
+ _df = df.filter(final_filter)
332
+ _league_df = df.filter(handedness_filter)
333
+
334
+ return (
335
+ _df,
336
+ _league_df,
337
+ compute_pitch_stats(_df),
338
+ compute_league_pitch_stats(_league_df),
339
+ )
340
 
341
  def create_set_download_file_fn(filepath):
342
  def set_download_file(df):