Add HTML notes and predicted-row styling to averaged report

Read # note: lines from samples.txt into averaged.html, style n_scans=0
rows as predicted, and document both in samples.example.txt.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-15 11:53:27 +03:30
parent 5eda0b70bd
commit d35ad9e944
2 changed files with 38 additions and 6 deletions

View File

@@ -20,6 +20,19 @@ WHITE_POINT = (96.422, 100.0, 82.521)
# sample's Delta-E is measured against it. The marker is stripped to recover
# the real sample name.
REF_MARKER = "*"
NOTE_PREFIX = "# note:"
def load_notes(path: Path) -> list[str]:
"""Read display notes from '# note: ...' comment lines in the samples file."""
if not path.exists():
return []
notes = []
for line in path.read_text(encoding="utf-8").splitlines():
stripped = line.strip()
if stripped.lower().startswith(NOTE_PREFIX):
notes.append(stripped[len(NOTE_PREFIX):].strip())
return notes
def _strip_ref_marker(name: str) -> str:
@@ -156,7 +169,9 @@ def de_bucket(d: float) -> str:
return "clearly different"
def write_html(rows: list[dict], ref_used: str | None, path: Path) -> None:
def write_html(
rows: list[dict], ref_used: str | None, path: Path, notes: list[str] | None = None
) -> None:
"""Write a self-contained HTML view of the averaged results."""
has_de = ref_used is not None
esc = html.escape
@@ -173,12 +188,15 @@ def write_html(rows: list[dict], ref_used: str | None, path: Path) -> None:
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 ""
is_pred = int(float(r["n_scans"])) == 0
row_cls = "ref" if is_ref else ("pred" if is_pred else "")
cls = f' class="{row_cls}"' if row_cls else ""
ref_tag = ' <span class="tag">reference</span>' if is_ref else ""
pred_tag = ' <span class="tag">predicted</span>' if is_pred else ""
cells = [
f'<td><span class="patch" style="background:{srgb(*lab)}"></span></td>',
f'<td class="name">{esc(r["sample_id"])}{ref_tag}</td>',
f'<td>{esc(str(r["n_scans"]))}</td>',
f'<td class="name">{esc(r["sample_id"])}{ref_tag}{pred_tag}</td>',
f'<td>{esc(str(r["n_scans"])) if not is_pred else "&mdash;"}</td>',
]
for m, s in zip(lab, std):
cells.append(f'<td>{m:.2f}<span class="std">&plusmn;{s:.2f}</span></td>')
@@ -195,6 +213,11 @@ def write_html(rows: list[dict], ref_used: str | None, path: Path) -> None:
"No Delta-E reference was set (mark one sample with a leading '*')."
)
generated = datetime.now().strftime("%Y-%m-%d %H:%M")
notes = notes or []
notes_html = ""
if notes:
items = "".join(f"<li>{esc(n)}</li>" for n in notes)
notes_html = f' <ul class="notes">{items}</ul>\n'
html_doc = f"""<!DOCTYPE html>
<html lang="en">
@@ -211,6 +234,8 @@ def write_html(rows: list[dict], ref_used: str | None, path: Path) -> None:
main {{ max-width:760px; margin:0 auto; }}
h1 {{ font-size:22px; margin:0 0 4px; }}
.muted {{ color:var(--text2); margin:0 0 20px; }}
.notes {{ list-style:none; padding:0; margin:0 0 20px; color:var(--text2); font-size:13px; }}
.notes li {{ margin:2px 0; }}
table {{ border-collapse:separate; border-spacing:0; width:100%;
border:1px solid var(--stroke); border-radius:8px; overflow:hidden; }}
th,td {{ padding:7px 10px; text-align:right; font-size:13px;
@@ -220,6 +245,8 @@ def write_html(rows: list[dict], ref_used: str | None, path: Path) -> None:
border-bottom:1px solid var(--stroke); }}
tbody tr + tr td {{ border-top:1px solid var(--stroke); }}
tr.ref td {{ background:var(--fill); }}
tr.pred td {{ background:#fff8e6; }}
tr.pred td.name {{ font-style:italic; }}
.patch {{ width:18px; height:18px; display:inline-block; vertical-align:middle;
border-radius:5px; border:1px solid var(--stroke); }}
.std {{ color:var(--text3); font-size:11px; margin-left:3px; }}
@@ -237,7 +264,7 @@ def write_html(rows: list[dict], ref_used: str | None, path: Path) -> None:
<h1>Averaged color measurements</h1>
<p class="muted">Per-sample CIELAB means (&plusmn; standard deviation) across the
averaged scans. {ref_note}</p>
<table>
{notes_html} <table>
<thead>
<tr>{"".join(head_cells)}</tr>
</thead>
@@ -339,7 +366,8 @@ def main() -> int:
writer.writerows(averaged)
html_path = args.output.with_suffix(".html")
write_html(averaged, ref_used, html_path)
notes = load_notes(args.samples) if args.samples.exists() else []
write_html(averaged, ref_used, html_path, notes)
print(f"Wrote {len(averaged)} averaged sample(s) to {args.output}")
print(f"Wrote HTML view to {html_path}")

View File

@@ -6,6 +6,10 @@
# measured against it. If none is marked, the first sample is used. Example:
# *INK_A
#
# Optional notes for averaged.html (one per line, shown above the results table):
# note: Glaze: CPO1192
# note: Firing Temperature: 1150 C
#
# Single materials: just the material name/code, e.g.
# INK_A
#