patrickramos commited on
Commit
5bb5836
·
1 Parent(s): 473c8e5

Fix mistake in resolving conflict

Browse files
Files changed (2) hide show
  1. data.py +0 -1
  2. demo.py +0 -147
data.py CHANGED
@@ -168,7 +168,6 @@ pitch_stats, rhb_pitch_stats, lhb_pitch_stats = [
168
  pl.len().alias('Count')
169
  )
170
  .sort(['name', 'Count'], descending=[False, True])
171
- .rename({'name': 'Player', 'pitch_name': 'Pitch'})
172
  )
173
  for _df
174
  in (
 
168
  pl.len().alias('Count')
169
  )
170
  .sort(['name', 'Count'], descending=[False, True])
 
171
  )
172
  for _df
173
  in (
demo.py DELETED
@@ -1,147 +0,0 @@
1
-
2
- import gradio as gr
3
- # import pandas as pd
4
- import polars as pl
5
-
6
- from math import ceil
7
- import os
8
-
9
- from data import df, pitch_stats, league_pitch_stats, player_df
10
- from gradio_function import *
11
- from translate import jp_pitch_to_en_pitch, max_pitch_types
12
-
13
- os.makedirs('files', exist_ok=True)
14
-
15
- css = '''
16
- .pitch-usage {height: 256px}
17
- .pitch-usage .js-plotly-plot {height: 100%}
18
-
19
- .pitch-velo {height: 100px}
20
- .pitch-velo .js-plotly-plot {height: 100%}
21
-
22
- .pitch-loc {height: 320px}
23
- .pitch-loc .js-plotly-plot {height: 100%}
24
-
25
- .pitch-velo-summary div.plotly-notifier {visibility: hidden}
26
- '''
27
-
28
- with gr.Blocks(
29
- css=css
30
- ) as demo:
31
- gr.Markdown('''
32
- # NPB data visualization demo
33
- [Data from SportsNavi](https://sports.yahoo.co.jp/)
34
- ''')
35
-
36
- source_df = gr.State(df)
37
- app_df = gr.State(df)
38
- app_league_df = gr.State(df)
39
- app_pitch_stats = gr.State(pitch_stats)
40
- app_league_pitch_stats = gr.State(league_pitch_stats)
41
-
42
- with gr.Tab('Pitcher Dashboard'):
43
- with gr.Row():
44
- player = gr.Dropdown(value=None, choices=sorted(player_df.filter(pl.col('name').is_not_null())['name'].to_list()), label='Player')
45
- handedness = gr.Radio(value='Both', choices=['Both', 'Left', 'Right'], type='value', interactive=False, label='Batter Handedness')
46
-
47
- # preview = gr.DataFrame()
48
- download_file = gr.DownloadButton(label='Download player data')
49
-
50
- with gr.Group():
51
- with gr.Row():
52
- usage = gr.Plot(label='Pitch usage')
53
- velo_summary = gr.Plot(label='Velocity summary', elem_classes='pitch-velo-summary')
54
- loc_summary = gr.Plot(label='Overall location')
55
-
56
- max_locs = len(jp_pitch_to_en_pitch)
57
- locs_per_row = 4
58
- max_rows = ceil(max_locs/locs_per_row)
59
-
60
- gr.Markdown('''
61
- ## Pitch Locations
62
- Pitcher's persective
63
- <br>
64
- `NPB` refers to the top 10% of pitches thrown across the league with the current search constraints e.g. handedness
65
- <br>
66
- Note: To speed up the KDE, we restrict the league-wide pitches to 5,000 pitches
67
- ''')
68
- pitch_rows = []
69
- pitch_groups = []
70
- pitch_names = []
71
- pitch_infos = []
72
- pitch_velos = []
73
- pitch_locs = []
74
- for row in range(max_rows):
75
- visible = row==0
76
- pitch_row = gr.Row(visible=visible)
77
- pitch_rows.append(pitch_row)
78
- with pitch_row:
79
- _locs_per_row = locs_per_row if row < max_rows-1 else max_locs - locs_per_row * (max_rows - 1)
80
- for col in range(_locs_per_row):
81
- with gr.Column(min_width=256):
82
- pitch_group = gr.Group(visible=visible)
83
- pitch_groups.append(pitch_group)
84
- with pitch_group:
85
- pitch_names.append(gr.Markdown(f'### Pitch {col+1}', visible=visible))
86
- pitch_infos.append(gr.DataFrame(pl.DataFrame([{'Whiff%': None, 'CSW%': None}]), interactive=False, visible=visible))
87
- pitch_velos.append(gr.Plot(show_label=False, elem_classes='pitch-velo', visible=visible))
88
- pitch_locs.append(gr.Plot(label='Pitch Location', elem_classes='pitch-loc', visible=visible))
89
-
90
- gr.Markdown('## Pitch Velocity')
91
- velo_stats = gr.DataFrame(pl.DataFrame([{'Avg. Velo': None, 'League Avg. Velo': None}]), interactive=False, label='Pitch Velocity')
92
-
93
- (
94
- player
95
- .input(update_dfs, inputs=[player, handedness, source_df], outputs=[app_df, app_league_df, app_pitch_stats, app_league_pitch_stats])
96
- .then(lambda : gr.update(value='Both', interactive=True), outputs=handedness)
97
- )
98
- handedness.input(update_dfs, inputs=[player, handedness, source_df], outputs=[app_df, app_league_df, app_pitch_stats, app_league_pitch_stats])
99
-
100
- # app_df.change(preview_df, inputs=app_df, outputs=preview)
101
- # app_df.change(set_download_file, inputs=app_df, outputs=download_file)
102
- # app_df.change(plot_usage, inputs=[app_df, player], outputs=usage)
103
- # app_df.change(plot_velo_summary, inputs=[app_df, app_league_df, player], outputs=velo_summary)
104
- # app_df.change(lambda df: plot_loc(df), inputs=app_df, outputs=loc_summary)
105
- # app_df.change(plot_pitch_cards, inputs=[app_df, app_pitch_stats], outputs=pitch_rows+pitch_groups+pitch_names+pitch_infos+pitch_velos+pitch_locs)
106
- app_pitch_stats.change(update_velo_stats, inputs=[app_pitch_stats, app_league_pitch_stats], outputs=velo_stats)
107
-
108
- (
109
- app_df
110
- .change(create_set_download_file_fn('files/player.csv'), inputs=app_df, outputs=download_file)
111
- .then(plot_usage, inputs=[app_df, player], outputs=usage)
112
- .then(plot_velo_summary, inputs=[app_df, app_league_df, player], outputs=velo_summary)
113
- .then(lambda df: plot_loc(df), inputs=app_df, outputs=loc_summary)
114
- .then(plot_pitch_cards, inputs=[app_df, app_league_df, app_pitch_stats], outputs=pitch_rows+pitch_groups+pitch_names+pitch_infos+pitch_velos+pitch_locs)
115
- )
116
-
117
- gr.Markdown('## Bugs and other notes')
118
- with gr.Accordion('Click to open', open=False):
119
- gr.Markdown('''
120
- - Y axis ticks messy when no velocity distribution is plotted
121
- - DataFrame precision inconsistent
122
- '''
123
- )
124
-
125
- with gr.Tab('Pitch Leaderboard'):
126
- def filter_pitch_leaderboard(min_pitches):
127
- return pitch_stats.filter(pl.col('Count') >= min_pitches).sort('CSW%', descending=True)
128
-
129
- init_min_pitches = 100
130
- pitch_stats.write_csv('pitch_leaderboard.csv')
131
- pitch_leaderboard_df = gr.State(filter_pitch_leaderboard(init_min_pitches))
132
-
133
- min_pitches = gr.Number(init_min_pitches, precision=0, label='Min. Pitches')
134
- pitch_leaderboard_download_file = gr.DownloadButton(value='pitch_leaderboard.csv', label='Download leaderboard')
135
- pitch_leaderboard = gr.Dataframe(value=pitch_leaderboard_df.value)
136
-
137
- min_pitches.change(filter_pitch_leaderboard, inputs=min_pitches, outputs=pitch_leaderboard_df)
138
- (
139
- pitch_leaderboard_df
140
- .change(create_set_download_file_fn('files/pitch_leaderboard.csv'), inputs=pitch_leaderboard_df, outputs=pitch_leaderboard_download_file)
141
- .then(lambda df: df, inputs=pitch_leaderboard_df, outputs=pitch_leaderboard)
142
- )
143
-
144
- demo.launch(
145
- share=True,
146
- debug=True
147
- )