Skip to content

Commit

Permalink
Added ability to update the predictions file.
Browse files Browse the repository at this point in the history
  • Loading branch information
barbacbd committed Feb 22, 2024
1 parent f9b3a6f commit 87d8801
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions src/nhl_model/ann.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,44 @@ def _loadConfig(override=False):
return inputs


def _setPredictions(todaysData):
"""Set or update the predictions with the values for the current run.
"""
filename = path_join(*[BASE_SAVE_DIR, "predictions.xlsx"])
outputForDF = todaysData

if exists(filename):
originalDF = pd.read_excel(filename)
originalDF.drop(originalDF.columns[originalDF.columns.str.contains(
'unnamed', case=False)], axis=1, inplace=True)

originalDFAsDicts = originalDF.to_dict(orient='records')
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 \
outRow["gameDate"] == originalDFAsDicts[idx]["gameDate"]:
# update the values in the original set with the predicted values
# that were currently executed
originalDFAsDicts[idx] = outRow
updated = True
# break out of the inner loop
break

if not updated:
outputNotFound.append(outRow)

# reset the output list of dictionaries so that all of the updates
# are included
outputForDF = originalDFAsDicts + outputNotFound

# set the data in excel file with the actual values
pd.DataFrame.from_dict(outputForDF, orient='columns').to_excel(filename)


def _execAnnCommon(model, predictionFile, comparisonFunction, day, month, year):
"""Execute the model using the values used for prediction.
Expand Down Expand Up @@ -596,8 +634,8 @@ def _execAnnCommon(model, predictionFile, comparisonFunction, day, month, year):
# extract metadata for comparison
teams = _getTeamNames()
todaysGameData = findGamesByDate(day, month, year)

outputForDF = []

for index, game in enumerate(todaysGameData["games"]):
homeTeam = [x["fullName"] for x in teams if x["id"] == game['homeTeam']['id']][0]
awayTeam = [x["fullName"] for x in teams if x["id"] == game['awayTeam']['id']][0]
Expand All @@ -606,18 +644,18 @@ def _execAnnCommon(model, predictionFile, comparisonFunction, day, month, year):
outputForDF.append({
"home": homeTeam,
"away": awayTeam,
"predictedWinner": predictedWinner
"gameDate": datetime(year, month, day).strftime("%Y-%m-%d"),
"datePredicted": datetime.now().strftime("%Y-%m-%d"),
"predictedWinner": predictedWinner,
"correct": False
})

print(
f"home = {homeTeam:<30} away = {awayTeam:<30} predicted winner = {predictedWinner:<30}"
)

if outputForDF:
todaysDate = datetime.now()
filename = f'{todaysDate.strftime("%Y-%m-%d")}-predictions.xlsx'
filename = path_join(*[BASE_SAVE_DIR, filename])
pd.DataFrame.from_dict(outputForDF, orient='columns').to_excel(filename)
_setPredictions(outputForDF)


def _createArtifactDir():
Expand Down

0 comments on commit 87d8801

Please sign in to comment.