Step 4: M2, multiple-instance model in PyTorch (shared sensor MLP, attention per family, averaged seeds); the time-split report shared with M1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maximeandClaude Opus 5.5 committed 2026-11-16 19:22:53 +01:00
1 parent 975ebc7c1a
commit fef30b5ef4
4 files changed
+153 -16

No files matched your search

+22
View File
@@ -75,6 +75,28 @@ def show(title, base, new):
print(f" {f:<22}{cell(b, 0):>21}{cell(n, 0):>21}{cell(b, 1):>21}{cell(n, 1):>21}")
CUT = 0.7 # the criterion: trained on the first 70 % of every station, tested on the last 30 %
def report_time_split(cache, data, proba, families, threshold=0.5):
"""The criterion: {station: proba [last 30 %, pilot family]} against the rules on the same instants -> the
regressions station by station and the total, printed; returns (rules, model) counts per station."""
base, new = {}, {}
for s, d in data.items():
cut = int(CUT * len(d["y"]))
rules_yhat = np.load(Path(cache) / "rules" / f"{s}.npz")["yhat"][cut:]
yhat = rules_yhat.copy()
for j, f in enumerate(families):
yhat[:, FAMILIES.index(f)] = proba[s][:, j] >= threshold
keep = lambda c: {f: v for f, v in c.items() if f in families} # noqa: E731
base[s], new[s] = keep(counts(d["y"][cut:], rules_yhat)), keep(counts(d["y"][cut:], yhat))
for f, name, pb, pn in regressions(base[s], new[s]):
print(f" regression: {s}, {f}, {name} {pb:.0f} % -> {pn:.0f} %"
f" (rules {base[s][f]}, model {new[s].get(f)}: labelled, flagged, both)")
show(f"Last {round(100 * (1 - CUT))} % of every station", total(base), total(new))
return base, new
def rules(a):
sys.path[:0] = [str(Path(a.detectors) / "scripts"), str(Path(a.detectors) / "bench")]
import score # noqa: E402