"""F10 pre-check: is a heartbeat visible in the Muse channels? numpy/scipy only.

Per 5 s window of the UNFILTERED signal, per channel: band-pass 5-30 Hz (QRS energy), envelope = |x| smoothed over 80 ms, autocorrelation
of the envelope; the heartbeat score is the largest autocorrelation at lags 0.4-1.5 s (40-150 bpm) minus the mean at lags 0.15-0.3 s
(no-beat baseline shape). Reference distribution: the same score for the window of the SAME recording shifted by half a window
period in the other channel pair (TP vs AF) and, as a null, for time-reversed envelopes of the AF channels (which carry no ECG).
Also reports whether the best lag agrees between TP9 and TP10 (a real heartbeat gives the same period in both) and whether the
period changes between the first and last minutes of the recordings (the quantity a feature would use).
usage: python f10_ecg_check.py [n_recordings, default 80]
"""
import os
import sys

import mne
import numpy as np
from scipy.signal import butter, sosfiltfilt

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
sys.path.insert(0, os.path.join(ROOT, "experiments/stft_tree"))
import run_stft_tree as R  # noqa: E402

FS, WIN = 128, 640
N = int(sys.argv[1]) if len(sys.argv) > 1 else 80
_, _, win, rec = R.extract()
sos = butter(4, [5, 30], "bandpass", fs=FS, output="sos")
sm = np.ones(10) / 10                                         # 80 ms
lo, hi, b0, b1 = int(0.4 * FS), int(1.5 * FS), int(0.15 * FS), int(0.3 * FS)


def score(x):
    """x (T,) -> (score, best lag in s)."""
    e = np.convolve(np.abs(sosfiltfilt(sos, x)), sm, "same")
    e = e - e.mean()
    ac = np.correlate(e, e, "full")[len(e) - 1:]
    ac = ac / (ac[0] + 1e-12)
    k = lo + int(np.argmax(ac[lo:hi]))
    return ac[k] - ac[b0:b1].mean(), k / FS


rng = np.random.default_rng(0)
ids = rng.choice(len(rec), N, replace=False)
rows = []
for i in ids:
    r = rec.iloc[i]
    x = mne.io.read_raw_brainvision(f"{R.BIDS}/{r.subject}/{r.session_id}/eeg/{r.rec_id}_task-sleeponset_eeg.vhdr", preload=True,
                                    verbose="ERROR").get_data(picks=R.CH) * 1e6
    n = int(win[win.rec == i].shape[0])
    for w in list(range(6, min(n, 30), 3)) + list(range(max(6, n - 30), n, 3)):          # early and late windows
        seg = x[:, w * WIN:(w + 1) * WIN]
        sc = [score(seg[c]) for c in range(4)]
        null = score(seg[1][::-1])                                                      # reversed AF7: same spectrum, no ECG rhythm
        rows.append((i, w < n // 2, *[a for a, _ in sc], *[b for _, b in sc], null[0]))
A = np.array(rows, float)
tp9, af7, af8, tp10 = A[:, 2], A[:, 3], A[:, 4], A[:, 5]
lag9, lag10 = A[:, 6], A[:, 9]
null = A[:, 10]
thr = np.quantile(null, 0.95)
print(f"{len(A)} windows from {N} recordings; score = autocorrelation peak (0.4-1.5 s) minus baseline")
print(f"null (reversed AF7) 95% quantile {thr:.3f}")
for name, v in (("TP9", tp9), ("TP10", tp10), ("AF7", af7), ("AF8", af8)):
    print(f"{name}: median {np.median(v):.3f}, share above the null 95% quantile {100 * (v > thr).mean():.0f}%")
both = (tp9 > thr) & (tp10 > thr)
print(f"both TP above threshold in {100 * both.mean():.0f}% of windows; in those, |lag TP9 - lag TP10| < 40 ms in "
      f"{100 * (np.abs(lag9 - lag10)[both] < 0.04).mean():.0f}% (chance about {100 * 0.08 / 1.1:.0f}%)")
per = {}
for i in np.unique(A[:, 0]):
    for early in (1, 0):
        m = (A[:, 0] == i) & (A[:, 1] == early) & both
        if m.sum() >= 3:
            per.setdefault(i, {})[early] = 60.0 / np.median(lag9[m])
d = [v[1] - v[0] for v in per.values() if len(v) == 2]
print(f"{len(d)} recordings with enough detected windows early and late: heart rate early - late = {np.mean(d):+.1f} bpm "
      f"(SD {np.std(d):.1f}, median rate {np.median([60 / np.median(lag9[both])]):.0f} bpm)")
