#!/usr/bin/env python3 """Render composite stress-strain plots for the D638 and D790 configs. Reads every specimen JSONL under data/{standard}/*.jsonl and splits it into two figures per standard: the main composite (SLS batches + FormLabs PA12GF benchtop control) at assets/{standard}_composite.png, and — only if the standard has any — a separate figure for the PLA/PETG filament controls at assets/{standard}_controls.png. PLA/PETG print at much higher stress/strain than the SLS specimens, which squashed the SLS curves near the origin when they shared an axis with the main composite. """ from pathlib import Path import matplotlib.pyplot as plt from matplotlib.lines import Line2D from _lib import OUT_DIR, ROOT, FILAMENT_CONTROLS, load_standard, style_for def legend_handles(rows: list[dict]) -> list[Line2D]: """One legend entry per unique (batch, material) combination.""" seen = {} for row in rows: key = (row["batch_label"], row["material_class"]) if key in seen: continue color, linestyle = style_for(row) if row["batch_label"]: label = f"Batch {row['batch_label']}" if row["material_class"] != "SLS": label += f" ({row['material_class']})" else: label = row["material_class"] # PLA / PETG controls seen[key] = Line2D([0], [0], color=color, linestyle=linestyle, linewidth=1.6, label=label) # Sort: SLS batches A..E first, controls last. sort_key = lambda kv: (kv[0][1] != "SLS", kv[0][0] or "ZZ", kv[0][1]) return [h for _, h in sorted(seen.items(), key=sort_key)] def render(standard: str, specs: list[dict], title: str, out_name: str) -> Path | None: if not specs: return None fig, ax = plt.subplots(figsize=(9, 6)) for s in specs: color, linestyle = style_for(s["row"]) ax.plot(s["strain"], s["stress_mpa"], color=color, linestyle=linestyle, linewidth=1.2, alpha=0.75) ax.set_xlabel("Strain (mm/mm)") ax.set_ylabel("Stress (MPa)") ax.set_title(f"{title} ({len(specs)} specimens)") ax.grid(True, alpha=0.3) ax.set_xlim(left=0) ax.set_ylim(bottom=0) ax.legend(handles=legend_handles([s["row"] for s in specs]), loc="upper left", bbox_to_anchor=(1.02, 1.0), borderaxespad=0, fontsize=9, framealpha=0.9) OUT_DIR.mkdir(parents=True, exist_ok=True) out_path = OUT_DIR / f"{standard}_{out_name}.png" fig.savefig(out_path, dpi=150, bbox_inches="tight") plt.close(fig) print(f"wrote {out_path.relative_to(ROOT)} ({len(specs)} curves)") return out_path def plot_standard(standard: str) -> None: specs = load_standard(standard) if not specs: raise RuntimeError(f"No specimens found in data/{standard}/") main_specs = [s for s in specs if s["row"]["material_class"] not in FILAMENT_CONTROLS] control_specs = [s for s in specs if s["row"]["material_class"] in FILAMENT_CONTROLS] title_map = {"D638": "ASTM D638 — tensile composite", "D790": "ASTM D790 — three-point flex composite"} render(standard, main_specs, title_map.get(standard, standard), "composite") controls_title_map = {"D638": "ASTM D638 — PLA/PETG filament controls", "D790": "ASTM D790 — PLA/PETG filament controls"} render(standard, control_specs, controls_title_map.get(standard, standard), "controls") if __name__ == "__main__": plot_standard("D638") plot_standard("D790")