Advertisement
matthewrmata

GIF Frames for Contact and Misses

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