# osmanttne/onn-backend architecture

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

- Interactive diagram: https://gitdiagram.com/osmanttne/onn-backend
- Repository: https://github.com/osmanttne/onn-backend

## Overview

#### Purpose and execution model
This is a notebook-first backend for an optical neural network (ONN) bench. Its core operation maps a temporal image batch `X (T,h,w)` through a DMD and optical path to camera measurements/predictions `Y (T,gh,gw)`. The main operator entry point is `onn_backend.ipynb`; it supports simulation and physical-bench execution. Supporting scripts include `onn_server.py` and benchmark utilities.

#### Orchestration and forward pipeline
`onn/forward.py` contains `ONNForward`, the central pipeline abstraction responsible for driving `X -> Y` and persisting results. It is expected to coordinate device setup, temporal DMD presentation, camera capture, and result saving. `onn_backend.ipynb` instantiates and runs this flow interactively; `data/run_sim.npz` is a representative persisted run.

Grounding paths:
- `onn/forward.py`
- `onn_backend.ipynb`
- `data/run_sim.npz`

#### Hardware abstraction boundary
`hardware/base.py` defines the shared device-layer boundary. Hardware-specific implementations are separated from ONN logic so the same forward flow can target real instruments or software twins. This isolates vendor SDKs, device connection details, lifecycle management, and low-level commands from pattern generation and measurement processing.

Grounding paths:
- `hardware/base.py`
- `hardware/__init__.py`
- `hardware/simulators.py`

#### Illumination and spatial modulation
The optical input path comprises laser control and spatial modulators. `hardware/laser.py` controls an OBIS laser through SCPI/serial-style communications. `hardware/dmd.py` drives a ViALUX DMD via ALP4lib and is the primary temporal input display device. `hardware/slm.py` manages a Meadowlark SLM through slmsuite, including a coverglass safety/lockout concern.

Grounding paths:
- `hardware/laser.py`
- `hardware/dmd.py`
- `hardware/slm.py`

#### Acquisition and measurement output
`hardware/camera.py` wraps FLIR camera operation through PySpin. The camera is the physical output boundary: it captures optical frames produced after DMD/SLM modulation and provides the raw measurements used to construct `Y`. Simulation counterparts in `hardware/simulators.py` permit this acquisition contract to run without attached devices.

Grounding paths:
- `hardware/camera.py`
- `hardware/simulators.py`
- `onn/forward.py`

#### Pattern and image representation layer
Named optical patterns for DMD and SLM are centralized in `onn/patterns.py`, keeping display content separate from device transport. `onn/tensor_bitmap.py` bridges numerical tensor/image data into bitmap-compatible representations required by modulator APIs. This is the main format boundary between model-style arrays and hardware display buffers.

Grounding paths:
- `onn/patterns.py`
- `onn/tensor_bitmap.py`
- `onn/forward.py`

#### Configuration, runtimes, and external dependencies
The bench profile is stored in `config/onn_nico.yaml`; physical deployment requires completing bench-specific parameters before disabling simulation. The runtime is Python/Jupyter with NumPy-based data handling and `.npz` run storage. Material external integrations are ALP4lib (ViALUX DMD), slmsuite (Meadowlark SLM), PySpin (FLIR camera), and pyserial/SCPI for laser communication. Simulation mode avoids these vendor dependencies.

Grounding paths:
- `config/onn_nico.yaml`
- `hardware/dmd.py`
- `hardware/camera.py`

Main entry points: [`onn/forward.py`](https://github.com/osmanttne/onn-backend/blob/main/onn/forward.py) (ONNForward), [`hardware/base.py`](https://github.com/osmanttne/onn-backend/blob/main/hardware/base.py) (Device contracts), [`hardware/dmd.py`](https://github.com/osmanttne/onn-backend/blob/main/hardware/dmd.py) (ViALUX DMD), [`hardware/slm.py`](https://github.com/osmanttne/onn-backend/blob/main/hardware/slm.py) (Meadowlark SLM)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_operator["Interactive runtime"]
  node_notebook["ONN backend notebook<br/>operator entry point<br/>[onn_backend.ipynb]"]
  node_config["Bench profile<br/>deployment config<br/>[onn_nico.yaml]"]
end

subgraph group_onn["ONN pipeline"]
  node_forward["ONNForward<br/>forward orchestrator<br/>[forward.py]"]
  node_patterns["Optical patterns<br/>pattern library<br/>[patterns.py]"]
  node_bitmap["Tensor bitmap bridge<br/>format adapter<br/>[tensor_bitmap.py]"]
  node_run_data[("Persisted run<br/>NPZ output<br/>[run_sim.npz]")]
end

subgraph group_hardware["Hardware boundary"]
  node_device_base{{"Device contracts<br/>hardware abstraction<br/>[base.py]"}}
  node_hardware_package["Hardware package<br/>device exports<br/>[__init__.py]"]
  node_simulators["Software twins<br/>simulation adapters<br/>[simulators.py]"]
end

subgraph group_physical["Physical bench adapters"]
  node_laser["OBIS laser<br/>laser adapter<br/>[laser.py]"]
  node_dmd["ViALUX DMD<br/>temporal modulator<br/>[dmd.py]"]
  node_slm["Meadowlark SLM<br/>spatial modulator<br/>[slm.py]"]
  node_camera["FLIR camera<br/>acquisition adapter<br/>[camera.py]"]
  node_optical_path(("Optical path<br/>physical propagation"))
  node_vendor_sdks["Vendor SDKs<br/>external integrations"]
end

node_notebook -->|"runs"| node_forward
node_config -->|"configures mode"| node_forward
node_forward -->|"selects"| node_patterns
node_patterns -->|"rendered as"| node_bitmap
node_forward -->|"converts X"| node_bitmap
node_forward -->|"uses contracts"| node_device_base
node_hardware_package -->|"exports"| node_device_base
node_simulators -.->|"implements"| node_device_base
node_laser -->|"implements"| node_device_base
node_dmd -->|"implements"| node_device_base
node_slm -->|"implements"| node_device_base
node_camera -->|"implements"| node_device_base
node_bitmap -->|"presents temporal X"| node_dmd
node_bitmap -->|"loads spatial pattern"| node_slm
node_laser -->|"illuminates"| node_optical_path
node_dmd -->|"modulates input"| node_optical_path
node_slm -->|"modulates field"| node_optical_path
node_optical_path -->|"produces frames"| node_camera
node_camera -->|"raw measurements Y"| node_forward
node_forward -->|"saves run"| node_run_data
node_dmd -.->|"ALP4lib"| node_vendor_sdks
node_slm -.->|"slmsuite"| node_vendor_sdks
node_camera -.->|"PySpin"| node_vendor_sdks
node_laser -.->|"SCPI/serial"| node_vendor_sdks

click node_notebook "https://github.com/osmanttne/onn-backend/blob/main/onn_backend.ipynb"
click node_forward "https://github.com/osmanttne/onn-backend/blob/main/onn/forward.py"
click node_patterns "https://github.com/osmanttne/onn-backend/blob/main/onn/patterns.py"
click node_bitmap "https://github.com/osmanttne/onn-backend/blob/main/onn/tensor_bitmap.py"
click node_run_data "https://github.com/osmanttne/onn-backend/blob/main/data/run_sim.npz"
click node_config "https://github.com/osmanttne/onn-backend/blob/main/config/onn_nico.yaml"
click node_device_base "https://github.com/osmanttne/onn-backend/blob/main/hardware/base.py"
click node_hardware_package "https://github.com/osmanttne/onn-backend/blob/main/hardware/__init__.py"
click node_simulators "https://github.com/osmanttne/onn-backend/blob/main/hardware/simulators.py"
click node_laser "https://github.com/osmanttne/onn-backend/blob/main/hardware/laser.py"
click node_dmd "https://github.com/osmanttne/onn-backend/blob/main/hardware/dmd.py"
click node_slm "https://github.com/osmanttne/onn-backend/blob/main/hardware/slm.py"
click node_camera "https://github.com/osmanttne/onn-backend/blob/main/hardware/camera.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_notebook,node_config toneBlue
class node_forward,node_patterns,node_bitmap,node_run_data toneAmber
class node_device_base,node_hardware_package,node_simulators toneMint
class node_laser,node_dmd,node_slm,node_camera,node_optical_path,node_vendor_sdks toneRose
```

## Components

### Interactive runtime

- [ONN backend notebook](https://github.com/osmanttne/onn-backend/blob/main/onn_backend.ipynb): operator entry point · Interactive launcher for simulation and bench runs. · `onn_backend.ipynb`
- [Bench profile](https://github.com/osmanttne/onn-backend/blob/main/config/onn_nico.yaml): deployment config · Bench-specific settings select and configure physical execution. · `config/onn_nico.yaml`

### ONN pipeline

- [ONNForward](https://github.com/osmanttne/onn-backend/blob/main/onn/forward.py): forward orchestrator · Drives temporal X-to-Y runs and persists results. · `onn/forward.py`
- [Optical patterns](https://github.com/osmanttne/onn-backend/blob/main/onn/patterns.py): pattern library · `onn/patterns.py`
- [Tensor bitmap bridge](https://github.com/osmanttne/onn-backend/blob/main/onn/tensor_bitmap.py): format adapter · Converts numerical image data to modulator buffers. · `onn/tensor_bitmap.py`
- [Persisted run](https://github.com/osmanttne/onn-backend/blob/main/data/run_sim.npz): NPZ output · Representative simulated X-to-Y run. · `data/run_sim.npz`

### Hardware boundary

- [Device contracts](https://github.com/osmanttne/onn-backend/blob/main/hardware/base.py): hardware abstraction · Separates ONN orchestration from device lifecycle and low-level commands. · `hardware/base.py`
- [Hardware package](https://github.com/osmanttne/onn-backend/blob/main/hardware/__init__.py): device exports · `hardware/__init__.py`
- [Software twins](https://github.com/osmanttne/onn-backend/blob/main/hardware/simulators.py): simulation adapters · Implement device and acquisition contracts without instruments. · `hardware/simulators.py`

### Physical bench adapters

- [OBIS laser](https://github.com/osmanttne/onn-backend/blob/main/hardware/laser.py): laser adapter · Controls illumination through SCPI or serial communication. · `hardware/laser.py`
- [ViALUX DMD](https://github.com/osmanttne/onn-backend/blob/main/hardware/dmd.py): temporal modulator · Displays temporal input patterns via ALP4lib. · `hardware/dmd.py`
- [Meadowlark SLM](https://github.com/osmanttne/onn-backend/blob/main/hardware/slm.py): spatial modulator · Applies SLM patterns and coverglass safety lockout via slmsuite. · `hardware/slm.py`
- [FLIR camera](https://github.com/osmanttne/onn-backend/blob/main/hardware/camera.py): acquisition adapter · Captures optical output frames through PySpin. · `hardware/camera.py`
- **Optical path**: physical propagation · Carries modulated illumination from input display to camera.
- **Vendor SDKs**: external integrations · ALP4lib, slmsuite, PySpin, and serial/SCPI interfaces.

## Connections

- ONN backend notebook → ONNForward: runs
- Bench profile → ONNForward: configures mode
- ONNForward → Optical patterns: selects
- Optical patterns → Tensor bitmap bridge: rendered as
- ONNForward → Tensor bitmap bridge: converts X
- ONNForward → Device contracts: uses contracts
- Hardware package → Device contracts: exports
- Software twins → Device contracts: implements
- OBIS laser → Device contracts: implements
- ViALUX DMD → Device contracts: implements
- Meadowlark SLM → Device contracts: implements
- FLIR camera → Device contracts: implements
- Tensor bitmap bridge → ViALUX DMD: presents temporal X
- Tensor bitmap bridge → Meadowlark SLM: loads spatial pattern
- OBIS laser → Optical path: illuminates
- ViALUX DMD → Optical path: modulates input
- Meadowlark SLM → Optical path: modulates field
- Optical path → FLIR camera: produces frames
- FLIR camera → ONNForward: raw measurements Y
- ONNForward → Persisted run: saves run
- ViALUX DMD → Vendor SDKs: ALP4lib
- Meadowlark SLM → Vendor SDKs: slmsuite
- FLIR camera → Vendor SDKs: PySpin
- OBIS laser → Vendor SDKs: SCPI/serial
