Added EPA to stats recorded

This commit is contained in:
Ryan Fitz-Gerald
2024-10-15 14:30:56 -04:00
parent ac02e0b65c
commit 5f05e63e23

View File

@@ -2,6 +2,7 @@ from pathlib import Path
from typing import Optional from typing import Optional
import tbaapiv3client import tbaapiv3client
from tbaapiv3client.rest import ApiException from tbaapiv3client.rest import ApiException
import statbotics
from pprint import pprint from pprint import pprint
from datetime import datetime as dt from datetime import datetime as dt
@@ -19,25 +20,49 @@ config = tbaapiv3client.Configuration(
config.verify_ssl = False config.verify_ssl = False
district = 'fim'
def get_rankings(api_client: tbaapiv3client.ApiClient, seasons: list[str]) -> dict[dict]:
def get_rankings(api_client: tbaapiv3client.ApiClient, seasons: list[int]) -> dict[dict]:
# Create an instance of the API class # Create an instance of the API class
api_instance = tbaapiv3client.DistrictApi(api_client) api_instance = tbaapiv3client.DistrictApi(api_client)
rankings = {} rankings = {}
for season in seasons: for season in seasons:
year = int(season[:4])
try: try:
results = api_instance.get_district_rankings(season) results = api_instance.get_district_rankings(season)
except ApiException as e: except ApiException as e:
print("Exception when calling DistrictApi->get_district_rankings: %s\n" % e) print("Exception when calling DistrictApi->get_district_rankings: %s\n" % e)
else: else:
epa_list:dict[int] = {}
try:
sb = statbotics.Statbotics()
sb.session.verify = False
sb_results = sb.get_team_years(year=year,district=district, limit=1000, fields=['team', 'epa_end'])
except Exception as e:
print("Exception when calling Statbotics DistrictApi->get_district_rankings: %s\n" % e)
else:
for sb_result in sb_results:
epa_list[sb_result['team']] = sb_result['epa_end']
for result in results: for result in results:
team = result.team_key team = result.team_key
team_num = int(team[3:])
if team not in rankings: if team not in rankings:
rankings[team] = {} rankings[team] = {}
epa = ''
if team_num in epa_list:
epa = epa_list[team_num]
points_event1 = None points_event1 = None
points_event1_na = None points_event1_na = None
points_event2 = None points_event2 = None
@@ -56,12 +81,15 @@ def get_rankings(api_client: tbaapiv3client.ApiClient, seasons: list[str]) -> di
rankings[team][season] = { rankings[team][season] = {
"rank": result.rank, "rank": result.rank,
"point_total": result.point_total, "point_total": result.point_total,
"epa_end": epa,
"points_event1": points_event1, "points_event1": points_event1,
"points_event2": points_event2, "points_event2": points_event2,
"points_event1_na": points_event1_na, "points_event1_na": points_event1_na,
"points_event2_na": points_event2_na "points_event2_na": points_event2_na,
} }
return rankings return rankings
@@ -111,6 +139,7 @@ def write_power_rank(path: str | Path, sheet_name: str, rankings: list[dict], se
rank_sum = 0 rank_sum = 0
point_total_sum = 0 point_total_sum = 0
epa_sum = 0
point_district_sum = 0 point_district_sum = 0
point_district_na_sum = 0 point_district_na_sum = 0
@@ -119,12 +148,14 @@ def write_power_rank(path: str | Path, sheet_name: str, rankings: list[dict], se
for season in seasons: for season in seasons:
rank = '' rank = ''
point_total = '' point_total = ''
epa = ''
point_district = '' point_district = ''
point_district_na = '' point_district_na = ''
if season in ranks: if season in ranks:
rank = ranks[season]['rank'] rank = ranks[season]['rank']
point_total = ranks[season]['point_total'] point_total = ranks[season]['point_total']
epa = ranks[season]['epa_end']
point_district = 0 point_district = 0
@@ -147,26 +178,33 @@ def write_power_rank(path: str | Path, sheet_name: str, rankings: list[dict], se
point_district_sum += point_district point_district_sum += point_district
point_district_na_sum += point_district_na point_district_na_sum += point_district_na
if epa != '':
epa_sum += epa
entry_count += 1 entry_count += 1
result[f'{season} Rank'] = rank result[f'{season} Rank'] = rank
result[f'{season} Point Total'] = point_total result[f'{season} Point Total'] = point_total
result[f'{season} EPA'] = epa
result[f'{season} Points District'] = point_district result[f'{season} Points District'] = point_district
result[f'{season} Points District No Awards'] = point_district_na result[f'{season} Points District No Awards'] = point_district_na
rank_avg = '' rank_avg = ''
point_total_avg = '' point_total_avg = ''
epa_avg = ''
point_district_avg = '' point_district_avg = ''
point_district_na_avg = '' point_district_na_avg = ''
if entry_count > 0: if entry_count > 0:
rank_avg = rank_sum / entry_count rank_avg = rank_sum / entry_count
point_total_avg = point_total_sum / entry_count point_total_avg = point_total_sum / entry_count
epa_avg = epa_sum / entry_count
point_district_avg = point_district_sum / entry_count point_district_avg = point_district_sum / entry_count
point_district_na_avg = point_district_na_sum / entry_count point_district_na_avg = point_district_na_sum / entry_count
result[f'Avg Rank'] = rank_avg result[f'Avg Rank'] = rank_avg
result[f'Avg Point Total'] = point_total_avg result[f'Avg Point Total'] = point_total_avg
result[f'Avg EPA'] = epa_avg
result[f'Avg Points District'] = point_district_avg result[f'Avg Points District'] = point_district_avg
result[f'Avg Points District No Awards'] = point_district_na_avg result[f'Avg Points District No Awards'] = point_district_na_avg
@@ -175,7 +213,7 @@ def write_power_rank(path: str | Path, sheet_name: str, rankings: list[dict], se
headers = ['team'] headers = ['team']
prefix_list = seasons.copy() prefix_list = seasons.copy()
prefix_list.append('Avg') prefix_list.append('Avg')
suffix_list = ['Rank', 'Point Total', suffix_list = ['Rank', 'Point Total', 'EPA',
'Points District', 'Points District No Awards'] 'Points District', 'Points District No Awards']
for suffix in suffix_list: for suffix in suffix_list:
@@ -185,6 +223,85 @@ def write_power_rank(path: str | Path, sheet_name: str, rankings: list[dict], se
write_result(path, results_list, sheet_name, headers=headers) write_result(path, results_list, sheet_name, headers=headers)
def write_event_summary(path: str | Path, seasons: list[str], event_teams: dict[dict], rankings: dict[dict]) -> None:
event_summary: list[dict] = []
for key, event in event_teams.items():
rank_sum = 0
point_total_sum = 0
epa_sum = 0
point_district_sum = 0
point_district_na_sum = 0
entry_count = 0
event_rankings: dict[dict] = {}
for team in event['teams']:
if team in rankings:
event_rankings[team] = rankings[team]
else:
event_rankings[team] = {}
if team in rankings:
for season in seasons:
if season in rankings[team]:
team_season = rankings[team][season]
rank_sum += team_season['rank']
point_total_sum += team_season['point_total']
if team_season['epa_end'] != '':
epa_sum += team_season['epa_end']
if team_season['points_event1'] is not None:
point_district_sum += team_season['points_event1']
if team_season['points_event2'] is not None:
point_district_sum += team_season['points_event2']
if team_season['points_event1_na'] is not None:
point_district_na_sum += team_season['points_event1_na']
if team_season['points_event2_na'] is not None:
point_district_na_sum += team_season['points_event2_na']
entry_count += 1
write_power_rank(path, event['key'], event_rankings, seasons)
# Generate Event Summary
rank_avg = ''
point_total_avg = ''
epa_avg = ''
point_district_avg = ''
point_district_na_avg = ''
if entry_count > 0:
rank_avg = rank_sum / entry_count
point_total_avg = point_total_sum / entry_count
epa_avg = epa_sum / entry_count
point_district_avg = point_district_sum / entry_count
point_district_na_avg = point_district_na_sum / entry_count
event_summary.append({
'key': key,
'name': event['name'],
'start date': event['start date'],
'week': event['week'],
'average rank': rank_avg,
'average point total': point_total_avg,
'average EPA': epa_avg,
'average point district': point_district_avg,
'average point district no awards': point_district_na_avg,
'entries': entry_count,
'team count': len(event['teams'])
})
# Write Event Summary
write_result(path, event_summary, "Summary", 0)
def write_rank(path: str | Path, rankings: list[dict], seasons: list[str]) -> None: def write_rank(path: str | Path, rankings: list[dict], seasons: list[str]) -> None:
# Create annual rank CSV # Create annual rank CSV
results_list: list[dict] = [] results_list: list[dict] = []
@@ -304,94 +421,29 @@ def write_result(path: str | Path, results_list: list[dict], sheet_name: Optiona
def main(): def main():
# Initialize system
run_time = dt.now() run_time = dt.now()
run_time_str = run_time.strftime('%Y%m%d_%H%M') run_time_str = run_time.strftime('%Y%m%d_%H%M')
skip_years = [2020,2021] skip_years = [2020, 2021]
seasons = [f'{year}fim' for year in range(2022, 2025) if year not in skip_years] seasons = [f'{year}{district}' for year in range(
2022, 2025) if year not in skip_years]
root_dir = Path('results') root_dir = Path('results')
# Get Statistics
event_details_path = root_dir / f'{run_time_str} - Event Details.xlsx'
with tbaapiv3client.ApiClient(config) as api_client: with tbaapiv3client.ApiClient(config) as api_client:
print('Getting Team Rankings')
rankings = get_rankings(api_client, seasons) rankings = get_rankings(api_client, seasons)
print('Getting event team lists')
event_teams = get_event_teams(api_client, '2025fim') event_teams = get_event_teams(api_client, '2025fim')
event_summary: list[dict] = []
for key, event in event_teams.items():
rank_sum = 0
point_total_sum = 0
point_district_sum = 0
point_district_na_sum = 0
entry_count = 0
event_rankings: dict[dict] = {}
for team in event['teams']:
if team in rankings:
event_rankings[team] = rankings[team]
else:
event_rankings[team] = {}
if team in rankings:
for season in seasons:
if season in rankings[team]:
team_season = rankings[team][season]
rank_sum += team_season['rank']
point_total_sum += team_season['point_total']
if team_season['points_event1'] is not None:
point_district_sum += team_season['points_event1']
if team_season['points_event2'] is not None:
point_district_sum += team_season['points_event2']
if team_season['points_event1_na'] is not None:
point_district_na_sum += team_season['points_event1_na']
if team_season['points_event2_na'] is not None:
point_district_na_sum += team_season['points_event2_na']
entry_count += 1
write_power_rank(event_details_path, event['key'], event_rankings, seasons)
# Generate Event Summary
rank_avg = ''
point_total_avg = ''
point_district_avg = ''
point_district_na_avg = ''
if entry_count > 0:
rank_avg = rank_sum / entry_count
point_total_avg = point_total_sum / entry_count
point_district_avg = point_district_sum / entry_count
point_district_na_avg = point_district_na_sum / entry_count
event_summary.append({
'key': key,
'name': event['name'],
'start date': event['start date'],
'week': event['week'],
'average rank': rank_avg,
'average point total': point_total_avg,
'average point district': point_district_avg,
'average point district no awards': point_district_na_avg,
'entries': entry_count,
'team count': len(event['teams'])
})
# Write Event Summary # Write Event Summary
print('Writing Event Summary') print('Writing Event Summary')
write_result(event_details_path, event_summary, "Summary", 0) write_event_summary(
root_dir / f'{run_time_str} - Event Details.xlsx', seasons, event_teams, rankings)
# Write Power Rankings # Write Power Rankings
print('Writing Power Ranking') print('Writing Power Ranking')