"""Checks of the A9M solver (training augmentations) before training it through benchopt."""
import importlib.util
import sys
import time

import numpy as np
import pandas as pd
import torch

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("a9m", f"{TRACK}/solvers/cnn_feat_gru_ens_a9m.py")
S = importlib.util.module_from_spec(spec); sys.modules["a9m"] = S; spec.loader.exec_module(S)
spec = importlib.util.spec_from_file_location("e4", f"{TRACK}/solvers/cnn_feat_gru_ens_e4.py")
E4 = importlib.util.module_from_spec(spec); sys.modules["e4"] = E4; spec.loader.exec_module(E4)
dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
win = pd.read_csv(f"{ROOT}/experiments/stft_tree/cache/windows.csv")
Rm = np.load(f"{ROOT}/experiments/e4_start/cache/mask_raw_f16.npy", mmap_mode="r")
Xm = np.load(f"{ROOT}/experiments/e4_start/cache/mask_X.npy")
idx = win.groupby("rec").indices

# 1. torch features == numpy features of the solver
rows = np.sort(np.random.default_rng(0).choice(len(win), 3000, replace=False))
x = np.asarray(Rm[rows]).astype(np.float64)
ref, _ = S.window_features(x)
tf = S.TorchFeatures(dev)
out = tf(torch.from_numpy(x).to(dev)).cpu().numpy()
rel = np.abs(out - ref) / (np.abs(ref) + 1e-3)
print(f"1. torch vs numpy features: max relative diff {rel.max():.1e}")
assert rel.max() < 1e-4

# 2. gain map and swap permutation are exact
xt = torch.from_numpy(x).to(dev)
F0 = tf(xt)
lg = torch.tensor([0.12, -0.07, 0.05, -0.15], dtype=torch.float64, device=dev)
d = (tf(xt * (10 ** lg)[None, :, None]) - (F0 + lg @ torch.from_numpy(S.GAIN_MAP).to(dev))).abs().cpu().numpy()
nonart = [i for i in range(S.N_FEAT) if i not in S.ART_COLS]
z = (d[:, nonart] / (Xm.std(0)[nonart] + 1e-9)).max()
print(f"2. gain formula vs recomputed: worst |diff| / column SD {z:.1e}")
assert z < 1e-2
ds = (tf(xt[:, S.SWAP]) - F0[:, torch.tensor(S.SWAP_PERM, device=dev)]).abs().max().item()
print(f"   swap permutation vs recomputed: max |diff| {ds:.1e}")
assert ds < 1e-9

# 3. inference is unchanged: same Member, EnsembleModel and predict as the E4 solver (same weights -> same predictions)
import inspect  # noqa: E402
for name in ("Member", "EnsembleModel", "window_features", "featurize", "lowpass", "median_of"):
    assert inspect.getsource(getattr(S, name)) == inspect.getsource(getattr(E4, name)), name
print("3. inference code identical to the E4 solver: Member, EnsembleModel, window_features, featurize, lowpass, median_of")


# 4. batch builder on real recordings (as fit() prepares them) + a few optimisation steps
class Fake(S.Solver):
    def __init__(self):
        pass


sol = Fake()
recs = []
for r in range(24):
    ix = idx[r]
    F = Xm[ix].astype(np.float64); y = win.y.values[ix].astype(np.float32)
    recs.append(dict(y=y, F=F, raw=torch.from_numpy(np.asarray(Rm[ix])).to(dev), w=S.window_weights(y)))
allf = np.concatenate([r["F"] for r in recs])
mean, std = allf.mean(0), allf.std(0) + 1e-6
sol._mu = torch.from_numpy(mean).to(dev); sol._sd = torch.from_numpy(std).to(dev); sol._tf = tf
sol._gain_map = torch.from_numpy(S.GAIN_MAP).to(dev); sol._swap_perm = torch.tensor(S.SWAP_PERM, device=dev)
sol._art = torch.tensor(S.ART_COLS, device=dev)
for r in recs:
    r["Fg"] = torch.from_numpy(r["F"]).to(dev)
    r["feat"] = torch.from_numpy(np.clip((r["F"] - mean) / std, -8, 8).astype(np.float32)).to(dev)
    r["yt"] = torch.from_numpy(r["y"]).to(dev); r["wt"] = torch.from_numpy(r["w"]).to(dev)
rng = np.random.default_rng(0)
torch.manual_seed(0)
t0 = time.time()
for _ in range(20):
    xr, f, y, w = sol._batch_train(recs[:16], rng)
    assert torch.isfinite(xr).all() and torch.isfinite(f).all() and torch.isfinite(y).all()
    print(f"   f max {f.abs().max().item():.4f}  y min {y.min().item():.4f}  y max {y.max().item():.6f}") if _ == 0 else None
    assert (f.abs() <= 8).all() and (y >= 0).all() and (y <= 600 + 1e-3).all()
    sums = w.sum(1).cpu().numpy()
    assert ((sums > 0.8) & (sums <= 1.0 + 1e-5)).all(), sums                  # 1, or 1 minus the masked first window
print(f"4. batch builder: 20 batches of 16 recordings in {time.time() - t0:.1f} s; finite, features within +-8, "
      f"targets in [0, 600], weights sum to <= 1 per recording")
m = S.Member().to(dev).train()
opt = torch.optim.AdamW(m.parameters(), lr=2e-3)
for i in range(30):
    xr, f, y, w = sol._batch_train(recs[:16], rng)
    ce = -(S.soft_targets(y) * torch.log_softmax(m(xr, f)[0].float(), -1)).sum(-1)
    loss = (w * ce).sum(1).mean()
    opt.zero_grad(); loss.backward(); opt.step()
    if i in (0, 29):
        print(f"   step {i + 1}: loss {loss.item():.3f}")
print("all checks passed")
