Skip to content

Commit

Permalink
Add team nickames
Browse files Browse the repository at this point in the history
  • Loading branch information
will-hou authored Feb 27, 2020
2 parents dbaeaec + 307f6f9 commit b3ee24c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
24 changes: 17 additions & 7 deletions convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
"The JSON file you uploaded does not have a timestamp value. Try running the program without the -t flag. Use --help for more info")
exit()

if os.path.exists("nicknames.json"):
with open("nicknames.json", "r") as fp:
nicknames = json.load(fp)
else:
generate_team_json()

# Filter the data, if needed
filter(json_data, args.filter) if args.filter else None

Expand All @@ -64,39 +70,43 @@
num_scouts += len(i)

# Preallocate numpy array with number of scouts and the number of metrics.
# Add two columns for team numbers and scout names. Add an extra column for timestamps, if needed.
num_metrics = num_metrics + 2 + (1 if args.timestamp else 0)
# Add three columns for team numbers, nicknames, and scout names. Add an extra column for timestamps, if needed.
num_metrics = num_metrics + 3 + (1 if args.timestamp else 0)
data = np.zeros((num_scouts, num_metrics), dtype='O')

# List of all metrics scouted
headers = [i['name'] for i in json_data['teams'][list(json_data['teams'].keys())[0]][0]['metrics'].values()]
# Add team number and name of scout to the CSV . Add timestamp header, if needed
headers.insert(0, "Timestamp") if args.timestamp else None
headers.insert(0, "Name of Scout")
headers.insert(0, "Team Nickname")
headers.insert(0, "Team Number")

# Put scout data into a numpy array
row = 0
# Find which column we should add the metrics to
column = 3 if args.timestamp else 2
column = 4 if args.timestamp else 3
for team in json_data['teams'].values():
for scout in team:
# Add the name of the scout to the data matrix
data[row, 1] = scout['name']
data[row, 2] = scout['name']
# Add the timestamp of the scout to the data matrix, if needed
data[row, 2] = scout['timestamp'] if args.timestamp else None
data[row, 3] = scout['timestamp'] if args.timestamp else None
# Adds all the metric values to a list. If the metric value is a string, remove duplicated spaces and tabs
metric_values = [" ".join(i['value'].split()) if type(i['value']) is str else i['value'] for i in
scout['metrics'].values()]
# Check to make sure that the metric values exist before adding to the data matrix
data[row, column:] = metric_values if (len(metric_values) + column) == len(headers) else None
row += 1

# Put team numbers into data matrix
# Put team numbers and their nicknames into data matrix
team_nums = []
team_nicks = []
for team in json_data["teams"]:
for i in range(0, len(json_data["teams"][team])):
for i in range(len(json_data["teams"][team])):
team_nums.append(team)
team_nicks.append(nicknames[team])
data[:, 1] = team_nicks
data[:, 0] = team_nums

# Convert array into dataframe
Expand Down
1 change: 1 addition & 0 deletions nicknames.json

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import requests
import json


# Removes a scout if user specified metric is empty
def filter(json_data, metric_to_filter):
repeats = []
Expand Down Expand Up @@ -32,3 +36,16 @@ def strip_empty_metrics(json_data):
for i in to_delete:
del json_data['teams'][i[0]][i[1]]
return len(to_delete)


# Generates a json file with keys corresponding to the team number and values corresponding to the team's nickname
def generate_team_json():
nickname_map = {}
for i in range(0, 17):
teams = requests.get('https://www.thebluealliance.com/api/v3/teams/{}/simple'.format(i), params={
'X-TBA-Auth-Key': 'KuyisSfG5mADtkhd2h0ebKbiCtE40vqwN5fX6voJq8i4IYr9STai3PpqLHT1z3kR'}).json()
for team in teams:
nickname_map[team['team_number']] = team['nickname']

with open('nicknames.json', 'w') as fp:
json.dump(nickname_map, fp)

0 comments on commit b3ee24c

Please sign in to comment.