Multichem commited on
Commit
aad53df
·
verified ·
1 Parent(s): 404a89f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -41,6 +41,7 @@ def init_baselines():
41
  load_display.replace('', np.nan, inplace=True)
42
  load_display = load_display.dropna(subset=['PA'])
43
  load_display = load_display.drop_duplicates(subset=['Player'], keep='first')
 
44
 
45
  hitter_rhp = load_display.copy()
46
 
@@ -51,6 +52,7 @@ def init_baselines():
51
  load_display.replace('', np.nan, inplace=True)
52
  load_display = load_display.dropna(subset=['PA'])
53
  load_display = load_display.drop_duplicates(subset=['Player'], keep='first')
 
54
 
55
  hitter_lhp = load_display.copy()
56
 
@@ -60,6 +62,7 @@ def init_baselines():
60
  load_display = pd.DataFrame(worksheet.get_all_records())
61
  load_display.replace('', np.nan, inplace=True)
62
  load_display = load_display.dropna(subset=['True AVG'])
 
63
 
64
  pitcher_rhh = load_display.copy()
65
 
@@ -69,17 +72,28 @@ def init_baselines():
69
  load_display = pd.DataFrame(worksheet.get_all_records())
70
  load_display.replace('', np.nan, inplace=True)
71
  load_display = load_display.dropna(subset=['True AVG'])
 
72
 
73
  pitcher_lhh = load_display.copy()
 
 
 
 
 
 
 
 
 
 
74
 
75
- return hitter_rhp, hitter_lhp, pitcher_rhh, pitcher_lhh
76
 
77
  @st.cache_resource
78
- def calc_poisson(hitter_val, sp_val, sp_count, bp_count):
79
  base_val = hitter_val
80
  opp_val = sp_val
81
  sp_combo_val = sum([base_val, opp_val]) / 2
82
- bp_combo_val = sum([base_val, .085]) / 2
83
  sp_instances = sp_count
84
  bp_instances = bp_count
85
  sp_mean = sp_combo_val * sp_instances
@@ -97,14 +111,14 @@ def calc_poisson(hitter_val, sp_val, sp_count, bp_count):
97
 
98
  return sp_outcome, bp_outcome, mean_outcome
99
 
100
- hitter_rhp, hitter_lhp, pitcher_rhh, pitcher_lhh = init_baselines()
101
 
102
  col1, col2 = st.columns([1, 7])
103
 
104
  with col1:
105
  if st.button("Load/Reset Data", key='reset1'):
106
  st.cache_data.clear()
107
- hitter_rhp, hitter_lhp, pitcher_rhh, pitcher_lhh = init_baselines()
108
 
109
  pitcher_var1 = st.selectbox("Which pitcher are you looking at?", options = pitcher_rhh['Names'].unique())
110
  working_pitcher = pitcher_rhh.copy()
@@ -118,13 +132,27 @@ with col1:
118
  hitter_var1 = st.selectbox("What hitter are you looking at?", options = hitter_lhp['Player'].unique())
119
  working_hitters = hitter_lhp.copy()
120
  hitter_check = working_hitters[working_hitters['Player'] == hitter_var1]
 
 
 
121
  sp_count = st.number_input("How many PA against the Pitcher?")
122
  bp_count = st.number_input("How many PA against the Bullpen?")
 
123
 
124
  with col2:
125
  if st.button('calculate theoretical means'):
126
- hitter_val = hitter_check['BB%'].iloc[0]
127
- sp_val = pitcher_check['BB%'].iloc[0]
 
 
 
 
 
 
 
 
 
 
128
  value = calc_poisson(hitter_val, sp_val, sp_count, bp_count)
129
  st.write(f"Theoretical mean of the SP instances: {value[0]}")
130
  st.write(f"Theoretical mean of the BP instances: {value[1]}")
 
41
  load_display.replace('', np.nan, inplace=True)
42
  load_display = load_display.dropna(subset=['PA'])
43
  load_display = load_display.drop_duplicates(subset=['Player'], keep='first')
44
+ load_display= load_display.sort_values(by='Player', ascending=False)
45
 
46
  hitter_rhp = load_display.copy()
47
 
 
52
  load_display.replace('', np.nan, inplace=True)
53
  load_display = load_display.dropna(subset=['PA'])
54
  load_display = load_display.drop_duplicates(subset=['Player'], keep='first')
55
+ load_display= load_display.sort_values(by='Player', ascending=False)
56
 
57
  hitter_lhp = load_display.copy()
58
 
 
62
  load_display = pd.DataFrame(worksheet.get_all_records())
63
  load_display.replace('', np.nan, inplace=True)
64
  load_display = load_display.dropna(subset=['True AVG'])
65
+ load_display= load_display.sort_values(by='Names', ascending=False)
66
 
67
  pitcher_rhh = load_display.copy()
68
 
 
72
  load_display = pd.DataFrame(worksheet.get_all_records())
73
  load_display.replace('', np.nan, inplace=True)
74
  load_display = load_display.dropna(subset=['True AVG'])
75
+ load_display= load_display.sort_values(by='Names', ascending=False)
76
 
77
  pitcher_lhh = load_display.copy()
78
+
79
+ time.sleep(.5)
80
+
81
+ worksheet = sh.worksheet('Bullpen_xData')
82
+ load_display = pd.DataFrame(worksheet.get_all_records())
83
+ load_display.replace('', np.nan, inplace=True)
84
+ load_display = load_display.dropna(subset=['HWS Ratio'])
85
+ load_display= load_display.sort_values(by='Names', ascending=False)
86
+
87
+ bullpen_data = load_display.copy()
88
 
89
+ return hitter_rhp, hitter_lhp, pitcher_rhh, pitcher_lhh, bullpen_data
90
 
91
  @st.cache_resource
92
+ def calc_poisson(hitter_val, sp_val, bp_val, sp_count, bp_count):
93
  base_val = hitter_val
94
  opp_val = sp_val
95
  sp_combo_val = sum([base_val, opp_val]) / 2
96
+ bp_combo_val = sum([base_val, bp_val]) / 2
97
  sp_instances = sp_count
98
  bp_instances = bp_count
99
  sp_mean = sp_combo_val * sp_instances
 
111
 
112
  return sp_outcome, bp_outcome, mean_outcome
113
 
114
+ hitter_rhp, hitter_lhp, pitcher_rhh, pitcher_lhh, bullpen_data = init_baselines()
115
 
116
  col1, col2 = st.columns([1, 7])
117
 
118
  with col1:
119
  if st.button("Load/Reset Data", key='reset1'):
120
  st.cache_data.clear()
121
+ hitter_rhp, hitter_lhp, pitcher_rhh, pitcher_lhh, bullpen_data = init_baselines()
122
 
123
  pitcher_var1 = st.selectbox("Which pitcher are you looking at?", options = pitcher_rhh['Names'].unique())
124
  working_pitcher = pitcher_rhh.copy()
 
132
  hitter_var1 = st.selectbox("What hitter are you looking at?", options = hitter_lhp['Player'].unique())
133
  working_hitters = hitter_lhp.copy()
134
  hitter_check = working_hitters[working_hitters['Player'] == hitter_var1]
135
+ bullpen_var1 = st.selectbox("Which Bullpen are you looking at?", options = bullpen_data['Names'].unique())
136
+ working_bullpen = bullpen_data.copy()
137
+ bullpen_check = working_bullpen[working_bullpen['Names'] == bullpen_var1]
138
  sp_count = st.number_input("How many PA against the Pitcher?")
139
  bp_count = st.number_input("How many PA against the Bullpen?")
140
+ stat_var1 = st.selectbox("What Stat are you looking at?", options = ['Projected Walks', 'Projected Strikeouts', 'Projected HRs'])
141
 
142
  with col2:
143
  if st.button('calculate theoretical means'):
144
+ if stat_var1 == 'Projected Walks':
145
+ hitter_val = hitter_check['BB%'].iloc[0]
146
+ sp_val = pitcher_check['BB%'].iloc[0]
147
+ bp_val = bullpen_check['Walkper'].iloc[0] / 100
148
+ elif stat_var1 == 'Projected Strikeouts':
149
+ hitter_val = hitter_check['K%'].iloc[0]
150
+ sp_val = pitcher_check['K%'].iloc[0]
151
+ bp_val = bullpen_check['Strikeoutper'].iloc[0] / 100
152
+ elif stat_var1 == 'Projected HRs':
153
+ hitter_val = hitter_check['xHRs'].iloc[0] / hitter_check['PA'].iloc[0]
154
+ sp_val = pitcher_check['xHRs'].iloc[0] / pitcher_check['PA'].iloc[0]
155
+ bp_val = bullpen_check['Homeruns'].iloc[0] / bullpen_check['PA'].iloc[0]
156
  value = calc_poisson(hitter_val, sp_val, sp_count, bp_count)
157
  st.write(f"Theoretical mean of the SP instances: {value[0]}")
158
  st.write(f"Theoretical mean of the BP instances: {value[1]}")