-
Notifications
You must be signed in to change notification settings - Fork 1
/
atbat.py
106 lines (69 loc) · 2.71 KB
/
atbat.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
import baseballwalkirk as bb
import os
import sys
import pybaseball
import pandas as pd
import pprint
import numpy as np
import random as rand
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel
from PyQt5.QtGui import QColor, QFont
from PyQt5.QtCore import Qt
#returns a list of values corresponding to events during a predicted at-bat
def at_bat(hitter_matrix, pitcher_matrix):
current_state = 0
#12 or greater refers to the first state where an atbat ends in an event
while current_state < 12:
random_float = rand.random()
print("*****finding next state*****")
total = 0
for i in range(len(hitter_matrix[current_state])):
if not hitter_matrix[current_state][i] == 1:
if random_float > total and random_float < total+hitter_matrix[current_state][i]:
current_state = i
print(current_state)
break
else:
total += hitter_matrix[current_state][i]
def write_matrix_to_file(matrix, foldername, filename):
try:
# Create the folder if it doesn't exist
if not os.path.exists(foldername):
os.makedirs(foldername)
filepath = os.path.join(foldername, filename)
with open(filepath, 'w') as file:
for row in matrix:
row_str = ' '.join(str(element) for element in row)
file.write(row_str + '\n')
print(f"Matrix successfully written to {filepath}")
except Exception as e:
print(f"Error writing to {filepath}: {e}")
def read_matrix_from_file(filepath):
try:
with open(filepath, 'r') as file:
lines = file.readlines()
matrix = [list(map(float, line.strip().split())) for line in lines]
return matrix
except Exception as e:
print(f"Error reading from {filepath}: {e}")
return None
if __name__ == "__main__":
#player_name = 'joey wiemer'
player_name = 'shohei ohtani'
#player_name = 'giancarlo stanton'
"""# Lookup batting stats for the specified player
batting_stats = lookup_batting_stats(player_name)
if batting_stats:
pprint.pprint(batting_stats)
# Lookup pitching stats for the specified player
pitching_stats = lookup_pitching_stats(player_name)
if pitching_stats:
pprint.pprint(pitching_stats)
player_stats = lookup_player_statcast(player_name)
print(player_stats)"""
pd.set_option('display.max_rows', None)
filepath = os.path.join('hitter_stats', player_name)
matrix = read_matrix_from_file(filepath)
at_bat(matrix, matrix)
bb.display_matrix(matrix, player_name)
sys.exit(0)