patrickramos commited on
Commit
a287b0b
·
1 Parent(s): 0bbd409

Add Secondary%

Browse files
Files changed (4) hide show
  1. convert.py +67 -4
  2. data.py +6 -1
  3. player_team_leaderboard.py +1 -1
  4. stats.py +2 -1
convert.py CHANGED
@@ -1,3 +1,11 @@
 
 
 
 
 
 
 
 
1
  aux_global_id_to_code = {
2
  7911: 'G',
3
  7912: 'S',
@@ -119,6 +127,60 @@ general_ball_kind_code = {
119
  52: 'VS'
120
  }
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  lr = {1: 'l', 2: 'r'}
123
 
124
  presult = {
@@ -161,10 +223,11 @@ presult = {
161
  141: 'Unknown'
162
  }
163
 
164
- def verify_and_return_presult(presults):
165
- for _presult in presults:
166
- assert _presult in presult.values(), f'{_presult} is invalid'
167
- return presults
 
168
 
169
  bresult = {
170
  0: '空振り三振',
 
1
+ def create_verify_and_return_fn(source):
2
+ def _verify_and_return(inputs):
3
+ for x in inputs:
4
+ assert x in source, f'{x} is invalid'
5
+ return inputs
6
+ return _verify_and_return
7
+
8
+
9
  aux_global_id_to_code = {
10
  7911: 'G',
11
  7912: 'S',
 
127
  52: 'VS'
128
  }
129
 
130
+ basic_ball_kind = {
131
+ -1: '-',
132
+ 31: 'Fastball',
133
+ 32: 'Breaking',
134
+ 33: 'Breaking',
135
+ 34: 'Breaking',
136
+ 35: 'Breaking',
137
+ 36: 'Breaking',
138
+ 37: 'Breaking',
139
+ 38: 'Breaking',
140
+ 39: 'Off-speed',
141
+ 40: 'Off-speed',
142
+ 41: 'Off-speed',
143
+ 42: 'Fastball',
144
+ 43: 'Off-speed',
145
+ 44: 'Off-speed',
146
+ 45: 'Breaking',
147
+ 46: 'Fastball',
148
+ 47: 'Fastball',
149
+ 48: 'Fastball',
150
+ 49: 'Fastball',
151
+ 50: 'Other', # technically "super" eephus but I haven't encountered a normal one yet
152
+ 51: 'Fastball',
153
+ 52: 'Breaking',
154
+ }
155
+
156
+ basic_ball_kind_code = {
157
+ -1: '-',
158
+ 31: 'FB',
159
+ 32: 'BR',
160
+ 33: 'BR',
161
+ 34: 'BR',
162
+ 35: 'BR',
163
+ 36: 'BR',
164
+ 37: 'BR',
165
+ 38: 'BR',
166
+ 39: 'OS',
167
+ 40: 'OS',
168
+ 41: 'OS',
169
+ 42: 'FB',
170
+ 43: 'OS',
171
+ 44: 'OS',
172
+ 45: 'BR',
173
+ 46: 'FB',
174
+ 47: 'FB',
175
+ 48: 'FB',
176
+ 49: 'FB',
177
+ 50: 'Other', # technically "super" eephus but I haven't encountered a normal one yet
178
+ 51: 'FB',
179
+ 52: 'BR',
180
+ }
181
+
182
+ verify_and_return_basic_ball_kind_code = create_verify_and_return_fn(basic_ball_kind_code.values())
183
+
184
  lr = {1: 'l', 2: 'r'}
185
 
186
  presult = {
 
223
  141: 'Unknown'
224
  }
225
 
226
+ verify_and_return_presult = create_verify_and_return_fn(presult.values())
227
+ # def verify_and_return_presult(presults):
228
+ # for _presult in presults:
229
+ # assert _presult in presult.values(), f'{_presult} is invalid'
230
+ # return presults
231
 
232
  bresult = {
233
  0: '空振り三振',
data.py CHANGED
@@ -11,7 +11,10 @@ from itertools import product
11
  from convert import (
12
  aux_global_id_to_code, presult,
13
  team_name_short,
14
- ball_kind, ball_kind_code, general_ball_kind, general_ball_kind_code, lr,
 
 
 
15
  game_kind
16
  )
17
 
@@ -188,6 +191,8 @@ data_df = (
188
  pl.col('ballKind').replace_strict(ball_kind_code).alias('ballKind_code'),
189
  pl.col('ballKind').replace_strict(general_ball_kind).alias('general_ballKind'),
190
  pl.col('ballKind').replace_strict(general_ball_kind_code).alias('general_ballKind_code'),
 
 
191
  pl.col('batLR').replace_strict(lr),
192
  pl.col('pitLR').replace_strict(lr),
193
  pl.col('date').str.to_date('%Y%m%d'),
 
11
  from convert import (
12
  aux_global_id_to_code, presult,
13
  team_name_short,
14
+ ball_kind, ball_kind_code,
15
+ general_ball_kind, general_ball_kind_code,
16
+ basic_ball_kind, basic_ball_kind_code,
17
+ lr,
18
  game_kind
19
  )
20
 
 
191
  pl.col('ballKind').replace_strict(ball_kind_code).alias('ballKind_code'),
192
  pl.col('ballKind').replace_strict(general_ball_kind).alias('general_ballKind'),
193
  pl.col('ballKind').replace_strict(general_ball_kind_code).alias('general_ballKind_code'),
194
+ pl.col('ballKind').replace_strict(basic_ball_kind).alias('basic_ballKind'),
195
+ pl.col('ballKind').replace_strict(basic_ball_kind_code).alias('basic_ballKind_code'),
196
  pl.col('batLR').replace_strict(lr),
197
  pl.col('pitLR').replace_strict(lr),
198
  pl.col('date').str.to_date('%Y%m%d'),
player_team_leaderboard.py CHANGED
@@ -38,7 +38,7 @@ def create_player_team_leaderboard_app(player_team_type):
38
  theme_to_cols = {
39
  'Plate Discipline': ['Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Con%', 'O-Con%', 'SwStr%', 'Whiff%', 'CSW%', 'Strike%', 'Ball%', 'F-Str%', 'PAR%', 'PLUS%'],
40
  'Batted Ball': ['GB%', 'FB%', 'LD%', 'OFFB%', 'IFFB%', 'AIR%'],
41
- 'Approach': ['Zone%', 'Arm%', 'Glove%', 'High%', 'Low%', 'MM%', 'Behind%']
42
  }
43
  all_cols = prefix_cols + sum(list(theme_to_cols.values()), [])
44
  for theme, cols in theme_to_cols.items():
 
38
  theme_to_cols = {
39
  'Plate Discipline': ['Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Con%', 'O-Con%', 'SwStr%', 'Whiff%', 'CSW%', 'Strike%', 'Ball%', 'F-Str%', 'PAR%', 'PLUS%'],
40
  'Batted Ball': ['GB%', 'FB%', 'LD%', 'OFFB%', 'IFFB%', 'AIR%'],
41
+ 'Approach': ['Zone%', 'Arm%', 'Glove%', 'High%', 'Low%', 'MM%', 'Behind%', 'Sec%']
42
  }
43
  all_cols = prefix_cols + sum(list(theme_to_cols.values()), [])
44
  for theme, cols in theme_to_cols.items():
stats.py CHANGED
@@ -4,7 +4,7 @@ from data import data_df
4
  # from enum import Enum
5
  from types import SimpleNamespace
6
 
7
- from convert import verify_and_return_presult
8
 
9
  class Player:
10
  PITCHER = 'pitcher'
@@ -69,6 +69,7 @@ register_stat('Arm%', (pl.when(pl.col('pitLR') == 'r').then(pl.col('x') >= 0).ot
69
  register_stat('High%', (pl.col('y') > 125).mean(), True, None)
70
  register_stat('Low%', (pl.col('y') <= 125).mean(), True, None)
71
  register_stat('MM%', (pl.col('x').is_between(-20, 20) & pl.col('y').is_between(100, 100+50)).mean(), True, None)
 
72
  register_stat('GB%', pl.col('G') + pl.col('B'), True, Player.PITCHER, True)
73
  register_stat('FB%', pl.col('F') + pl.col('P'), True, Player.BATTER, True)
74
  register_stat('LD%', pl.col('L'), True, Player.BATTER, True)
 
4
  # from enum import Enum
5
  from types import SimpleNamespace
6
 
7
+ from convert import verify_and_return_presult, verify_and_return_basic_ball_kind_code
8
 
9
  class Player:
10
  PITCHER = 'pitcher'
 
69
  register_stat('High%', (pl.col('y') > 125).mean(), True, None)
70
  register_stat('Low%', (pl.col('y') <= 125).mean(), True, None)
71
  register_stat('MM%', (pl.col('x').is_between(-20, 20) & pl.col('y').is_between(100, 100+50)).mean(), True, None)
72
+ register_stat('Sec%', (pl.col('basic_ballKind_code').is_in(verify_and_return_basic_ball_kind_code(['BR', 'OS']))).sum() / pl.col('pitch').sum(), True, None)
73
  register_stat('GB%', pl.col('G') + pl.col('B'), True, Player.PITCHER, True)
74
  register_stat('FB%', pl.col('F') + pl.col('P'), True, Player.BATTER, True)
75
  register_stat('LD%', pl.col('L'), True, Player.BATTER, True)