How picspin/mrqlab works
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: runner.py Engine runner · main.py FastAPI application · protocol.py Backend protocol · WorkspaceProvider.tsx Shared workspace state
Components 21
Interactive workspace
- Explore, Editor & Signal Lab
apps/web/app/page.tsx - Shared workspace state
apps/web/components/workspace/WorkspaceProvider.tsx - API client · Falls back to offline status when the API is unavailable.
apps/web/lib/api.ts
Application boundary
- FastAPI application · Serves validation, execution, presets, engine discovery, health, and legacy simulation.
services/api/mrqlab_api/main.py
Experiment compilation
- Experiment kernel · Owns validation, capabilities, execution semantics, disturbances, and objectives.
packages/mrqlab_experiment/mrqlab_experiment/kernel.py - Experiment graph · Canonical experiment intent and result contract.
packages/mrqlab_experiment/mrqlab_experiment/models.py - Experiment presets
packages/mrqlab_experiment/mrqlab_experiment/presets.py - Three-layer compiler · Translates experiment intent into physics-oriented IR.
packages/mrqlab_experiment/mrqlab_experiment/compiler.py - Physics IR
packages/mrqlab_experiment/mrqlab_experiment/physics_ir.py - Sequence event graph · Shared RF, gradient, ADC, and oscillator event models.
packages/sequence-ir/mrqlab_sequence/models.py - SE, TSE & GRE templates
packages/sequence-ir/mrqlab_sequence/templates.py - Pulse construction
packages/mrqlab_experiment/mrqlab_experiment/pulse.py - Observations & results · ADC-gated samples and execution results.
packages/mrqlab_experiment/mrqlab_experiment/observations.py
Physics microkernel
- Engine runner · Schedules events, enforces capabilities and work caps, handles ADC/NCO, and assembles results.
packages/physics/mrqlab_physics/kernel/runner.py - Engine registry · Selects Bloch, EPG, or spectral execution from request or template metadata.
packages/physics/mrqlab_physics/registry.py - Backend protocol · Supports additional engines without changing public contracts.
packages/physics/mrqlab_physics/backends/protocol.py - Bloch engine · Primary SE and GRE state simulation.
packages/physics/mrqlab_physics/engines/bloch_engine.py - EPG engine · Primary TSE state simulation.
packages/physics/mrqlab_physics/engines/epg_engine.py - Physics operators · RF, relaxation, diffusion, and sampling operations.
Other components
- Reconstruction adapter · Reconstructs Cartesian data; NUFFT is an extension seam.
packages/recon/mrqlab_recon/__init__.py - Architecture & physics contracts · Documents modular-monolith boundaries and simulator limitations.
docs/ARCHITECTURE.md
Connections 26
- 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