cdcavin commited on
Commit
0247f30
Β·
verified Β·
1 Parent(s): ffe2e3f

Upload 8 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ fit_l6_champ.rds filter=lfs diff=lfs merge=lfs -text
37
+ haa_anchor_model.rds filter=lfs diff=lfs merge=lfs -text
38
+ vaa_anchor_model.rds filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM rstudio/plumber
2
+
3
+ RUN Rscript -e "install.packages(c('data.table','jsonlite'), repos='https://cran.rstudio.com/')"
4
+
5
+ WORKDIR /app
6
+ COPY plumber.R .
7
+ COPY fit_l6_champ.rds .
8
+ COPY haa_anchor_model.rds .
9
+ COPY vaa_anchor_model.rds .
10
+ COPY shape_scaling_factors.rds .
11
+ COPY train_means.csv .
12
+ COPY pitcher_profiles.csv .
13
+
14
+ EXPOSE 7860
15
+
16
+ ENTRYPOINT ["Rscript", "-e", "pr <- plumber::plumb('plumber.R'); pr$run(host='0.0.0.0', port=7860)"]
fit_l6_champ.rds ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b27fc254821bff8902653ce60f639245c2e54f29e5cce775b8fecdcb1418b670
3
+ size 720263
haa_anchor_model.rds ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2e900f8d0d027ea0987d0f2042c578cfa9c497a597a4696cebc8db7491da3ed8
3
+ size 62003317
pitcher_profiles.csv ADDED
The diff for this file is too large to render. See raw diff
 
plumber.R ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #QUEENS PICTH DESIGN APP API
2
+
3
+ library(plumber)
4
+ library(mgcv)
5
+ library(data.table)
6
+ library(jsonlite)
7
+
8
+ cat("Loading model artifacts...\n")
9
+ fit <- readRDS("fit_l6_champ.rds")
10
+ vaa_mod <- readRDS("vaa_anchor_model.rds")
11
+ haa_mod <- readRDS("haa_anchor_model.rds")
12
+ scaling <- readRDS("shape_scaling_factors.rds")
13
+ mu_l6 <- scaling$mu_l6
14
+ sig_l6 <- scaling$sig_l6
15
+ train_means <- fread("train_means.csv")
16
+
17
+ model_levels <- c("FF","CU","ST","SI","FC","SL","CH","FS","KC","SV","KN","EP","FA","FO","CS","SC")
18
+
19
+ cat(sprintf("Model ready. %d pitch types loaded.\n", length(model_levels)))
20
+
21
+ cat("Loading pitcher profiles...\n")
22
+ pitcher_profiles <- tryCatch({
23
+ dt <- fread("pitcher_profiles.csv")
24
+ cat(sprintf("Pitcher profiles loaded: %d rows, %d pitchers.\n",
25
+ nrow(dt), dt[, uniqueN(player_name)]))
26
+ dt
27
+ }, error = function(e) {
28
+ warning(sprintf("Failed to load pitcher_profiles.csv: %s", conditionMessage(e)))
29
+ NULL
30
+ })
31
+
32
+ build_features <- function(d) {
33
+ d <- as.data.table(d)
34
+
35
+ d[, p_throws := fifelse(p_throws %in% c("Left","LHP"), "L",
36
+ fifelse(p_throws %in% c("Right","RHP"), "R", p_throws))]
37
+
38
+ d[, pfx_x_sym := fifelse(p_throws == "L", pfx_x * -1, pfx_x)]
39
+ d[, plate_x_sym := fifelse(p_throws == "L", plate_x * -1, plate_x)]
40
+ d[, rel_x_sym := fifelse(p_throws == "L", release_pos_x * -1, release_pos_x)]
41
+
42
+ if (!"release_extension" %in% names(d)) d[, release_extension := 6.0]
43
+ d[is.na(release_extension), release_extension := 6.0]
44
+
45
+ d[, flight_time := (1.4167 - 50) / vy0]
46
+ d[, vaa := atan((vz0 + az * flight_time) / -(vy0 + ay * flight_time)) * (180 / pi)]
47
+ d[, haa := atan((vx0 + ax * flight_time) / -(vy0 + ay * flight_time)) * (180 / pi)]
48
+ d[, haa_sym := fifelse(p_throws == "L", haa * -1, haa)]
49
+
50
+ d[, vaa_resid := vaa - predict(vaa_mod, newdata = d)]
51
+ d[, haa_resid_sym := haa_sym - predict(haa_mod, newdata = d)]
52
+
53
+ d <- merge(d, train_means, by = "pitch_type", all.x = FALSE)
54
+ d[, ivb_diff := pfx_z - base_ivb]
55
+ d[, hb_diff_sym := pfx_x_sym - base_hb]
56
+
57
+ d[, pitch_type := factor(as.character(pitch_type), levels = model_levels)]
58
+ d[, p_throws := factor(as.character(p_throws), levels = c("L", "R"))]
59
+
60
+ d[!is.na(pitch_type)]
61
+ }
62
+
63
+ score_pitches <- function(d) {
64
+ d <- build_features(d)
65
+ if (nrow(d) == 0) return(list(error = "No valid pitches after feature engineering"))
66
+
67
+ raw <- predict(fit, newdata = d)
68
+ shape_plus <- (((raw * -1) - mu_l6) / sig_l6) * 10 + 100
69
+
70
+ term_mat <- predict(fit, newdata = d, type = "terms")
71
+ col_names <- colnames(term_mat)
72
+
73
+ list(
74
+ n = nrow(d),
75
+ shape_plus = round(as.numeric(shape_plus), 1),
76
+ terms = lapply(seq_len(nrow(d)), function(i) {
77
+ row <- as.list(round(term_mat[i, ], 3))
78
+ names(row) <- col_names
79
+ row
80
+ })
81
+ )
82
+ }
83
+
84
+
85
+ #* @apiTitle Queens Shape+ API
86
+ #* @apiDescription Scores pitch shapes using the Shape+ v2.0 GAM model
87
+
88
+ #* @filter cors
89
+ function(req, res) {
90
+ res$setHeader("Access-Control-Allow-Origin", "*")
91
+ res$setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
92
+ res$setHeader("Access-Control-Allow-Headers", "Content-Type")
93
+ if (req$REQUEST_METHOD == "OPTIONS") {
94
+ res$status <- 200
95
+ return(list())
96
+ }
97
+ plumber::forward()
98
+ }
99
+
100
+ #* @post /score
101
+ #* @serializer json
102
+ function(req) {
103
+ tryCatch({
104
+ body <- fromJSON(req$postBody, simplifyDataFrame = TRUE)
105
+ score_pitches(as.data.table(body))
106
+ }, error = function(e) {
107
+ list(error = conditionMessage(e))
108
+ })
109
+ }
110
+
111
+ #* Find comparable pitchers by movement profile
112
+ #* Expects JSON body: pitch_type, ivb, hb_sym, velo, arm_angle, p_throws
113
+ #* Returns top 5 closest pitchers with their full arsenals
114
+ #* @post /find_comps
115
+ #* @serializer unboxedJSON
116
+ function(req) {
117
+ tryCatch({
118
+
119
+ # ── Guard: profiles must have loaded at startup ──────────────────────────
120
+ if (is.null(pitcher_profiles)) {
121
+ return(list(error = "pitcher_profiles.csv failed to load at startup"))
122
+ }
123
+
124
+ # ── Parse request body ───────────────────────────────────────────────────
125
+ body <- fromJSON(req$postBody, simplifyVector = TRUE)
126
+
127
+ input_pitch_type <- as.character(body$pitch_type)
128
+ input_p_throws <- as.character(body$p_throws)
129
+ input_ivb <- as.numeric(body$ivb)
130
+ input_hb <- as.numeric(body$hb_sym)
131
+ input_velo <- as.numeric(body$velo)
132
+ input_arm <- as.numeric(body$arm_angle)
133
+
134
+ # ── Filter to matching anchor pitch type (any handedness β€” hb_sym is already symmetrized)
135
+ anchor_rows <- pitcher_profiles[pitch_type == input_pitch_type]
136
+
137
+ if (nrow(anchor_rows) == 0) {
138
+ return(list(error = "no matches"))
139
+ }
140
+
141
+ # ── Compute weighted distance for each matching row ──────────────────────
142
+ # Arm angle tightly weighted (/8) so a submarine pitcher never matches an over-the-top guy.
143
+ # If input arm_angle is NA/null, skip that term to avoid NaN distances.
144
+ arm_term <- if (!is.na(input_arm)) ((anchor_rows$arm_angle - input_arm) / 8)^2 else 0
145
+ anchor_rows[, dist := sqrt(
146
+ ((ivb - input_ivb) / 3)^2 +
147
+ ((hb_sym - input_hb) / 3)^2 +
148
+ ((velo - input_velo) / 5)^2 +
149
+ arm_term
150
+ )]
151
+
152
+ # ── Top 5 closest pitchers ───────────────────────────────────────────────
153
+ top5 <- head(anchor_rows[order(dist)], 5)
154
+
155
+ # ── For each top pitcher, pull their full arsenal ────────────────────────
156
+ result <- lapply(seq_len(nrow(top5)), function(i) {
157
+ row <- top5[i]
158
+ pitcher_nm <- row$player_name
159
+ anchor_dist <- round(row$dist, 3)
160
+
161
+ arsenal_rows <- pitcher_profiles[player_name == pitcher_nm]
162
+
163
+ arsenal <- lapply(seq_len(nrow(arsenal_rows)), function(j) {
164
+ r <- arsenal_rows[j]
165
+ list(
166
+ pitch_type = r$pitch_type,
167
+ velo = r$velo,
168
+ ivb = r$ivb,
169
+ hb_sym = r$hb_sym,
170
+ rel_z = r$rel_z,
171
+ arm_angle = r$arm_angle,
172
+ n = r$n
173
+ )
174
+ })
175
+
176
+ list(
177
+ player_name = pitcher_nm,
178
+ p_throws = row$p_throws,
179
+ anchor_dist = anchor_dist,
180
+ arsenal = arsenal
181
+ )
182
+ })
183
+
184
+ result
185
+
186
+ }, error = function(e) {
187
+ list(error = conditionMessage(e))
188
+ })
189
+ }
190
+
191
+ #* Health check
192
+ #* @get /health
193
+ function() {
194
+ list(status = "ok", model = "fit_l6_champ", time = as.character(Sys.time()))
195
+ }
shape_scaling_factors.rds ADDED
Binary file (111 Bytes). View file
 
train_means.csv ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ "pitch_type","base_ivb","base_hb"
2
+ "FF",15.7226652318726,-7.59848379430437
3
+ "CU",-9.46873166287982,9.29305843795502
4
+ "ST",1.41277593345969,14.1691205620677
5
+ "SI",7.68716853902634,-14.9308301016803
6
+ "FC",7.87145427417174,2.49044997251481
7
+ "SL",1.56391178202547,5.15958839635327
8
+ "CH",5.47923625981444,-14.2191902609247
9
+ "FS",3.13004562334216,-10.6894726790451
10
+ "KC",-9.33853080031728,6.55772330702118
11
+ "SV",-4.08449147560259,14.0320987654321
12
+ "KN",-2.76856405846947,-5.07900257953568
13
+ "EP",16.5390134529148,-1.99243049327354
14
+ "FA",15.8844520547945,-6.44458904109588
15
+ "FO",1.42253164556962,-7.27341772151899
16
+ "CS",-8.6772972972973,11.3237837837838
17
+ "SC",-3.4246511627907,-8.28279069767442
vaa_anchor_model.rds ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c8cb714817820e40ab8773519c7ebcd2e9c548fba5872076d4a08e59df52bc86
3
+ size 61642244