lschlessinger's picture
remove comment
416a506
raw
history blame
7.72 kB
from pathlib import Path
import gradio as gr
import match_parser as mp
def usatt_rating_analyzer(file_obj):
# Load data.
df, is_tournament = mp.load_match_df(Path(file_obj.name))
# Create outputs.
current_rating = mp.get_current_rating(df)
peak_rating = mp.get_max_rating(df)
n_competitions_played = mp.get_num_competitions_played(df, is_tournament)
n_matches_played = len(df)
first_comp_year = mp.get_first_competition_year(df, is_tournament)
n_active_years = mp.get_num_active_years(df, is_tournament)
matches_per_competition_fig = mp.get_matches_per_competition_fig(df, is_tournament)
opponent_name_word_cloud_fig = mp.get_opponent_name_word_cloud_fig(df)
competition_name_word_cloud_fig = mp.get_competition_name_word_cloud_fig(df, is_tournament)
best_competitions = mp.make_df_columns_readable(mp.get_best_competitions(df, is_tournament), is_tournament)
most_frequent_opponents = mp.make_df_columns_readable(mp.get_most_frequent_opponents(df), is_tournament)
best_wins = mp.make_df_columns_readable(mp.get_best_wins(df), is_tournament)
biggest_upsets = mp.make_df_columns_readable(mp.get_biggest_upsets(df), is_tournament)
worst_recent_losses = mp.make_df_columns_readable(mp.get_worst_recent_losses(df, is_tournament), is_tournament)
highest_rated_opponent = mp.make_df_columns_readable(mp.get_highest_rated_opponent(df), is_tournament)
rating_over_time_fig = mp.get_rating_over_time_fig(df, is_tournament)
match_with_longest_game = mp.make_df_columns_readable(mp.get_match_with_longest_game(df, is_tournament),
is_tournament)
longest_match = mp.make_df_columns_readable(mp.get_longest_match(df, is_tournament), is_tournament)
opponent_rating_distr_fig = mp.get_opponent_rating_distr_fig(df)
opponent_rating_dist_over_time_fig = mp.get_opponent_rating_dist_over_time_fig(df, is_tournament)
return ( # player_name,
current_rating,
peak_rating,
n_competitions_played,
n_matches_played,
first_comp_year,
n_active_years,
rating_over_time_fig,
opponent_rating_distr_fig,
opponent_rating_dist_over_time_fig,
best_wins,
biggest_upsets,
worst_recent_losses,
best_competitions,
most_frequent_opponents,
highest_rated_opponent,
match_with_longest_game,
longest_match,
opponent_name_word_cloud_fig,
competition_name_word_cloud_fig,
matches_per_competition_fig,
)
with gr.Blocks() as demo:
analyze_btn_title = "Analyze"
gr.Markdown(f"""# USATT rating analyzer
Analyze [USA table tennis](https://www.teamusa.org/usa-table-tennis) tournament and league results. The more matches
and competitions you have played, the better the tool works. Additionally, due to limitations on the available
data, ratings are always displayed as the rating received *after* the competition has been played. Here,
"competition" is defined as a tournament or a league.
## Downloading match results
1. Make sure you are [logged in](https://usatt.simplycompete.com/login/auth) to your USATT account.
2. Find the *active* player you wish to analyze (e.g., [Kanak Jha](https://usatt.simplycompete.com/userAccount/up/3431)).
3. Under 'Tournaments' or 'Leagues', click *Download Tournament/League Match History*.
## Usage
1. Simply add your tournament/league match history CSV file and click the "{analyze_btn_title}" button.
---
""")
with gr.Row():
with gr.Column():
input_file = gr.File(label='USATT Results File', file_types=['file'])
btn = gr.Button(analyze_btn_title)
gr.Markdown("""<br />
## Overview
<br />
""")
with gr.Group():
with gr.Row():
with gr.Column():
current_rating_box = gr.Textbox(lines=1, label="Current rating")
with gr.Column():
peak_rating_box = gr.Textbox(lines=1, label="Highest rating")
with gr.Column():
num_comps_box = gr.Textbox(lines=1, label="Number of competitions played")
with gr.Column():
num_matches_box = gr.Textbox(lines=1, label="Number of matches played")
with gr.Column():
first_competition_box = gr.Textbox(lines=1, label="First competition played")
with gr.Column():
num_active_years_box = gr.Textbox(lines=1,
label="Number of active years (participated in at least 1 competition)")
with gr.Row():
with gr.Column():
rating_over_time_plot = gr.Plot(show_label=False)
with gr.Row():
with gr.Column():
opponent_rating_dist_plot = gr.Plot(show_label=False)
with gr.Column():
opponent_rating_dist_over_time_plot = gr.Plot(show_label=False)
gr.Markdown("""<br />
## Best and Worst Matches
<br />
""")
with gr.Row():
with gr.Column():
best_wins_gdf = gr.Dataframe(label="Best wins (matches won sorted by opponent post-competition rating)",
height=500)
biggest_upsets_gdf = gr.Dataframe(
label="Biggest upsets (matches won sorted by rating - opponent post-competition rating)",
height=500)
worst_recent_losses_gdf = gr.Dataframe(label="Worst recent losses (matches lost sorted by opponent "
"post-competition rating from the 5 most recent "
"competitions)", height=500)
gr.Markdown("""<br />
## Fun Facts
<br />
""")
with gr.Row():
with gr.Column():
best_competitions_gdf = gr.Dataframe(
label="Best competitions (those having the largest increase in rating)",
height=500)
most_frequent_opponents_gdf = gr.Dataframe(label="Most frequent opponents", height=500)
highest_rated_opponent_gdf = gr.Dataframe(label="Highest rated opponent", height=100)
match_longest_game_gdf = gr.Dataframe(label="Match with longest game", height=100)
longest_match_gdf = gr.Dataframe(label="Longest match (highest number of points played)", height=100)
with gr.Row():
with gr.Column():
opponent_names_plot = gr.Plot(label="Opponent names")
with gr.Column():
comp_names_plot = gr.Plot(label="Competition names")
with gr.Column():
matches_per_comp_plot = gr.Plot(show_label=False)
inputs = [input_file]
outputs = [
current_rating_box,
peak_rating_box,
num_comps_box,
num_matches_box,
first_competition_box,
num_active_years_box,
rating_over_time_plot,
opponent_rating_dist_plot,
opponent_rating_dist_over_time_plot,
best_wins_gdf,
biggest_upsets_gdf,
worst_recent_losses_gdf,
best_competitions_gdf,
most_frequent_opponents_gdf,
highest_rated_opponent_gdf,
match_longest_game_gdf,
longest_match_gdf,
opponent_names_plot,
comp_names_plot,
matches_per_comp_plot,
]
btn.click(usatt_rating_analyzer, inputs=inputs, outputs=outputs)
if __name__ == "__main__":
demo.launch()