lschlessinger commited on
Commit
0c6b2bb
·
1 Parent(s): 663f0b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py CHANGED
@@ -113,6 +113,34 @@ def get_match_with_longest_game(df: pd.DataFrame, is_tournament: bool) -> Option
113
  return df.loc[[np.argmax(df.scores.apply(get_max_int))]]
114
 
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  def get_opponent_rating_distr_fig(df: pd.DataFrame):
117
  fig = plt.figure()
118
  plt.title('Opponent rating distribution')
@@ -159,6 +187,8 @@ def usatt_rating_analyzer(file_obj):
159
  matches_per_competition_fig = get_matches_per_competition_fig(df, is_tournament)
160
  opponent_name_word_cloud_fig = get_opponent_name_word_cloud_fig(df)
161
  competition_name_word_cloud_fig = get_competition_name_word_cloud_fig(df, is_tournament)
 
 
162
  rating_over_time_fig = get_rating_over_time_fig(df, is_tournament)
163
  match_with_longest_game = get_match_with_longest_game(df, is_tournament)
164
  opponent_rating_distr_fig = get_opponent_rating_distr_fig(df)
@@ -169,6 +199,8 @@ def usatt_rating_analyzer(file_obj):
169
  matches_per_competition_fig,
170
  opponent_name_word_cloud_fig,
171
  competition_name_word_cloud_fig,
 
 
172
  rating_over_time_fig,
173
  match_with_longest_game,
174
  opponent_rating_distr_fig,
@@ -213,6 +245,11 @@ with gr.Blocks() as demo:
213
  with gr.Column():
214
  comp_names_plot = gr.Plot(label="Competition names")
215
 
 
 
 
 
 
216
  match_longest_game_gdf = gr.Dataframe(label="Match with longest game", max_rows=1)
217
 
218
  with gr.Row():
@@ -228,12 +265,15 @@ with gr.Blocks() as demo:
228
  matches_per_comp_plot,
229
  opponent_names_plot,
230
  comp_names_plot,
 
 
231
  rating_over_time_plot,
232
  match_longest_game_gdf,
233
  opponent_rating_dist_plot,
234
  opponent_rating_dist_over_time_plot,
235
  ]
236
 
 
237
  btn.click(usatt_rating_analyzer, inputs=inputs, outputs=outputs)
238
 
239
  if __name__ == "__main__":
 
113
  return df.loc[[np.argmax(df.scores.apply(get_max_int))]]
114
 
115
 
116
+ def get_win_loss_record_str(group_df) -> str:
117
+ if len(group_df) > 0:
118
+ win_loss_counts = group_df.value_counts()
119
+ n_wins = win_loss_counts.Won if hasattr(win_loss_counts, "Won") else 0
120
+ n_losses = win_loss_counts.Lost if hasattr(win_loss_counts, "Lost") else 0
121
+ else:
122
+ n_wins = 0
123
+ n_losses = 0
124
+
125
+ return f"{n_wins}, {n_losses}"
126
+
127
+
128
+ def get_most_frequent_opponents(df: pd.DataFrame, top_n: int = 5) -> pd.DataFrame:
129
+ df_with_opponents = df.loc[df.opponent != "-, -"]
130
+
131
+ most_common_opponents_df = df_with_opponents.groupby('opponent').agg({"result": [get_win_loss_record_str, "size"]})
132
+ most_common_opponents_df.columns = most_common_opponents_df.columns.get_level_values(1)
133
+ most_common_opponents_df.rename({"get_win_loss_record_str": "Win/loss record", "size": "Number of matches"}, axis=1,
134
+ inplace=True)
135
+ most_common_opponents_df["Opponent"] = most_common_opponents_df.index
136
+ return most_common_opponents_df.sort_values("Number of matches", ascending=False)[
137
+ ["Opponent", "Number of matches", "Win/loss record"]].head(top_n)
138
+
139
+
140
+ def get_highest_rated_opponent(df: pd.DataFrame) -> pd.DataFrame:
141
+ return df.iloc[df.opponent_rating.idxmax()].to_frame().transpose()
142
+
143
+
144
  def get_opponent_rating_distr_fig(df: pd.DataFrame):
145
  fig = plt.figure()
146
  plt.title('Opponent rating distribution')
 
187
  matches_per_competition_fig = get_matches_per_competition_fig(df, is_tournament)
188
  opponent_name_word_cloud_fig = get_opponent_name_word_cloud_fig(df)
189
  competition_name_word_cloud_fig = get_competition_name_word_cloud_fig(df, is_tournament)
190
+ most_frequent_opponents = get_most_frequent_opponents(df)
191
+ highest_rated_opponent = get_highest_rated_opponent(df)
192
  rating_over_time_fig = get_rating_over_time_fig(df, is_tournament)
193
  match_with_longest_game = get_match_with_longest_game(df, is_tournament)
194
  opponent_rating_distr_fig = get_opponent_rating_distr_fig(df)
 
199
  matches_per_competition_fig,
200
  opponent_name_word_cloud_fig,
201
  competition_name_word_cloud_fig,
202
+ most_frequent_opponents,
203
+ highest_rated_opponent,
204
  rating_over_time_fig,
205
  match_with_longest_game,
206
  opponent_rating_distr_fig,
 
245
  with gr.Column():
246
  comp_names_plot = gr.Plot(label="Competition names")
247
 
248
+ with gr.Row():
249
+ with gr.Column():
250
+ most_frequent_opponents_gdf = gr.Dataframe(label="Most frequent opponents", max_rows=5)
251
+ highest_rated_opponent_gdf = gr.Dataframe(label="Best opponent", max_rows=1)
252
+
253
  match_longest_game_gdf = gr.Dataframe(label="Match with longest game", max_rows=1)
254
 
255
  with gr.Row():
 
265
  matches_per_comp_plot,
266
  opponent_names_plot,
267
  comp_names_plot,
268
+ most_frequent_opponents_gdf,
269
+ highest_rated_opponent_gdf,
270
  rating_over_time_plot,
271
  match_longest_game_gdf,
272
  opponent_rating_dist_plot,
273
  opponent_rating_dist_over_time_plot,
274
  ]
275
 
276
+
277
  btn.click(usatt_rating_analyzer, inputs=inputs, outputs=outputs)
278
 
279
  if __name__ == "__main__":