mirror of
https://github.com/Dasemu/skipredictor.git
synced 2026-11-26 15:26:15 +00:00
94 lines
3.0 KiB
Python
94 lines
3.0 KiB
Python
"""Model retraining script for the ski predictor.
|
|
|
|
Retrains models from the historical featured dataset stored in SQLite and
|
|
evaluates the saved artefacts on a held-out split from the same dataset.
|
|
"""
|
|
|
|
import logging
|
|
import sys
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Ensure the project root is on sys.path so all project packages are importable
|
|
# when this script is executed directly.
|
|
# ---------------------------------------------------------------------------
|
|
_SCRIPT_DIR = Path(__file__).resolve().parent
|
|
_PROJECT_ROOT = _SCRIPT_DIR.parent
|
|
if str(_PROJECT_ROOT) not in sys.path:
|
|
sys.path.insert(0, str(_PROJECT_ROOT))
|
|
|
|
from ml.train_model import ModelTrainer
|
|
from ml.evaluate_model import ModelEvaluator
|
|
from storage import HISTORICAL_FEATURED_TABLE, SQLiteStore
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Logging setup
|
|
# ---------------------------------------------------------------------------
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format="%(asctime)s %(levelname)-8s %(name)s - %(message)s",
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Main entry point
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def main() -> int:
|
|
"""Run the full retraining pipeline.
|
|
|
|
Returns
|
|
-------
|
|
int
|
|
Exit code: 0 on success, 1 on fatal error.
|
|
"""
|
|
logger.info("=== Model retraining started at %s ===", datetime.utcnow().isoformat())
|
|
store = SQLiteStore()
|
|
|
|
try:
|
|
dataset = store.read_dataframe(HISTORICAL_FEATURED_TABLE, parse_dates=("date",))
|
|
except Exception as exc:
|
|
logger.critical("Failed to load dataset from SQLite: %s.", exc, exc_info=True)
|
|
return 1
|
|
|
|
if dataset.empty:
|
|
logger.critical(
|
|
"Dataset table %s is empty. Run scripts/build_full_dataset.py first.",
|
|
HISTORICAL_FEATURED_TABLE,
|
|
)
|
|
return 1
|
|
|
|
logger.info(
|
|
"Loaded %d rows from SQLite table %s.",
|
|
len(dataset),
|
|
HISTORICAL_FEATURED_TABLE,
|
|
)
|
|
|
|
trainer = ModelTrainer(store=store, dataset_table=HISTORICAL_FEATURED_TABLE)
|
|
evaluator = ModelEvaluator(store=store, dataset_table=HISTORICAL_FEATURED_TABLE)
|
|
|
|
try:
|
|
cv_results = trainer.train_all()
|
|
logger.info("Training complete: %s", cv_results)
|
|
except Exception as exc:
|
|
logger.critical("Training failed: %s.", exc, exc_info=True)
|
|
return 1
|
|
|
|
try:
|
|
evaluation = evaluator.evaluate_all()
|
|
logger.info("Evaluation complete: %s", evaluation)
|
|
except Exception as exc:
|
|
# Evaluation failure is non-fatal; models are already saved.
|
|
logger.error("Evaluation failed: %s. Models were still saved.", exc, exc_info=True)
|
|
|
|
logger.info("=== Model retraining finished successfully. ===")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|