Skip to content

Commit

Permalink
Allow updated predictions to include how well the predictions went.
Browse files Browse the repository at this point in the history
  • Loading branch information
barbacbd committed Jul 19, 2024
1 parent 37a6048 commit a797790
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/nhl_model/ann.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,17 +576,20 @@ def _setPredictions(todaysData):

originalDF = _readPredictionsFile()
if originalDF is None:
logger.debug("failed to read predictions file")
logger.debug("failed to read predictions file, setting current data")
print("here")
pd.DataFrame.from_dict(outputForDF, orient='columns').to_excel(filename)
return

originalDFAsDicts = originalDF.to_dict(orient='records')
originalDFAsDicts = originalDF.to_dict(orient='columns') #records
print(originalDFAsDicts)
outputNotFound = []

for outRow in outputForDF:
updated = False
for idx in range(len(originalDFAsDicts)):
if outRow["home"] == originalDFAsDicts[idx]["home"] and \
outRow["away"] == originalDFAsDicts[idx]["away"] and \
if outRow["homeTeam"] == originalDFAsDicts[idx]["homeTeam"] and \
outRow["awayTeam"] == originalDFAsDicts[idx]["awayTeam"] and \
outRow["gameDate"] == originalDFAsDicts[idx]["gameDate"]:
# update the values in the original set with the predicted values
# that were currently executed
Expand Down Expand Up @@ -665,8 +668,8 @@ def _execAnnCommon(model, predictionFile, comparisonFunction, day, month, year):

predictedWinner = homeTeam if predictedOutcomes[index] == 1 else awayTeam
outputForDF.append({
"home": homeTeam,
"away": awayTeam,
"homeTeam": homeTeam,
"awayTeam": awayTeam,
"gameDate": datetime(year, month, day).strftime("%Y-%m-%d"),
"datePredicted": datetime.now().strftime("%Y-%m-%d"),
"predictedWinner": predictedWinner,
Expand Down Expand Up @@ -780,7 +783,10 @@ def determineWinners():

pulledData = []
spGameDate = gameDate.split("-")
gamesPlayed = findGamesByDate(year=spGameDate[0], month=spGameDate[1], day=spGameDate[2])
gamesPlayed = findGamesByDate(
year=int(
spGameDate[0]), month=int(spGameDate[1]), day=int(spGameDate[2])
)
if gamesPlayed is not None:
for game in gamesPlayed["games"]:
pulledData.append({
Expand All @@ -793,8 +799,7 @@ def determineWinners():
for game in games:
for actualGame in pulledData:
if actualGame["homeTeam"] in game["homeTeam"] and \
actualGame["awayTeam"] in game["awayTeam"] and \
None in (game["correct"], game["winner"]):
actualGame["awayTeam"] in game["awayTeam"]:
# determine the winner
if actualGame["homeTeam"] in game["predictedWinner"] and \
actualGame["homeScore"] > actualGame["awayScore"]:
Expand All @@ -811,7 +816,13 @@ def determineWinners():
break # found the game, break out

filename = path_join(*[BASE_SAVE_DIR, "predictions.xlsx"])
pd.DataFrame(gamesByDate).to_excel(filename)
if isinstance(gamesByDate, (defaultdict, dict)):
combined = []
for _, value in gamesByDate.items():
combined.extend(value)
pd.DataFrame.from_records(combined).to_excel(filename)
else:
pd.DataFrame(gamesByDate).to_excel(filename)


def analyze(*args, **kwargs):
Expand Down
4 changes: 4 additions & 0 deletions src/nhl_model/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ def _verify_exists(bs, list_of_vars):
"gameId": boxscore["id"],
"winner": bool(ret["htGoals"] > ret["atGoals"])
})

if "gameDate" in boxscore:
dateInfo = boxscore["gameDate"].split("-")
ret.update({"year": dateInfo[0], "month": dateInfo[1], "day": dateInfo[2]})

return ret

Expand Down

0 comments on commit a797790

Please sign in to comment.