James McCool
commited on
Commit
·
8e462c9
1
Parent(s):
aef223f
Enhance exposure_spread function to include sport-specific position eligibility checks, ensuring accurate player replacements based on their eligible positions across various sports.
Browse files- app.py +1 -1
- global_func/exposure_spread.py +187 -3
app.py
CHANGED
@@ -1156,7 +1156,7 @@ with tab2:
|
|
1156 |
submitted = st.form_submit_button("Submit")
|
1157 |
if submitted:
|
1158 |
st.session_state['settings_base'] = False
|
1159 |
-
parsed_frame = exposure_spread(st.session_state['working_frame'], exposure_player, exposure_target, exposure_stack_bool, st.session_state['projections_df'])
|
1160 |
st.session_state['working_frame'] = parsed_frame.reset_index(drop=True)
|
1161 |
st.session_state['export_merge'] = st.session_state['working_frame'].copy()
|
1162 |
with st.container():
|
|
|
1156 |
submitted = st.form_submit_button("Submit")
|
1157 |
if submitted:
|
1158 |
st.session_state['settings_base'] = False
|
1159 |
+
parsed_frame = exposure_spread(st.session_state['working_frame'], exposure_player, exposure_target, exposure_stack_bool, st.session_state['projections_df'], sport_var)
|
1160 |
st.session_state['working_frame'] = parsed_frame.reset_index(drop=True)
|
1161 |
st.session_state['export_merge'] = st.session_state['working_frame'].copy()
|
1162 |
with st.container():
|
global_func/exposure_spread.py
CHANGED
@@ -8,7 +8,186 @@ import math
|
|
8 |
#### Find the player and the amount of rows that contain them and then find an exposure rate which is the percentage of total rows
|
9 |
#### Use the exposure target argument and try to replace the player from as many rows as necessary to be at or just under the target
|
10 |
|
11 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Find comparable players in the projections
|
13 |
comparable_players = projections_df[projections_df['player_names'] == exposure_player]
|
14 |
|
@@ -70,8 +249,13 @@ def exposure_spread(working_frame, exposure_player, exposure_target, exposure_st
|
|
70 |
row_data = working_frame.iloc[row]
|
71 |
for col in working_frame.columns:
|
72 |
if row_data[col] == exposure_player:
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
75 |
change_counter += 1
|
76 |
return working_frame
|
77 |
|
|
|
8 |
#### Find the player and the amount of rows that contain them and then find an exposure rate which is the percentage of total rows
|
9 |
#### Use the exposure target argument and try to replace the player from as many rows as necessary to be at or just under the target
|
10 |
|
11 |
+
def check_nba_position_eligibility(column_name, player_positions):
|
12 |
+
"""
|
13 |
+
Check if a player is eligible for a specific NBA column position.
|
14 |
+
|
15 |
+
Args:
|
16 |
+
column_name (str): The column name (PG, PG1, PG2, SG, SG1, SG2, etc.)
|
17 |
+
player_positions (list): List of positions the player is eligible for
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
bool: True if player is eligible for the column
|
21 |
+
"""
|
22 |
+
if any(pos in column_name for pos in ['PG', 'SG', 'SF', 'PF', 'C']):
|
23 |
+
# Extract the base position from the column name
|
24 |
+
base_position = next(pos for pos in ['PG', 'SG', 'SF', 'PF', 'C'] if pos in column_name)
|
25 |
+
return base_position in player_positions
|
26 |
+
elif 'G' in column_name:
|
27 |
+
return any(pos in ['PG', 'SG'] for pos in player_positions)
|
28 |
+
elif 'F' in column_name:
|
29 |
+
return any(pos in ['SF', 'PF'] for pos in player_positions)
|
30 |
+
elif 'UTIL' in column_name:
|
31 |
+
return True # UTIL can be any position
|
32 |
+
return False
|
33 |
+
|
34 |
+
def check_mlb_position_eligibility(column_name, player_positions):
|
35 |
+
"""
|
36 |
+
Check if a player is eligible for a specific MLB column position.
|
37 |
+
|
38 |
+
Args:
|
39 |
+
column_name (str): The column name (P, SP, RP, C, 1B, 2B, 3B, SS, OF)
|
40 |
+
player_positions (list): List of positions the player is eligible for
|
41 |
+
|
42 |
+
Returns:
|
43 |
+
bool: True if player is eligible for the column
|
44 |
+
"""
|
45 |
+
if any(pos in column_name for pos in ['P', 'SP', 'RP']):
|
46 |
+
return any(pos in ['P', 'SP', 'RP'] for pos in player_positions)
|
47 |
+
elif any(pos in column_name for pos in ['C', '1B', '2B', '3B', 'SS', 'OF']):
|
48 |
+
return any(pos in ['C', '1B', '2B', '3B', 'SS', 'OF'] for pos in player_positions)
|
49 |
+
return False
|
50 |
+
|
51 |
+
def check_nfl_position_eligibility(column_name, player_positions):
|
52 |
+
"""
|
53 |
+
Check if a player is eligible for a specific NFL column position.
|
54 |
+
|
55 |
+
Args:
|
56 |
+
column_name (str): The column name (QB, RB, WR, TE, FLEX, DST)
|
57 |
+
player_positions (list): List of positions the player is eligible for
|
58 |
+
|
59 |
+
Returns:
|
60 |
+
bool: True if player is eligible for the column
|
61 |
+
"""
|
62 |
+
if any(pos in column_name for pos in ['QB', 'RB', 'WR', 'TE', 'DST']):
|
63 |
+
return any(pos in ['QB', 'RB', 'WR', 'TE', 'DST'] for pos in player_positions)
|
64 |
+
elif 'FLEX' in column_name:
|
65 |
+
return any(pos in ['RB', 'WR', 'TE'] for pos in player_positions)
|
66 |
+
elif 'UTIL' in column_name:
|
67 |
+
return any(pos in ['RB', 'WR', 'TE'] for pos in player_positions)
|
68 |
+
return False
|
69 |
+
|
70 |
+
def check_golf_position_eligibility(column_name, player_positions):
|
71 |
+
"""
|
72 |
+
Check if a player is eligible for a specific Golf column position.
|
73 |
+
|
74 |
+
Args:
|
75 |
+
column_name (str): The column name (G)
|
76 |
+
player_positions (list): List of positions the player is eligible for
|
77 |
+
|
78 |
+
Returns:
|
79 |
+
bool: True if player is eligible for the column
|
80 |
+
"""
|
81 |
+
return True
|
82 |
+
|
83 |
+
def check_tennis_position_eligibility(column_name, player_positions):
|
84 |
+
"""
|
85 |
+
Check if a player is eligible for a specific Tennis column position.
|
86 |
+
|
87 |
+
Args:
|
88 |
+
column_name (str): The column name (TEN)
|
89 |
+
player_positions (list): List of positions the player is eligible for
|
90 |
+
|
91 |
+
Returns:
|
92 |
+
bool: True if player is eligible for the column
|
93 |
+
"""
|
94 |
+
return True
|
95 |
+
|
96 |
+
def check_mma_position_eligibility(column_name, player_positions):
|
97 |
+
"""
|
98 |
+
Check if a player is eligible for a specific MMA column position.
|
99 |
+
|
100 |
+
Args:
|
101 |
+
column_name (str): The column name (MMA)
|
102 |
+
player_positions (list): List of positions the player is eligible for
|
103 |
+
|
104 |
+
Returns:
|
105 |
+
bool: True if player is eligible for the column
|
106 |
+
"""
|
107 |
+
return True
|
108 |
+
|
109 |
+
def check_nascar_position_eligibility(column_name, player_positions):
|
110 |
+
"""
|
111 |
+
Check if a player is eligible for a specific NASCAR column position.
|
112 |
+
|
113 |
+
Args:
|
114 |
+
column_name (str): The column name (NAS)
|
115 |
+
player_positions (list): List of positions the player is eligible for
|
116 |
+
|
117 |
+
Returns:
|
118 |
+
bool: True if player is eligible for the column
|
119 |
+
"""
|
120 |
+
return True
|
121 |
+
|
122 |
+
def check_cfb_position_eligibility(column_name, player_positions):
|
123 |
+
"""
|
124 |
+
Check if a player is eligible for a specific CFB column position.
|
125 |
+
|
126 |
+
Args:
|
127 |
+
column_name (str): The column name (QB, RB, WR, TE, FLEX, DST)
|
128 |
+
player_positions (list): List of positions the player is eligible for
|
129 |
+
|
130 |
+
Returns:
|
131 |
+
bool: True if player is eligible for the column
|
132 |
+
"""
|
133 |
+
if any(pos in column_name for pos in ['QB', 'RB', 'WR']):
|
134 |
+
return any(pos in ['QB', 'RB', 'WR'] for pos in player_positions)
|
135 |
+
elif 'FLEX' in column_name:
|
136 |
+
return any(pos in ['RB', 'WR'] for pos in player_positions)
|
137 |
+
elif 'SUPERFLEX' in column_name:
|
138 |
+
return any(pos in ['RB', 'WR', 'QB'] for pos in player_positions)
|
139 |
+
return False
|
140 |
+
|
141 |
+
def check_nhl_position_eligibility(column_name, player_positions):
|
142 |
+
"""
|
143 |
+
Check if a player is eligible for a specific NHL column position.
|
144 |
+
|
145 |
+
Args:
|
146 |
+
column_name (str): The column name (C, LW, RW, D, G, UTIL)
|
147 |
+
player_positions (list): List of positions the player is eligible for
|
148 |
+
|
149 |
+
Returns:
|
150 |
+
bool: True if player is eligible for the column
|
151 |
+
"""
|
152 |
+
if any(pos in column_name for pos in ['C', 'W', 'D', 'G']):
|
153 |
+
return any(pos in ['C', 'W', 'D', 'G'] for pos in player_positions)
|
154 |
+
elif 'FLEX' in column_name:
|
155 |
+
return True # UTIL can be any position
|
156 |
+
elif 'UTIL' in column_name:
|
157 |
+
return True # UTIL can be any position
|
158 |
+
return False
|
159 |
+
|
160 |
+
def check_position_eligibility(sport, column_name, player_positions):
|
161 |
+
"""
|
162 |
+
Main function to check position eligibility based on sport.
|
163 |
+
|
164 |
+
Args:
|
165 |
+
sport (str): The sport (NBA, MLB, NFL, NHL)
|
166 |
+
column_name (str): The column name
|
167 |
+
player_positions (list): List of positions the player is eligible for
|
168 |
+
|
169 |
+
Returns:
|
170 |
+
bool: True if player is eligible for the column
|
171 |
+
"""
|
172 |
+
if sport == 'NBA':
|
173 |
+
return check_nba_position_eligibility(column_name, player_positions)
|
174 |
+
elif sport == 'MLB':
|
175 |
+
return check_mlb_position_eligibility(column_name, player_positions)
|
176 |
+
elif sport == 'NFL':
|
177 |
+
return check_nfl_position_eligibility(column_name, player_positions)
|
178 |
+
elif sport == 'NHL':
|
179 |
+
return check_nhl_position_eligibility(column_name, player_positions)
|
180 |
+
elif sport == 'MMA':
|
181 |
+
return check_cfb_position_eligibility(column_name, player_positions)
|
182 |
+
elif sport == 'Golf':
|
183 |
+
return check_golf_position_eligibility(column_name, player_positions)
|
184 |
+
elif sport == 'Tennis':
|
185 |
+
return check_tennis_position_eligibility(column_name, player_positions)
|
186 |
+
else:
|
187 |
+
# Default fallback - assume exact position match
|
188 |
+
return column_name in player_positions
|
189 |
+
|
190 |
+
def exposure_spread(working_frame, exposure_player, exposure_target, exposure_stack_bool, projections_df, sport_var):
|
191 |
# Find comparable players in the projections
|
192 |
comparable_players = projections_df[projections_df['player_names'] == exposure_player]
|
193 |
|
|
|
249 |
row_data = working_frame.iloc[row]
|
250 |
for col in working_frame.columns:
|
251 |
if row_data[col] == exposure_player:
|
252 |
+
# Get the replacement player's positions
|
253 |
+
replacement_player_positions = projections_df[projections_df['player_names'] == insert_player]['position'].iloc[0].split('/')
|
254 |
+
|
255 |
+
# Check if the replacement player is eligible for this column
|
256 |
+
if check_position_eligibility(sport_var, col, replacement_player_positions):
|
257 |
+
working_frame.at[row, col] = insert_player
|
258 |
+
break
|
259 |
change_counter += 1
|
260 |
return working_frame
|
261 |
|