Syntrex commited on
Commit
26e1c80
·
verified ·
1 Parent(s): 85f02a4

Update models/batter_zone_model.py

Browse files
Files changed (1) hide show
  1. models/batter_zone_model.py +57 -0
models/batter_zone_model.py CHANGED
@@ -5,6 +5,63 @@ import pandas as pd
5
 
6
  from models.batter_zone_store import load_batter_zone_store_metrics
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  def build_batter_zone_feature_row(
10
  statcast_df: pd.DataFrame,
 
5
 
6
  from models.batter_zone_store import load_batter_zone_store_metrics
7
 
8
+ PITCH_FAMILY_MAP = {
9
+ "4-seam fastball": "fastball",
10
+ "four-seam fastball": "fastball",
11
+ "fastball": "fastball",
12
+ "sinker": "fastball",
13
+ "cutter": "fastball",
14
+ "slider": "breaking",
15
+ "sweeper": "breaking",
16
+ "curveball": "breaking",
17
+ "knuckle curve": "breaking",
18
+ "slurve": "breaking",
19
+ "changeup": "offspeed",
20
+ "splitter": "offspeed",
21
+ "forkball": "offspeed",
22
+ "split-finger": "offspeed",
23
+ "circle change": "offspeed",
24
+ }
25
+
26
+
27
+ def normalize_pitch_family(pitch_name: Any) -> str:
28
+ text = str(pitch_name or "").strip().lower()
29
+ if text in {"", "nan", "none"}:
30
+ return "unknown"
31
+ return PITCH_FAMILY_MAP.get(text, "unknown")
32
+
33
+
34
+ def classify_zone_bucket(plate_x: Any, plate_z: Any) -> str:
35
+ try:
36
+ x = float(plate_x)
37
+ z = float(plate_z)
38
+ except Exception:
39
+ return "unknown"
40
+
41
+ zone_left = -0.83
42
+ zone_right = 0.83
43
+ zone_bottom = 1.50
44
+ zone_top = 3.50
45
+
46
+ if zone_left <= x <= zone_right and zone_bottom <= z <= zone_top:
47
+ inner_left = -0.45
48
+ inner_right = 0.45
49
+ inner_bottom = 1.90
50
+ inner_top = 3.10
51
+
52
+ if inner_left <= x <= inner_right and inner_bottom <= z <= inner_top:
53
+ return "heart"
54
+ return "shadow"
55
+
56
+ chase_left = -1.20
57
+ chase_right = 1.20
58
+ chase_bottom = 1.10
59
+ chase_top = 3.90
60
+
61
+ if chase_left <= x <= chase_right and chase_bottom <= z <= chase_top:
62
+ return "chase"
63
+
64
+ return "waste"
65
 
66
  def build_batter_zone_feature_row(
67
  statcast_df: pd.DataFrame,