nesticot commited on
Commit
c611b68
·
verified ·
1 Parent(s): 3a13ecf

Upload 2 files

Browse files
pitch_summary_functions.py CHANGED
@@ -228,43 +228,59 @@ def percentile(n):
228
  return percentile_
229
 
230
  ### TJ STUFF+ DF CLEAN ###
 
231
  def df_clean(df):
232
  df_copy = df.copy()
233
-
234
- df_copy = df_copy[(df_copy['spin_rate']>0)&(df_copy['extension']>0)]
235
-
236
  df_copy.loc[df_copy['pitcher_hand'] == 'L','hb'] *= -1
237
  df_copy.loc[df_copy['pitcher_hand'] == 'L','x0'] *= -1
238
  df_copy.loc[df_copy['pitcher_hand'] == 'L','spin_direction'] = 360 - df_copy.loc[df_copy['pitcher_hand'] == 'L','spin_direction']
239
 
240
  df_copy['pitch_l'] = [1 if x == 'L' else 0 for x in df_copy['pitcher_hand']]
241
  df_copy['bat_l'] = [1 if x == 'L' else 0 for x in df_copy['batter_hand']]
242
- #df_copy = df_copy[~df_copy.pitch_type.isin(["EP", "PO", "KN", "CS", "SC", "FA"])].reset_index(drop=True)
243
- #df_copy = df_copy[~df_copy.pitch_type.isin(["EP", "PO", "CS", "SC", "FA"])].reset_index(drop=True)
244
-
245
-
246
- df_copy['pitch_type'] = df_copy['pitch_type'].replace({'FT':'SI',
247
- 'FA':'FF',
248
- 'SV':'SL',
249
- 'FO':'FS'})
250
-
251
- df_copy_fb_sum = df_copy[df_copy.pitch_type.isin(["FF", "FC", "SI"])].groupby(['pitcher_id']).agg(
252
- fb_velo = ('start_speed','mean'),
253
- fb_max_ivb = ('ivb',percentile(0.9)),
254
- fb_max_x = ('hb',percentile(0.9)),
255
- fb_min_x = ('hb',percentile(0.1)),
256
- fb_max_velo = ('start_speed',percentile(0.9)),
257
- fb_axis = ('spin_direction','mean'),
258
- )
259
-
260
- df_copy = df_copy.merge(df_copy_fb_sum,left_on='pitcher_id',right_index=True,how='left')
261
-
262
- df_copy['fb_velo_diff'] = df_copy['start_speed']- df_copy['fb_velo']
263
- df_copy['fb_max_ivb_diff'] = df_copy['ivb']- df_copy['fb_max_ivb']
264
- df_copy['fb_max_hb_diff'] = -abs(df_copy['hb']- df_copy['fb_max_x'])
265
- df_copy['fb_min_hb_diff'] = df_copy['hb']- df_copy['fb_min_x']
266
- df_copy['fb_max_velo_diff'] = df_copy['start_speed']- df_copy['fb_max_velo']
267
- df_copy['fb_axis_diff'] = df_copy['spin_direction']- df_copy['fb_axis']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
 
269
  # df_copy.loc[df_copy.pitch_type.isin(["FF", "FC", "SI"]),'fb_velo_diff'] = 0
270
  # df_copy.loc[df_copy.pitch_type.isin(["FF", "FC", "SI"]),'fb_max_ivb_diff'] = 0
@@ -295,10 +311,11 @@ def df_clean(df):
295
 
296
  # df_copy['vaa'] = np.arctan(df_copy['z_diff'] / df_copy['release_pos_y']) * 360 / np.pi
297
  # df_copy['haa'] = np.arctan(-df_copy['x_diff'] / df_copy['release_pos_y']) * 360 / np.pi
298
-
299
- df_copy = df_copy.dropna(subset=['pitch_type'])#.fillna(0)
300
  return df_copy
301
 
 
302
  ### PITCH COLOURS ###
303
  pitch_colours = {
304
  'Four-Seam Fastball':'#FF007D',#BC136F
@@ -641,6 +658,36 @@ def tj_stuff_roling(df,
641
  # ax.set_xlim(left=1)
642
  ax.xaxis.set_major_locator(MaxNLocator(integer=True))
643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
644
  ### BREAK PLOT ###
645
  def break_plot(df,
646
  ax):
@@ -1044,4 +1091,30 @@ def location_plot(df,ax,hand):
1044
  if len(pitch_location_group['px'])>0:
1045
  ax.get_legend().remove()
1046
  ax.grid(False)
1047
- ax.set_title(f"Pitch Locations vs {hand}HB\n{pitch_location_group['pitches'].sum()} Pitches",fontdict=font_properties_titles)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
  return percentile_
229
 
230
  ### TJ STUFF+ DF CLEAN ###
231
+
232
  def df_clean(df):
233
  df_copy = df.copy()
 
 
 
234
  df_copy.loc[df_copy['pitcher_hand'] == 'L','hb'] *= -1
235
  df_copy.loc[df_copy['pitcher_hand'] == 'L','x0'] *= -1
236
  df_copy.loc[df_copy['pitcher_hand'] == 'L','spin_direction'] = 360 - df_copy.loc[df_copy['pitcher_hand'] == 'L','spin_direction']
237
 
238
  df_copy['pitch_l'] = [1 if x == 'L' else 0 for x in df_copy['pitcher_hand']]
239
  df_copy['bat_l'] = [1 if x == 'L' else 0 for x in df_copy['batter_hand']]
240
+ df_copy = df_copy[~df_copy.pitch_type.isin(["EP", "PO", "KN", "CS", "SC", "FA"])].reset_index(drop=True)
241
+ df_copy['pitch_type'] = df_copy['pitch_type'].replace({'FT':'SI','KC':'CU','SV':'SL','FO':'FS'})
242
+
243
+ # df_copy['des_new'] = df_copy['play_description'].map(des_dict)
244
+ # df_copy['ev_new'] = df_copy.loc[df_copy['des_new'] == 'hit_into_play','event_type'].map(ev_dict)
245
+ # df_copy.loc[df_copy['des_new']=='hit_into_play','des_new'] = df_copy.loc[df_copy['des_new']=='hit_into_play','ev_new']
246
+ # df_copy = df_copy.dropna(subset=['des_new'])
247
+ # des_values = df_copy.groupby(['des_new'])['delta_run_exp'].mean()
248
+ # df_copy = df_copy.merge(des_values,left_on='des_new',right_on='des_new',suffixes=['','_mean'])
249
+
250
+
251
+
252
+
253
+ # df_copy_fb_sum = df_copy[df_copy.pitch_type.isin(["FF", "FC", "SI"])].groupby(['pitcher_id']).agg(
254
+ # fb_velo = ('start_speed','mean'),
255
+ # fb_max_ivb = ('ivb',percentile(0.9)),
256
+ # fb_max_x = ('hb',percentile(0.9)),
257
+ # fb_min_x = ('hb',percentile(0.1)),
258
+ # fb_max_velo = ('start_speed',percentile(0.9)),
259
+ # fb_axis = ('spin_direction','mean'),
260
+ # )
261
+ df_grouping = df_copy.groupby(['pitcher_id'])[['pitch_type']].value_counts().sort_values(ascending=False).reset_index()
262
+ df_grouping_dupl = df_grouping.drop_duplicates(subset=['pitcher_id'])#.set_index(['pitcher_id','pitch_type'])
263
+
264
+ df_copy_fb_sum = df_copy.groupby(['pitcher_id','pitch_type']).agg(
265
+ primary_mean_velo = ('start_speed','mean'),
266
+ primary_mean_ivb = ('ivb','mean'),
267
+ primary_mean_x = ('hb','mean'),
268
+ # fb_max_velo = ('start_speed','mean'),
269
+ # fb_axis = ('spin_direction','mean'),
270
+ ).reset_index()
271
+
272
+ df_copy_fb_sum = df_grouping_dupl.merge(df_copy_fb_sum,left_on=['pitcher_id','pitch_type'],
273
+ right_on=['pitcher_id','pitch_type']).reset_index()
274
+
275
+ df_copy = df_copy.merge(df_copy_fb_sum,left_on='pitcher_id',right_on='pitcher_id',how='right',suffixes=['','_primary'])
276
+ # return df_copy_fb_sum
277
+
278
+ df_copy['primary_mean_velo_diff'] = df_copy['start_speed']- df_copy['primary_mean_velo']
279
+ df_copy['primary_mean_ivb_diff'] = df_copy['ivb']- df_copy['primary_mean_ivb']
280
+ df_copy['primary_mean_hb_diff'] = abs(df_copy['hb']- df_copy['primary_mean_x'])
281
+ #df_copy['primary_min_hb_diff'] = df_copy['hb']- df_copy['fb_min_x']
282
+ #df_copy['primary_max_velo_diff'] = df_copy['start_speed']- df_copy['fb_max_velo']
283
+ #df_copy['primary_axis_diff'] = df_copy['spin_direction']- df_copy['fb_axis']
284
 
285
  # df_copy.loc[df_copy.pitch_type.isin(["FF", "FC", "SI"]),'fb_velo_diff'] = 0
286
  # df_copy.loc[df_copy.pitch_type.isin(["FF", "FC", "SI"]),'fb_max_ivb_diff'] = 0
 
311
 
312
  # df_copy['vaa'] = np.arctan(df_copy['z_diff'] / df_copy['release_pos_y']) * 360 / np.pi
313
  # df_copy['haa'] = np.arctan(-df_copy['x_diff'] / df_copy['release_pos_y']) * 360 / np.pi
314
+ df_copy['log_extension'] = np.log(df_copy['extension'])
315
+ df_copy = df_copy.dropna(subset=['pitch_type']).fillna(0)
316
  return df_copy
317
 
318
+
319
  ### PITCH COLOURS ###
320
  pitch_colours = {
321
  'Four-Seam Fastball':'#FF007D',#BC136F
 
658
  # ax.set_xlim(left=1)
659
  ax.xaxis.set_major_locator(MaxNLocator(integer=True))
660
 
661
+ ### TJ STUFF+ ROLLING ###
662
+ def tj_velocity_roling(df,
663
+ window,
664
+ ax):
665
+ ## Velocity Plot
666
+ sorted_value_counts = df['pitch_type'].value_counts().sort_values(ascending=False)
667
+
668
+ # Get the list of items ordered from most to least frequent
669
+ items_in_order = sorted_value_counts.index.tolist()
670
+
671
+
672
+ for i in items_in_order:
673
+ if max(df[df['pitch_type']==i]['pitch_type_count_each']) >= window:
674
+ sns.lineplot(x=range(1,max(df[df['pitch_type']==i]['pitch_type_count_each'])+1),
675
+ y=df[df['pitch_type']==i]['start_speed'].rolling(window).sum()/window,
676
+ color=pitch_colours[df[df['pitch_type']==i]['pitch_description'].values[0]],
677
+ ax=ax,linewidth=3)
678
+
679
+ # Adjust x-axis limits to start from 1
680
+ ax.set_xlim(window,max(df['pitch_type_count_each']))
681
+ ax.set_ylim(65,105)
682
+ #ax.get_legend().remove()
683
+ ax.set_xlabel('Pitches', fontdict=font_properties_axes)
684
+ ax.set_ylabel('Velocity (mph)', fontdict=font_properties_axes)
685
+ ax.set_title(f"{window} Pitch Rolling Velocity",fontdict=font_properties_titles)
686
+ # ax.axis('square')
687
+ # ax.set_xlim(left=1)
688
+ ax.xaxis.set_major_locator(MaxNLocator(integer=True))
689
+
690
+
691
  ### BREAK PLOT ###
692
  def break_plot(df,
693
  ax):
 
1091
  if len(pitch_location_group['px'])>0:
1092
  ax.get_legend().remove()
1093
  ax.grid(False)
1094
+ ax.set_title(f"Pitch Locations vs {hand}HH\n{pitch_location_group['pitches'].sum()} Pitches",fontdict=font_properties_titles)
1095
+
1096
+
1097
+
1098
+
1099
+ ####LHH
1100
+ def location_pitch_plot(df,ax,hand):
1101
+ #label_labels = df.sort_values(by=['prop','pitch_type'],ascending=[False,True]).pitch_description.unique()
1102
+ ## Location Plot
1103
+ df_pitch= df[df['batter_hand']==hand]
1104
+ sns.scatterplot(ax=ax,x=df_pitch['px'],
1105
+ y=df_pitch['pz'],
1106
+ hue=df_pitch['pitch_description'],
1107
+ palette=pitch_colours,ec='black',
1108
+ s=175,
1109
+ linewidth=2,
1110
+ zorder=2)
1111
+
1112
+ ax.axis('square')
1113
+ draw_line(ax,alpha_spot=0.75,catcher_p=False)
1114
+ ax.axis('off')
1115
+ ax.set_xlim((-2.75,2.75))
1116
+ ax.set_ylim((-0.5,5))
1117
+ if len(df_pitch['px'])>0:
1118
+ ax.get_legend().remove()
1119
+ ax.grid(False)
1120
+ ax.set_title(f"Pitch Locations vs {hand}HH\n{df_pitch['pitches'].sum()} Pitches",fontdict=font_properties_titles)
stuff_values_20240428.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ # dictionary.py
2
+
3
+ my_dict = {"y_pred_mean":-0.0022258872631937265,
4
+ "y_pred_std": 0.007943911477923393}