How kych23/ff_predictors works
1. Repository purpose
- FantasyForecast is a Python fantasy-football decision engine with two main products: a preseason draft assistant and an in-season start/sit optimizer.
- The codebase is organized as an ETL + modeling pipeline backed by PostgreSQL, with offline benchmarking and interactive CLI workflows on top.
2. Core data backbone
- src/db/ defines the persistent storage layer: SQLAlchemy models, session management, DB initialization, and upsert/load helpers.
- The database is snapshot-stamped so source data can be reproduced even when upstream NFL data is retroactively corrected.
- scripts/seed_db.py and src/db/init_db.py are the entry points that prepare the warehouse before feature generation and training.
3. Ingestion boundary
- src/ingest/ is the only layer that touches external draft-market and football data sources directly.
- It pulls from nflverse/nflreadpy, Fantasy Football Calculator ADP, and optionally CFBD for rookie college stats.
- This layer also owns ID crosswalking and source normalization (player_ids.py, sources.py, adp.py, college.py), so downstream model code works on unified player keys.
4. Feature engineering subsystem
- src/features/ builds season-level and weekly-level model inputs from historical and contextual signals.
- Key feature groups include prior production, rookies, role change, team context, and weekly matchup context.
- leakage_guard.py is an important boundary: it ensures only strictly prior information is used for a target season or week.
- team_context.py intentionally adds Week-1 betting-line context for team scoring expectations, but this is kept separate from the ADP wall.
5. Label generation and scoring
- src/labels/ turns historical outcomes into supervised targets using fantasy scoring logic.
- Season labels are based on per-game fantasy output, while weekly labels support in-season forecasting and lineup evaluation.
- Scoring rules live in src/labels/scoring.py, keeping fantasy point calculation centralized and consistent across training and evaluation.
6. Season projection model
- src/projection/ contains the main preseason model pipeline.
- It uses a pooled LightGBM quantile model across QB/RB/WR/TE rather than separate per-position models.
- Training and evaluation are split into reusable pieces: datasets, fold construction, model fitting, calibration, and metrics.
- Quantile outputs are rearranged for monotonicity rather than clipped, and calibration is done with split conformal methods by player bucket.
7. Weekly projection model
- src/projection/weekly_* extends the same modeling pattern to in-season weekly forecasting.
- The weekly model uses the season P50 projection as a prior feature, then adjusts for current-week context like opponent and recent form.
- scripts/project_week.py builds the forward-looking weekly dataset, and scripts/train_weekly_projection.py fits the weekly model.
8. Draft recommender and roster state
- src/recommender/ turns model projections into draft-time recommendations.
- It combines marginal value over replacement, expected availability at the user’s next pick, and roster state to compute VONA/FIT rankings.
- draft_state_source.py and roster_state.py keep track of the live draft board and the user’s roster needs, while survival.py models ADP-based pick survival.
- This layer is the only part of the app that is allowed to consume ADP, preserving the model-vs-market separation described in the README.
9. Lineup optimization
- src/lineup/optimizer.py solves the weekly start/sit problem as an exact integer linear program.
- It assigns rostered players to legal starter slots, including FLEX constraints, to maximize the chosen quantile outcome.
- This is the bridge from forecasts to actionable lineup decisions, and it is used by scripts/start.py.
10. Benchmarking and reporting
- src/benchmark/ evaluates both ranking quality and downstream fantasy usefulness.
- It includes season-ranking metrics, calibration checks, draft simulations, weekly bench-point benchmarks, and report generation.
- These modules compare the engine against ADP baselines and provide the artifact outputs described in the README tables.
11. Scripts as orchestration layer
- scripts/ contains the operational entry points for the full lifecycle: ingest, build labels/features, train projections, run benchmarks, draft live, project weekly, and optimize lineups.
- scripts/run_pipeline.sh is the top-level batch workflow for a fresh season refresh.
- scripts/draft.py, scripts/start.py, and related CLI tools expose the interactive user-facing behavior while the src/ package keeps the logic reusable.
12. Configuration and testing
- config/league.yaml holds league-specific scoring and roster assumptions so the same engine can be adapted to different fantasy settings.
- pyproject.toml and requirements.txt define the Python environment around LightGBM, PostgreSQL access, and the data libraries.
- The tests/ directory is broad and architecture-relevant: it validates leakage protection, quantile behavior, optimizer correctness, calibration, crosswalking, weekly assembly, and CLI behavior, which indicates the repo is built around reproducible offline computation rather than a thin web/API layer.
Main entry points: projection Season model · features Features · db DB layer · weekly_train.py Weekly model
Components 18
Scripts
Operational CLI and batch entry points
- Pipeline run · Top-level batch workflow for a fresh season refresh
scripts/run_pipeline.sh - Seed DB · Prepares the warehouse before feature generation and training
scripts/seed_db.py - Build/train · Feature, label, and model training entry points
- Weekly live · Forward weekly projection, benchmarking, and lineup workflows
Core
Reusable engine and data pipeline
- DB layer · SQLAlchemy models, sessions, loaders, and upsert/init helpers
src/db - Ingest · External data acquisition, source normalization, and player ID crosswalks
src/ingest - Features · Season and weekly feature assembly with leakage protection
src/features - Labels · Fantasy scoring and supervised target construction
src/labels - Season model · Preseason pooled quantile model training, calibration, and evaluation
src/projection - Weekly model · In-season weekly forecasting built from season priors and current context
src/projection/weekly_train.py - Draft recommender · Converts projections into draft rankings using roster needs and pick survival
src/recommender - Lineup optimizer · Exact ILP start/sit solver for weekly roster assignment
src/lineup/optimizer.py - Benchmarking · Ranking, calibration, draft simulation, weekly benchmark, and reporting
src/benchmark - League config · League scoring and roster assumptions
src/config/league_config.py
Tests
Architecture-level verification coverage
- Leakage tests · Validates temporal boundaries and leakage guards
tests/test_leakage.py - Model tests · Covers quantiles, calibration, folds, evaluation, and weekly modeling
- Decision tests · Covers replacement, survival, roster state, VONA, and optimizer behavior
- CLI/report tests · Covers report generation, draft layout, weekly benchmark, and start CLI
Connections 31
- Pipeline run → Ingest: ingest · Fresh season refresh starts with external data ingestion
- Pipeline run → DB layer: seed/load · Initializes and populates the warehouse
- Seed DB → DB layer: init · Creates and seeds persistent storage
- Build/train → Features: build · Assembles season features
- Build/train → Labels: label · Builds supervised targets
- Build/train → Season model: train · Fits the preseason projection model
- Weekly live → Weekly model: project · Creates forward-looking weekly forecasts
- Weekly live → Draft recommender: draft · Generates draft-time recommendations
- Weekly live → Lineup optimizer: start/sit · Optimizes weekly lineups
- Ingest → DB layer: write · Loads normalized source data into persistent storage
- Ingest → Features: unify ids · Provides crosswalked player identifiers and normalized sources
- DB layer → Features: read · Feature builders consume stored snapshots
- DB layer → Labels: read · Labels are derived from historical stored outcomes
- Features → Season model: inputs · Season model consumes engineered features
- Labels → Season model: targets · Season model trains on fantasy outcomes
- Season model → Weekly model: prior · Season P50 projection is used as a weekly prior feature
- Features → Weekly model: weekly inputs · Weekly model uses matchup and recent-form features
- Season model → Draft recommender: projections · Draft rankings are driven by season forecasts
- Draft recommender → Lineup optimizer: roster decisions · Projected values inform actionable lineup choices
- Season model → Benchmarking: evaluate · Ranking and calibration checks benchmark the season model
- Weekly model → Benchmarking: evaluate · Weekly benchmarks assess in-season forecasting utility
- League config → Labels: scoring · League settings determine fantasy point calculation
- League config → Lineup optimizer: roster rules · Roster and scoring assumptions constrain optimization
- League config → Draft recommender: league settings · Recommendations depend on league scoring and roster context
- Leakage tests → Features: verify · Tests temporal leakage protections
- Model tests → Season model: verify · Tests quantiles, folds, calibration, and evaluation
- Model tests → Weekly model: verify · Tests weekly model assembly and behavior
- Decision tests → Draft recommender: verify · Tests replacement, survival, roster state, and VONA logic
- Decision tests → Lineup optimizer: verify · Tests ILP lineup optimization correctness
- CLI/report tests → Benchmarking: verify · Tests reporting and benchmark outputs
- CLI/report tests → Weekly live: verify · Tests interactive weekly CLI behavior