-
Notifications
You must be signed in to change notification settings - Fork 1
/
baseballwalkirk.py
314 lines (257 loc) · 12.1 KB
/
baseballwalkirk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import sys
import pybaseball
import pandas as pd
import pprint
import numpy as np
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel
from PyQt5.QtGui import QColor, QFont
from PyQt5.QtCore import Qt
###### CONSTANTS #####
MATRIX_ROWS = 12
MATRIX_COLS = 19
def separate_name(player_name):
# Split the full name into first name and last name
names = player_name.split()
if len(names) == 1:
# Only one name provided (e.g., aaron), consider it as the first name
first_name = names[0]
last_name = ""
else:
# First name is the first word, last name is the last word
first_name = names[0]
last_name = names[-1]
return names
def get_player_id(player_name):
name = separate_name(player_name)
# Use playerid_lookup to retrieve the player ID
player_id = pybaseball.playerid_lookup(name[-1], name[0])
if player_id.empty:
print(f"Player ID not found for: {player_name}")
sys.exit()
return player_id['key_mlbam'].values[0]
def lookup_player_statcast(player_name):
# Get the player ID
player_id = get_player_id(player_name)
# Fetch statcast data for the specified player with one ball and one strike
statcast_data = pybaseball.statcast_batter('2019-01-01','2023-06-21',player_id = player_id)
# Check if no data found for the player and exit if so
if statcast_data.empty:
print(f"No stats found for player: {player_name}")
sys.exit()
return statcast_data
def lookup_batting_stats(player_name):
season = 2023
pybaseball.cache.enable()
# Fetch batting stats for the specified season with a minimum qualifying threshold
batting = pybaseball.batting_stats(season, qual=1)
# Find the row corresponding to the specified player name
row_data = batting[batting['Name'] == player_name]
# Check if no stats found for the player and return None if so
if row_data.empty:
print(f"No batting stats found for player: {player_name}")
return None
# Create a dictionary to store player batting stats
stats_dict = {}
for column_name, value in row_data.iloc[0].iteritems():
stats_dict[column_name] = value
return stats_dict
def lookup_pitching_stats(player_name):
season = 2023
pybaseball.cache.enable()
# Fetch pitching stats for the specified season
pitching = pybaseball.pitching_stats(season)
# Find the row corresponding to the specified player name
row_data = pitching[pitching['Name'] == player_name]
# Check if no stats found for the player and return None if so
if row_data.empty:
print(f"No pitching stats found for player: {player_name}")
return None
# Create a dictionary to store player pitching stats
stats_dict = {}
for column_name, value in row_data.iloc[0].iteritems():
stats_dict[column_name] = value
return stats_dict
def display_matrix(matrix, player_name):
num_rows, num_cols = len(matrix), len(matrix[0])
cell_width = 40
# Define row and column labels
row_labels = ['0-0', '0-1', '1-0', '0-2', '1-1', '2-0', '1-2', '2-1', '3-0', '2-2', '3-1', '3-2']
col_labels = row_labels + ['1B', '2B', '3B', 'HR', 'BIP', 'BB', 'K']
# Create the Qt application
app = QApplication(sys.argv)
# Create the main window
window = QMainWindow()
window.setWindowTitle("Matrix Display for " + player_name)
dark_gray = QColor(40, 40, 40)
window.setStyleSheet(f"background-color: {dark_gray.name()};")
# Create a central widget to hold the layout
central_widget = QWidget()
window.setCentralWidget(central_widget)
# Create a vertical layout for the central widget
layout = QVBoxLayout()
central_widget.setLayout(layout)
# Create the table to display the matrix
for i in range(num_rows + 1):
row_layout = QHBoxLayout()
for j in range(num_cols + 1):
if i == 0 and j == 0:
# Top-left cell: empty label
label = QLabel()
elif i == 0:
# Column labels
label = QLabel(col_labels[j - 1])
elif j == 0:
# Row labels
label = QLabel(row_labels[i - 1])
else:
# Matrix values
val = matrix[i - 1][j - 1]
g = int(val * 400) # Adjust the range of green component for a more drastic change
color = QColor(0, g, 0) # Adjusted green color
text_color = QColor(255, 255, 255) if val != 1 else QColor(0, 0, 0) # White text for non-zero values, black for zeros
label = QLabel(f"{val:.3f}")
# Set the background and text color
label.setAutoFillBackground(True)
label.setStyleSheet(f"background-color: {color.name()}; color: {text_color.name()};")
label.setAlignment(Qt.AlignCenter) # Center the label text
label.setFixedSize(cell_width, cell_width)
row_layout.addWidget(label)
layout.addLayout(row_layout)
# Show the window
window.show()
# Start the Qt event loop
sys.exit(app.exec_())
def construct_transition_matrix(player_name):
player_stats = lookup_player_statcast(player_name)
selected_columns = ['game_date', 'balls', 'strikes','at_bat_number', "pitch_number",'events','description', 'game_type']
parsed_data = player_stats[selected_columns]
#print(parsed_data)
matrix = np.ones((MATRIX_ROWS, MATRIX_COLS))
for i in range(4):
for j in range(3):
filtered_data = parsed_data.loc[(parsed_data['balls'] == i) & (parsed_data['strikes'] == j) & (parsed_data["game_type"] == 'R')]
row_count = len(filtered_data)
counts = filtered_data['description'].value_counts()
ball = counts.get('ball', 0) + counts.get('blocked_ball', 0)
strike = counts.get('called_strike', 0) + counts.get('swinging_strike', 0) + counts.get('swinging_strike_blocked', 0)
foul = counts.get('foul', 0) + counts.get('foul_tip', 0)
counts = filtered_data['events'].value_counts()
single = counts.get('single', 0)
double = counts.get('double', 0)
triple = counts.get('triple', 0)
homer = counts.get('home_run', 0)
BIP = row_count - single - double - triple - homer - ball - strike - foul
print("Ball:", ball)
print("Strike:", strike)
print("Foul:", foul)
print("BIP:", BIP)
print("Single:", single)
print("Double:", double)
print("Triple:", triple)
print("Home Run:", homer)
print("Total:", row_count)
if i == 0:
if j == 0:
matrix[0][1] = (strike + foul) / row_count
matrix[0][2] = ball / row_count
matrix[0][12] = single / row_count
matrix[0][13] = double/ row_count
matrix[0][14] = triple/ row_count
matrix[0][15] = homer/ row_count
matrix[0][16] = BIP / row_count
elif j == 1:
matrix[1][3] = (strike + foul) / row_count
matrix[1][4] = ball / row_count
matrix[1][12] = single / row_count
matrix[1][13] = double/ row_count
matrix[1][14] = triple/ row_count
matrix[1][15] = homer/ row_count
matrix[1][16] = BIP / row_count
elif j == 2:
matrix[3][18] = strike / row_count
matrix[3][3] = foul / row_count
matrix[3][6] = ball / row_count
matrix[3][12] = single / row_count
matrix[3][13] = double/ row_count
matrix[3][14] = triple/ row_count
matrix[3][15] = homer/ row_count
matrix[3][16] = BIP / row_count
elif i == 1:
if j == 0:
matrix[2][4] = (strike + foul) / row_count
matrix[2][5] = ball / row_count
matrix[2][12] = single / row_count
matrix[2][13] = double/ row_count
matrix[2][14] = triple/ row_count
matrix[2][15] = homer/ row_count
matrix[2][16] = BIP / row_count
elif j == 1:
matrix[4][6] = (strike + foul) / row_count
matrix[4][7] = ball / row_count
matrix[4][12] = single / row_count
matrix[4][13] = double/ row_count
matrix[4][14] = triple/ row_count
matrix[4][15] = homer/ row_count
matrix[4][16] = BIP / row_count
elif j == 2:
matrix[6][18] = strike / row_count
matrix[6][6] = foul / row_count
matrix[6][9] = ball / row_count
matrix[6][12] = single / row_count
matrix[6][13] = double/ row_count
matrix[6][14] = triple/ row_count
matrix[6][15] = homer/ row_count
matrix[6][16] = BIP / row_count
elif i == 2:
if j == 0:
matrix[5][7] = (strike + foul) / row_count
matrix[5][8] = ball / row_count
matrix[5][12] = single / row_count
matrix[5][13] = double/ row_count
matrix[5][14] = triple/ row_count
matrix[5][15] = homer/ row_count
matrix[5][16] = BIP / row_count
elif j == 1:
matrix[7][9] = (strike + foul) / row_count
matrix[7][10] = ball / row_count
matrix[7][12] = single / row_count
matrix[7][13] = double/ row_count
matrix[7][14] = triple/ row_count
matrix[7][15] = homer/ row_count
matrix[7][16] = BIP / row_count
elif j == 2:
matrix[9][18] = strike / row_count
matrix[9][9] = foul / row_count
matrix[9][11] = ball / row_count
matrix[9][12] = single / row_count
matrix[9][13] = double/ row_count
matrix[9][14] = triple/ row_count
matrix[9][15] = homer/ row_count
matrix[9][16] = BIP / row_count
elif i == 3:
if j == 0:
matrix[8][10] = (strike + foul) / row_count
matrix[8][17] = ball / row_count
matrix[8][12] = single / row_count
matrix[8][13] = double/ row_count
matrix[8][14] = triple/ row_count
matrix[8][15] = homer/ row_count
matrix[8][16] = BIP / row_count
elif j == 1:
matrix[10][11] = (strike + foul) / row_count
matrix[10][17] = ball / row_count
matrix[10][12] = single / row_count
matrix[10][13] = double/ row_count
matrix[10][14] = triple/ row_count
matrix[10][15] = homer/ row_count
matrix[10][16] = BIP / row_count
elif j == 2:
matrix[11][18] = strike / row_count
matrix[11][11] = foul / row_count
matrix[11][17] = ball / row_count
matrix[11][12] = single / row_count
matrix[11][13] = double/ row_count
matrix[11][14] = triple/ row_count
matrix[11][15] = homer/ row_count
matrix[11][16] = BIP / row_count
return matrix