Spaces:
Running
Running
Commit
·
15b45d6
1
Parent(s):
16db15a
Add velo stats to pitch leaderboard
Browse files- pitch_leaderboard.py +19 -6
- plotting.py +2 -3
- stats.py +7 -2
pitch_leaderboard.py
CHANGED
|
@@ -10,9 +10,9 @@ from stats import compute_pitch_stats, filter_data_by_date_and_game_kind
|
|
| 10 |
from convert import ball_kind, ball_kind_to_color, get_text_color_from_color, team_names_short_to_color, get_text_color_from_team
|
| 11 |
from plotting import stat_cmap
|
| 12 |
|
| 13 |
-
STATS = ['Count', 'Usage', 'Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Contact%', 'O-Contact%', 'SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%', 'Zone%', 'Arm%', 'Glove%', 'High%', 'Low%', 'MM%']
|
| 14 |
PCT_STATS = ['Usage', 'Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Contact%', 'O-Contact%', 'SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%', 'Zone%', 'Arm%', 'Glove%', 'High%', 'Low%', 'MM%']
|
| 15 |
-
STATS_WITH_PCTLS = ['Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Contact%', 'O-Contact%', 'SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%', 'Zone%']
|
| 16 |
COLUMNS = ['Pitcher', 'Team', 'Pitch', 'Pitch (General)'] + STATS
|
| 17 |
|
| 18 |
PITCH_TYPES = [pitch_type for pitch_type in ball_kind.values() if pitch_type != '-']
|
|
@@ -69,7 +69,18 @@ def gr_create_pitch_leaderboard(start_date, end_date, min_pitches, pitcher_lr='B
|
|
| 69 |
compute_pitch_stats(data, player_type='pitcher', min_pitches=min_pitches, pitch_class_type='specific')
|
| 70 |
.filter(pl.col('qualified') & (pl.col('ballKind').is_in(include_pitches)))
|
| 71 |
.drop('pitId', 'ballKind_code', 'qualified')
|
| 72 |
-
.rename({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
.with_columns(
|
| 74 |
pl.col(stat).mul(100)
|
| 75 |
for stat in PCT_STATS
|
|
@@ -85,7 +96,7 @@ def gr_create_pitch_leaderboard(start_date, end_date, min_pitches, pitcher_lr='B
|
|
| 85 |
styling_row = []
|
| 86 |
for col, item in zip(pitch_stats[COLUMNS].columns, row):
|
| 87 |
_styling = 'font-size: 0.75em; '
|
| 88 |
-
if
|
| 89 |
r, g, b = (stat_cmap([pitch_stats[f'{col}_pctl'][i]])[0, :3]*255).astype(np.uint8)
|
| 90 |
styling_row.append(f'background-color: rgba({r}, {g}, {b})')
|
| 91 |
elif col == 'Team':
|
|
@@ -100,9 +111,11 @@ def gr_create_pitch_leaderboard(start_date, end_date, min_pitches, pitcher_lr='B
|
|
| 100 |
display_value = []
|
| 101 |
for row in pitch_stats[COLUMNS].iter_rows():
|
| 102 |
display_value_row = []
|
| 103 |
-
for item in row:
|
| 104 |
-
if
|
| 105 |
display_value_row.append(f'{item:.1f}%')
|
|
|
|
|
|
|
| 106 |
else:
|
| 107 |
display_value_row.append(item)
|
| 108 |
display_value.append(display_value_row)
|
|
|
|
| 10 |
from convert import ball_kind, ball_kind_to_color, get_text_color_from_color, team_names_short_to_color, get_text_color_from_team
|
| 11 |
from plotting import stat_cmap
|
| 12 |
|
| 13 |
+
STATS = ['Count', 'Usage', 'Avg Velo', 'Max Velo', 'Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Contact%', 'O-Contact%', 'SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%', 'Zone%', 'Arm%', 'Glove%', 'High%', 'Low%', 'MM%']
|
| 14 |
PCT_STATS = ['Usage', 'Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Contact%', 'O-Contact%', 'SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%', 'Zone%', 'Arm%', 'Glove%', 'High%', 'Low%', 'MM%']
|
| 15 |
+
STATS_WITH_PCTLS = ['Avg Velo', 'Max Velo', 'Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Contact%', 'O-Contact%', 'SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%', 'Zone%']
|
| 16 |
COLUMNS = ['Pitcher', 'Team', 'Pitch', 'Pitch (General)'] + STATS
|
| 17 |
|
| 18 |
PITCH_TYPES = [pitch_type for pitch_type in ball_kind.values() if pitch_type != '-']
|
|
|
|
| 69 |
compute_pitch_stats(data, player_type='pitcher', min_pitches=min_pitches, pitch_class_type='specific')
|
| 70 |
.filter(pl.col('qualified') & (pl.col('ballKind').is_in(include_pitches)))
|
| 71 |
.drop('pitId', 'ballKind_code', 'qualified')
|
| 72 |
+
.rename({
|
| 73 |
+
'pitcher_name': 'Pitcher',
|
| 74 |
+
'pitcher_team_name_short': 'Team',
|
| 75 |
+
'count': 'Count',
|
| 76 |
+
'usage': 'Usage',
|
| 77 |
+
'ballKind': 'Pitch',
|
| 78 |
+
'general_ballKind': 'Pitch (General)',
|
| 79 |
+
'Avg MPH': 'Avg Velo',
|
| 80 |
+
'Max MPH': 'Max Velo',
|
| 81 |
+
'Avg MPH_pctl': 'Avg Velo_pctl',
|
| 82 |
+
'Max MPH_pctl': 'Max Velo_pctl'
|
| 83 |
+
})
|
| 84 |
.with_columns(
|
| 85 |
pl.col(stat).mul(100)
|
| 86 |
for stat in PCT_STATS
|
|
|
|
| 96 |
styling_row = []
|
| 97 |
for col, item in zip(pitch_stats[COLUMNS].columns, row):
|
| 98 |
_styling = 'font-size: 0.75em; '
|
| 99 |
+
if col in STATS_WITH_PCTLS:
|
| 100 |
r, g, b = (stat_cmap([pitch_stats[f'{col}_pctl'][i]])[0, :3]*255).astype(np.uint8)
|
| 101 |
styling_row.append(f'background-color: rgba({r}, {g}, {b})')
|
| 102 |
elif col == 'Team':
|
|
|
|
| 111 |
display_value = []
|
| 112 |
for row in pitch_stats[COLUMNS].iter_rows():
|
| 113 |
display_value_row = []
|
| 114 |
+
for col, item in zip(COLUMNS, row):
|
| 115 |
+
if col in PCT_STATS:
|
| 116 |
display_value_row.append(f'{item:.1f}%')
|
| 117 |
+
elif isinstance(item, float):
|
| 118 |
+
display_value_row.append(f'{item:.1f}')
|
| 119 |
else:
|
| 120 |
display_value_row.append(item)
|
| 121 |
display_value.append(display_value_row)
|
plotting.py
CHANGED
|
@@ -58,8 +58,8 @@ y_range = np.arange(0, 250+1)
|
|
| 58 |
X, Y = np.meshgrid(x_range, y_range)
|
| 59 |
|
| 60 |
|
| 61 |
-
def fit_pred_kde(data):
|
| 62 |
-
kde = gaussian_kde(data)
|
| 63 |
Z = kde(np.concat((X, Y)).reshape(2, -1)).reshape(*X.shape)
|
| 64 |
return Z
|
| 65 |
|
|
@@ -169,7 +169,6 @@ font = load_google_font('Saira Extra Condensed', weight='medium')
|
|
| 169 |
|
| 170 |
def create_pitcher_overview_card(id, season, dpi=300):
|
| 171 |
data = get_card_data(id, start_date=date(season, 1, 1), end_date=date(season, 12, 31), game_kind='Regular Season', min_pitches=100, pitch_class_type='general')
|
| 172 |
-
print(data)
|
| 173 |
|
| 174 |
fig = plt.figure(figsize=(1080/300, 1350/300), dpi=dpi)
|
| 175 |
gs = fig.add_gridspec(8, 6, height_ratios=[1, 1, 1.5, 6, 1, 3, 1, 0.5])
|
|
|
|
| 58 |
X, Y = np.meshgrid(x_range, y_range)
|
| 59 |
|
| 60 |
|
| 61 |
+
def fit_pred_kde(data, **kwargs):
|
| 62 |
+
kde = gaussian_kde(data, **kwargs)
|
| 63 |
Z = kde(np.concat((X, Y)).reshape(2, -1)).reshape(*X.shape)
|
| 64 |
return Z
|
| 65 |
|
|
|
|
| 169 |
|
| 170 |
def create_pitcher_overview_card(id, season, dpi=300):
|
| 171 |
data = get_card_data(id, start_date=date(season, 1, 1), end_date=date(season, 12, 31), game_kind='Regular Season', min_pitches=100, pitch_class_type='general')
|
|
|
|
| 172 |
|
| 173 |
fig = plt.figure(figsize=(1080/300, 1350/300), dpi=dpi)
|
| 174 |
gs = fig.add_gridspec(8, 6, height_ratios=[1, 1, 1.5, 6, 1, 3, 1, 0.5])
|
stats.py
CHANGED
|
@@ -72,12 +72,17 @@ def compute_pitch_stats(data, player_type, pitch_class_type, min_pitches=1):
|
|
| 72 |
pitch_name_col = 'ballKind' if pitch_class_type == 'specific' else 'general_ballKind'
|
| 73 |
pitch_stats = (
|
| 74 |
data
|
|
|
|
| 75 |
.group_by(id_col, pitch_col, 'pitcher_team_name_short')
|
| 76 |
.agg(
|
| 77 |
pl.first(name_col),
|
| 78 |
*([pl.first('general_ballKind')] if pitch_class_type == 'specific' else []),
|
| 79 |
pl.first(pitch_name_col),
|
| 80 |
pl.len().alias('count'),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
pl.col('aux_bresult').struct.field('batType').drop_nulls().value_counts(normalize=True),
|
| 82 |
(pl.col('swing').sum() / pl.col('pitch').sum()).alias('Swing%'),
|
| 83 |
((pl.col('swing') & pl.col('zone')).sum() / pl.col('pitch').sum()).alias('Z-Swing%'),
|
|
@@ -97,7 +102,7 @@ def compute_pitch_stats(data, player_type, pitch_class_type, min_pitches=1):
|
|
| 97 |
)
|
| 98 |
.with_columns(
|
| 99 |
(pl.col('count')/pl.sum('count').over('pitId')).alias('usage'),
|
| 100 |
-
(pl.col('count') >= min_pitches).alias('qualified')
|
| 101 |
)
|
| 102 |
.explode('batType')
|
| 103 |
.unnest('batType')
|
|
@@ -111,7 +116,7 @@ def compute_pitch_stats(data, player_type, pitch_class_type, min_pitches=1):
|
|
| 111 |
.drop('G', 'F', 'B', 'P', 'L', 'null')
|
| 112 |
.with_columns(
|
| 113 |
(pl.when(pl.col('qualified')).then(pl.col(stat)).rank(descending=((stat in ['FB%', 'LD%'] or 'Contact%' in stat)))/pl.when(pl.col('qualified')).then(pl.col(stat)).count()).alias(f'{stat}_pctl')
|
| 114 |
-
for stat in ['Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Contact%', 'O-Contact%', 'SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%', 'Zone%']
|
| 115 |
)
|
| 116 |
.rename({pitch_col: 'ballKind_code', pitch_name_col: 'ballKind'} if pitch_class_type == 'general' else {})
|
| 117 |
.sort(id_col, 'count', descending=[False, True])
|
|
|
|
| 72 |
pitch_name_col = 'ballKind' if pitch_class_type == 'specific' else 'general_ballKind'
|
| 73 |
pitch_stats = (
|
| 74 |
data
|
| 75 |
+
.with_columns((pl.col('ballSpeed') / 1.609).round(1).alias('mph'))
|
| 76 |
.group_by(id_col, pitch_col, 'pitcher_team_name_short')
|
| 77 |
.agg(
|
| 78 |
pl.first(name_col),
|
| 79 |
*([pl.first('general_ballKind')] if pitch_class_type == 'specific' else []),
|
| 80 |
pl.first(pitch_name_col),
|
| 81 |
pl.len().alias('count'),
|
| 82 |
+
pl.when(pl.col('x').is_not_null() & pl.col('y').is_not_null() & (pl.col('ballSpeed') > 0)).then('ballSpeed').mean().alias('Avg KPH'),
|
| 83 |
+
pl.col('ballSpeed').max().alias('Max KPH'),
|
| 84 |
+
pl.when(pl.col('x').is_not_null() & pl.col('y').is_not_null() & (pl.col('ballSpeed') > 0)).then('mph').mean().round(1).alias('Avg MPH'),
|
| 85 |
+
pl.col('mph').max().alias('Max MPH'),
|
| 86 |
pl.col('aux_bresult').struct.field('batType').drop_nulls().value_counts(normalize=True),
|
| 87 |
(pl.col('swing').sum() / pl.col('pitch').sum()).alias('Swing%'),
|
| 88 |
((pl.col('swing') & pl.col('zone')).sum() / pl.col('pitch').sum()).alias('Z-Swing%'),
|
|
|
|
| 102 |
)
|
| 103 |
.with_columns(
|
| 104 |
(pl.col('count')/pl.sum('count').over('pitId')).alias('usage'),
|
| 105 |
+
(pl.col('count') >= min_pitches).alias('qualified'),
|
| 106 |
)
|
| 107 |
.explode('batType')
|
| 108 |
.unnest('batType')
|
|
|
|
| 116 |
.drop('G', 'F', 'B', 'P', 'L', 'null')
|
| 117 |
.with_columns(
|
| 118 |
(pl.when(pl.col('qualified')).then(pl.col(stat)).rank(descending=((stat in ['FB%', 'LD%'] or 'Contact%' in stat)))/pl.when(pl.col('qualified')).then(pl.col(stat)).count()).alias(f'{stat}_pctl')
|
| 119 |
+
for stat in ['Avg KPH', 'Max KPH', 'Avg MPH', 'Max MPH', 'Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Contact%', 'O-Contact%', 'SwStr%', 'Whiff%', 'CSW%', 'GB%', 'FB%', 'LD%', 'Zone%']
|
| 120 |
)
|
| 121 |
.rename({pitch_col: 'ballKind_code', pitch_name_col: 'ballKind'} if pitch_class_type == 'general' else {})
|
| 122 |
.sort(id_col, 'count', descending=[False, True])
|