| |
| """Render composite stress-strain plots for the D638 and D790 configs. |
| |
| Reads every specimen JSONL under data/{standard}/*.jsonl, overlays all curves |
| onto one figure per standard, color-codes by batch_label, and uses linestyle |
| to distinguish SLS from PLA/PETG controls. Writes to assets/{standard}_composite.png. |
| """ |
| import json |
| from pathlib import Path |
|
|
| import matplotlib.pyplot as plt |
| from matplotlib.lines import Line2D |
|
|
| ROOT = Path(__file__).parent.parent.parent |
| DATA_DIR = ROOT / "data" |
| OUT_DIR = ROOT / "assets" |
|
|
| |
| |
| BATCH_COLORS = { |
| "A": "#1f77b4", |
| "B": "#ff7f0e", |
| "C": "#2ca02c", |
| "D": "#d62728", |
| "E": "#9467bd", |
| } |
| CONTROL_COLOR = "#7f7f7f" |
|
|
| MATERIAL_STYLES = {"SLS": "-", "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 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 |
|
|
|
|
| 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"] |
| seen[key] = Line2D([0], [0], color=color, linestyle=linestyle, |
| linewidth=1.6, label=label) |
| |
| 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 plot_standard(standard: str) -> Path: |
| paths = sorted((DATA_DIR / standard).glob("*.jsonl")) |
| specs = [s for p in paths if (s := load_specimen(p))] |
| if not specs: |
| raise RuntimeError(f"No specimens found in data/{standard}/") |
|
|
| 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)") |
| title_map = {"D638": "ASTM D638 — tensile composite", |
| "D790": "ASTM D790 — three-point flex composite"} |
| ax.set_title(f"{title_map.get(standard, standard)} ({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="lower right", fontsize=9, framealpha=0.9) |
|
|
| OUT_DIR.mkdir(parents=True, exist_ok=True) |
| out_path = OUT_DIR / f"{standard}_composite.png" |
| fig.tight_layout() |
| fig.savefig(out_path, dpi=150) |
| plt.close(fig) |
| print(f"wrote {out_path.relative_to(ROOT)} ({len(specs)} curves)") |
| return out_path |
|
|
|
|
| if __name__ == "__main__": |
| plot_standard("D638") |
| plot_standard("D790") |
|
|