import copy, sys, time, importlib.util, numpy as np, torch, mne, pandas as pd
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("e4", f"{TRACK}/solvers/cnn_feat_gru_ens_e4.py"); S = importlib.util.module_from_spec(spec); spec.loader.exec_module(S)
win = pd.read_csv(f"{ROOT}/experiments/stft_tree/cache/windows.csv"); rec = pd.read_csv(f"{ROOT}/experiments/stft_tree/cache/recordings.csv")
Xm = np.load(f"{ROOT}/experiments/e4_start/cache/mask_X.npy"); Rm = np.load(f"{ROOT}/experiments/e4_start/cache/mask_raw_f16.npy"); idx = win.groupby("rec").indices
BIDS = f"{TRACK}/data/neural_compet/Interaxon2026Muse/download/nm000287"
def load(i):
    r = rec.iloc[i]; x = mne.io.read_raw_brainvision(f"{BIDS}/{r.subject}/{r.session_id}/eeg/{r.rec_id}_task-sleeponset_eeg.vhdr", preload=True, verbose="ERROR").get_data(picks=S.CH) * 1e6
    n = len(idx[i]); return x[:, : n * 640].astype(np.float32), n
# 1. streaming path of the solver (first window zeroed, state carried) vs the masked caches of experiment E4
m = S.EnsembleModel(n_models=2).eval()
for i in (0, 7, 300):
    x, n = load(i); zi = None; F, Rw = [], []
    for w in range(n):
        xw = x[:, w * 640:(w + 1) * 640].astype(np.float64); xin = xw.copy()
        if zi is None: xin[:, :S.ZERO_SAMPLES] = 0.0
        xf, zi = S.lowpass(xin, zi); F.append(S.featurize(xw[None], xf[None])[0]); Rw.append(xf)
    F = np.stack(F); ref = Xm[idx[i]]; rel = np.abs(F - ref) / (np.abs(ref) + 1e-3)
    print(f"recording {i}: features vs E4 mask cache: max relative diff {rel.max():.1e}; first-window filtered signal vs cache max |diff| {np.abs(np.stack(Rw)[0].astype(np.float16).astype(float) - Rm[idx[i]][0].astype(float)).max():.1e}")
# 2. model: streaming predict == whole-sequence forward, reset_state, deepcopy, no warning
torch.manual_seed(0)
Xc = np.load(f"{ROOT}/experiments/iir_filter/cache/lp45_X.npy")
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); xin = xw.copy()
    if zi is None: xin[:, :128] = 0.0
    xf, zi = S.lowpass(xin, 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)
print(f"streaming vs whole sequence: max |diff| {np.abs(np.array(stream) - np.array([S.median_of(p.numpy()) for p in probs])).max():.2e} s")
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))
# 3. the training batch builder: random start, weights of the cropped recording, mask of the true first window
class Fake(S.Solver):
    def __init__(self): pass
sol = Fake(); rng = np.random.default_rng(0)
y = np.concatenate([np.full(40, 600.0), np.linspace(595, 0, 120)]).astype(np.float32)
r = dict(y=y, raw=torch.zeros(len(y), 4, 640), feat=torch.zeros(len(y), 217), yt=torch.from_numpy(y))
starts = []
for _ in range(400):
    xr, f, yy, w = sol._batch_train([r], rng); starts.append(len(y) - xr.shape[1])
    assert abs(w.sum().item() - (1.0 if starts[-1] > 0 else 1.0 - S.window_weights(y)[0])) < 1e-5 or starts[-1] == 0
    if starts[-1] == 0: assert w[0, 0].item() == 0.0
st = np.array(starts); print(f"random starts: {np.mean(st > 0):.2f} of batches cropped (expect ~0.5), max start {st.max()} windows, uncropped batches mask the first window: ok")
