# magesh-ram/afr-framework architecture

> Architecture diagram of the GitHub repository magesh-ram/afr-framework, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/magesh-ram/afr-framework
- Repository: https://github.com/magesh-ram/afr-framework

## Overview

#### Purpose and Topology
AFR is a domain-agnostic adaptive multi-horizon forecasting framework. It combines a pluggable scientific Python core with a FastAPI REST backend and React/Vite dashboard. Its decision output is a calibrated forecast interval plus reliable horizon `H*`: the longest forecast prefix meeting a configured risk budget.

Primary runtime entry points are `app/main.py` (`uvicorn app.main:app`) and `frontend/src/main.jsx`. Offline preparation and study execution enter through `scripts/preprocess.py`, `scripts/run_calibration_study.py`, and `scripts/run_deep_validation.py`.

#### Scientific Pipeline and Contracts
The core orchestration and replaceable-role contracts are in `parc/core/pipeline.py`, `parc/core/interfaces.py`, and `parc/core/types.py`. A registry/capability layer in `parc/core/registry.py` selects and validates compatible forecast, uncertainty, calibration, and reliability implementations before a run; incompatible pairings are rejected rather than silently substituted.

The pipeline is intentionally data-domain independent. Dataset-specific NYC Taxi handling is outside the core contract.

#### Data Ingestion, Features, and Splits
Input CSVs or bundled taxi data enter through `parc/data/ingest.py` and `app/services/datasets.py`. The data layer validates time-series inputs, derives modeling features, preprocesses observations, and creates train/calibration/test temporal partitions via `parc/data/features.py`, `parc/data/preprocessing.py`, and `parc/data/splits.py`.

Processed NYC panels and metadata are materialized under `data/processed/`; bundled demo/source datasets reside in `app/demo_data/` and `sample_data/`. `scripts/preprocess.py` is the raw-TLC-to-processed-data boundary.

#### Forecast and Uncertainty Providers
Forecast providers generate point paths using baseline, linear, GBM, or neural implementations: `parc/forecast/baselines.py`, `parc/forecast/gbm.py`, and `parc/forecast/deep.py`. Neural execution is runtime-controlled by backend settings, so deployment can retain LightGBM functionality while refusing neural requests.

Uncertainty providers attach predictive spread to a forecast through residual, intrinsic-model, or MC-dropout approaches in `parc/uncertainty/residual.py`, `parc/uncertainty/model_intrinsic.py`, and `parc/uncertainty/mc_dropout.py`. This boundary lets models with native quantiles differ from models requiring post-hoc residual uncertainty.

#### Calibration, Reliability, and Evaluation
Calibration transforms raw uncertainty into published intervals using identity and conformal methods in `parc/calibration/identity.py` and `parc/calibration/conformal.py`; diagnostics are in `parc/calibration/diagnostics.py`.

The reliability controller consumes published intervals and the risk budget to compute per-horizon risk and `H*`, without realized future outcomes in the certificate signature. Adaptive/static control and monitoring live in `parc/reliability/controller.py` and `parc/reliability/monitors.py`. Aggregate quality metrics are produced by `parc/evaluation/metrics.py`; research outputs are persisted in `reports/`.

#### Backend API and Experiment Lifecycle
FastAPI application setup, CORS, health probing, and configuration limits are in `app/main.py` and `app/config.py`. REST routing is centralized in `app/api/routes.py`; request/response contracts are in `app/schemas/experiment.py`.

Service modules form the API-to-core boundary: experiment execution/caching in `app/services/experiments.py`, dataset handling in `app/services/datasets.py`, catalog/configuration exposure in `app/services/catalog.py`, and downloadable result generation in `app/services/exports.py`. Upload size, row count, horizon, training epoch, and MC-pass caps protect the service runtime.

#### Frontend and Deployment Boundaries
The SPA uses `frontend/src/api/client.js` to call the `/api` backend configured at build time. `frontend/src/App.jsx` and page modules such as `frontend/src/pages/NewExperiment.jsx`, `frontend/src/pages/Results.jsx`, and `frontend/src/pages/Dashboard.jsx` drive configuration, run state, and result visualization.

The backend is deployable on Render via `render.yaml`; the frontend targets Vercel through `frontend/vercel.json`. Material runtimes include Python/FastAPI, LightGBM, PyTorch for neural models, and Node-based React/Vite. No external database or secret-backed service is indicated; experiments and demo results are application-managed/cached.

Main entry points: [`parc/core/pipeline.py`](https://github.com/magesh-ram/afr-framework/blob/main/parc/core/pipeline.py) (Forecast pipeline), [`app/api/routes.py`](https://github.com/magesh-ram/afr-framework/blob/main/app/api/routes.py) (REST API), [`parc/data/ingest.py`](https://github.com/magesh-ram/afr-framework/blob/main/parc/data/ingest.py) (Data preparation), [`app/services/experiments.py`](https://github.com/magesh-ram/afr-framework/blob/main/app/services/experiments.py) (Experiment service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_offline["Offline workflows"]
  node_preprocess["Preprocess script<br/>offline entry<br/>[preprocess.py]"]
  node_studies["Study runners<br/>offline entry"]
  node_processed_data[("Processed panels<br/>data artifact")]
  node_reports["Research reports<br/>output artifact"]
end

subgraph group_core["Scientific core"]
  node_data_pipeline["Data preparation<br/>data layer<br/>[ingest.py]"]
  node_contracts["Pipeline contracts<br/>core contracts<br/>[interfaces.py]"]
  node_registry["Capability registry<br/>compatibility gate<br/>[registry.py]"]
  node_pipeline{{"Forecast pipeline<br/>core orchestrator<br/>[pipeline.py]"}}
  node_forecasters["Forecast providers<br/>model providers<br/>[gbm.py]"]
  node_uncertainty["Uncertainty providers<br/>spread providers<br/>[residual.py]"]
  node_calibration["Interval calibration<br/>calibration providers<br/>[conformal.py]"]
  node_reliability["Reliability controller<br/>risk controller<br/>[controller.py]"]
  node_metrics["Evaluation metrics<br/>[metrics.py]"]
end

subgraph group_backend["FastAPI service"]
  node_fastapi_app{{"FastAPI app<br/>Python runtime<br/>[main.py]"}}
  node_api_routes["REST API<br/>route layer<br/>[routes.py]"]
  node_experiment_service["Experiment service<br/>service layer<br/>[experiments.py]"]
  node_dataset_service["Dataset service<br/>service layer<br/>[datasets.py]"]
  node_api_artifacts["Catalog and exports<br/>service layer<br/>[catalog.py]"]
end

subgraph group_frontend["Dashboard"]
  node_react_app{{"React dashboard<br/>Node SPA<br/>[App.jsx]"}}
  node_api_client["API client<br/>frontend boundary<br/>[client.js]"]
end

node_preprocess -->|"prepares"| node_data_pipeline
node_data_pipeline -->|"materializes"| node_processed_data
node_studies -->|"runs"| node_pipeline
node_pipeline -->|"validates selection"| node_registry
node_registry -->|"checks roles"| node_contracts
node_pipeline -->|"uses prepared splits"| node_data_pipeline
node_pipeline -->|"generates paths"| node_forecasters
node_forecasters -->|"point forecasts"| node_uncertainty
node_uncertainty -->|"raw spread"| node_calibration
node_calibration -->|"published intervals"| node_reliability
node_reliability -->|"risk certificate and H*"| node_metrics
node_metrics -->|"persists study outputs"| node_reports
node_fastapi_app -->|"mounts"| node_api_routes
node_api_routes -->|"run requests"| node_experiment_service
node_api_routes -->|"dataset requests"| node_dataset_service
node_api_routes -->|"catalog and downloads"| node_api_artifacts
node_dataset_service -->|"validates and prepares"| node_data_pipeline
node_experiment_service -->|"executes"| node_pipeline
node_experiment_service -.->|"reads cached panels"| node_processed_data
node_api_client -->|"/api"| node_api_routes
node_react_app -->|"uses"| node_api_client

click node_preprocess "https://github.com/magesh-ram/afr-framework/blob/main/scripts/preprocess.py"
click node_studies "https://github.com/magesh-ram/afr-framework/blob/main/scripts/run_calibration_study.py"
click node_data_pipeline "https://github.com/magesh-ram/afr-framework/blob/main/parc/data/ingest.py"
click node_contracts "https://github.com/magesh-ram/afr-framework/blob/main/parc/core/interfaces.py"
click node_registry "https://github.com/magesh-ram/afr-framework/blob/main/parc/core/registry.py"
click node_pipeline "https://github.com/magesh-ram/afr-framework/blob/main/parc/core/pipeline.py"
click node_forecasters "https://github.com/magesh-ram/afr-framework/blob/main/parc/forecast/gbm.py"
click node_uncertainty "https://github.com/magesh-ram/afr-framework/blob/main/parc/uncertainty/residual.py"
click node_calibration "https://github.com/magesh-ram/afr-framework/blob/main/parc/calibration/conformal.py"
click node_reliability "https://github.com/magesh-ram/afr-framework/blob/main/parc/reliability/controller.py"
click node_metrics "https://github.com/magesh-ram/afr-framework/blob/main/parc/evaluation/metrics.py"
click node_fastapi_app "https://github.com/magesh-ram/afr-framework/blob/main/app/main.py"
click node_api_routes "https://github.com/magesh-ram/afr-framework/blob/main/app/api/routes.py"
click node_experiment_service "https://github.com/magesh-ram/afr-framework/blob/main/app/services/experiments.py"
click node_dataset_service "https://github.com/magesh-ram/afr-framework/blob/main/app/services/datasets.py"
click node_api_artifacts "https://github.com/magesh-ram/afr-framework/blob/main/app/services/catalog.py"
click node_react_app "https://github.com/magesh-ram/afr-framework/blob/main/frontend/src/App.jsx"
click node_api_client "https://github.com/magesh-ram/afr-framework/blob/main/frontend/src/api/client.js"

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_preprocess,node_studies,node_processed_data,node_reports toneBlue
class node_data_pipeline,node_contracts,node_registry,node_pipeline,node_forecasters,node_uncertainty,node_calibration,node_reliability,node_metrics toneAmber
class node_fastapi_app,node_api_routes,node_experiment_service,node_dataset_service,node_api_artifacts toneMint
class node_react_app,node_api_client toneRose
```

## Components

### Offline workflows

- [Preprocess script](https://github.com/magesh-ram/afr-framework/blob/main/scripts/preprocess.py): offline entry · Converts raw TLC data into processed panels. · `scripts/preprocess.py`
- [Study runners](https://github.com/magesh-ram/afr-framework/blob/main/scripts/run_calibration_study.py): offline entry · Calibration and deep-validation research runs. · `scripts/run_calibration_study.py`
- **Processed panels**: data artifact · NYC panels and metadata materialized for runs.
- **Research reports**: output artifact

### Scientific core

- [Data preparation](https://github.com/magesh-ram/afr-framework/blob/main/parc/data/ingest.py): data layer · Validates series, derives features, preprocesses, and splits time windows. · `parc/data/ingest.py`
- [Pipeline contracts](https://github.com/magesh-ram/afr-framework/blob/main/parc/core/interfaces.py): core contracts · Pluggable forecast, uncertainty, calibration, and reliability roles. · `parc/core/interfaces.py`
- [Capability registry](https://github.com/magesh-ram/afr-framework/blob/main/parc/core/registry.py): compatibility gate · Selects valid provider combinations and rejects incompatible ones. · `parc/core/registry.py`
- [Forecast pipeline](https://github.com/magesh-ram/afr-framework/blob/main/parc/core/pipeline.py): core orchestrator · Runs the domain-agnostic forecasting workflow. · `parc/core/pipeline.py`
- [Forecast providers](https://github.com/magesh-ram/afr-framework/blob/main/parc/forecast/gbm.py): model providers · Baseline, linear, GBM, and neural point forecasts. · `parc/forecast/gbm.py`
- [Uncertainty providers](https://github.com/magesh-ram/afr-framework/blob/main/parc/uncertainty/residual.py): spread providers · Residual, intrinsic-model, or MC-dropout predictive spread. · `parc/uncertainty/residual.py`
- [Interval calibration](https://github.com/magesh-ram/afr-framework/blob/main/parc/calibration/conformal.py): calibration providers · Identity or conformal conversion of raw uncertainty to intervals. · `parc/calibration/conformal.py`
- [Reliability controller](https://github.com/magesh-ram/afr-framework/blob/main/parc/reliability/controller.py): risk controller · Computes per-horizon risk and longest reliable prefix H*. · `parc/reliability/controller.py`
- [Evaluation metrics](https://github.com/magesh-ram/afr-framework/blob/main/parc/evaluation/metrics.py): evaluation · Aggregates forecast and interval quality metrics. · `parc/evaluation/metrics.py`

### FastAPI service

- [FastAPI app](https://github.com/magesh-ram/afr-framework/blob/main/app/main.py): Python runtime · Application setup, CORS, health checks, and runtime limits. · `app/main.py`
- [REST API](https://github.com/magesh-ram/afr-framework/blob/main/app/api/routes.py): route layer · Centralized /api routing and experiment contracts. · `app/api/routes.py`
- [Experiment service](https://github.com/magesh-ram/afr-framework/blob/main/app/services/experiments.py): service layer · Executes and caches core pipeline runs. · `app/services/experiments.py`
- [Dataset service](https://github.com/magesh-ram/afr-framework/blob/main/app/services/datasets.py): service layer · Handles uploads, bundled data, and dataset preparation. · `app/services/datasets.py`
- [Catalog and exports](https://github.com/magesh-ram/afr-framework/blob/main/app/services/catalog.py): service layer · Exposes configuration catalog and downloadable results. · `app/services/catalog.py`

### Dashboard

- [React dashboard](https://github.com/magesh-ram/afr-framework/blob/main/frontend/src/App.jsx): Node SPA · Configuration, run state, and result visualization. · `frontend/src/App.jsx`
- [API client](https://github.com/magesh-ram/afr-framework/blob/main/frontend/src/api/client.js): frontend boundary · Build-time configured client for the /api backend. · `frontend/src/api/client.js`

## Connections

- Preprocess script → Data preparation: prepares
- Data preparation → Processed panels: materializes
- Study runners → Forecast pipeline: runs
- Forecast pipeline → Capability registry: validates selection
- Capability registry → Pipeline contracts: checks roles
- Forecast pipeline → Data preparation: uses prepared splits
- Forecast pipeline → Forecast providers: generates paths
- Forecast providers → Uncertainty providers: point forecasts
- Uncertainty providers → Interval calibration: raw spread
- Interval calibration → Reliability controller: published intervals
- Reliability controller → Evaluation metrics: risk certificate and H*
- Evaluation metrics → Research reports: persists study outputs
- FastAPI app → REST API: mounts
- REST API → Experiment service: run requests
- REST API → Dataset service: dataset requests
- REST API → Catalog and exports: catalog and downloads
- Dataset service → Data preparation: validates and prepares
- Experiment service → Forecast pipeline: executes
- Experiment service → Processed panels: reads cached panels
- API client → REST API: /api
- React dashboard → API client: uses
