nesticot commited on
Commit
5bc5447
1 Parent(s): 34dcd5a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -35
app.py CHANGED
@@ -17,7 +17,7 @@ colour_palette = ['#FFB000','#648FFF','#785EF0',
17
  import datasets
18
  from datasets import load_dataset
19
  ### Import Datasets
20
- dataset = load_dataset('nesticot/mlb_data', data_files=['mlb_pitch_data_2024.csv' ])
21
  dataset_train = dataset['train']
22
  df_2024 = dataset_train.to_pandas().set_index(list(dataset_train.features.keys())[0]).reset_index(drop=True).drop_duplicates(subset=['play_id'],keep='last')
23
 
@@ -59,17 +59,17 @@ spring_teams = df_2024.groupby(['pitcher_id']).tail(1)[['pitcher_id','pitcher_te
59
  season_start = '2024-03-20'
60
  season_end = '2024-09-29'
61
  season_fg=2024
62
- chad_fg = requests.get(f'https://www.fangraphs.com/api/leaders/major-league/data?age=&pos=all&stats=pit&lg=all&qual=0&season={season_fg}&season={season_fg}&month=1000&season1={season_fg}&ind=0&pageitems=2000000000&pagenum=1&ind=0&rost=0&players=&type=36&postseason=&sortdir=default&sortstat=sp_pitching').json()
63
 
64
 
65
- chadwick_df_small = pd.DataFrame(data={
66
- 'key_mlbam':[x['xMLBAMID'] for x in chad_fg['data']],
67
- 'key_fangraphs':[x['playerid'] for x in chad_fg['data']],
68
- 'Name':[x['PlayerName'] for x in chad_fg['data']],
69
- })
70
 
71
- pitcher_dicts = chadwick_df_small.set_index('key_mlbam')['Name'].sort_values().to_dict()
72
- mlb_fg_dicts = chadwick_df_small.set_index('key_mlbam')['key_fangraphs'].sort_values().to_dict()
73
 
74
 
75
  statcast_pitch_summary = pd.read_csv('statcast_pitch_summary.csv')
@@ -403,16 +403,50 @@ def server(input, output, session):
403
  font_properties_titles = {'family': 'calibi', 'size': 20}
404
  font_properties_axes = {'family': 'calibi', 'size': 16}
405
 
406
- ## FANGRAPHS TABLE ###
407
- data_pull = psf.fangraphs_scrape(pitcher_id=pitcher_id_select,
408
- split=input.split_id(),
409
- start_date=input.date_range_id()[0],
410
- end_date=input.date_range_id()[1])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
411
 
412
 
413
- psf.fangraphs_table(data=data_pull,
414
- stats=['IP','WHIP','ERA','FIP','TBF','K%','BB%','K-BB%'],
415
- ax=ax1_table)
416
 
417
  for x,y,z in zip([input.plot_id_1(),input.plot_id_2(),input.plot_id_3()],[ax2_left,ax2_middle,ax2_right],[1,2,3]):
418
  if x == 'velocity_kde':
@@ -481,17 +515,21 @@ def server(input, output, session):
481
 
482
 
483
  ax0.text(x=0.5,y=0.8,s=title_spot,fontname='Calibri',ha='center',fontsize=56,va='top')
484
- ax0.text(x=0.5,y=0.5,s='Season Pitching Summary',fontname='Calibri',ha='center',fontsize=40,va='top',fontstyle='italic')
485
 
 
 
 
 
486
 
487
  #ax0.text(x=0.5,y=0.25,s=f'2024 Spring Training',fontname='Calibri',ha='center',fontsize=30,va='top')
488
  # ax0.text(x=0.5,y=0.25,s=f'{season_fg} MLB Season',fontname='Calibri',ha='center',fontsize=30,va='top')
489
  # ax0.axis('off')
490
 
491
 
492
- ax0.text(x=0.5,y=0.25,s=f'{input.date_range_id()[0]} to {input.date_range_id()[1]}',fontname='Calibri',ha='center',fontsize=30,va='top',fontstyle='italic')
493
 
494
- ax0.text(x=0.5,y=0.05,s=f'{ball_title}{strike_title}{split_title}',fontname='Calibri',ha='center',fontsize=20,va='top')
495
  ax0.axis('off')
496
 
497
  from matplotlib.offsetbox import (OffsetImage, AnnotationBbox)
@@ -501,15 +539,16 @@ def server(input, output, session):
501
  from urllib.error import HTTPError
502
 
503
  try:
504
- url = f'https://img.mlbstatic.com/mlb-photos/image/upload/d_people:generic:headshot:67:current.png/w_213,q_auto:best/v1/people/{pitcher_id_select}/headshot/67/current.png'
505
  test_mage = plt.imread(url)
506
  except urllib.error.HTTPError as err:
507
  url = f'https://img.mlbstatic.com/mlb-photos/image/upload/d_people:generic:headshot:67:current.png/w_213,q_auto:best/v1/people/1/headshot/67/current.png'
 
508
  imagebox = OffsetImage(test_mage, zoom = 0.5)
509
  ab = AnnotationBbox(imagebox, (0.125, 0.4), frameon = False)
510
  ax0.add_artist(ab)
511
 
512
- player_bio = requests.get(url=f"https://statsapi.mlb.com/api/v1/people?personIds={pitcher_id_select}&hydrate=currentTeam").json()
513
 
514
 
515
 
@@ -667,16 +706,49 @@ def server(input, output, session):
667
  font_properties_axes = {'family': 'calibi', 'size': 16}
668
 
669
  print(df_2024_update['game_date'].values[0])
670
- ## FANGRAPHS TABLE ###
671
- data_pull = psf.fangraphs_scrape(pitcher_id=pitcher_id_select,
672
- split=input.split_id(),
673
- start_date=df_plot['game_date'].values[0],
674
- end_date=df_plot['game_date'].values[0])
 
 
 
 
675
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
676
 
677
- psf.fangraphs_table(data=data_pull,
678
- stats=['IP','WHIP','ERA','FIP','TBF','K%','BB%','K-BB%'],
679
- ax=ax1_table)
680
 
681
  # psf.velocity_kdes(df=df_plot,
682
  # ax=ax2_loc,
@@ -746,7 +818,7 @@ def server(input, output, session):
746
 
747
 
748
  ax0.text(x=0.5,y=0.8,s=title_spot,fontname='Calibri',ha='center',fontsize=56,va='top')
749
- ax0.text(x=0.5,y=0.5,s='Game Pitching Summary',fontname='Calibri',ha='center',fontsize=40,va='top',fontstyle='italic')
750
 
751
 
752
  #ax0.text(x=0.5,y=0.25,s=f'2024 Spring Training',fontname='Calibri',ha='center',fontsize=30,va='top')
@@ -754,9 +826,9 @@ def server(input, output, session):
754
  #ax0.text(x=0.5,y=0.25,s=f'2024 Spring Training',fontname='Calibri',ha='center',fontsize=30,va='top')
755
  # ax0.text(x=0.5,y=0.25,s=f'{season_fg} MLB Season',fontname='Calibri',ha='center',fontsize=30,va='top')
756
 
757
- ax0.text(x=0.5,y=0.25,s= df_plot['game_opp'].values[0],fontname='Calibri',ha='center',fontstyle='italic',fontsize=30,va='top')
758
 
759
- ax0.text(x=0.5,y=0.05,s=f'{ball_title}{strike_title}{split_title}',fontname='Calibri',ha='center',fontsize=20,va='top')
760
  ax0.axis('off')
761
 
762
 
@@ -767,16 +839,21 @@ def server(input, output, session):
767
  from urllib.error import HTTPError
768
 
769
  try:
770
- url = f'https://img.mlbstatic.com/mlb-photos/image/upload/d_people:generic:headshot:67:current.png/w_213,q_auto:best/v1/people/{pitcher_id_select}/headshot/67/current.png'
771
  test_mage = plt.imread(url)
772
  except urllib.error.HTTPError as err:
773
  url = f'https://img.mlbstatic.com/mlb-photos/image/upload/d_people:generic:headshot:67:current.png/w_213,q_auto:best/v1/people/1/headshot/67/current.png'
 
774
  imagebox = OffsetImage(test_mage, zoom = 0.5)
775
  ab = AnnotationBbox(imagebox, (0.125, 0.4), frameon = False)
776
  ax0.add_artist(ab)
777
 
778
- player_bio = requests.get(url=f"https://statsapi.mlb.com/api/v1/people?personIds={pitcher_id_select}&hydrate=currentTeam").json()
779
 
 
 
 
 
780
 
781
 
782
 
 
17
  import datasets
18
  from datasets import load_dataset
19
  ### Import Datasets
20
+ dataset = load_dataset('nesticot/mlb_data', data_files=['aaa_pitch_data_2024.csv' ])
21
  dataset_train = dataset['train']
22
  df_2024 = dataset_train.to_pandas().set_index(list(dataset_train.features.keys())[0]).reset_index(drop=True).drop_duplicates(subset=['play_id'],keep='last')
23
 
 
59
  season_start = '2024-03-20'
60
  season_end = '2024-09-29'
61
  season_fg=2024
62
+ #chad_fg = requests.get(f'https://www.fangraphs.com/api/leaders/major-league/data?age=&pos=all&stats=pit&lg=all&qual=0&season={season_fg}&season={season_fg}&month=1000&season1={season_fg}&ind=0&pageitems=2000000000&pagenum=1&ind=0&rost=0&players=&type=36&postseason=&sortdir=default&sortstat=sp_pitching').json()
63
 
64
 
65
+ # chadwick_df_small = pd.DataFrame(data={
66
+ # 'key_mlbam':[x['xMLBAMID'] for x in chad_fg['data']],
67
+ # 'key_fangraphs':[x['playerid'] for x in chad_fg['data']],
68
+ # 'Name':[x['PlayerName'] for x in chad_fg['data']],
69
+ # })
70
 
71
+ pitcher_dicts = df_2024.set_index('pitcher_id')['pitcher_name'].sort_values().to_dict()
72
+ # mlb_fg_dicts = chadwick_df_small.set_index('key_mlbam')['key_fangraphs'].sort_values().to_dict()
73
 
74
 
75
  statcast_pitch_summary = pd.read_csv('statcast_pitch_summary.csv')
 
403
  font_properties_titles = {'family': 'calibi', 'size': 20}
404
  font_properties_axes = {'family': 'calibi', 'size': 16}
405
 
406
+ # ## FANGRAPHS TABLE ###
407
+ # data_pull = psf.fangraphs_scrape(pitcher_id=pitcher_id_select,
408
+ # split=input.split_id(),
409
+ # start_date=input.date_range_id()[0],
410
+ # end_date=input.date_range_id()[1])
411
+
412
+
413
+ # psf.fangraphs_table(data=data_pull,
414
+ # stats=['IP','WHIP','ERA','FIP','TBF','K%','BB%','K-BB%'],
415
+ # ax=ax1_table)
416
+
417
+ start_date = str(pd.to_datetime(input.date_range_id()[0]).strftime('%m/%d/%Y'))
418
+ end_date = str(pd.to_datetime(input.date_range_id()[1]).strftime('%m/%d/%Y'))
419
+
420
+
421
+ pitcher_stats_call = requests.get(f'https://statsapi.mlb.com/api/v1/people/{pitcher_id_select}?appContext=minorLeague&hydrate=stats(group=[pitching],type=[byDateRange],sportId=11,startDate={start_date},endDate={end_date})').json()
422
+
423
+ pitcher_stats_call_header = [x for x in pitcher_stats_call['people'][0]['stats'][0]['splits'][0]['stat']]
424
+ pitcher_stats_call_values = [pitcher_stats_call['people'][0]['stats'][0]['splits'][0]['stat'][x] for x in pitcher_stats_call['people'][0]['stats'][0]['splits'][0]['stat']]
425
+ pitcher_stats_call_df = pd.DataFrame(data=dict(zip(pitcher_stats_call_header,pitcher_stats_call_values)),index=[0])
426
+ pitcher_stats_call_df['k_percent'] = pitcher_stats_call_df['strikeOuts']/pitcher_stats_call_df['battersFaced']
427
+ pitcher_stats_call_df['bb_percent'] = pitcher_stats_call_df['baseOnBalls']/pitcher_stats_call_df['battersFaced']
428
+ pitcher_stats_call_df['k_bb_percent'] = pitcher_stats_call_df['k_percent']-pitcher_stats_call_df['bb_percent']
429
+ pitcher_stats_call_df_small = pitcher_stats_call_df[['inningsPitched','battersFaced','era','whip','k_percent','bb_percent','k_bb_percent']]
430
+
431
+ pitcher_stats_call_df_small['k_percent'] = pitcher_stats_call_df_small['k_percent'].astype(float).apply(lambda x: '{:.1%}'.format(x))
432
+ pitcher_stats_call_df_small['bb_percent'] = pitcher_stats_call_df_small['bb_percent'].astype(float).apply(lambda x: '{:.1%}'.format(x))
433
+ pitcher_stats_call_df_small['k_bb_percent'] = pitcher_stats_call_df_small['k_bb_percent'].astype(float).apply(lambda x: '{:.1%}'.format(x))
434
+
435
+ table_fg = ax1_table.table(cellText=pitcher_stats_call_df_small.values, colLabels=pitcher_stats_call_df_small.columns, cellLoc='center',
436
+ bbox=[0.04, 0.2, 0.92, 0.8])
437
+
438
+ min_font_size = 20
439
+ table_fg.set_fontsize(min_font_size)
440
+
441
+
442
+ new_column_names = ['$\\bf{IP}$','$\\bf{PA}$','$\\bf{ERA}$','$\\bf{WHIP}$','$\\bf{K\%}$','$\\bf{BB\%}$','$\\bf{K-BB\%}$']
443
+ # #new_column_names = ['Pitch Name', 'Pitch%', 'Velocity', 'Spin Rate','Exit Velocity', 'Whiff%', 'CSW%']
444
+ for i, col_name in enumerate(new_column_names):
445
+ table_fg.get_celld()[(0, i)].get_text().set_text(col_name)
446
+
447
+ ax1_table.axis('off')
448
 
449
 
 
 
 
450
 
451
  for x,y,z in zip([input.plot_id_1(),input.plot_id_2(),input.plot_id_3()],[ax2_left,ax2_middle,ax2_right],[1,2,3]):
452
  if x == 'velocity_kde':
 
515
 
516
 
517
  ax0.text(x=0.5,y=0.8,s=title_spot,fontname='Calibri',ha='center',fontsize=56,va='top')
518
+ ax0.text(x=0.5,y=0.35,s='AAA Season Pitching Summary',fontname='Calibri',ha='center',fontsize=40,va='top',fontstyle='italic')
519
 
520
+ player_bio = requests.get(url=f"https://statsapi.mlb.com/api/v1/people?personIds={pitcher_id_select}&hydrate=currentTeam").json()
521
+ #ax0.text(x=0.5,y=0.05,s=f'{ball_title}{strike_title}{split_title}',fontname='Calibri',ha='center',fontsize=20,va='top')
522
+ ax0.axis('off')
523
+ ax0.text(x=0.5,y=0.5,s=f"{ player_bio['people'][0]['pitchHand']['code']}HP, Age: {player_bio['people'][0]['currentAge']}, {player_bio['people'][0]['height']}/{player_bio['people'][0]['weight']}",fontname='Calibri',ha='center',fontsize=24,va='top')
524
 
525
  #ax0.text(x=0.5,y=0.25,s=f'2024 Spring Training',fontname='Calibri',ha='center',fontsize=30,va='top')
526
  # ax0.text(x=0.5,y=0.25,s=f'{season_fg} MLB Season',fontname='Calibri',ha='center',fontsize=30,va='top')
527
  # ax0.axis('off')
528
 
529
 
530
+ ax0.text(x=0.5,y=0.15,s=f'{input.date_range_id()[0]} to {input.date_range_id()[1]}',fontname='Calibri',ha='center',fontsize=30,va='top',fontstyle='italic')
531
 
532
+ ax0.text(x=0.5,y=0.0,s=f'{ball_title}{strike_title}{split_title}',fontname='Calibri',ha='center',fontsize=20,va='top')
533
  ax0.axis('off')
534
 
535
  from matplotlib.offsetbox import (OffsetImage, AnnotationBbox)
 
539
  from urllib.error import HTTPError
540
 
541
  try:
542
+ url = f'https://img.mlbstatic.com/mlb-photos/image/upload/c_fill,g_auto/w_180/v1/people/{pitcher_id_select}/headshot/milb/current.png'
543
  test_mage = plt.imread(url)
544
  except urllib.error.HTTPError as err:
545
  url = f'https://img.mlbstatic.com/mlb-photos/image/upload/d_people:generic:headshot:67:current.png/w_213,q_auto:best/v1/people/1/headshot/67/current.png'
546
+ test_mage = plt.imread(url)
547
  imagebox = OffsetImage(test_mage, zoom = 0.5)
548
  ab = AnnotationBbox(imagebox, (0.125, 0.4), frameon = False)
549
  ax0.add_artist(ab)
550
 
551
+ #player_bio = requests.get(url=f"https://statsapi.mlb.com/api/v1/people?personIds={pitcher_id_select}&hydrate=currentTeam").json()
552
 
553
 
554
 
 
706
  font_properties_axes = {'family': 'calibi', 'size': 16}
707
 
708
  print(df_2024_update['game_date'].values[0])
709
+ # ## FANGRAPHS TABLE ###
710
+ # data_pull = psf.fangraphs_scrape(pitcher_id=pitcher_id_select,
711
+ # split=input.split_id(),
712
+ # start_date=df_plot['game_date'].values[0],
713
+ # end_date=df_plot['game_date'].values[0])
714
+
715
+ start_date = str(pd.to_datetime(df_plot['game_date'].values[0]).strftime('%m/%d/%Y'))
716
+ end_date = str(pd.to_datetime(df_plot['game_date'].values[0]).strftime('%m/%d/%Y'))
717
+
718
 
719
+ pitcher_stats_call = requests.get(f'https://statsapi.mlb.com/api/v1/people/{pitcher_id_select}?appContext=minorLeague&hydrate=stats(group=[pitching],type=[byDateRange],sportId=11,startDate={start_date},endDate={end_date})').json()
720
+
721
+ pitcher_stats_call_header = [x for x in pitcher_stats_call['people'][0]['stats'][0]['splits'][0]['stat']]
722
+ pitcher_stats_call_values = [pitcher_stats_call['people'][0]['stats'][0]['splits'][0]['stat'][x] for x in pitcher_stats_call['people'][0]['stats'][0]['splits'][0]['stat']]
723
+ pitcher_stats_call_df = pd.DataFrame(data=dict(zip(pitcher_stats_call_header,pitcher_stats_call_values)),index=[0])
724
+ # pitcher_stats_call_df['k_percent'] = pitcher_stats_call_df['strikeOuts']/pitcher_stats_call_df['battersFaced']
725
+ # pitcher_stats_call_df['bb_percent'] = pitcher_stats_call_df['baseOnBalls']/pitcher_stats_call_df['battersFaced']
726
+ # pitcher_stats_call_df['k_bb_percent'] = pitcher_stats_call_df['k_percent']-pitcher_stats_call_df['bb_percent']
727
+ pitcher_stats_call_df_small = pitcher_stats_call_df[['inningsPitched','battersFaced','earnedRuns','hits','strikeOuts','baseOnBalls','hitByPitch','homeRuns']]
728
+ pitcher_stats_call_df_small['whiffs'] = int(df_plot['is_whiff'].sum())
729
+ # pitcher_stats_call_df_small['k_percent'] = pitcher_stats_call_df_small['k_percent'].astype(float).apply(lambda x: '{:.1%}'.format(x))
730
+ # pitcher_stats_call_df_small['bb_percent'] = pitcher_stats_call_df_small['bb_percent'].astype(float).apply(lambda x: '{:.1%}'.format(x))
731
+ # pitcher_stats_call_df_small['k_bb_percent'] = pitcher_stats_call_df_small['k_bb_percent'].astype(float).apply(lambda x: '{:.1%}'.format(x))
732
+
733
+ table_fg = ax1_table.table(cellText=pitcher_stats_call_df_small.values, colLabels=pitcher_stats_call_df_small.columns, cellLoc='center',
734
+ bbox=[0.04, 0.2, 0.92, 0.8])
735
+
736
+ min_font_size = 20
737
+ table_fg.set_fontsize(min_font_size)
738
+
739
+
740
+ new_column_names = ['$\\bf{IP}$','$\\bf{PA}$','$\\bf{ER}$','$\\bf{H}$','$\\bf{K}$','$\\bf{BB}$','$\\bf{HBP}$','$\\bf{HR}$','$\\bf{Whiffs}$']
741
+ # #new_column_names = ['Pitch Name', 'Pitch%', 'Velocity', 'Spin Rate','Exit Velocity', 'Whiff%', 'CSW%']
742
+ for i, col_name in enumerate(new_column_names):
743
+ table_fg.get_celld()[(0, i)].get_text().set_text(col_name)
744
+
745
+ ax1_table.axis('off')
746
+
747
+
748
+ # psf.fangraphs_table(data=data_pull,
749
+ # stats=['IP','WHIP','ERA','FIP','TBF','K%','BB%','K-BB%'],
750
+ # ax=ax1_table)
751
 
 
 
 
752
 
753
  # psf.velocity_kdes(df=df_plot,
754
  # ax=ax2_loc,
 
818
 
819
 
820
  ax0.text(x=0.5,y=0.8,s=title_spot,fontname='Calibri',ha='center',fontsize=56,va='top')
821
+ ax0.text(x=0.5,y=0.35,s='AAA Game Pitching Summary',fontname='Calibri',ha='center',fontsize=40,va='top',fontstyle='italic')
822
 
823
 
824
  #ax0.text(x=0.5,y=0.25,s=f'2024 Spring Training',fontname='Calibri',ha='center',fontsize=30,va='top')
 
826
  #ax0.text(x=0.5,y=0.25,s=f'2024 Spring Training',fontname='Calibri',ha='center',fontsize=30,va='top')
827
  # ax0.text(x=0.5,y=0.25,s=f'{season_fg} MLB Season',fontname='Calibri',ha='center',fontsize=30,va='top')
828
 
829
+ ax0.text(x=0.5,y=0.15,s= df_plot['game_opp'].values[0],fontname='Calibri',ha='center',fontstyle='italic',fontsize=30,va='top')
830
 
831
+ ax0.text(x=0.5,y=0.00,s=f'{ball_title}{strike_title}{split_title}',fontname='Calibri',ha='center',fontsize=20,va='top')
832
  ax0.axis('off')
833
 
834
 
 
839
  from urllib.error import HTTPError
840
 
841
  try:
842
+ url = f'https://img.mlbstatic.com/mlb-photos/image/upload/c_fill,g_auto/w_180/v1/people/{pitcher_id_select}/headshot/milb/current.png'
843
  test_mage = plt.imread(url)
844
  except urllib.error.HTTPError as err:
845
  url = f'https://img.mlbstatic.com/mlb-photos/image/upload/d_people:generic:headshot:67:current.png/w_213,q_auto:best/v1/people/1/headshot/67/current.png'
846
+ test_mage = plt.imread(url)
847
  imagebox = OffsetImage(test_mage, zoom = 0.5)
848
  ab = AnnotationBbox(imagebox, (0.125, 0.4), frameon = False)
849
  ax0.add_artist(ab)
850
 
851
+ #player_bio = requests.get(url=f"https://statsapi.mlb.com/api/v1/people?personIds={pitcher_id_select}&hydrate=currentTeam").json()
852
 
853
+ player_bio = requests.get(url=f"https://statsapi.mlb.com/api/v1/people?personIds={pitcher_id_select}&hydrate=currentTeam").json()
854
+ #ax0.text(x=0.5,y=0.05,s=f'{ball_title}{strike_title}{split_title}',fontname='Calibri',ha='center',fontsize=20,va='top')
855
+ ax0.axis('off')
856
+ ax0.text(x=0.5,y=0.5,s=f"{ player_bio['people'][0]['pitchHand']['code']}HP, Age: {player_bio['people'][0]['currentAge']}, {player_bio['people'][0]['height']}/{player_bio['people'][0]['weight']}",fontname='Calibri',ha='center',fontsize=24,va='top')
857
 
858
 
859