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:
43
auto-scan.sh
43
auto-scan.sh
@@ -19,7 +19,12 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
ARGYLL_VERSION="3.4.1"
|
||||
ARGYLL_URL="https://www.argyllcms.com/Argyll_V${ARGYLL_VERSION}_linux_x86_64.tgz"
|
||||
OS="$(uname -s)"
|
||||
case "$OS" in
|
||||
Darwin) ARGYLL_PKG="osx10.6_x86_64" ;; # runs under Rosetta 2 on Apple Silicon
|
||||
*) ARGYLL_PKG="linux_x86_64" ;;
|
||||
esac
|
||||
ARGYLL_URL="https://www.argyllcms.com/Argyll_V${ARGYLL_VERSION}_${ARGYLL_PKG}.tgz"
|
||||
SAMPLES_FILE="samples.txt"
|
||||
AVERAGED_CSV="averaged.csv"
|
||||
FORMULA_CSV="formula.csv"
|
||||
@@ -44,22 +49,32 @@ done
|
||||
info() { printf '\n\033[1;34m==> %s\033[0m\n' "$*"; }
|
||||
warn() { printf '\033[1;33mWARNING: %s\033[0m\n' "$*" >&2; }
|
||||
fail() { printf '\033[1;31mERROR: %s\033[0m\n' "$*" >&2; exit 1; }
|
||||
# Lowercase helper: macOS ships bash 3.2, which lacks ${var,,}.
|
||||
lower() { printf '%s' "$1" | tr '[:upper:]' '[:lower:]'; }
|
||||
|
||||
# ---------------------------------------------------------------- step 1
|
||||
check_dependencies() {
|
||||
info "Step 1/5: Checking required software"
|
||||
|
||||
if ! command -v python3 >/dev/null 2>&1; then
|
||||
warn "python3 not found - attempting to install (needs sudo)"
|
||||
sudo apt-get update && sudo apt-get install -y python3 \
|
||||
|| fail "Could not install python3"
|
||||
warn "python3 not found - attempting to install"
|
||||
if [[ "$OS" == "Darwin" ]]; then
|
||||
command -v brew >/dev/null 2>&1 \
|
||||
|| fail "python3 not found. Install Homebrew (https://brew.sh), then: brew install python3"
|
||||
brew install python3 || fail "Could not install python3 via brew"
|
||||
elif command -v apt-get >/dev/null 2>&1; then
|
||||
sudo apt-get update && sudo apt-get install -y python3 \
|
||||
|| fail "Could not install python3"
|
||||
else
|
||||
fail "python3 not found and no supported package manager (apt-get/brew) available"
|
||||
fi
|
||||
fi
|
||||
echo " python3: $(command -v python3)"
|
||||
|
||||
if ! command -v spotread >/dev/null 2>&1; then
|
||||
# Look for an existing local ArgyllCMS install first
|
||||
local argyll_bin
|
||||
argyll_bin="$(ls -d "$HOME"/.local/bin/Argyll_V*/bin 2>/dev/null | sort -V | tail -1 || true)"
|
||||
argyll_bin="$(ls -d "$HOME"/.local/bin/Argyll_V*/bin 2>/dev/null | sort | tail -1 || true)"
|
||||
if [[ -n "$argyll_bin" ]]; then
|
||||
export PATH="$argyll_bin:$PATH"
|
||||
fi
|
||||
@@ -79,7 +94,7 @@ check_dependencies() {
|
||||
export PATH="$HOME/.local/bin/Argyll_V${ARGYLL_VERSION}/bin:$PATH"
|
||||
command -v spotread >/dev/null 2>&1 || fail "ArgyllCMS install failed"
|
||||
echo " Installed ArgyllCMS to ~/.local/bin/Argyll_V${ARGYLL_VERSION}"
|
||||
echo " Add this to your ~/.bashrc to make it permanent:"
|
||||
echo " Add this to your shell profile (~/.bashrc or ~/.zshrc) to make it permanent:"
|
||||
echo " export PATH=\"\$HOME/.local/bin/Argyll_V${ARGYLL_VERSION}/bin:\$PATH\""
|
||||
fi
|
||||
echo " spotread: $(command -v spotread)"
|
||||
@@ -88,7 +103,7 @@ check_dependencies() {
|
||||
[[ -f "$helper" ]] || fail "Missing helper script: $helper"
|
||||
done
|
||||
|
||||
if [[ ! -f /etc/udev/rules.d/55-Argyll.rules ]]; then
|
||||
if [[ "$OS" == "Linux" && ! -f /etc/udev/rules.d/55-Argyll.rules ]]; then
|
||||
warn "Argyll udev rules not installed. If the instrument is not detected, run: sudo ./setup-udev.sh"
|
||||
fi
|
||||
}
|
||||
@@ -106,14 +121,20 @@ wait_for_instrument() {
|
||||
echo " No instrument detected. Connect the i1Pro 2 via USB."
|
||||
read -r -p " Press Enter to check again (or Ctrl+C to abort)... "
|
||||
done
|
||||
grep -E "^\s+[0-9]+ = " <<<"$ports" | sed 's/^/ Found:/' || true
|
||||
grep -E "^[[:space:]]+[0-9]+ = " <<<"$ports" | sed 's/^/ Found:/' || true
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------- step 3+4
|
||||
scan_samples() {
|
||||
[[ -f "$SAMPLES_FILE" ]] || fail "$SAMPLES_FILE not found"
|
||||
|
||||
mapfile -t SAMPLES < <(grep -v '^#' "$SAMPLES_FILE" | grep -v '^[[:space:]]*$')
|
||||
# Strip a leading "*" (the Delta-E reference marker) from displayed names.
|
||||
# A while-read loop is used instead of mapfile for bash 3.2 (macOS) support.
|
||||
SAMPLES=()
|
||||
while IFS= read -r line; do
|
||||
SAMPLES+=("$line")
|
||||
done < <(grep -v '^#' "$SAMPLES_FILE" | grep -v '^[[:space:]]*$' \
|
||||
| sed -E 's/^[[:space:]]*\*[[:space:]]*//')
|
||||
local count="${#SAMPLES[@]}"
|
||||
[[ "$count" -gt 0 ]] || fail "No sample names found in $SAMPLES_FILE"
|
||||
|
||||
@@ -150,7 +171,7 @@ scan_samples() {
|
||||
if [[ "$readings" -ne "$count" ]]; then
|
||||
warn "Expected $count readings but got $readings."
|
||||
read -r -p " Keep this scan anyway? [y/N] " ans
|
||||
if [[ "${ans,,}" != "y" ]]; then
|
||||
if [[ "$(lower "$ans")" != "y" ]]; then
|
||||
mkdir -p archive
|
||||
mv "$TSV" "archive/${TSV%.tsv}_incomplete.tsv"
|
||||
fail "Scan discarded (moved to archive/). Re-run ./auto-scan.sh"
|
||||
@@ -191,7 +212,7 @@ update_average() {
|
||||
|
||||
if [[ -f build-formula-table.py ]]; then
|
||||
read -r -p " Rebuild $FORMULA_CSV as well? [Y/n] " ans
|
||||
if [[ "${ans,,}" != "n" ]]; then
|
||||
if [[ "$(lower "$ans")" != "n" ]]; then
|
||||
python3 build-formula-table.py -i "$AVERAGED_CSV" -o "$FORMULA_CSV"
|
||||
else
|
||||
echo " Skipped $FORMULA_CSV (rebuild later with: python3 build-formula-table.py)"
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
# One sample name per line, in measurement order.
|
||||
# Lines starting with # are ignored.
|
||||
#
|
||||
# Prefix one sample with * to mark it as the Delta-E reference: every other
|
||||
# sample's Delta-E (CIEDE2000, written as the dE00 column in averaged.csv) is
|
||||
# measured against it. If none is marked, the first sample is used. Example:
|
||||
# *INK_A
|
||||
#
|
||||
# Single materials: just the material name/code, e.g.
|
||||
# INK_A
|
||||
#
|
||||
@@ -10,7 +15,7 @@
|
||||
#
|
||||
# Copy this file to samples.txt and replace with your own materials:
|
||||
# cp samples.example.txt samples.txt
|
||||
INK_A
|
||||
*INK_A
|
||||
INK_B
|
||||
INK_C
|
||||
INK_A+INK_B[75 to 15 mix]
|
||||
|
||||
@@ -8,10 +8,13 @@ from pathlib import Path
|
||||
|
||||
|
||||
def load_sample_names(path: Path) -> list[str]:
|
||||
# A leading "*" marks the Delta-E reference sample; strip it to get the name.
|
||||
names = []
|
||||
for line in path.read_text(encoding="utf-8").splitlines():
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#"):
|
||||
if line.startswith("*"):
|
||||
line = line[1:].strip()
|
||||
names.append(line)
|
||||
return names
|
||||
|
||||
|
||||
Reference in New Issue
Block a user