# Bench

The numbers in the top-level README come from here.

```sh
npm install
npm run bench                      # synthetic corpus, in this repo
node bench/run.mjs --real <dir>    # plus a private set of real artwork
node bench/labels.mjs --dir <dir>/files --truth <dir>/ground_truth.json   # false alarms on real labels
```

Each run writes `bench/runs/latest/summary.md` (per-rule table, per-category table, every miss and false alarm) and `rows.json` (one row per ground-truth code).

## Synthetic corpus

`bench/corpus/` holds 387 files (PDF and PNG) with one known code each, and `manifest.json` with the ground truth: format, data, where the code sits, and for each rule whether the grader should flag it (`flag`), stay quiet (`ok`), or either is defensible (`any`). Linear codes in the `colour` and `defects` categories carry a reference ISO/IEC 15416 grade computed from the drawn bar profile.

Categories: size (60–210% magnification for EAN/UPC; X-dimension below and above range for the others), PNG resolutions, check digits, quiet zones, truncated bars, colours (RGB and CMYK; the only spot colour in the corpus is the dieline on the carton files), print defects (blur, bar gain and loss, low contrast, spots and voids), symbology rules (GS1-128, ITF-14, DataBar, GS1 DataMatrix, GS1 QR, Digital Link), HRI digits, rotation, cartons and trim.

`bench/gen/build.py` regenerates it byte for byte on macOS and Linux from pinned inputs (numpy, scipy and Pillow pinned in `bench/gen/requirements.txt`, bwip-js from this repo's dev dependencies, Liberation Sans bundled in `bench/gen/fonts/` for the digits drawn into PNGs). `docker build -t barcode-corpus bench/gen && docker run --rm -v "$PWD":/repo:ro barcode-corpus` rebuilds it in a container and checks it against the committed files; see `bench/gen/README.md`.

## Scoring

The grader's summary sentence for each code is classified per rule with the patterns in `rules.json` (the same patterns and the same matching the reference implementation was scored with), after stripping sentences that say a rule is fine. A ground-truth code is matched to a reported code when their boxes overlap by more than 15%.

Placement (folds, cuts, glue flaps, curved packs) is not scored: it needs the dieline, which this package doesn't read.

## Real artwork (private)

Real label files are third-party artwork and are not in this repo. To score them, keep them in a folder of your own:

```
<dir>/manifest.json
<dir>/<id>/<file>.pdf|.jpg|.png
```

`manifest.json` is a list (or `{ "items": [...] }`) of

```json
{
  "id": "brand-cereal",
  "file": "brand-cereal/artwork.pdf",
  "mime": "application/pdf",
  "has_print_size": true,
  "ground_truth_pages": [1],
  "codes": [{
    "page": 1, "format": "UPCA", "text": "012345678905", "gs1": false,
    "bbox": [0.61, 0.70, 0.83, 0.86],
    "check_digit_ok": true, "hri_matches": "yes", "vector": true, "drawn_x_mm": 0.33,
    "problems": []
  }]
}
```

`bbox` is `[x0, y0, x1, y1]` as fractions of the page. `problems` uses tags such as `undersized`, `truncated_bottom`, `dimension_lines_in_quiet_zone`, `near_image_edge`, `blur`; `fromReal()` in `run.mjs` lists every tag it reads and what it expects for each.

## Real labels (private)

`labels.mjs` grades the first four pages of each label and checks what it says about codes against hand-read truth:

```
<dir>/files/<id>/<one PDF or image>
<dir>/ground_truth.json   { "files": { "<id>": { "codes": [{ "format": "UPC-A", "value": "071012081042" }], "accept": ["bars are .* tall"] } } }
```

A code counts as read when its value appears in the report. Anything said about a code other than "looks right" is a false alarm unless an `accept` pattern for that file matches it (for findings that are defensible either way), and so is any code reported that isn't in the truth.
