| """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" |
|
|
| |
| BATCH_COLORS = { |
| "A": "#1f77b4", |
| "B": "#ff7f0e", |
| "C": "#2ca02c", |
| "D": "#d62728", |
| "E": "#9467bd", |
| "F": "#8c564b", |
| "G": "#e377c2", |
| "H": "#bcbd22", |
| "I": "#17becf", |
| "J": "#aec7e8", |
| "J_MB": "#08519c", |
| "K": "#98df8a", |
| "L": "#ff9896", |
| } |
| CONTROL_COLOR = "#7f7f7f" |
|
|
| 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 |
|
|