lschlessinger/usatt-rating-analyzer
0
1from pathlib import Path2 3import gradio as gr4 5import match_parser as mp6 7 8def usatt_rating_analyzer(file_obj):9 # Load data.10 df, is_tournament = mp.load_match_df(Path(file_obj.name))11 12 # Create outputs.13 current_rating = mp.get_current_rating(df)14 peak_rating = mp.get_max_rating(df)15 n_competitions_played = mp.get_num_competitions_played(df, is_tournament)16 n_matches_played = len(df)17 first_comp_year = mp.get_first_competition_year(df, is_tournament)18 n_active_years = mp.get_num_active_years(df, is_tournament)19 matches_per_competition_fig = mp.get_matches_per_competition_fig(df, is_tournament)20 opponent_name_word_cloud_fig = mp.get_opponent_name_word_cloud_fig(df)21 competition_name_word_cloud_fig = mp.get_competition_name_word_cloud_fig(df, is_tournament)22 best_competitions = mp.make_df_columns_readable(mp.get_best_competitions(df, is_tournament), is_tournament)23 most_frequent_opponents = mp.make_df_columns_readable(mp.get_most_frequent_opponents(df), is_tournament)24 best_wins = mp.make_df_columns_readable(mp.get_best_wins(df), is_tournament)25 biggest_upsets = mp.make_df_columns_readable(mp.get_biggest_upsets(df), is_tournament)26 worst_recent_losses = mp.make_df_columns_readable(mp.get_worst_recent_losses(df, is_tournament), is_tournament)27 highest_rated_opponent = mp.make_df_columns_readable(mp.get_highest_rated_opponent(df), is_tournament)28 rating_over_time_fig = mp.get_rating_over_time_fig(df, is_tournament)29 match_with_longest_game = mp.make_df_columns_readable(mp.get_match_with_longest_game(df, is_tournament),30 is_tournament)31 longest_match = mp.make_df_columns_readable(mp.get_longest_match(df, is_tournament), is_tournament)32 opponent_rating_distr_fig = mp.get_opponent_rating_distr_fig(df)33 opponent_rating_dist_over_time_fig = mp.get_opponent_rating_dist_over_time_fig(df, is_tournament)34 35 return ( # player_name,36 current_rating,37 peak_rating,38 n_competitions_played,39 n_matches_played,40 first_comp_year,41 n_active_years,42 rating_over_time_fig,43 opponent_rating_distr_fig,44 opponent_rating_dist_over_time_fig,45 best_wins,46 biggest_upsets,47 worst_recent_losses,48 best_competitions,49 most_frequent_opponents,50 highest_rated_opponent,51 match_with_longest_game,52 longest_match,53 opponent_name_word_cloud_fig,54 competition_name_word_cloud_fig,55 matches_per_competition_fig,56 )57 58 59with gr.Blocks() as demo:60 analyze_btn_title = "Analyze"61 gr.Markdown(f"""# USATT rating analyzer62 Analyze [USA table tennis](https://www.teamusa.org/usa-table-tennis) tournament and league results. The more matches63 and competitions you have played, the better the tool works. Additionally, due to limitations on the available 64 data, ratings are always displayed as the rating received *after* the competition has been played. Here, 65 "competition" is defined as a tournament or a league.66 ## Downloading match results67 1. Make sure you are [logged in](https://usatt.simplycompete.com/login/auth) to your USATT account.68 2. Find the *active* player you wish to analyze (e.g., [Kanak Jha](https://usatt.simplycompete.com/userAccount/up/3431)).69 3. Under 'Tournaments' or 'Leagues', click *Download Tournament/League Match History*.70 ## Usage71 1. Simply add your tournament/league match history CSV file and click the "{analyze_btn_title}" button.72 73 ---74 75 """)76 with gr.Row():77 with gr.Column():78 input_file = gr.File(label='USATT Results File', file_types=['file'])79 btn = gr.Button(analyze_btn_title)80 81 gr.Markdown("""<br />82 83 ## Overview84 85 <br />86 """)87 88 with gr.Group():89 with gr.Row():90 with gr.Column():91 current_rating_box = gr.Textbox(lines=1, label="Current rating")92 with gr.Column():93 peak_rating_box = gr.Textbox(lines=1, label="Highest rating")94 with gr.Column():95 num_comps_box = gr.Textbox(lines=1, label="Number of competitions played")96 with gr.Column():97 num_matches_box = gr.Textbox(lines=1, label="Number of matches played")98 with gr.Column():99 first_competition_box = gr.Textbox(lines=1, label="First competition played")100 with gr.Column():101 num_active_years_box = gr.Textbox(lines=1,102 label="Number of active years (participated in at least 1 competition)")103 104 with gr.Row():105 with gr.Column():106 rating_over_time_plot = gr.Plot(show_label=False)107 108 with gr.Row():109 with gr.Column():110 opponent_rating_dist_plot = gr.Plot(show_label=False)111 with gr.Column():112 opponent_rating_dist_over_time_plot = gr.Plot(show_label=False)113 114 gr.Markdown("""<br />115 116 ## Best and Worst Matches117 118 <br />119 """)120 121 with gr.Row():122 with gr.Column():123 best_wins_gdf = gr.Dataframe(label="Best wins (matches won sorted by opponent post-competition rating)",124 height=500)125 biggest_upsets_gdf = gr.Dataframe(126 label="Biggest upsets (matches won sorted by rating - opponent post-competition rating)",127 height=500)128 worst_recent_losses_gdf = gr.Dataframe(label="Worst recent losses (matches lost sorted by opponent "129 "post-competition rating from the 5 most recent "130 "competitions)", height=500)131 132 gr.Markdown("""<br />133 134 ## Fun Facts135 136 <br />137 """)138 139 with gr.Row():140 with gr.Column():141 best_competitions_gdf = gr.Dataframe(142 label="Best competitions (those having the largest increase in rating)",143 height=500)144 most_frequent_opponents_gdf = gr.Dataframe(label="Most frequent opponents", height=500)145 highest_rated_opponent_gdf = gr.Dataframe(label="Highest rated opponent", height=100)146 match_longest_game_gdf = gr.Dataframe(label="Match with longest game", height=100)147 longest_match_gdf = gr.Dataframe(label="Longest match (highest number of points played)", height=100)148 149 with gr.Row():150 with gr.Column():151 opponent_names_plot = gr.Plot(label="Opponent names")152 with gr.Column():153 comp_names_plot = gr.Plot(label="Competition names")154 with gr.Column():155 matches_per_comp_plot = gr.Plot(show_label=False)156 157 inputs = [input_file]158 outputs = [159 current_rating_box,160 peak_rating_box,161 num_comps_box,162 num_matches_box,163 first_competition_box,164 num_active_years_box,165 rating_over_time_plot,166 opponent_rating_dist_plot,167 opponent_rating_dist_over_time_plot,168 best_wins_gdf,169 biggest_upsets_gdf,170 worst_recent_losses_gdf,171 best_competitions_gdf,172 most_frequent_opponents_gdf,173 highest_rated_opponent_gdf,174 match_longest_game_gdf,175 longest_match_gdf,176 opponent_names_plot,177 comp_names_plot,178 matches_per_comp_plot,179 ]180 181 btn.click(usatt_rating_analyzer, inputs=inputs, outputs=outputs)182 183if __name__ == "__main__":184 demo.launch()185 