# pausarock/pidl-based-tse architecture

> Architecture diagram of the GitHub repository pausarock/pidl-based-tse, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pausarock/pidl-based-tse
- Repository: https://github.com/pausarock/pidl-based-tse

## Overview

1. **Purpose and scope**
   - This repository is a small, single-purpose Python project for **traffic state estimation on freeways** using a **Physics-Informed Neural Network (PINN)** approach.
   - The architecture is organized around one core model plus supporting loaders, with separate entry points for **training** and **testing**.

2. **Main execution paths**
   - There are two top-level workflows:
     - `train.py` builds the model, loads data/configuration, trains, evaluates, and saves artifacts.
     - `test.py` loads a saved model and produces prediction outputs and evaluation results.
   - This separation is important for diagramming the system as two pipelines sharing the same model/data preparation layer.

3. **Core model subsystem**
   - `pinn.py` contains the main **PINN implementation**.
   - It is the central computational component, combining:
     - data-driven prediction from traffic measurements
     - physics-based constraints for traffic dynamics
   - The model likely consumes detector-based inputs and produces predicted traffic state variables such as **flow** and **speed**.

4. **Data ingestion subsystem**
   - `data_loader.py` is the boundary between raw CSV data and the model.
   - The README indicates the project uses **CSV-based configuration and traffic data**, so this module is responsible for turning those files into model-ready tensors/arrays.
   - The external data dependency is on parent-directory files such as:
     - `sh_flow_data.csv`
     - `sh_speed_data.csv`

5. **Topology and station configuration subsystem**
   - `topology_loader.py` handles the freeway structure and station metadata.
   - It reads configuration files such as:
     - `detector_positions.csv`
     - `ramp_info.csv`
     - `station_config.csv`
   - This module defines the spatial layout the model must respect: detector locations, ramp locations/types, and which stations are supervised vs. hidden.

6. **Configuration-driven architecture**
   - The project is strongly driven by CSV configuration rather than hardcoded topology.
   - `train_config.csv` provides default detector ID lists used during training.
   - Command-line arguments in `train.py` override or refine the configuration, such as:
     - network depth/width (`--layers`)
     - training duration (`--total_steps`)
     - physics loss weights (`--alpha1`, `--alpha2`, `--beta1`, `--beta2`, `--gamma`)
   - For an architecture diagram, this is a key boundary: **configuration files feed both topology construction and training setup**.

7. **Training pipeline**
   - `train.py` is the orchestration layer.
   - Its responsibilities are likely:
     - load topology and detector selections
     - load traffic flow/speed data
     - instantiate the PINN
     - train with supervised and physics-informed objectives
     - periodically validate or select the best checkpoint
   - The output artifact mentioned is `best_model.pat`, which is the saved trained model weights.

8. **Testing and evaluation pipeline**
   - `test.py` is the inference/evaluation entry point.
   - After loading the trained model, it generates:
     - `pred_flow_JWZ.csv`
     - `pred_speed_JWZ.csv`
   - The README notes automatic evaluation using **MAPE** and **RMSE**, so testing is not just inference; it also computes quantitative performance metrics.

9. **Data flow from files to outputs**
   - The end-to-end flow is:
     - CSV topology/config files and traffic data are loaded
     - topology and station definitions shape the estimation problem
     - the PINN trains on observed detector data plus physics constraints
     - the resulting model is saved
     - test/inference produces prediction CSVs and plots
   - This makes the architecture a classic **offline ML pipeline** rather than a request/response service.

10. **External inputs and system boundaries**
   - All operational inputs are file-based, not service-based:
     - configuration CSVs
     - traffic measurement CSVs
   - There is no evidence of a database, message broker, or web frontend in this repo.
   - The main system boundary is the local filesystem plus Python runtime dependencies listed in `requirements.txt`.

11. **Outputs and artifacts**
   - The repository produces both machine-readable and visual outputs:
     - model checkpoint: `best_model.pat`
     - prediction tables: `pred_flow_JWZ.csv`, `pred_speed_JWZ.csv`
     - plots in PNG/SVG formats
   - In a diagram, these are downstream artifacts from the training/testing pipeline rather than separate subsystems.

12. **Repo structure summary**
   - `__init__.py` indicates the code is packaged as a Python module, but the project is still script-oriented.
   - The main architectural split is:
     - **Topology/configuration layer**: `topology_loader.py`, CSV files
     - **Data layer**: `data_loader.py`, traffic CSVs
     - **Model layer**: `pinn.py`
     - **Orchestration layer**: `train.py`, `test.py`
   - That separation is the clearest way to draw the system accurately.

Main entry points: [`test.py`](https://github.com/pausarock/pidl-based-tse/blob/main/test.py) (Test), [`train.py`](https://github.com/pausarock/pidl-based-tse/blob/main/train.py) (Train), [`pinn.py`](https://github.com/pausarock/pidl-based-tse/blob/main/pinn.py) (PINN), [`topology_loader.py`](https://github.com/pausarock/pidl-based-tse/blob/main/topology_loader.py) (Topology loader)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_orch["Orchestration"]
  node_train_py["Train<br/>entry script<br/>[train.py]"]
  node_test_py["Test<br/>entry script<br/>[test.py]"]
end

subgraph group_g_model["Model"]
  node_pinn{{"PINN<br/>core model<br/>[pinn.py]"}}
end

subgraph group_g_data["Inputs"]
  node_data_loader["Data loader<br/>dataset prep<br/>[data_loader.py]"]
  node_topology_loader["Topology loader<br/>spatial config<br/>[topology_loader.py]"]
  node_train_config["Train config<br/>csv config"]
  node_flow_data["Flow data<br/>traffic csv"]
  node_speed_data["Speed data<br/>traffic csv"]
  node_detector_positions["Detectors<br/>topology csv"]
  node_ramp_info["Ramps<br/>topology csv"]
  node_station_config["Stations<br/>station csv"]
end

subgraph group_g_out["Outputs"]
  node_checkpoint[("Best model<br/>model artifact")]
  node_pred_flow["Flow preds<br/>prediction csv"]
  node_pred_speed["Speed preds<br/>prediction csv"]
  node_metrics["Metrics<br/>evaluation"]
  node_plots["Plots<br/>visual output"]
end

node_train_config -->|"defaults"| node_train_py
node_detector_positions -->|"load"| node_topology_loader
node_ramp_info -->|"load"| node_topology_loader
node_station_config -->|"load"| node_topology_loader
node_flow_data -->|"load"| node_data_loader
node_speed_data -->|"load"| node_data_loader
node_topology_loader -->|"topology"| node_train_py
node_data_loader -->|"batches"| node_train_py
node_train_py -->|"build/train"| node_pinn
node_pinn -->|"losses"| node_train_py
node_train_py -->|"save"| node_checkpoint
node_checkpoint -->|"load"| node_test_py
node_topology_loader -->|"context"| node_test_py
node_data_loader -->|"inputs"| node_test_py
node_test_py -->|"infer"| node_pinn
node_pinn -->|"emit"| node_pred_flow
node_pinn -->|"emit"| node_pred_speed
node_test_py -->|"score"| node_metrics
node_test_py -->|"render"| node_plots

click node_train_py "https://github.com/pausarock/pidl-based-tse/blob/main/train.py"
click node_test_py "https://github.com/pausarock/pidl-based-tse/blob/main/test.py"
click node_pinn "https://github.com/pausarock/pidl-based-tse/blob/main/pinn.py"
click node_data_loader "https://github.com/pausarock/pidl-based-tse/blob/main/data_loader.py"
click node_topology_loader "https://github.com/pausarock/pidl-based-tse/blob/main/topology_loader.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_train_py,node_test_py toneBlue
class node_pinn toneAmber
class node_data_loader,node_topology_loader,node_train_config,node_flow_data,node_speed_data,node_detector_positions,node_ramp_info,node_station_config toneMint
class node_checkpoint,node_pred_flow,node_pred_speed,node_metrics,node_plots toneRose
```

## Components

### Orchestration

Entry points that assemble, train, and evaluate the PINN pipeline.

- [Train](https://github.com/pausarock/pidl-based-tse/blob/main/train.py): entry script · Main training workflow that loads configuration, builds the model, trains, validates, and saves the best checkpoint. · `train.py`
- [Test](https://github.com/pausarock/pidl-based-tse/blob/main/test.py): entry script · Inference and evaluation workflow that loads a trained model and produces predictions and metrics. · `test.py`

### Model

Core physics-informed network and its training objective.

- [PINN](https://github.com/pausarock/pidl-based-tse/blob/main/pinn.py): core model · Physics-informed neural network for freeway traffic state estimation. · `pinn.py`

### Inputs

CSV-based traffic data, topology, and configuration inputs.

- [Data loader](https://github.com/pausarock/pidl-based-tse/blob/main/data_loader.py): dataset prep · Converts raw traffic measurement CSVs into model-ready arrays or tensors. · `data_loader.py`
- [Topology loader](https://github.com/pausarock/pidl-based-tse/blob/main/topology_loader.py): spatial config · Loads detector, ramp, and station layout metadata that defines the estimation topology. · `topology_loader.py`
- **Train config**: csv config · Default detector selection and training setup used by the training pipeline.
- **Flow data**: traffic csv · Observed freeway flow measurements used for supervised learning and evaluation.
- **Speed data**: traffic csv · Observed freeway speed measurements used for supervised learning and evaluation.
- **Detectors**: topology csv · Detector locations along the freeway corridor.
- **Ramps**: topology csv · Ramp metadata describing on-ramps and off-ramps.
- **Stations**: station csv · Station visibility and supervision layout for the estimation problem.

### Outputs

Saved model artifacts and evaluation results.

- **Best model**: model artifact · Saved trained PINN weights produced by training.
- **Flow preds**: prediction csv · Predicted traffic flow output from testing.
- **Speed preds**: prediction csv · Predicted traffic speed output from testing.
- **Metrics**: evaluation · MAPE and RMSE computed during testing.
- **Plots**: visual output · Generated figures for model behavior and prediction quality.

## Connections

- Train config → Train: defaults · Provides default detector IDs and training setup.
- Detectors → Topology loader: load · Defines detector spatial positions.
- Ramps → Topology loader: load · Provides ramp layout metadata.
- Stations → Topology loader: load · Defines supervised and hidden stations.
- Flow data → Data loader: load · Supplies traffic flow observations.
- Speed data → Data loader: load · Supplies traffic speed observations.
- Topology loader → Train: topology · Shapes the estimation problem used in training.
- Data loader → Train: batches · Provides model-ready training data.
- Train → PINN: build/train · Instantiates and optimizes the PINN.
- PINN → Train: losses · Returns supervised and physics-informed losses for optimization.
- Train → Best model: save · Persists the best trained model weights.
- Best model → Test: load · Restores the trained model for inference.
- Topology loader → Test: context · Provides the same spatial configuration for evaluation.
- Data loader → Test: inputs · Provides evaluation inputs derived from traffic data.
- Test → PINN: infer · Runs the trained PINN for prediction.
- PINN → Flow preds: emit · Produces flow predictions.
- PINN → Speed preds: emit · Produces speed predictions.
- Test → Metrics: score · Computes MAPE and RMSE against observed values.
- Test → Plots: render · Generates visual evaluation outputs.
