File size: 17,243 Bytes
bb22c9e
 
 
 
95914a4
bb22c9e
 
 
 
 
 
 
 
 
 
 
 
 
 
95914a4
8645606
 
bb22c9e
8645606
bb22c9e
 
 
 
 
 
efd6469
bb22c9e
 
efd6469
bb22c9e
 
 
 
 
 
 
efd6469
bb22c9e
 
 
efd6469
bb22c9e
 
 
 
 
 
 
 
95914a4
bb22c9e
 
 
 
 
 
efd6469
bb22c9e
efd6469
95914a4
bb22c9e
 
 
 
 
 
 
df85249
bb22c9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d272dcf
bb22c9e
 
d272dcf
bb22c9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa052a1
bb22c9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8645606
bb22c9e
 
 
 
 
 
 
 
 
f2c2f23
 
be6b33b
 
 
 
 
 
 
 
 
 
f2c2f23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be6b33b
f2c2f23
 
992a5f2
8db62f0
bb22c9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8db62f0
bb22c9e
 
 
 
8db62f0
bb22c9e
 
 
 
 
 
 
aa8877d
bb22c9e
 
 
aa8877d
bb22c9e
 
 
aa8877d
bb22c9e
 
 
aa8877d
bb22c9e
 
 
aa8877d
bb22c9e
 
 
aa8877d
bb22c9e
 
 
aa8877d
 
bb22c9e
 
aa8877d
bb22c9e
 
 
 
 
 
 
 
aa8877d
bb22c9e
 
 
aa8877d
bb22c9e
 
 
aa8877d
bb22c9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8e2bc6
bb22c9e
 
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
##### games.,py #####

# Import modules
from shiny import *
import shinyswatch
#import plotly.express as px
from shinywidgets import output_widget, render_widget
import pandas as pd
from configure import base_url
import math
import datetime
import datasets
from datasets import load_dataset
import numpy as np
import matplotlib
from matplotlib.ticker import MaxNLocator
from matplotlib.gridspec import GridSpec
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

season = 2024

### Import Datasets
dataset = load_dataset('nesticot/mlb_data', data_files=[f'mlb_pitch_data_{season}.csv'])
dataset_train = dataset['train']
df_2023 = dataset_train.to_pandas().set_index(list(dataset_train.features.keys())[0]).reset_index(drop=True)
# Paths to data
### Normalize Hit Locations
df_2023['hit_x'] = df_2023['hit_x'] - 126#df_2023['hit_x'].median()
df_2023['hit_y'] = -df_2023['hit_y']+204.5#df_2023['hit_y'].quantile(0.9999)

df_2023['hit_x_og'] = df_2023['hit_x']
df_2023.loc[df_2023['batter_hand'] == 'R','hit_x'] = -1*df_2023.loc[df_2023['batter_hand'] == 'R','hit_x']

### Calculate Horizontal Launch Angles
df_2023['h_la'] = np.arctan(df_2023['hit_x'] / df_2023['hit_y'])*180/np.pi
conditions_ss = [
    (df_2023['h_la']<-16+5/6),
    (df_2023['h_la']<16+5/6)&(df_2023['h_la']>=-16+5/6),
    (df_2023['h_la']>=16+5/6)
]

choices_ss = ['Oppo','Straight','Pull']
df_2023['traj'] = np.select(conditions_ss, choices_ss, default=np.nan)
df_2023['bip'] = [1 if x > 0  else np.nan for x in df_2023['launch_speed']]

conditions_woba = [
        (df_2023['event_type']=='walk'),
        (df_2023['event_type']=='hit_by_pitch'),
        (df_2023['event_type']=='single'),
        (df_2023['event_type']=='double'),
        (df_2023['event_type']=='triple'),
        (df_2023['event_type']=='home_run'),
    ]

choices_woba = [0.698,
                    0.728,
                    0.887,
                    1.253,
                    1.583,
                    2.027]

df_2023['woba'] = np.select(conditions_woba, choices_woba, default=0)



df_2023_bip = df_2023[~df_2023['bip'].isnull()].dropna(subset=['h_la','launch_angle'])
df_2023_bip['h_la'] = df_2023_bip['h_la'].round(0)


df_2023_bip['season'] = df_2023_bip['game_date'].str[0:4].astype(int)

df_2023_bip = df_2023_bip[df_2023_bip['season'] == season]
df_2022_bip = df_2023_bip[df_2023_bip['season'] == 2022]

batter_dict = df_2023_bip.sort_values('batter_name').set_index('batter_id')['batter_name'].to_dict()





def server(input,output,session):
    @output
    @render.plot(alt="plot")
    @reactive.event(input.go, ignore_none=False)
    def plot():

        batter_id_select = int(input.batter_id())
        df_batter_2023 = df_2023_bip.loc[(df_2023_bip['batter_id'] == batter_id_select)&(df_2023_bip['season']==season)]
        df_batter_2022 = df_2023_bip.loc[(df_2023_bip['batter_id'] == batter_id_select)&(df_2023_bip['season']==2022)]

        df_non_batter_2023 = df_2023_bip.loc[(df_2023_bip['batter_id'] != batter_id_select)&(df_2023_bip['season']==season)]
        df_non_batter_2022 = df_2023_bip.loc[(df_2023_bip['batter_id'] != batter_id_select)&(df_2023_bip['season']==2022)]

        traj_df = df_batter_2023.groupby(['traj'])['launch_speed'].count() / len(df_batter_2023)
        trajectory_df = df_batter_2023.groupby(['trajectory'])['launch_speed'].count() / len(df_batter_2023)#.loc['Oppo']




        colour_palette = ['#FFB000','#648FFF','#785EF0',
                        '#DC267F','#FE6100','#3D1EB2','#894D80','#16AA02','#B5592B','#A3C1ED']
        
        fig = plt.figure(figsize=(10, 10))



        # Create a 2x2 grid of subplots using GridSpec
        gs = GridSpec(3, 3, width_ratios=[0.1,0.8,0.1], height_ratios=[0.1,0.8,0.1])

        # ax00 = fig.add_subplot(gs[0, 0]) 
        ax01 = fig.add_subplot(gs[0, :])  # Subplot at the top-right position
        # ax02 = fig.add_subplot(gs[0, 2])
        # Subplot spanning the entire bottom row
        ax10 = fig.add_subplot(gs[1, 0]) 
        ax11 = fig.add_subplot(gs[1, 1])  # Subplot at the top-right position
        ax12 = fig.add_subplot(gs[1, 2])
        # ax20 = fig.add_subplot(gs[2, 0]) 
        ax21 = fig.add_subplot(gs[2, :])  # Subplot at the top-right position
        # ax22 = fig.add_subplot(gs[2, 2])

        initial_position = ax12.get_position()

        # Change the size of the axis
        # new_width = 0.06  # Set your desired width
        # new_height = 0.4  # Set your desired height
        # new_position = [initial_position.x0-0.01, initial_position.y0+0.065, new_width, new_height]
        # ax12.set_position(new_position)

        cmap_hue = matplotlib.colors.LinearSegmentedColormap.from_list("", [colour_palette[1],'#ffffff',colour_palette[0]])
        # Generate two sets of two-dimensional data
        # data1 = np.random.multivariate_normal([0, 0], [[1, 0.5], [0.5, 1]], 1000)
        # data2 = np.random.multivariate_normal([3, 3], [[1, -0.5], [-0.5, 1]], 1000)
        bat_hand = df_batter_2023.groupby('batter_hand')['launch_speed'].count().sort_values(ascending=False).index[0]

        bat_hand_value = 1

        if bat_hand == 'R':
            bat_hand_value = -1

        kde1_df = df_batter_2023[['h_la','launch_angle']]
        kde1_df['h_la'] = kde1_df['h_la'] * bat_hand_value
        kde2_df = df_non_batter_2023[['h_la','launch_angle']].sample(n=min(50000,len(df_non_batter_2023)), random_state=42)
        kde2_df['h_la'] = kde2_df['h_la'] * bat_hand_value


        # Calculate 2D KDE for each dataset
        kde1 = gaussian_kde(kde1_df.values.T)
        kde2 = gaussian_kde(kde2_df.values.T)

        # Generate a grid of points for evaluation
        x, y = np.meshgrid(np.arange(-45, 46,1 ), np.arange(-30, 61,1 ))
        positions = np.vstack([x.ravel(), y.ravel()])

        # Evaluate the KDEs on the grid
        kde1_values = np.reshape(kde1(positions).T, x.shape)
        kde2_values = np.reshape(kde2(positions).T, x.shape)

        # Subtract one KDE from the other
        result_kde_values = kde1_values - kde2_values

        # Normalize the array to the range [0, 1]
        # result_kde_values = (result_kde_values - np.min(result_kde_values)) / (np.max(result_kde_values) - np.min(result_kde_values))
        result_kde_values = (result_kde_values - np.mean(result_kde_values)) / (np.std(result_kde_values))

        result_kde_values = np.clip(result_kde_values, -3, 3)
        # # Plot the original KDEs
        # plt.contourf(x, y, kde1_values, cmap='Blues', alpha=0.5, levels=20)
        # plt.contourf(x, y, kde2_values, cmap='Reds', alpha=0.5, levels=20)

        # Plot the subtracted KDE
        # Set the number of levels and midrange value
        # Set the number of levels and midrange value
        num_levels = 14
        midrange_value = 0

        # Create a filled contour plot with specified levels
        levels = np.linspace(-3, 3, num_levels)

        batter_plot = ax11.contourf(x, y, result_kde_values, cmap=cmap_hue, levels=levels, vmin=-3, vmax=3)


        ax11.hlines(y=10,xmin=45,xmax=-45,color=colour_palette[3],linewidth=1)
        ax11.hlines(y=25,xmin=45,xmax=-45,color=colour_palette[3],linewidth=1)
        ax11.hlines(y=50,xmin=45,xmax=-45,color=colour_palette[3],linewidth=1)

        ax11.vlines(x=-15,ymin=-30,ymax=60,color=colour_palette[3],linewidth=1)
        ax11.vlines(x=15,ymin=-30,ymax=60,color=colour_palette[3],linewidth=1)
        #ax11.axis('square')
        #ax11.axis('off')
        #ax.hlines(y=10,xmin=-45,xmax=-45)
        # Add labels and legend
        #plt.xlabel('X-axis')
        #plt.ylabel('Y-axis')
        #ax.plot('equal')
        #plt.gca().set_aspect('equal')

        #Choose a mappable (can be any plot or image)
        ax12.set_ylim(0,1)
        cbar = plt.colorbar(batter_plot, cax=ax12, orientation='vertical',shrink=1)
        cbar.set_ticks([])
        # Set the colorbar to have 13 levels
        cbar_locator = MaxNLocator(nbins=13)
        cbar.locator = cbar_locator
        cbar.update_ticks()
        #cbar.set_clim(vmin=-3, vmax=)
        # Set ticks and tick labels
        # cbar.set_ticks(np.linspace(-3, 3, 13))
        # cbar.set_ticklabels(np.linspace(0, 3, 13))
        cbar.set_ticks([])




        ax10.text(s=f"Pop Up\n({trajectory_df.loc['popup']:.1%})",
                x=1,
                y=0.95,va='center',ha='right',fontsize=16)
        # Choose a mappable (can be any plot or image)
        ax10.text(s=f"Fly Ball\n({trajectory_df.loc['fly_ball']:.1%})",
                x=1,
                y=0.75,va='center',ha='right',fontsize=16)

        ax10.text(s=f"Line\nDrive\n({trajectory_df.loc['line_drive']:.1%})",
                x=1,
                y=0.53,va='center',ha='right',fontsize=16)


        ax10.text(s=f"Ground\nBall\n({trajectory_df.loc['ground_ball']:.1%})",
                x=1,
                y=0.23,va='center',ha='right',fontsize=16)
        #ax12.axis(True)
        # Set equal aspect ratio for the contour plot

        if bat_hand == 'R':


            ax21.text(s=f"Pull\n({traj_df.loc['Pull']:.1%})",
                    x=0.2+1/16*0.8,
                    y=1,va='top',ha='center',fontsize=16)

            ax21.text(s=f"Straight\n({traj_df.loc['Straight']:.1%})",
                    x=0.5,
                    y=1,va='top',ha='center',fontsize=16)

            ax21.text(s=f"Oppo\n({traj_df.loc['Oppo']:.1%})",
                    x=0.8-1/16*0.8,
                    y=1,va='top',ha='center',fontsize=16)

        else:

            ax21.text(s=f"Pull\n({traj_df.loc['Pull']:.1%})",
                    x=0.8-1/16*0.8,
                    y=1,va='top',ha='center',fontsize=16)

            ax21.text(s=f"Straight\n({traj_df.loc['Straight']:.1%})",
                    x=0.5,
                    y=1,va='top',ha='center',fontsize=16)

            ax21.text(s=f"Oppo\n({traj_df.loc['Oppo']:.1%})",
                    x=0.2+1/16*0.8,
                    y=1,va='top',ha='center',fontsize=16)
            
        # Define the initial position of the axis

        # Customize colorbar properties
        # cbar = fig.colorbar(orientation='vertical', pad=0.1,ax=ax12)
        #cbar.set_label('Difference', rotation=270, labelpad=15)
        # Show the plot
        # ax21.text(0.0, 0., "By: Thomas Nestico\n      @TJStats",ha='left', va='bottom',fontsize=12)
        # ax21.text(1, 0., "Data: MLB",ha='right', va='bottom',fontsize=12)
        # ax21.text(0.5, 0., "Inspired by @blandalytics",ha='center', va='bottom',fontsize=12)

        # ax00.axis('off')
        ax01.axis('off')
        # ax02.axis('off')
        ax10.axis('off')
        #ax11.axis('off')
        #ax12.axis('off')
        # ax20.axis('off')
        ax21.axis('off')
        # ax22.axis('off')

        ax21.text(0.0, 0., "By: Thomas Nestico\n      @TJStats",ha='left', va='bottom',fontsize=12)
        ax21.text(0.98, 0., "Data: MLB",ha='right', va='bottom',fontsize=12)
        ax21.text(0.5, 0., "Inspired by @blandalytics",ha='center', va='bottom',fontsize=12)


        ax11.set_xticks([])
        ax11.set_yticks([])

        # ax12.text(s='Same',x=np.mean([x for x in ax12.get_xlim()]),y=np.median([x for x in ax12.get_ylim()]),
        #           va='center',ha='center',fontsize=12)

        # ax12.text(s='More\nOften',x=0.5,y=0.74,
        #         va='top',ha='center',fontsize=12)

        ax12.text(s='+3σ',x=0.5,y=3-1/14*3,
                va='center',ha='center',fontsize=12)

        ax12.text(s='+2σ',x=0.5,y=2-1/14*2,
                va='center',ha='center',fontsize=12)

        ax12.text(s='+1σ',x=0.5,y=1-1/14*1,
                va='center',ha='center',fontsize=12)


        ax12.text(s='±0σ',x=0.5,y=0,
                va='center',ha='center',fontsize=12)

        ax12.text(s='-1σ',x=0.5,y=-1-1/14*-1,
                va='center',ha='center',fontsize=12)

        ax12.text(s='-2σ',x=0.5,y=-2-1/14*-2,
                va='center',ha='center',fontsize=12)

        ax12.text(s='-3σ',x=0.5,y=-3-1/14*-3,
                va='center',ha='center',fontsize=12)

        # # ax12.text(s='Less\nOften',x=0.5,y=0.26,
        # #         va='bottom',ha='center',fontsize=12)

        ax01.text(s=f"{df_batter_2023['batter_name'].values[0]}'s {season} Batted Ball Tendencies",
                x=0.5,
                y=0.8,va='top',ha='center',fontsize=20)

        ax01.text(s=f"(Compared to rest of MLB)",
                x=0.5,
                y=0.3,va='top',ha='center',fontsize=16)
        
        #plt.show()


        traj_trajectory_df= df_batter_2023.groupby(['traj','trajectory'])['launch_speed'].count() / len(df_batter_2023)
        
        
        # Define the four indices you want
        desired_traj = ['Pull', 'Straight', 'Oppo']
        trajectory = ['fly_ball', 'ground_ball', 'line_drive', 'popup']
        # Create a MultiIndex from the desired indices
        multi_index = pd.MultiIndex.from_product([desired_traj,trajectory], names=['traj','trajectory'])
        traj_trajectory_df = traj_trajectory_df.reindex(multi_index, fill_value=0)
        
        
        traj_dict = { 'Pull':30,
                                 'Straight':0,
                                 'Oppo':-30}
        
        traj_trajectory_dict = { 'fly_ball':37.5,
                                 'ground_ball':-10,
                                 'line_drive':17.5,
                                 'popup':55}
        
        for i in traj_dict:
            for j in traj_trajectory_dict:
                if bat_hand == 'R':
                        ax11.text(s=f"{traj_trajectory_df.loc[i,j]:.1%}",
                                x=-traj_dict[i],
                                y=traj_trajectory_dict[j],va='center',ha='center',fontsize=12,alpha=0.7)
                else:
                        ax11.text(s=f"{traj_trajectory_df.loc[i,j]:.1%}",
                                x=traj_dict[i],
                                y=traj_trajectory_dict[j],va='center',ha='center',fontsize=12,alpha=0.7)      


app = App(ui.page_fluid(
#    ui.tags.base(href=base_url),
    ui.tags.div(
         {"style": "width:90%;margin: 0 auto;max-width: 1600px;"},
        ui.tags.style(
            """
            h4 {
                margin-top: 1em;font-size:35px;
            }
            h2{
                font-size:25px;
            }
            """
         ),
    shinyswatch.theme.simplex(),
    ui.tags.h4("TJStats"),
    ui.tags.i("Baseball Analytics and Visualizations"),
    ui.markdown("""<a href='https://www.patreon.com/tj_stats'>Support me on Patreon for Access to 2024 Apps</a><sup>1</sup>"""),

    ui.navset_tab(
        ui.nav_control(
             ui.a(
                "Home",
                href="https://nesticot-tjstats-site.hf.space/home/"
            ),
        ),
        ui.nav_menu(
            "Batter Charts",
            ui.nav_control(
            ui.a(
                "Batting Rolling",
                href="https://nesticot-tjstats-site-rolling-batter.hf.space/"
            ),
            ui.a(
                "Spray",
                href="https://nesticot-tjstats-site-spray.hf.space/"
            ),
            ui.a(
                "Decision Value",
                href="https://nesticot-tjstats-site-decision-value.hf.space/"
            ),
            ui.a(
                "Damage Model",
                href="https://nesticot-tjstats-site-damage.hf.space/"
            ),
            ui.a(
                "Batter Scatter",
                href="https://nesticot-tjstats-site-batter-scatter.hf.space/"
            ),
            ui.a(
                "EV vs LA Plot",
                href="https://nesticot-tjstats-site-ev-angle.hf.space/"
            ),
            ui.a(
                "Statcast Compare",
                href="https://nesticot-tjstats-site-statcast-compare.hf.space/"
            ),
            ui.a(
                "MLB/MiLB Cards",
                href="https://nesticot-tjstats-site-mlb-cards.hf.space/"
            )
        ),
        ),
        ui.nav_menu(
            "Pitcher Charts",
            ui.nav_control(
             ui.a(
                "Pitcher Rolling",
                href="https://nesticot-tjstats-site-rolling-pitcher.hf.space/"
            ),
             ui.a(
                "Pitcher Summary",
                href="https://nesticot-tjstats-site-pitching-summary-graphic-new.hf.space/"
            ),
             ui.a(
                "Pitcher Scatter",
                href="https://nesticot-tjstats-site-pitcher-scatter.hf.space"
            )
        ),
        )),ui.row(
    ui.layout_sidebar(
        
        ui.panel_sidebar(
                ui.input_select("batter_id",
                                "Select Batter",
                                 batter_dict,
                                 width=1,
                                 size=1,
                                 selectize=True),
                ui.input_action_button("go", "Generate",class_="btn-primary",
                                       )),

   ui.panel_main(     
        ui.navset_tab(

            ui.nav("2024 vs MLB",
                   ui.output_plot('plot',
                                  width='1000px',
                                  height='1000px')),
        ))
    )),)),server)