-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpyMadness.py
executable file
·157 lines (145 loc) · 5.72 KB
/
pyMadness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python3
import json
from urllib.request import urlopen
from bs4 import BeautifulSoup
import html5lib
import pdb
from scipy.stats import norm
from collections import OrderedDict
def FindMergeTeams(teama, teamb, dict_merge):
FoundA = ""
FoundB = ""
for index in range(len(dict_merge["Index"])):
idx = str(index)
if (teama == dict_merge["bracket team"][idx]):
FoundA = dict_merge["stats team"][idx]
if (dict_merge["fixed stats team"][idx]):
FoundA = dict_merge["fixed stats team"][idx]
if (teamb == dict_merge["bracket team"][idx]):
FoundB = dict_merge["stats team"][idx]
if (dict_merge["fixed stats team"][idx]):
FoundB = dict_merge["fixed stats team"][idx]
if (FoundA and FoundB):
break
if (FoundA == "" or FoundB == ""):
if (teama != "?" or teamb != "?"):
print ("warning, in FindTeams() both teams not found, correct your merge spreadsheet, please")
print ("*** TeamA: [{0}:{1}], TeamB: [{2}:{3}] ***".format(teama, FoundA, teamb, FoundB))
return FoundA, FoundB
def findTeams(first, second, file = "json/stats.json"):
teama = {}
teamb = {}
count = 0
with open(file) as stats_file:
dict_stats = json.load(stats_file, object_pairs_hook=OrderedDict)
for item in dict_stats.values():
if (item["Team"].lower().strip() == first.lower().strip()):
teama = item
count += 1
if (item["Team"].lower().strip() == second.lower().strip()):
teamb = item
count += 1
if (count == 2):
break
if (count < 2):
if (not teama):
print ("Could not find stats for {0}".format(first))
if (not teamb):
print ("Could not find stats for {0}".format(second))
return {}, {}
return teama, teamb
def Chance(teama, teamb, neutral, verbose):
EffMgn = Line(teama, teamb, neutral, verbose)
if verbose:
print ("Chance(efficiency margin) {0}".format(EffMgn))
bPercent = norm.cdf(0, EffMgn, 11)
aPercent = 1.0 - bPercent
aPercent = int(round(aPercent * 100.0))
bPercent = int(round(bPercent * 100.0))
if verbose:
print ("Chance({0}) {1}%".format(teama["Team"], aPercent), "vs. Chance({0}) {1}%".format(teamb["Team"], bPercent))
return aPercent, bPercent
def Tempo(teama, teamb, verbose):
Tdiff = (float(teama['AdjT']) + float(teamb['AdjT'])) / 200.0
if verbose:
print ("Tempo(tempo) {0}".format(Tdiff * 100))
return Tdiff
def Test(verbose):
result = 0
# Purdue, Northwestern on 3/5/17
# Actual Score: Edwards leads No. 16 Purdue past Northwestern, 69-65
# venue was: Welsh-Ryan Arena in Evanston, IL (Northwestern was the home team)
teama = {'Team':"purdue", 'AdjEM':24.31, 'AdjT':69.5, 'Result1':57, 'Result2':69}
teamb = {'Team':"northwestern", 'AdjEM':15.83, 'AdjT':65.8, 'Result1':43,'Result2':67}
if verbose:
print (teama)
print (teamb)
print ("Test #1 Purdue at Northwestern on 3/5/17")
print (" Northwestern is Home team, testing Chance() routine)")
chancea, chanceb = Chance(teama, teamb, False, verbose)
if (teama['Result1'] == chancea):
result += 1
if (teamb['Result1'] == chanceb):
result += 1
if (result == 2):
print ("Test #1 - pass")
print (" ")
print ("Test #2 Purdue at Northwestern on 3/5/17")
print (" Northwestern is Home team, testing Score() routine)")
print (" Actual Score: Edwards lead No. 16 Purdue past Northwestern, 69-65")
scorea, scoreb = Score(teama, teamb, False, verbose)
if (teama['Result2'] == scorea):
result += 1
if (teamb['Result2'] == scoreb):
result += 1
if (result == 4):
print ("Test #2 - pass")
return True
return False
def Score(teama, teamb, neutral, verbose):
tempo = Tempo(teama, teamb, verbose)
EffMgn = Line(teama, teamb, neutral, verbose)
if verbose:
print ("Score(efficiency margin) {0}".format(EffMgn))
bScore = int(round((tempo * 100) - (EffMgn / 2.0)))
aScore = int(round((tempo * 100) + (EffMgn / 2.0)))
if verbose:
print ("Score({0}) {1}".format(teama["Team"], aScore), "vs. Score({0}) {1}".format(teamb["Team"], bScore))
return aScore, bScore
def Line(teama, teamb, neutral, verbose):
tempo = Tempo(teama, teamb, verbose)
if verbose:
print ("Line(tempo) {0}".format(tempo * 100))
EMdiff = (float(teama['AdjEM']) - float(teamb['AdjEM'])) * tempo
EffMgn = 0
if neutral:
EffMgn = EMdiff
else:
EffMgn = EMdiff - 3.75
if verbose:
print ("Line(efficiency margin) {0}".format(EffMgn))
return EffMgn
def Calculate(first, second, neutral, verbose):
if verbose:
if neutral:
info = "{0} verses {1} at a neutral location".format(first, second)
print (info)
else:
info = "Visiting team: {0} verses Home team: {1}".format(first, second)
print (info)
teama, teamb = findTeams(first, second)
if (not teama or not teamb):
return {}
chancea, chanceb = Chance(teama, teamb, neutral, verbose)
scorea, scoreb = Score(teama, teamb, neutral, verbose)
line = Line(teama, teamb, neutral, verbose)
tempo = Tempo(teama, teamb, verbose)
dict_score = \
{
'teama':first, 'scorea':"{0}".format(scorea), 'chancea':"{0}%".format(chancea) \
,'teamb':second, 'scoreb':"{0}".format(scoreb), 'chanceb':"{0}%".format(chanceb), \
'line':int(round(line)), 'tempo':"{0}".format(int(round(tempo * 100))) \
}
if verbose:
print ("Calculate(dict_score) {0}".format(dict_score))
return dict_score