Update stuff_model/calculate_arm_angles.py
Browse files
stuff_model/calculate_arm_angles.py
CHANGED
|
@@ -1,66 +1,66 @@
|
|
| 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).drop_nulls(subset=["release_pos_x", "release_pos_z"])
|
| 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 |
-
ball_angle = df_arm_angle.filter(pl.col("pitcher") == pitcher_id)["ball_angle"][0]
|
| 40 |
-
|
| 41 |
-
print(shoulder_x, shoulder_z)
|
| 42 |
-
|
| 43 |
-
df_filter = (df_filter.with_columns(
|
| 44 |
-
|
| 45 |
-
)
|
| 46 |
-
.with_columns(
|
| 47 |
-
(pl.col("release_pos_z") - shoulder_z).alias("Opp"),
|
| 48 |
-
(pl.col("release_pos_x") - shoulder_x*-1).alias("Adj"),
|
| 49 |
-
)
|
| 50 |
-
.with_columns(
|
| 51 |
-
pl.struct(["Opp", "Adj"]).map_elements(lambda x: np.arctan2(x["Opp"], x["Adj"])).alias("arm_angle_rad")
|
| 52 |
-
)
|
| 53 |
-
.with_columns(
|
| 54 |
-
pl.col("arm_angle_rad").degrees().alias("arm_angle")
|
| 55 |
-
)
|
| 56 |
-
#.drop(["Opp", "arm_angle_rad"])
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
df_filter = df_filter.with_columns(
|
| 60 |
-
((pl.col("arm_angle") *
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
return df_filter
|
|
|
|
| 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).drop_nulls(subset=["release_pos_x", "release_pos_z"])
|
| 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 |
+
ball_angle = df_arm_angle.filter(pl.col("pitcher") == pitcher_id)["ball_angle"][0]
|
| 40 |
+
|
| 41 |
+
print(shoulder_x, shoulder_z)
|
| 42 |
+
|
| 43 |
+
df_filter = (df_filter.with_columns(
|
| 44 |
+
|
| 45 |
+
)
|
| 46 |
+
.with_columns(
|
| 47 |
+
(pl.col("release_pos_z") - shoulder_z).alias("Opp"),
|
| 48 |
+
(pl.col("release_pos_x") - shoulder_x*-1).alias("Adj"),
|
| 49 |
+
)
|
| 50 |
+
.with_columns(
|
| 51 |
+
pl.struct(["Opp", "Adj"]).map_elements(lambda x: np.arctan2(x["Opp"], x["Adj"])).alias("arm_angle_rad")
|
| 52 |
+
)
|
| 53 |
+
.with_columns(
|
| 54 |
+
pl.col("arm_angle_rad").degrees().alias("arm_angle")
|
| 55 |
+
)
|
| 56 |
+
#.drop(["Opp", "arm_angle_rad"])
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
df_filter = df_filter.with_columns(
|
| 60 |
+
((pl.col("arm_angle") * 0.5) + (ball_angle * 0.5)).alias("arm_angle")
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
return df_filter
|