#!/usr/bin/env python # coding: utf-8 # In[1]: from collections import namedtuple import numpy.matlib as mat import numpy as np from numpy.linalg import inv Match = namedtuple("Match", ["teams", "score"]) def CalcOpr(matches): """ Given a list of matches, to least squares OPR calculation """ num_matches = len(matches) # rows teams = list(set().union(*[match.teams for match in matches])) num_teams = len(teams) alliances = mat.zeros((num_matches, num_teams)) scores = mat.zeros((num_matches, 1)) for idx, match in enumerate(matches): scores[idx, 0] = match.score for team in match.teams: alliances[idx, teams.index(team)] = 1 least_squares_approx = np.dot( inv(np.dot(np.transpose(alliances), alliances)), np.dot(np.transpose(alliances), scores)) oprs = {} for idx, team in enumerate(teams): oprs[team] = least_squares_approx[idx, 0] return oprs print(CalcOpr([ Match(teams=['A', 'B'], score=10), Match(teams=['A', 'C'], score=13), Match(teams=['B', 'C'], score=7), Match(teams=['A', 'D'], score=15), Match(teams=['B', 'D'], score=10), ])) # In[2]: import tbapy from pprint import pprint api_key = "6qOZ9uAEsb4CDrOBNG6ZnIdi9cWBaZ6DHnCSato97Qfo7bBeUwT9NfFt4Gi5sHFN" tba = tbapy.TBA(api_key) pprint(tba.status()) # In[3]: matches = tba.event_matches('2017roe') # In[4]: match_outcomes = [] for match in matches: match_outcomes.append( Match(teams=match['alliances']['blue']['team_keys'], score=match['alliances']['blue']['score']) ) match_outcomes.append( Match(teams=match['alliances']['red']['team_keys'], score=match['alliances']['red']['score']) ) score_oprs = sorted(CalcOpr(match_outcomes).items(), key=lambda x: -x[1]) pprint(score_oprs) # In[5]: match_outcomes = [] for match in matches: match_outcomes.append( Match(teams=match['alliances']['blue']['team_keys'], score=match['score_breakdown']['blue']['kPaRankingPointAchieved']) ) match_outcomes.append( Match(teams=match['alliances']['red']['team_keys'], score=match['score_breakdown']['red']['kPaRankingPointAchieved']) ) kpa_oprs = CalcOpr(match_outcomes) pprint(kpa_oprs) # In[6]: true_positives = 0 true_negatives = 0 false_positives = 0 false_negatives = 0 for match in matches: for alliance in ['red', 'blue']: result = match['score_breakdown'][alliance]['kPaRankingPointAchieved'] prediction = sum( kpa_oprs[team] for team in match['alliances'][alliance]['team_keys'] ) > 0.5 if result and prediction: true_positives += 1 elif result and not prediction: false_negatives += 1 elif not result and prediction: false_positives += 1 elif not result and not prediction: true_negatives += 1 print("True positives", true_positives) print("True negatives", true_negatives) print("False positives", false_positives) print("False negatives", false_negatives) print("Correctness", (true_positives + true_negatives) / (true_positives + true_negatives + false_positives + false_negatives + 0.0)) # In[7]: match_outcomes = [] for match in matches: match_outcomes.append( Match(teams=match['alliances']['blue']['team_keys'], score=match['score_breakdown']['blue']['rotorRankingPointAchieved']) ) match_outcomes.append( Match(teams=match['alliances']['red']['team_keys'], score=match['score_breakdown']['red']['rotorRankingPointAchieved']) ) rotor_oprs = CalcOpr(match_outcomes) pprint(rotor_oprs) # In[8]: true_positives = 0 true_negatives = 0 false_positives = 0 false_negatives = 0 for match in matches: for alliance in ['red', 'blue']: result = match['score_breakdown'][alliance]['rotorRankingPointAchieved'] prediction = sum( rotor_oprs[team] for team in match['alliances'][alliance]['team_keys'] ) > 0.5 if result and prediction: true_positives += 1 elif result and not prediction: false_negatives += 1 elif not result and prediction: false_positives += 1 elif not result and not prediction: true_negatives += 1 print("True positives", true_positives) print("True negatives", true_negatives) print("False positives", false_positives) print("False negatives", false_negatives) print("Correctness", (true_positives + true_negatives) / (true_positives + true_negatives + false_positives + false_negatives + 0.0))