Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###############################################################################
- # Generate images for a GIF for the movement of the pitch based on swing/take #
- ###############################################################################
- #############
- # LIBRARIES #
- #############
- library(RODBC);
- library(fields);
- library(diagram);
- library(calibrate);
- ####################
- # ADDITIONAL FILES #
- ####################
- source("Movement Scripts/Mvt_Fcns.r");
- ########
- # MAIN #
- ########
- # Set the pitcher name
- first <- 'Julio';
- last <- 'Urias';
- # Set the pitch type based on the two-letter designation
- pitch_type <- 'CU';
- # Set the handedness of the batter
- stand <- 'R';
- # Set the year
- year <- '2016';
- # Set up the database name
- database <- paste("MLB",year,sep="");
- # Set the number of frames
- Ny <- 30;
- # Set the increment in the y-direction
- y_seq <- seq(55,17/12,length=Ny);
- # Set the movement type
- # 1 <- PITCHf/x movement
- # 2 <- PITCHf/x movement without drag
- # 3 <- planar movement
- # 4 <- planar movement without drag
- mvt_type <- 1;
- # Set the descriptor based on the type of movement
- mvt_desc <- switch(mvt_type,'PFX','PFX D','W','W D');
- # Set the quadrilateral core of the strike zone
- if (stand == 'L'){
- UL <- c(-0.47,2.85);
- UR <- c(0.22,2.87);
- LL <- c(-0.5,2.04);
- LR <- c(0.24,2.16);
- } else {
- UL <- c(-0.38,2.88);
- UR <- c(0.35,2.81);
- LL <- c(-0.46,2.1);
- LR <- c(0.35,2.09);
- }
- # Associate the pitch type with its full name
- pitch_name <- switch(pitch_type,
- FA = 'Fastballs',
- FF = 'Four-Seam FBs',
- FT = 'Two-Seam FBs',
- FC = 'Cut Fastballs',
- FS = 'Split-Finger FBs',
- FO = 'Forkballs',
- SI = 'Sinkers',
- SL = 'Sliders',
- CU = 'Curveballs',
- KC = 'Knuckle Curves',
- EP = 'Ephuuses',
- CH = 'Changeups',
- SC = 'Screwballs',
- KN = 'Knuckleballs',
- UN = 'Unknown Pitches');
- # Set the cases where a batter has swung at a pitch
- swing_cases <- "(des = 'In play, no out' OR
- des = 'In play, out(s)' OR
- des = 'In play, run(s)' OR
- des = 'Foul' OR
- des = 'Foul (Runner Going)' OR
- des = 'Foul Bunt' OR
- des = 'Foul Tip' OR
- des = 'Swinging Strike' OR
- des = 'Swinging Pitchout' OR
- des = 'Missed Bunt' OR
- des = 'Swinging Strike (Blocked)')";
- # Set the cases where a batter has taken a pitch
- take_cases <- "(des = 'Called Strike' OR
- des = 'Ball' OR
- des = 'Ball in Dirt' OR
- des = 'Hit By Pitch')";
- # Open the channel
- channel <- odbcConnect(database);
- # Set the query for swings
- Swing_Query = paste("SELECT x0, y0, z0, vx0, vy0, vz0, ax, ay, az
- FROM pitches WHERE pitch_type = '",pitch_type,"' AND
- ",swing_cases," AND ab_id IN
- (SELECT ab_id FROM atbats WHERE
- pitcher = (SELECT eliasid FROM players WHERE first = '",
- first,"' AND last = '",last,"') AND stand = '",stand,"')
- ",sep="");
- # Download the swing data
- Swing <- sqlQuery(channel,Swing_Query);
- # Close the channel
- close(channel);
- # Project the swing data
- if (mvt_type == 1){
- Swing_xz <- Pitch_Project_PFX(Swing,Ny,stand);
- } else if (mvt_type == 2){
- Swing_xz <- Pitch_Project_PFX_Drag(Swing,Ny,stand);
- } else if (mvt_type == 3){
- Swing_xz <- Pitch_Project_W(Swing,Ny,stand);
- } else {
- Swing_xz <- Pitch_Project_W_Drag(Swing,Ny,stand);
- }
- # Set the x and z coordinates of the swing projections
- x_S <- Swing_xz$x_PL;
- z_S <- Swing_xz$z_PL;
- # Open the channel
- channel <- odbcConnect(database);
- # Set the query for taken pitches
- Take_Query = paste("SELECT x0, y0, z0, vx0, vy0, vz0, ax, ay, az
- FROM pitches WHERE pitch_type = '",pitch_type,"' AND
- ",take_cases," AND ab_id IN
- (SELECT ab_id FROM atbats WHERE
- pitcher = (SELECT eliasid FROM players WHERE first = '",
- first,"' AND last = '",last,"') AND stand = '",stand,"')
- ",sep="");
- # Download the take data
- Take <- sqlQuery(channel,Take_Query);
- # Close the channel
- close(channel);
- # Project the take data
- if (mvt_type == 1){
- Take_xz <- Pitch_Project_PFX(Take,Ny,stand);
- } else if (mvt_type == 2){
- Take_xz <- Pitch_Project_PFX_Drag(Take,Ny,stand);
- } else if (mvt_type == 3){
- Take_xz <- Pitch_Project_W(Take,Ny,stand);
- } else {
- Take_xz <- Pitch_Project_W_Drag(Take,Ny,stand);
- }
- # Set the x and z coordinates of the taken projections
- x_T <- Take_xz$x_PL;
- z_T <- Take_xz$z_PL;
- # Find the ball/strike indices
- Swing_Ball_Str <- ball_strike_prob(Swing,stand);
- Take_Ball_Str <- ball_strike_prob(Take,stand);
- # Parse the indices
- S_Str_ind <- Swing_Ball_Str$S;
- S_Ball_ind <- Swing_Ball_Str$B;
- T_Str_ind <- Take_Ball_Str$S;
- T_Ball_ind <- Take_Ball_Str$B;
- # Set up an array to draw a strike zone contour
- dx <- 0.005;
- X <- seq(-3+dx/2,3-dx/2,length=1200);
- Y <- seq(dx/2,6-dx/2,length=1200);
- k_sz <- 4;
- Str_Prb_Array <- array(0,dim=c(1200,1200));
- for (i in 1:1200) {
- for (j in 1:1200){
- P <- c(X[i],Y[j]);
- d_sz <- Min_Dist_Quad(P,UL,UR,LL,LR);
- Str_Prb_Array[i,j] <- exp(-k_sz*d_sz^4);
- }
- }
- # Generate the images
- for (j in 1:Ny){
- if (j < 10){
- filename <- paste(last,"_",pitch_type,"_ST_",stand,"_0",j,".jpg",sep="");
- }
- else {
- filename <- paste(last,"_",pitch_type,"_ST_",stand,"_",j,".jpg",sep="");
- }
- jpeg(filename);
- plot(x_S[S_Str_ind,j],z_S[S_Str_ind,j],pch=20,col="green",xlim=c(-3,3),ylim=c(0,6),
- xlab='Horizontal Location (Feet)',ylab='Vertical Location (Feet)',
- main=paste(first," ",last,": ",pitch_name," v. ",stand,"HB at ",round(y_seq[j],digits=2)," Feet (",mvt_desc,")",sep=""),
- xaxs='i',yaxs='i');
- par(new=TRUE);
- plot(x_S[S_Ball_ind,j],z_S[S_Ball_ind,j],pch=20,col="blue",xlim=c(-3,3),ylim=c(0,6),
- xlab='',ylab='',xaxs='i',yaxs='i');
- par(new=TRUE);
- plot(x_T[T_Str_ind,j],z_T[T_Str_ind,j],pch=20,col="red",xlim=c(-3,3),ylim=c(0,6),
- xlab='',ylab='',xaxs='i',yaxs='i');
- par(new=TRUE);
- plot(x_T[T_Ball_ind,j],z_T[T_Ball_ind,j],pch=20,col="darkorange",xlim=c(-3,3),ylim=c(0,6),
- xlab='',ylab='',xaxs='i',yaxs='i');
- par(new=TRUE);
- contour(X,Y,Str_Prb_Array,levels=c(0.5),drawlabels=FALSE,col='black',xlim=c(-3,3),ylim=c(0,6),xaxs='i',yaxs='i',lwd=2);
- par(new=FALSE);
- dev.off();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement