Spaces:
Paused
Paused
File size: 10,334 Bytes
a2bbd94 fb23187 a2bbd94 fb23187 a2bbd94 fb23187 a2bbd94 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | /**
* Cohort explorer: pick a dimension tab, click a cohort value to drill —
* the value becomes a filter breadcrumb (e.g. Pedestrian → Night → Occluded)
* and the next level groups by the newly selected dimension. Every node shows
* cube metrics via POST /api/evaluations/query. "Why?" runs the factor
* decomposition for the current filter set.
*/
import { useMemo, useState } from 'react';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import Chip from '@mui/material/Chip';
import CircularProgress from '@mui/material/CircularProgress';
import Tab from '@mui/material/Tab';
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableHead from '@mui/material/TableHead';
import TableRow from '@mui/material/TableRow';
import Tabs from '@mui/material/Tabs';
import Typography from '@mui/material/Typography';
import { HelpCircle, ChevronRight } from 'lucide-react';
import type { DimName, WhyResponse } from '../../types/megaeval';
import { DIM_NAMES } from '../../types/megaeval';
import { fmtCompact, queryEvaluations, whyAnalysis } from '../../services/megaeval';
import { usePoll } from '../../services/labeleval';
import { ErrorNote, HBar, LoadingBox, SectionCard, fmtNum, fmtPct } from '../labeleval/shared';
import { HeadCell } from '../help/InfoTip';
import { PctBarCell, QueryBadge, rowNum, rowStr } from './shared';
interface Crumb {
dim: DimName;
value: string;
}
function crumbsToFilters(crumbs: Crumb[]): Partial<Record<DimName, string[]>> {
const filters: Partial<Record<DimName, string[]>> = {};
for (const c of crumbs) {
filters[c.dim] = [...(filters[c.dim] ?? []), c.value];
}
return filters;
}
function WhyPanel({ why }: { why: WhyResponse }) {
const maxCohortN = Math.max(1, ...why.top_cohorts.map((c) => c.n));
return (
<SectionCard
title={`Why is ${why.metric} degraded here?`}
sx={{ flex: '1 1 340px', borderColor: '#4a3b12' }}
>
<Typography variant="caption" sx={{ color: '#8a949e', display: 'block', mb: 1 }}>
{fmtCompact(why.failure_count)} failures decomposed · {why.method}
</Typography>
{why.factors.map((f) => (
<HBar
key={f.factor}
label={f.factor.replace(/_/g, ' ')}
value={f.share <= 1 ? f.share * 100 : f.share}
max={100}
color="#ffd54f"
valueLabel={`${fmtPct(f.share)} (${fmtCompact(f.count)})`}
/>
))}
{why.top_cohorts.length ? (
<>
<Typography variant="caption" sx={{ color: '#aab4be', display: 'block', mt: 1.5, mb: 0.5 }}>
Top affected cohorts
</Typography>
{why.top_cohorts.map((c) => (
<HBar
key={c.cohort}
label={c.cohort}
value={c.n}
max={maxCohortN}
color="#ef5350"
valueLabel={`n=${fmtCompact(c.n)}${c.fn !== undefined && c.fn !== null ? ` · fn=${fmtCompact(c.fn)}` : ''}${
c.fp !== undefined && c.fp !== null ? ` · fp=${fmtCompact(c.fp)}` : ''
}`}
/>
))}
</>
) : null}
</SectionCard>
);
}
export default function CohortTab({ runId, refreshKey }: { runId: string; refreshKey: number }) {
const [crumbs, setCrumbs] = useState<Crumb[]>([]);
const [dim, setDim] = useState<DimName>('class');
const [why, setWhy] = useState<WhyResponse | null>(null);
const [whyBusy, setWhyBusy] = useState(false);
const [whyError, setWhyError] = useState<string | null>(null);
const filters = useMemo(() => crumbsToFilters(crumbs), [crumbs]);
const filtersKey = JSON.stringify(filters);
const query = usePoll(
() =>
queryEvaluations({
evaluation_id: runId,
filters,
group_by: [dim],
metrics: ['n', 'recall', 'precision', 'mean_iou', 'anomaly_rate'],
}),
null,
[runId, refreshKey, dim, filtersKey]
);
const usedDims = new Set(crumbs.map((c) => c.dim));
const drillInto = (value: string) => {
const next = [...crumbs, { dim, value }];
setCrumbs(next);
setWhy(null);
// Auto-advance the group-by dimension to the next unused one.
const nextUsed = new Set(next.map((c) => c.dim));
const nextDim = DIM_NAMES.find((d) => !nextUsed.has(d));
if (nextDim) setDim(nextDim);
};
const removeCrumb = (idx: number) => {
setCrumbs((prev) => prev.filter((_, i) => i !== idx));
setWhy(null);
};
const runWhy = async () => {
setWhyBusy(true);
setWhyError(null);
try {
setWhy(await whyAnalysis({ run_id: runId, filters, metric: 'recall' }));
} catch (err) {
setWhyError(err instanceof Error ? err.message : String(err));
} finally {
setWhyBusy(false);
}
};
const rows = useMemo(
() =>
query.data
? // usePoll keeps the previous response while a new query is in flight;
// drop rows that don't carry the currently-selected dimension so stale
// groupings never render (and never collide on duplicate row keys).
query.data.rows
.filter((r) => typeof r[dim] === 'string')
.sort((a, b) => b.n - a.n)
: [],
[query.data, dim]
);
return (
<Box sx={{ display: 'flex', gap: 2, flexWrap: 'wrap', alignItems: 'flex-start' }}>
<SectionCard
title="Cohort explorer"
helpTerm="cohort"
action={
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
<QueryBadge meta={query.data?.meta} />
<Button
size="small"
variant="outlined"
color="warning"
startIcon={whyBusy ? <CircularProgress size={14} color="inherit" /> : <HelpCircle size={14} />}
disabled={crumbs.length === 0 || whyBusy}
onClick={() => void runWhy()}
>
Why?
</Button>
</Box>
}
sx={{ flex: '2 1 560px' }}
>
{/* Filter breadcrumb */}
<Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5, flexWrap: 'wrap', mb: 1 }}>
<Chip
size="small"
label="All objects"
onClick={() => {
setCrumbs([]);
setWhy(null);
}}
sx={{ height: 22, bgcolor: crumbs.length ? '#232a31' : '#12303f', color: '#81d4fa', fontWeight: 700 }}
/>
{crumbs.map((c, i) => (
<Box key={`${c.dim}-${c.value}-${i}`} sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
<ChevronRight size={13} color="#8a949e" />
<Chip
size="small"
label={`${c.dim}: ${c.value}`}
onDelete={() => removeCrumb(i)}
sx={{ height: 22, bgcolor: '#12303f', color: '#81d4fa', fontFamily: 'monospace', fontSize: 11 }}
/>
</Box>
))}
</Box>
{/* Dimension tabs */}
<Tabs
value={dim}
onChange={(_, v: DimName) => setDim(v)}
variant="scrollable"
scrollButtons="auto"
sx={{ minHeight: 32, mb: 1, '& .MuiTab-root': { minHeight: 32, py: 0.5, fontSize: 12 } }}
>
{DIM_NAMES.map((d) => (
<Tab key={d} label={d.replace(/_/g, ' ')} value={d} disabled={usedDims.has(d)} />
))}
</Tabs>
{query.error ? <ErrorNote error={query.error} /> : null}
{query.loading && !query.data ? <LoadingBox /> : null}
{query.data ? (
<Table size="small">
<TableHead>
<TableRow>
<TableCell>{dim.replace(/_/g, ' ')}</TableCell>
<TableCell align="right">
<HeadCell label="n" title="n" detail="Objects in this cohort under the current filters (exact cube count)." />
</TableCell>
<TableCell>
<HeadCell label="Recall" term="recall" />
</TableCell>
<TableCell>
<HeadCell label="Precision" term="precision" />
</TableCell>
<TableCell align="right">
<HeadCell label="Mean IoU" term="iou_3d" />
</TableCell>
<TableCell align="right">
<HeadCell label="Anomaly rate" term="anomaly_rate" />
</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row, i) => {
const value = rowStr(row, dim);
return (
<TableRow
key={`${value}-${i}`}
hover
onClick={() => drillInto(value)}
sx={{ cursor: 'pointer' }}
>
<TableCell sx={{ fontWeight: 600, color: '#4fc3f7' }}>{value}</TableCell>
<TableCell align="right" sx={{ fontFamily: 'monospace' }}>
{fmtCompact(row.n)}
</TableCell>
<TableCell>
<PctBarCell value={rowNum(row, 'recall')} color="#66bb6a" />
</TableCell>
<TableCell>
<PctBarCell value={rowNum(row, 'precision')} color="#4fc3f7" />
</TableCell>
<TableCell align="right" sx={{ fontFamily: 'monospace' }}>
{fmtNum(rowNum(row, 'mean_iou'), 3)}
</TableCell>
<TableCell align="right" sx={{ fontFamily: 'monospace' }}>
{fmtPct(rowNum(row, 'anomaly_rate'))}
</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
) : null}
<Typography variant="caption" sx={{ color: '#8a949e', display: 'block', mt: 1 }}>
Click a row to drill into that cohort; the next level groups by the selected dimension tab.
</Typography>
</SectionCard>
{whyError ? (
<Box sx={{ flex: '1 1 340px' }}>
<ErrorNote error={whyError} />
</Box>
) : null}
{why ? <WhyPanel why={why} /> : null}
</Box>
);
}
|