Update calcs_defense.py

Adding error checking for defense
This commit is contained in:
Cal Corum 2024-05-13 15:35:28 -05:00
parent 585b036969
commit 68270781a1

View File

@ -18,106 +18,118 @@ async def create_positions(
no_data = True
for pos_data in [(df_1b, '1b'), (df_2b, '2b'), (df_3b, '3b'), (df_ss, 'ss')]:
if df_data['key_bbref'] in pos_data[0].index:
logging.debug(f'Running {pos_data[1]} stats for {df_data["p_name"]}')
no_data = False
average_range = (int(pos_data[0].at[df_data["key_bbref"], 'tz_runs_total']) +
int(pos_data[0].at[df_data["key_bbref"], 'bis_runs_total']) +
min(
int(pos_data[0].at[df_data["key_bbref"], 'tz_runs_total']),
int(pos_data[0].at[df_data["key_bbref"], 'bis_runs_total'])
)) / 3
logging.info(f'Running {pos_data[1]} stats for {df_data["p_name"]}')
try:
average_range = (int(pos_data[0].at[df_data["key_bbref"], 'tz_runs_total']) +
int(pos_data[0].at[df_data["key_bbref"], 'bis_runs_total']) +
min(
int(pos_data[0].at[df_data["key_bbref"], 'tz_runs_total']),
int(pos_data[0].at[df_data["key_bbref"], 'bis_runs_total'])
)) / 3
position_payload.append({
"player_id": int(df_data['player_id']),
"position": pos_data[1].upper(),
"innings": float(pos_data[0].at[df_data["key_bbref"], 'Inn_def']),
"range": get_if_range(
pos_code=pos_data[1],
tz_runs=round(average_range),
r_dp=0,
season_pct=season_pct
),
"error": get_any_error(
pos_code=pos_data[1],
errors=int(pos_data[0].at[df_data["key_bbref"], 'E_def']),
chances=int(pos_data[0].at[df_data["key_bbref"], 'chances']),
season_pct=season_pct
)
})
position_payload.append({
"player_id": int(df_data['player_id']),
"position": pos_data[1].upper(),
"innings": float(pos_data[0].at[df_data["key_bbref"], 'Inn_def']),
"range": get_if_range(
pos_code=pos_data[1],
tz_runs=round(average_range),
r_dp=0,
season_pct=season_pct
),
"error": get_any_error(
pos_code=pos_data[1],
errors=int(pos_data[0].at[df_data["key_bbref"], 'E_def']),
chances=int(pos_data[0].at[df_data["key_bbref"], 'chances']),
season_pct=season_pct
)
})
no_data = False
except Exception as e:
logging.info(f'Infield position failed: {e}')
of_arms = []
of_payloads = []
for pos_data in [(df_lf, 'lf'), (df_cf, 'cf'), (df_rf, 'rf')]:
if df_data["key_bbref"] in pos_data[0].index:
try:
average_range = (int(pos_data[0].at[df_data["key_bbref"], 'tz_runs_total']) +
int(pos_data[0].at[df_data["key_bbref"], 'bis_runs_total']) +
min(
int(pos_data[0].at[df_data["key_bbref"], 'tz_runs_total']),
int(pos_data[0].at[df_data["key_bbref"], 'bis_runs_total'])
)) / 3
of_payloads.append({
"player_id": int(df_data['player_id']),
"position": pos_data[1].upper(),
"innings": float(pos_data[0].at[df_data["key_bbref"], 'Inn_def']),
"range": get_of_range(
pos_code=pos_data[1],
tz_runs=round(average_range),
season_pct=season_pct
)
})
of_arms.append(int(pos_data[0].at[df_data["key_bbref"], 'bis_runs_outfield']))
no_data = False
except Exception as e:
logging.info(f'Outfield position failed: {e}')
if df_data["key_bbref"] in df_of.index and len(of_arms) > 0 and len(of_payloads) > 0:
try:
error_rating = get_any_error(
pos_code=pos_data[1],
errors=int(df_of.at[df_data["key_bbref"], 'E_def']),
chances=int(df_of.at[df_data["key_bbref"], 'chances']),
season_pct=season_pct
)
arm_rating = arm_outfield(of_arms)
for f in of_payloads:
f['error'] = error_rating
f['arm'] = arm_rating
position_payload.append(f)
no_data = False
average_range = (int(pos_data[0].at[df_data["key_bbref"], 'tz_runs_total']) +
int(pos_data[0].at[df_data["key_bbref"], 'bis_runs_total']) +
min(
int(pos_data[0].at[df_data["key_bbref"], 'tz_runs_total']),
int(pos_data[0].at[df_data["key_bbref"], 'bis_runs_total'])
)) / 3
of_payloads.append({
except Exception as e:
logging.info(f'Outfield position failed: {e}')
if df_data["key_bbref"] in df_c.index:
try:
if df_c.at[df_data["key_bbref"], 'SB'] + df_c.at[df_data["key_bbref"], 'CS'] == 0:
arm_rating = 3
else:
arm_rating = arm_catcher(
cs_pct=df_c.at[df_data["key_bbref"], 'caught_stealing_perc'],
raa=int(df_c.at[df_data["key_bbref"], 'bis_runs_catcher_sb']),
season_pct=season_pct
)
position_payload.append({
"player_id": int(df_data['player_id']),
"position": pos_data[1].upper(),
"innings": float(pos_data[0].at[df_data["key_bbref"], 'Inn_def']),
"range": get_of_range(
pos_code=pos_data[1],
tz_runs=round(average_range),
"position": 'C',
"innings": float(df_c.at[df_data["key_bbref"], 'Inn_def']),
"range": range_catcher(
rs_value=int(df_c.at[df_data["key_bbref"], 'tz_runs_catcher']),
season_pct=season_pct
),
"error": get_any_error(
pos_code='c',
errors=int(df_c.at[df_data["key_bbref"], 'E_def']),
chances=int(df_c.at[df_data["key_bbref"], 'chances']),
season_pct=season_pct
),
"arm": arm_rating,
"pb": pb_catcher(
pb=int(df_c.at[df_data["key_bbref"], 'PB']),
innings=int(float(df_c.at[df_data["key_bbref"], 'Inn_def'])),
season_pct=season_pct
),
"overthrow": ot_catcher(
errors=int(df_c.at[df_data["key_bbref"], 'E_def']),
chances=int(df_c.at[df_data["key_bbref"], 'chances']),
season_pct=season_pct
)
})
of_arms.append(int(pos_data[0].at[df_data["key_bbref"], 'bis_runs_outfield']))
if df_data["key_bbref"] in df_of.index and len(of_arms) > 0 and len(of_payloads) > 0:
no_data = False
error_rating = get_any_error(
pos_code=pos_data[1],
errors=int(df_of.at[df_data["key_bbref"], 'E_def']),
chances=int(df_of.at[df_data["key_bbref"], 'chances']),
season_pct=season_pct
)
arm_rating = arm_outfield(of_arms)
for f in of_payloads:
f['error'] = error_rating
f['arm'] = arm_rating
position_payload.append(f)
if df_data["key_bbref"] in df_c.index:
if df_c.at[df_data["key_bbref"], 'SB'] + df_c.at[df_data["key_bbref"], 'CS'] == 0:
arm_rating = 3
else:
arm_rating = arm_catcher(
cs_pct=df_c.at[df_data["key_bbref"], 'caught_stealing_perc'],
raa=int(df_c.at[df_data["key_bbref"], 'bis_runs_catcher_sb']),
season_pct=season_pct
)
no_data = False
position_payload.append({
"player_id": int(df_data['player_id']),
"position": 'C',
"innings": float(df_c.at[df_data["key_bbref"], 'Inn_def']),
"range": range_catcher(
rs_value=int(df_c.at[df_data["key_bbref"], 'tz_runs_catcher']),
season_pct=season_pct
),
"error": get_any_error(
pos_code='c',
errors=int(df_c.at[df_data["key_bbref"], 'E_def']),
chances=int(df_c.at[df_data["key_bbref"], 'chances']),
season_pct=season_pct
),
"arm": arm_rating,
"pb": pb_catcher(
pb=int(df_c.at[df_data["key_bbref"], 'PB']),
innings=int(float(df_c.at[df_data["key_bbref"], 'Inn_def'])),
season_pct=season_pct
),
"overthrow": ot_catcher(
errors=int(df_c.at[df_data["key_bbref"], 'E_def']),
chances=int(df_c.at[df_data["key_bbref"], 'chances']),
season_pct=season_pct
)
})
no_data = False
except Exception as e:
logging.info(f'Catcher position failed: {e}')
if no_data:
position_payload.append({
@ -215,7 +227,7 @@ def range_shortstop(tz_runs: int, r_dp: int, season_pct: float):
def get_if_range(pos_code: str, tz_runs: int, r_dp: int, season_pct: float):
logging.debug(f'pos: {pos_code} / tz_runs: {tz_runs} ({type(tz_runs)})')
logging.info(f'pos: {pos_code} / tz_runs: {tz_runs} ({type(tz_runs)})')
if pos_code == '1b':
return range_first_base(tz_runs, 0, season_pct)
elif pos_code == '2b':