File size: 5,618 Bytes
8353665
 
 
 
65fefb5
8353665
 
 
 
 
8fe9801
 
f89cae0
8353665
8fe9801
 
 
 
 
bb5e4ba
 
 
 
8fe9801
 
bb5e4ba
 
8353665
 
 
bb5e4ba
 
8fe9801
 
 
bb5e4ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8353665
bb5e4ba
 
 
8fe9801
bb5e4ba
 
 
 
8fe9801
8353665
 
 
 
 
 
 
 
 
 
 
 
 
 
8fe9801
bb5e4ba
8fe9801
bb5e4ba
 
8fe9801
 
 
8353665
 
8fe9801
8353665
bb5e4ba
8fe9801
8353665
 
8fe9801
8353665
 
8fe9801
8353665
8fe9801
 
 
 
 
8353665
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import gradio as gr
import polars as pl

from datetime import datetime
# from itertools import chain

from data import data_df
from stats import compute_pitch_stats, filter_data_by_date_and_game_kind
from convert import ball_kind

STATS = ['Count', 'Usage', 'Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Contact%', 'O-Contact%', 'SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%', 'Zone%', 'Arm%', 'Glove%', 'High%', 'Low%', 'MM%']
PCT_STATS = ['Usage', 'Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Contact%', 'O-Contact%', 'SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%', 'Zone%', 'Arm%', 'Glove%', 'High%', 'Low%', 'MM%']
STATS_WITH_PCTLS = ['Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Contact%', 'O-Contact%', 'SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%', 'Zone%']

PITCH_TYPES = [pitch_type for pitch_type in ball_kind.values() if pitch_type != '-']
TEAMS  = ['G', 'S', 'DB', 'D', 'T', 'C', 'F', 'E', 'L', 'M', 'B', 'H']
notes = '''**Limitations**
- Foreign players names are in Hebpurn romanization.

**To-do**
- Color cells according to percentiles
'''


def gr_create_pitch_leaderboard(start_date, end_date, min_pitches, pitcher_lr='Both', include_pitches=PITCH_TYPES, include_teams=None):
  assert pitcher_lr in ['Both', 'Left', 'Right']

  data = data_df.filter(pl.col('ballKind_code') != '-')
  
  data = filter_data_by_date_and_game_kind(data, start_date=start_date, end_date=end_date, game_kind='Regular Season')
  if pitcher_lr != 'Both':
    data = data.filter(pl.col('batLR') == pitcher_lr[0].lower())

  if include_teams is not None:
    data = data.filter(pl.col('pitcher_team').is_in(include_teams))
    
  # both, left, right = [
    # (
      # compute_pitch_stats(df, player_type='pitcher', min_pitches=min_pitches, pitch_class_type='specific')
      # .filter(pl.col('qualified') & (pl.col('ballKind').is_in(include_pitches)))
      # .drop('qualified')
      # .rename({'pitcher_name': 'Pitcher', 'count': 'Count', 'usage': 'Usage', 'ballKind': 'Pitch', 'general_ballKind': 'Pitch (General)'} | {f'{stat}_pctl': f'{stat} (Pctl)' for stat in STATS_WITH_PCTLS})
      # .with_columns(
        # pl.col(stat).mul(100).round(1)
        # for stat in PCT_STATS + [f'{stat} (Pctl)' for stat in STATS_WITH_PCTLS]
      # )
      # [['pitId', 'ballKind_code', 'Pitcher', 'Pitch', 'Pitch (General)', 'Count', 'Usage'] + STATS_WITH_PCTLS]
    # )
    # for df
    # in [data, data.filter(pl.col('batLR') == 'l'), data.filter(pl.col('batLR') == 'r')]
  # ]
  # pitch_stats = (
    # both
    # .join(left, on=['pitId', 'ballKind_code'], suffix=' (LHH)', how='full')
    # .join(right, on=['pitId', 'ballKind_code'], suffix=' (RHH)', how='full')
    # .drop('pitId', 'ballKind_code', *list(chain.from_iterable([[f'{col} ({handedness}HH)' for col in ['pitId', 'ballKind_code', 'Pitcher', 'Pitch', 'Pitch (General)']] for handedness in ('L', 'R')])))
  # )
  pitch_stats = (
        compute_pitch_stats(data, player_type='pitcher', min_pitches=min_pitches, pitch_class_type='specific')
        .filter(pl.col('qualified') & (pl.col('ballKind').is_in(include_pitches)))
        .drop('pitId', 'ballKind_code', 'qualified')
        .rename({'pitcher_name': 'Pitcher', 'pitcher_team': 'Team', 'count': 'Count', 'usage': 'Usage', 'ballKind': 'Pitch', 'general_ballKind': 'Pitch (General)'} | {f'{stat}_pctl': f'{stat} (Pctl)' for stat in STATS_WITH_PCTLS})
        .with_columns(
          pl.col(stat).mul(100).round(1)
          for stat in PCT_STATS + [f'{stat} (Pctl)' for stat in STATS_WITH_PCTLS]
        )
        [['Pitcher', 'Team', 'Pitch', 'Pitch (General)'] + STATS]
  )
  return pitch_stats


def create_pitch_leaderboard():
  now = datetime.now()
  start_datetime_init = datetime(now.year, 1, 1)
  end_datetime_init = now
  with gr.Blocks() as app:
    gr.Markdown('# Pitch Leaderboard')
    with gr.Row():
      start_date = gr.DateTime(start_datetime_init, include_time=False, type='datetime', label='Start')
      end_date = gr.DateTime(end_datetime_init, include_time=False, type='datetime', label='End')
    with gr.Row():
      include_pitches = gr.CheckboxGroup(PITCH_TYPES, value=PITCH_TYPES, label='Pitches', scale=3)
      with gr.Column(scale=1):
        all_pitches = gr.Button('Select/Deselect all pitches')
        min_pitches = gr.Number(100, label='Min. Pitches', precision=0, minimum=0)
        pitcher_lr = gr.Radio(['Both', 'Left', 'Right'], value='Both', label='Batter handedness')
    with gr.Row():
      include_teams = gr.CheckboxGroup(TEAMS, value=TEAMS, label='Teams', scale=3)
      all_teams = gr.Button('Select/Deselect all teams')
        
    search = gr.Button('Search')
    # pin_columns = gr.Checkbox(True, 'Pin columns')
    leaderboard = gr.DataFrame(
      pl.DataFrame({'Pitcher': [], 'Pitch': []}),
      column_widths=[200, 60, 200, 200] + [100]*len(STATS),
      show_copy_button=True,
      show_search=True,
      pinned_columns=3
    )

    gr.Markdown(notes)

    search.click(gr_create_pitch_leaderboard, inputs=[start_date, end_date, min_pitches, pitcher_lr, include_pitches, include_teams], outputs=leaderboard)
    all_pitches.click(lambda _pitch_types : [] if _pitch_types == PITCH_TYPES else PITCH_TYPES, inputs=include_pitches, outputs=include_pitches)
    all_teams.click(lambda _teams : [] if _teams == TEAMS else TEAMS, inputs=include_teams, outputs=include_teams)
    # pin_columns.input(lambda _pin_columns : (gr.update(pinned_columns=None if _pin_columns else 3), not _pin_columns), inputs=pin_columns, outputs=[leaderboard, pin_columns])
    
  return app

if __name__ == '__main__':
  app = create_pitch_leaderboard()
  app.launch()