Skip to content

Instantly share code, notes, and snippets.

@phil-lopreiato
Created April 19, 2016 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phil-lopreiato/a5de477857c87402129a84429c0c5b2f to your computer and use it in GitHub Desktop.
Save phil-lopreiato/a5de477857c87402129a84429c0c5b2f to your computer and use it in GitHub Desktop.
2016 FRC Scheduling With/Against
Event Key Mean Median Max Min
2016abca 27.0 27.0 30 24
2016alhu 29.372549019607842 30 30 27
2016arlr 31.591836734693878 32 34 28
2016ausy 28.27906976744186 28 30 26
2016azfl 29.653846153846153 30.0 32 28
2016azpx 31.72093023255814 32 36 28
2016cada 24.0 24.0 24 24
2016calb 24.0 24.0 24 24
2016cama 29.306122448979593 29 32 27
2016capl 30.904761904761905 31.0 34 28
2016casd 27.0 27.0 27 27
2016casj 24.09375 24.0 26 23
2016cave 30.80952380952381 31.0 35 28
2016chcmp 34.93103448275862 35.0 36 33
2016code 31.0 31.0 33 28
2016cthar 29.85 30.0 34 27
2016ctwat 29.210526315789473 29.0 32 26
2016flor 27.11111111111111 27 30 27
2016flwp 26.966666666666665 27.0 27 26
2016gaalb 20.56 21 22 18
2016gacmp 30.48780487804878 30 33 28
2016gacol 29.2 29.0 33 26
2016gadal 22.444444444444443 22 26 20
2016gaken 27.428571428571427 28 30 24
2016hiho 28.473684210526315 28.0 32 25
2016iacf 29.58490566037736 30 32 27
2016idbo 22.933333333333334 23.0 26 19
2016ilch 29.615384615384617 30.0 31 28
2016ilpe 27.7 28.0 29 25
2016incmp 23.548387096774192 24 26 19
2016inpmh 25.393939393939394 25 29 23
2016inwch 26.61111111111111 27.0 29 22
2016inwla 28.736842105263158 29.0 32 26
2016ista 27.11111111111111 27 30 27
2016lake 29.857142857142858 30.0 31 29
2016mabos 30.05 30.0 32 27
2016manda 25.75 26.0 29 23
2016marea 29.28205128205128 29 34 26
2016mawor 29.85 30.0 34 26
2016mdbet 29.641025641025642 30 32 27
2016mdblr 26.529411764705884 26.0 31 22
2016mdedg 29.794871794871796 30 34 27
2016melew 29.25 29.0 33 26
2016miann 30.05 30.0 33 26
2016mibro 30.1 30.0 34 27
2016micen 30.3 30.0 35 26
2016micmp 36.0 36.0 36 36
2016miesc 29.6 30.0 32 26
2016mihow 29.55 30.0 33 27
2016mike2 30.634146341463413 30 34 27
2016miken 30.3 30.0 33 28
2016miket 30.2 30.0 33 27
2016milak 30.5 31.0 33 28
2016milan 30.2 30.0 33 28
2016miliv 29.7 30.0 33 26
2016milsu 30.78048780487805 31 34 28
2016mimar 30.1 30.0 33 26
2016mimid 30.15 30.0 32 27
2016misjo 30.0 30 33 26
2016misou 29.333333333333332 29 32 25
2016mista 30.15 30.0 33 26
2016mitry 29.487179487179485 30 31 27
2016mitvc 30.3 30.0 33 27
2016miwat 29.692307692307693 30 32 26
2016miwmi 30.585365853658537 31 33 27
2016mndu 24.0 24 24 24
2016mndu2 24.0 24.0 24 24
2016mnmi 24.0 24 24 24
2016mnmi2 24.0 24.0 24 24
2016mokc 29.89830508474576 30 32 29
2016mosl 29.551020408163264 30 31 28
2016mrcmp 35.3 35.0 36 32
2016mxmc 29.58490566037736 30 32 28
2016ncash 20.083333333333332 20.0 22 18
2016ncbui 25.3125 25.0 28 23
2016nccmp 24.875 25.0 27 22
2016ncmcl 23.0 23.0 26 19
2016ncral 25.3125 25.0 28 22
2016necmp 35.714285714285715 36 36 34
2016nhdur 30.0 30.0 32 28
2016nhgrs 24.0 24.0 27 21
2016njbri 28.43243243243243 29 31 26
2016njfla 29.692307692307693 30 34 27
2016njski 29.263157894736842 29.0 33 26
2016njtab 28.526315789473685 29.0 32 25
2016nvlv 30.93877551020408 31 33 28
2016nyli 29.607843137254903 30 30 28
2016nyny 24.0 24.0 24 24
2016nyro 29.142857142857142 29 32 27
2016nytr 27.38888888888889 27.5 30 25
2016ohci 29.428571428571427 30.0 31 28
2016ohcl 26.96551724137931 27.0 27 26
2016okok 27.049180327868854 27 29 26
2016onnb 23.75 24.0 27 20
2016onto 29.306122448979593 29 31 27
2016onto2 27.5609756097561 28 30 25
2016onwa 23.666666666666668 24.0 26 21
2016onwi 26.807692307692307 27.0 27 25
2016orore 25.25 25.0 28 22
2016orphi 23.6 24.0 26 18
2016orwil 27.38888888888889 27.0 30 24
2016paca 29.615384615384617 30.0 32 29
2016pahat 28.216216216216218 28 32 26
2016paphi 23.419354838709676 24 26 20
2016pawch 27.83783783783784 28 31 25
2016pncmp 35.65625 36.0 36 34
2016qcmo 26.92 27.0 27 26
2016ripro 27.333333333333332 28.0 30 24
2016scmb 24.125 24.0 26 24
2016tnkn 28.979591836734695 29 31 26
2016txda 30.41860465116279 30 33 28
2016txho 24.0 24.0 24 24
2016txlu 28.95 29.0 32 25
2016txsa 24.0 24.0 24 24
2016utwv 27.38888888888889 28.0 30 24
2016vabla 26.941176470588236 27.0 30 24
2016vados 29.210526315789473 29.0 32 26
2016vahay 29.17948717948718 29 32 26
2016vapor 27.257142857142856 27 30 24
2016waahs 27.944444444444443 28.0 31 25
2016waamv 29.487179487179485 30 32 25
2016waell 28.82051282051282 28 32 26
2016wamou 27.485714285714284 27 31 25
2016wasno 29.210526315789473 29.0 33 25
2016waspo 26.352941176470587 26.0 30 23
2016wimi 29.660377358490567 30 32 28
Event Key Mean Median Max Min
2016abca 22.0 22.0 22 22
2016alhu 20.0 20 20 20
2016arlr 22.040816326530614 22 24 22
2016ausy 20.093023255813954 20 22 20
2016azfl 20.076923076923077 20.0 22 20
2016azpx 24.0 24 24 24
2016cada 16.0 16.0 16 16
2016calb 16.0 16.0 16 16
2016cama 20.081632653061224 20 22 20
2016capl 24.0 24.0 24 24
2016casd 18.0 18.0 18 18
2016casj 16.125 16.0 18 16
2016cave 24.0 24.0 24 24
2016chcmp 24.0 24.0 24 24
2016code 22.0 22.0 22 22
2016cthar 24.0 24.0 24 24
2016ctwat 24.0 24.0 24 24
2016flor 18.095238095238095 18 20 18
2016flwp 18.0 18.0 18 18
2016gaalb 21.2 21 23 20
2016gacmp 24.0 24 24 24
2016gacol 24.0 24.0 24 24
2016gadal 21.555555555555557 22 24 20
2016gaken 24.0 24 24 24
2016hiho 24.0 24.0 24 24
2016iacf 20.150943396226417 20 22 20
2016idbo 22.066666666666666 22.0 24 20
2016ilch 20.076923076923077 20.0 22 20
2016ilpe 20.1 20.0 22 20
2016incmp 22.838709677419356 23 24 21
2016inpmh 24.0 24 24 24
2016inwch 24.0 24.0 24 24
2016inwla 24.0 24.0 24 24
2016ista 18.095238095238095 18 20 18
2016lake 20.142857142857142 20.0 22 20
2016mabos 24.0 24.0 24 24
2016manda 24.0 24.0 24 24
2016marea 24.0 24 24 24
2016mawor 24.0 24.0 24 24
2016mdbet 24.0 24 24 24
2016mdblr 23.941176470588236 24.0 24 23
2016mdedg 24.0 24 24 24
2016melew 24.0 24.0 24 24
2016miann 24.0 24.0 24 24
2016mibro 24.0 24.0 24 24
2016micen 24.0 24.0 24 24
2016micmp 24.0 24.0 24 24
2016miesc 24.0 24.0 24 24
2016mihow 24.0 24.0 24 24
2016mike2 24.0 24 24 24
2016miken 24.0 24.0 24 24
2016miket 24.0 24.0 24 24
2016milak 24.0 24.0 24 24
2016milan 24.0 24.0 24 24
2016miliv 24.0 24.0 24 24
2016milsu 24.0 24 24 24
2016mimar 24.0 24.0 24 24
2016mimid 24.0 24.0 24 24
2016misjo 24.0 24 24 24
2016misou 24.0 24 24 24
2016mista 24.0 24.0 24 24
2016mitry 24.0 24 24 24
2016mitvc 24.0 24.0 24 24
2016miwat 24.0 24 24 24
2016miwmi 24.0 24 24 24
2016mndu 16.0 16 16 16
2016mndu2 16.0 16.0 16 16
2016mnmi 16.0 16 16 16
2016mnmi2 16.0 16.0 16 16
2016mokc 20.135593220338983 20 22 20
2016mosl 20.081632653061224 20 22 20
2016mrcmp 24.0 24.0 24 24
2016mxmc 20.150943396226417 20 22 20
2016ncash 20.916666666666668 21.0 22 19
2016ncbui 23.875 24.0 24 22
2016nccmp 23.8125 24.0 24 23
2016ncmcl 22.4 22.5 24 20
2016ncral 23.8125 24.0 24 23
2016necmp 24.0 24 24 24
2016nhdur 24.0 24.0 24 24
2016nhgrs 22.5 22.5 24 20
2016njbri 24.0 24 24 24
2016njfla 24.0 24 24 24
2016njski 24.0 24.0 24 24
2016njtab 24.0 24.0 24 24
2016nvlv 22.040816326530614 22 24 22
2016nyli 20.0 20 20 20
2016nyny 16.0 16.0 16 16
2016nyro 20.081632653061224 20 22 20
2016nytr 24.0 24.0 24 24
2016ohci 20.142857142857142 20.0 22 20
2016ohcl 18.0 18.0 18 18
2016okok 18.098360655737704 18 20 18
2016onnb 22.5625 23.0 24 20
2016onto 20.081632653061224 20 22 20
2016onto2 20.195121951219512 20 22 20
2016onwa 23.533333333333335 24.0 25 20
2016onwi 18.0 18.0 18 18
2016orore 24.0 24.0 24 24
2016orphi 22.133333333333333 22.0 24 20
2016orwil 24.0 24.0 24 24
2016paca 20.076923076923077 20.0 22 20
2016pahat 24.0 24 24 24
2016paphi 22.322580645161292 22 24 20
2016pawch 24.0 24 24 24
2016pncmp 24.0 24.0 24 24
2016qcmo 18.0 18.0 18 18
2016ripro 24.0 24.0 24 24
2016scmb 16.125 16.0 18 16
2016tnkn 20.081632653061224 20 22 20
2016txda 22.046511627906977 22 24 22
2016txho 16.0 16.0 16 16
2016txlu 22.2 22.0 24 22
2016txsa 16.0 16.0 16 16
2016utwv 24.0 24.0 24 24
2016vabla 23.941176470588236 24.0 24 23
2016vados 24.0 24.0 24 24
2016vahay 24.0 24 24 24
2016vapor 24.0 24 24 24
2016waahs 23.944444444444443 24.0 24 23
2016waamv 24.0 24 24 24
2016waell 24.0 24 24 24
2016wamou 24.0 24 24 24
2016wasno 24.0 24.0 24 24
2016waspo 24.0 24.0 24 24
2016wimi 20.150943396226417 20 22 20
#! /usr/bin/python3
import argparse
import json
import urllib.request
import statistics
import csv
from collections import defaultdict
"""
A script to calculate distributions of unique partners and opponents for 2016 FRC events
"""
BASE_URL = 'https://www.thebluealliance.com/api/v2/{}'
APP_HEADER = 'X-TBA-App-Id'
APP_ID = 'plnyyanks:schedstats:v0.1'
# See https://github.com/the-blue-alliance/the-blue-alliance/blob/master/consts/event_type.py
VALID_EVENT_TYPES = [0, 1, 2, 3]
def fetch_endpoint(endpoint):
full_url = BASE_URL.format(endpoint)
url = urllib.request.Request(full_url, headers={APP_HEADER: APP_ID, 'User-agent': 'Mozilla/5.0'})
response = urllib.request.urlopen(url)
return json.loads(response.read().decode('utf-8'))
def fetch_event_keys_in_year(year):
api_events = fetch_endpoint("events/{}".format(year))
return [event["key"] for event in api_events if event["event_type"] in VALID_EVENT_TYPES]
def fetch_event_matches(event_key):
matches = fetch_endpoint("event/{}/matches".format(event_key))
return [match for match in matches if match["comp_level"] == "qm"]
def fetch_event_team_keys(event_key):
teams = fetch_endpoint("event/{}/teams".format(event_key))
return [team["key"] for team in teams]
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--year", help="Competition season to test", type=int, default=2016)
args = parser.parse_args()
# Build list of all relevent event keys
print("Fetching events in {}".format(args.year))
event_keys = []
event_keys.extend(fetch_event_keys_in_year(args.year))
# Build dict of each event's matches
matches = {}
teams = {}
for event_key in event_keys:
print("Fetching qual matches for {}".format(event_key))
matches[event_key] = fetch_event_matches(event_key)
teams[event_key] = fetch_event_team_keys(event_key)
# Calculate stats for schedule
# {# with, # against} x {mean, median, max, min}
teams_with = defaultdict(lambda: defaultdict(set))
teams_against = defaultdict(lambda: defaultdict(set))
count_with = defaultdict(lambda: defaultdict(int))
count_against = defaultdict(lambda: defaultdict(int))
for event_key in event_keys:
if len(matches[event_key]) == 0:
continue
for match in matches[event_key]:
for team in match["alliances"]["blue"]["teams"]:
for other in match["alliances"]["blue"]["teams"]:
if team != other:
teams_with[event_key][team].add(other)
for other in match["alliances"]["red"]["teams"]:
teams_against[event_key][team].add(other)
for team in match["alliances"]["red"]["teams"]:
for other in match["alliances"]["red"]["teams"]:
if team != other:
teams_with[event_key][team].add(other)
for other in match["alliances"]["blue"]["teams"]:
teams_against[event_key][team].add(other)
for team in teams[event_key]:
count_with[event_key][team] = len(teams_with[event_key][team])
count_against[event_key][team] = len(teams_against[event_key][team])
with_res = [["Event Key", "Mean", "Median", "Max", "Min"]]
against_res = [["Event Key", "Mean", "Median", "Max", "Min"]]
for event_key in event_keys:
if len(matches[event_key]) == 0:
continue
withs = [count for team, count in count_with[event_key].items() if count != 0]
ags = [count for team, count in count_against[event_key].items() if count != 0]
with_res.append([event_key, statistics.mean(withs), statistics.median(withs), max(withs), min(withs)])
against_res.append([event_key, statistics.mean(ags), statistics.median(ags), max(ags), min(ags)])
with open('2016-with.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(with_res)
with open('2016-against.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(against_res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment