"""Reference channels of every device under the five readings of the paper. six_random calibrator on the six random context windows of the device (Tab. 1 row 3, Tab. 2 'Calibrator') six_of_twelve calibrator on the six of twelve context windows it selects itself (Tab. 1 last row, abstract) walking_frame training-free walking frame from the gait windows among the six (Tab. 2 'Walking frame') zeroed reference channels zero everywhere (Tab. 2 'Zeroed') one_window calibrator on one of the six windows, numpy.random.default_rng([2000, k, g]) (Sec. 4.3) The calibrator always runs on CPU in float32 with the batching of the paper: one pass over all devices of a dataset and draw; for 6 of 12, one pass per elimination round over all candidate subsets of all devices. Devices of groups with fewer than 7 windows keep zero reference channels. """ from __future__ import annotations import numpy as np from . import processing as PS from .protocol import Draw READINGS = ("six_random", "six_of_twelve", "walking_frame", "zeroed", "one_window") CHUNK = 4096 def device_input(model, X, rows, site): """Calibrator input of one device from its windows `rows` at `site`. Whether the device has a gyroscope is a property of the dataset's sensor at that site.""" has_gyro = bool(np.abs(X[:, :, site, 3:6]).sum() > 0) w, v = PS.calibrator_input(X[rows][:, :, site], has_gyro, model.config.force_scale, model.config.rate_scale) return w, v, has_gyro def calibrate(model, X, keys, rows_of): """{(group, site): reference (3, 3)} in one calibrator pass; rows_of(group) gives the context rows.""" if not keys: return {} ws, vs, missing = [], [], [] for g, s in keys: w, v, has_gyro = device_input(model, X, rows_of(g), s) ws.append(w) vs.append(v) missing.append(not has_gyro) res = model.calibrate_batch(np.stack(ws), np.stack(vs), [s for _, s in keys], missing) return {key: res["reference"][i] for i, key in enumerate(keys)} def six_of_twelve(model, X, draw: Draw): """{(group, site): reference} for the devices whose group has a twelve-window pool, by greedy elimination.""" pool = {} for g, s in draw.keys: if g in draw.pools: w, v, has_gyro = device_input(model, X, draw.pools[g], s) pool[(g, s)] = (w, v, s, not has_gyro) store = {} def score(requests): need = list(dict.fromkeys(q for q in requests if q not in store)) by_size = {} for q in need: by_size.setdefault(len(q[1]), []).append(q) for _, qs in sorted(by_size.items()): for c0 in range(0, len(qs), CHUNK): chunk = qs[c0:c0 + CHUNK] res = model.calibrate_batch(np.stack([pool[q[0]][0][list(q[1])] for q in chunk]), np.stack([pool[q[0]][1][list(q[1])] for q in chunk]), [pool[q[0]][2] for q in chunk], [pool[q[0]][3] for q in chunk]) sc = PS.tilt_score(res["M"], res["V"], res["s"], res["logit"]) for i, q in enumerate(chunk): store[q] = (res["reference"][i], float(sc[i])) return {q: store[q][1] for q in requests} if not pool: return {} chosen = PS.select_by_elimination({key: 12 for key in pool}, score, keep=6) return {key: store[(key, chosen[key])][0] for key in pool} def put(Xr, X, groups, key, reference): """Reference channels of one device := Q g_t on its valid frames.""" g, s = key rows = np.where(groups == g)[0] Xr[rows, :, s] = PS.set_reference(X[rows][:, :, s], reference) def reading_inputs(model, X, groups, draw: Draw, readings): """{reading: X with the reference channels of that reading} for one draw.""" out = {} six = None if {"six_random", "six_of_twelve"} & set(readings): six = calibrate(model, X, draw.keys, lambda g: draw.rows[g]) for r in readings: if r == "walking_frame": out[r] = draw.walking elif r == "zeroed": Xz = X.copy() Xz[..., 6:9] = 0.0 out[r] = Xz else: if r == "six_random": refs = six elif r == "six_of_twelve": sel = six_of_twelve(model, X, draw) refs = {key: sel.get(key, six[key]) for key in draw.keys} elif r == "one_window": sub = {g: np.sort(np.random.default_rng([2000, draw.k, int(g)]).choice(rows, 1, replace=False)) for g, rows in draw.rows.items()} refs = calibrate(model, X, draw.keys, lambda g: sub[g]) else: raise ValueError(r) Xr = draw.walking.copy() for key in draw.keys: put(Xr, X, groups, key, refs[key]) out[r] = Xr return out