# kych23/ff_predictors architecture

> Architecture diagram of the GitHub repository kych23/ff_predictors, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kych23/ff_predictors
- Repository: https://github.com/kych23/ff_predictors

## Overview

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: [`src/projection`](https://github.com/kych23/ff_predictors/tree/main/src/projection) (Season model), [`src/features`](https://github.com/kych23/ff_predictors/tree/main/src/features) (Features), [`src/db`](https://github.com/kych23/ff_predictors/tree/main/src/db) (DB layer), [`src/projection/weekly_train.py`](https://github.com/kych23/ff_predictors/blob/main/src/projection/weekly_train.py) (Weekly model)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_scripts["Scripts"]
  node_scripts_run_pipeline["Pipeline run<br/>orchestration script<br/>[run_pipeline.sh]"]
  node_scripts_seed_db["Seed DB<br/>orchestration script<br/>[seed_db.py]"]
  node_scripts_build_train["Build/train<br/>orchestration scripts"]
  node_scripts_weekly_live["Weekly live<br/>CLI scripts"]
end

subgraph group_grp_src["Core"]
  node_src_db[("DB layer<br/>persistence")]
  node_src_ingest["Ingest<br/>etl boundary"]
  node_src_features["Features<br/>feature engineering"]
  node_src_labels["Labels<br/>target generation"]
  node_src_projection["Season model<br/>forecasting pipeline"]
  node_src_weekly_projection["Weekly model<br/>forecasting pipeline<br/>[weekly_train.py]"]
  node_src_recommender["Draft recommender<br/>decision engine"]
  node_src_lineup["Lineup optimizer<br/>[optimizer.py]"]
  node_src_benchmark["Benchmarking<br/>evaluation"]
  node_src_config["League config<br/>configuration<br/>[league_config.py]"]
end

subgraph group_grp_tests["Tests"]
  node_tests_leakage["Leakage tests<br/>[test_leakage.py]"]
  node_tests_model["Model tests"]
  node_tests_reco_opt["Decision tests"]
  node_tests_cli_report["CLI/report tests"]
end

node_scripts_run_pipeline -->|"ingest"| node_src_ingest
node_scripts_run_pipeline -->|"seed/load"| node_src_db
node_scripts_seed_db -->|"init"| node_src_db
node_scripts_build_train -->|"build"| node_src_features
node_scripts_build_train -->|"label"| node_src_labels
node_scripts_build_train -->|"train"| node_src_projection
node_scripts_weekly_live -->|"project"| node_src_weekly_projection
node_scripts_weekly_live -->|"draft"| node_src_recommender
node_scripts_weekly_live -->|"start/sit"| node_src_lineup
node_src_ingest -->|"write"| node_src_db
node_src_ingest -->|"unify ids"| node_src_features
node_src_db -->|"read"| node_src_features
node_src_db -->|"read"| node_src_labels
node_src_features -->|"inputs"| node_src_projection
node_src_labels -->|"targets"| node_src_projection
node_src_projection -->|"prior"| node_src_weekly_projection
node_src_features -->|"weekly inputs"| node_src_weekly_projection
node_src_projection -->|"projections"| node_src_recommender
node_src_recommender -->|"roster decisions"| node_src_lineup
node_src_projection -->|"evaluate"| node_src_benchmark
node_src_weekly_projection -->|"evaluate"| node_src_benchmark
node_src_config -->|"scoring"| node_src_labels
node_src_config -->|"roster rules"| node_src_lineup
node_src_config -->|"league settings"| node_src_recommender
node_tests_leakage -.->|"verify"| node_src_features
node_tests_model -.->|"verify"| node_src_projection
node_tests_model -.->|"verify"| node_src_weekly_projection
node_tests_reco_opt -.->|"verify"| node_src_recommender
node_tests_reco_opt -.->|"verify"| node_src_lineup
node_tests_cli_report -.->|"verify"| node_src_benchmark
node_tests_cli_report -.->|"verify"| node_scripts_weekly_live

click node_scripts_run_pipeline "https://github.com/kych23/ff_predictors/blob/main/scripts/run_pipeline.sh"
click node_scripts_seed_db "https://github.com/kych23/ff_predictors/blob/main/scripts/seed_db.py"
click node_src_db "https://github.com/kych23/ff_predictors/tree/main/src/db"
click node_src_ingest "https://github.com/kych23/ff_predictors/tree/main/src/ingest"
click node_src_features "https://github.com/kych23/ff_predictors/tree/main/src/features"
click node_src_labels "https://github.com/kych23/ff_predictors/tree/main/src/labels"
click node_src_projection "https://github.com/kych23/ff_predictors/tree/main/src/projection"
click node_src_weekly_projection "https://github.com/kych23/ff_predictors/blob/main/src/projection/weekly_train.py"
click node_src_recommender "https://github.com/kych23/ff_predictors/tree/main/src/recommender"
click node_src_lineup "https://github.com/kych23/ff_predictors/blob/main/src/lineup/optimizer.py"
click node_src_benchmark "https://github.com/kych23/ff_predictors/tree/main/src/benchmark"
click node_src_config "https://github.com/kych23/ff_predictors/blob/main/src/config/league_config.py"
click node_tests_leakage "https://github.com/kych23/ff_predictors/blob/main/tests/test_leakage.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_scripts_run_pipeline,node_scripts_seed_db,node_scripts_build_train,node_scripts_weekly_live toneBlue
class node_src_db,node_src_ingest,node_src_features,node_src_labels,node_src_projection,node_src_weekly_projection,node_src_recommender,node_src_lineup,node_src_benchmark,node_src_config toneAmber
class node_tests_leakage,node_tests_model,node_tests_reco_opt,node_tests_cli_report toneMint
```

## Components

### Scripts

Operational CLI and batch entry points

- [Pipeline run](https://github.com/kych23/ff_predictors/blob/main/scripts/run_pipeline.sh): orchestration script · Top-level batch workflow for a fresh season refresh · `scripts/run_pipeline.sh`
- [Seed DB](https://github.com/kych23/ff_predictors/blob/main/scripts/seed_db.py): orchestration script · Prepares the warehouse before feature generation and training · `scripts/seed_db.py`
- **Build/train**: orchestration scripts · Feature, label, and model training entry points
- **Weekly live**: CLI scripts · Forward weekly projection, benchmarking, and lineup workflows

### Core

Reusable engine and data pipeline

- [DB layer](https://github.com/kych23/ff_predictors/tree/main/src/db): persistence · SQLAlchemy models, sessions, loaders, and upsert/init helpers · `src/db`
- [Ingest](https://github.com/kych23/ff_predictors/tree/main/src/ingest): etl boundary · External data acquisition, source normalization, and player ID crosswalks · `src/ingest`
- [Features](https://github.com/kych23/ff_predictors/tree/main/src/features): feature engineering · Season and weekly feature assembly with leakage protection · `src/features`
- [Labels](https://github.com/kych23/ff_predictors/tree/main/src/labels): target generation · Fantasy scoring and supervised target construction · `src/labels`
- [Season model](https://github.com/kych23/ff_predictors/tree/main/src/projection): forecasting pipeline · Preseason pooled quantile model training, calibration, and evaluation · `src/projection`
- [Weekly model](https://github.com/kych23/ff_predictors/blob/main/src/projection/weekly_train.py): forecasting pipeline · In-season weekly forecasting built from season priors and current context · `src/projection/weekly_train.py`
- [Draft recommender](https://github.com/kych23/ff_predictors/tree/main/src/recommender): decision engine · Converts projections into draft rankings using roster needs and pick survival · `src/recommender`
- [Lineup optimizer](https://github.com/kych23/ff_predictors/blob/main/src/lineup/optimizer.py): optimizer · Exact ILP start/sit solver for weekly roster assignment · `src/lineup/optimizer.py`
- [Benchmarking](https://github.com/kych23/ff_predictors/tree/main/src/benchmark): evaluation · Ranking, calibration, draft simulation, weekly benchmark, and reporting · `src/benchmark`
- [League config](https://github.com/kych23/ff_predictors/blob/main/src/config/league_config.py): configuration · League scoring and roster assumptions · `src/config/league_config.py`

### Tests

Architecture-level verification coverage

- [Leakage tests](https://github.com/kych23/ff_predictors/blob/main/tests/test_leakage.py): tests · Validates temporal boundaries and leakage guards · `tests/test_leakage.py`
- **Model tests**: tests · Covers quantiles, calibration, folds, evaluation, and weekly modeling
- **Decision tests**: tests · Covers replacement, survival, roster state, VONA, and optimizer behavior
- **CLI/report tests**: tests · Covers report generation, draft layout, weekly benchmark, and start CLI

## Connections

- 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
