Spaces:
Sleeping
Sleeping
HatmanStack commited on
Commit ·
0b2e2a2
1
Parent(s): 75e1132
fix(logging): replace f-string logging with lazy %s formatting
Browse files- pages/2_play_game.py +3 -3
- scripts/compile_model.py +9 -9
- src/database/queries.py +2 -2
- src/ml/model.py +4 -4
- src/state/session.py +1 -1
pages/2_play_game.py
CHANGED
|
@@ -178,17 +178,17 @@ if teams_good and not st.session_state.away_team_df.empty:
|
|
| 178 |
index=["Home Team", "Away Team"],
|
| 179 |
)
|
| 180 |
|
| 181 |
-
logger.info(
|
| 182 |
|
| 183 |
except ModelLoadError as e:
|
| 184 |
st.error("Could not load prediction model. Please contact support.")
|
| 185 |
-
logger.error(
|
| 186 |
teams_good = False
|
| 187 |
winner_label = ""
|
| 188 |
box_score = pd.DataFrame()
|
| 189 |
except ValueError as e:
|
| 190 |
st.error("Error processing team stats. Please try again.")
|
| 191 |
-
logger.error(
|
| 192 |
teams_good = False
|
| 193 |
winner_label = ""
|
| 194 |
box_score = pd.DataFrame()
|
|
|
|
| 178 |
index=["Home Team", "Away Team"],
|
| 179 |
)
|
| 180 |
|
| 181 |
+
logger.info("Prediction: %.4f", probability)
|
| 182 |
|
| 183 |
except ModelLoadError as e:
|
| 184 |
st.error("Could not load prediction model. Please contact support.")
|
| 185 |
+
logger.error("Model load error: %s", e)
|
| 186 |
teams_good = False
|
| 187 |
winner_label = ""
|
| 188 |
box_score = pd.DataFrame()
|
| 189 |
except ValueError as e:
|
| 190 |
st.error("Error processing team stats. Please try again.")
|
| 191 |
+
logger.error("Stats processing error: %s", e)
|
| 192 |
teams_good = False
|
| 193 |
winner_label = ""
|
| 194 |
box_score = pd.DataFrame()
|
scripts/compile_model.py
CHANGED
|
@@ -172,7 +172,7 @@ def train_model(
|
|
| 172 |
"init": INITIALIZERS,
|
| 173 |
}
|
| 174 |
|
| 175 |
-
logger.info(
|
| 176 |
|
| 177 |
random_search = RandomizedSearchCV(
|
| 178 |
estimator=model,
|
|
@@ -195,17 +195,17 @@ def main() -> None:
|
|
| 195 |
logger.info("Loading data files")
|
| 196 |
|
| 197 |
if not ROSTER_FILE.exists():
|
| 198 |
-
logger.error(
|
| 199 |
raise FileNotFoundError(f"Missing {ROSTER_FILE}")
|
| 200 |
|
| 201 |
if not SCHEDULE_FILE.exists():
|
| 202 |
-
logger.error(
|
| 203 |
raise FileNotFoundError(f"Missing {SCHEDULE_FILE}")
|
| 204 |
|
| 205 |
roster = pd.read_csv(ROSTER_FILE, delimiter=",")
|
| 206 |
schedule = pd.read_csv(SCHEDULE_FILE, delimiter=",")
|
| 207 |
|
| 208 |
-
logger.info(
|
| 209 |
|
| 210 |
# Create target variable: 0 = home wins, 1 = away wins
|
| 211 |
schedule["winner"] = schedule.apply(
|
|
@@ -217,14 +217,14 @@ def main() -> None:
|
|
| 217 |
X = np.array(create_stats(roster, schedule))
|
| 218 |
y = np.array(schedule["winner"])
|
| 219 |
|
| 220 |
-
logger.info(
|
| 221 |
|
| 222 |
# Split data
|
| 223 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 224 |
X, y, test_size=0.2, random_state=42
|
| 225 |
)
|
| 226 |
|
| 227 |
-
logger.info(
|
| 228 |
|
| 229 |
# Train model
|
| 230 |
best_model, best_params, test_accuracy = train_model(
|
|
@@ -232,11 +232,11 @@ def main() -> None:
|
|
| 232 |
)
|
| 233 |
|
| 234 |
# Save model
|
| 235 |
-
logger.info(
|
| 236 |
best_model.save(OUTPUT_MODEL)
|
| 237 |
|
| 238 |
-
logger.info(
|
| 239 |
-
logger.info(
|
| 240 |
|
| 241 |
|
| 242 |
if __name__ == "__main__":
|
|
|
|
| 172 |
"init": INITIALIZERS,
|
| 173 |
}
|
| 174 |
|
| 175 |
+
logger.info("Starting randomized search with %d iterations", n_iterations)
|
| 176 |
|
| 177 |
random_search = RandomizedSearchCV(
|
| 178 |
estimator=model,
|
|
|
|
| 195 |
logger.info("Loading data files")
|
| 196 |
|
| 197 |
if not ROSTER_FILE.exists():
|
| 198 |
+
logger.error("Roster file not found: %s", ROSTER_FILE)
|
| 199 |
raise FileNotFoundError(f"Missing {ROSTER_FILE}")
|
| 200 |
|
| 201 |
if not SCHEDULE_FILE.exists():
|
| 202 |
+
logger.error("Schedule file not found: %s", SCHEDULE_FILE)
|
| 203 |
raise FileNotFoundError(f"Missing {SCHEDULE_FILE}")
|
| 204 |
|
| 205 |
roster = pd.read_csv(ROSTER_FILE, delimiter=",")
|
| 206 |
schedule = pd.read_csv(SCHEDULE_FILE, delimiter=",")
|
| 207 |
|
| 208 |
+
logger.info("Loaded %d players and %d games", len(roster), len(schedule))
|
| 209 |
|
| 210 |
# Create target variable: 0 = home wins, 1 = away wins
|
| 211 |
schedule["winner"] = schedule.apply(
|
|
|
|
| 217 |
X = np.array(create_stats(roster, schedule))
|
| 218 |
y = np.array(schedule["winner"])
|
| 219 |
|
| 220 |
+
logger.info("Feature shape: %s, Target shape: %s", X.shape, y.shape)
|
| 221 |
|
| 222 |
# Split data
|
| 223 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 224 |
X, y, test_size=0.2, random_state=42
|
| 225 |
)
|
| 226 |
|
| 227 |
+
logger.info("Train size: %d, Test size: %d", len(X_train), len(X_test))
|
| 228 |
|
| 229 |
# Train model
|
| 230 |
best_model, best_params, test_accuracy = train_model(
|
|
|
|
| 232 |
)
|
| 233 |
|
| 234 |
# Save model
|
| 235 |
+
logger.info("Saving model to %s", OUTPUT_MODEL)
|
| 236 |
best_model.save(OUTPUT_MODEL)
|
| 237 |
|
| 238 |
+
logger.info("Best parameters: %s", best_params)
|
| 239 |
+
logger.info("Test accuracy: %.4f", test_accuracy)
|
| 240 |
|
| 241 |
|
| 242 |
if __name__ == "__main__":
|
src/database/queries.py
CHANGED
|
@@ -119,11 +119,11 @@ def get_away_team_by_stats(
|
|
| 119 |
|
| 120 |
results = df.loc[list(selected_indices)]
|
| 121 |
if len(results) == 5:
|
| 122 |
-
logger.info(
|
| 123 |
return results
|
| 124 |
|
| 125 |
except ValueError as e:
|
| 126 |
-
logger.debug(
|
| 127 |
continue
|
| 128 |
|
| 129 |
raise QueryExecutionError(
|
|
|
|
| 119 |
|
| 120 |
results = df.loc[list(selected_indices)]
|
| 121 |
if len(results) == 5:
|
| 122 |
+
logger.info("Got away team on attempt %d", attempt + 1)
|
| 123 |
return results
|
| 124 |
|
| 125 |
except ValueError as e:
|
| 126 |
+
logger.debug("Attempt %d failed: %s", attempt + 1, e)
|
| 127 |
continue
|
| 128 |
|
| 129 |
raise QueryExecutionError(
|
src/ml/model.py
CHANGED
|
@@ -32,16 +32,16 @@ def get_winner_model(model_path: str | Path = DEFAULT_MODEL_PATH) -> Model:
|
|
| 32 |
"""
|
| 33 |
path = Path(model_path)
|
| 34 |
if not path.exists():
|
| 35 |
-
logger.error(
|
| 36 |
raise ModelLoadError(f"Model file not found: {path}")
|
| 37 |
|
| 38 |
try:
|
| 39 |
-
logger.info(
|
| 40 |
model = load_model(str(path))
|
| 41 |
logger.info("Model loaded successfully")
|
| 42 |
return model
|
| 43 |
except Exception as e:
|
| 44 |
-
logger.error(
|
| 45 |
raise ModelLoadError(f"Failed to load model: {e}") from e
|
| 46 |
|
| 47 |
|
|
@@ -71,7 +71,7 @@ def predict_winner(combined_stats: np.ndarray) -> tuple[float, int]:
|
|
| 71 |
probability = float(sigmoid_output[0][0])
|
| 72 |
prediction = int(np.round(probability))
|
| 73 |
|
| 74 |
-
logger.info(
|
| 75 |
return probability, prediction
|
| 76 |
|
| 77 |
|
|
|
|
| 32 |
"""
|
| 33 |
path = Path(model_path)
|
| 34 |
if not path.exists():
|
| 35 |
+
logger.error("Model file not found: %s", path)
|
| 36 |
raise ModelLoadError(f"Model file not found: {path}")
|
| 37 |
|
| 38 |
try:
|
| 39 |
+
logger.info("Loading model from %s", path)
|
| 40 |
model = load_model(str(path))
|
| 41 |
logger.info("Model loaded successfully")
|
| 42 |
return model
|
| 43 |
except Exception as e:
|
| 44 |
+
logger.error("Failed to load model: %s", e)
|
| 45 |
raise ModelLoadError(f"Failed to load model: {e}") from e
|
| 46 |
|
| 47 |
|
|
|
|
| 71 |
probability = float(sigmoid_output[0][0])
|
| 72 |
prediction = int(np.round(probability))
|
| 73 |
|
| 74 |
+
logger.info("Prediction: probability=%.4f, winner=%d", probability, prediction)
|
| 75 |
return probability, prediction
|
| 76 |
|
| 77 |
|
src/state/session.py
CHANGED
|
@@ -32,7 +32,7 @@ def init_session_state() -> None:
|
|
| 32 |
for key, default_value in defaults.items():
|
| 33 |
if key not in st.session_state:
|
| 34 |
st.session_state[key] = default_value
|
| 35 |
-
logger.debug(
|
| 36 |
|
| 37 |
|
| 38 |
def get_away_stats() -> list[int]:
|
|
|
|
| 32 |
for key, default_value in defaults.items():
|
| 33 |
if key not in st.session_state:
|
| 34 |
st.session_state[key] = default_value
|
| 35 |
+
logger.debug("Initialized session state: %s", key)
|
| 36 |
|
| 37 |
|
| 38 |
def get_away_stats() -> list[int]:
|