"""Shared helpers for scripts/plots/*.py.""" import json from pathlib import Path ROOT = Path(__file__).parent.parent.parent DATA_DIR = ROOT / "data" OUT_DIR = ROOT / "assets" # Kept consistent across figures so e.g. batch C is the same color everywhere. BATCH_COLORS = { "A": "#1f77b4", # blue "B": "#ff7f0e", # orange "C": "#2ca02c", # green "D": "#d62728", # red "E": "#9467bd", # purple "F": "#8c564b", # brown "G": "#e377c2", # pink "H": "#bcbd22", # olive "I": "#17becf", # cyan "J": "#aec7e8", # light blue "J_MB": "#08519c", # dark blue (media blasted variant of Batch J) "K": "#98df8a", # light green "L": "#ff9896", # light red } CONTROL_COLOR = "#7f7f7f" # gray for PLA/PETG MATERIAL_STYLES = {"SLS": "-", "PLA": "--", "PETG": ":"} FILAMENT_CONTROLS = {"PLA", "PETG"} def load_specimen(path: Path) -> dict | None: with path.open() as f: row = json.loads(f.readline()) pairs = [ (s, t) for s, t in zip(row["curves"]["strain"], row["curves"]["stress_pa"]) if s is not None and t is not None ] if not pairs: return None strain, stress_pa = zip(*pairs) return { "row": row, "strain": list(strain), "stress_mpa": [t / 1e6 for t in stress_pa], } def load_standard(standard: str) -> list[dict]: """Load every specimen with a non-empty curve for a config.""" paths = sorted((DATA_DIR / standard).glob("*.jsonl")) return [s for p in paths if (s := load_specimen(p))] def style_for(row: dict) -> tuple[str, str]: material = row["material_class"] batch = row["batch_label"] color = BATCH_COLORS.get(batch, CONTROL_COLOR) if batch else CONTROL_COLOR linestyle = MATERIAL_STYLES.get(material, "-") return color, linestyle