'use client' import { useMemo, useState } from 'react' import { SCORE_CATEGORIES, SCORE_REGISTRY, getScoresByCategory, ScoreDefinition } from '@/lib/score-registry' import { SimulatedScoreResult } from '@/lib/score-simulator' import { computeScoreTopLinks } from '@/lib/score-links' import { ConnectomeLink } from '@/lib/types' import { getROIInfo } from '@/lib/shen268-labels' import { LazyImportanceBrain } from '@/components/LazyImportanceBrain' interface BrainVisualizationPanelProps { predictedValues: Record correlationMatrix: number[][] connectomeHtml?: string | null importanceBrainUrl?: string | null importanceStaticBrainUrl?: string | null selectedScoreId: string | null onSelectScore: (scoreId: string) => void } const TOP_K_OPTIONS = [3, 5, 10, 20, 50] function formatValue(value: number, scoreDef: ScoreDefinition): string { const range = scoreDef.scoreRange[1] - scoreDef.scoreRange[0] if (range <= 1) return value.toFixed(2) if (range <= 30) return value.toFixed(1) return Math.round(value).toString() } function formatPredictionValue(value: number, scoreDef: ScoreDefinition, valueScale?: string): string { if (valueScale === 'normalized') return value.toFixed(3) return formatValue(value, scoreDef) } function formatPredictionUnit(scoreDef: ScoreDefinition, valueScale?: string): string { if (valueScale === 'normalized') return 'normalized' return scoreDef.unit } function sourceBadgeClass(source?: string): string { return source === 'model' ? 'bg-emerald-100 text-emerald-800 border-emerald-300/80' : 'bg-amber-100 text-amber-800 border-amber-300/80' } const linkBadgeClass = (link: ConnectomeLink) => link.sign === 'positive' ? 'bg-rose-100 text-rose-800 border-rose-300/70' : 'bg-blue-100 text-blue-800 border-blue-300/70' function ScoreInfoTooltip({ score }: { score: ScoreDefinition }) { return ( i {score.name} {score.domain ? ( Area: {score.domain} ) : null} {score.construct ? ( Measures: {score.construct} ) : null} {score.measureName ? ( Full measure: {score.measureName} ) : null} {score.detail ? {score.detail} : null} {score.interpretation ? ( How to read it: {score.interpretation} ) : null} ) } export default function BrainVisualizationPanel({ predictedValues, correlationMatrix, connectomeHtml, importanceBrainUrl, importanceStaticBrainUrl, selectedScoreId, onSelectScore, }: BrainVisualizationPanelProps) { const [topK, setTopK] = useState(10) const [minR, setMinR] = useState(0) const [showDetails, setShowDetails] = useState(false) const selectedScore = useMemo( () => SCORE_REGISTRY.find((s) => s.id === selectedScoreId) ?? SCORE_REGISTRY.find((s) => s.id === 'listsort_ageadj') ?? null, [selectedScoreId] ) const effectiveSelectedScoreId = selectedScore?.id ?? null const topLinks = useMemo(() => { if (!selectedScore) return [] return computeScoreTopLinks(correlationMatrix, selectedScore, topK, minR) }, [correlationMatrix, selectedScore, topK, minR]) const relevantLobes = useMemo(() => { if (!selectedScore) return [] const lobes = new Set() for (const roi of selectedScore.relevantROIs) { const info = getROIInfo(roi) if (info) lobes.add(`${info.hemisphere} ${info.lobe}`) } return Array.from(lobes).sort() }, [selectedScore]) const result = selectedScore ? predictedValues[selectedScore.id] : null const showImportanceBrain = Boolean(importanceBrainUrl) return (
{/* Score selector tabs */}
{SCORE_CATEGORIES.map((category) => { const scores = getScoresByCategory(category.id) if (scores.length === 0) return null return (
{category.label} {scores.map((scoreDef) => { const sv = predictedValues[scoreDef.id] const isActive = effectiveSelectedScoreId === scoreDef.id return ( ) })}
) })}
{/* Selected score summary */} {selectedScore && result && (

{selectedScore.name}

{selectedScore.description}

{result.source === 'model' ? 'Model prediction' : 'Simulated fallback'} {result.modelFile && File: {result.modelFile}} {result.modelArchitecture && Architecture: {result.modelArchitecture}} {result.nGraphWindows && Windows: {result.nGraphWindows}} {result.normalizedValue !== undefined && result.valueScale === 'original' && ( Normalized: {result.normalizedValue.toFixed(3)} )}

{formatPredictionValue(result.value, selectedScore, result.valueScale)} {formatPredictionUnit(selectedScore, result.valueScale)}

95% CI: {formatPredictionValue(result.ci95Lower, selectedScore, result.valueScale)} - {formatPredictionValue(result.ci95Upper, selectedScore, result.valueScale)}

)} {/* 3D Connectome */}
{showImportanceBrain && importanceBrainUrl ? ( ) : connectomeHtml ? (