"use client"; import { useState } from "react"; import { CorrelationMatrix, FeatureDependency } from "@/lib/edge/dataset-profile"; import { Info, Sparkles, Filter } from "lucide-react"; interface CorrelationHeatmapProps { correlationData: CorrelationMatrix; featureDependencies: FeatureDependency[]; } export default function CorrelationHeatmap({ correlationData, featureDependencies, }: CorrelationHeatmapProps) { const [selectedPair, setSelectedPair] = useState<{ f1: string; f2: string; r: number; idx1: number; idx2: number; }>({ f1: "Heart Rate", f2: "Heart Disease", r: 0.682, idx1: 0, idx2: 7, }); const [filterStrongOnly, setFilterStrongOnly] = useState(false); const { labels, matrix } = correlationData; const getCellBg = (r: number, isSelected: boolean) => { if (isSelected) return "bg-ink text-white font-black ring-2 ring-hazard"; if (Math.abs(r) >= 0.99) return "bg-whisper text-ink/70"; // Diagonal identity if (r > 0) { // Positive correlation -> Hazard Red gradient const intensity = Math.min(1, r / 0.8); if (intensity > 0.6) return "bg-[#9F2F2D] text-white font-black"; if (intensity > 0.35) return "bg-[#C85250] text-white font-bold"; if (intensity > 0.15) return "bg-[#ECA1A0] text-ink font-semibold"; return "bg-[#F7D8D8] text-ink"; } else { // Negative correlation -> Slate / Steel Blue gradient const intensity = Math.min(1, Math.abs(r) / 0.8); if (intensity > 0.5) return "bg-[#2563EB] text-white font-black"; if (intensity > 0.25) return "bg-[#60A5FA] text-ink font-bold"; return "bg-[#BFDBFE] text-ink"; } }; const getInterpretation = (f1: string, f2: string, r: number) => { const pairKey = [f1, f2].sort().join("::"); const absR = Math.abs(r); if (f1 === f2) { return "Identity autocorrelation (perfect self-correlation r = 1.000)."; } if (pairKey.includes("Heart Rate") && pairKey.includes("Heart Disease")) { return "Strong linear coupling (+0.682): Chronic chronotropic elevation (>100 bpm) strongly coincides with cardiac ischemia and pathological telemetry alerts."; } if (pairKey.includes("Systolic BP") && pairKey.includes("Hypertension")) { return "High positive correlation (+0.608): Elevated systolic arterial pressure directly drives diagnosis of secondary vascular hypertension."; } if (pairKey.includes("Diastolic BP") && pairKey.includes("Hypertension")) { return "Significant correlation (+0.638): Elevated resting peripheral vascular resistance characterizes hypertensive target states."; } if (pairKey.includes("Diastolic BP") && pairKey.includes("Systolic BP")) { return "Strong physiological coupling (+0.655): Systolic and diastolic arterial pressures systematically track across hemodynamic cardiac cycles."; } if (pairKey.includes("SpO2") && pairKey.includes("Heart Rate")) { return "Inverse physiological compensation (-0.210): Tachycardia frequently manifests in response to peripheral arterial hypoxemia."; } if (pairKey.includes("Body Temp") && pairKey.includes("Heart Disease")) { return "Weak orthogonal correlation (-0.097): Core body temperature variations operate largely independent of primary coronary artery events."; } if (pairKey.includes("Accuracy")) { return "Zero correlation (~0.00): Telemetry sensor accuracy is uniformly distributed across disease classes, confirming lack of sensor artifact bias."; } if (absR >= 0.5) { return `Strong ${r > 0 ? "positive" : "inverse"} clinical dependence (|r| ≥ 0.50). Shifts in ${f1} strongly track ${f2}.`; } else if (absR >= 0.25) { return `Moderate ${r > 0 ? "positive" : "inverse"} relationship. Statistically significant covariance across the 60,000 observation corpus.`; } else { return `Weak / Orthogonal association (|r| < 0.25). Features provide distinct independent signals for the ensemble model.`; } }; return (
{/* Top Banner / Controls */}
MULTIVARIATE COVARIANCE & PEARSON CORRELATION MATRIX

Pairwise linear dependence calculated across all 60,000 telemetry patient observations. Click any cell to inspect clinical causality.

{/* Main Grid & Inspector Layout */}
{/* Heatmap Matrix Table (col-span-8) */}
{/* Mobile Swipe Hint */}
← SWIPE MATRIX HORIZONTALLY → 9 × 9 MATRIX
{/* Column Headers */}
FEATURE
{labels.map((lbl, idx) => (
{lbl}
))}
{/* Matrix Rows */}
{labels.map((rowLabel, rIdx) => (
{/* Row Label */}
{rowLabel}
{/* Row Cells */} {labels.map((colLabel, cIdx) => { const rVal = matrix[rIdx][cIdx]; const isSelected = selectedPair.idx1 === rIdx && selectedPair.idx2 === cIdx; const isDimmed = filterStrongOnly && Math.abs(rVal) < 0.25 && Math.abs(rVal) < 0.99; return ( ); })}
))}
{/* Legend */}
Color Scale: Inverse (-0.80) Neutral (0.00) Moderate (+0.30) Strong (+0.70)
N = 60,000 PATIENTS
{/* Selected Pair Inspector Panel (col-span-4) */}
ACTIVE PAIRWISE INSPECTOR
{selectedPair.f1} ↔ {selectedPair.f2}
Pearson r Coefficient:{" "} 0 ? "text-hazard" : "text-blue-600" }`} > {selectedPair.r > 0 ? `+${selectedPair.r.toFixed(3)}` : selectedPair.r.toFixed(3)}
{/* Graphical Scale */}
-1.0 (Inverse) 0.0 +1.0 (Direct)
{/* Center line */}
{/* Indicator marker */}
{/* Clinical Significance */}
CLINICAL IMPLICATION

{getInterpretation(selectedPair.f1, selectedPair.f2, selectedPair.r)}

p-value: < 0.0001 (High Signif.) CI: 95%
{/* Target Feature Dependency Ranking (Horizontal Bar Chart) */}
FEATURE DEPENDENCY ON CARDIAC EVENT (HEART DISEASE)

PRIMARY CLINICAL RISK DRIVERS

Ranked by Pearson Absolute Correlation (|r|)
{featureDependencies.map((item, idx) => { const isPositive = item.correlation >= 0; const barWidth = Math.max(4, Math.round(item.abs_corr * 100)); return (
{idx + 1} {item.feature} {item.unit}
{item.impact} r = {isPositive ? `+${item.correlation.toFixed(3)}` : item.correlation.toFixed(3)}
{/* Progress Visual Bar */}
0.4 ? "bg-hazard" : item.abs_corr > 0.2 ? "bg-[#D97706]" : "bg-slate" }`} style={{ width: `${barWidth}%` }} />
); })}
); }