def calcYearMCA(team, year): teamEvents = gen.readTeamCsv("frc" + str(team), 'events', year) yearMCA = 0 #Check if team actually had events in the current year, if not then zero mCA if teamEvents is not None: isOfficial = teamEvents['Type'].between(eventTypes.REGIONAL, eventTypes.FOC, inclusive=True) officialEvents = teamEvents[isOfficial] #Make sure the team actually had official events before trying any calc if not officialEvents.empty: teamYearAwards = gen.readTeamCsv('frc' + str(team), 'awards', year) #If the team didn't win any awards then don't bother calculating per event mCA if teamYearAwards is not None and not teamYearAwards.empty: for event in officialEvents['Event']: yearMCA += calcEventMCA(team, event) #Previous year Elo values above the baseline increase mCA rating if year == endYear: teamPrevElo = df.loc[team, str(year) + ' Elo'] if teamPrevElo > eloBaseLine: yearMCA += eloScale * (teamPrevElo - eloBaseLine) return yearMCA
def getPerformanceData(team, year): team = gen.teamString(team) oprs = [] wins = 0 losses = 0 ties = 0 try: matches = gen.readTeamCsv(team, 'matches', year) if len(matches) > 0: for idx, match in matches.iterrows(): result = gen.matchResult(team, match) wins += 'WIN' == result losses += 'LOSS' == result ties += 'TIE' == result except: print("Could not retrieve matches for", team) try: events = gen.readTeamCsv(team, 'events', year) if len(events) > 0: for idx, e in events.iterrows(): try: eOprs = gen.readEventCsv(e['Event'], 'opr') teamOPR = eOprs[eOprs.Team == team]['OPR'].values[0] oprs.append(teamOPR) except Exception as e: print(e) except: print("Could not retrieve events for", team) if len(oprs) is 0: maxOPR = 0 avgOPR = 0 else: maxOPR = max(oprs) avgOPR = stat.mean(oprs) played = wins + losses + ties winPercent = 0 if played > 0: winPercent = wins / played teamKey = gen.teamNumber(team) return { 'Team': teamKey, 'Max OPR': maxOPR, 'Avg OPR': avgOPR, 'Wins': wins, 'Losses': losses, 'Ties': ties, 'Win %': winPercent }
def getData(year, team): playPoints = 0 awardPoints = 0 teamEvents = gen.readTeamCsv(team, 'events', year) if teamEvents is not None: official = teamEvents[teamEvents.Type < 10] if len(official) > 0: firstEvent = official.iloc[0]['Event'] playPoints, awardPoints = teamAtEvent(team, firstEvent) return {'team': team, 'playPoints': playPoints, 'awardPoints': awardPoints}
def getTeamRatingData(team, yearDepth=3, YEAR=None): overallRating = 0 playRating = 0 teamTotal = 0 eventMax = 0 eventCount = 0 eventAvg = 0 team = gen.teamString(team) teamYears = getTeamYears(team, YEAR) pastYears = sorted(teamYears[-yearDepth:], key=int, reverse=True) tmpYears = pastYears[:] for year in tmpYears: if year < YEAR - yearDepth: pastYears.remove(year) for count, year in enumerate(pastYears): yearPoints = 0 yearPlayPoints = 0 for event in gen.readTeamCsv(team, 'events', year)['Event']: eventPlayPoints, eventAwardPoints = getTeamEventPoints(team, event) eventPoints = eventPlayPoints + eventAwardPoints yearPoints += eventPoints yearPlayPoints += eventPlayPoints if eventPoints > 0: eventCount += 1 eventMax = eventPoints if eventPoints > eventMax else eventMax playRating += yearPlayPoints / pow(2, count * 2) overallRating += yearPoints / pow(2, count * 2) teamTotal += yearPoints eventAvg = 0 if eventCount > 0: eventAvg = teamTotal / eventCount teamKey = gen.teamNumber(team) return { 'Team': teamKey, 'Overall Rating': overallRating, 'Event Max': eventMax, 'Total Points': teamTotal, 'Year Avg': teamTotal / yearDepth, 'Event Avg': eventAvg, 'Events': eventCount, 'Play Rating': playRating }
def saveTeamYearMatches(year, team): fileExists, fullPath = filePathHandler('teams', team, 'matches', year) fileExists = False if not fileExists: try: teamEvents = gen.readTeamCsv(team, 'events', year) teamMatches = pd.concat( gen.teamEventMatches(team, event) for event in teamEvents['Event']) teamMatches.to_csv(fullPath + '.csv', index=False) except Exception as e: print(e)
def saveTeamAwards(year, team): fileExists, fullPath = filePathHandler('teams', team, 'awards', year) eventData = gen.readTeamCsv(team, 'events', year) fileExists = False fileExists = False if not fileExists: try: teamYearAwards = [] for event in eventData['Event']: awardsExist, awardsPath = filePathHandler( 'events', event, 'awards') if awardsExist: evAwards = pd.read_csv(awardsPath + '.csv', index_col=False, names=['Award', 'Name', 'Team']) evAwards.Team = evAwards.Team.str.strip() if type(evAwards['Team'][0]) is not str: evAwards['Team'] = 'frc' + evAwards['Team'].astype(str) teamAwards = evAwards[evAwards.Team == team] for idx, row in teamAwards.iterrows(): awardType = row['Award'].split('_')[1] awardName = row['Name'] teamYearAwards.append({ 'Event': event, 'Type': awardType, 'Name': awardName }) colOrder = ['Event', 'Type', 'Name'] gen.listOfDictToCSV(fullPath, teamYearAwards, colOrder, False) except Exception as e: print(e)
def calcEventMCA(team, event): eventMCA = 0 teamAwards = gen.readTeamCsv('frc' + str(team), 'awards', int(event[:4])) #Don't bother doing calc if the team didn't win awards that year #This check is duplicated in calcYearMCA, but included here as well in case #function is run independently. if teamAwards is not None: eventFilter = teamAwards['Event'] == event currentAwards = teamAwards[eventFilter] #If the team didn't win any awards at this event don't calc. if not currentAwards.empty: eventValuation = len(gen.readEventCsv(event, 'teams')) / baseTeamCount for idx, award in currentAwards.iterrows(): if award['Type'] in awardValues: #For CA Finalist or Honorable Mention treat it as 100 team event if award['Type'] == awardTypes.CHAIRMANS_FINALIST or award[ 'Type'] == awardTypes.CHAIRMANS_HONORABLE_MENTION: eventValuation = 2 eventMCA += eventValuation * awardValues[award['Type']] return eventMCA
for year in years: yr = str(year) dist = yr + district yearData[yr] = {} yearData[yr]['Teams'] = tba.district_teams(dist, False, True) yearData[yr]['Points'] = { t['team_key']: t['point_total'] for t in tba.district_rankings(dist) } currentTeams = tba.district_teams(str(currentYear) + district, False, True) teamData = [] for team in currentTeams: events = gen.readTeamCsv(team, 'events', currentYear - 1) tm = tba.team(team) maxOPR = 0 totalOPR = 0 avgOPR = 0 eventAdjust = 0 if events is not None: seasonEvents = events[events.Type < 4]['Event'] for event in seasonEvents: eventData = gen.readEventCsv(event, 'opr') teamRow = eventData[eventData['Team'] == team] if teamRow.empty: eventAdjust += 1
teamList[year] = gen.readTeamListCsv(year)['Teams'].tolist() eventList[year] = slff.fetchNormalEventData(year) #teams = gen.readTeamListCsv(2019)['Teams'].tolist() teams = tba.district_teams('2019tx', False, True) #teams = ['frc125', 'frc401', 'frc7179'] eventFields = ['Code', 'Type', 'Week', 'Total Points', 'Award Points', 'Award Names', 'Draft Points', 'Rank Points', 'Elim Points', 'Rank'] teamData = {} for team in tqdm(teams): teamData[team] = {} for year in yearRange: teamData[team][year] = [] if team in teamList[year]: teamEvents = gen.readTeamCsv(team, 'events', year) for idx, event in teamEvents.iterrows(): eventKey = event['Event'] eventType = 'District' if event['Type'] == 1 else 'Regional' if eventKey in eventList[year]: awardData = slff.getAwardPoints(team, eventKey) playData = slff.getPlayPoints(team, eventKey) totalPoints = sum(playData[:3]) + awardData[0] eventDict = [eventKey, eventType, eventList[year][eventKey], totalPoints] + awardData + playData eventPacket = {} for idx, field in enumerate(eventFields): eventPacket[field] = eventDict[idx] teamData[team][year].append(eventPacket) def prepFields(): fields = ['Team']
def getAwardPoints(team, event, eventType, useTba=False): ROBOT_AWARDS = [16, 17, 20, 21, 29, 71] OTHER_AWARDS = [5, 11, 13, 14, 18, 22, 27, 30] NO_POINTS = [1, 2] CA = 0 EI = 9 WF = 3 DEANS = 4 RAS = 10 RI = 15 CAF = 69 WILDCARD = 68 awardData = { 'REG': { CA: 60, EI: 45, RAS: 25, RI: 15, WF: 10, DEANS: 5, WILDCARD: 0, 'ROBOT': 20, 'OTHER': 5, 'NONE': 0 }, 'CMP': { CA: 110, EI: 60, RAS: 35, RI: 20, WF: 30, DEANS: 15, CAF: 90, 'ROBOT': 30, 'OTHER': 10, 'NONE': 0 } } awardPoints = 0 if useTba: try: tya = tba.team_awards(team, None, event) for award in tya: awardType = award['award_type'] if awardType in ROBOT_AWARDS: awardType = 'ROBOT' if awardType in OTHER_AWARDS: awardType = 'OTHER' if awardType in NO_POINTS: awardType = 'NONE' awardPoints += awardData[eventType][awardType] except: pass else: tya = gen.readTeamCsv(team, 'awards', event[:4]) if tya is not None: teamEventAwards = tya[tya.Event == event] for idx, row in teamEventAwards.iterrows(): awardType = row['Type'] if awardType in ROBOT_AWARDS: awardType = 'ROBOT' if awardType in OTHER_AWARDS: awardType = 'OTHER' if awardType in NO_POINTS: awardType = 'NONE' if awardType is CA and eventType == 'CMP': awardPoints -= 90 awardPoints += awardData[eventType][awardType] return awardPoints
import gen tba = gen.setup() dist = 'ont' startYear = 2014 currYear = 2018 distTeams = tba.district_teams(str(currYear) + dist, False, True) records = [] for team in distTeams: wins = 0 for year in range(startYear, currYear + 1): awards = gen.readTeamCsv(team, 'awards', year) if awards is not None: for idx, award in awards.iterrows(): if award['Type'] == 1: wins += 1 records.append({'Team': team[3:], 'Wins': wins}) records = sorted(records, key=lambda k: k['Wins'], reverse=True) gen.listOfDictToCSV( dist.upper() + " " + str(startYear) + " to " + str(currYear) + " Wins", records, ['Team', 'Wins'])
import gen teams = gen.readTeamListCsv(2018)['Teams'] records = [] for team in teams: matches = gen.readTeamCsv(team, 'matches', 2018) wins = 0 losses = 0 ties = 0 if matches is not None: for idx, match in matches.iterrows(): result = gen.matchResult(team, match) wins += result == 'WIN' losses += result == 'LOSS' ties += result == 'TIE' total = wins + losses + ties if total != 0: if wins == 0: wins = 1 total = total * 10 rating = wins / total wins = 0 else: rating = wins / total recordString = str(wins) + ' / ' + str(losses) + ' / ' + str(ties) records.append({
import slff currentYear = 2018 rookieYear = 2006 prevTeams = gen.readTeamListCsv(rookieYear - 1)['Teams'] currTeams = gen.readTeamListCsv(rookieYear)['Teams'] rookies = [] for team in currTeams: prevYears = slff.getTeamYears(team, rookieYear - 1) if len(prevYears) == 0: print(team) #Event data (ranks, opr) events = gen.readTeamCsv(team, 'events', currentYear) ranks = [] oprs = [] avgOpr = 0 avgRank = 99 if events is not None: for event in events['Event']: rank = gen.readEventCsv(event, 'rankings') opr = gen.readEventCsv(event, 'opr') if opr is not None: oprs.append(opr[opr.Team == team]['OPR'].values[0]) else: print("Couldn't get OPR data for", event) if rank is not None: if len(rank.columns) > 2:
def teamAtEvent(team, event): ROBOT_AWARDS = [16, 17, 20, 21, 29, 71] OTHER_AWARDS = [5, 11, 13, 14, 18, 22, 27, 30] NO_POINTS = [1, 2] CA = 0 EI = 9 WF = 3 DEANS = 4 RAS = 10 RI = 15 CAF = 69 WILDCARD = 68 awardData = { 'REG': { CA: 25, EI: 15, RAS: 5, RI: 5, WF: 5, DEANS: 5, WILDCARD: 0, 'ROBOT': 10, 'OTHER': 5, 'NONE': 0 }, 'CMP': { CA: 110, EI: 60, RAS: 35, RI: 20, WF: 30, DEANS: 15, CAF: 90, 'ROBOT': 30, 'OTHER': 10, 'NONE': 0 } } awardPoints = 0 playPoints = 0 #Fetch event info, do this first since we only want to really process official events. eventInfo = gen.readEventCsv(event, 'info') if eventInfo is not None: isOfficial = eventInfo['Type'][0] in range(0, 7) isChamps = eventInfo['Type'][0] in range(3, 5) eventType = 'CMP' if isChamps else 'REG' if isOfficial: playPoints = calcPoints(team, event) tya = gen.readTeamCsv(team, 'awards', event[:4]) if tya is not None: teamEventAwards = tya[tya.Event == event] for idx, row in teamEventAwards.iterrows(): awardType = row['Type'] if awardType in ROBOT_AWARDS: awardType = 'ROBOT' if awardType in OTHER_AWARDS: awardType = 'OTHER' if awardType in NO_POINTS: awardType = 'NONE' awardPoints += awardData[eventType][awardType] return [playPoints, awardPoints]
def getAwardPoints(team, event): ROBOT_AWARDS = [16, 17, 20, 21, 29, 71] OTHER_AWARDS = [11, 13, 18, 22, 27, 30] NO_POINTS = [1, 2, 5, 14] CA = 0 EI = 9 WF = 3 DEANS = 4 RAS = 10 RI = 15 WILDCARD = 68 numberToName = { 0: 'CA', 1: 'Winner', 2: 'Finalist', 9: 'EI', 3: 'WF', 4: 'DEANS', 10: 'RAS', 15: 'RI', 16: 'ID', 17: 'Quality', 20: 'Creativity', 21: 'EE', 29: 'Control', 71: 'Auto', 11: 'GP', 13: 'Judges', 18: 'Safety', 22: 'Ent', 27: 'Imagery', 30: 'Spirit' } awardValues = { CA: 60, EI: 45, RAS: 25, RI: 15, WF: 10, DEANS: 5, WILDCARD: 0, 'ROBOT': 20, 'OTHER': 5, 'NONE': 0 } awardPoints = 0 awardNames = [] tya = gen.readTeamCsv(team, 'awards', event[:4], ['Event', 'Type', 'Name']) if tya is not None: teamEventAwards = tya[tya.Event == event] for idx, row in teamEventAwards.iterrows(): awardType = row['Type'] if awardType in ROBOT_AWARDS: awardType = 'ROBOT' if awardType in OTHER_AWARDS: awardType = 'OTHER' if awardType in NO_POINTS: awardType = 'NONE' if row['Type'] in numberToName.keys(): awardNames.append(numberToName[row['Type']]) awardPoints += awardValues[awardType] return [awardPoints, ' / '.join(awardNames)]
yearStart = 2016 yearEnd = 2018 teamList = {} eventList = {} for year in range(yearStart, 2020): teamList[year] = gen.readTeamListCsv(year)['Teams'].tolist() eventList[year] = slff.fetchNormalEventData(year) maxEvents = 0 maxTeam = '' maxYear = 0 maxEventCodes = [] for team in tqdm(teamList[2019]): for year in range(yearStart, 2019): teamEvents = 0 eventCodes = [] eventDF = gen.readTeamCsv(team, 'events', year) if eventDF is not None: for event in eventDF['Event']: if event in eventList[year]: eventCodes.append(event) teamEvents += 1 if teamEvents > maxEvents: maxEventCodes = eventCodes maxEvents = teamEvents maxTeam = team maxYear = year print(maxEvents, maxTeam, year) print(maxEventCodes)