From 5eda0b70bd212891b49884d39b4d4dc83d26b779 Mon Sep 17 00:00:00 2001 From: diaco Date: Wed, 15 Jul 2026 08:18:06 +0330 Subject: [PATCH] Add averaged.html output and document OS support average-results.py now writes a formatted HTML view alongside averaged.csv, and README documents Linux/macOS requirements plus the Delta-E reference marker. Co-authored-by: Cursor --- README.md | 42 ++++++++++---- average-results.py | 139 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b5a719a..7b56f45 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,9 @@ average repeated scans, and relate results to ink mix formulas. `auto-scan.sh` runs the whole process in one go: 1. **Checks required software** — verifies `python3` and ArgyllCMS - (`spotread`) are available. Installs python3 via apt and downloads - ArgyllCMS to `~/.local/bin` automatically if missing. + (`spotread`) are available. Installs python3 (via `apt` on Linux or + Homebrew on macOS) and downloads ArgyllCMS to `~/.local/bin` + automatically if missing. 2. **Waits for the instrument** — prompts you to connect the i1Pro 2 via USB and re-checks until it is detected. 3. **Scans the samples** — lists the sample names from `samples.txt` in @@ -27,8 +28,9 @@ average repeated scans, and relate results to ink mix formulas. `samples.txt`. 5. **Updates the averages** — re-averages **all** complete scan CSVs in the folder into `averaged.csv` (L\*, a\*, b\* mean and standard - deviation per sample), then asks whether to rebuild the formula table - `formula.csv` (default: yes). + deviation per sample, plus a `dE00` CIEDE2000 column against the + reference sample) and a formatted `averaged.html` view, then asks + whether to rebuild the formula table `formula.csv` (default: yes). ### Options @@ -46,10 +48,27 @@ is built only from scans of the mode you pass (default D50). ## Prerequisites -- Linux with bash, python3 (auto-installed if missing) +### Operating system + +The scripts run on **Linux** and **macOS**: + +- **Linux** — the primary target. Uses `apt` to install python3 if missing + and udev rules for USB access (see below). Ships bash 4+. +- **macOS** — supported. The default `/bin/bash` is 3.2, so `auto-scan.sh` + is written to stay compatible with it; no newer bash is required. python3 + is installed via [Homebrew](https://brew.sh) if missing, and ArgyllCMS + uses the macOS build (runs under Rosetta 2 on Apple Silicon). macOS needs + no udev rules. Note: only the desktop workflow is verified on Linux; the + macOS paths are portability fixes and have not been hardware-tested. +- **Windows** — not supported (no Bourne shell / udev; use WSL instead). + +### Other requirements + +- bash, python3 (auto-installed if missing) - ArgyllCMS (auto-downloaded to `~/.local/bin/Argyll_V3.4.1` if missing) - X-Rite i1Pro 2 with its white calibration tile -- (Recommended, one time) udev rules so the instrument works without root: +- (Linux only, recommended, one time) udev rules so the instrument works + without root: ```bash sudo ./setup-udev.sh @@ -67,10 +86,12 @@ cp samples.example.txt samples.txt ``` Lines starting with `#` are ignored. Material names can be any codes. -Mix samples use the pattern `NAME_A+NAME_B[w1 to w2 mix]`, e.g.: +Prefix one sample with `*` to mark it as the **Delta-E reference** that the +`dE00` column is measured against (if none is marked, the first sample is +used). Mix samples use the pattern `NAME_A+NAME_B[w1 to w2 mix]`, e.g.: ```text -INK_A +*INK_A INK_B INK_A+INK_B[75 to 15 mix] INK_A+INK_C[30 to 80 mix] @@ -86,7 +107,8 @@ discovers the material names automatically from your sample list. |------|----------| | `scan_D50_.tsv` | Raw spotread log (Lab + XYZ + spectrum 380–730 nm) | | `scan_D50_.csv` | Same data as CSV with `sample_id` column | -| `averaged.csv` | Per-sample L\*, a\*, b\* mean and std across all scans | +| `averaged.csv` | Per-sample L\*, a\*, b\* mean and std, plus `dE00` vs the reference | +| `averaged.html` | Formatted view of `averaged.csv` with color swatches | | `formula.csv` | Averaged Lab plus one percentage column per ink | | `archive/` | Discarded or superseded scans | @@ -99,7 +121,7 @@ These are used by `auto-scan.sh` but can also be run on their own: | `measure.sh [mode]` | Just the spotread measurement session (TSV output) | | `run-measurement.sh [mode]` | Measurement session + CSV conversion | | `tsv-to-csv.py [-s samples.txt]` | Convert a spotread TSV to labelled CSV | -| `average-results.py -o out.csv` | Average Lab across scan CSVs | +| `average-results.py -o out.csv` | Average Lab across scan CSVs (writes CSV + HTML) | | `build-formula-table.py` | Rebuild `formula.csv` from the averaged CSV | | `rebuild-average.sh` | Shortcut for `./auto-scan.sh --average-only` | | `setup-udev.sh` | Install udev rules for USB instrument access (sudo) | diff --git a/average-results.py b/average-results.py index 30a33ef..3bdaad2 100755 --- a/average-results.py +++ b/average-results.py @@ -3,14 +3,19 @@ import argparse import csv +import html import math import statistics import sys +from datetime import datetime from pathlib import Path LAB_COLS = ("L*", "a*", "b*") DE_COL = "dE00" +# D50 / 2 deg white point, used to render approximate sRGB swatches from Lab. +WHITE_POINT = (96.422, 100.0, 82.521) + # A sample line prefixed with "*" is the Delta-E reference: every other # sample's Delta-E is measured against it. The marker is stripped to recover # the real sample name. @@ -119,6 +124,136 @@ def add_delta_e(rows: list[dict], reference: str | None) -> str | None: return ref_id +def srgb(L: float, a: float, b: float) -> str: + """Approximate sRGB hex for a CIELAB triple (D50), for display swatches.""" + fy = (L + 16) / 116 + fx = fy + a / 500 + fz = fy - b / 200 + + def finv(f): + return f**3 if f**3 > 0.008856 else (f - 16 / 116) / 7.787 + + X, Y, Z = (finv(f) * w / 100 for f, w in zip((fx, fy, fz), WHITE_POINT)) + r = 3.1338561 * X - 1.6168667 * Y - 0.4906146 * Z + g = -0.9787684 * X + 1.9161415 * Y + 0.0334540 * Z + bl = 0.0719453 * X - 0.2289914 * Y + 1.4052427 * Z + + def enc(c): + c = max(0.0, min(1.0, c)) + c = 1.055 * c ** (1 / 2.4) - 0.055 if c > 0.0031308 else 12.92 * c + return round(max(0.0, min(1.0, c)) * 255) + + return "#%02x%02x%02x" % (enc(r), enc(g), enc(bl)) + + +def de_bucket(d: float) -> str: + if d < 1: + return "not perceptible" + if d < 2: + return "perceptible on close inspection" + if d < 3.5: + return "perceptible at a glance" + return "clearly different" + + +def write_html(rows: list[dict], ref_used: str | None, path: Path) -> None: + """Write a self-contained HTML view of the averaged results.""" + has_de = ref_used is not None + esc = html.escape + + head_cells = [ + "", "Sample", "n", + "L*", "a*", "b*", + ] + if has_de: + head_cells.append("ΔE00") + + body_rows = [] + for r in rows: + lab = tuple(float(r[f"{c}_mean"]) for c in LAB_COLS) + std = tuple(float(r[f"{c}_std"]) for c in LAB_COLS) + is_ref = has_de and r["sample_id"] == ref_used + cls = ' class="ref"' if is_ref else "" + ref_tag = ' reference' if is_ref else "" + cells = [ + f'', + f'{esc(r["sample_id"])}{ref_tag}', + f'{esc(str(r["n_scans"]))}', + ] + for m, s in zip(lab, std): + cells.append(f'{m:.2f}±{s:.2f}') + if has_de: + d = float(r[DE_COL]) + de_cls = "de win" if is_ref else ("de bad" if d >= 3.5 else "de") + cells.append(f'{d:.2f}') + body_rows.append(f' {"".join(cells)}') + + ref_note = ( + f"ΔE00 is CIEDE2000 against {esc(ref_used)}; " + "lower is a closer match." + if has_de else + "No Delta-E reference was set (mark one sample with a leading '*')." + ) + generated = datetime.now().strftime("%Y-%m-%d %H:%M") + + html_doc = f""" + + + + +Averaged color measurements + + + +
+

Averaged color measurements

+

Per-sample CIELAB means (± standard deviation) across the + averaged scans. {ref_note}

+ + + {"".join(head_cells)} + + +{chr(10).join(body_rows)} + +
+

Swatches are sRGB approximations of the measured Lab values + (D50 / 2°). Generated by average-results.py on {generated}.

+
+ + +""" + path.write_text(html_doc, encoding="utf-8") + + def read_measurements(path: Path, sample_names: list[str]) -> list[dict]: with path.open(newline="", encoding="utf-8") as f: rows = list(csv.DictReader(f)) @@ -203,7 +338,11 @@ def main() -> int: writer.writeheader() writer.writerows(averaged) + html_path = args.output.with_suffix(".html") + write_html(averaged, ref_used, html_path) + print(f"Wrote {len(averaged)} averaged sample(s) to {args.output}") + print(f"Wrote HTML view to {html_path}") if ref_used is not None: note = "" if reference else " (no '*'-marked sample; using first sample)" print(f"{DE_COL} computed as CIEDE2000 vs {ref_used!r}{note}")