File size: 3,361 Bytes
651cebd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Rebuild cache2023/ from raw/ (same processing as Interpretable-motor/code/r11/cache.py, paths adapted to this dataset).

V2 (IEPE) recordings with rpm > 0, no knock test -> remove the per-channel mean -> decimate 51.2 kHz -> 2560 Hz with zero-phase FIR
filters in two stages (x4, then x5; removes the 8.1 kHz carrier) -> first 10 s, zero-padded ->
  cache2023/x2560.npy     (N, 25600, 3) float32, channels V2V, V2H, V2A
  cache2023/meta.parquet  one row per file (row i <-> x2560[i]); n_valid = number of valid samples before the zero padding
Run from the dataset root (needs raw/ and metadata/files_full.csv):  python scripts/build_cache2023.py [--out cache2023] [--procs 16]
"""
import argparse
from multiprocessing import Pool
from pathlib import Path
import numpy as np, pandas as pd
from scipy import signal

ROOT = Path(__file__).resolve().parents[1]; RAW = ROOT / "raw"; FS = 2560; NMAX = 10 * FS


def family_group(cond):
    if cond == "Normal": return "H", "H"
    if cond.startswith("Combo"): return "UPA", "UPA"
    sev = lambda s: s.split("(")[1].split(")")[0]
    if cond.startswith("Unbalance(1P)_ParallelAlign"): return "UP", "UP" + sev(cond.split("_")[1])
    if cond.startswith("Unbalance(1P)_AngularAlign"): return "UA", "UA" + sev(cond.split("_")[1])
    if cond.startswith("Unbalance"): return "U", "U" + sev(cond)
    if cond.startswith("ParallelAlign"): return "P", "P" + sev(cond)
    if cond.startswith("AngularAlign"): return "A", "A" + sev(cond)
    raise ValueError(cond)


def load_one(r):
    p = RAW / str(r["session"]) / r["folder"] / r["sensor"] / r["file"]
    x = pd.read_csv(p, skiprows=5 if r["header"] else 0, header=None, usecols=[1, 2, 3]).dropna().to_numpy(np.float64)
    x = x - x.mean(0)
    y = signal.decimate(signal.decimate(x, 4, ftype="fir", zero_phase=True, axis=0), 5, ftype="fir", zero_phase=True, axis=0)
    return y[:NMAX].astype(np.float32)


def index():
    m = pd.read_csv(ROOT / "metadata" / "files_full.csv")
    return m[(m.sensor != "CurrentS") & (m.rpm > 0) & (m.cond != "KnockRadial")].reset_index(drop=True)


def main(out, procs):
    m = index()
    with Pool(procs) as pool: xs = pool.map(load_one, m.to_dict("records"), chunksize=4)
    X = np.zeros((len(xs), NMAX, 3), np.float32); nv = np.zeros(len(xs), int)
    for i, x in enumerate(xs): X[i, :len(x)] = x; nv[i] = len(x)
    fg = [family_group(c) for c in m.cond]; day = m.session.astype(str).str[4:]
    meta = pd.DataFrame(dict(file=m.file, folder=m.folder, day=day, family=[f for f, _ in fg], group=[g for _, g in fg],
                             torque=m.torque.astype(int), rpm=m.rpm.astype(int), rpm_meas=(m.f1 * 60).round(1),
                             round=m.rep.astype(int), take=m["take"].astype(int), block=np.where(day.isin(["0828", "0829"]), "B", "A"),
                             n_valid=nv, t0=m.t0))
    meta["mislabel"] = (meta.day == "0817") & (meta.family == "H") & (meta.rpm == 600)   # IEPE 35 N*m vs CurrentS 30 N*m
    out = ROOT / out; out.mkdir(parents=True, exist_ok=True)
    np.save(out / "x2560.npy", X); meta.to_parquet(out / "meta.parquet", index=False); print("saved", X.shape, "->", out)


if __name__ == "__main__":
    ap = argparse.ArgumentParser(); ap.add_argument("--out", default="cache2023"); ap.add_argument("--procs", type=int, default=16); a = ap.parse_args()
    main(a.out, a.procs)