File size: 30,788 Bytes
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
"""Evaluation runs: first-class async jobs producing cubes, error indexes and sketches.

Lifecycle:  create -> queued -> running (workers over partitions, partial stats)
            -> reducing -> materializing -> published   (or failed)

Each worker processes one population partition (Spark-executor stand-in):
  1. vectorized model simulation (deterministic given lineage seed)
  2. partial sufficient statistics grouped by the 9 cube dims
  3. per-object outputs persisted for forensic drill-down / honest scan fallback
  4. error rows + sketch updates + container partial aggregates

Reduce = concat partials + groupby-sum (cube.reduce_partials).
"""

from __future__ import annotations

import hashlib
import json
import os
import platform
import threading
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
from typing import Dict, List, Optional

import numpy as np
import pandas as pd

from sensorflow.megaeval import cube as cube_mod
from sensorflow.megaeval import population as pop_mod
from sensorflow.megaeval.population import DIMENSIONS, DIM_NAMES
from sensorflow.megaeval.sketches import HyperLogLog, QuantileHistogram, Reservoir

ERROR_TYPES = ["FN", "FP", "LOCALIZATION", "ANOMALY", "LOW_CONF"]
CONTAINER_STATUS = ["ok", "warn", "critical"]
IOU_MATCH_THRESHOLD = 0.5
EVALUATOR_CODE_VERSION = "megaeval-1.0.0"
METRIC_VERSION = "metrics-1.0"


def _now() -> str:
    return datetime.now(timezone.utc).isoformat()


def run_dir(run_id: str) -> str:
    return os.path.join(pop_mod.MEGA_ROOT, "runs", run_id)


# ------------------------------------------------------------------ model profiles


def model_params(model_version: str, overrides: Optional[Dict] = None) -> Dict:
    """Deterministic model skill profile derived from the version string.

    Overrides let tests/demos inject regressions (e.g. worse night detection).
    """
    h = int(hashlib.sha256(model_version.encode()).hexdigest()[:8], 16)
    r = np.random.default_rng(h)
    params = {
        "base_detect": 0.972 + float(r.uniform(-0.01, 0.015)),
        "difficulty_weight": 0.18,
        "night_penalty": 0.030 + float(r.uniform(0, 0.02)),
        "rain_penalty": 0.020 + float(r.uniform(0, 0.015)),
        "occlusion_penalty": 0.050,
        "vru_penalty": 0.020 + float(r.uniform(0, 0.015)),
        "far_penalty": 0.045,
        "iou_base": 0.875 + float(r.uniform(-0.01, 0.015)),
        "iou_difficulty": 0.28,
        "fp_per_container": 0.30 + float(r.uniform(0, 0.08)),
        "anomaly_base": 0.018,
        "low_conf_threshold": 0.45,
    }
    params.update(overrides or {})
    return params


# ------------------------------------------------------------------ run entity


class EvaluationRun:
    def __init__(self, population_id: str, model_version: str,
                 overrides: Optional[Dict] = None, seed: Optional[int] = None,
                 worker_delay_s: float = 0.0, workers: int = 4,
                 label_version: str = "labels-v1",
                 threshold_config: Optional[Dict] = None,
                 sampling_config: Optional[Dict] = None):
        self.run_id = f"eval-{uuid.uuid4().hex[:10]}"
        self.population_id = population_id
        self.model_version = model_version
        self.overrides = overrides or {}
        self.seed = seed if seed is not None else int(
            hashlib.sha256(f"{population_id}|{model_version}|{label_version}".encode()
                           ).hexdigest()[:8], 16)
        self.worker_delay_s = worker_delay_s
        self.workers = workers
        self.status = "created"
        self.error: Optional[str] = None
        self.created_at = _now()
        self.started_at: Optional[str] = None
        self.published_at: Optional[str] = None
        self.partitions_total = 0
        self.partitions_done = 0
        self.objects_total = 0
        self.objects_processed = 0
        self.throughput_objs_per_s = 0.0
        self.eta_s: Optional[float] = None
        self.headline: Dict = {}
        self.per_class: Dict = {}
        self.lineage = {
            "evaluation_id": self.run_id,
            "dataset_version": population_id,
            "model_version": model_version,
            "model_checkpoint": f"ckpt-{hashlib.sha256(model_version.encode()).hexdigest()[:10]}",
            "label_version": label_version,
            "evaluator_code_version": EVALUATOR_CODE_VERSION,
            "metric_version": METRIC_VERSION,
            "threshold_config": {"iou_match": IOU_MATCH_THRESHOLD,
                                 **(threshold_config or {})},
            "sampling_config": sampling_config or {"method": "stratified_risk_weighted",
                                                   "target_n": 1500},
            "seed": self.seed,
            "hardware": f"{platform.machine()}/{platform.system()} single-node",
            "timestamp": self.created_at,
        }

    def progress_dict(self) -> Dict:
        pct = (self.partitions_done / self.partitions_total * 100) if self.partitions_total else 0.0
        return {
            "run_id": self.run_id,
            "population_id": self.population_id,
            "model_version": self.model_version,
            "status": self.status,
            "percent": round(pct, 2),
            "partitions_done": self.partitions_done,
            "partitions_total": self.partitions_total,
            "objects_processed": int(self.objects_processed),
            "objects_total": int(self.objects_total),
            "workers": self.workers,
            "throughput_objs_per_s": round(self.throughput_objs_per_s, 1),
            "eta_s": round(self.eta_s, 1) if self.eta_s is not None else None,
            "error": self.error,
        }

    def to_dict(self) -> Dict:
        return {
            **self.progress_dict(),
            "created_at": self.created_at,
            "started_at": self.started_at,
            "published_at": self.published_at,
            "lineage": self.lineage,
            "headline": self.headline,
            "per_class": self.per_class,
            "overrides": self.overrides,
        }


# ------------------------------------------------------------------ partition evaluation


def evaluate_partition(pop_id: str, partition: int, params: Dict, seed: int) -> Dict:
    """Simulate the model on one partition; return partials + per-object outputs.

    Fully vectorized and deterministic for (lineage seed, partition).
    """
    cols = pop_mod.load_partition(pop_id, partition)
    n = cols["object_id"].shape[0]
    rng = np.random.default_rng((seed * 1_000_003 + partition) % (2**63))

    difficulty = cols["difficulty"].astype(np.float64)
    p_det = params["base_detect"] - difficulty * params["difficulty_weight"]
    p_det -= params["night_penalty"] * (cols["lighting"] == 1)
    p_det -= params["rain_penalty"] * (cols["weather"] >= 1)
    p_det -= params["occlusion_penalty"] * (cols["occlusion"] == 2)
    p_det -= params["vru_penalty"] * np.isin(cols["class"], [1, 2, 3])
    p_det -= params["far_penalty"] * (cols["distance_band"] == 3)
    p_det = np.clip(p_det, 0.02, 0.995)
    detected = rng.random(n) < p_det

    iou = np.zeros(n, dtype=np.float64)
    iou[detected] = np.clip(
        rng.normal(params["iou_base"] - difficulty[detected] * params["iou_difficulty"], 0.07),
        0.02, 0.985)
    tp = detected & (iou >= IOU_MATCH_THRESHOLD)
    loc = detected & ~tp

    conf = np.zeros(n, dtype=np.float64)
    conf[detected] = np.clip(
        0.55 * iou[detected] + 0.25 * (1 - difficulty[detected])
        + rng.normal(0.05, 0.08, size=int(detected.sum())), 0.01, 0.999)

    anomaly_p = params["anomaly_base"] * (1 + 2.0 * difficulty
                                          + 0.8 * (cols["scenario"] >= 4))
    anomaly = rng.random(n) < anomaly_p
    sensor_disagree = rng.random(n) < (0.04 + 0.25 * (cols["scenario"] == 5))

    safety = cols["safety_critical"].astype(bool)

    # ---- spurious FPs per container
    uniq_containers, first_idx = np.unique(cols["container_id"], return_index=True)
    ncont = uniq_containers.size
    wx_c = cols["weather"][first_idx]
    lt_c = cols["lighting"][first_idx]
    lam = params["fp_per_container"] * (1 + 0.5 * (wx_c >= 1) + 0.6 * (lt_c == 1))
    fp_counts = rng.poisson(lam)
    total_fp = int(fp_counts.sum())
    fp_container = np.repeat(uniq_containers, fp_counts)
    fp_src = np.repeat(first_idx, fp_counts)
    fp_cols = {k: cols[k][fp_src] for k in pop_mod.CONTAINER_DIMS}
    fp_cols["class"] = rng.choice(6, size=total_fp,
                                  p=[0.42, 0.18, 0.10, 0.08, 0.14, 0.08]).astype(np.int8)
    fp_cols["sensor"] = rng.choice(3, size=total_fp).astype(np.int8)
    fp_cols["distance_band"] = rng.choice(4, size=total_fp, p=[0.15, 0.35, 0.30, 0.20]).astype(np.int8)
    fp_cols["speed_band"] = rng.choice(4, size=total_fp).astype(np.int8)
    fp_cols["occlusion"] = rng.choice(3, size=total_fp, p=[0.5, 0.3, 0.2]).astype(np.int8)
    fp_conf = np.clip(rng.beta(2.0, 4.5, size=total_fp) + 0.05, 0.01, 0.99)

    # ---- contribution rows -> partial stats
    gt = pd.DataFrame({k: cols[k] for k in DIM_NAMES})
    gt["n"] = 1
    gt["tp"] = tp.astype(np.int64)
    gt["fp"] = loc.astype(np.int64)          # badly localized box counts as FP too
    gt["fn"] = (~tp).astype(np.int64)
    gt["loc_err"] = loc.astype(np.int64)
    gt["anomalies"] = anomaly.astype(np.int64)
    gt["sum_iou"] = np.where(tp, iou, 0.0)
    gt["sum_conf"] = np.where(detected, conf, 0.0)
    gt["sum_conf2"] = np.where(detected, conf**2, 0.0)
    gt["safety_n"] = safety.astype(np.int64)
    gt["safety_tp"] = (safety & tp).astype(np.int64)
    gt["reviewed"] = 0
    gt["verified"] = 0

    fpf = pd.DataFrame({k: fp_cols[k] for k in DIM_NAMES})
    fpf["n"] = 0
    fpf["tp"] = 0
    fpf["fp"] = 1
    fpf["fn"] = 0
    fpf["loc_err"] = 0
    fpf["anomalies"] = 0
    fpf["sum_iou"] = 0.0
    fpf["sum_conf"] = fp_conf
    fpf["sum_conf2"] = fp_conf**2
    fpf["safety_n"] = 0
    fpf["safety_tp"] = 0
    fpf["reviewed"] = 0
    fpf["verified"] = 0

    partial = cube_mod.partial_stats(pd.concat([gt, fpf], ignore_index=True))

    # ---- error rows (error index)
    err_frames = []
    fn_mask = ~detected
    for etype, mask, sev_base in (("FN", fn_mask, 0.75), ("LOCALIZATION", loc, 0.55)):
        idx = np.where(mask)[0]
        if idx.size == 0:
            continue
        code = ERROR_TYPES.index(etype)
        err_frames.append(pd.DataFrame({
            "object_id": cols["object_id"][idx],
            "container_id": cols["container_id"][idx],
            "error_type": np.full(idx.size, code, dtype=np.int8),
            "severity": np.clip(sev_base + 0.25 * safety[idx] + 0.15 * difficulty[idx], 0, 1),
            "confidence": conf[idx],
            "safety_critical": safety[idx].astype(np.uint8),
            "sensor_disagree": sensor_disagree[idx].astype(np.uint8),
            **{k: cols[k][idx] for k in DIM_NAMES},
        }))
    an_idx = np.where(anomaly & tp)[0]
    if an_idx.size:
        err_frames.append(pd.DataFrame({
            "object_id": cols["object_id"][an_idx],
            "container_id": cols["container_id"][an_idx],
            "error_type": np.full(an_idx.size, ERROR_TYPES.index("ANOMALY"), dtype=np.int8),
            "severity": np.clip(0.4 + 0.3 * difficulty[an_idx], 0, 1),
            "confidence": conf[an_idx],
            "safety_critical": safety[an_idx].astype(np.uint8),
            "sensor_disagree": sensor_disagree[an_idx].astype(np.uint8),
            **{k: cols[k][an_idx] for k in DIM_NAMES},
        }))
    lc_idx = np.where(tp & (conf < params["low_conf_threshold"]))[0]
    if lc_idx.size:
        err_frames.append(pd.DataFrame({
            "object_id": cols["object_id"][lc_idx],
            "container_id": cols["container_id"][lc_idx],
            "error_type": np.full(lc_idx.size, ERROR_TYPES.index("LOW_CONF"), dtype=np.int8),
            "severity": np.clip(0.3 + 0.2 * difficulty[lc_idx], 0, 1),
            "confidence": conf[lc_idx],
            "safety_critical": safety[lc_idx].astype(np.uint8),
            "sensor_disagree": sensor_disagree[lc_idx].astype(np.uint8),
            **{k: cols[k][lc_idx] for k in DIM_NAMES},
        }))
    if total_fp:
        err_frames.append(pd.DataFrame({
            "object_id": np.full(total_fp, -1, dtype=np.int64),
            "container_id": fp_container,
            "error_type": np.full(total_fp, ERROR_TYPES.index("FP"), dtype=np.int8),
            "severity": np.clip(0.45 + 0.4 * fp_conf, 0, 1),
            "confidence": fp_conf,
            "safety_critical": np.zeros(total_fp, dtype=np.uint8),
            "sensor_disagree": (rng.random(total_fp) < 0.08).astype(np.uint8),
            **{k: fp_cols[k] for k in DIM_NAMES},
        }))
    errors = (pd.concat(err_frames, ignore_index=True) if err_frames
              else pd.DataFrame())

    # ---- container partial aggregates
    cdf = pd.DataFrame({
        "container_id": cols["container_id"],
        **{k: cols[k] for k in pop_mod.CONTAINER_DIMS},
        "n_objects": 1,
        "tp": tp.astype(np.int64),
        "fp": loc.astype(np.int64),
        "fn": (~tp).astype(np.int64),
        "anomalies": anomaly.astype(np.int64),
        "sum_iou": np.where(tp, iou, 0.0),
        "safety_n": safety.astype(np.int64),
    })
    containers = cdf.groupby(["container_id"] + pop_mod.CONTAINER_DIMS,
                             as_index=False, observed=True).sum()
    if total_fp:
        fp_per = pd.Series(fp_container).value_counts()
        containers["fp"] = containers["fp"] + containers["container_id"].map(fp_per).fillna(0).astype(np.int64)

    # ---- sketches (per partition, merged at reduce)
    conf_hist = QuantileHistogram(0, 1, 64)
    conf_hist.add(np.concatenate([conf[detected], fp_conf]))
    iou_hist = QuantileHistogram(0, 1, 64)
    iou_hist.add(iou[tp])
    hll = HyperLogLog(12)
    hll.add_ids(uniq_containers)
    reservoir = Reservoir(k=100, seed=partition)
    if len(errors):
        reservoir.add_ids(errors["object_id"].to_numpy()[errors["object_id"].to_numpy() >= 0][:2000])

    # ---- per-object outputs (forensic view + scan fallback + brute-force tests)
    obj_out = {
        "object_id": cols["object_id"],
        "container_id": cols["container_id"],
        "detected": detected.astype(np.uint8),
        "tp": tp.astype(np.uint8),
        "iou": iou.astype(np.float32),
        "confidence": conf.astype(np.float32),
        "anomaly": anomaly.astype(np.uint8),
        "sensor_disagree": sensor_disagree.astype(np.uint8),
    }
    fp_out = {
        "container_id": fp_container,
        "confidence": fp_conf.astype(np.float32),
        **{k: fp_cols[k] for k in DIM_NAMES},
    }

    return {"partial": partial, "errors": errors, "containers": containers,
            "conf_hist": conf_hist, "iou_hist": iou_hist, "hll": hll,
            "reservoir": reservoir, "objects": obj_out, "fps": fp_out,
            "rows": n}


# ------------------------------------------------------------------ store


class MegaStore:
    """Registry of populations + runs, with lazily-loaded run artifacts."""

    def __init__(self):
        self.lock = threading.RLock()
        self.runs: Dict[str, EvaluationRun] = {}
        self._artifacts: Dict[str, Dict] = {}
        self.router = cube_mod.QueryRouter()
        self._load_persisted_runs()

    def _load_persisted_runs(self) -> None:
        base = os.path.join(pop_mod.MEGA_ROOT, "runs")
        if not os.path.isdir(base):
            return
        for rid in sorted(os.listdir(base)):
            meta_path = os.path.join(base, rid, "run.json")
            if not os.path.exists(meta_path):
                continue
            try:
                with open(meta_path) as f:
                    d = json.load(f)
                run = EvaluationRun.__new__(EvaluationRun)
                run.__dict__.update(d["state"])
                self.runs[run.run_id] = run
            except Exception:
                continue

    def save_run(self, run: EvaluationRun) -> None:
        d = run_dir(run.run_id)
        os.makedirs(d, exist_ok=True)
        state = {k: v for k, v in run.__dict__.items() if not k.startswith("_")}
        with open(os.path.join(d, "run.json"), "w") as f:
            json.dump({"state": state}, f)

    # ---- artifacts

    def artifacts(self, run_id: str) -> Dict:
        with self.lock:
            if run_id in self._artifacts:
                return self._artifacts[run_id]
        d = run_dir(run_id)
        art: Dict = {}
        cube_path = os.path.join(d, "cube.npz")
        if os.path.exists(cube_path):
            art["cube"] = cube_mod.load_cube(cube_path)
        err_path = os.path.join(d, "errors.npz")
        if os.path.exists(err_path):
            with np.load(err_path) as z:
                art["errors"] = pd.DataFrame({k: z[k] for k in z.files})
        cont_path = os.path.join(d, "containers.npz")
        if os.path.exists(cont_path):
            with np.load(cont_path) as z:
                art["containers"] = pd.DataFrame({k: z[k] for k in z.files})
        emb_path = os.path.join(d, "embeddings.npz")
        if os.path.exists(emb_path):
            with np.load(emb_path) as z:
                art["emb_ids"] = z["container_id"]
                art["emb"] = z["emb"]
        sk_path = os.path.join(d, "sketches.npz")
        if os.path.exists(sk_path):
            with np.load(sk_path) as z:
                art["sketches"] = {
                    "confidence": QuantileHistogram.from_arrays(0, 1, z["conf_hist"]),
                    "iou": QuantileHistogram.from_arrays(0, 1, z["iou_hist"]),
                    "container_hll_estimate": float(z["hll_estimate"]),
                    "exemplar_ids": z["exemplars"].tolist(),
                }
        rev_path = os.path.join(d, "review.json")
        if os.path.exists(rev_path):
            with open(rev_path) as f:
                art["review"] = json.load(f)
        with self.lock:
            if len(self._artifacts) > 6:
                self._artifacts.pop(next(iter(self._artifacts)))
            self._artifacts[run_id] = art
        return art

    def invalidate_artifacts(self, run_id: str) -> None:
        with self.lock:
            self._artifacts.pop(run_id, None)
        self.router.cache.invalidate()

    # ---- lifecycle

    def create_run(self, **kwargs) -> EvaluationRun:
        run = EvaluationRun(**kwargs)
        meta = pop_mod.load_meta(run.population_id)
        if meta is None:
            raise KeyError(f"Unknown population {run.population_id}")
        run.partitions_total = meta["num_partitions"]
        run.objects_total = meta["num_objects"]
        run.status = "queued"
        with self.lock:
            self.runs[run.run_id] = run
        self.save_run(run)
        return run

    def start_async(self, run: EvaluationRun) -> None:
        t = threading.Thread(target=self._execute, args=(run,), daemon=True)
        t.start()

    def execute_sync(self, run: EvaluationRun) -> None:
        self._execute(run)

    def _execute(self, run: EvaluationRun) -> None:
        try:
            run.status = "running"
            run.started_at = _now()
            t_start = time.perf_counter()
            params = model_params(run.model_version, run.overrides)
            d = run_dir(run.run_id)
            os.makedirs(d, exist_ok=True)

            partials: List[pd.DataFrame] = []
            error_frames: List[pd.DataFrame] = []
            container_frames: List[pd.DataFrame] = []
            conf_hist = QuantileHistogram(0, 1, 64)
            iou_hist = QuantileHistogram(0, 1, 64)
            hll = HyperLogLog(12)
            reservoir = Reservoir(k=200, seed=run.seed)
            agg_lock = threading.Lock()

            def work(p: int) -> None:
                res = evaluate_partition(run.population_id, p, params, run.seed)
                if run.worker_delay_s:
                    time.sleep(run.worker_delay_s)  # simulated distributed I/O latency
                # uncompressed npz: these are hot-path worker writes (few MB total)
                np.savez(os.path.join(d, f"objects-part-{p:04d}.npz"), **res["objects"])
                np.savez(os.path.join(d, f"fp-part-{p:04d}.npz"), **res["fps"])
                nonlocal conf_hist, iou_hist, hll, reservoir
                with agg_lock:
                    partials.append(res["partial"])
                    if len(res["errors"]):
                        error_frames.append(res["errors"])
                    container_frames.append(res["containers"])
                    conf_hist = conf_hist.merge(res["conf_hist"])
                    iou_hist = iou_hist.merge(res["iou_hist"])
                    hll = hll.merge(res["hll"])
                    reservoir = reservoir.merge(res["reservoir"])
                    run.partitions_done += 1
                    run.objects_processed += res["rows"]
                    elapsed = time.perf_counter() - t_start
                    run.throughput_objs_per_s = run.objects_processed / max(elapsed, 1e-6)
                    remaining = run.objects_total - run.objects_processed
                    run.eta_s = remaining / max(run.throughput_objs_per_s, 1e-6)

            with ThreadPoolExecutor(max_workers=run.workers) as ex:
                list(ex.map(work, range(run.partitions_total)))

            run.status = "reducing"
            cube_df = cube_mod.reduce_partials(partials)

            run.status = "materializing"
            cube_mod.save_cube(os.path.join(d, "cube.npz"), cube_df)

            errors = (pd.concat(error_frames, ignore_index=True)
                      if error_frames else pd.DataFrame())
            if len(errors):
                errors = errors.reset_index(drop=True)
                errors["error_id"] = np.arange(len(errors), dtype=np.int64)
                risk = (errors["severity"] * 0.5
                        + errors["safety_critical"] * 0.3
                        + (1 - errors["confidence"]) * 0.15
                        + errors["sensor_disagree"] * 0.05)
                errors["risk_score"] = np.clip(risk, 0, 1)
                np.savez_compressed(os.path.join(d, "errors.npz"),
                                    **{c: errors[c].to_numpy() for c in errors.columns})

            containers = (pd.concat(container_frames, ignore_index=True)
                          .groupby(["container_id"] + pop_mod.CONTAINER_DIMS,
                                   as_index=False, observed=True).sum())
            containers["reviewed"] = 0
            containers["verified"] = 0
            err_share = (containers["fn"] + containers["fp"]) / containers["n_objects"].clip(lower=1)
            containers["risk_score"] = np.clip(
                0.55 * err_share + 0.25 * (containers["anomalies"] / containers["n_objects"].clip(lower=1))
                + 0.20 * (containers["safety_n"] > 0), 0, 1).round(4)
            containers["status"] = np.select(
                [containers["risk_score"] >= 0.45, containers["risk_score"] >= 0.2],
                [2, 1], default=0).astype(np.int8)
            np.savez_compressed(os.path.join(d, "containers.npz"),
                                **{c: containers[c].to_numpy() for c in containers.columns})

            self._build_embeddings(d, containers)

            np.savez_compressed(os.path.join(d, "sketches.npz"),
                                conf_hist=conf_hist.counts, iou_hist=iou_hist.counts,
                                hll_estimate=np.array(hll.estimate()),
                                exemplars=np.array(reservoir.items, dtype=np.int64))

            headline_rows, _ = cube_mod.aggregate(
                cube_df, None, None,
                ["n", "tp", "fp", "fn", "loc_err", "anomalies", "precision", "recall", "f1",
                 "mean_iou", "anomaly_rate", "safety_recall", "conf_mean", "error_rate"], 1)
            run.headline = headline_rows[0]
            run.headline["containers"] = int(containers.shape[0])
            run.headline["containers_hll_estimate"] = round(hll.estimate(), 1)

            per_class_rows, _ = cube_mod.aggregate(
                cube_df, None, ["class"],
                ["n", "precision", "recall", "f1", "mean_iou", "safety_recall", "anomaly_rate"], 50)
            run.per_class = {r["class"]: r for r in per_class_rows}

            run.status = "published"
            run.published_at = _now()
            run.eta_s = 0.0
            self.save_run(run)
            self.invalidate_artifacts(run.run_id)
        except Exception as e:  # pragma: no cover - defensive
            run.status = "failed"
            run.error = str(e)
            self.save_run(run)

    @staticmethod
    def _build_embeddings(d: str, containers: pd.DataFrame) -> None:
        """32-d container embeddings: structural features + seeded random projection."""
        feats = []
        nobj = containers["n_objects"].clip(lower=1).to_numpy(dtype=np.float64)
        for col in ("tp", "fp", "fn", "anomalies", "safety_n"):
            feats.append(containers[col].to_numpy(dtype=np.float64) / nobj)
        feats.append(containers["sum_iou"].to_numpy(dtype=np.float64)
                     / containers["tp"].clip(lower=1).to_numpy(dtype=np.float64))
        feats.append(np.log1p(nobj) / 4.0)
        feats.append(containers["risk_score"].to_numpy(dtype=np.float64))
        for dim in pop_mod.CONTAINER_DIMS:
            k = len(DIMENSIONS[dim])
            onehot = np.eye(k)[containers[dim].to_numpy(dtype=np.int64)]
            feats.extend(onehot.T)
        X = np.vstack(feats).T  # (n, ~25)
        rng = np.random.default_rng(1234)
        proj = rng.normal(size=(X.shape[1], 32)) / np.sqrt(X.shape[1])
        emb = X @ proj
        emb = emb / np.clip(np.linalg.norm(emb, axis=1, keepdims=True), 1e-9, None)
        np.savez_compressed(os.path.join(d, "embeddings.npz"),
                            container_id=containers["container_id"].to_numpy(),
                            emb=emb.astype(np.float32))

    # ---- forensic drill-down (deepest level; loads exactly one partition)

    def container_objects(self, run: EvaluationRun, container_id: int) -> List[Dict]:
        p = pop_mod.partition_of_container(container_id)
        pop_cols = pop_mod.load_partition(run.population_id, p)
        d = run_dir(run.run_id)
        with np.load(os.path.join(d, f"objects-part-{p:04d}.npz")) as z:
            out_cols = {k: z[k] for k in z.files}
        mask = pop_cols["container_id"] == container_id
        idx = np.where(mask)[0]
        rows = []
        for i in idx.tolist():
            det = bool(out_cols["detected"][i])
            tp = bool(out_cols["tp"][i])
            rows.append({
                "annotation_id": f"obj-{int(pop_cols['object_id'][i])}",
                "container_id": int(container_id),
                **{k: DIMENSIONS[k][int(pop_cols[k][i])] for k in DIM_NAMES},
                "safety_critical": bool(pop_cols["safety_critical"][i]),
                "difficulty": round(float(pop_cols["difficulty"][i]), 4),
                "detected": det,
                "outcome": "TP" if tp else ("LOCALIZATION" if det else "FN"),
                "iou": round(float(out_cols["iou"][i]), 4) if det else None,
                "confidence": round(float(out_cols["confidence"][i]), 4) if det else None,
                "anomaly": bool(out_cols["anomaly"][i]),
                "sensor_disagree": bool(out_cols["sensor_disagree"][i]),
            })
        with np.load(os.path.join(d, f"fp-part-{p:04d}.npz")) as z:
            fp_cols = {k: z[k] for k in z.files}
        fp_mask = fp_cols["container_id"] == container_id
        for j in np.where(fp_mask)[0].tolist():
            rows.append({
                "annotation_id": f"fp-{run.run_id[-4:]}-{p}-{j}",
                "container_id": int(container_id),
                **{k: DIMENSIONS[k][int(fp_cols[k][j])] for k in DIM_NAMES},
                "safety_critical": False,
                "difficulty": None,
                "detected": True,
                "outcome": "FP",
                "iou": 0.0,
                "confidence": round(float(fp_cols["confidence"][j]), 4),
                "anomaly": False,
                "sensor_disagree": False,
            })
        return rows

    # ---- honest record-scan fallback for the query router

    def scan_records(self, run: EvaluationRun, filters: Optional[Dict],
                     group_by: Optional[List[str]], metrics: Optional[List[str]],
                     limit: int) -> tuple:
        frames = []
        d = run_dir(run.run_id)
        for p in range(run.partitions_total):
            pop_cols = pop_mod.load_partition(run.population_id, p)
            with np.load(os.path.join(d, f"objects-part-{p:04d}.npz")) as z:
                df = pd.DataFrame({
                    **{k: pop_cols[k] for k in DIM_NAMES},
                    "container_id": pop_cols["container_id"],
                    "tp": z["tp"].astype(np.int64),
                    "fn": (1 - z["tp"]).astype(np.int64),
                    "fp": (z["detected"] & ~z["tp"].astype(bool)).astype(np.int64),
                    "anomalies": z["anomaly"].astype(np.int64),
                    "iou": z["iou"],
                })
            frames.append(df)
        full = pd.concat(frames, ignore_index=True)
        full = cube_mod.apply_filters(full, filters)
        gb = [g for g in (group_by or []) if g in list(full.columns)]
        if not gb:
            gb = ["container_id"]
        agg = full.groupby(gb, as_index=False, observed=True).agg(
            n=("tp", "size"), tp=("tp", "sum"), fp=("fp", "sum"), fn=("fn", "sum"),
            anomalies=("anomalies", "sum"), mean_iou=("iou", "mean"))
        agg["recall"] = agg["tp"] / (agg["tp"] + agg["fn"]).clip(lower=1)
        agg = agg.sort_values("n", ascending=False).head(limit)
        rows = []
        for _, r in agg.iterrows():
            row = {}
            for g in gb:
                row[g] = DIMENSIONS[g][int(r[g])] if g in DIM_NAMES else int(r[g])
            row.update({"n": int(r["n"]), "tp": int(r["tp"]), "fp": int(r["fp"]),
                        "fn": int(r["fn"]), "anomalies": int(r["anomalies"]),
                        "mean_iou": round(float(r["mean_iou"]), 6),
                        "recall": round(float(r["recall"]), 6)})
            rows.append(row)
        return rows, len(full)


_STORE: Optional[MegaStore] = None
_STORE_LOCK = threading.Lock()


def get_mega_store() -> MegaStore:
    global _STORE
    with _STORE_LOCK:
        if _STORE is None:
            _STORE = MegaStore()
        return _STORE


def reset_mega_store() -> None:
    global _STORE
    with _STORE_LOCK:
        _STORE = None