MedMatrix Workspace Architecture

Welcome to the developer workstation technical manual. KVISION is a modular clinical application designed for high-performance MRI acquisition preprocessing, State Space Model (SSM) anomaly detection, Fourier-domain reconstruction, and volumetric neural network pathology classification.

📦 Monorepo design The workspace uses a Monorepo design managed with pnpm workspaces, synchronizing a high-performance C++ ONNX inference library, a FastAPI deep learning pipeline, an Express.js job worker backend, and an Electron desktop framework.

Enhanced Folder Explorer

Click on any folder to toggle sub-directories, or click a file to load its clinical architecture details below:

📄 docker-compose.yml
DOCKER Orchestrates database, cache, and object storage
📄 package.json
CONFIG Monorepo scripts registry
📄 pnpm-workspace.yaml
CONFIG PNPM workspaces list
📁 ai-service/
Python AI Engine & FastAPI
📄 fused_model_128.onnx
ONNX Volumetric pathology weights
📄 anomaly_detector.onnx
ONNX SSM Anomaly Estimator weights
📄 main.py
PYTHON FastAPI controller application
📄 kspace_reader.py
PYTHON Siemens Twix & H5 Reader
📄 reconstruction.py
PYTHON 2D IFFT & RSS Combination
📁 inference/
C++ Inference Engine
📄 CMakeLists.txt
CMAKE CMake compiler definition
📄 kvision_inference.h
C++ HEADER Inference engine header mapping
📄 kvision_inference.cpp
C++ IMPL ONNX Runtime C++ implementation
📄 test_inference.cpp
C++ TEST Inference test CLI suite
📁 apps/
UI Frontend & API Backend
📁 electron/
Electron desktop app
📄 App.tsx
TSX / REACT Main desktop interface root
📄 index.css
VANILLA CSS Clinical stylesheet layout
📁 backend/
Express API & BullMQ
📄 schema.prisma
PRISMA Postgres relational schema
📄 worker.ts
TYPESCRIPT Background job processor
📄 ai-client.ts
TYPESCRIPT AI Service Axios client
📁 rust-mri/
High-Performance Rust Prototype
📄 Cargo.toml
RUST CARGO Cargo configuration details
📄 main.rs
RUST SOURCE Fourier Transform execution core
FILE_PATH
TECH
File details will be generated here.

Technology Stack Matrix

Component Core Technology Primary Libraries / Specs Purpose
Frontend UI Electron + React + TS Cornerstone3D, Recharts, Three.js / VTK.js Utilitarian workstation GUI, 3D brain/lesion rendering
App Backend Node.js Express + TS Prisma, PostgreSQL 16, BullMQ, Redis, MinIO SDK Database persistence, DICOM upload routing, async worker queue
AI Service FastAPI + PyTorch SimpleITK, NumPy, scikit-image, h5py Reconstruction algorithms, registration, model training
C++ Engine C++17 (CMake) ONNX Runtime v1.24.4, CUDA Toolkit, Protobuf Sub-12ms high-throughput volumetric pathology prediction
Rust Subsystem Rust Cargo rustfft, num-complex, printpdf, image Prototype library for high-speed offline reconstructions

Electron Desktop Application

The user interface mimics a medical diagnostic screen, built on Electron (v29) running a React (v18) application with TypeScript.

Clinical Modern Design Theme The styling utilizes a high-contrast layout, high-density data panels, a border system, typography, and an optional CRT scanning line overlay in its dark workstation mode.

Core Modules in App.tsx

The primary client controller coordinates four workstation states:

  • Ingest Tab: Manages DICOM uploads and raw K-Space arrays. Connects to backend endpoints and triggers MinIO uploads.
  • Archive/Studies Tab: Presents a tabular index of patient scans, process states (pending, processing, complete, failed), and metadata.
  • Patients Tab: Integrates clinical detail sheets, medical history logs, and risk factors.
  • AI Reports Tab: Renders structured diagnosis recommendations, pathology classification graphs (Recharts), and interactive 3D visualizations.

3D Volumetric Visualizations

To assist radiologists, the frontend integrates complex rendering libraries:

  • Cornerstone3D: Core viewport for slicing raw DICOM files, mapping pixel coordinates, window/leveling, and medical annotations.
  • Three.js / VTK.js: Renders a 3D polygonal volume reconstruct of the brain. Highlights predicted pathology locations as glowing nodes with corresponding bounding boxes computed by the spatial attention models.
apps/electron/src/renderer/index.css
/* Siemens Syngo style grid layout definition */
.syngo-layout {
  display: grid;
  grid-template-rows: 40px 1fr 24px;
  height: 100vh;
}
.bevel-inset {
  border: 2px solid;
  border-color: #8898a5 #ffffff #ffffff #8898a5;
}
.clinical-btn-primary {
  background: linear-gradient(to bottom, #e3eeeb, #ccdcd7);
  border-color: #799a8c;
  color: var(--color-accent-green);
  font-weight: bold;
}

Node.js / Express Backend & Worker

The Express backend acts as the data hub, mapping PostgreSQL models via Prisma ORM and running heavy background jobs via BullMQ + Redis.

Job Processing Pipeline

Heavy computation is isolated from the main HTTP loop. The components are structured as follows:

1
Express Router (routes/studies.ts)
Handles requests from the Electron app. When a user uploads a raw study, the server reads the DICOM headers using dcmjs, stores the files in MinIO under the kspace-raw bucket, creates a pending database study, and pushes a job to the BullMQ queue.
2
BullMQ Queue Manager (queue.ts)
Configures a task queue named study-processing backed by Redis. This decouples file uploads from GPU-heavy AI model predictions.
3
Background Worker (worker.ts)
Listens for jobs. For each job, it instantiates AIServiceClient, posts the data to FastAPI, parses results, and updates PostgreSQL states.
apps/backend/src/worker.ts
import { Worker, Job } from 'bullmq';
import { aiClient } from './ai-client';
import { prisma } from './db';

export const studyWorker = new Worker('study-processing', async (job: Job) => {
  const { studyId } = job.data;
  await prisma.study.update({ where: { id: studyId }, data: { status: 'processing' } });
  
  try {
    // Call Python/C++ FastAPI predictive cascade
    const aiResult = await aiClient.predictStudy(studyId);
    
    // Persist results in PostgreSQL
    await prisma.$transaction([
      prisma.modelResult.create({ data: { ... } }),
      prisma.study.update({ where: { id: studyId }, data: { status: 'complete' } })
    ]);
  } catch (err) {
    await prisma.study.update({ where: { id: studyId }, data: { status: 'failed' } });
  }
}, { connection: redisConn });

MinIO Object Storage

Manages binary asset storage. Utilizes two S3 buckets: kspace-raw for uploaded numpy kspace matrices/DICOM files, and reconstructed-magnitude for the final parsed magnitude slices.

Python AI Service (FastAPI)

The AI service runs as a FastAPI microservice containing reconstruction pipelines, PyTorch neural networks, and model training routines.

Phase 2 Two-Tier Gating Flow

To optimize CUDA memory, the application implements a cascading gateway mechanism before running the volumetric spatial classifier:

Stage Algorithm/Model Input Shape Gating Criteria / Actions
Tier 1: Anomaly Gating CNN Artifact Classifier / SSM Estimator [Batch, 16, 128, 128] magnitude / [B, 32, 256, 256] kspace Computes artifact score. If score >= 0.5, triggers Tier 2. Otherwise, bypasses image models to save compute.
Tier 2: Reconstruction SimpleITK Motion Correction + DnCNN Denoising [Batch, 8, 128, 128] magnitude images Aligns corrupted slices via rigid registration and removes high-frequency acquisition noise.
Tier 3: Pathology Fused S4-CNN Volumetric Model [Batch, 8, 16, 128, 128, 2] complex matrix Fuses frequency and spatial representations to output class probabilities for 11 pathologies.

Volumetric Fused S4-CNN Architecture

The production classification model (fused_model_128.onnx) uses a dual-branch encoder that fuses raw frequency acquisitions with spatial magnitude images:

  • Frequency Branch: Diagonal Continuous State Space Model (S4D) layers map raw flat K-space sequence data. It tracks phase encoding paths across 8 sequential slices in the complex frequency domain.
  • Spatial Branch: Parameter-efficient Conv2D convolutions evaluate the magnitude reconstructions. Adaptive average pooling compresses spatial activations to a 128-dimensional latent vector.
  • Slice Cross-Attention: The spatial query vectors interact with the frequency key/value vectors, generating a joint representation pooled for logit mapping.
Model Size Efficiency By switching from a traditional 3D convolutional model (requiring ~67M parameters) to this hybrid S4-CNN architecture, total parameters dropped to just 281,419. This enabled export to a lightweight 4.3 MB ONNX asset with negligible accuracy loss (88.28% validation score).

C++ Inference Engine & ONNX Runtime

For high-throughput diagnostic execution, model files are loaded by a C++ inference library (ai-service/inference/) compiled with ONNX Runtime v1.24.4.

Features & Performance

  • CUDA Acceleration: Attempts to initialize the CUDA Execution Provider. Fallback to CPU execution is automatically handled on devices without discrete NVIDIA GPUs.
  • Memory Binding: Binds raw input pointers directly to CPU/GPU buffers using Ort::Value::CreateTensor to avoid redundant copies of volumetric tensors.
  • Softmax Post-processing: Computes numerically stable softmax directly in C++ before packing results.
  • Multi-threading Control: Limits intra-op threads to 1 or 2 threads to prevent CPU spinlock thread thrashing under concurrent WebWorker schedules.

Throughput Statistics

Backend Average Latency (ms) Throughput (Inferences/Sec) Tests Passed
NVIDIA CUDA GPU 11.8 ms 84.8 inf/sec 10 / 10
CPU (4 Threads) 79.2 ms 12.6 inf/sec 10 / 10
SSM Anomaly (CPU) 23.4 ms 42.8 inf/sec 9 / 9

C++ Engine API Definition

ai-service/inference/kvision_inference.h
#pragma once

#include <memory>
#include <string>
#include <vector>
#include <array>

namespace kvision {

/// Pathology class labels (11 classes matching training)
enum class Pathology : int {
    Normal                  = 0,
    Tumor_Glioma            = 1,
    Ischemia                = 2,
    MS_Lesions              = 3,
    Hydrocephalus           = 4,
    Atrophy                 = 5,
    Hemorrhage              = 6,
    Cerebral_Cyst           = 7,
    Edema                   = 8,
    AVM                     = 9,
    Cerebral_Microbleeds    = 10
};

struct InferenceResult {
    Pathology predicted_class;
    float     confidence;
    std::array<float, 11> logits;
    std::array<float, 11> probabilities;
    double    inference_time_ms;
};

class InferenceEngine {
public:
    explicit InferenceEngine(const InferenceConfig& config);
    ~InferenceEngine();

    InferenceResult infer(const std::vector<float>& kspace_data);
    InferenceResult infer(const float* kspace_data, size_t num_elements);
};

} // namespace kvision

Database Schema

Relational tables stored in PostgreSQL and mapped via Prisma. Focuses on patient archiving and AI gating checkpoints:

👤
Patient
  • id (String / UUID)
  • name (String)
  • dateOfBirth (DateTime)
  • gender (String)
  • phone (String?)
📁
Study
  • id (String / UUID)
  • patientId (String)
  • modality (String)
  • status (String)
  • dicomKey (String?)
📊
ModelResult
  • id (String / UUID)
  • studyId (String)
  • modelName (String)
  • modelVersion (String)
  • confidenceScore (Float)
AnomalyDetection
  • id (String / UUID)
  • studyId (String)
  • ghostingScore (Float)
  • wrapAroundScore (Float)
  • anomalyDetected (Boolean)
🛡️
GatingDecision
  • id (String / UUID)
  • studyId (String)
  • imageEncoderTriggered (Boolean)
  • confidence (Float)
  • reason (String)

Interactive MRI Pipeline Simulator

Adjust variables and trigger the dual-stage processing workflow. This runs a mathematical mockup mimicking the gating threshold equations, denoiser activation, and final volumetric pathology classifier logits.

1. Simulator Inputs

Coil Noise Ratio 0.12
Patient Motion Severity 0.05
Phase Wrap-Around 0.08
Gating Threshold (Artifact limit) 0.50
INGEST: Raw Complex K-Space Uploaded
TIER 1: Anomaly Detector (SSM)
TIER 2: Rigid Registration & DnCNN Denoise
TIER 3: Fused S4-CNN Pathology Classifier

Workstation System Output Logs

[SYSTEM] Workstation initialized. Ready for pulse sequence input.

Pathology Probability Profiling (Logits Softmax)