Spaces:
Running
Running
Commit
·
8353665
1
Parent(s):
d1369a2
Start pitch leaderboard
Browse files- pitch_leaderboard.py +77 -0
pitch_leaderboard.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import polars as pl
|
3 |
+
|
4 |
+
from datetime import datetime
|
5 |
+
from itertools import chain
|
6 |
+
|
7 |
+
from data import data_df
|
8 |
+
from stats import compute_pitch_stats, filter_data_by_date_and_game_kind
|
9 |
+
from convert import ball_kind
|
10 |
+
|
11 |
+
STATS = ['Count', 'Usage', 'SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%']
|
12 |
+
PCT_STATS = ['Usage', 'SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%']
|
13 |
+
STATS_WITH_PCTLS = ['SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%']
|
14 |
+
|
15 |
+
def gr_create_pitch_leaderboard(start_date, end_date, min_pitches, include_pitches):
|
16 |
+
data = data_df.filter(pl.col('ballKind_code') != '-')
|
17 |
+
|
18 |
+
data = filter_data_by_date_and_game_kind(data, start_date=start_date, end_date=end_date, game_kind='Regular Season')
|
19 |
+
both, left, right = [
|
20 |
+
(
|
21 |
+
compute_pitch_stats(df, player_type='pitcher', min_pitches=min_pitches, pitch_class_type='specific')
|
22 |
+
.filter(pl.col('qualified') & (pl.col('ballKind').is_in(include_pitches)))
|
23 |
+
.drop('qualified')
|
24 |
+
.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})
|
25 |
+
.with_columns(
|
26 |
+
pl.col(stat).mul(100).round(1)
|
27 |
+
for stat in PCT_STATS + [f'{stat} (Pctl)' for stat in STATS_WITH_PCTLS]
|
28 |
+
)
|
29 |
+
[['pitId', 'ballKind_code', 'Pitcher', 'Pitch', 'Pitch (General)', 'Count', 'Usage'] + STATS_WITH_PCTLS]
|
30 |
+
)
|
31 |
+
for df
|
32 |
+
in [data, data.filter(pl.col('batLR') == 'l'), data.filter(pl.col('batLR') == 'r')]
|
33 |
+
]
|
34 |
+
pitch_stats = (
|
35 |
+
both
|
36 |
+
.join(left, on=['pitId', 'ballKind_code'], suffix=' (LHH)', how='full')
|
37 |
+
.join(right, on=['pitId', 'ballKind_code'], suffix=' (RHH)', how='full')
|
38 |
+
.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')])))
|
39 |
+
)
|
40 |
+
return pitch_stats
|
41 |
+
|
42 |
+
|
43 |
+
def create_pitch_leaderboard():
|
44 |
+
now = datetime.now()
|
45 |
+
start_datetime_init = datetime(now.year, 1, 1)
|
46 |
+
end_datetime_init = now
|
47 |
+
pitch_types = [pitch_type for pitch_type in ball_kind.values() if pitch_type != '-']
|
48 |
+
with gr.Blocks() as app:
|
49 |
+
gr.Markdown('# Pitch Leaderboard')
|
50 |
+
with gr.Row():
|
51 |
+
start_date = gr.DateTime(start_datetime_init, include_time=False, type='datetime', label='Start')
|
52 |
+
end_date = gr.DateTime(end_datetime_init, include_time=False, type='datetime', label='End')
|
53 |
+
min_pitches = gr.Number(100, label='Min. Pitches', precision=0, minimum=0)
|
54 |
+
with gr.Row():
|
55 |
+
include_pitches = gr.CheckboxGroup(pitch_types, value=pitch_types, label='Pitches', scale=2)
|
56 |
+
# with gr.Column(scale=1):
|
57 |
+
# all_pitches = gr.Checkbox(label='Select all pitches')
|
58 |
+
|
59 |
+
search = gr.Button('Search')
|
60 |
+
leaderboard = gr.DataFrame(
|
61 |
+
# gr_create_pitch_leaderboard(start_date=start_date_init, end_date=end_date_init, min_pitches=100),
|
62 |
+
column_widths=[200]*3 + [100]*(3*len(STATS)),
|
63 |
+
show_copy_button=True,
|
64 |
+
show_search=True,
|
65 |
+
pinned_columns=2
|
66 |
+
)
|
67 |
+
|
68 |
+
|
69 |
+
search.click(gr_create_pitch_leaderboard, inputs=[start_date, end_date, min_pitches, include_pitches], outputs=leaderboard)
|
70 |
+
# include_pitches.input(lambda _pitch_types: (_pitch_types == pitch_types), inputs=include_pitches, outputs=all_pitches)
|
71 |
+
# all_pitches.input(lambda _all_pitches : pitch_types, inputs=all_outputs=all_pitches)
|
72 |
+
|
73 |
+
return app
|
74 |
+
|
75 |
+
if __name__ == '__main__':
|
76 |
+
app = create_pitch_leaderboard()
|
77 |
+
app.launch()
|