Skip to content

Instantly share code, notes, and snippets.

@allisontharp
Last active April 11, 2018 22:31
Show Gist options
  • Save allisontharp/b2bb5cf9662db6d870aba0cf46915f67 to your computer and use it in GitHub Desktop.
Save allisontharp/b2bb5cf9662db6d870aba0cf46915f67 to your computer and use it in GitHub Desktop.
Generate a radial NHL bracket where your favorite team wins it all! Simulates a 50/50 chance of winning per game. The team with the best of 7 moves on! The console output will show the total number of games for each matchup. The idea came from JohnMLTX's awesome plot on reddit: https://www.reddit.com/r/hockey/comments/8b3ry6/heres_my_radial_brac…
import random
import numpy as np
import matplotlib.pyplot as plt
favoriteTeam = 'TBL'
eastern = ((('TBL', 'NJD'), ('BOS', 'TOR')), (('WSH', 'CBJ'), ('PIT', 'PHI')))
western = ((('NSH', 'COL'), ('WPG', 'MIN')), (('VGK', 'LAK'), ('ANA', 'SJS')))
championship = (eastern, western)
easternList = [lasttup for firsttup in eastern for nexttup in firsttup for lasttup in nexttup]
westernList = [lasttup for firsttup in western for nexttup in firsttup for lasttup in nexttup]
teamColors = {
'TBL':'#00205B',
'NJD':'#C8102E',
'BOS':'#FFB81C',
'TOR':'#00205B',
'WSH':'#C8102E',
'CBJ':'#041E42',
'PIT':'#FFB81C',
'PHI':'#FA4616',
'NSH':'#FFB81C',
'COL':'#6F263D',
'WPG':'#041E42',
'MIN':'#154734',
'VGK':'#B9975B',
'LAK':'#A2AAAD',
'ANA':'#FC4C02',
'SJS':'#006272'
}
def matchup(match):
matchGames = {}
for team in match:
matchGames[team] = 0
while matchGames[max(matchGames, key=matchGames.get)] < 4:
winner = random.choice(match)
matchGames[winner] += 1
winner = max(matchGames, key=matchGames.get)
return [winner, sum(matchGames.values())]
def championshipFinals(championship):
conferenceFinals = []
conferenceWinners = {}
stanleyCupFinal = []
roundOneOutput = []
roundTwoOutput = []
for conference in championship:
roundOneWinners = {}
roundTwo = []
roundTwoWinners = {}
for division in conference:
divisionWinners = []
## Round 1
for match in division:
[winner, numGames] = matchup(match)
roundOneWinners[winner] = numGames
divisionWinners.append(winner)
roundTwo.append(tuple(divisionWinners))
## Round 2
for match in roundTwo:
[winner, numGames] = matchup(match)
roundTwoWinners[winner] = numGames
conferenceFinals.append(tuple(roundTwoWinners))
roundOneOutput.append(roundOneWinners)
roundTwoOutput.append(roundTwoWinners)
#print("round one winners:", roundOneWinners)
#print("round two matchup:", roundTwo)
#print("round two winners:", roundTwoWinners)
#print("")
#print("Conference Finals:", conferenceFinals)
for match in conferenceFinals:
[winner, numGames] = matchup(match)
conferenceWinners[winner] = numGames
stanleyCupFinal.append(tuple(conferenceWinners))
#print("Conference Winners", conferenceWinners)
# Stanley Cup Final
[winner, numGames] = matchup(stanleyCupFinal[0])
#print("")
#print("Stanley Cup Winner: ", winner, ", Number of Games:", numGames)
return [roundOneOutput, roundTwoOutput, conferenceWinners, winner, numGames]
winner = ""
numSims = 0
while winner != favoriteTeam:
[roundOneOutput, roundTwoOutput, conferenceWinners, winner, numGames] = championshipFinals(championship)
numSims += 1
print("Number of Simulations: ", numSims)
print("Round One Winners:")
for i in roundOneOutput:
if any([c in i for c in easternList]):
print("Eastern: ", i)
roundOne_Eastern = list(i.keys())
else:
print("Western: ", i)
roundOne_Western = list(i.keys())
print("")
print("Round Two Winners:")
for i in roundTwoOutput:
if any([c in i for c in easternList]):
print("Eastern: ", i)
roundTwo_Eastern = list(i.keys())
else:
print("Western: ", i)
roundTwo_Western = list(i.keys())
print("")
print("Conference Winners:")
print(conferenceWinners)
print("")
print("Stanley Cup Winner: ", winner, ", Number of Games:", numGames)
def sunburst(nodes, total=np.pi * 2, offset=0, level=0, ax=None):
ax = ax or plt.subplot(111, projection='polar')
if level == 0 and len(nodes) == 1:
label, value, subnodes = nodes[0]
ax.bar([0], [0.5], [np.pi * 2], color=teamColors[label])
ax.text(0, 0, label, ha='center', va='center')
sunburst(subnodes, total=value, level=level + 1, ax=ax)
elif nodes:
d = np.pi * 2 / total
labels = []
widths = []
colors = []
local_offset = offset
for label, value, subnodes in nodes:
colors.append(teamColors[label])
labels.append(label)
widths.append(value * d)
sunburst(subnodes, total=total, offset=local_offset,
level=level + 1, ax=ax)
local_offset += value
values = np.cumsum([offset * d] + widths[:-1])
heights = [1] * len(nodes)
bottoms = np.zeros(len(nodes)) + level - 0.5
rects = ax.bar(values, heights, widths, bottoms, linewidth=1,
edgecolor='white', align='edge', color=colors)
for rect, label in zip(rects, labels):
x = rect.get_x() + rect.get_width() / 2
y = rect.get_y() + rect.get_height() / 2
rotation = (90 + (360 - np.degrees(x) % 180)) % 360
ax.text(x, y, label, rotation=rotation, ha='center', va='center')
if level == 0:
ax.set_theta_direction(-1)
ax.set_theta_zero_location('N')
ax.set_axis_off()
data = [
(winner, 100, [
(list(conferenceWinners.keys())[0], 50, [
(roundTwo_Eastern[0], 25, [
(roundOne_Eastern[0], 12.5, [
(easternList[0], 6.25, []),
(easternList[1], 6.25, [])
]),
(roundOne_Eastern[1], 12.5, [
(easternList[2], 6.25, []),
(easternList[3], 6.25, [])
])
]),
(roundTwo_Eastern[1], 25, [
(roundOne_Eastern[2], 12.5, [
(easternList[4], 6.25, []),
(easternList[5], 6.25, [])
]),
(roundOne_Eastern[3], 12.5, [
(easternList[6], 6.25, []),
(easternList[7], 6.25, [])
])
]),
]),
(list(conferenceWinners.keys())[1], 50, [
(roundTwo_Western[0], 25, [
(roundOne_Western[0], 12.5, [
(westernList[0], 6.25, []),
(westernList[1], 6.25, [])
]),
(roundOne_Western[1], 12.5, [
(westernList[2], 6.25, []),
(westernList[3], 6.25, [])
])
]),
(roundTwo_Western[1], 25, [
(roundOne_Western[2], 12.5, [
(westernList[4], 6.25, []),
(westernList[5], 6.25, [])
]),
(roundOne_Western[3], 12.5, [
(westernList[6], 6.25, []),
(westernList[7], 6.25, [])
])
]),
]),
]),
]
sunburst(data)
plt.title("Number of Runs: " + str(numSims))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment