Download scripts/make_figures.py from xijiazhao/welding2023: direct link, hf CLI and curl.
- Browser
- Download file 4.46 kB
-
https://huggingface.co/datasets/xijiazhao/welding2023/resolve/main/scripts/make_figures.py
- Command line
-
hf download hf://datasets/xijiazhao/welding2023/scripts/make_figures.py
-
curl -L -o make_figures.py https://huggingface.co/datasets/xijiazhao/welding2023/resolve/main/scripts/make_figures.py
4.46 kB
| """Figures for README.md: design matrix, torque signature in the order spectrum, healthy drift across days.""" | |
| import sys, numpy as np, pandas as pd, matplotlib | |
| matplotlib.use("Agg") | |
| import matplotlib.pyplot as plt | |
| ROOT = sys.argv[1] | |
| INK, MUTED, GRID = "#0b0b0b", "#52514e", "#e4e3df" | |
| TQ = {20: "#86b6ef", 25: "#3987e5", 30: "#1c5cab", 35: "#0d366b"} | |
| plt.rcParams.update({"font.size": 9, "axes.edgecolor": MUTED, "axes.labelcolor": INK, "xtick.color": MUTED, | |
| "ytick.color": MUTED, "axes.spines.top": False, "axes.spines.right": False}) | |
| d = pd.read_csv("files_full.csv") | |
| v = d[(d.sensor == "IEPE_S") & (d.rpm > 0)] | |
| # Fig 1: design matrix (IEPE take-0 files per condition x torque x rpm) | |
| v0 = v[v["take"] == 0].copy() | |
| v0["col"] = v0.torque.astype(str) + " N·m\n" + v0.rpm.astype(str) | |
| order = [f"{t} N·m\n{r}" for t in (20, 25, 27, 30, 35, 37) for r in (600, 750, 800, 900, 1050, 1100, 1200, 1300, 1460)] | |
| tab = pd.crosstab(v0.folder + " [" + v0.session.astype(str).str[4:] + "]", v0.col) | |
| tab = tab[[c for c in order if c in tab.columns]] | |
| tab = tab.groupby(level=0).sum() | |
| fig, ax = plt.subplots(figsize=(15, 8.5)) | |
| cm = matplotlib.colors.ListedColormap(["#fcfcfb", "#b7d3f6", "#6da7ec", "#2a78d6", "#1c5cab", "#104281"]) | |
| ax.imshow(tab.values, cmap=cm, vmin=0, vmax=6, aspect="auto") | |
| for i in range(tab.shape[0]): | |
| for j in range(tab.shape[1]): | |
| n = tab.values[i, j] | |
| if n: ax.text(j, i, n, ha="center", va="center", fontsize=7, color="white" if n >= 3 else INK) | |
| ax.set_xticks(range(tab.shape[1]), [c.replace("\n", " ") for c in tab.columns], rotation=90, fontsize=7) | |
| ax.set_yticks(range(tab.shape[0]), tab.index, fontsize=7.5) | |
| ax.set_title("V2 (IEPE) recordings per cell: condition [MMDD session] × torque set-point × speed (take 0 only)", | |
| loc="left", color=INK) | |
| fig.tight_layout(); fig.savefig("figures/fig1_design_matrix.png", dpi=150); plt.close(fig) | |
| # Fig 2: torque signature at fixed speed: harmonic amplitudes vs torque, same session per row | |
| RPMC = {600: "#b7d3f6", 750: "#86b6ef", 900: "#5598e7", 1050: "#2a78d6", 1200: "#1c5cab", 1460: "#0d366b"} | |
| cases = [("20230804", "Normal"), ("20230811", "Parallel Misalignment (25mm)"), ("20230817", "Unbalance (2P)"), | |
| ("20230915", "Combo(27_23)")] | |
| feats = [("a1_V2H", "1X, V2H"), ("a2_V2A", "2X, V2A"), ("a3_V2A", "3X, V2A")] | |
| fig, axs = plt.subplots(len(cases), 3, figsize=(12, 10.5)) | |
| for i, (s, fo) in enumerate(cases): | |
| g = v[(v.session == int(s)) & (v.folder == fo) & (v["take"] == 0) & (v.dur > 9.9)] | |
| for j, (f, nm) in enumerate(feats): | |
| ax = axs[i, j] | |
| for rpm, col in RPMC.items(): | |
| h = g[g.rpm == rpm] | |
| if h.torque.nunique() < 2: continue | |
| ax.semilogy(h.torque, h[f], "o", ms=4, color=col, alpha=0.8) | |
| mu = h.groupby("torque")[f].mean() | |
| ax.semilogy(mu.index, mu.values, "-", lw=2, color=col, label=f"{rpm} rpm") | |
| ax.set_xticks([20, 25, 30, 35]); ax.grid(color=GRID, lw=0.6) | |
| ax.set_title(f"{fo} [{s[4:]}] · {nm}", loc="left", fontsize=8.5, color=INK) | |
| if i == len(cases) - 1: ax.set_xlabel("torque set-point (N·m)") | |
| if j == 0: ax.set_ylabel("amplitude (a.u.)") | |
| axs[2, 1].legend(frameon=False, fontsize=7.5, ncol=3, loc="lower left") | |
| fig.suptitle("At fixed speed, torque moves the 1X-3X amplitudes; dots = rounds, line = mean (rounds agree, torque levels do not)", | |
| x=0.01, ha="left", color=INK, fontsize=10) | |
| fig.tight_layout(); fig.savefig("figures/fig2_torque_harmonics.png", dpi=150); plt.close(fig) | |
| # Fig 3: healthy baseline across sessions (30 N·m) | |
| h = v[(v.cond == "Normal") & (v.torque == 30) & (v["take"] == 0)] | |
| fig, axs = plt.subplots(1, 3, figsize=(13, 3.6), sharey=False) | |
| for ax, rpm in zip(axs, (600, 900, 1200)): | |
| g = h[h.rpm == rpm] | |
| ss = sorted(g.session.unique()) | |
| for k, s in enumerate(ss): | |
| y = g[g.session == s].a1_V2H | |
| ax.plot([k] * len(y), y, "o", ms=6, color="#2a78d6", mec="#fcfcfb", mew=1.5) | |
| ax.set_xticks(range(len(ss)), [str(s)[4:] for s in ss], rotation=60, fontsize=7.5) | |
| ax.set_title(f"Normal, 30 N·m, {rpm} rpm", loc="left", fontsize=9, color=INK) | |
| ax.grid(axis="y", color=GRID, lw=0.6) | |
| axs[0].set_ylabel("1X amplitude, V2H (a.u.)") | |
| fig.suptitle("Healthy 1X level moves between sessions (each dot = one 10 s recording)", x=0.01, ha="left", color=INK) | |
| fig.tight_layout(); fig.savefig("figures/fig3_healthy_drift.png", dpi=150); plt.close(fig) | |
| print("ok") | |