Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ desktop.ini

# Templates are always committed, even if a rule above matches them.
!*.example

# Trained models, rebuilt by ml/train.py from data/raw.
ml/models/
101 changes: 101 additions & 0 deletions docs/results/2026-11-22-first-model/checks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
Windows per device and distance (5 s windows, 1 s step)
dist 0.5 1.0 1.5 2.0 2.5 3.0 3.5
device
earpiece B class 55 55 55 54 54 55 52
phone B 55 55 55 54 55 55 56
phone B class 55 54 55 0 0 0 0
phone C class 55 55 53 53 55 7 49

== Band, three classes ==
all features leave one session out accuracy 62.9% balanced 60.8%
all features leave one device out accuracy 62.6% balanced 63.6%
BLE only (no spectrum) leave one session out accuracy 44.3% balanced 46.9%
BLE only (no spectrum) leave one device out accuracy 53.3% balanced 55.7%
spectrum only leave one session out accuracy 42.8% balanced 35.8%
spectrum only leave one device out accuracy 45.2% balanced 42.9%

== Band without the 1.0 m and 2.0 m sessions (they sit on band edges) ==
all features: accuracy 71.1% balanced 67.7%
predicted far mid near
true
far 355 82 1
mid 156 61 1
near 8 5 207

BLE only (no spectrum): accuracy 53.4% balanced 52.0%
predicted far mid near
true
far 251 186 1
mid 208 9 1
near 4 8 208

== Mid vs far only (chance is 50% balanced) ==
all features leave one session out accuracy 70.0% balanced 70.1%
all features leave one device out accuracy 62.4% balanced 55.9%
BLE only (no spectrum) leave one session out accuracy 48.7% balanced 48.9%
BLE only (no spectrum) leave one device out accuracy 55.9% balanced 53.7%

Mid vs far, BLE only, leave one session out: windows predicted per session
predicted far mid
device dist
earpiece B class 1.0 7 48
1.5 18 37
2.0 52 2
2.5 39 15
3.0 30 25
3.5 49 3
phone B 1.0 0 55
1.5 33 22
2.0 41 13
2.5 17 38
3.0 22 33
3.5 14 42
phone B class 1.0 14 40
1.5 52 3
phone C class 1.0 0 55
1.5 44 9
2.0 52 1
2.5 8 47
3.0 0 7
3.5 41 8

Mid vs far, BLE only, one model per device (leave one session out)
earpiece B class: accuracy 60.0% balanced 60.0% (6 sessions)
phone B: accuracy 57.6% balanced 57.6% (6 sessions)
phone B class: only one band recorded, skipped
phone C class: accuracy 48.9% balanced 48.7% (6 sessions)

== Spectrum by session, in recording order ==
The nRF24 sees the whole room. If these move with the clock and not with distance,
the spectrum features describe the room's Wi-Fi traffic, not the test device.
minute device dist spec_active spec_mean_frac rssi_median
0 0 phone B 0.500 68.000 0.051 -63.500
1 5 phone B 1.000 77.000 0.064 -67.000
2 6 phone B 1.500 72.000 0.052 -78.000
3 9 phone B 2.000 75.000 0.056 -78.000
4 10 phone B 2.500 75.000 0.067 -77.500
5 11 phone B 3.000 75.000 0.068 -79.000
6 13 phone B 3.500 76.000 0.073 -79.000
7 95 phone B class 0.500 31.000 0.017 -62.000
8 97 phone B class 1.000 31.000 0.017 -74.000
9 98 phone B class 1.500 32.000 0.016 -86.000
10 120 phone C class 0.500 36.000 0.019 -65.000
11 122 phone C class 1.000 36.000 0.020 -78.000
12 124 phone C class 1.500 32.000 0.016 -95.000
13 125 phone C class 2.000 31.000 0.016 -95.000
14 127 phone C class 2.500 33.000 0.017 -98.000
15 128 phone C class 3.000 36.000 0.022 -92.000
16 130 phone C class 3.500 36.000 0.020 -99.000
17 194 earpiece B class 0.500 38.000 0.021 -64.000
18 196 earpiece B class 1.000 32.000 0.018 -73.500
19 198 earpiece B class 1.500 28.000 0.017 -85.000
20 199 earpiece B class 2.000 29.000 0.017 -78.000
21 202 earpiece B class 2.500 51.500 0.035 -82.750
22 203 earpiece B class 3.000 45.000 0.061 -77.500
23 204 earpiece B class 3.500 45.000 0.022 -80.000

== Type (phone vs earpiece) ==
all features leave one session out accuracy 100.0% balanced 100.0%
no manufacturer columns leave one session out accuracy 97.9% balanced 97.3%
BLE timing and RSSI only leave one session out accuracy 96.9% balanced 96.4%
Leave one device out is not possible for type: there is only one earpiece.
4 changes: 4 additions & 0 deletions docs/results/2026-11-22-first-model/confusion_band.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
,near,mid,far
near,98,121,1
mid,85,317,196
far,1,102,335
3 changes: 3 additions & 0 deletions docs/results/2026-11-22-first-model/confusion_type.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
,phone,earpiece
phone,876,0
earpiece,0,380
189 changes: 189 additions & 0 deletions docs/results/2026-11-22-first-model/make_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
"""Train and check the first device-type and distance-band models.

Run from the repo root (needs the labelled sessions in data/raw/, which stay on the laptop):
python docs/results/2026-11-22-first-model/make_checks.py

Writes next to this file:
train_output.md the standard ml.train report (5 grouped folds, whole sessions held out)
confusion_type.csv, confusion_band.csv
checks.txt the extra checks the report quotes (other splits, feature subsets)
rssi_by_distance.png window RSSI by distance, one panel per device
and saves the two models, trained on every session, to ml/models/ (gitignored).

No hashed addresses are written: rows are grouped by device model and distance.
"""

from __future__ import annotations

import json
import sys
from pathlib import Path

import joblib
import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score, balanced_accuracy_score
from sklearn.model_selection import LeaveOneGroupOut

sys.path.insert(0, ".")
from ml.features import BLE_COLS, FEATURE_COLS, MFR_COLS, SPECTRUM_COLS, dataset # noqa: E402
from ml.train import evaluate, make_model, report_md # noqa: E402

RAW = Path("data/raw")
OUT = Path(__file__).resolve().parent
MODELS = Path("ml/models")

SUBSETS = {
"all features": FEATURE_COLS,
"BLE only (no spectrum)": BLE_COLS + MFR_COLS,
"spectrum only": SPECTRUM_COLS,
}

# Brand tokens (docs/brand/tokens.json, light theme).
INK, INK_MUTED, LINE, PAPER = "#0E1726", "#4B5871", "#D3DAE4", "#FFFFFF"
BAND_COLORS = {"near": "#1F6FEB", "mid": "#D97706", "far": "#6B7280"}


def load() -> pd.DataFrame:
df = dataset(RAW)
info = {}
for s in df["session"].unique():
meta = json.loads((RAW / s / "session.json").read_text(encoding="utf-8"))
dev = next(iter(meta["devices"].values()))
info[s] = (dev["model"], float(dev["distance_m"]))
df["device"] = df["session"].map(lambda s: info[s][0])
df["dist"] = df["session"].map(lambda s: info[s][1])
return df


def cv_predict(df: pd.DataFrame, target: str, cols: list[str], group: str) -> tuple[np.ndarray, np.ndarray]:
"""Leave one group out: each session (or device) is predicted by a model that never saw it."""
X, y, g = df[cols].to_numpy(dtype=float), df[target].to_numpy(), df[group]
pred = np.empty(len(y), dtype=object)
for tr, te in LeaveOneGroupOut().split(X, y, g):
assert not set(g.iloc[tr]) & set(g.iloc[te])
pred[te] = make_model().fit(X[tr], y[tr]).predict(X[te])
return y, pred


def score(y, pred) -> str:
return f"accuracy {accuracy_score(y, pred):6.1%} balanced {balanced_accuracy_score(y, pred):6.1%}"


def checks(df: pd.DataFrame) -> str:
out = []
w = out.append

w("Windows per device and distance (5 s windows, 1 s step)")
w(df.groupby(["device", "dist"]).size().unstack(fill_value=0).to_string())
w("")

w("== Band, three classes ==")
for name, cols in SUBSETS.items():
for group, label in (("session", "leave one session out"), ("device", "leave one device out")):
y, p = cv_predict(df, "band", cols, group)
w(f"{name:24s} {label:22s} {score(y, p)}")
w("")

core = df[df["dist"].isin([0.5, 1.5, 2.5, 3.0, 3.5])]
w("== Band without the 1.0 m and 2.0 m sessions (they sit on band edges) ==")
for name in ("all features", "BLE only (no spectrum)"):
y, p = cv_predict(core, "band", SUBSETS[name], "session")
w(f"{name}: {score(y, p)}")
w(pd.crosstab(pd.Series(y, name="true"), pd.Series(p, name="predicted")).to_string())
w("")

mf = df[df["band"].isin(["mid", "far"])]
w("== Mid vs far only (chance is 50% balanced) ==")
for name in ("all features", "BLE only (no spectrum)"):
for group, label in (("session", "leave one session out"), ("device", "leave one device out")):
y, p = cv_predict(mf, "band", SUBSETS[name], group)
w(f"{name:24s} {label:22s} {score(y, p)}")
w("")
y, p = cv_predict(mf, "band", SUBSETS["BLE only (no spectrum)"], "session")
w("Mid vs far, BLE only, leave one session out: windows predicted per session")
w(pd.crosstab([mf["device"], mf["dist"]], pd.Series(p, index=mf.index, name="predicted")).to_string())
w("")
w("Mid vs far, BLE only, one model per device (leave one session out)")
for dev, g in mf.groupby("device"):
if g["band"].nunique() < 2:
w(f"{dev}: only one band recorded, skipped")
continue
y, p = cv_predict(g, "band", SUBSETS["BLE only (no spectrum)"], "session")
w(f"{dev}: {score(y, p)} ({g['session'].nunique()} sessions)")
w("")

w("== Spectrum by session, in recording order ==")
w("The nRF24 sees the whole room. If these move with the clock and not with distance,")
w("the spectrum features describe the room's Wi-Fi traffic, not the test device.")
first = df.groupby("session")["t_start"].min().sort_values()
s = df.groupby("session")[["spec_active", "spec_mean_frac", "rssi_median"]].median().loc[first.index]
s.insert(0, "minute", ((first - first.iloc[0]) / 60000).round().astype(int).to_numpy())
s.insert(1, "device", [df.loc[df["session"] == x, "device"].iat[0] for x in s.index])
s.insert(2, "dist", [df.loc[df["session"] == x, "dist"].iat[0] for x in s.index])
w(s.reset_index(drop=True).to_string(float_format=lambda v: f"{v:.3f}"))
w("")

w("== Type (phone vs earpiece) ==")
type_subsets = {
"all features": FEATURE_COLS,
"no manufacturer columns": BLE_COLS + SPECTRUM_COLS,
"BLE timing and RSSI only": BLE_COLS,
}
for name, cols in type_subsets.items():
y, p = cv_predict(df, "type", cols, "session")
w(f"{name:26s} leave one session out {score(y, p)}")
w("Leave one device out is not possible for type: there is only one earpiece.")
return "\n".join(out) + "\n"


def plot(df: pd.DataFrame) -> None:
devices = sorted(df["device"].unique())
fig, axes = plt.subplots(1, len(devices), figsize=(3.2 * len(devices), 3.6), sharey=True)
fig.patch.set_facecolor(PAPER)
for ax, dev in zip(axes, devices):
g = df[df["device"] == dev]
dists = sorted(g["dist"].unique())
data = [g.loc[g["dist"] == d, "rssi_median"] for d in dists]
parts = ax.boxplot(data, positions=range(len(dists)), widths=0.6, patch_artist=True, showfliers=False)
for box, d in zip(parts["boxes"], dists):
band = g.loc[g["dist"] == d, "band"].iat[0]
box.set(facecolor=BAND_COLORS[band], edgecolor=INK, alpha=0.85)
for med in parts["medians"]:
med.set(color=INK)
ax.set_xticks(range(len(dists)), [f"{d:g}" for d in dists])
ax.set_title(dev, color=INK, fontsize=10)
ax.set_xlabel("distance (m)", color=INK_MUTED)
ax.grid(axis="y", color=LINE)
ax.set_axisbelow(True)
axes[0].set_ylabel("median RSSI per 5 s window (dBm)", color=INK_MUTED)
handles = [plt.Rectangle((0, 0), 1, 1, color=c) for c in BAND_COLORS.values()]
fig.legend(handles, BAND_COLORS, loc="upper right", frameon=False, ncol=3)
fig.suptitle("Window RSSI by distance: mid and far overlap", x=0.01, ha="left", color=INK)
fig.tight_layout(rect=(0, 0, 1, 0.92))
fig.savefig(OUT / "rssi_by_distance.png", dpi=150)


def main() -> None:
df = load()
results = [evaluate(df, t) for t in ("type", "band")]
(OUT / "train_output.md").write_text(report_md(results, RAW), encoding="utf-8")
MODELS.mkdir(parents=True, exist_ok=True)
for r in results:
pd.DataFrame(r["confusion"], index=r["labels"], columns=r["labels"]).to_csv(OUT / f"confusion_{r['target']}.csv")
joblib.dump(
{"model": r["model"], "features": FEATURE_COLS, "classes": r["labels"]},
MODELS / f"{r['target']}.joblib",
)
print(f"{r['target']}: unseen sessions {r['test_acc']:.1%}, training data {r['train_acc']:.1%}")
(OUT / "checks.txt").write_text(checks(df), encoding="utf-8")
plot(df)
print(f"wrote {OUT}")


if __name__ == "__main__":
main()
Loading