Skip to content

Commit

Permalink
Factor out function for table pretty-printing.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlew committed May 12, 2024
1 parent 76d24de commit c460592
Show file tree
Hide file tree
Showing 3 changed files with 316 additions and 21 deletions.
45 changes: 28 additions & 17 deletions accurating/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ class Model:
rating: dict[str, dict[int, float]]
"""Player rating, indexed by name and season"""

def tabulate(self):
last_rating = []
min_season, max_season = None, None
for name, ratings in self.rating.items():
assert min_season in [None, min(ratings.keys())]
assert max_season in [None, max(ratings.keys())]
min_season = min(ratings.keys())
max_season = max(ratings.keys())
last_rating.append((ratings[max_season], name))
if min_season == None:
return ""
min_season += 1 # Skip season no 0
last_rating.sort(reverse=True)
headers = ['Nick']
for season in range(max_season, min_season-1, -1):
headers.append(f'S{season}')
table = []
for _, name in last_rating:
# if len(table) > 10: break # max rows
row = [name]
for season in range(max_season, min_season-1, -1):
row.append(self.rating[name][season])
table.append(row)
return tabulate(table, headers=headers, floatfmt=".1f", numalign="decimal")


def fit(
data: MatchResultArrays,
Expand Down Expand Up @@ -239,31 +264,17 @@ def model(params):

def postprocess():
rating = {}
last_rating = []
for id, name in enumerate(data.player_name):
rating[name] = {}
for season in range(season_count):
rating[name][season] = float(get_ratings(params)[id, season]) * config.rating_difference_for_2_to_1_odds
last_rating.append((rating[name][season_count - 1], name))
model = Model(rating=rating)
if config.do_log:
headers = ['Nick']
for season in range(season_count-1, -1, -1):
headers.append(f'S{season}')
last_rating.sort(reverse=True)
table = []
for _, name in last_rating:
# if len(table) > 10: break # max rows
row = [name]
for season in range(season_count-1, 0, -1):
row.append(rating[name][season])
table.append(row)
print(tabulate(table, headers=headers, floatfmt=".1f", numalign="decimal"))

return Model(rating=rating)
print(model.tabulate())
return model

return postprocess()


def data_from_dicts(matches) -> MatchResultArrays:
player_set = set()

Expand Down
13 changes: 9 additions & 4 deletions iglo/iglo.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def save_iglo_data(path, test=False):
json.dump(get_data2(test), f)


def train_ielo(data_path, cfg_path, output_path):
def train_ielo(data_path, cfg_path, output_path, igor_path):
with open(data_path, 'r') as f:
data_dicts = json.load(f)
data = accurating.data_from_dicts(data_dicts)
Expand All @@ -93,6 +93,10 @@ def train_ielo(data_path, cfg_path, output_path):
with open(output_path, 'w') as f:
json.dump(dataclasses.asdict(model), f)

with open(igor_path, 'w') as f:
f.write(model.tabulate())
f.write("\n")

return model


Expand All @@ -102,7 +106,7 @@ def main(argv):
print(f' ./iglo.py download matches.json')
print(f' ./iglo.py download_test matches.json # used for accurating testing')
print(f' ./iglo.py download_local_iglo matches.json # used for IGLO development')
print(f' ./iglo.py ielo matches.json cfg.json ratings.json')
print(f' ./iglo.py ielo matches.json cfg.json ratings.json igor.txt')
return
cmd = argv[1]

Expand All @@ -117,11 +121,12 @@ def main(argv):
save_iglo_data_local(data_path)

elif cmd == 'ielo':
assert len(argv) == 5
assert len(argv) == 6
data_path = argv[2]
cfg_path = argv[3]
output_path = argv[4]
train_ielo(data_path, cfg_path, output_path)
igor_path = argv[5]
train_ielo(data_path, cfg_path, output_path, igor_path)

else:
print(f'No such command: {cmd}')
Expand Down
Loading

0 comments on commit c460592

Please sign in to comment.