Skip to content

Commit

Permalink
DatePicker improvements and handling days with no games
Browse files Browse the repository at this point in the history
  • Loading branch information
peteb206 committed Sep 15, 2023
1 parent 3f27ff9 commit 4d1bb65
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
33 changes: 29 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,34 @@
from pathlib import Path
from data import get_enhanced_at_bats
from model import BTSBatterClassifier
from datetime import datetime, date
import pandas as pd
from datetime import datetime, date, timedelta

# Filter data to begin of 2 seasons ago
now = datetime.now()
print('Fetching and processing data from DB...', end = '')
enhanced_at_bats = get_enhanced_at_bats(from_date = datetime(now.year - 1, 1, 1))
print(' complete after', round((datetime.now() - now).seconds, 1), 'seconds')

# Date picker
game_dts: list[date] = [game_dt.date() for game_dt in enhanced_at_bats.index.get_level_values('game_date').unique() if game_dt.year == now.year]
min_game_dt, max_game_dt, disabled_dts = min(game_dts), max(game_dts), list()
# game_dt = min_game_dt
# while game_dt < max_game_dt:
# if game_dt not in game_dts:
# disabled_dts.append(game_dt.strftime('%Y-%m-%d'))
# game_dt += timedelta(days = 1)

# Classifier
classifier = BTSBatterClassifier(None, enhanced_at_bats, 'log_reg')

game_date = now.date()
todays_predictions_df = classifier.todays_predictions(game_date)#.query('`H%` >= 0.7')
if len(todays_predictions_df.index) > 0:
max_game_dt = game_date
else:
game_date = max_game_dt
todays_predictions_df = classifier.todays_predictions(game_date)

def timestamp_to_str(timestamp):
hour = str(timestamp.hour - (13 if timestamp.hour > 13 else 1))
Expand All @@ -34,8 +50,11 @@ def timestamp_to_str(timestamp):
'', # 'Home',
ui.row(
ui.column(4, ui.strong('Recommendations for')).add_style('width: 202px; padding-top: 5px;'),
ui.column(4, ui.input_date('date', '', value = now.date(), min = date(now.year, 3, 1), max = now.date(), format = 'M d, yyyy')) \
.add_style('width: 150px;'),
ui.column(
4,
ui.input_date('date', '', value = max_game_dt, min = min_game_dt, max = max_game_dt, datesdisabled = disabled_dts,
format = 'M d, yyyy')
).add_style('width: 150px;'),
ui.column(4, ui.output_ui('make_picks_button')).add_style('width: 114px;')
),
ui.output_ui('recommendations').add_style('padding-bottom: 10px;'),
Expand Down Expand Up @@ -63,7 +82,11 @@ def updated_predictions():
@output
@render.ui
def recommendations():
todays_recommendations_df = updated_predictions().head(2).query('`H%` >= 0.75').reset_index()
todays_recommendations_df = updated_predictions()
if len(todays_recommendations_df.index) == 0:
return ui.div()
else:
todays_recommendations_df = todays_predictions_df.head(2).query('`H%` >= 0.75').reset_index()
cols = [
ui.div(
ui.row(
Expand Down Expand Up @@ -103,6 +126,8 @@ def recommendations():
@render.data_frame
def picks_dataframe():
df = updated_predictions().reset_index()
if len(df.index) == 0:
return render.DataGrid(pd.DataFrame())
df['game'] = df.apply(lambda row: f'{row["team"]} {"vs" if row["home"] else "@"} {row["opponent"]}', axis = 1)
df.lineup = df.lineup.apply(lambda x: 'OUT' if x == 10 else 'TBD' if x == 0 else str(int(x)))
df['H%'] = df['H%'].apply(lambda x: f'{round(x * 100, 1)}%')
Expand Down
3 changes: 3 additions & 0 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ def todays_predictions(self, game_date = date.today()):
todays_options_df = todays_batters_df.loc[:, ['name', 'lineup', 'opp_sp_name']] \
.merge(self.model_input_df, left_index = True, right_index = True)

if len(todays_options_df.index) == 0:
return pd.DataFrame()

scaler, pca, clf = pickle.load(open(f'{self.PKL_DIR}/{self.pkl_name}.pkl', 'rb'))
if scaler != None:
todays_options_df = pd.DataFrame(scaler.transform(todays_options_df.loc[:, scaler.feature_names_in_]), index = todays_options_df.index,
Expand Down

0 comments on commit 4d1bb65

Please sign in to comment.