Update app.py
Browse files
app.py
CHANGED
|
@@ -88,6 +88,9 @@ app_ui = ui.page_fluid(
|
|
| 88 |
),
|
| 89 |
ui.nav("Daily Pitches",
|
| 90 |
ui.row(
|
|
|
|
|
|
|
|
|
|
| 91 |
ui.column(2,
|
| 92 |
ui.div(
|
| 93 |
{"class": "input-group"},
|
|
@@ -242,6 +245,103 @@ def server(input, output, session):
|
|
| 242 |
|
| 243 |
return df_merge
|
| 244 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
# @reactive.Calc
|
| 246 |
# def ts_data():
|
| 247 |
|
|
@@ -401,6 +501,9 @@ def server(input, output, session):
|
|
| 401 |
def download_all():
|
| 402 |
yield ts_data().write_csv()
|
| 403 |
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
@session.download(filename="data_tjstuff.csv")
|
| 406 |
def download_tjsumm():
|
|
|
|
| 88 |
),
|
| 89 |
ui.nav("Daily Pitches",
|
| 90 |
ui.row(
|
| 91 |
+
ui.column(1,
|
| 92 |
+
ui.download_button("download_daily", "Download Data", class_="btn-sm mb-3")
|
| 93 |
+
),
|
| 94 |
ui.column(2,
|
| 95 |
ui.div(
|
| 96 |
{"class": "input-group"},
|
|
|
|
| 245 |
|
| 246 |
return df_merge
|
| 247 |
|
| 248 |
+
|
| 249 |
+
@reactive.Calc
|
| 250 |
+
def ts_data_daily():
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
df_spring = spring_data().unique(subset=['play_id'])
|
| 254 |
+
|
| 255 |
+
# df_year_old = stuff_apply.stuff_apply(fe.feature_engineering(pl.concat([df_mlb,df_aaa,df_a,df_afl])))
|
| 256 |
+
# df_year_2old = stuff_apply.stuff_apply(fe.feature_engineering(pl.concat([df_mlb_2023])))
|
| 257 |
+
df_spring_stuff = stuff_apply.stuff_apply(fe.feature_engineering(df_spring))
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
import polars as pl
|
| 262 |
+
|
| 263 |
+
# Compute total pitches for each pitcher
|
| 264 |
+
df_pitcher_totals = df_spring_stuff.group_by(["pitcher_id",'game_id','game_date']).agg(
|
| 265 |
+
pl.col("start_speed").count().alias("pitcher_total")
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
df_pitcher_totals_hands = (
|
| 269 |
+
df_spring_stuff
|
| 270 |
+
.group_by(["pitcher_id", "batter_hand",'game_id','game_date'])
|
| 271 |
+
.agg(pl.col("start_speed").count().alias("pitcher_total"))
|
| 272 |
+
.pivot(
|
| 273 |
+
values="pitcher_total",
|
| 274 |
+
index=["pitcher_id",'game_id'],
|
| 275 |
+
columns="batter_hand",
|
| 276 |
+
aggregate_function="sum"
|
| 277 |
+
)
|
| 278 |
+
.rename({"L": "pitcher_total_left", "R": "pitcher_total_right"})
|
| 279 |
+
.fill_null(0) # Fill missing values with 0 if some pitchers don't face both hands
|
| 280 |
+
)
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
df_spring_group = df_spring_stuff.group_by(['pitcher_id', 'pitcher_name', 'pitch_type','game_id','game_date']).agg([
|
| 284 |
+
pl.col('start_speed').count().alias('count'),
|
| 285 |
+
pl.col('start_speed').mean().alias('start_speed'),
|
| 286 |
+
pl.col('start_speed').max().alias('max_start_speed'),
|
| 287 |
+
pl.col('ivb').mean().alias('ivb'),
|
| 288 |
+
pl.col('hb').mean().alias('hb'),
|
| 289 |
+
pl.col('release_pos_z').mean().alias('release_pos_z'),
|
| 290 |
+
pl.col('release_pos_x').mean().alias('release_pos_x'),
|
| 291 |
+
pl.col('extension').mean().alias('extension'),
|
| 292 |
+
pl.col('tj_stuff_plus').mean().alias('tj_stuff_plus'),
|
| 293 |
+
(pl.col("batter_hand").eq("R").sum()).alias("rhh_count"), # Corrected: Counts RHH (batter_hand == "R")
|
| 294 |
+
(pl.col("batter_hand").eq("L").sum()).alias("lhh_count") # Corrected: Counts LHH (batter_hand == "L")
|
| 295 |
+
])
|
| 296 |
+
|
| 297 |
+
# Join total pitches per pitcher to the grouped DataFrame on pitcher_id
|
| 298 |
+
df_spring_group = df_spring_group.join(df_pitcher_totals, on=["pitcher_id",'game_id'], how="left")
|
| 299 |
+
df_spring_group = df_spring_group.join(df_pitcher_totals_hands, on=["pitcher_id",'game_id'], how="left")
|
| 300 |
+
|
| 301 |
+
# Now calculate the pitch percent for each pitcher/pitch_type combination
|
| 302 |
+
df_spring_group = df_spring_group.with_columns(
|
| 303 |
+
(pl.col("count") / pl.col("pitcher_total")).alias("pitch_percent")
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
+
# Optionally, if you want the percentage of left/right-handed batters within the group:
|
| 307 |
+
df_spring_group = df_spring_group.with_columns([
|
| 308 |
+
(pl.col("rhh_count") / pl.col("pitcher_total_right")).alias("rhh_percent"),
|
| 309 |
+
(pl.col("lhh_count") / pl.col("pitcher_total_left")).alias("lhh_percent")
|
| 310 |
+
])
|
| 311 |
+
|
| 312 |
+
df_merge = df_spring_group.join(df_year_old_group,on=['pitcher_id','pitch_type'],how='left',suffix='_old')
|
| 313 |
+
|
| 314 |
+
|
| 315 |
+
df_merge = df_merge.with_columns(
|
| 316 |
+
pl.col('pitcher_id').is_in(df_year_old_group['pitcher_id']).alias('exists_in_old')
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
df_merge = df_merge.with_columns(
|
| 320 |
+
pl.when(pl.col('start_speed_old').is_null() & pl.col('exists_in_old'))
|
| 321 |
+
.then(pl.lit(True))
|
| 322 |
+
.otherwise(pl.lit(None))
|
| 323 |
+
.alias("new_pitch")
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
df_merge = df_merge.select([
|
| 327 |
+
'pitcher_id',
|
| 328 |
+
'pitcher_name',
|
| 329 |
+
'pitch_type',
|
| 330 |
+
'count',
|
| 331 |
+
'pitch_percent',
|
| 332 |
+
'rhh_percent',
|
| 333 |
+
'lhh_percent',
|
| 334 |
+
'start_speed',
|
| 335 |
+
'max_start_speed',
|
| 336 |
+
'ivb',
|
| 337 |
+
'hb',
|
| 338 |
+
'release_pos_z',
|
| 339 |
+
'release_pos_x',
|
| 340 |
+
'extension',
|
| 341 |
+
'tj_stuff_plus',
|
| 342 |
+
])
|
| 343 |
+
|
| 344 |
+
return df_merge
|
| 345 |
# @reactive.Calc
|
| 346 |
# def ts_data():
|
| 347 |
|
|
|
|
| 501 |
def download_all():
|
| 502 |
yield ts_data().write_csv()
|
| 503 |
|
| 504 |
+
@session.download(filename="data_daily.csv")
|
| 505 |
+
def download_daily():
|
| 506 |
+
yield ts_data_daily().write_csv()
|
| 507 |
|
| 508 |
@session.download(filename="data_tjstuff.csv")
|
| 509 |
def download_tjsumm():
|