Spaces:
Running

leaderboard / src /components /DatasetAtlasAndStudies.tsx
dbonet's picture
Open model details from leaderboard charts and dataset atlas
72f3276
Raw History Blame Contribute Delete
16.3 kB
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { Search, X } from 'lucide-react';
import type {
BenchmarkData,
DomainSlug,
ModelInfo,
TaskType,
} from '../types/benchmark';
import {
DOMAIN_COLORS,
FAMILY_META,
FAMILY_SHORT,
METRIC_LABELS,
TASK_LABELS,
formatScore,
formatTimeSec,
} from '../lib/palette';
import { compareModelNames } from '../lib/modelNames';
import { ModelDetailsButton } from './ModelDetailsButton';
interface DatasetAtlasProps {
data: BenchmarkData;
initialDomain: DomainSlug | null;
onSelectModel: (model: ModelInfo) => void;
}
function formatMissingness(fraction: number): string {
const percent = fraction * 100;
return percent < 0.01 ? '<0.01%' : `${percent.toFixed(2)}%`;
}
export const DatasetAtlasView: React.FC<DatasetAtlasProps> = ({
data,
initialDomain,
onSelectModel,
}) => {
const [domainFilter, setDomainFilter] = useState<DomainSlug | 'all'>(
initialDomain ?? 'all'
);
const [missingFilter, setMissingFilter] = useState<
'all' | 'with_missing' | 'no_missing'
>('all');
const [taskFilter, setTaskFilter] = useState<TaskType | 'all'>('all');
const [search, setSearch] = useState('');
const datasetSearchInput = useRef<HTMLInputElement>(null);
const [selectedDatasetId, setSelectedDatasetId] = useState<string | null>(null);
const detailsCard = useRef<HTMLDivElement>(null);
const detailsHeading = useRef<HTMLHeadingElement>(null);
const datasetButton = useRef<HTMLButtonElement | null>(null);
const focusDetails = useRef(false);
const scrollDetails = useRef(false);
const scrollDetailsIntoView = () => {
const el = detailsCard.current;
if (!el) return;
const reduced = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
el.scrollIntoView({ behavior: reduced ? 'auto' : 'smooth', block: 'start' });
};
const filteredDatasets = useMemo(() => {
const q = search.trim().toLowerCase();
return data.datasets.filter((d) => {
if (domainFilter !== 'all' && d.domainSlug !== domainFilter) return false;
if (missingFilter === 'with_missing' && !d.hasMissing) return false;
if (missingFilter === 'no_missing' && d.hasMissing) return false;
if (taskFilter !== 'all' && d.task !== taskFilter) return false;
if (
q &&
!d.id.toLowerCase().includes(q) &&
!d.domain.toLowerCase().includes(q) &&
!d.sourceShort.toLowerCase().includes(q) &&
!d.description.toLowerCase().includes(q)
) {
return false;
}
return true;
});
}, [data.datasets, domainFilter, missingFilter, taskFilter, search]);
const selectedDataset = filteredDatasets.find((dataset) => dataset.id === selectedDatasetId);
useEffect(() => {
if (!selectedDataset) {
focusDetails.current = false;
scrollDetails.current = false;
return;
}
if (focusDetails.current) {
focusDetails.current = false;
scrollDetails.current = false;
detailsHeading.current?.focus();
scrollDetailsIntoView();
} else if (scrollDetails.current) {
scrollDetails.current = false;
scrollDetailsIntoView();
}
}, [selectedDataset]);
const datasetModelRows = useMemo(() => {
if (!selectedDataset) return [];
const dIdx = data.datasets.findIndex((x) => x.id === selectedDataset.id);
if (dIdx < 0) return [];
const evs = data.evaluations.filter((ev) => ev.d === dIdx);
const higher = selectedDataset.higherIsBetter;
const rows = evs.map((ev) => {
const m = data.models[ev.m];
return {
model: m,
ev,
};
});
rows.sort((a, b) => {
const sa = a.ev.selScore;
const sb = b.ev.selScore;
if (sa === sb) return compareModelNames(a.model.id, b.model.id);
if (sa === null) return 1;
if (sb === null) return -1;
return higher ? sb - sa : sa - sb;
});
return rows;
}, [selectedDataset, data]);
return (
<div className="atlas-container">
{/* Filter bar */}
<div className="atlas-controls-card">
<div className="search-input-box">
<Search size={15} />
<input
ref={datasetSearchInput}
type="text"
aria-label="Search datasets"
placeholder="Search 94 datasets by name, domain, biological source, cohort..."
value={search}
onChange={(e) => setSearch(e.target.value)}
/>
{search && (
<button
type="button"
className="clear-btn"
aria-label="Clear dataset search"
onClick={() => {
setSearch('');
datasetSearchInput.current?.focus();
}}
>
<X size={13} />
</button>
)}
</div>
<select
className="scientific-select"
aria-label="Dataset domain"
value={domainFilter}
onChange={(e) => setDomainFilter(e.target.value as DomainSlug | 'all')}
>
<option value="all">All 8 Biomedical Domains</option>
{data.domains.map((d) => (
<option key={d.slug} value={d.slug}>
{d.name} ({d.nDatasets})
</option>
))}
</select>
<select
className="scientific-select"
aria-label="Dataset missingness"
value={missingFilter}
onChange={(e) =>
setMissingFilter(e.target.value as 'all' | 'with_missing' | 'no_missing')
}
>
<option value="all">Missingness: All Datasets</option>
<option value="with_missing">Has Missing Values (&gt;0%)</option>
<option value="no_missing">Complete (0% Missing)</option>
</select>
<select
className="scientific-select"
aria-label="Dataset task type"
value={taskFilter}
onChange={(e) => setTaskFilter(e.target.value as TaskType | 'all')}
>
<option value="all">All Task Types</option>
<option value="binary">Binary Classification</option>
<option value="classification">Multiclass Classification</option>
<option value="regression">Regression</option>
<option value="multiregression">Multi-output Regression</option>
</select>
</div>
{/* Selected Dataset Deep-Dive Drawer */}
{selectedDataset && (
<div id="dataset-details" ref={detailsCard} className="dataset-detail-card">
<div className="dataset-detail-header">
<div>
<div className="dataset-detail-badges">
<span className="inspector-chip">{selectedDataset.domain}</span>
<span className="inspector-chip">{TASK_LABELS[selectedDataset.task]}</span>
<span className="inspector-chip">
{selectedDataset.splitStrategy === 'cv'
? '5-Fold Outer CV'
: 'Fixed Holdout Split'}
</span>
<span className="inspector-chip">
{selectedDataset.hasMissing
? `Missing: ${formatMissingness(selectedDataset.missingCellFraction)}`
: '0% Missing'}
</span>
</div>
<h3 ref={detailsHeading} tabIndex={-1}>{selectedDataset.id}</h3>
<p className="dataset-detail-desc">{selectedDataset.description}</p>
<div className="dataset-detail-meta">
<span>
<strong>Samples:</strong> {selectedDataset.numSamples.toLocaleString()}
</span>
<span>
<strong>Features:</strong> {selectedDataset.numFeatures.toLocaleString()} (
{selectedDataset.numCategorical} categorical)
</span>
<span>
<strong>Source:</strong>{' '}
{selectedDataset.sourceUrl ? (
<a href={selectedDataset.sourceUrl} target="_blank" rel="noreferrer">
{selectedDataset.sourceShort}
</a>
) : selectedDataset.sourceShort}
</span>
{selectedDataset.license && (
<span>
<strong>License:</strong> {selectedDataset.license}
</span>
)}
</div>
</div>
<button
type="button"
className="ghost-action-btn"
onClick={() => {
setSelectedDatasetId(null);
datasetButton.current?.focus();
}}
>
<X size={15} /> Close
</button>
</div>
<div className="table-scroll" style={{ maxHeight: 340 }}>
<table className="leaderboard-data-table compact-table">
<thead>
<tr>
<th>#</th>
<th>Model</th>
<th>Family</th>
<th className="num-col">
Selected ({METRIC_LABELS[selectedDataset.primaryMetric]})
</th>
<th className="num-col">Tuned</th>
<th className="num-col">Default</th>
<th className="num-col">Train s/1K</th>
<th className="num-col">Infer s/1K</th>
</tr>
</thead>
<tbody>
{datasetModelRows.map(({ model, ev }, idx) => {
const fm = FAMILY_META[model.family];
const col = fm.colorLight;
const tr = ev.selVar === 'tuned' ? ev.tunTrain1k : ev.defTrain1k;
const inf = ev.selVar === 'tuned' ? ev.tunInfer1k : ev.defInfer1k;
return (
<tr key={model.id}>
<td className="mono-muted-cell">
{ev.selScore !== null ? idx + 1 : '-'}
</td>
<td>
<ModelDetailsButton modelId={model.id} onClick={() => onSelectModel(model)} />
</td>
<td>
<span
className="family-tag"
style={{
color: col,
background: fm.bgLight,
borderColor: fm.borderLight,
}}
>
{FAMILY_SHORT[model.family]}
</span>
</td>
<td className="num-col mono-cell">
<strong>
{formatScore(ev.selScore, selectedDataset.primaryMetric)}
</strong>
{ev.failCause && ev.selScore === null && (
<span className="imputed-tag">{ev.failCause}</span>
)}
</td>
<td className="num-col mono-muted-cell">
{model.hpoEnabled
? formatScore(ev.tunScore, selectedDataset.primaryMetric)
: '-'}
</td>
<td className="num-col mono-muted-cell">
{formatScore(ev.defScore, selectedDataset.primaryMetric)}
</td>
<td className="num-col mono-muted-cell">{formatTimeSec(tr)}</td>
<td className="num-col mono-muted-cell">{formatTimeSec(inf)}</td>
</tr>
);
})}
</tbody>
</table>
</div>
</div>
)}
{/* Catalogue Table */}
<div className="table-panel">
<div className="table-panel-bar">
<span className="table-bar-count">
<strong>{filteredDatasets.length}</strong> biomedical tasks in catalogue (select a dataset to inspect model scores)
</span>
</div>
<div className="table-scroll" style={{ maxHeight: 560 }}>
<table className="leaderboard-data-table atlas-catalogue-table">
<thead>
<tr>
<th>Dataset</th>
<th>Domain</th>
<th>Task</th>
<th className="num-col">Samples</th>
<th className="num-col">Features</th>
<th className="num-col">Missingness</th>
<th className="num-col">Categorical</th>
<th>Split</th>
<th>Source</th>
</tr>
</thead>
<tbody>
{filteredDatasets.map((ds) => {
const dc = DOMAIN_COLORS[ds.domainSlug];
const isSel = selectedDataset?.id === ds.id;
return (
<tr
key={ds.id}
className={`clickable-row ${isSel ? 'selected-row' : ''}`}
onClick={(event) => {
const selection = window.getSelection();
if (selection && selection.toString().length > 0) return;
datasetButton.current = event.currentTarget.querySelector('button');
if (isSel) {
scrollDetailsIntoView();
} else {
scrollDetails.current = true;
setSelectedDatasetId(ds.id);
}
}}
>
<td className="model-primary-name atlas-dataset-cell">
<button
type="button"
className="table-control-button atlas-dataset-btn"
title={ds.id}
aria-expanded={isSel}
aria-controls={isSel ? 'dataset-details' : undefined}
onClick={(event) => {
const selection = window.getSelection();
if (selection && selection.toString().length > 0) {
event.stopPropagation();
return;
}
if (event.detail !== 0) return;
if (isSel) detailsHeading.current?.focus();
else focusDetails.current = true;
}}
>
{ds.id}
</button>
</td>
<td>
<span className="domain-inline-badge">
<span
className="domain-dot"
style={{ background: dc.light }}
/>
{ds.domain}
</span>
</td>
<td className="mono-muted-cell">{TASK_LABELS[ds.task]}</td>
<td className="num-col mono-cell">{ds.numSamples.toLocaleString()}</td>
<td className="num-col mono-cell">{ds.numFeatures.toLocaleString()}</td>
<td className="num-col mono-cell">
{ds.hasMissing ? (
<span className="missing-highlight">
{formatMissingness(ds.missingCellFraction)}
</span>
) : (
<span className="mono-muted-cell">0%</span>
)}
</td>
<td className="num-col mono-muted-cell">
{ds.numCategorical > 0 ? `${ds.numCategorical}` : '0'}
</td>
<td className="mono-muted-cell">
{ds.splitStrategy === 'cv' ? '5-CV' : 'Fixed'}
</td>
<td className="source-cell" title={ds.sourceShort}>
<span className="source-cell-text">{ds.sourceShort}</span>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
</div>
</div>
);
};