# donkkis/modelconductor architecture

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

- Interactive diagram: https://gitdiagram.com/donkkis/modelconductor
- Repository: https://github.com/donkkis/modelconductor

## Overview

1. **What this repo is**
   ModelConductor is a Python co-simulation/orchestration library for “digital twin” style experiments. The core architecture is not a web app; it is a runtime that connects an incoming measurement source to a simulation or ML model and coordinates them in an online loop.

2. **Primary runtime boundary**
   The main boundary is between **external measurements** and **model execution**. The repository is built around taking data that arrives over time, polling or receiving it, transforming it into model inputs, and pushing model outputs back into the experiment flow.

3. **Experiment orchestration layer**
   `modelconductor/experiment.py` is the central coordinator. The README shows `OnlineOneToOneExperiment`, which suggests the orchestrator manages:
   - route setup between a source and a target model
   - runtime/stop-time control
   - optional logging to CSV
   - execution of the online loop in a background thread

4. **Measurement ingestion subsystem**
   `modelconductor/measurementhandler.py` contains the inbound data side. The documented class `IncomingMeasurementPoller` reads from a database URI at a polling interval, so the measurement boundary is a persistent store rather than direct stream plumbing. In the example, SQLite is used as the measurement buffer, but the abstraction is “poll database for new rows.”

5. **Model execution subsystem**
   `modelconductor/modelhandler.py` is the model-side abstraction. The README explicitly says the framework supports:
   - **FMU models**
   - **sklearn machine learning models**
   
   The example uses `FMUModelHandler`, which implies the handler encapsulates model lifecycle, stepping, and mapping between input/control keys and output/target keys.

6. **Data mapping and schema conventions**
   The model interface is key-based rather than strongly typed by domain object. The README shows:
   - `input_keys`
   - `control_keys`
   - `target_keys`
   - `timestamp_key`
   
   This means the architecture depends on a field mapping layer between raw measurements and model variables, with time as a first-class coordination signal.

7. **Execution flow**
   The documented flow is:
   - an external writer appends measurement rows to a DB table
   - `IncomingMeasurementPoller` polls that DB
   - `OnlineOneToOneExperiment` routes the data into the model handler
   - the model advances in steps and emits outputs
   - outputs can be logged to CSV
   
   This is an online, threaded, producer/consumer-style pipeline rather than batch processing.

8. **Persistence and example environment**
   The examples under `modelconductor/examples` show the expected integration environment:
   - `CoupledClutches.fmu` for the FMU simulation
   - CSV files for input/output data
   - Jupyter notebooks for experimentation and demonstration
   
   This indicates the repo is meant for prototyping and reproducible simulation setups, not for a deployed production service.

9. **Utilities and shared concerns**
   `exceptions.py` and `utils.py` likely hold shared error types and helper functions used across handlers and experiments. These are supporting modules, not major architectural pillars, but they define cross-cutting behavior such as validation, conversion, and failure handling.

10. **Server layer**
   `modelconductor/server.py` suggests there is an optional service boundary, likely for exposing the framework over a network or enabling remote control. However, the README does not document it as the primary path; the main architecture is library-centric, with direct Python orchestration as the standard usage model.

11. **Testing and training support**
   `modelconductor/tests.py`, `test_utils.py`, and `testresources/train.py` show that the repo includes test fixtures and a small training resource for model-related workflows. The presence of a `train.py` under test resources reinforces that sklearn-style model support is part of the supported surface area.

12. **Diagram takeaway**
   A good architecture diagram should show:
   - an external data source writing measurements into a DB
   - a polling measurement handler reading that DB
   - an experiment controller coordinating the loop
   - a model handler executing an FMU or sklearn model
   - optional logging/output persistence
   - example notebooks and sample FMUs/CSV files as non-runtime assets supporting experimentation

Main entry points: [`modelconductor/experiment.py`](https://github.com/donkkis/modelconductor/blob/master/modelconductor/experiment.py) (Experiment), [`modelconductor/modelhandler.py`](https://github.com/donkkis/modelconductor/blob/master/modelconductor/modelhandler.py) (Model handler), [`modelconductor/examples`](https://github.com/donkkis/modelconductor/tree/master/modelconductor/examples) (Examples), [`modelconductor/measurementhandler.py`](https://github.com/donkkis/modelconductor/blob/master/modelconductor/measurementhandler.py) (Poller)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_core["Core runtime"]
  node_n_experiment["Experiment<br/>orchestrator<br/>[experiment.py]"]
  node_n_route["Routing<br/>mapping layer<br/>[experiment.py]"]
  node_n_clock(("Runtime control<br/>loop control<br/>[experiment.py]"))
  node_n_server["Server<br/>optional service<br/>[server.py]"]
  node_n_utils["Utils<br/>shared helpers<br/>[utils.py]"]
  node_n_exceptions["Exceptions<br/>shared errors<br/>[exceptions.py]"]
end

subgraph group_grp_io["I/O boundaries"]
  node_n_source[("Measurement DB<br/>external source")]
  node_n_poller["Poller<br/>measurement handler"]
  node_n_log["CSV log<br/>output sink<br/>[experiment.py]"]
end

subgraph group_grp_models["Model support"]
  node_n_modelhandler["Model handler<br/>model abstraction<br/>[modelhandler.py]"]
  node_n_fmu["FMU model<br/>simulation backend<br/>[modelhandler.py]"]
  node_n_sklearn["Sklearn model<br/>ml backend<br/>[modelhandler.py]"]
  node_n_outputs(("Model outputs<br/>signal flow"))
end

subgraph group_grp_assets["Examples and assets"]
  node_n_examples["Examples<br/>demo assets"]
  node_n_fmu_asset[("CoupledClutches.fmu<br/>sample asset")]
  node_n_in_csv["Input CSV<br/>sample data"]
  node_n_out_csv["Output CSV<br/>sample data<br/>[out.csv]"]
end

node_n_source -->|"rows polled"| node_n_poller
node_n_poller -->|"measurements"| node_n_experiment
node_n_experiment -->|"map fields"| node_n_route
node_n_experiment -->|"control loop"| node_n_clock
node_n_route -->|"inputs"| node_n_modelhandler
node_n_clock -->|"step"| node_n_modelhandler
node_n_modelhandler -->|"backend"| node_n_fmu
node_n_modelhandler -->|"backend"| node_n_sklearn
node_n_fmu -->|"emit"| node_n_outputs
node_n_sklearn -->|"emit"| node_n_outputs
node_n_outputs -->|"feedback"| node_n_experiment
node_n_experiment -->|"optional log"| node_n_log
node_n_examples -.->|"demonstrates"| node_n_experiment
node_n_examples -.->|"uses"| node_n_fmu_asset
node_n_examples -.->|"uses"| node_n_in_csv
node_n_examples -.->|"produces"| node_n_out_csv
node_n_server -.->|"exposes"| node_n_experiment
node_n_utils -.->|"helpers"| node_n_poller
node_n_utils -.->|"helpers"| node_n_modelhandler
node_n_exceptions -.->|"errors"| node_n_experiment
node_n_exceptions -.->|"errors"| node_n_modelhandler

click node_n_poller "https://github.com/donkkis/modelconductor/blob/master/modelconductor/measurementhandler.py"
click node_n_experiment "https://github.com/donkkis/modelconductor/blob/master/modelconductor/experiment.py"
click node_n_route "https://github.com/donkkis/modelconductor/blob/master/modelconductor/experiment.py"
click node_n_clock "https://github.com/donkkis/modelconductor/blob/master/modelconductor/experiment.py"
click node_n_log "https://github.com/donkkis/modelconductor/blob/master/modelconductor/experiment.py"
click node_n_modelhandler "https://github.com/donkkis/modelconductor/blob/master/modelconductor/modelhandler.py"
click node_n_fmu "https://github.com/donkkis/modelconductor/blob/master/modelconductor/modelhandler.py"
click node_n_sklearn "https://github.com/donkkis/modelconductor/blob/master/modelconductor/modelhandler.py"
click node_n_server "https://github.com/donkkis/modelconductor/blob/master/modelconductor/server.py"
click node_n_examples "https://github.com/donkkis/modelconductor/tree/master/modelconductor/examples"
click node_n_fmu_asset "https://github.com/donkkis/modelconductor/blob/master/modelconductor/examples/CoupledClutches.fmu"
click node_n_in_csv "https://github.com/donkkis/modelconductor/blob/master/modelconductor/examples/CoupledClutches_in_sh.csv"
click node_n_out_csv "https://github.com/donkkis/modelconductor/blob/master/modelconductor/examples/out.csv"
click node_n_utils "https://github.com/donkkis/modelconductor/blob/master/modelconductor/utils.py"
click node_n_exceptions "https://github.com/donkkis/modelconductor/blob/master/modelconductor/exceptions.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_n_experiment,node_n_route,node_n_clock,node_n_server,node_n_utils,node_n_exceptions toneBlue
class node_n_source,node_n_poller,node_n_log toneAmber
class node_n_modelhandler,node_n_fmu,node_n_sklearn,node_n_outputs toneMint
class node_n_examples,node_n_fmu_asset,node_n_in_csv,node_n_out_csv toneRose
```

## Components

### Core runtime

Online orchestration and execution boundary.

- [Experiment](https://github.com/donkkis/modelconductor/blob/master/modelconductor/experiment.py): orchestrator · Coordinates the online loop, routing, timing, and threaded execution for co-simulation. · `modelconductor/experiment.py`
- [Routing](https://github.com/donkkis/modelconductor/blob/master/modelconductor/experiment.py): mapping layer · Maps measurement fields into model inputs, control signals, and timestamps. · `modelconductor/experiment.py`
- [Runtime control](https://github.com/donkkis/modelconductor/blob/master/modelconductor/experiment.py): loop control · Controls run/stop timing and step progression for the online experiment. · `modelconductor/experiment.py`
- [Server](https://github.com/donkkis/modelconductor/blob/master/modelconductor/server.py): optional service · Optional network-facing entry point for exposing the library over a service boundary. · `modelconductor/server.py`
- [Utils](https://github.com/donkkis/modelconductor/blob/master/modelconductor/utils.py): shared helpers · Cross-cutting helper functions used across the package. · `modelconductor/utils.py`
- [Exceptions](https://github.com/donkkis/modelconductor/blob/master/modelconductor/exceptions.py): shared errors · Package-level exception types for validation and failure handling. · `modelconductor/exceptions.py`

### I/O boundaries

Incoming measurements and output logging.

- **Measurement DB**: external source · Persistent store that receives time-series measurements from outside the library.
- [Poller](https://github.com/donkkis/modelconductor/blob/master/modelconductor/measurementhandler.py): measurement handler · Reads new measurement rows from a database URI at a polling interval. · `modelconductor/measurementhandler.py`
- [CSV log](https://github.com/donkkis/modelconductor/blob/master/modelconductor/experiment.py): output sink · Optional logging of experiment outputs to CSV. · `modelconductor/experiment.py`

### Model support

Supported model abstractions and lifecycle handling.

- [Model handler](https://github.com/donkkis/modelconductor/blob/master/modelconductor/modelhandler.py): model abstraction · Unified interface for stepping and managing supported model backends. · `modelconductor/modelhandler.py`
- [FMU model](https://github.com/donkkis/modelconductor/blob/master/modelconductor/modelhandler.py): simulation backend · Handler path for FMU-based co-simulation models. · `modelconductor/modelhandler.py`
- [Sklearn model](https://github.com/donkkis/modelconductor/blob/master/modelconductor/modelhandler.py): ml backend · Handler path for machine-learning model execution. · `modelconductor/modelhandler.py`
- **Model outputs**: signal flow · Emitted model results that feed back into the experiment flow and logs.

### Examples and assets

Sample experiments and bundled resources.

- [Examples](https://github.com/donkkis/modelconductor/tree/master/modelconductor/examples): demo assets · Notebook-driven demonstration setup for the online co-simulation workflow. · `modelconductor/examples`
- [CoupledClutches.fmu](https://github.com/donkkis/modelconductor/blob/master/modelconductor/examples/CoupledClutches.fmu): sample asset · Bundled FMU used by the example experiment. · `modelconductor/examples/CoupledClutches.fmu`
- [Input CSV](https://github.com/donkkis/modelconductor/blob/master/modelconductor/examples/CoupledClutches_in_sh.csv): sample data · Example input table used by the notebook workflow. · `modelconductor/examples/CoupledClutches_in_sh.csv`
- [Output CSV](https://github.com/donkkis/modelconductor/blob/master/modelconductor/examples/out.csv): sample data · Example output capture generated by the experiment. · `modelconductor/examples/out.csv`

## Connections

- Measurement DB → Poller: rows polled · External measurements are written to a database and then read by the poller.
- Poller → Experiment: measurements · New measurements are handed into the online experiment loop.
- Experiment → Routing: map fields · The orchestrator maps raw measurement keys into model variables.
- Experiment → Runtime control: control loop · Experiment timing and stop conditions govern the online execution loop.
- Routing → Model handler: inputs · Mapped inputs and control values are passed to the model layer.
- Runtime control → Model handler: step · Runtime control triggers stepping and advancement of the model.
- Model handler → FMU model: backend · One supported execution path is FMU-based simulation.
- Model handler → Sklearn model: backend · Another supported execution path is sklearn model inference or prediction.
- FMU model → Model outputs: emit · FMU execution produces outputs for the online loop.
- Sklearn model → Model outputs: emit · Sklearn execution produces outputs for the online loop.
- Model outputs → Experiment: feedback · Model outputs flow back into the orchestrated experiment flow.
- Experiment → CSV log: optional log · Outputs can be persisted to CSV for later inspection.
- Examples → Experiment: demonstrates · The notebooks show how to assemble and run the online experiment.
- Examples → CoupledClutches.fmu: uses · Example notebooks use the bundled FMU sample.
- Examples → Input CSV: uses · Example notebooks use sample input CSV data.
- Examples → Output CSV: produces · Example notebooks show output capture.
- Server → Experiment: exposes · Optional service boundary can wrap or expose experiment control.
- Utils → Poller: helpers · Shared helpers support measurement handling.
- Utils → Model handler: helpers · Shared helpers support model-side conversion and validation.
- Exceptions → Experiment: errors · Experiment-level failures use shared exception types.
- Exceptions → Model handler: errors · Model lifecycle and mapping failures use shared exception types.
