"""stem_ms96 (eeg-thao.zip second stage) on a converted test set, for comparison with the main ensemble.

NOT a submission candidate as it stands: on synthetic switches it learned the corpus structure (column 0 attended in the
first 24 s, schedule >= 30 s; REAL_SWITCH_STRATEGY.md), which the organisers warn against; on real switches it is below
chance (stem_kappa_summary.txt). Run on request (2026-11-11) to look at its test-set output.

Input: the 4 all-five DirectNet sw2 logits of the test trials (results/submission/<SET>/clf_s0..3.pkl, the same models
whose AASD / Carta evidence eval_stem_external.py used). The 24 causal 'ms' features go through each of the five fold
models (weights_only=True, numpy globals only; nothing from the zip is executed); variant orig = f(x), sym =
(f(x) - f(-x)) / 2. One stream per trial = mean of the five fold models; HMM emissions from the corpus streams of the
same mean (5 datasets, usual schedule), p_stay 0.998; labels as sub_fuse.py (lookahead 0.75 + stamping = 1 s).
--check: the same procedure on AASD / Carta2026 (stored evidence), scored with the kappa scorer.
usage: python sub_stem.py --set TEST_DLAAD --out <dir>/stem_orig_dlaad [--variant orig|sym] | --check
"""
import argparse
import os
import pickle
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)

import numpy as np  # noqa: E402
import torch  # noqa: E402

import score as S  # noqa: E402
import sub_fuse as SF  # noqa: E402
from common import DS_KEYS  # noqa: E402
from common_exp import EXP  # noqa: E402
from eval_stem_external import EXT_TAG, LogitTransformer, emission, ms_features, streams_of  # noqa: E402
from stem_kappa import CLF, allow_numpy  # noqa: E402

CKPT = os.path.join(EXP, "..", "..", "eeg-thao_unzipped", "eeg-thao", "models", "stem_ms96_image_split")
P_STAY, LA = 0.998, 0.75


def models(dev):
    allow_numpy(CKPT)
    out = []
    for held in DS_KEYS:
        ck = torch.load(os.path.join(CKPT, f"stem_ms96__held-{held}.pt"), map_location="cpu", weights_only=True)
        m = LogitTransformer().to(dev)
        m.load_state_dict(ck["state"])
        m.eval()
        out.append((m, np.asarray(ck["mu"], np.float32), np.asarray(ck["sd"], np.float32)))
    return out


def stream(ms, X, vi, dev):
    return np.mean([streams_of(m, X, mu, sd, dev)[vi] for m, mu, sd in ms], axis=0)


def corpus_emission(ms, vi, dev):
    evs = [S.load_folds(c, "causal")[0] for c in CLF]
    st, att = {}, {}
    for ds in DS_KEYS:
        for k in evs[0][ds]:
            st[k] = stream(ms, ms_features(np.stack([(e[ds][k]["r0"] - e[ds][k]["r1"]) for e in evs], 1)), vi, dev)
            att[k] = evs[0][ds][k]["att"]
    return emission(st, att)


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--set")
    ap.add_argument("--out")
    ap.add_argument("--variant", default="orig", choices=("orig", "sym"))
    ap.add_argument("--check", action="store_true")
    a = ap.parse_args()
    torch.set_num_threads(2)
    dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    vi = ("orig", "sym").index(a.variant)
    ms = models(dev)
    mu, sd = corpus_emission(ms, vi, dev)
    print(f"stem_{a.variant}: corpus emissions mu {mu:.4f} sd {sd:.4f}", flush=True)
    from e1_switch import integ_hmm
    if a.check:
        import eval_external as E
        import r1_acc_decision_grid as R
        R.install_scorer()
        for ds in ("AASD", "Carta2026"):
            E4 = [pickle.load(open(os.path.join(EXP, "results", "research", f"ext_clf_{ds}{EXT_TAG}_evidence_s{i}.pkl"), "rb")) for i in range(4)]
            ev = {k: dict(E4[0][k], r0=stream(ms, ms_features(np.stack([(e[k]["r0"] - e[k]["r1"]).astype(np.float64) for e in E4], 1)), vi, dev),
                          r1=np.zeros(len(E4[0][k]["r0"]))) for k in E4[0]}
            out = E.score(ev, mu, sd, P_STAY, LA)
            print(f"check stem_{a.variant} (mean of 5 fold models) {ds}: Acc {out['Acc']:.2f} H {out['H']:.4f} SDT {out['SDT']:.2f}", flush=True)
        sys.exit(0)
    ec = SF.load_evidence(a.set, "clf")

    def decide(k):
        X = ms_features(np.stack([(e[k]["r0"] - e[k]["r1"]).astype(np.float64) for e in ec], 1))
        return integ_hmm(stream(ms, X, vi, dev), dict(p_stay=P_STAY, mu=mu, sd=sd)), ec[0][k]["t_end"], LA
    SF.write_predictions(a.set, a.out, decide, f"stem_{a.variant}")
