Spaces:
Sleeping
Sleeping
Commit
·
e7a5154
1
Parent(s):
26e0ac6
Update app
Browse files- demo.py +7 -2
- gradio_function.py +21 -10
demo.py
CHANGED
|
@@ -35,7 +35,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 35 |
[Data from SportsNavi](https://sports.yahoo.co.jp/)
|
| 36 |
''')
|
| 37 |
# player = gr.Dropdown(value=None, choices=sorted(player_df['name'].dropna().tolist()), label='Player')
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
player_info = gr.Markdown()
|
| 40 |
download_file = gr.DownloadButton(label='Download player data')
|
| 41 |
with gr.Group():
|
|
@@ -83,7 +85,10 @@ with gr.Blocks(css=css) as demo:
|
|
| 83 |
'''
|
| 84 |
)
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
demo.launch(
|
| 89 |
share=True,
|
|
|
|
| 35 |
[Data from SportsNavi](https://sports.yahoo.co.jp/)
|
| 36 |
''')
|
| 37 |
# player = gr.Dropdown(value=None, choices=sorted(player_df['name'].dropna().tolist()), label='Player')
|
| 38 |
+
with gr.Row():
|
| 39 |
+
player = gr.Dropdown(value=None, choices=sorted(player_df.filter(pl.col('name').is_not_null())['name'].to_list()), label='Player')
|
| 40 |
+
handedness = gr.Radio(value='Both', choices=['Both', 'Left', 'Right'], type='value', interactive=False, label='Batter Handedness')
|
| 41 |
player_info = gr.Markdown()
|
| 42 |
download_file = gr.DownloadButton(label='Download player data')
|
| 43 |
with gr.Group():
|
|
|
|
| 85 |
'''
|
| 86 |
)
|
| 87 |
|
| 88 |
+
inputs = [player, handedness]
|
| 89 |
+
outputs = [player_info, handedness, download_file, usage, pitch_velo_summary, pitch_loc_summary, *pitch_groups, *pitch_names, *pitch_infos, *pitch_velos, *pitch_maps, velo_stats]
|
| 90 |
+
player.input(get_data, inputs=inputs, outputs=outputs)
|
| 91 |
+
handedness.input(get_data, inputs=inputs, outputs=outputs)
|
| 92 |
|
| 93 |
demo.launch(
|
| 94 |
share=True,
|
gradio_function.py
CHANGED
|
@@ -49,7 +49,7 @@ colorscale = [
|
|
| 49 |
]
|
| 50 |
|
| 51 |
|
| 52 |
-
def plot_pitch_map(player=None, loc=None, pitch_type=None, pitch_name=None, all_pitches=False, min_pitches=2):
|
| 53 |
assert not ((loc is None and player is None) or (loc is not None and player is not None)), 'exactly one of `player` or `loc` must be specified'
|
| 54 |
|
| 55 |
if loc is None and player is not None:
|
|
@@ -109,18 +109,19 @@ def plot_pitch_map(player=None, loc=None, pitch_type=None, pitch_name=None, all_
|
|
| 109 |
|
| 110 |
|
| 111 |
# velo distribution
|
| 112 |
-
def plot_pitch_velo(player=None, velos=None, pitch_type=None, pitch_name=None, min_pitches=2):
|
| 113 |
-
assert not ((velos is None and player is None) or (velos is not None and player is not None)), 'exactly one of `player` or `
|
| 114 |
|
| 115 |
if velos is None and player is not None:
|
| 116 |
assert not ((pitch_type is None and pitch_name is None) or (pitch_type is not None and pitch_name is not None)), 'exactly one of `pitch_type` or `pitch_name` must be specified'
|
|
|
|
| 117 |
pitch_val = pitch_type or pitch_name
|
| 118 |
pitch_col = 'pitch_type' if pitch_type else 'pitch_name'
|
| 119 |
# velos = df.set_index(['name', pitch_col]).sort_index().loc[(player, pitch_val), 'release_speed']
|
| 120 |
velos = df.filter((pl.col('name') == player) & (pl.col(pitch_col) == pitch_val))['release_speed']
|
| 121 |
|
| 122 |
if isinstance(velos, int):
|
| 123 |
-
velos = [velos]
|
| 124 |
|
| 125 |
fig = go.Figure()
|
| 126 |
if len(velos) >= min_pitches:
|
|
@@ -157,11 +158,12 @@ def plot_pitch_velo(player=None, velos=None, pitch_type=None, pitch_name=None, m
|
|
| 157 |
return fig
|
| 158 |
|
| 159 |
|
| 160 |
-
def plot_all_pitch_velo(player=None, player_df=None, pitch_counts=None, min_pitches=2):
|
| 161 |
# assert not ((player is None and player_df is None) or (player is not None and player_df is not None)), 'exactly one of `player` or `player_df` must be specified'
|
| 162 |
|
| 163 |
if player_df is None and player is not None:
|
| 164 |
assert pitch_counts is None, '`pitch_counts` must be `None` if `player_df` is None'
|
|
|
|
| 165 |
# player_df = df.set_index('name').sort_index().loc[player].sort_values('pitch_name').set_index('pitch_name')
|
| 166 |
# pitch_counts = player_df.index.value_counts(ascending=True)
|
| 167 |
player_df = df.filter((pl.col('name') == player) & (pl.col('release_speed').is_not_null()))
|
|
@@ -251,13 +253,22 @@ def plot_all_pitch_velo(player=None, player_df=None, pitch_counts=None, min_pitc
|
|
| 251 |
return fig
|
| 252 |
|
| 253 |
|
| 254 |
-
def get_data(player):
|
| 255 |
player_name = f'# {player}'
|
| 256 |
|
| 257 |
# _df = df.set_index('name').sort_index().loc[player]
|
| 258 |
# _df.to_csv(f'files/npb.csv', index=False)
|
| 259 |
# _df_by_pitch_name = _df.set_index('pitch_name').sort_index()
|
| 260 |
_df = df.filter(pl.col('name') == player)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
# usage_fig = px.pie(_df['pitch_name'], names='pitch_name')
|
| 263 |
usage_fig = px.pie(_df.select('pitch_name'), names='pitch_name')
|
|
@@ -267,8 +278,8 @@ def get_data(player):
|
|
| 267 |
pitch_counts = _df['pitch_name'].value_counts().sort('count', descending=True)
|
| 268 |
|
| 269 |
# pitch_velo_summary = plot_all_pitch_velo(player=player, player_df=_df_by_pitch_name, pitch_counts=pitch_counts.sort_values(ascending=True))
|
| 270 |
-
pitch_velo_summary = plot_all_pitch_velo(player=player, player_df=_df.filter(pl.col('release_speed').is_not_null()), pitch_counts=pitch_counts.sort('count', descending=False))
|
| 271 |
-
pitch_loc_summary = plot_pitch_map(player, all_pitches=True)
|
| 272 |
|
| 273 |
pitch_groups = []
|
| 274 |
pitch_names = []
|
|
@@ -295,7 +306,7 @@ def get_data(player):
|
|
| 295 |
visible=True
|
| 296 |
))
|
| 297 |
pitch_maps.append(gr.update(
|
| 298 |
-
value=plot_pitch_map(player, pitch_name=pitch_name),
|
| 299 |
label='Pitch location',
|
| 300 |
visible=True
|
| 301 |
))
|
|
@@ -322,4 +333,4 @@ def get_data(player):
|
|
| 322 |
.drop('Count')
|
| 323 |
)
|
| 324 |
|
| 325 |
-
return player_name, 'files/npb.csv', usage_fig, pitch_velo_summary, pitch_loc_summary, *pitch_groups, *pitch_names, *pitch_infos, *pitch_velos, *pitch_maps, velo_stats
|
|
|
|
| 49 |
]
|
| 50 |
|
| 51 |
|
| 52 |
+
def plot_pitch_map(df, player=None, loc=None, pitch_type=None, pitch_name=None, all_pitches=False, min_pitches=2):
|
| 53 |
assert not ((loc is None and player is None) or (loc is not None and player is not None)), 'exactly one of `player` or `loc` must be specified'
|
| 54 |
|
| 55 |
if loc is None and player is not None:
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
# velo distribution
|
| 112 |
+
def plot_pitch_velo(df=None, player=None, velos=None, pitch_type=None, pitch_name=None, min_pitches=2):
|
| 113 |
+
assert not ((velos is None and player is None) or (velos is not None and player is not None)), 'exactly one of `player` or `velos` must be specified'
|
| 114 |
|
| 115 |
if velos is None and player is not None:
|
| 116 |
assert not ((pitch_type is None and pitch_name is None) or (pitch_type is not None and pitch_name is not None)), 'exactly one of `pitch_type` or `pitch_name` must be specified'
|
| 117 |
+
assert df is not None, '`df` must be provided if `velos` not provided'
|
| 118 |
pitch_val = pitch_type or pitch_name
|
| 119 |
pitch_col = 'pitch_type' if pitch_type else 'pitch_name'
|
| 120 |
# velos = df.set_index(['name', pitch_col]).sort_index().loc[(player, pitch_val), 'release_speed']
|
| 121 |
velos = df.filter((pl.col('name') == player) & (pl.col(pitch_col) == pitch_val))['release_speed']
|
| 122 |
|
| 123 |
if isinstance(velos, int):
|
| 124 |
+
velos = [velos] # is this line still necessary after porting to polars?
|
| 125 |
|
| 126 |
fig = go.Figure()
|
| 127 |
if len(velos) >= min_pitches:
|
|
|
|
| 158 |
return fig
|
| 159 |
|
| 160 |
|
| 161 |
+
def plot_all_pitch_velo(df=None, player=None, player_df=None, pitch_counts=None, min_pitches=2):
|
| 162 |
# assert not ((player is None and player_df is None) or (player is not None and player_df is not None)), 'exactly one of `player` or `player_df` must be specified'
|
| 163 |
|
| 164 |
if player_df is None and player is not None:
|
| 165 |
assert pitch_counts is None, '`pitch_counts` must be `None` if `player_df` is None'
|
| 166 |
+
assert df is not None, '`df` must be provided if `player_df` is None'
|
| 167 |
# player_df = df.set_index('name').sort_index().loc[player].sort_values('pitch_name').set_index('pitch_name')
|
| 168 |
# pitch_counts = player_df.index.value_counts(ascending=True)
|
| 169 |
player_df = df.filter((pl.col('name') == player) & (pl.col('release_speed').is_not_null()))
|
|
|
|
| 253 |
return fig
|
| 254 |
|
| 255 |
|
| 256 |
+
def get_data(player, handedness):
|
| 257 |
player_name = f'# {player}'
|
| 258 |
|
| 259 |
# _df = df.set_index('name').sort_index().loc[player]
|
| 260 |
# _df.to_csv(f'files/npb.csv', index=False)
|
| 261 |
# _df_by_pitch_name = _df.set_index('pitch_name').sort_index()
|
| 262 |
_df = df.filter(pl.col('name') == player)
|
| 263 |
+
league_df = df
|
| 264 |
+
if handedness == 'Right':
|
| 265 |
+
_df = _df.filter(pl.col('stand') == 'R')
|
| 266 |
+
league_df = league_df.filter(pl.col('stand') == 'R')
|
| 267 |
+
elif handedness == 'Left':
|
| 268 |
+
_df = _df.filter(pl.col('stand') == 'L')
|
| 269 |
+
league_df = league_df.filter(pl.col('stand') == 'L')
|
| 270 |
+
|
| 271 |
+
handedness = gr.update(value=handedness, interactive=True)
|
| 272 |
|
| 273 |
# usage_fig = px.pie(_df['pitch_name'], names='pitch_name')
|
| 274 |
usage_fig = px.pie(_df.select('pitch_name'), names='pitch_name')
|
|
|
|
| 278 |
pitch_counts = _df['pitch_name'].value_counts().sort('count', descending=True)
|
| 279 |
|
| 280 |
# pitch_velo_summary = plot_all_pitch_velo(player=player, player_df=_df_by_pitch_name, pitch_counts=pitch_counts.sort_values(ascending=True))
|
| 281 |
+
pitch_velo_summary = plot_all_pitch_velo(df=league_df, player=player, player_df=_df.filter(pl.col('release_speed').is_not_null()), pitch_counts=pitch_counts.sort('count', descending=False))
|
| 282 |
+
pitch_loc_summary = plot_pitch_map(df=_df, player=player, all_pitches=True)
|
| 283 |
|
| 284 |
pitch_groups = []
|
| 285 |
pitch_names = []
|
|
|
|
| 306 |
visible=True
|
| 307 |
))
|
| 308 |
pitch_maps.append(gr.update(
|
| 309 |
+
value=plot_pitch_map(df=_df, player=player, pitch_name=pitch_name),
|
| 310 |
label='Pitch location',
|
| 311 |
visible=True
|
| 312 |
))
|
|
|
|
| 333 |
.drop('Count')
|
| 334 |
)
|
| 335 |
|
| 336 |
+
return player_name, handedness, 'files/npb.csv', usage_fig, pitch_velo_summary, pitch_loc_summary, *pitch_groups, *pitch_names, *pitch_infos, *pitch_velos, *pitch_maps, velo_stats
|