File size: 14,444 Bytes
a2bbd94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb23187
a2bbd94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb23187
a2bbd94
 
 
 
 
 
 
 
 
 
 
 
fb23187
 
 
 
 
 
 
a2bbd94
fb23187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2bbd94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb23187
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
 * Container explorer ("Quality by Container"): sortable container table with
 * risk presets. Drilling into a container opens the forensic object table —
 * intentionally the deepest level of the app — plus nearest-neighbor
 * "similar containers" retrieval for casting a wider net.
 */
import { useState } from 'react';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import CircularProgress from '@mui/material/CircularProgress';
import MenuItem from '@mui/material/MenuItem';
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 TextField from '@mui/material/TextField';
import Typography from '@mui/material/Typography';
import { ArrowLeft, Search, ShieldAlert } from 'lucide-react';
import type { ContainerSortPreset, SimilarityResponse } from '../../types/megaeval';
import {
  findSimilarContainers,
  fmtCompact,
  getContainerObjects,
  getRunContainers,
} from '../../services/megaeval';
import { usePoll } from '../../services/labeleval';
import { ErrorNote, LoadingBox, SectionCard, fmtNum, fmtPct } from '../labeleval/shared';
import { HeadCell } from '../help/InfoTip';
import { ContainerStatusIcon, DimChips, OutcomeChip } from './shared';

const SORT_PRESETS: Array<{ value: ContainerSortPreset; label: string }> = [
  { value: 'highest_risk', label: 'Highest risk' },
  { value: 'worst_recall', label: 'Worst recall' },
  { value: 'worst_precision', label: 'Worst precision' },
  { value: 'worst_iou', label: 'Worst IoU' },
  { value: 'most_anomalies', label: 'Most anomalies' },
  { value: 'least_verified', label: 'Least verified' },
];

// ---------------------------------------------------------------- forensic view

function ForensicView({
  runId,
  containerId,
  onBack,
  onDrill,
}: {
  runId: string;
  containerId: number;
  onBack: () => void;
  onDrill: (cid: number) => void;
}) {
  const objects = usePoll(() => getContainerObjects(runId, containerId), null, [runId, containerId]);
  const [similar, setSimilar] = useState<SimilarityResponse | null>(null);
  const [simBusy, setSimBusy] = useState(false);
  const [simError, setSimError] = useState<string | null>(null);

  const findSimilar = async () => {
    setSimBusy(true);
    setSimError(null);
    try {
      setSimilar(await findSimilarContainers({ run_id: runId, container_id: containerId, k: 10 }));
    } catch (err) {
      setSimError(err instanceof Error ? err.message : String(err));
    } finally {
      setSimBusy(false);
    }
  };

  return (
    <Box sx={{ display: 'flex', gap: 2, flexWrap: 'wrap', alignItems: 'flex-start' }}>
      <SectionCard
        title={
          <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
            <Button size="small" startIcon={<ArrowLeft size={14} />} onClick={onBack}>
              Containers
            </Button>
            <span>Forensic object table · container #{containerId}</span>
          </Box>
        }
        action={
          <Button
            size="small"
            variant="outlined"
            startIcon={simBusy ? <CircularProgress size={14} color="inherit" /> : <Search size={14} />}
            disabled={simBusy}
            onClick={() => void findSimilar()}
          >
            Find similar containers
          </Button>
        }
        help="Every annotation in this one container with its evaluation outcome. This is the deepest level of the app — per-object data is loaded on demand only here, never scanned in aggregate views. The shield icon marks safety-critical objects."
        sx={{ flex: '2 1 620px' }}
      >
        <Typography variant="caption" sx={{ color: '#8a949e', display: 'block', mb: 1 }}>
          Deepest drill-down level: individual annotations, loaded on demand for this container only.
        </Typography>
        {objects.error ? <ErrorNote error={objects.error} /> : null}
        {objects.loading && !objects.data ? <LoadingBox /> : null}
        {objects.data ? (
          <Box sx={{ maxHeight: 460, overflowY: 'auto' }}>
            <Table size="small" stickyHeader>
              <TableHead>
                <TableRow>
                  <TableCell>
                    <HeadCell
                      label="Outcome"
                      title="Outcome"
                      detail="Evaluation verdict for this annotation: TP (correct), FN (missed object), FP (phantom), LOCALIZATION (matched but geometry off), LOW_CONF. Hover each chip for its definition."
                    />
                  </TableCell>
                  <TableCell>Class</TableCell>
                  <TableCell align="right">
                    <HeadCell label="IoU" term="iou_3d" />
                  </TableCell>
                  <TableCell align="right">
                    <HeadCell label="Conf" term="confidence" />
                  </TableCell>
                  <TableCell align="center">
                    <HeadCell label="Anomaly" term="anomaly_score" />
                  </TableCell>
                  <TableCell>
                    <HeadCell
                      label="Dims"
                      title="Dimensions"
                      detail="The cohort dimensions this object belongs to (weather, lighting, scenario, sensor, distance band, occlusion) plus evidence flags like sensor-disagree."
                    />
                  </TableCell>
                </TableRow>
              </TableHead>
              <TableBody>
                {objects.data.objects.map((o) => (
                  <TableRow key={o.annotation_id} hover>
                    <TableCell>
                      <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
                        <OutcomeChip outcome={o.outcome} />
                        {o.safety_critical ? <ShieldAlert size={14} color="#ef5350" /> : null}
                      </Box>
                    </TableCell>
                    <TableCell sx={{ fontWeight: 600 }}>{o.class}</TableCell>
                    <TableCell align="right" sx={{ fontFamily: 'monospace' }}>
                      {fmtNum(o.iou, 3)}
                    </TableCell>
                    <TableCell align="right" sx={{ fontFamily: 'monospace' }}>
                      {fmtNum(o.confidence, 2)}
                    </TableCell>
                    <TableCell align="center">
                      {o.anomaly ? (
                        <Typography component="span" sx={{ color: '#ec407a', fontSize: 13 }}>
                          ●
                        </Typography>
                      ) : (
                        <Typography component="span" sx={{ color: '#37474f', fontSize: 13 }}>
                          ○
                        </Typography>
                      )}
                    </TableCell>
                    <TableCell>
                      <DimChips
                        values={[
                          o.weather,
                          o.lighting,
                          o.scenario,
                          o.sensor,
                          o.distance_band,
                          o.occlusion,
                          o.sensor_disagree ? 'sensor-disagree' : undefined,
                        ]}
                      />
                    </TableCell>
                  </TableRow>
                ))}
              </TableBody>
            </Table>
          </Box>
        ) : null}
      </SectionCard>

      {simError ? (
        <Box sx={{ flex: '1 1 340px' }}>
          <ErrorNote error={simError} />
        </Box>
      ) : null}
      {similar ? (
        <SectionCard
          title={`Similar containers${similar.retrieval ? ` · ${similar.retrieval}` : ''}`}
          helpTerm="embedding_similarity"
          sx={{ flex: '1 1 380px' }}
        >
          <Table size="small">
            <TableHead>
              <TableRow>
                <TableCell>Container</TableCell>
                <TableCell align="right">Similarity</TableCell>
                <TableCell>Context</TableCell>
                <TableCell align="right">FN/FP</TableCell>
                <TableCell align="right">Risk</TableCell>
              </TableRow>
            </TableHead>
            <TableBody>
              {similar.results.map((s) => (
                <TableRow key={s.container_id} hover onClick={() => onDrill(s.container_id)} sx={{ cursor: 'pointer' }}>
                  <TableCell sx={{ fontFamily: 'monospace', color: '#4fc3f7' }}>#{s.container_id}</TableCell>
                  <TableCell align="right" sx={{ fontFamily: 'monospace' }}>
                    {fmtPct(s.similarity)}
                  </TableCell>
                  <TableCell>
                    <DimChips values={[s.scenario, s.lighting, s.weather]} />
                  </TableCell>
                  <TableCell align="right" sx={{ fontFamily: 'monospace' }}>
                    {s.fn}/{s.fp}
                  </TableCell>
                  <TableCell align="right" sx={{ fontFamily: 'monospace' }}>
                    {fmtNum(s.risk_score, 2)}
                  </TableCell>
                </TableRow>
              ))}
            </TableBody>
          </Table>
        </SectionCard>
      ) : null}
    </Box>
  );
}

// ---------------------------------------------------------------- containers table

export default function ContainersTab({
  runId,
  refreshKey,
  drillContainerId,
  onDrill,
}: {
  runId: string;
  refreshKey: number;
  drillContainerId: number | null;
  onDrill: (cid: number | null) => void;
}) {
  const [sort, setSort] = useState<ContainerSortPreset>('highest_risk');
  const containers = usePoll(() => getRunContainers(runId, sort, 50), null, [runId, sort, refreshKey]);

  if (drillContainerId !== null) {
    return (
      <ForensicView
        runId={runId}
        containerId={drillContainerId}
        onBack={() => onDrill(null)}
        onDrill={(cid) => onDrill(cid)}
      />
    );
  }

  return (
    <SectionCard
      title="Quality by container"
      helpTerm="container"
      action={
        <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
          {containers.data ? (
            <Typography variant="caption" sx={{ color: '#8a949e' }}>
              {fmtCompact(containers.data.total)} containers (exact) · showing top {containers.data.rows.length}
            </Typography>
          ) : null}
          <TextField
            select
            size="small"
            label="Sort"
            value={sort}
            onChange={(e) => setSort(e.target.value as ContainerSortPreset)}
            sx={{ minWidth: 180 }}
          >
            {SORT_PRESETS.map((p) => (
              <MenuItem key={p.value} value={p.value}>
                {p.label}
              </MenuItem>
            ))}
          </TextField>
        </Box>
      }
    >
      {containers.error ? <ErrorNote error={containers.error} /> : null}
      {containers.loading && !containers.data ? <LoadingBox /> : null}
      {containers.data ? (
        <Box sx={{ maxHeight: 520, overflowY: 'auto' }}>
          <Table size="small" stickyHeader>
            <TableHead>
              <TableRow>
                <TableCell />
                <TableCell>
                  <HeadCell label="Container" term="container" />
                </TableCell>
                <TableCell>Context</TableCell>
                <TableCell align="right">Objects</TableCell>
                <TableCell align="right">
                  <HeadCell label="Verified" term="status_verified" />
                </TableCell>
                <TableCell align="right">
                  <HeadCell label="Precision" term="precision" />
                </TableCell>
                <TableCell align="right">
                  <HeadCell label="Recall" term="recall" />
                </TableCell>
                <TableCell align="right">
                  <HeadCell label="Mean IoU" term="iou_3d" />
                </TableCell>
                <TableCell align="right">
                  <HeadCell label="Anomalies" term="anomaly_score" />
                </TableCell>
                <TableCell align="right">
                  <HeadCell label="Risk" term="risk_score" />
                </TableCell>
              </TableRow>
            </TableHead>
            <TableBody>
              {containers.data.rows.map((c) => (
                <TableRow key={c.container_id} hover onClick={() => onDrill(c.container_id)} sx={{ cursor: 'pointer' }}>
                  <TableCell sx={{ width: 30 }}>
                    <ContainerStatusIcon status={c.status} />
                  </TableCell>
                  <TableCell sx={{ fontFamily: 'monospace', color: '#4fc3f7' }}>#{c.container_id}</TableCell>
                  <TableCell>
                    <DimChips values={[c.scenario, c.lighting, c.weather, c.road_type]} />
                  </TableCell>
                  <TableCell align="right" sx={{ fontFamily: 'monospace' }}>
                    {fmtCompact(c.n_objects)}
                  </TableCell>
                  <TableCell align="right" sx={{ fontFamily: 'monospace' }}>
                    {c.verified}
                  </TableCell>
                  <TableCell align="right" sx={{ fontFamily: 'monospace' }}>
                    {fmtPct(c.precision)}
                  </TableCell>
                  <TableCell align="right" sx={{ fontFamily: 'monospace' }}>
                    {fmtPct(c.recall)}
                  </TableCell>
                  <TableCell align="right" sx={{ fontFamily: 'monospace' }}>
                    {fmtNum(c.mean_iou, 3)}
                  </TableCell>
                  <TableCell align="right" sx={{ fontFamily: 'monospace', color: c.anomalies > 0 ? '#ec407a' : undefined }}>
                    {c.anomalies}
                  </TableCell>
                  <TableCell align="right" sx={{ fontFamily: 'monospace' }}>
                    {fmtNum(c.risk_score, 2)}
                  </TableCell>
                </TableRow>
              ))}
            </TableBody>
          </Table>
        </Box>
      ) : null}
      <Typography variant="caption" sx={{ color: '#8a949e', display: 'block', mt: 1 }}>
        Click a container to open its forensic object table (deepest drill-down).
      </Typography>
    </SectionCard>
  );
}