lschlessinger commited on
Commit
1eb15c8
·
1 Parent(s): 51e64b6

feat: make df cols more readable

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -49,6 +49,16 @@ def _fix_dtypes(df: pd.DataFrame, is_tournament: bool) -> pd.DataFrame:
49
 
50
  return df
51
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  def _check_match_type(match_type: str) -> str:
54
  allowed_match_types = {"tournament", "league"}
@@ -198,12 +208,12 @@ def usatt_rating_analyzer(file_obj):
198
  matches_per_competition_fig = get_matches_per_competition_fig(df, is_tournament)
199
  opponent_name_word_cloud_fig = get_opponent_name_word_cloud_fig(df)
200
  competition_name_word_cloud_fig = get_competition_name_word_cloud_fig(df, is_tournament)
201
- most_frequent_opponents = get_most_frequent_opponents(df)
202
- best_wins = get_best_wins(df)
203
- biggest_upsets = get_biggest_upsets(df)
204
- highest_rated_opponent = get_highest_rated_opponent(df)
205
  rating_over_time_fig = get_rating_over_time_fig(df, is_tournament)
206
- match_with_longest_game = get_match_with_longest_game(df, is_tournament)
207
  opponent_rating_distr_fig = get_opponent_rating_distr_fig(df)
208
  opponent_rating_dist_over_time_fig = get_opponent_rating_dist_over_time_fig(df, is_tournament)
209
 
@@ -227,12 +237,10 @@ with gr.Blocks() as demo:
227
  analyze_btn_title = "Analyze"
228
  gr.Markdown(f"""# USATT rating analyzer
229
  Analyze [USA table tennis](https://www.teamusa.org/usa-table-tennis) tournament and league results.
230
-
231
  ## Downloading match results
232
  1. Make sure you are [logged in](https://usatt.simplycompete.com/login/auth) to your USATT account.
233
  2. Find the *active* player you wish to analyze (e.g., [Kanak Jha](https://usatt.simplycompete.com/userAccount/up/3431)).
234
  3. Under 'Tournaments' or 'Leagues', click *Download Tournament/League Match History*.
235
-
236
  ## Usage
237
  1. Simply add your tournament/league match history CSV file and click the "{analyze_btn_title}" button.
238
  """)
@@ -247,13 +255,13 @@ with gr.Blocks() as demo:
247
  num_comps_box = gr.Textbox(lines=1, label="Number of competitions (tournaments/leagues) played")
248
  with gr.Column():
249
  num_matches_box = gr.Textbox(lines=1, label="Number of matches played")
250
-
251
  with gr.Row():
252
  with gr.Column():
253
  rating_over_time_plot = gr.Plot(show_label=False)
254
  with gr.Column():
255
  matches_per_comp_plot = gr.Plot(show_label=False)
256
-
257
  with gr.Row():
258
  with gr.Column():
259
  opponent_names_plot = gr.Plot(label="Opponent names")
@@ -263,7 +271,7 @@ with gr.Blocks() as demo:
263
  with gr.Row():
264
  with gr.Column():
265
  most_frequent_opponents_gdf = gr.Dataframe(label="Most frequent opponents", max_rows=5)
266
- best_wins_gdf = gr.Dataframe(label="Best wins (matches won sorted by opponent post-competition rating)",
267
  max_rows=5)
268
  biggest_upsets_gdf = gr.Dataframe(label="Biggest upsets (matches won sorted by rating - opponent post-competition rating)",
269
  max_rows=5)
@@ -294,7 +302,6 @@ with gr.Blocks() as demo:
294
  opponent_rating_dist_over_time_plot,
295
  ]
296
 
297
-
298
  btn.click(usatt_rating_analyzer, inputs=inputs, outputs=outputs)
299
 
300
  if __name__ == "__main__":
 
49
 
50
  return df
51
 
52
+ def make_df_columns_readable(df: Optional[pd.DataFrame], is_tournament: bool) -> Optional[pd.DataFrame]:
53
+ """Make a data frame's columns human-readable."""
54
+ if df is None:
55
+ return None
56
+
57
+ if not is_tournament:
58
+ df = df.rename(columns={"league_name": "league"})
59
+
60
+ df = df.rename(columns=lambda c: " ".join(c.capitalize().split("_")))
61
+ return df
62
 
63
  def _check_match_type(match_type: str) -> str:
64
  allowed_match_types = {"tournament", "league"}
 
208
  matches_per_competition_fig = get_matches_per_competition_fig(df, is_tournament)
209
  opponent_name_word_cloud_fig = get_opponent_name_word_cloud_fig(df)
210
  competition_name_word_cloud_fig = get_competition_name_word_cloud_fig(df, is_tournament)
211
+ most_frequent_opponents = make_df_columns_readable(get_most_frequent_opponents(df), is_tournament)
212
+ best_wins = make_df_columns_readable(get_best_wins(df), is_tournament)
213
+ biggest_upsets = make_df_columns_readable(get_biggest_upsets(df), is_tournament)
214
+ highest_rated_opponent = make_df_columns_readable(get_highest_rated_opponent(df), is_tournament)
215
  rating_over_time_fig = get_rating_over_time_fig(df, is_tournament)
216
+ match_with_longest_game = make_df_columns_readable(get_match_with_longest_game(df, is_tournament), is_tournament)
217
  opponent_rating_distr_fig = get_opponent_rating_distr_fig(df)
218
  opponent_rating_dist_over_time_fig = get_opponent_rating_dist_over_time_fig(df, is_tournament)
219
 
 
237
  analyze_btn_title = "Analyze"
238
  gr.Markdown(f"""# USATT rating analyzer
239
  Analyze [USA table tennis](https://www.teamusa.org/usa-table-tennis) tournament and league results.
 
240
  ## Downloading match results
241
  1. Make sure you are [logged in](https://usatt.simplycompete.com/login/auth) to your USATT account.
242
  2. Find the *active* player you wish to analyze (e.g., [Kanak Jha](https://usatt.simplycompete.com/userAccount/up/3431)).
243
  3. Under 'Tournaments' or 'Leagues', click *Download Tournament/League Match History*.
 
244
  ## Usage
245
  1. Simply add your tournament/league match history CSV file and click the "{analyze_btn_title}" button.
246
  """)
 
255
  num_comps_box = gr.Textbox(lines=1, label="Number of competitions (tournaments/leagues) played")
256
  with gr.Column():
257
  num_matches_box = gr.Textbox(lines=1, label="Number of matches played")
258
+
259
  with gr.Row():
260
  with gr.Column():
261
  rating_over_time_plot = gr.Plot(show_label=False)
262
  with gr.Column():
263
  matches_per_comp_plot = gr.Plot(show_label=False)
264
+
265
  with gr.Row():
266
  with gr.Column():
267
  opponent_names_plot = gr.Plot(label="Opponent names")
 
271
  with gr.Row():
272
  with gr.Column():
273
  most_frequent_opponents_gdf = gr.Dataframe(label="Most frequent opponents", max_rows=5)
274
+ best_wins_gdf = gr.Dataframe(label="Best wins (matches won sorted by opponent post-competition rating)",
275
  max_rows=5)
276
  biggest_upsets_gdf = gr.Dataframe(label="Biggest upsets (matches won sorted by rating - opponent post-competition rating)",
277
  max_rows=5)
 
302
  opponent_rating_dist_over_time_plot,
303
  ]
304
 
 
305
  btn.click(usatt_rating_analyzer, inputs=inputs, outputs=outputs)
306
 
307
  if __name__ == "__main__":