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.
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:
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.
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.
/* 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:
dcmjs, stores the files in MinIO under the kspace-raw bucket, creates a pending database study, and pushes a job to the BullMQ queue.study-processing backed by Redis. This decouples file uploads from GPU-heavy AI model predictions.AIServiceClient, posts the data to FastAPI, parses results, and updates PostgreSQL states.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.
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::CreateTensorto 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
#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:
id(String / UUID)name(String)dateOfBirth(DateTime)gender(String)phone(String?)
id(String / UUID)patientId(String)modality(String)status(String)dicomKey(String?)
id(String / UUID)studyId(String)modelName(String)modelVersion(String)confidenceScore(Float)
id(String / UUID)studyId(String)ghostingScore(Float)wrapAroundScore(Float)anomalyDetected(Boolean)
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.