Add Delta-E column and make auto-scan.sh macOS-compatible
- averaged.csv now gets a dE00 (CIEDE2000) column measured against the
sample marked with a leading '*' in samples.txt (falls back to the first)
- auto-scan.sh: drop bash-4-only mapfile/${var,,}, OS-aware Argyll package
and python3 install, portable grep/sort, Linux-only udev check
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3,14 +3,28 @@
|
||||
|
||||
import argparse
|
||||
import csv
|
||||
import math
|
||||
import statistics
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
LAB_COLS = ("L*", "a*", "b*")
|
||||
DE_COL = "dE00"
|
||||
|
||||
# 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.
|
||||
REF_MARKER = "*"
|
||||
|
||||
|
||||
def load_sample_names(path: Path) -> list[str]:
|
||||
def _strip_ref_marker(name: str) -> str:
|
||||
"""Remove a leading Delta-E reference marker from a sample name."""
|
||||
if name.startswith(REF_MARKER):
|
||||
return name[len(REF_MARKER):].strip()
|
||||
return name
|
||||
|
||||
|
||||
def _sample_lines(path: Path) -> list[str]:
|
||||
return [
|
||||
line.strip()
|
||||
for line in path.read_text(encoding="utf-8").splitlines()
|
||||
@@ -18,6 +32,93 @@ def load_sample_names(path: Path) -> list[str]:
|
||||
]
|
||||
|
||||
|
||||
def load_sample_names(path: Path) -> list[str]:
|
||||
return [_strip_ref_marker(line) for line in _sample_lines(path)]
|
||||
|
||||
|
||||
def load_reference(path: Path) -> str | None:
|
||||
"""Return the sample marked with a leading '*', if any."""
|
||||
for line in _sample_lines(path):
|
||||
if line.startswith(REF_MARKER):
|
||||
return _strip_ref_marker(line)
|
||||
return None
|
||||
|
||||
|
||||
def de2000(lab1, lab2) -> float:
|
||||
"""CIEDE2000 color difference between two (L*, a*, b*) triples."""
|
||||
L1, a1, b1 = lab1
|
||||
L2, a2, b2 = lab2
|
||||
C1 = math.hypot(a1, b1)
|
||||
C2 = math.hypot(a2, b2)
|
||||
Cbar = (C1 + C2) / 2
|
||||
G = 0.5 * (1 - math.sqrt(Cbar**7 / (Cbar**7 + 25**7)))
|
||||
a1p, a2p = a1 * (1 + G), a2 * (1 + G)
|
||||
C1p, C2p = math.hypot(a1p, b1), math.hypot(a2p, b2)
|
||||
h1p = math.degrees(math.atan2(b1, a1p)) % 360
|
||||
h2p = math.degrees(math.atan2(b2, a2p)) % 360
|
||||
dLp = L2 - L1
|
||||
dCp = C2p - C1p
|
||||
dh = h2p - h1p
|
||||
if C1p * C2p == 0:
|
||||
dhp = 0
|
||||
elif abs(dh) <= 180:
|
||||
dhp = dh
|
||||
elif dh > 180:
|
||||
dhp = dh - 360
|
||||
else:
|
||||
dhp = dh + 360
|
||||
dHp = 2 * math.sqrt(C1p * C2p) * math.sin(math.radians(dhp) / 2)
|
||||
Lbp = (L1 + L2) / 2
|
||||
Cbp = (C1p + C2p) / 2
|
||||
if C1p * C2p == 0:
|
||||
hbp = h1p + h2p
|
||||
elif abs(h1p - h2p) <= 180:
|
||||
hbp = (h1p + h2p) / 2
|
||||
elif h1p + h2p < 360:
|
||||
hbp = (h1p + h2p + 360) / 2
|
||||
else:
|
||||
hbp = (h1p + h2p - 360) / 2
|
||||
T = (1 - 0.17 * math.cos(math.radians(hbp - 30))
|
||||
+ 0.24 * math.cos(math.radians(2 * hbp))
|
||||
+ 0.32 * math.cos(math.radians(3 * hbp + 6))
|
||||
- 0.20 * math.cos(math.radians(4 * hbp - 63)))
|
||||
SL = 1 + 0.015 * (Lbp - 50) ** 2 / math.sqrt(20 + (Lbp - 50) ** 2)
|
||||
SC = 1 + 0.045 * Cbp
|
||||
SH = 1 + 0.015 * Cbp * T
|
||||
dTheta = 30 * math.exp(-(((hbp - 275) / 25) ** 2))
|
||||
RC = 2 * math.sqrt(Cbp**7 / (Cbp**7 + 25**7))
|
||||
RT = -RC * math.sin(math.radians(2 * dTheta))
|
||||
return math.sqrt((dLp / SL) ** 2 + (dCp / SC) ** 2 + (dHp / SH) ** 2
|
||||
+ RT * (dCp / SC) * (dHp / SH))
|
||||
|
||||
|
||||
def add_delta_e(rows: list[dict], reference: str | None) -> str | None:
|
||||
"""Add a CIEDE2000 column to each averaged row, measured against `reference`.
|
||||
|
||||
Returns the reference sample_id actually used, or None if no column was
|
||||
added (missing/unknown reference). Never raises so the scan pipeline can
|
||||
continue even when the reference is misconfigured.
|
||||
"""
|
||||
if not rows:
|
||||
return None
|
||||
|
||||
ref_id = reference or rows[0]["sample_id"]
|
||||
ref_row = next((r for r in rows if r["sample_id"] == ref_id), None)
|
||||
if ref_row is None:
|
||||
print(
|
||||
f"Warning: Delta-E reference {ref_id!r} not found among samples; "
|
||||
f"skipping {DE_COL} column",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return None
|
||||
|
||||
ref_lab = tuple(ref_row[f"{c}_mean"] for c in LAB_COLS)
|
||||
for row in rows:
|
||||
lab = tuple(row[f"{c}_mean"] for c in LAB_COLS)
|
||||
row[DE_COL] = de2000(ref_lab, lab)
|
||||
return ref_id
|
||||
|
||||
|
||||
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))
|
||||
@@ -78,6 +179,7 @@ def main() -> int:
|
||||
args = parser.parse_args()
|
||||
|
||||
sample_names = load_sample_names(args.samples) if args.samples.exists() else []
|
||||
reference = load_reference(args.samples) if args.samples.exists() else None
|
||||
|
||||
sets = []
|
||||
for path in args.csv_files:
|
||||
@@ -87,9 +189,14 @@ def main() -> int:
|
||||
sets.append(read_measurements(path, sample_names))
|
||||
|
||||
averaged = average_sets(sets)
|
||||
|
||||
ref_used = add_delta_e(averaged, reference)
|
||||
|
||||
fieldnames = ["sample_id", "n_scans"]
|
||||
for col in LAB_COLS:
|
||||
fieldnames.extend([f"{col}_mean", f"{col}_std"])
|
||||
if ref_used is not None:
|
||||
fieldnames.append(DE_COL)
|
||||
|
||||
with args.output.open("w", newline="", encoding="utf-8") as f:
|
||||
writer = csv.DictWriter(f, fieldnames=fieldnames)
|
||||
@@ -97,6 +204,9 @@ def main() -> int:
|
||||
writer.writerows(averaged)
|
||||
|
||||
print(f"Wrote {len(averaged)} averaged sample(s) to {args.output}")
|
||||
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}")
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user