nesticot commited on
Commit
f415f91
·
verified ·
1 Parent(s): 06cd542

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py CHANGED
@@ -60,6 +60,7 @@ app_ui = ui.page_fluid(
60
  ),
61
  ui.navset_tab(
62
  ui.nav("All Pitches",
 
63
  output_tabulator("table_all")
64
  ),
65
  ui.nav("Daily Pitches",
@@ -77,6 +78,109 @@ app_ui = ui.page_fluid(
77
  )
78
 
79
  def server(input, output, session):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  @output
81
  @render_tabulator
82
  @reactive.event(input.refresh)
 
60
  ),
61
  ui.navset_tab(
62
  ui.nav("All Pitches",
63
+ ui.download_button("download_all", "Download Data", class_="btn-sm mb-3"),
64
  output_tabulator("table_all")
65
  ),
66
  ui.nav("Daily Pitches",
 
78
  )
79
 
80
  def server(input, output, session):
81
+
82
+
83
+ @reactive.Calc
84
+ def ts_data():
85
+
86
+ import polars as pl
87
+ df_spring = pl.read_parquet(f"hf://datasets/TJStatsApps/mlb_data/data/mlb_pitch_data_2025_spring.parquet")
88
+
89
+
90
+ date = (datetime.datetime.now() - datetime.timedelta(hours=8)).date()
91
+ print(datetime.datetime.now())
92
+ date_str = date.strftime('%Y-%m-%d')
93
+ # Initialize the scraper
94
+
95
+
96
+ game_list_input = (scraper.get_schedule(year_input=[int(date_str[0:4])], sport_id=[1], game_type=['S'])
97
+ .filter(pl.col('date') == date)['game_id'])
98
+
99
+ data = scraper.get_data(game_list_input)
100
+ df = scraper.get_data_df(data)
101
+
102
+ df_spring = pl.concat([df_spring, df]).sort('game_date', descending=True)
103
+
104
+
105
+
106
+ # df_year_old = stuff_apply.stuff_apply(fe.feature_engineering(pl.concat([df_mlb,df_aaa,df_a,df_afl])))
107
+ # df_year_2old = stuff_apply.stuff_apply(fe.feature_engineering(pl.concat([df_mlb_2023])))
108
+ df_spring_stuff = stuff_apply.stuff_apply(fe.feature_engineering(pl.concat([df_spring])))
109
+
110
+
111
+
112
+ import polars as pl
113
+
114
+ # Compute total pitches for each pitcher
115
+ df_pitcher_totals = df_spring_stuff.group_by("pitcher_id").agg(
116
+ pl.col("start_speed").count().alias("pitcher_total")
117
+ )
118
+
119
+ df_spring_group = df_spring_stuff.group_by(['pitcher_id', 'pitcher_name', 'pitch_type']).agg([
120
+ pl.col('start_speed').count().alias('count'),
121
+ pl.col('start_speed').mean().alias('start_speed'),
122
+ pl.col('start_speed').max().alias('max_start_speed'),
123
+ pl.col('ivb').mean().alias('ivb'),
124
+ pl.col('hb').mean().alias('hb'),
125
+ pl.col('release_pos_z').mean().alias('release_pos_z'),
126
+ pl.col('release_pos_x').mean().alias('release_pos_x'),
127
+ pl.col('extension').mean().alias('extension'),
128
+ pl.col('tj_stuff_plus').mean().alias('tj_stuff_plus'),
129
+ (pl.col('start_speed').filter(pl.col('batter_hand')=='L').count()).alias('rhh_count'),
130
+ (pl.col('start_speed').filter(pl.col('batter_hand')=='R').count()).alias('lhh_count')
131
+ ])
132
+
133
+ # Join total pitches per pitcher to the grouped DataFrame on pitcher_id
134
+ df_spring_group = df_spring_group.join(df_pitcher_totals, on="pitcher_id", how="left")
135
+
136
+ # Now calculate the pitch percent for each pitcher/pitch_type combination
137
+ df_spring_group = df_spring_group.with_columns(
138
+ (pl.col("count") / pl.col("pitcher_total")).alias("pitch_percent")
139
+ )
140
+
141
+ # Optionally, if you want the percentage of left/right-handed batters within the group:
142
+ df_spring_group = df_spring_group.with_columns([
143
+ (pl.col("rhh_count") / pl.col("pitcher_total")).alias("rhh_percent"),
144
+ (pl.col("lhh_count") / pl.col("pitcher_total")).alias("lhh_percent")
145
+ ])
146
+
147
+ df_merge = df_spring_group.join(df_year_old_group,on=['pitcher_id','pitch_type'],how='left',suffix='_old')
148
+
149
+
150
+ df_merge = df_merge.with_columns(
151
+ pl.col('pitcher_id').is_in(df_year_old_group['pitcher_id']).alias('exists_in_old')
152
+ )
153
+
154
+ df_merge = df_merge.with_columns(
155
+ pl.when(pl.col('start_speed_old').is_null() & pl.col('exists_in_old'))
156
+ .then(pl.lit(True))
157
+ .otherwise(pl.lit(None))
158
+ .alias("new_pitch")
159
+ )
160
+
161
+ df_merge = df_merge.select([
162
+ 'pitcher_id',
163
+ 'pitcher_name',
164
+ 'pitch_type',
165
+ 'count',
166
+ 'pitch_percent',
167
+ 'rhh_percent',
168
+ 'lhh_percent',
169
+ 'start_speed',
170
+ 'max_start_speed',
171
+ 'ivb',
172
+ 'hb',
173
+ 'release_pos_z',
174
+ 'release_pos_x',
175
+ 'extension',
176
+ 'tj_stuff_plus',
177
+ ])
178
+
179
+ return df_merge
180
+
181
+ @session.download(filename="data.csv")
182
+ def download_all():
183
+ yield ts_data().write_csv()
184
  @output
185
  @render_tabulator
186
  @reactive.event(input.refresh)