182 lines
6.2 KiB
TypeScript
182 lines
6.2 KiB
TypeScript
/**
|
|
* Medical Imaging: High-Precision Image Similarity Search
|
|
*
|
|
* Use Case: Medical diagnosis requires highest possible accuracy
|
|
* for similar case retrieval and diagnostic assistance.
|
|
*
|
|
* Optimization Priority: RECALL/PRECISION (latency trade-off acceptable)
|
|
*/
|
|
|
|
import { UnifiedMetrics } from '../../types';
|
|
|
|
export const MEDICAL_ATTENTION_CONFIG = {
|
|
heads: 16, // More heads = better quality (vs 8-head optimal)
|
|
forwardPassTargetMs: 50, // 50ms acceptable (diagnostic aid, not real-time)
|
|
batchSize: 32, // Batch processing for efficiency
|
|
precision: 'float32' as const, // Full precision (medical data critical)
|
|
ensembleSize: 3, // 3-model ensemble for robustness
|
|
|
|
// High-recall configuration
|
|
recallTarget: 0.99, // 99% recall required (vs 96.8% general)
|
|
precisionTarget: 0.95, // 95% precision required
|
|
|
|
// Self-healing with medical data integrity
|
|
selfHealing: {
|
|
enabled: true,
|
|
adaptationIntervalMs: 1000, // 1s monitoring (less critical than trading)
|
|
degradationThreshold: 0.01, // 1% tolerance (strict for medical)
|
|
dataIntegrityChecks: true // Verify data quality
|
|
}
|
|
};
|
|
|
|
// Medical-specific metrics
|
|
export interface MedicalMetrics extends UnifiedMetrics {
|
|
recallAt100: number; // Recall@100 (retrieve more candidates)
|
|
precisionAt10: number; // Precision@10 (top results critical)
|
|
diagnosticAgreement: number; // Agreement with expert diagnosis
|
|
falseNegativeRate: number; // Critical for medical (missed diagnoses)
|
|
dataIntegrityScore: number; // DICOM compliance, quality checks
|
|
}
|
|
|
|
// Similar case interface
|
|
export interface SimilarCase {
|
|
caseId: string;
|
|
diagnosis: string;
|
|
similarity: number;
|
|
radiologistNotes: string;
|
|
confidence: number;
|
|
}
|
|
|
|
// Example: Similar case retrieval for diagnosis
|
|
export async function findSimilarCases(
|
|
patientScan: Float32Array, // MRI/CT scan embeddings
|
|
medicalDatabase: any, // HNSWGraph type
|
|
applyAttention: (data: Float32Array, config: any) => Promise<Float32Array>,
|
|
runEnsemble: (data: Float32Array, size: number) => Promise<any[]>,
|
|
calculateEnsembleConfidence: (candidate: any, ensemble: any[]) => number,
|
|
minConfidence: number = 0.95
|
|
): Promise<SimilarCase[]> {
|
|
const config = MEDICAL_ATTENTION_CONFIG;
|
|
|
|
// 16-head attention for high-quality embeddings
|
|
const enhanced = await applyAttention(patientScan, config);
|
|
|
|
// High-recall search (k=100 for comprehensive retrieval)
|
|
const candidates = await medicalDatabase.search(enhanced, 100);
|
|
|
|
// Ensemble voting for robustness
|
|
const ensembleResults = await runEnsemble(patientScan, config.ensembleSize);
|
|
|
|
// Filter by confidence threshold
|
|
return candidates
|
|
.filter((c: any) => c.score >= minConfidence)
|
|
.map((c: any) => ({
|
|
caseId: c.id,
|
|
diagnosis: c.metadata.diagnosis,
|
|
similarity: c.score,
|
|
radiologistNotes: c.metadata.notes,
|
|
confidence: calculateEnsembleConfidence(c, ensembleResults)
|
|
}));
|
|
}
|
|
|
|
// Performance targets for medical imaging
|
|
export const MEDICAL_PERFORMANCE_TARGETS = {
|
|
recallAt100: 0.99, // 99% recall (comprehensive retrieval)
|
|
precisionAt10: 0.95, // 95% precision (top 10 highly relevant)
|
|
p50LatencyMs: 50, // 50ms median (batch processing acceptable)
|
|
falseNegativeRate: 0.01, // <1% false negatives (critical)
|
|
uptimePercent: 99.9 // 99.9% uptime (3 nines)
|
|
};
|
|
|
|
// Medical imaging modality-specific configurations
|
|
export const MEDICAL_CONFIG_VARIATIONS = {
|
|
// CT scans (high resolution, more detail needed)
|
|
ctScans: {
|
|
...MEDICAL_ATTENTION_CONFIG,
|
|
heads: 20, // Even more heads for fine detail
|
|
recallTarget: 0.995, // 99.5% recall
|
|
ensembleSize: 5 // Larger ensemble
|
|
},
|
|
|
|
// MRI scans (multiple sequences, complex)
|
|
mriScans: {
|
|
...MEDICAL_ATTENTION_CONFIG,
|
|
heads: 16,
|
|
multiSequenceFusion: true, // Fuse T1, T2, FLAIR, etc.
|
|
recallTarget: 0.99
|
|
},
|
|
|
|
// X-rays (simpler, faster acceptable)
|
|
xrays: {
|
|
...MEDICAL_ATTENTION_CONFIG,
|
|
heads: 12,
|
|
forwardPassTargetMs: 30, // Faster processing
|
|
recallTarget: 0.98
|
|
},
|
|
|
|
// Pathology slides (ultra-high resolution)
|
|
pathology: {
|
|
...MEDICAL_ATTENTION_CONFIG,
|
|
heads: 24, // Maximum detail
|
|
forwardPassTargetMs: 100, // Allow more time
|
|
recallTarget: 0.995,
|
|
hierarchicalProcessing: true // Process at multiple scales
|
|
}
|
|
};
|
|
|
|
// Clinical urgency adaptations
|
|
export function adaptConfigToUrgency(
|
|
baseConfig: typeof MEDICAL_ATTENTION_CONFIG,
|
|
urgency: 'routine' | 'urgent' | 'emergency'
|
|
): typeof MEDICAL_ATTENTION_CONFIG {
|
|
switch (urgency) {
|
|
case 'routine':
|
|
return {
|
|
...baseConfig,
|
|
heads: 20, // Maximum quality
|
|
forwardPassTargetMs: 100, // Allow more time
|
|
ensembleSize: 5 // Largest ensemble
|
|
};
|
|
case 'urgent':
|
|
return {
|
|
...baseConfig,
|
|
heads: 16, // Balanced
|
|
forwardPassTargetMs: 50
|
|
};
|
|
case 'emergency':
|
|
return {
|
|
...baseConfig,
|
|
heads: 12, // Faster, still high quality
|
|
forwardPassTargetMs: 20,
|
|
ensembleSize: 1, // Skip ensemble for speed
|
|
recallTarget: 0.97 // Slight quality trade-off
|
|
};
|
|
}
|
|
}
|
|
|
|
// Data quality monitoring
|
|
export interface MedicalDataQuality {
|
|
dicomCompliance: boolean;
|
|
resolutionAdequate: boolean;
|
|
contrastQuality: number;
|
|
artifactScore: number;
|
|
calibrationValid: boolean;
|
|
}
|
|
|
|
export function validateMedicalData(scan: any): MedicalDataQuality {
|
|
return {
|
|
dicomCompliance: checkDICOMHeaders(scan),
|
|
resolutionAdequate: checkResolution(scan),
|
|
contrastQuality: assessContrast(scan),
|
|
artifactScore: detectArtifacts(scan),
|
|
calibrationValid: verifyCalibration(scan)
|
|
};
|
|
}
|
|
|
|
// Placeholder validation functions
|
|
function checkDICOMHeaders(scan: any): boolean { return true; }
|
|
function checkResolution(scan: any): boolean { return true; }
|
|
function assessContrast(scan: any): number { return 0.95; }
|
|
function detectArtifacts(scan: any): number { return 0.02; }
|
|
function verifyCalibration(scan: any): boolean { return true; }
|