nesticot commited on
Commit
f8a34cf
·
verified ·
1 Parent(s): fdf9d88

Create calculate_arm_angles.py

Browse files
Files changed (1) hide show
  1. stuff_model/calculate_arm_angles.py +62 -0
stuff_model/calculate_arm_angles.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import polars as pl
2
+ import numpy as np
3
+ import requests
4
+
5
+ def calculate_arm_angles(df: pl.DataFrame,pitcher_id:int) -> pl.DataFrame:
6
+ df_arm_angle = pl.read_csv('stuff_model/pitcher_arm_angles_2024.csv')
7
+ #pitcher_id = 489446
8
+ df_filter = df.filter(pl.col("pitcher_id") == pitcher_id)
9
+ # data = requests.get(f'https://statsapi.mlb.com/api/v1/people?personIds={pitcher_id}').json()
10
+
11
+ if pitcher_id not in df_arm_angle["pitcher"]:
12
+
13
+ data = requests.get(f'https://statsapi.mlb.com/api/v1/people?personIds={pitcher_id}').json()
14
+ height_in = data['people'][0]['height']
15
+ height = int(height_in.split("'")[0]) * 12 + int(height_in.split("'")[1].split('"')[0])
16
+ df_filter = (df_filter.with_columns(
17
+ (pl.col("release_pos_x") * 12).alias("release_pos_x"),
18
+ (pl.col("release_pos_z") * 12).alias("release_pos_z"),
19
+ (pl.lit(height * 0.70)).alias("shoulder_pos"),
20
+ )
21
+ .with_columns(
22
+ (pl.col("release_pos_z") - pl.col("shoulder_pos")).alias("Opp"),
23
+ pl.col("release_pos_x").abs().alias("Adj"),
24
+ )
25
+ .with_columns(
26
+ pl.struct(["Opp", "Adj"]).map_elements(lambda x: np.arctan2(x["Opp"], x["Adj"])).alias("arm_angle_rad")
27
+ ))
28
+
29
+ df_filter = (df_filter.with_columns(
30
+
31
+ pl.col("arm_angle_rad").degrees().alias("arm_angle")
32
+
33
+ #.drop(["Opp", "arm_angle_rad"])
34
+ ))
35
+
36
+ else:
37
+ shoulder_x = df_arm_angle.filter(pl.col("pitcher") == pitcher_id)["relative_shoulder_x"][0]
38
+ shoulder_z = df_arm_angle.filter(pl.col("pitcher") == pitcher_id)["shoulder_z"][0]
39
+
40
+ print(shoulder_x, shoulder_z)
41
+
42
+ df_filter = (df_filter.with_columns(
43
+
44
+ )
45
+ .with_columns(
46
+ (pl.col("release_pos_z") - shoulder_z).alias("Opp"),
47
+ (pl.col("release_pos_x") - shoulder_x).alias("Adj"),
48
+ )
49
+ .with_columns(
50
+ pl.struct(["Opp", "Adj"]).map_elements(lambda x: np.arctan2(x["Opp"], x["Adj"])).alias("arm_angle_rad")
51
+ )
52
+ .with_columns(
53
+ pl.col("arm_angle_rad").degrees().alias("arm_angle")
54
+ )
55
+ #.drop(["Opp", "arm_angle_rad"])
56
+ )
57
+
58
+
59
+
60
+
61
+ return df_filter
62
+