"""Next Signal: self-supervised representations for inertial sensor windows (UCI HAR). Shared by the notebook, inference.py and (ported) web/core.js: * normalise: per-channel standardisation with statistics from the pre-training subjects * encode: frozen 1-D CNN encoder exported to nlrt (BatchNorm folded) -> 64-d embedding * stats_features: 54 hand-made features (6 per channel) * probe: standardise + multinomial logistic regression (weights fitted in the notebook) * perturb: deterministic noise / amplitude / circular time-shift used by the Space """ import numpy as np from nlrt import Mulberry32, forward ACTIVITIES = ["walking", "walking upstairs", "walking downstairs", "sitting", "standing", "lying"] CHANNELS = ["body_acc_x", "body_acc_y", "body_acc_z", "body_gyro_x", "body_gyro_y", "body_gyro_z", "total_acc_x", "total_acc_y", "total_acc_z"] def normalise(x, mean, std): return (np.asarray(x, dtype=np.float64) - np.asarray(mean)[:, None]) / np.asarray(std)[:, None] def encode(layers, xn): """xn: (N, 9, 128) normalised windows -> (N, 64).""" return forward(layers, xn) def stats_features(x): """x: (N, 9, 128) raw windows -> (N, 54): mean, std, min, max, mean |diff|, mean square per channel.""" x = np.asarray(x, dtype=np.float64) mean = x.mean(axis=2) std = np.sqrt(((x - mean[:, :, None]) ** 2).mean(axis=2)) feats = [mean, std, x.min(axis=2), x.max(axis=2), np.abs(np.diff(x, axis=2)).mean(axis=2), (x * x).mean(axis=2)] return np.stack(feats, axis=2).reshape(len(x), -1) def probe_proba(probe, Z): Zs = (np.asarray(Z) - probe["mu"]) / probe["sd"] z = Zs @ np.asarray(probe["W"]).T + probe["b"] z = z - z.max(axis=1, keepdims=True) e = np.exp(z) return e / e.sum(axis=1, keepdims=True) def perturb(x, noise=0.0, scale=1.0, shift=0, seed=0): """x: (9, 128) raw window. Adds Gaussian noise (std = noise x channel std), scales amplitude, shifts in time.""" x = np.asarray(x, dtype=np.float64) rng = Mulberry32(seed) sd = np.sqrt(((x - x.mean(axis=1, keepdims=True)) ** 2).mean(axis=1)) out = np.empty_like(x) for c in range(x.shape[0]): for t in range(x.shape[1]): out[c, t] = x[c, (t - shift) % x.shape[1]] * scale + noise * sd[c] * rng.normal() return out