# picspin/mrqlab architecture

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

- Interactive diagram: https://gitdiagram.com/picspin/mrqlab
- Repository: https://github.com/picspin/mrqlab

## Overview

#### Purpose and entry points
MRQLab is a teaching MRI simulator that turns time-dependent sequence events into observations and reconstructed signals/images. It is a modular monolith: Python provides the experiment/physics application stack and FastAPI boundary; Next.js provides an interactive workspace.
- API application: `services/api/mrqlab_api/main.py`
- Web routes: `apps/web/app/page.tsx`, `apps/web/app/editor/page.tsx`, `apps/web/app/signal-lab/page.tsx`
- Package/runtime definition: `pyproject.toml`, `apps/web/package.json`

#### HTTP application boundary
FastAPI exposes canonical experiment validation and execution (`/experiments/validate`, `/experiments/run`), presets, sequence building, engine discovery, health, and legacy `/simulate`. This is the boundary between browser/client payloads and typed Python domain models; compatibility simulation is adapted into the same experiment application flow.
- `services/api/mrqlab_api/main.py`
- `packages/mrqlab_experiment/mrqlab_experiment/kernel.py`
- `packages/mrqlab_experiment/mrqlab_experiment/presets.py`

#### Experiment kernel and three-layer compilation
The product center is an `ExperimentGraph`. The experiment package owns graph validation, capability checks, compile/execution semantics, disturbances, objectives, and typed observations/results. Compilation bridges clinical/experiment intent into physics-oriented IR rather than exposing backend-specific details to clients.
- `packages/mrqlab_experiment/mrqlab_experiment/models.py`
- `packages/mrqlab_experiment/mrqlab_experiment/compiler.py`
- `packages/mrqlab_experiment/mrqlab_experiment/physics_ir.py`

#### Sequence IR and event construction
Shared Pydantic sequence models represent the MR event graph, while template builders construct standard SE, TSE, and GRE-style sequences. The compiler converts these channel-specific RF, gradient, ADC, and oscillator events into the representation required for simulation.
- `packages/sequence-ir/mrqlab_sequence/models.py`
- `packages/sequence-ir/mrqlab_sequence/templates.py`
- `packages/mrqlab_experiment/mrqlab_experiment/pulse.py`

#### Physics microkernel
A kernel-owned engine façade selects and runs shipped NumPy-first physics engines. The registry/protocol boundary permits backend descriptors while retaining scheduling, capability enforcement, conventions, work caps, ADC/NCO handling, and result assembly inside the microkernel. Engine choice is explicit or inferred from template metadata: Bloch for SE/GRE, EPG for TSE, and spectral for requested pools.
- `packages/physics/mrqlab_physics/kernel/runner.py`
- `packages/physics/mrqlab_physics/registry.py`
- `packages/physics/mrqlab_physics/backends/protocol.py`

#### Simulation representations and operators
Physics separates state representations/backends from reusable operations. Bloch, EPG, and spectral engines apply RF, relaxation, diffusion, and sampling operations over scheduled events; optional PDG/EPG-X seams support additional backend behavior without changing public sequence or HTTP contracts.
- `packages/physics/mrqlab_physics/engines/bloch_engine.py`
- `packages/physics/mrqlab_physics/engines/epg_engine.py`
- `packages/physics/mrqlab_physics/ops/`

#### Results, reconstruction, and UI consumption
Execution samples simulated states under ADC gates into typed observations/result graphs. Cartesian data then crosses a reconstruction adapter, with a declared NUFFT extension seam. The web workspace calls the API, manages shared experiment state, and presents Explore/Editor/Signal Lab views; it degrades to offline status if the API is unavailable.
- `packages/mrqlab_experiment/mrqlab_experiment/observations.py`
- `packages/recon/mrqlab_recon/__init__.py`
- `apps/web/components/workspace/WorkspaceProvider.tsx`
- `apps/web/lib/api.ts`

#### Operational and safety boundaries
The supported runtimes are Python 3.11+ and Node 20+. The system has no cloud, secrets, scanner-control, acquisition, or hardware integration; it is explicitly a simulation/teaching tool. Architectural contracts and limitations are maintained in ADRs and physics documentation.
- `docs/ARCHITECTURE.md`
- `docs/PHYSICS.md`
- `docs/adr/ADR-0005-modular-monolith.md`

Main entry points: [`packages/physics/mrqlab_physics/kernel/runner.py`](https://github.com/picspin/mrqlab/blob/main/packages/physics/mrqlab_physics/kernel/runner.py) (Engine runner), [`services/api/mrqlab_api/main.py`](https://github.com/picspin/mrqlab/blob/main/services/api/mrqlab_api/main.py) (FastAPI application), [`packages/physics/mrqlab_physics/backends/protocol.py`](https://github.com/picspin/mrqlab/blob/main/packages/physics/mrqlab_physics/backends/protocol.py) (Backend protocol), [`apps/web/components/workspace/WorkspaceProvider.tsx`](https://github.com/picspin/mrqlab/blob/main/apps/web/components/workspace/WorkspaceProvider.tsx) (Shared workspace state)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Interactive workspace"]
  node_web_routes["Explore, Editor &amp; Signal Lab<br/>Next.js routes<br/>[page.tsx]"]
  node_workspace["Shared workspace state<br/>React provider"]
  node_web_api_client["API client<br/>browser adapter<br/>[api.ts]"]
end

subgraph group_application["Application boundary"]
  node_api{{"FastAPI application<br/>HTTP boundary<br/>[main.py]"}}
end

subgraph group_experiment["Experiment compilation"]
  node_experiment_kernel["Experiment kernel<br/>application service<br/>[kernel.py]"]
  node_experiment_graph["Experiment graph<br/>typed domain model<br/>[models.py]"]
  node_presets["Experiment presets<br/>preset catalog<br/>[presets.py]"]
  node_compiler["Three-layer compiler<br/>experiment compiler<br/>[compiler.py]"]
  node_physics_ir["Physics IR<br/>simulation IR<br/>[physics_ir.py]"]
  node_sequence_ir["Sequence event graph<br/>Pydantic sequence IR<br/>[models.py]"]
  node_sequence_templates["SE, TSE &amp; GRE templates<br/>sequence builders<br/>[templates.py]"]
  node_pulse_builder["Pulse construction<br/>event adapter<br/>[pulse.py]"]
  node_observations["Observations &amp; results<br/>typed result graph<br/>[observations.py]"]
end

subgraph group_physics["Physics microkernel"]
  node_physics_runner["Engine runner<br/>kernel façade<br/>[runner.py]"]
  node_engine_registry["Engine registry<br/>backend selection<br/>[registry.py]"]
  node_backend_protocol["Backend protocol<br/>extension contract<br/>[protocol.py]"]
  node_bloch_engine["Bloch engine<br/>NumPy physics backend<br/>[bloch_engine.py]"]
  node_epg_engine["EPG engine<br/>NumPy physics backend<br/>[epg_engine.py]"]
  node_physics_ops["Physics operators<br/>reusable operations"]
end

node_reconstruction["Reconstruction adapter<br/>Cartesian recon<br/>[__init__.py]"]
node_architecture_docs["Architecture &amp; physics contracts<br/>documentation<br/>[ARCHITECTURE.md]"]

node_web_routes -->|"uses shared state"| node_workspace
node_workspace -->|"requests simulation"| node_web_api_client
node_web_api_client -->|"HTTP"| node_api
node_api -->|"validates and runs"| node_experiment_kernel
node_api -->|"serves presets"| node_presets
node_experiment_kernel -->|"validates intent"| node_experiment_graph
node_presets -->|"creates"| node_experiment_graph
node_sequence_templates -->|"builds"| node_sequence_ir
node_pulse_builder -->|"constructs events"| node_sequence_ir
node_experiment_graph -->|"compiles"| node_compiler
node_sequence_ir -->|"provides event graph"| node_compiler
node_compiler -->|"emits"| node_physics_ir
node_physics_ir -->|"executes"| node_physics_runner
node_physics_runner -->|"selects engine"| node_engine_registry
node_engine_registry -->|"describes backends"| node_backend_protocol
node_backend_protocol -.->|"implemented by"| node_bloch_engine
node_backend_protocol -.->|"implemented by"| node_epg_engine
node_physics_runner -->|"runs SE/GRE"| node_bloch_engine
node_physics_runner -->|"runs TSE"| node_epg_engine
node_bloch_engine -->|"applies"| node_physics_ops
node_epg_engine -->|"applies"| node_physics_ops
node_physics_runner -->|"samples and assembles"| node_observations
node_observations -->|"Cartesian data"| node_reconstruction
node_reconstruction -->|"signals/images"| node_workspace
node_architecture_docs -.->|"documents boundary"| node_api
node_architecture_docs -.->|"documents conventions"| node_physics_runner

click node_web_routes "https://github.com/picspin/mrqlab/blob/main/apps/web/app/page.tsx"
click node_workspace "https://github.com/picspin/mrqlab/blob/main/apps/web/components/workspace/WorkspaceProvider.tsx"
click node_web_api_client "https://github.com/picspin/mrqlab/blob/main/apps/web/lib/api.ts"
click node_api "https://github.com/picspin/mrqlab/blob/main/services/api/mrqlab_api/main.py"
click node_experiment_kernel "https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/kernel.py"
click node_experiment_graph "https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/models.py"
click node_presets "https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/presets.py"
click node_compiler "https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/compiler.py"
click node_physics_ir "https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/physics_ir.py"
click node_sequence_ir "https://github.com/picspin/mrqlab/blob/main/packages/sequence-ir/mrqlab_sequence/models.py"
click node_sequence_templates "https://github.com/picspin/mrqlab/blob/main/packages/sequence-ir/mrqlab_sequence/templates.py"
click node_pulse_builder "https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/pulse.py"
click node_physics_runner "https://github.com/picspin/mrqlab/blob/main/packages/physics/mrqlab_physics/kernel/runner.py"
click node_engine_registry "https://github.com/picspin/mrqlab/blob/main/packages/physics/mrqlab_physics/registry.py"
click node_backend_protocol "https://github.com/picspin/mrqlab/blob/main/packages/physics/mrqlab_physics/backends/protocol.py"
click node_bloch_engine "https://github.com/picspin/mrqlab/blob/main/packages/physics/mrqlab_physics/engines/bloch_engine.py"
click node_epg_engine "https://github.com/picspin/mrqlab/blob/main/packages/physics/mrqlab_physics/engines/epg_engine.py"
click node_observations "https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/observations.py"
click node_reconstruction "https://github.com/picspin/mrqlab/blob/main/packages/recon/mrqlab_recon/__init__.py"
click node_architecture_docs "https://github.com/picspin/mrqlab/blob/main/docs/ARCHITECTURE.md"

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_web_routes,node_workspace,node_web_api_client toneBlue
class node_api toneAmber
class node_experiment_kernel,node_experiment_graph,node_presets,node_compiler,node_physics_ir,node_sequence_ir,node_sequence_templates,node_pulse_builder,node_observations toneMint
class node_physics_runner,node_engine_registry,node_backend_protocol,node_bloch_engine,node_epg_engine,node_physics_ops toneRose
class node_reconstruction,node_architecture_docs toneNeutral
```

## Components

### Interactive workspace

- [Explore, Editor & Signal Lab](https://github.com/picspin/mrqlab/blob/main/apps/web/app/page.tsx): Next.js routes · `apps/web/app/page.tsx`
- [Shared workspace state](https://github.com/picspin/mrqlab/blob/main/apps/web/components/workspace/WorkspaceProvider.tsx): React provider · `apps/web/components/workspace/WorkspaceProvider.tsx`
- [API client](https://github.com/picspin/mrqlab/blob/main/apps/web/lib/api.ts): browser adapter · Falls back to offline status when the API is unavailable. · `apps/web/lib/api.ts`

### Application boundary

- [FastAPI application](https://github.com/picspin/mrqlab/blob/main/services/api/mrqlab_api/main.py): HTTP boundary · Serves validation, execution, presets, engine discovery, health, and legacy simulation. · `services/api/mrqlab_api/main.py`

### Experiment compilation

- [Experiment kernel](https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/kernel.py): application service · Owns validation, capabilities, execution semantics, disturbances, and objectives. · `packages/mrqlab_experiment/mrqlab_experiment/kernel.py`
- [Experiment graph](https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/models.py): typed domain model · Canonical experiment intent and result contract. · `packages/mrqlab_experiment/mrqlab_experiment/models.py`
- [Experiment presets](https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/presets.py): preset catalog · `packages/mrqlab_experiment/mrqlab_experiment/presets.py`
- [Three-layer compiler](https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/compiler.py): experiment compiler · Translates experiment intent into physics-oriented IR. · `packages/mrqlab_experiment/mrqlab_experiment/compiler.py`
- [Physics IR](https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/physics_ir.py): simulation IR · `packages/mrqlab_experiment/mrqlab_experiment/physics_ir.py`
- [Sequence event graph](https://github.com/picspin/mrqlab/blob/main/packages/sequence-ir/mrqlab_sequence/models.py): Pydantic sequence IR · Shared RF, gradient, ADC, and oscillator event models. · `packages/sequence-ir/mrqlab_sequence/models.py`
- [SE, TSE & GRE templates](https://github.com/picspin/mrqlab/blob/main/packages/sequence-ir/mrqlab_sequence/templates.py): sequence builders · `packages/sequence-ir/mrqlab_sequence/templates.py`
- [Pulse construction](https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/pulse.py): event adapter · `packages/mrqlab_experiment/mrqlab_experiment/pulse.py`
- [Observations & results](https://github.com/picspin/mrqlab/blob/main/packages/mrqlab_experiment/mrqlab_experiment/observations.py): typed result graph · ADC-gated samples and execution results. · `packages/mrqlab_experiment/mrqlab_experiment/observations.py`

### Physics microkernel

- [Engine runner](https://github.com/picspin/mrqlab/blob/main/packages/physics/mrqlab_physics/kernel/runner.py): kernel façade · Schedules events, enforces capabilities and work caps, handles ADC/NCO, and assembles results. · `packages/physics/mrqlab_physics/kernel/runner.py`
- [Engine registry](https://github.com/picspin/mrqlab/blob/main/packages/physics/mrqlab_physics/registry.py): backend selection · Selects Bloch, EPG, or spectral execution from request or template metadata. · `packages/physics/mrqlab_physics/registry.py`
- [Backend protocol](https://github.com/picspin/mrqlab/blob/main/packages/physics/mrqlab_physics/backends/protocol.py): extension contract · Supports additional engines without changing public contracts. · `packages/physics/mrqlab_physics/backends/protocol.py`
- [Bloch engine](https://github.com/picspin/mrqlab/blob/main/packages/physics/mrqlab_physics/engines/bloch_engine.py): NumPy physics backend · Primary SE and GRE state simulation. · `packages/physics/mrqlab_physics/engines/bloch_engine.py`
- [EPG engine](https://github.com/picspin/mrqlab/blob/main/packages/physics/mrqlab_physics/engines/epg_engine.py): NumPy physics backend · Primary TSE state simulation. · `packages/physics/mrqlab_physics/engines/epg_engine.py`
- **Physics operators**: reusable operations · RF, relaxation, diffusion, and sampling operations.

### Other components

- [Reconstruction adapter](https://github.com/picspin/mrqlab/blob/main/packages/recon/mrqlab_recon/__init__.py): Cartesian recon · Reconstructs Cartesian data; NUFFT is an extension seam. · `packages/recon/mrqlab_recon/__init__.py`
- [Architecture & physics contracts](https://github.com/picspin/mrqlab/blob/main/docs/ARCHITECTURE.md): documentation · Documents modular-monolith boundaries and simulator limitations. · `docs/ARCHITECTURE.md`

## Connections

- Explore, Editor & Signal Lab → Shared workspace state: uses shared state
- Shared workspace state → API client: requests simulation
- API client → FastAPI application: HTTP
- FastAPI application → Experiment kernel: validates and runs
- FastAPI application → Experiment presets: serves presets
- Experiment kernel → Experiment graph: validates intent
- Experiment presets → Experiment graph: creates
- SE, TSE & GRE templates → Sequence event graph: builds
- Pulse construction → Sequence event graph: constructs events
- Experiment graph → Three-layer compiler: compiles
- Sequence event graph → Three-layer compiler: provides event graph
- Three-layer compiler → Physics IR: emits
- Physics IR → Engine runner: executes
- Engine runner → Engine registry: selects engine
- Engine registry → Backend protocol: describes backends
- Backend protocol → Bloch engine: implemented by
- Backend protocol → EPG engine: implemented by
- Engine runner → Bloch engine: runs SE/GRE
- Engine runner → EPG engine: runs TSE
- Bloch engine → Physics operators: applies
- EPG engine → Physics operators: applies
- Engine runner → Observations & results: samples and assembles
- Observations & results → Reconstruction adapter: Cartesian data
- Reconstruction adapter → Shared workspace state: signals/images
- Architecture & physics contracts → FastAPI application: documents boundary
- Architecture & physics contracts → Engine runner: documents conventions
