-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselfConsistentRanker.py
253 lines (236 loc) · 10.4 KB
/
selfConsistentRanker.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#! /usr/bin/python3.10
import numpy as np
import csv
import sys
import pandas as pd
import pickle
from functions import general as gen
import os
nTeamDetails = 0
pickling = False
if len(sys.argv) > 1:
if sys.argv[1] == 'pickle':
pickling = True
else:
nTeamDetails = len(sys.argv) - 1
if nTeamDetails > 0:
teamDetails = sys.argv[1:nTeamDetails+1]
seasonFile = 'data/2024season.csv'
teamsFile = 'data/2024fbs.csv'
extendedPrint = True
maxIts = 10000
tol = 1e-14
maxWeek = 15
maxWeekRemaining = 16
rankstrings = [(f'({i+1}) ') for i in range(25)]
nameSwaps = [['Central Florida','UCF'],['Pittsburgh','Pitt'],['Alabama-Birmingham','UAB'],['Texas-San Antonio','UTSA'],
['Texas-El Paso','UTEP'],['Southern Methodist','SMU'],['Brigham Young','BYU'],['Mississippi','Ole Miss'],
['Louisiana State','LSU'],['Southern California','USC']]
season = []
with open(seasonFile, newline='') as csvfile:
gameData = csv.reader(csvfile, delimiter=',')
header = next(gameData, None) # skip the header
wIndex = header.index('Winner')
lIndex = header.index('Loser')
for game in gameData:
name1 = game[wIndex]
name2 = game[lIndex]
for rank in rankstrings:
name1 = name1.replace(rank,'')
name2 = name2.replace(rank,'')
for swap in nameSwaps:
if name1 == swap[0]:
name1 = swap[1]
if name2 == swap[0]:
name2 = swap[1]
season.append([int(game[1]),name1,name2])
teams = []
with open(teamsFile, newline='') as csvfile:
teamData = csv.reader(csvfile, delimiter=',')
next(teamData, None) # skip the header
next(teamData, None) # skip the header
for team in teamData:
teams.append(team[1])
maxNameLength = max([len(team) for team in teams])
nTeam = len(teams)
winLossMatrix = [[ [] for i in range(nTeam+1)] for j in range(nTeam+1)]
remainingSchedule = [[ [] for i in range(nTeam+1)] for j in range(nTeam+1)]
teams.append('Non-FBS')
gamesPlayed = np.zeros(nTeam+1)
gamesRemaining = np.zeros(nTeam+1)
ws = np.zeros(nTeam+1)
ls = np.zeros(nTeam+1)
for game in season:
try:
winner = teams.index(game[1])
except ValueError:
winner = nTeam
try:
loser = teams.index(game[2])
except ValueError:
loser = nTeam
if int(game[0]) <= maxWeek:
winLossMatrix[winner][loser].append(1)
winLossMatrix[loser][winner].append(-1)
gamesPlayed[winner] += 1
gamesPlayed[loser] += 1
ws[winner] += 1
ls[loser] += 1
elif int(game[0]) <= maxWeekRemaining:
remainingSchedule[winner][loser].append(1)
remainingSchedule[loser][winner].append(1)
gamesRemaining[winner] += 1
gamesRemaining[loser] += 1
naw = ws - ls
newNAW = np.copy(naw[0:-1])
for j in range(maxIts):
naw[nTeam] = min(newNAW) - 1
nawScale = max(np.abs(newNAW))
for i in range(nTeam):
newNAW[i] = 0
for k in range(nTeam+1):
for l in range(len(winLossMatrix[i][k])):
newNAW[i] = newNAW[i] + winLossMatrix[i][k][l]*np.exp(winLossMatrix[i][k][l]*naw[k]/nawScale)
maxDiff = np.amax(np.abs(newNAW-naw[0:nTeam]))
naw[:-1] = np.copy(newNAW)
iterations = j + 1
if maxDiff < tol:
break
ncs = np.zeros(nTeam+1)
for i in range(nTeam):
for k in range(nTeam+1):
for l in range(len(winLossMatrix[i][k])):
ncs[i] = ncs[i] + np.exp(naw[k]/nawScale)
nawScale = max(np.abs(newNAW))
aaw = naw / np.amax([gamesPlayed,np.ones(nTeam+1)],axis=0)
nrs = np.zeros(nTeam+1)
for i in range(nTeam):
for k in range(nTeam+1):
for l in range(len(remainingSchedule[i][k])):
nrs[i] = nrs[i] + remainingSchedule[i][k][l]*np.exp(naw[k]/nawScale)
aaw[-1] = -np.inf
ncs[-1] = -np.inf
nrs[-1] = -np.inf
ranks = list(reversed(np.argsort(naw)))
# use sort instead of argsort here to allow ties
naworder = list(reversed(np.sort(naw)))
aaworder = list(reversed(np.sort(aaw)))
ncsorder = list(reversed(np.sort(ncs)))
nrsorder = list(reversed(np.sort(nrs)))
# Formatting
fnd = 3 # float number of decimals (choose odd)
ffw = fnd + 4 # float full width, :{ffw}.{fnd}f
ifw = 4 # integer full width, :{ifw}d
tlp = ' '*int((fnd+3)/2) # padding around 3-letter strings
tbs = '-'*int(ffw+2) # table border segment
print()
if nTeamDetails == 0:
print(f'Ranks to week {maxWeek-1} after {iterations} iterations:')
print()
print(f'| Rank | {"Team":{maxNameLength}} |{tlp}NAW{tlp}|{f"{tlp}AAW{tlp}|{tlp}NCS{tlp}|{tlp}NRS{tlp}| Record |" if extendedPrint else ""}')
print(f'|------|{"-"*(maxNameLength+2)}|{tbs}|{f"{tbs}|{tbs}|{tbs}|---------|" if extendedPrint else ""}')
for i in range(nTeam):
print(f'| {naworder.index(naw[ranks[i]])+1:{ifw}} | {teams[ranks[i]]:{maxNameLength}} | {naw[ranks[i]]:{ffw}.{fnd}f} | {f"{aaw[ranks[i]]:{ffw}.{fnd}f} | {ncs[ranks[i]]:{ffw}.{fnd}f} | {nrs[ranks[i]]:{ffw}.{fnd}f} | {int(ws[ranks[i]]):2d} - {int(ls[ranks[i]]):2d} |" if extendedPrint else ""}')
else:
for team in teamDetails:
if team in teams:
i = teams.index(team)
if gamesPlayed[i] > 0:
print(f'{team} ({int(ws[i])} - {int(ls[i])})')
print(f'| |{tlp}NAW{tlp}|{tlp}AAW{tlp}|{tlp}NCS{tlp}|{tlp}NRS{tlp}|')
print(f'|-------|{tbs}|{tbs}|{tbs}|{tbs}|')
print(f'| Value | {naw[i]:{ffw}.{fnd}f} | {aaw[i]:{ffw}.{fnd}f} | {ncs[i]:{ffw}.{fnd}f} | {nrs[i]:{ffw}.{fnd}f} |')
print(f'| Rank | {naworder.index(naw[i])+1:{ffw}d} | {aaworder.index(aaw[i])+1:{ffw}d} | {ncsorder.index(ncs[i])+1:{ffw}d} | {nrsorder.index(nrs[i])+1:{ffw}d} |')
print()
print(f'|{" Played":{maxNameLength+5}}| Outcome |{tlp[1:]}Change{tlp[1:]}|')
print(f'|{"-"*(maxNameLength+5)}|------------|-{tbs}|')
for j in range(nTeam+1):
k = ranks[j]
for l in range(len(winLossMatrix[i][k])):
print(f'|{naworder.index(naw[k])+1:{ifw}} {teams[k]:{maxNameLength}}|{" Win " if winLossMatrix[i][k][l]==1 else " Loss "}| {"+" if winLossMatrix[i][k][l]==1 else "-"}{np.exp(winLossMatrix[i][k][l]*naw[k]/nawScale):{ffw}.{fnd}f} |')
print()
if gamesRemaining[i] > 0:
print(f'|{" Remaining":{maxNameLength+5}}|{tlp[1:]}If Win{tlp[1:]}|{tlp[1:]}If Loss{tlp[2:]}|')
print(f'|{"-"*(maxNameLength+5)}|-{tbs}|-{tbs}|')
for j in range(nTeam+1):
k = ranks[j]
for l in range(len(remainingSchedule[i][k])):
print(f'|{naworder.index(naw[k])+1:{ifw}} {teams[k]:{maxNameLength}}| +{np.exp(naw[k]/nawScale):{ffw}.{fnd}f} | -{np.exp(-naw[k]/nawScale):{ffw}.{fnd}f} |')
print()
if pickling:
pickleFile = 'ncaafb.p'
try:
d = pd.read_pickle(pickleFile)
except FileNotFoundError:
gen.dbInit(pickleFile)
d = pd.read_pickle(pickleFile)
ldf = []
for i in range(nTeam):
rank = f'{naworder.index(naw[ranks[i]])+1:{ifw}}'
team = teams[ranks[i]]
tNaw = f'{naw[ranks[i]]:{ffw}.{fnd}f}'
tAaw = f'{aaw[ranks[i]]:{ffw}.{fnd}f}'
tNcs = f'{ncs[ranks[i]]:{ffw}.{fnd}f}'
tNrs = f'{nrs[ranks[i]]:{ffw}.{fnd}f}'
record = f'{int(ws[ranks[i]])} - {int(ls[ranks[i]])}'
ldf.append([rank,team,tNaw,tAaw,tNcs,tNrs,record])
df = pd.DataFrame(ldf,columns=['Rank','Team','NAW','AAW','NCS','NRS','Record'])
df.sort_values(['Rank'],inplace=True)
d['analysis']['teamRankings'] = df
pickle.dump(d, open(os.path.join(pickleFile), 'wb'))
d['analysis']['byTeam'] = dict()
for team in teams:
i = teams.index(team)
v = dict()
v['team'] = f'{team} ({int(ws[i])} - {int(ls[i])})'
d['analysis']['byTeam'][team] = v
if gamesPlayed[i] > 0:
# make stats summary table
tNaw = f'{naw[i]:{ffw}.{fnd}f}'
tAaw = f'{aaw[i]:{ffw}.{fnd}f}'
tNcs = f'{ncs[i]:{ffw}.{fnd}f}'
tNrs = f'{nrs[i]:{ffw}.{fnd}f}'
rNaw = f'{naworder.index(naw[i])+1:{ffw}d}'
rAaw = f'{aaworder.index(aaw[i])+1:{ffw}d}'
rNcs = f'{ncsorder.index(ncs[i])+1:{ffw}d}'
rNrs = f'{nrsorder.index(nrs[i])+1:{ffw}d}'
ldf = [[tNaw,tAaw,tNcs,tNrs],[rNaw,rAaw,rNcs,rNrs]]
df = pd.DataFrame(ldf,columns=['NAW','AAW','NCS','NRS'])
d['analysis']['byTeam'][team]['stats'] = df
pickle.dump(d, open(os.path.join(pickleFile), 'wb'))
# make games played/results table
ldf = []
for j in range(nTeam+1):
k = ranks[j]
for l in range(len(winLossMatrix[i][k])):
opponent = f'{naworder.index(naw[k])+1:4} {teams[k]:{maxNameLength}}'
outcome = f'{"Win" if winLossMatrix[i][k][l]==1 else "Loss"}'
change = f'{"+" if winLossMatrix[i][k][l]==1 else "-"}{np.exp(winLossMatrix[i][k][l]*naw[k]/nawScale):{ffw}.{fnd}f}'
ldf.append([opponent,outcome,change])
df = pd.DataFrame(ldf,columns=['Played','Outcome','Change'])
d['analysis']['byTeam'][team]['results'] = df
pickle.dump(d, open(os.path.join(pickleFile), 'wb'))
else:
# dump empty if no games played
d['analysis']['byTeam'][team]['stats'] = []
d['analysis']['byTeam'][team]['results'] = []
if gamesRemaining[i] > 0:
# make games remaining table
ldf = []
for j in range(nTeam+1):
k = ranks[j]
for l in range(len(remainingSchedule[i][k])):
opponent = f'{naworder.index(naw[k])+1:{ifw}} {teams[k]:{maxNameLength}}'
cifw = f'+{np.exp(naw[k]/nawScale):{ffw}.{fnd}f}'
cifl = f'-{np.exp(-naw[k]/nawScale):{ffw}.{fnd}f}'
ldf.append([opponent,cifw,cifl])
df = pd.DataFrame(ldf,columns=['Remaining','If Win','If Loss'])
d['analysis']['byTeam'][team]['remaining'] = df
pickle.dump(d, open(os.path.join(pickleFile), 'wb'))
else:
# dump empty if no games remaining
d['analysis']['byTeam'][team]['remaining'] = []
d['analysis']['week'] = str(maxWeek-1)
teams.sort()
d['data']['teams']= teams
pickle.dump(d, open(os.path.join(pickleFile), 'wb'))