import copy, sys, time, importlib.util, numpy as np, torch, mne, pandas as pd, os
ROOT = "/root/disk_3tb/Neural_Interfaces_2026_Track_3_Sleep_Onset"
TRACK = f"{ROOT}/2026-competition/tracks/sleep_onset"
sys.path.insert(0, TRACK)
spec = importlib.util.spec_from_file_location("ens", f"{TRACK}/solvers/cnn_feat_gru_ens.py"); S = importlib.util.module_from_spec(spec); spec.loader.exec_module(S)
print("n features:", S.N_FEAT, "| artifact columns:", [S.FEATURE_NAMES[i] for i in S.ART_COLS][:3], "... (", len(S.ART_COLS), ")")

win = pd.read_csv(f"{ROOT}/experiments/stft_tree/cache/windows.csv"); rec = pd.read_csv(f"{ROOT}/experiments/stft_tree/cache/recordings.csv")
Xc = np.load(f"{ROOT}/experiments/iir_filter/cache/lp45_X.npy"); idx = win.groupby("rec").indices
BIDS = f"{TRACK}/data/neural_compet/Interaxon2026Muse/download/nm000287"
def load(i):
    r = rec.iloc[i]; vhdr = f"{BIDS}/{r.subject}/{r.session_id}/eeg/{r.rec_id}_task-sleeponset_eeg.vhdr"
    x = mne.io.read_raw_brainvision(vhdr, preload=True, verbose="ERROR").get_data(picks=S.CH) * 1e6
    n = len(idx[i]); return x[:, : n * 640].astype(np.float32), n       # what the loader hands over: float32 microvolts

# 1. features: streaming window by window (state carried) vs the cache used in all experiments
worst = 0
for i in (0, 7, 300):
    x, n = load(i); zi = None; F = []
    for w in range(n):
        xw = x[:, w * 640:(w + 1) * 640].astype(np.float64)
        xf, zi = S.lowpass(xw, zi); F.append(S.featurize(xw[None], xf[None])[0])
    F = np.stack(F); ref = Xc[idx[i]]
    d = np.abs(F - ref); rel = d / (np.abs(ref) + 1e-3)
    worst = max(worst, rel.max()); print(f"recording {i}: {n} windows, max |diff| {d.max():.2e}, max relative diff {rel.max():.2e}, worst column {S.FEATURE_NAMES[int(rel.max(0).argmax())]}")
print("features match the experiment cache:", worst < 1e-3)

# 2. model: streaming predict == whole-sequence forward (same featurisation), random weights
torch.manual_seed(0)
m = S.EnsembleModel(n_models=3); m.feat_mean.copy_(torch.from_numpy(Xc.mean(0))); m.feat_std.copy_(torch.from_numpy(Xc.std(0) + 1e-6)); m.eval()
x, n = load(7); n = min(n, 60)
stream = [m.predict(torch.from_numpy(x[:, w * 640:(w + 1) * 640][None]))[0].item() for w in range(n)]
zi = None; raws, feats = [], []
for w in range(n):
    xw = x[:, w * 640:(w + 1) * 640].astype(np.float64); xf, zi = S.lowpass(xw, zi); raws.append(xf.astype(np.float32)); feats.append(m.standardise(S.featurize(xw[None], xf[None])[0]).astype(np.float32))
xr = torch.from_numpy(np.stack(raws))[None]; ft = torch.from_numpy(np.stack(feats))[None]
with torch.no_grad():
    probs = sum(torch.softmax(mem(xr, ft)[0][0], -1) for mem in m.members) / len(m.members)
seq = [S.median_of(p.numpy()) for p in probs]
print(f"streaming vs whole sequence: max |diff| {np.abs(np.array(stream) - np.array(seq)).max():.2e} s")

# 3. reset_state and deepcopy
m.reset_state(); again = [m.predict(torch.from_numpy(x[:, w * 640:(w + 1) * 640][None]))[0].item() for w in range(n)]
c = copy.deepcopy(m); c.reset_state(); third = [c.predict(torch.from_numpy(x[:, w * 640:(w + 1) * 640][None]))[0].item() for w in range(n)]
print("after reset_state identical:", np.allclose(stream, again), "| deepcopy + reset identical:", np.allclose(stream, third))

# 4. latency per window
for nm, dev in ((5, "cuda"), (8, "cuda"), (5, "cpu")):
    mm = S.EnsembleModel(n_models=nm).to(dev).eval(); mm.feat_mean.copy_(torch.from_numpy(Xc.mean(0))); mm.feat_std.copy_(torch.from_numpy(Xc.std(0) + 1e-6))
    X1 = torch.from_numpy(x[:, :640][None]).to(dev)
    for _ in range(5): mm.predict(X1)
    t = time.time()
    for w in range(40): mm.predict(torch.from_numpy(x[:, (w % n) * 640:(w % n + 1) * 640][None]).to(dev))
    print(f"{nm} members on {dev}: {1000 * (time.time() - t) / 40:.1f} ms per window")
