nesticot commited on
Commit
aa747ac
·
verified ·
1 Parent(s): 88546ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -17
app.py CHANGED
@@ -409,10 +409,25 @@ def server(input, output, session):
409
  import polars as pl
410
 
411
  # Compute total pitches for each pitcher
412
- df_pitcher_totals = df_spring_stuff.group_by("pitcher_id").agg(
413
  pl.col("start_speed").count().alias("pitcher_total")
414
  )
415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
416
  df_spring_group = df_spring_stuff.group_by(['pitcher_id', 'pitcher_name', 'pitch_type']).agg([
417
  pl.col('start_speed').count().alias('count'),
418
  pl.col('start_speed').mean().alias('start_speed'),
@@ -423,12 +438,13 @@ def server(input, output, session):
423
  pl.col('release_pos_x').mean().alias('release_pos_x'),
424
  pl.col('extension').mean().alias('extension'),
425
  pl.col('tj_stuff_plus').mean().alias('tj_stuff_plus'),
426
- (pl.col('start_speed').filter(pl.col('batter_hand')=='L').count()).alias('rhh_count'),
427
- (pl.col('start_speed').filter(pl.col('batter_hand')=='R').count()).alias('lhh_count')
428
  ])
429
 
430
  # Join total pitches per pitcher to the grouped DataFrame on pitcher_id
431
- df_spring_group = df_spring_group.join(df_pitcher_totals, on="pitcher_id", how="left")
 
432
 
433
  # Now calculate the pitch percent for each pitcher/pitch_type combination
434
  df_spring_group = df_spring_group.with_columns(
@@ -437,10 +453,11 @@ def server(input, output, session):
437
 
438
  # Optionally, if you want the percentage of left/right-handed batters within the group:
439
  df_spring_group = df_spring_group.with_columns([
440
- (pl.col("rhh_count") / pl.col("pitcher_total")).alias("rhh_percent"),
441
- (pl.col("lhh_count") / pl.col("pitcher_total")).alias("lhh_percent")
442
  ])
443
 
 
444
  df_merge = df_spring_group.join(df_year_old_group,on=['pitcher_id','pitch_type'],how='left',suffix='_old')
445
 
446
 
@@ -515,8 +532,8 @@ def server(input, output, session):
515
  { "title": "New Pitch?", "field": "new_pitch", "width": 125, "headerFilter":"input" ,"frozen":False,},
516
  { "title": "Pitches", "field": "count", "width": 100 , "headerFilter":"input","contextMenu":True},
517
  { "title": "Pitch%", "field": "pitch_percent_formatted", "width": 100, "headerFilter":"input"},
518
- { "title": "RHH%", "field": "rhh_percent_formatted", "width": 100, "headerFilter":"input"},
519
  { "title": "LHH%", "field": "lhh_percent_formatted", "width": 100, "headerFilter":"input"},
 
520
  { "title": "Velocity", "field": "start_speed_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
521
  { "title": "Max Velo", "field": "max_start_speed_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
522
  { "title": "iVB", "field": "ivb_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
@@ -566,11 +583,26 @@ def server(input, output, session):
566
  import polars as pl
567
 
568
  # Compute total pitches for each pitcher
569
- df_pitcher_totals = df_spring_stuff.group_by(["pitcher_id",'game_id','game_date']).agg(
570
  pl.col("start_speed").count().alias("pitcher_total")
571
  )
572
 
573
- df_spring_group = df_spring_stuff.group_by(['pitcher_id', 'pitcher_name', 'pitch_type','game_id','game_date']).agg([
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
574
  pl.col('start_speed').count().alias('count'),
575
  pl.col('start_speed').mean().alias('start_speed'),
576
  pl.col('start_speed').max().alias('max_start_speed'),
@@ -580,12 +612,13 @@ def server(input, output, session):
580
  pl.col('release_pos_x').mean().alias('release_pos_x'),
581
  pl.col('extension').mean().alias('extension'),
582
  pl.col('tj_stuff_plus').mean().alias('tj_stuff_plus'),
583
- (pl.col('start_speed').filter(pl.col('batter_hand')=='L').count()).alias('rhh_count'),
584
- (pl.col('start_speed').filter(pl.col('batter_hand')=='R').count()).alias('lhh_count')
585
  ])
586
 
587
  # Join total pitches per pitcher to the grouped DataFrame on pitcher_id
588
- df_spring_group = df_spring_group.join(df_pitcher_totals, on=["pitcher_id",'game_id','game_date'], how="left")
 
589
 
590
  # Now calculate the pitch percent for each pitcher/pitch_type combination
591
  df_spring_group = df_spring_group.with_columns(
@@ -594,10 +627,11 @@ def server(input, output, session):
594
 
595
  # Optionally, if you want the percentage of left/right-handed batters within the group:
596
  df_spring_group = df_spring_group.with_columns([
597
- (pl.col("rhh_count") / pl.col("pitcher_total")).alias("rhh_percent"),
598
- (pl.col("lhh_count") / pl.col("pitcher_total")).alias("lhh_percent")
599
  ])
600
 
 
601
  df_merge = df_spring_group.join(df_year_old_group,on=['pitcher_id','pitch_type'],how='left',suffix='_old')
602
 
603
 
@@ -673,8 +707,8 @@ def server(input, output, session):
673
  { "title": "Date", "field": "game_date", "width": 100, "headerFilter":"input" ,"frozen":True,},
674
  { "title": "Pitches", "field": "count", "width": 100 , "headerFilter":"input"},
675
  { "title": "Pitch%", "field": "pitch_percent_formatted", "width": 100, "headerFilter":"input"},
676
- { "title": "RHH%", "field": "rhh_percent_formatted", "width": 100, "headerFilter":"input"},
677
  { "title": "LHH%", "field": "lhh_percent_formatted", "width": 100, "headerFilter":"input"},
 
678
  { "title": "Velocity", "field": "start_speed_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
679
  { "title": "Max Velo", "field": "max_start_speed_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
680
  { "title": "iVB", "field": "ivb_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
@@ -842,9 +876,9 @@ def server(input, output, session):
842
  { "title": "Pitch Type", "field": "pitch_type", "width": 125, "headerFilter":"input" ,"frozen":True,},
843
  { "title": "New?", "field": "new_pitch", "width": 125, "headerFilter":"input" ,"frozen":False,},
844
  { "title": "Pitches", "field": "count", "width": 100 , "headerFilter":"input"},
845
- { "title": "Pitch%", "field": "pitch_percent_formatted", "width": 100, "headerFilter":"input"},
846
- { "title": "RHH%", "field": "rhh_percent_formatted", "width": 90, "headerFilter":"input"},
847
  { "title": "LHH%", "field": "lhh_percent_formatted", "width": 90, "headerFilter":"input"},
 
848
  { "title": "Velocity", "field": "start_speed_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
849
  { "title": "Max Velo", "field": "max_start_speed_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
850
  { "title": "iVB", "field": "ivb_formatted", "width": 80, "headerFilter":"input", "formatter":"textarea" },
 
409
  import polars as pl
410
 
411
  # Compute total pitches for each pitcher
412
+ df_pitcher_totals = df_spring_stuff.group_by(["pitcher_id"]).agg(
413
  pl.col("start_speed").count().alias("pitcher_total")
414
  )
415
 
416
+ df_pitcher_totals_hands = (
417
+ df_spring_stuff
418
+ .group_by(["pitcher_id", "batter_hand"])
419
+ .agg(pl.col("start_speed").count().alias("pitcher_total"))
420
+ .pivot(
421
+ values="pitcher_total",
422
+ index="pitcher_id",
423
+ columns="batter_hand",
424
+ aggregate_function="sum"
425
+ )
426
+ .rename({"L": "pitcher_total_left", "R": "pitcher_total_right"})
427
+ .fill_null(0) # Fill missing values with 0 if some pitchers don't face both hands
428
+ )
429
+
430
+
431
  df_spring_group = df_spring_stuff.group_by(['pitcher_id', 'pitcher_name', 'pitch_type']).agg([
432
  pl.col('start_speed').count().alias('count'),
433
  pl.col('start_speed').mean().alias('start_speed'),
 
438
  pl.col('release_pos_x').mean().alias('release_pos_x'),
439
  pl.col('extension').mean().alias('extension'),
440
  pl.col('tj_stuff_plus').mean().alias('tj_stuff_plus'),
441
+ (pl.col("batter_hand").eq("R").sum()).alias("rhh_count"), # Corrected: Counts RHH (batter_hand == "R")
442
+ (pl.col("batter_hand").eq("L").sum()).alias("lhh_count") # Corrected: Counts LHH (batter_hand == "L")
443
  ])
444
 
445
  # Join total pitches per pitcher to the grouped DataFrame on pitcher_id
446
+ df_spring_group = df_spring_group.join(df_pitcher_totals, on=["pitcher_id"], how="left")
447
+ df_spring_group = df_spring_group.join(df_pitcher_totals_hands, on=["pitcher_id"], how="left")
448
 
449
  # Now calculate the pitch percent for each pitcher/pitch_type combination
450
  df_spring_group = df_spring_group.with_columns(
 
453
 
454
  # Optionally, if you want the percentage of left/right-handed batters within the group:
455
  df_spring_group = df_spring_group.with_columns([
456
+ (pl.col("rhh_count") / pl.col("pitcher_total_right")).alias("rhh_percent"),
457
+ (pl.col("lhh_count") / pl.col("pitcher_total_left")).alias("lhh_percent")
458
  ])
459
 
460
+
461
  df_merge = df_spring_group.join(df_year_old_group,on=['pitcher_id','pitch_type'],how='left',suffix='_old')
462
 
463
 
 
532
  { "title": "New Pitch?", "field": "new_pitch", "width": 125, "headerFilter":"input" ,"frozen":False,},
533
  { "title": "Pitches", "field": "count", "width": 100 , "headerFilter":"input","contextMenu":True},
534
  { "title": "Pitch%", "field": "pitch_percent_formatted", "width": 100, "headerFilter":"input"},
 
535
  { "title": "LHH%", "field": "lhh_percent_formatted", "width": 100, "headerFilter":"input"},
536
+ { "title": "RHH%", "field": "rhh_percent_formatted", "width": 100, "headerFilter":"input"},
537
  { "title": "Velocity", "field": "start_speed_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
538
  { "title": "Max Velo", "field": "max_start_speed_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
539
  { "title": "iVB", "field": "ivb_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
 
583
  import polars as pl
584
 
585
  # Compute total pitches for each pitcher
586
+ df_pitcher_totals = df_spring_stuff.group_by(["pitcher_id"]).agg(
587
  pl.col("start_speed").count().alias("pitcher_total")
588
  )
589
 
590
+ df_pitcher_totals_hands = (
591
+ df_spring_stuff
592
+ .group_by(["pitcher_id", "batter_hand"])
593
+ .agg(pl.col("start_speed").count().alias("pitcher_total"))
594
+ .pivot(
595
+ values="pitcher_total",
596
+ index="pitcher_id",
597
+ columns="batter_hand",
598
+ aggregate_function="sum"
599
+ )
600
+ .rename({"L": "pitcher_total_left", "R": "pitcher_total_right"})
601
+ .fill_null(0) # Fill missing values with 0 if some pitchers don't face both hands
602
+ )
603
+
604
+
605
+ df_spring_group = df_spring_stuff.group_by(['pitcher_id', 'pitcher_name', 'pitch_type']).agg([
606
  pl.col('start_speed').count().alias('count'),
607
  pl.col('start_speed').mean().alias('start_speed'),
608
  pl.col('start_speed').max().alias('max_start_speed'),
 
612
  pl.col('release_pos_x').mean().alias('release_pos_x'),
613
  pl.col('extension').mean().alias('extension'),
614
  pl.col('tj_stuff_plus').mean().alias('tj_stuff_plus'),
615
+ (pl.col("batter_hand").eq("R").sum()).alias("rhh_count"), # Corrected: Counts RHH (batter_hand == "R")
616
+ (pl.col("batter_hand").eq("L").sum()).alias("lhh_count") # Corrected: Counts LHH (batter_hand == "L")
617
  ])
618
 
619
  # Join total pitches per pitcher to the grouped DataFrame on pitcher_id
620
+ df_spring_group = df_spring_group.join(df_pitcher_totals, on=["pitcher_id"], how="left")
621
+ df_spring_group = df_spring_group.join(df_pitcher_totals_hands, on=["pitcher_id"], how="left")
622
 
623
  # Now calculate the pitch percent for each pitcher/pitch_type combination
624
  df_spring_group = df_spring_group.with_columns(
 
627
 
628
  # Optionally, if you want the percentage of left/right-handed batters within the group:
629
  df_spring_group = df_spring_group.with_columns([
630
+ (pl.col("rhh_count") / pl.col("pitcher_total_right")).alias("rhh_percent"),
631
+ (pl.col("lhh_count") / pl.col("pitcher_total_left")).alias("lhh_percent")
632
  ])
633
 
634
+
635
  df_merge = df_spring_group.join(df_year_old_group,on=['pitcher_id','pitch_type'],how='left',suffix='_old')
636
 
637
 
 
707
  { "title": "Date", "field": "game_date", "width": 100, "headerFilter":"input" ,"frozen":True,},
708
  { "title": "Pitches", "field": "count", "width": 100 , "headerFilter":"input"},
709
  { "title": "Pitch%", "field": "pitch_percent_formatted", "width": 100, "headerFilter":"input"},
 
710
  { "title": "LHH%", "field": "lhh_percent_formatted", "width": 100, "headerFilter":"input"},
711
+ { "title": "RHH%", "field": "rhh_percent_formatted", "width": 100, "headerFilter":"input"},
712
  { "title": "Velocity", "field": "start_speed_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
713
  { "title": "Max Velo", "field": "max_start_speed_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
714
  { "title": "iVB", "field": "ivb_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
 
876
  { "title": "Pitch Type", "field": "pitch_type", "width": 125, "headerFilter":"input" ,"frozen":True,},
877
  { "title": "New?", "field": "new_pitch", "width": 125, "headerFilter":"input" ,"frozen":False,},
878
  { "title": "Pitches", "field": "count", "width": 100 , "headerFilter":"input"},
879
+ { "title": "Pitch%", "field": "pitch_percent_formatted", "width": 100, "headerFilter":"input"},+
 
880
  { "title": "LHH%", "field": "lhh_percent_formatted", "width": 90, "headerFilter":"input"},
881
+ { "title": "RHH%", "field": "rhh_percent_formatted", "width": 90, "headerFilter":"input"},
882
  { "title": "Velocity", "field": "start_speed_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
883
  { "title": "Max Velo", "field": "max_start_speed_formatted", "width": 100, "headerFilter":"input", "formatter":"textarea" },
884
  { "title": "iVB", "field": "ivb_formatted", "width": 80, "headerFilter":"input", "formatter":"textarea" },