Advertisement
matthewrmata

GIF Frames for Swings/Takes

Jan 25th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 6.15 KB | None | 0 0
  1. ###############################################################################
  2. # Generate images for a GIF for the movement of the pitch based on swing/take #
  3. ###############################################################################
  4.  
  5. #############
  6. # LIBRARIES #
  7. #############
  8.  
  9. library(RODBC);
  10. library(fields);
  11. library(diagram);
  12. library(calibrate);
  13.  
  14. ####################
  15. # ADDITIONAL FILES #
  16. ####################
  17.  
  18. source("Movement Scripts/Mvt_Fcns.r");
  19.  
  20. ########
  21. # MAIN #
  22. ########
  23.  
  24. # Set the pitcher name
  25. first <- 'Julio';
  26. last <- 'Urias';
  27.  
  28. # Set the pitch type based on the two-letter designation
  29. pitch_type <- 'CU';
  30.  
  31. # Set the handedness of the batter
  32. stand <- 'R';
  33.  
  34. # Set the year
  35. year <- '2016';
  36.  
  37. # Set up the database name
  38. database <- paste("MLB",year,sep="");
  39.  
  40. # Set the number of frames
  41. Ny <- 30;
  42.  
  43. # Set the increment in the y-direction
  44. y_seq <- seq(55,17/12,length=Ny);
  45.  
  46. # Set the movement type
  47. # 1 <- PITCHf/x movement
  48. # 2 <- PITCHf/x movement without drag
  49. # 3 <- planar movement
  50. # 4 <- planar movement without drag
  51. mvt_type <- 1;
  52.  
  53. # Set the descriptor based on the type of movement
  54. mvt_desc <- switch(mvt_type,'PFX','PFX D','W','W D');
  55.  
  56. # Set the quadrilateral core of the strike zone
  57. if (stand == 'L'){
  58.     UL <- c(-0.47,2.85);
  59.     UR <- c(0.22,2.87);
  60.     LL <- c(-0.5,2.04);
  61.     LR <- c(0.24,2.16);
  62. } else {
  63.     UL <- c(-0.38,2.88);
  64.     UR <- c(0.35,2.81);
  65.     LL <- c(-0.46,2.1);
  66.     LR <- c(0.35,2.09);
  67. }
  68.  
  69. # Associate the pitch type with its full name
  70. pitch_name <- switch(pitch_type,
  71.        FA = 'Fastballs',
  72.        FF = 'Four-Seam FBs',
  73.        FT = 'Two-Seam FBs',
  74.        FC = 'Cut Fastballs',
  75.        FS = 'Split-Finger FBs',
  76.        FO = 'Forkballs',
  77.        SI = 'Sinkers',
  78.        SL = 'Sliders',
  79.        CU = 'Curveballs',
  80.        KC = 'Knuckle Curves',
  81.        EP = 'Ephuuses',
  82.        CH = 'Changeups',
  83.        SC = 'Screwballs',
  84.        KN = 'Knuckleballs',
  85.        UN = 'Unknown Pitches');
  86.  
  87. # Set the cases where a batter has swung at a pitch
  88. swing_cases <- "(des = 'In play, no out' OR
  89.                  des = 'In play, out(s)' OR
  90.                  des = 'In play, run(s)' OR
  91.                  des = 'Foul' OR
  92.                  des = 'Foul (Runner Going)' OR
  93.                  des = 'Foul Bunt' OR
  94.                  des = 'Foul Tip' OR
  95.                  des = 'Swinging Strike' OR
  96.                  des = 'Swinging Pitchout' OR
  97.                  des = 'Missed Bunt' OR
  98.                  des = 'Swinging Strike (Blocked)')";
  99.  
  100. # Set the cases where a batter has taken a pitch
  101. take_cases <- "(des = 'Called Strike' OR
  102.                 des = 'Ball' OR
  103.                 des = 'Ball in Dirt' OR
  104.                 des = 'Hit By Pitch')";
  105.                  
  106. # Open the channel
  107. channel <- odbcConnect(database);
  108.  
  109. # Set the query for swings
  110. Swing_Query = paste("SELECT x0, y0, z0, vx0, vy0, vz0, ax, ay, az
  111.                      FROM pitches WHERE pitch_type = '",pitch_type,"' AND
  112.                      ",swing_cases," AND ab_id IN
  113.                      (SELECT ab_id FROM atbats WHERE
  114.                      pitcher = (SELECT eliasid FROM players WHERE first = '",
  115.                      first,"' AND last = '",last,"') AND stand = '",stand,"')
  116.                      ",sep="");
  117.  
  118. # Download the swing data
  119. Swing <- sqlQuery(channel,Swing_Query);
  120.  
  121. # Close the channel
  122. close(channel);
  123.  
  124. # Project the swing data
  125. if (mvt_type == 1){
  126.     Swing_xz <- Pitch_Project_PFX(Swing,Ny,stand);
  127. } else if (mvt_type == 2){
  128.     Swing_xz <- Pitch_Project_PFX_Drag(Swing,Ny,stand);
  129. } else if (mvt_type == 3){
  130.     Swing_xz <- Pitch_Project_W(Swing,Ny,stand);
  131. } else {
  132.     Swing_xz <- Pitch_Project_W_Drag(Swing,Ny,stand);
  133. }
  134.  
  135. # Set the x and z coordinates of the swing projections
  136. x_S <- Swing_xz$x_PL;
  137. z_S <- Swing_xz$z_PL;
  138.  
  139. # Open the channel
  140. channel <- odbcConnect(database);
  141.  
  142. # Set the query for taken pitches
  143. Take_Query = paste("SELECT x0, y0, z0, vx0, vy0, vz0, ax, ay, az  
  144.                     FROM pitches WHERE pitch_type = '",pitch_type,"' AND
  145.                     ",take_cases," AND ab_id IN
  146.                     (SELECT ab_id FROM atbats WHERE
  147.                     pitcher = (SELECT eliasid FROM players WHERE first = '",
  148.                     first,"' AND last = '",last,"') AND stand = '",stand,"')
  149.                     ",sep="");
  150.                    
  151. # Download the take data
  152. Take <- sqlQuery(channel,Take_Query);
  153.  
  154. # Close the channel
  155. close(channel);
  156.  
  157. # Project the take data
  158. if (mvt_type == 1){
  159.     Take_xz <- Pitch_Project_PFX(Take,Ny,stand);
  160. } else if (mvt_type == 2){
  161.     Take_xz <- Pitch_Project_PFX_Drag(Take,Ny,stand);
  162. } else if (mvt_type == 3){
  163.     Take_xz <- Pitch_Project_W(Take,Ny,stand);
  164. } else {
  165.     Take_xz <- Pitch_Project_W_Drag(Take,Ny,stand);
  166. }
  167.  
  168. # Set the x and z coordinates of the taken projections
  169. x_T <- Take_xz$x_PL;
  170. z_T <- Take_xz$z_PL;
  171.  
  172. # Find the ball/strike indices
  173. Swing_Ball_Str <- ball_strike_prob(Swing,stand);
  174. Take_Ball_Str <- ball_strike_prob(Take,stand);
  175.  
  176. # Parse the indices
  177. S_Str_ind <- Swing_Ball_Str$S;
  178. S_Ball_ind <- Swing_Ball_Str$B;
  179. T_Str_ind <- Take_Ball_Str$S;
  180. T_Ball_ind <- Take_Ball_Str$B;
  181.  
  182. # Set up an array to draw a strike zone contour
  183. dx <- 0.005;
  184. X <- seq(-3+dx/2,3-dx/2,length=1200);
  185. Y <- seq(dx/2,6-dx/2,length=1200);
  186. k_sz <- 4;
  187. Str_Prb_Array <- array(0,dim=c(1200,1200));
  188. for (i in 1:1200) {
  189.     for (j in 1:1200){
  190.         P <- c(X[i],Y[j]);
  191.         d_sz <- Min_Dist_Quad(P,UL,UR,LL,LR);
  192.         Str_Prb_Array[i,j] <- exp(-k_sz*d_sz^4);
  193.     }
  194. }
  195.  
  196. # Generate the images
  197. for (j in 1:Ny){
  198.     if (j < 10){
  199.         filename <- paste(last,"_",pitch_type,"_ST_",stand,"_0",j,".jpg",sep="");
  200.     }
  201.     else {
  202.         filename <- paste(last,"_",pitch_type,"_ST_",stand,"_",j,".jpg",sep="");
  203.     }
  204.     jpeg(filename);
  205.     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),
  206.          xlab='Horizontal Location (Feet)',ylab='Vertical Location (Feet)',
  207.          main=paste(first," ",last,": ",pitch_name," v. ",stand,"HB at ",round(y_seq[j],digits=2)," Feet (",mvt_desc,")",sep=""),
  208.          xaxs='i',yaxs='i');
  209.     par(new=TRUE);
  210.     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),
  211.          xlab='',ylab='',xaxs='i',yaxs='i');
  212.     par(new=TRUE);
  213.     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),
  214.          xlab='',ylab='',xaxs='i',yaxs='i');
  215.     par(new=TRUE);
  216.     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),
  217.          xlab='',ylab='',xaxs='i',yaxs='i');     
  218.     par(new=TRUE);
  219.     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);
  220.     par(new=FALSE);
  221.     dev.off();
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement