patrickramos commited on
Commit
b5b8cda
·
1 Parent(s): e7a5154

Update app

Browse files
Files changed (3) hide show
  1. data.py +32 -12
  2. demo.py +0 -1
  3. gradio_function.py +14 -4
data.py CHANGED
@@ -8,7 +8,11 @@ from tqdm.auto import tqdm
8
  import os
9
  import re
10
 
11
- from translate import translate_pa_outcome, translate_pitch_outcome, jp_pitch_to_en_pitch, jp_pitch_to_pitch_code, translate_pitch_outcome, max_pitch_types
 
 
 
 
12
 
13
  # load game data
14
  # game_df = pd.read_csv('game.csv').drop_duplicates()
@@ -177,15 +181,31 @@ df = (
177
  # pitch_stats = pd.concat([whiff_rate, csw_rate, velo], axis=1)
178
  # league_pitch_stats = pd.DataFrame(df.groupby('pitch_name')['release_speed'].apply(lambda x: round(x.mean(), 1)).rename('Velocity'))
179
 
180
- pitch_stats= (
181
- df
182
- .group_by(['name', 'pitch_name'])
183
- .agg(
184
- ((pl.col('whiff').sum() / pl.col('swing').sum()) * 100).round(1).alias('Whiff%'),
185
- ((pl.col('csw').sum() / pl.col('normal_pitch').sum()) * 100).round(1).alias('CSW%'),
186
- pl.col('release_speed').mean().round(1).alias('Velocity'),
187
- pl.len().alias('Count')
 
 
 
188
  )
189
- .sort(['name', 'Count'], descending=[False, True])
190
- )
191
- league_pitch_stats = df.group_by('pitch_name').agg(pl.col('release_speed').mean().round(1).alias('Velocity'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  import os
9
  import re
10
 
11
+ from translate import (
12
+ translate_pa_outcome, translate_pitch_outcome,
13
+ jp_pitch_to_en_pitch, jp_pitch_to_pitch_code,
14
+ max_pitch_types
15
+ )
16
 
17
  # load game data
18
  # game_df = pd.read_csv('game.csv').drop_duplicates()
 
181
  # pitch_stats = pd.concat([whiff_rate, csw_rate, velo], axis=1)
182
  # league_pitch_stats = pd.DataFrame(df.groupby('pitch_name')['release_speed'].apply(lambda x: round(x.mean(), 1)).rename('Velocity'))
183
 
184
+ pitch_stats, rhb_pitch_stats, lhb_pitch_stats = [
185
+ (
186
+ _df
187
+ .group_by(['name', 'pitch_name'])
188
+ .agg(
189
+ ((pl.col('whiff').sum() / pl.col('swing').sum()) * 100).round(1).alias('Whiff%'),
190
+ ((pl.col('csw').sum() / pl.col('normal_pitch').sum()) * 100).round(1).alias('CSW%'),
191
+ pl.col('release_speed').mean().round(1).alias('Velocity'),
192
+ pl.len().alias('Count')
193
+ )
194
+ .sort(['name', 'Count'], descending=[False, True])
195
  )
196
+ for _df
197
+ in (
198
+ df,
199
+ df.filter(pl.col('stand') == 'R'),
200
+ df.filter(pl.col('stand') == 'L'),
201
+ )
202
+ ]
203
+ league_pitch_stats, rhb_league_pitch_stats, lhb_league_pitch_stats = [
204
+ _df.group_by('pitch_name').agg(pl.col('release_speed').mean().round(1).alias('Velocity'))
205
+ for _df
206
+ in (
207
+ df,
208
+ df.filter(pl.col('stand') == 'R'),
209
+ df.filter(pl.col('stand') == 'L'),
210
+ )
211
+ ]
demo.py CHANGED
@@ -46,7 +46,6 @@ with gr.Blocks(css=css) as demo:
46
  pitch_velo_summary = gr.Plot(label='Velocity Summary')#, elem_classes='pitch-velo-summary')
47
  pitch_loc_summary = gr.Plot(label='Overall Location')
48
 
49
-
50
  max_pitch_maps = len(jp_pitch_to_en_pitch)
51
  pitch_maps_per_row = 4
52
  max_rows = ceil(max_pitch_maps/pitch_maps_per_row)
 
46
  pitch_velo_summary = gr.Plot(label='Velocity Summary')#, elem_classes='pitch-velo-summary')
47
  pitch_loc_summary = gr.Plot(label='Overall Location')
48
 
 
49
  max_pitch_maps = len(jp_pitch_to_en_pitch)
50
  pitch_maps_per_row = 4
51
  max_rows = ceil(max_pitch_maps/pitch_maps_per_row)
gradio_function.py CHANGED
@@ -9,7 +9,11 @@ import polars as pl
9
  import gradio as gr
10
 
11
  from translate import max_pitch_types
12
- from data import df, pitch_stats, league_pitch_stats
 
 
 
 
13
 
14
  # GRADIO FUNCTIONS
15
 
@@ -261,12 +265,18 @@ def get_data(player, handedness):
261
  # _df_by_pitch_name = _df.set_index('pitch_name').sort_index()
262
  _df = df.filter(pl.col('name') == player)
263
  league_df = df
 
 
264
  if handedness == 'Right':
265
  _df = _df.filter(pl.col('stand') == 'R')
266
  league_df = league_df.filter(pl.col('stand') == 'R')
 
 
267
  elif handedness == 'Left':
268
  _df = _df.filter(pl.col('stand') == 'L')
269
  league_df = league_df.filter(pl.col('stand') == 'L')
 
 
270
 
271
  handedness = gr.update(value=handedness, interactive=True)
272
 
@@ -296,7 +306,7 @@ def get_data(player, handedness):
296
  # 'Whiff%': pitch_stats.loc[(player, pitch_name), 'Whiff%'].item(),
297
  # 'CSW%': pitch_stats.loc[(player, pitch_name), 'CSW%'].item()
298
  # }]),
299
- value=pitch_stats.filter((pl.col('name') == player) & (pl.col('pitch_name') == pitch_name)).select(['Whiff%', 'CSW%']),
300
  visible=True
301
  ))
302
 
@@ -321,11 +331,11 @@ def get_data(player, handedness):
321
 
322
  # velo_stats = pd.concat([pitch_stats.loc[player, 'Velocity'].rename('Avg. Velo'), league_pitch_stats['Velocity'].rename('League Avg. Velo')], join='inner', axis=1).rename_axis(['Pitch']).reset_index()
323
  velo_stats = (
324
- pitch_stats
325
  .filter(pl.col('name') == player)
326
  .select(pl.col('pitch_name').alias('Pitch'), pl.col('Velocity').alias('Avg. Velo'), pl.col('Count'))
327
  .join(
328
- league_pitch_stats.select(pl.col('pitch_name').alias('Pitch'), pl.col('Velocity').alias('League Avg. Velo')),
329
  on='Pitch',
330
  how='inner'
331
  )
 
9
  import gradio as gr
10
 
11
  from translate import max_pitch_types
12
+ from data import (
13
+ df,
14
+ pitch_stats, rhb_pitch_stats,lhb_pitch_stats,
15
+ league_pitch_stats, rhb_league_pitch_stats, lhb_league_pitch_stats
16
+ )
17
 
18
  # GRADIO FUNCTIONS
19
 
 
265
  # _df_by_pitch_name = _df.set_index('pitch_name').sort_index()
266
  _df = df.filter(pl.col('name') == player)
267
  league_df = df
268
+ _pitch_stats = pitch_stats
269
+ _league_pitch_stats = league_pitch_stats
270
  if handedness == 'Right':
271
  _df = _df.filter(pl.col('stand') == 'R')
272
  league_df = league_df.filter(pl.col('stand') == 'R')
273
+ _pitch_stats = rhb_pitch_stats
274
+ _league_pitch_stats = rhb_league_pitch_stats
275
  elif handedness == 'Left':
276
  _df = _df.filter(pl.col('stand') == 'L')
277
  league_df = league_df.filter(pl.col('stand') == 'L')
278
+ _pitch_stats = lhb_pitch_stats
279
+ _league_pitch_stats = lhb_league_pitch_stats
280
 
281
  handedness = gr.update(value=handedness, interactive=True)
282
 
 
306
  # 'Whiff%': pitch_stats.loc[(player, pitch_name), 'Whiff%'].item(),
307
  # 'CSW%': pitch_stats.loc[(player, pitch_name), 'CSW%'].item()
308
  # }]),
309
+ value=_pitch_stats.filter((pl.col('name') == player) & (pl.col('pitch_name') == pitch_name)).select(['Whiff%', 'CSW%']),
310
  visible=True
311
  ))
312
 
 
331
 
332
  # velo_stats = pd.concat([pitch_stats.loc[player, 'Velocity'].rename('Avg. Velo'), league_pitch_stats['Velocity'].rename('League Avg. Velo')], join='inner', axis=1).rename_axis(['Pitch']).reset_index()
333
  velo_stats = (
334
+ _pitch_stats
335
  .filter(pl.col('name') == player)
336
  .select(pl.col('pitch_name').alias('Pitch'), pl.col('Velocity').alias('Avg. Velo'), pl.col('Count'))
337
  .join(
338
+ _league_pitch_stats.select(pl.col('pitch_name').alias('Pitch'), pl.col('Velocity').alias('League Avg. Velo')),
339
  on='Pitch',
340
  how='inner'
341
  )