# woolen-yarn/kohdalab-trkr architecture

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

- Interactive diagram: https://gitdiagram.com/woolen-yarn/kohdalab-trkr
- Repository: https://github.com/woolen-yarn/kohdalab-trkr

## Overview

#### Purpose and entry points
KohdaLab TRKR is a Python 3.13+ laboratory-control toolkit for time-resolved Kerr rotation measurement workflows: Signal Monitor, TRKR, SRKR, STRKR, and SRKR 2D. It exposes a typed automation API, a CLI, and an optional Qt GUI. Primary entry modules are `src/kohdalab/api/cli.py` and `src/kohdalab/apps/trkr_gui.py`; desktop launch support exists at `desktop/KohdaLab TRKR.vbs`.

#### Public experiment orchestration
The API layer defines experiment-facing models, scan plans, measurement generation, session ownership, status, and provenance. Experiment code should compose these APIs rather than talk directly to drivers. Central orchestration paths are:
- `src/kohdalab/api/experiment.py`
- `src/kohdalab/api/session.py`
- `src/kohdalab/api/measurements.py`

A session controls hardware lifecycle and per-device access boundaries. Planning and target validation occur before hardware sessions are opened, enabling rejection of invalid scans without instrument side effects.

#### Configuration, planning, and preflight
Configuration is loaded and interpreted into typed models, device requirements, scan constraints, and executable scan plans. Default settings are packaged in both repository and package-resource form:
- `config/default.json`
- `src/kohdalab/resources/default.json`
- `src/kohdalab/api/config.py`

The planning boundary is implemented through `src/kohdalab/api/scan_plan.py`, `src/kohdalab/api/scan_limits.py`, and `src/kohdalab/api/device_requirements.py`. This layer translates user/configured coordinates and measurement parameters into validated targets and required devices, separating configuration errors from physical I/O.

#### Device-facing API and abstraction boundary
Device APIs normalize delay-stage, lock-in, and scanner operations for experiments and applications. They sit between orchestration and hardware-specific drivers:
- `src/kohdalab/api/devices/delay_stage.py`
- `src/kohdalab/api/devices/lockin.py`
- `src/kohdalab/api/devices/scanner.py`

Shared protocols and interfaces establish the expected behavior and allow simulated transports in tests, decoupling callers from vendor controllers:
- `src/kohdalab/interfaces/protocols.py`
- `src/kohdalab/interfaces/delay_stage.py`
- `src/kohdalab/interfaces/lockin.py`

#### Instrument implementations
Concrete driver implementations target lab controllers for motion, signal acquisition, and scanning. Drivers are grouped by physical subsystem and carry controller-specific transport/validation behavior:
- Delay stages: `src/kohdalab/instruments/delay_stage/gsc01.py`, `src/kohdalab/instruments/delay_stage/gsc01a.py`, `src/kohdalab/instruments/delay_stage/shot302.py`
- Lock-ins: `src/kohdalab/instruments/lockin/sr830.py`, `src/kohdalab/instruments/lockin/sr7265.py`, `src/kohdalab/instruments/lockin/li5640.py`
- Scanners: `src/kohdalab/instruments/scanner/conexagap.py`, `src/kohdalab/instruments/scanner/conexcc.py`

Controller capability metadata is externalized in `stages.toml` and `actuator.toml`, keeping supported hardware definitions distinct from orchestration logic.

#### Measurement output and reproducibility
Measurement rows are constructed separately from execution, then written with run metadata/provenance. UTC timestamps and measurement CSV-associated metadata are first-class outputs:
- `src/kohdalab/api/measurement_rows.py`
- `src/kohdalab/api/run_metadata.py`
- `src/kohdalab/api/models.py`

Conversion and notebook helpers support downstream analysis without making Jupyter a base-runtime dependency: `src/kohdalab/api/conversion.py` and `src/kohdalab/api/notebook.py`.

#### GUI workflow runtime
The optional GUI is a specialized operator client over the API/device layer, not the hardware implementation itself. It divides configuration, device connection, measurement control, live signals, plotting, output, snapshots, and worker execution:
- `src/kohdalab/apps/trkr_gui.py`
- `src/kohdalab/apps/trkr_gui_measurement.py`
- `src/kohdalab/apps/trkr_gui_workers.py`

Qt/Jupyter are optional extras; base installations support headless API and CLI operation. Hardware operation depends on attached instrument controllers, while simulated transports permit automated driver verification without physical devices.

Main entry points: [`src/kohdalab/api/experiment.py`](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/experiment.py) (Experiment API), [`src/kohdalab/api/measurements.py`](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/measurements.py) (Measurement Generation), [`src/kohdalab/interfaces/protocols.py`](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/interfaces/protocols.py) (Protocols & Interfaces), [`src/kohdalab/api/session.py`](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/session.py) (Hardware Session)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry Points"]
  node_cli["CLI<br/>entry module<br/>[cli.py]"]
end

subgraph group_api["Experiment API"]
  node_experiment["Experiment API<br/>orchestration<br/>[experiment.py]"]
  node_session["Hardware Session<br/>lifecycle manager<br/>[session.py]"]
  node_measurements["Measurement Generation<br/>measurement API<br/>[measurements.py]"]
  node_config["Typed Configuration<br/>[config.py]"]
  node_defaults["Default Settings<br/>package resource<br/>[default.json]"]
  node_planning["Scan Planning<br/>preflight<br/>[scan_plan.py]"]
  node_requirements["Device Requirements<br/>preflight"]
end

subgraph group_hardware["Hardware Stack"]
  node_device_api["Device APIs<br/>abstraction layer<br/>[delay_stage.py]"]
  node_interfaces["Protocols &amp; Interfaces<br/>contracts<br/>[protocols.py]"]
  node_stage_drivers["Delay Stage Drivers<br/>instrument drivers<br/>[gsc01.py]"]
  node_lockin_drivers["Lock-in Drivers<br/>instrument drivers<br/>[sr830.py]"]
  node_scanner_drivers["Scanner Drivers<br/>instrument drivers<br/>[conexagap.py]"]
  node_capabilities["Controller Capabilities<br/>hardware metadata<br/>[stages.toml]"]
end

subgraph group_output["Outputs"]
  node_rows["Measurement Rows<br/>output builder"]
  node_provenance["Run Metadata<br/>provenance<br/>[run_metadata.py]"]
  node_analysis["Conversion &amp; Notebooks<br/>analysis helpers<br/>[conversion.py]"]
end

subgraph group_gui["Optional GUI"]
  node_gui_app["TRKR GUI<br/>Qt operator client<br/>[trkr_gui.py]"]
  node_gui_measurement["GUI Measurement Control<br/>GUI workflow"]
  node_gui_workers["GUI Workers<br/>background execution"]
end

node_cli -->|"invokes"| node_experiment
node_config -->|"loads defaults"| node_defaults
node_experiment -->|"uses"| node_config
node_experiment -->|"plans scan"| node_planning
node_planning -->|"derives devices"| node_requirements
node_planning -->|"validated plan opens"| node_session
node_experiment -->|"generates"| node_measurements
node_measurements -->|"executes through"| node_session
node_session -->|"accesses"| node_device_api
node_device_api -->|"implements contracts"| node_interfaces
node_interfaces -->|"implemented by"| node_stage_drivers
node_interfaces -->|"implemented by"| node_lockin_drivers
node_interfaces -->|"implemented by"| node_scanner_drivers
node_capabilities -->|"defines support"| node_stage_drivers
node_measurements -->|"builds"| node_rows
node_measurements -->|"records"| node_provenance
node_rows -->|"converted for"| node_analysis
node_gui_app -->|"hosts"| node_gui_measurement
node_gui_measurement -->|"runs via"| node_gui_workers
node_gui_measurement -->|"uses API"| node_experiment
node_gui_workers -->|"controls"| node_session

click node_cli "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/cli.py"
click node_experiment "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/experiment.py"
click node_session "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/session.py"
click node_measurements "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/measurements.py"
click node_config "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/config.py"
click node_defaults "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/resources/default.json"
click node_planning "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/scan_plan.py"
click node_requirements "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/device_requirements.py"
click node_device_api "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/devices/delay_stage.py"
click node_interfaces "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/interfaces/protocols.py"
click node_stage_drivers "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/instruments/delay_stage/gsc01.py"
click node_lockin_drivers "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/instruments/lockin/sr830.py"
click node_scanner_drivers "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/instruments/scanner/conexagap.py"
click node_capabilities "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/instruments/delay_stage/stages.toml"
click node_rows "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/measurement_rows.py"
click node_provenance "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/run_metadata.py"
click node_analysis "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/conversion.py"
click node_gui_app "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/apps/trkr_gui.py"
click node_gui_measurement "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/apps/trkr_gui_measurement.py"
click node_gui_workers "https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/apps/trkr_gui_workers.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_cli toneBlue
class node_experiment,node_session,node_measurements,node_config,node_defaults,node_planning,node_requirements toneAmber
class node_device_api,node_interfaces,node_stage_drivers,node_lockin_drivers,node_scanner_drivers,node_capabilities toneMint
class node_rows,node_provenance,node_analysis toneRose
class node_gui_app,node_gui_measurement,node_gui_workers toneIndigo
```

## Components

### Entry Points

- [CLI](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/cli.py): entry module · `src/kohdalab/api/cli.py`

### Experiment API

- [Experiment API](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/experiment.py): orchestration · Public composition boundary for measurement workflows. · `src/kohdalab/api/experiment.py`
- [Hardware Session](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/session.py): lifecycle manager · Owns hardware lifecycle and per-device access. · `src/kohdalab/api/session.py`
- [Measurement Generation](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/measurements.py): measurement API · `src/kohdalab/api/measurements.py`
- [Typed Configuration](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/config.py): configuration · Loads settings into experiment and device models. · `src/kohdalab/api/config.py`
- [Default Settings](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/resources/default.json): package resource · `src/kohdalab/resources/default.json`
- [Scan Planning](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/scan_plan.py): preflight · Validates targets and derives executable plans before I/O. · `src/kohdalab/api/scan_plan.py`
- [Device Requirements](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/device_requirements.py): preflight · `src/kohdalab/api/device_requirements.py`

### Hardware Stack

- [Device APIs](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/devices/delay_stage.py): abstraction layer · Normalizes stage, lock-in, and scanner operations. · `src/kohdalab/api/devices/delay_stage.py`
- [Protocols & Interfaces](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/interfaces/protocols.py): contracts · Decouples callers and simulated transports from controllers. · `src/kohdalab/interfaces/protocols.py`
- [Delay Stage Drivers](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/instruments/delay_stage/gsc01.py): instrument drivers · GSC01, GSC01A, and SHOT302 controllers. · `src/kohdalab/instruments/delay_stage/gsc01.py`
- [Lock-in Drivers](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/instruments/lockin/sr830.py): instrument drivers · SR830, SR7265, and LI5640 controllers. · `src/kohdalab/instruments/lockin/sr830.py`
- [Scanner Drivers](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/instruments/scanner/conexagap.py): instrument drivers · CONEX-AGAP and CONEX-CC controllers. · `src/kohdalab/instruments/scanner/conexagap.py`
- [Controller Capabilities](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/instruments/delay_stage/stages.toml): hardware metadata · External definitions for supported stages and actuators. · `src/kohdalab/instruments/delay_stage/stages.toml`

### Outputs

- [Measurement Rows](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/measurement_rows.py): output builder · Constructs rows independently from execution. · `src/kohdalab/api/measurement_rows.py`
- [Run Metadata](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/run_metadata.py): provenance · Captures UTC timestamps and run metadata. · `src/kohdalab/api/run_metadata.py`
- [Conversion & Notebooks](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/api/conversion.py): analysis helpers · Optional downstream conversion and notebook support. · `src/kohdalab/api/conversion.py`

### Optional GUI

- [TRKR GUI](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/apps/trkr_gui.py): Qt operator client · Optional GUI over the API and device layers. · `src/kohdalab/apps/trkr_gui.py`
- [GUI Measurement Control](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/apps/trkr_gui_measurement.py): GUI workflow · `src/kohdalab/apps/trkr_gui_measurement.py`
- [GUI Workers](https://github.com/woolen-yarn/kohdalab-trkr/blob/main/src/kohdalab/apps/trkr_gui_workers.py): background execution · `src/kohdalab/apps/trkr_gui_workers.py`

## Connections

- CLI → Experiment API: invokes
- Typed Configuration → Default Settings: loads defaults
- Experiment API → Typed Configuration: uses
- Experiment API → Scan Planning: plans scan
- Scan Planning → Device Requirements: derives devices
- Scan Planning → Hardware Session: validated plan opens
- Experiment API → Measurement Generation: generates
- Measurement Generation → Hardware Session: executes through
- Hardware Session → Device APIs: accesses
- Device APIs → Protocols & Interfaces: implements contracts
- Protocols & Interfaces → Delay Stage Drivers: implemented by
- Protocols & Interfaces → Lock-in Drivers: implemented by
- Protocols & Interfaces → Scanner Drivers: implemented by
- Controller Capabilities → Delay Stage Drivers: defines support
- Measurement Generation → Measurement Rows: builds
- Measurement Generation → Run Metadata: records
- Measurement Rows → Conversion & Notebooks: converted for
- TRKR GUI → GUI Measurement Control: hosts
- GUI Measurement Control → GUI Workers: runs via
- GUI Measurement Control → Experiment API: uses API
- GUI Workers → Hardware Session: controls
