Commit
·
d963711
1
Parent(s):
c67c9bd
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
|
|
| 1 |
from typing import Optional, Tuple
|
| 2 |
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
-
|
| 6 |
import seaborn as sns
|
| 7 |
-
import matplotlib.pyplot as plt
|
| 8 |
from wordcloud import WordCloud
|
| 9 |
-
|
| 10 |
|
| 11 |
def _rename_columns(df: pd.DataFrame, is_tournament: bool) -> pd.DataFrame:
|
| 12 |
columns = {
|
|
@@ -49,6 +51,9 @@ def _fix_dtypes(df: pd.DataFrame, is_tournament: bool) -> pd.DataFrame:
|
|
| 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:
|
|
@@ -83,7 +88,7 @@ def make_df_columns_readable(df: Optional[pd.DataFrame], is_tournament: bool) ->
|
|
| 83 |
df['event_date'] = df['event_date'].dt.date.astype(str).apply(nat_to_none)
|
| 84 |
df = df.rename(columns={"league_name": "league"})
|
| 85 |
|
| 86 |
-
df = df.rename(columns=lambda c:
|
| 87 |
return df
|
| 88 |
|
| 89 |
def _check_match_type(match_type: str) -> str:
|
|
@@ -125,15 +130,21 @@ def get_opponent_name_word_cloud_fig(df: pd.DataFrame):
|
|
| 125 |
|
| 126 |
|
| 127 |
def get_rating_over_time_fig(df: pd.DataFrame, is_tournament: bool):
|
| 128 |
-
fig =
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
return fig
|
| 138 |
|
| 139 |
|
|
@@ -208,6 +219,7 @@ def get_opponent_rating_dist_over_time_fig(df: pd.DataFrame, is_tournament: bool
|
|
| 208 |
inner='points',
|
| 209 |
cut=1,
|
| 210 |
ax=ax)
|
|
|
|
| 211 |
plt.xlabel('Competition year')
|
| 212 |
plt.ylabel('Opponent rating')
|
| 213 |
return fig
|
|
@@ -262,7 +274,9 @@ def usatt_rating_analyzer(file_obj):
|
|
| 262 |
with gr.Blocks() as demo:
|
| 263 |
analyze_btn_title = "Analyze"
|
| 264 |
gr.Markdown(f"""# USATT rating analyzer
|
| 265 |
-
Analyze [USA table tennis](https://www.teamusa.org/usa-table-tennis) tournament and league results.
|
|
|
|
|
|
|
| 266 |
## Downloading match results
|
| 267 |
1. Make sure you are [logged in](https://usatt.simplycompete.com/login/auth) to your USATT account.
|
| 268 |
2. Find the *active* player you wish to analyze (e.g., [Kanak Jha](https://usatt.simplycompete.com/userAccount/up/3431)).
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
from typing import Optional, Tuple
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import numpy as np
|
| 7 |
import pandas as pd
|
| 8 |
+
import plotly.graph_objects as go
|
| 9 |
import seaborn as sns
|
|
|
|
| 10 |
from wordcloud import WordCloud
|
| 11 |
+
|
| 12 |
|
| 13 |
def _rename_columns(df: pd.DataFrame, is_tournament: bool) -> pd.DataFrame:
|
| 14 |
columns = {
|
|
|
|
| 51 |
|
| 52 |
return df
|
| 53 |
|
| 54 |
+
def snake_case_to_human_readable(s: str) -> str:
|
| 55 |
+
return " ".join(s.capitalize().split("_"))
|
| 56 |
+
|
| 57 |
def make_df_columns_readable(df: Optional[pd.DataFrame], is_tournament: bool) -> Optional[pd.DataFrame]:
|
| 58 |
"""Make a data frame's columns human-readable."""
|
| 59 |
if df is None:
|
|
|
|
| 88 |
df['event_date'] = df['event_date'].dt.date.astype(str).apply(nat_to_none)
|
| 89 |
df = df.rename(columns={"league_name": "league"})
|
| 90 |
|
| 91 |
+
df = df.rename(columns=lambda c: snake_case_to_human_readable(c))
|
| 92 |
return df
|
| 93 |
|
| 94 |
def _check_match_type(match_type: str) -> str:
|
|
|
|
| 130 |
|
| 131 |
|
| 132 |
def get_rating_over_time_fig(df: pd.DataFrame, is_tournament: bool):
|
| 133 |
+
fig = go.Figure()
|
| 134 |
+
fig.add_trace(go.Scatter(x=df["tournament_end_date" if is_tournament else "event_date"],
|
| 135 |
+
y=df["rating"],
|
| 136 |
+
mode='lines+markers',
|
| 137 |
+
line=dict( width=0.9),
|
| 138 |
+
marker=dict(size=4))),
|
| 139 |
+
|
| 140 |
+
fig.update_layout(
|
| 141 |
+
title='Rating over time',
|
| 142 |
+
xaxis_title='Competition date',
|
| 143 |
+
yaxis_title='Rating',
|
| 144 |
+
showlegend=False,
|
| 145 |
+
template="plotly_white",
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
return fig
|
| 149 |
|
| 150 |
|
|
|
|
| 219 |
inner='points',
|
| 220 |
cut=1,
|
| 221 |
ax=ax)
|
| 222 |
+
plt.xticks(rotation=30)
|
| 223 |
plt.xlabel('Competition year')
|
| 224 |
plt.ylabel('Opponent rating')
|
| 225 |
return fig
|
|
|
|
| 274 |
with gr.Blocks() as demo:
|
| 275 |
analyze_btn_title = "Analyze"
|
| 276 |
gr.Markdown(f"""# USATT rating analyzer
|
| 277 |
+
Analyze [USA table tennis](https://www.teamusa.org/usa-table-tennis) tournament and league results. The more matches
|
| 278 |
+
and competitions you have played, the better the tool works. Additionally, due to limitations on the available
|
| 279 |
+
data, ratings are always displayed as the rating received *after* the competition has been played.
|
| 280 |
## Downloading match results
|
| 281 |
1. Make sure you are [logged in](https://usatt.simplycompete.com/login/auth) to your USATT account.
|
| 282 |
2. Find the *active* player you wish to analyze (e.g., [Kanak Jha](https://usatt.simplycompete.com/userAccount/up/3431)).
|