"""A variational autoencoder on the Old Faithful data of Bishop Chapter 9: the "what's next" step.

Chapter 9 fits a two-component Gaussian mixture to the standardised Old Faithful eruptions by EM.
Murphy's Advanced Topics (draft of 10 December 2025, Chapter 21) replaces the mixture with a
continuous latent variable z, a decoder p(x|z) = N(x | f_theta(z), sigma^2 I) and an encoder
q_phi(z|x) trained by maximising the evidence lower bound (ELBO):

    ELBO(x) = E_q[log p(x|z)] - KL(q_phi(z|x) || N(0, I))          (Murphy Ch. 21; Bishop eq. (10.2))

Reparameterisation: z = mu_phi(x) + sigma_phi(x) * eps, eps ~ N(0, I), so gradients flow through mu and sigma.

Settings (stated so they can be varied): 2-d latent space, MLP encoder and decoder with 32 tanh units,
Gaussian likelihood with fixed sigma = 0.3 on standardised data, Adam (lr 1e-2), 3000 full-batch steps,
torch seed 0. The KL weight beta_kl = 1 is the standard VAE; changing it gives the beta-VAE trade-off.

Run: python3 bridge_vae_faithful.py [--check] [--beta 4]
"""
import argparse
import os
import sys

import numpy as np
import torch
import torch.nn as nn
import matplotlib.pyplot as plt

import common


def make_model(latent_dim=2, hidden=32):
    enc = nn.Sequential(nn.Linear(2, hidden), nn.Tanh(), nn.Linear(hidden, hidden), nn.Tanh(),
                        nn.Linear(hidden, 2 * latent_dim))
    dec = nn.Sequential(nn.Linear(latent_dim, hidden), nn.Tanh(), nn.Linear(hidden, hidden), nn.Tanh(),
                        nn.Linear(hidden, 2))
    return enc, dec


def elbo_terms(enc, dec, x, sigma=0.3, latent_dim=2):
    h = enc(x)
    mu, logvar = h[:, :latent_dim], h[:, latent_dim:]
    eps = torch.randn_like(mu)
    z = mu + torch.exp(0.5 * logvar) * eps                      # reparameterisation
    xhat = dec(z)
    rec = -0.5 * ((x - xhat) ** 2).sum(1) / sigma ** 2 - x.shape[1] * np.log(sigma * np.sqrt(2 * np.pi))
    kl = 0.5 * (mu ** 2 + torch.exp(logvar) - 1 - logvar).sum(1)  # KL(N(mu, diag s^2) || N(0, I)), closed form
    return rec, kl, mu, logvar


def train(beta_kl=1.0, steps=3000, seed=0):
    torch.manual_seed(seed)
    X = torch.tensor(common.old_faithful(standardize=True), dtype=torch.float32)
    enc, dec = make_model()
    opt = torch.optim.Adam(list(enc.parameters()) + list(dec.parameters()), lr=1e-2)
    hist = []
    for t in range(steps):
        opt.zero_grad()
        rec, kl, _, _ = elbo_terms(enc, dec, X)
        loss = -(rec - beta_kl * kl).mean()
        loss.backward(); opt.step()
        if t % 50 == 0 or t == steps - 1:
            hist.append((t, float((rec - beta_kl * kl).detach().mean()), float(kl.detach().mean())))
    return enc, dec, X, np.array(hist)


def evaluate(enc, dec, X, beta_kl=1.0):
    with torch.no_grad():
        torch.manual_seed(1)
        rec, kl, mu, logvar = elbo_terms(enc, dec, X)
        elbo = float((rec - kl).mean())
        recon_mse = float(((dec(mu) - X) ** 2).mean())
        active = float((torch.exp(logvar).mean(0) < 0.5).float().mean())
    return {"elbo": elbo, "kl": float(kl.mean()), "recon_mse": recon_mse}


def fig_vae(out_dir=None, beta_kl=1.0):
    """VAE latent space and samples on the Old Faithful data (companion to Bishop Fig. 9.1 and 9.8)."""
    enc, dec, X, hist = train(beta_kl=beta_kl)
    Xn = X.numpy()
    with torch.no_grad():
        mu = enc(X)[:, :2].numpy()
        gx = torch.linspace(-2.5, 2.5, 25)
        G = torch.stack(torch.meshgrid(gx, gx, indexing="ij"), -1).reshape(-1, 2)
        recon = dec(mu_t := torch.tensor(mu)).numpy()
        samples = dec(torch.randn(400, 2, generator=torch.Generator().manual_seed(3))).numpy()
    fig, ax = plt.subplots(1, 4, figsize=(15, 3.9))
    ax[0].scatter(Xn[:, 0], Xn[:, 1], s=8, color="#1f5fbf")
    ax[0].set_title("(a) data: standardised Old Faithful")
    sc = ax[1].scatter(mu[:, 0], mu[:, 1], s=8, c=Xn[:, 1], cmap="viridis")
    ax[1].set_title("(b) encoder means mu(x), coloured by waiting time")
    ax[2].scatter(samples[:, 0], samples[:, 1], s=8, color="#c2185b")
    ax[2].set_title("(c) decoder samples f(z), z ~ N(0, I)")
    ax[3].plot(hist[:, 0], hist[:, 1], color="#1f5fbf"); ax[3].set_xlabel("step"); ax[3].set_ylabel("ELBO per point")
    ax[3].set_title("(d) training: ELBO rises")
    for a in ax[:3]:
        a.set_xlabel("x1 (duration)"); a.set_ylabel("x2 (waiting)")
    fig.suptitle("Variational autoencoder, 2-d latent space, beta_KL = %g (Murphy Ch. 21 on Bishop's Old Faithful data)" % beta_kl, fontsize=10)
    fig.tight_layout()
    folder = out_dir or os.path.join(common.HERE, "..", "img", "bridge")
    os.makedirs(folder, exist_ok=True)
    name = "bridge-vae-faithful" + ("" if beta_kl == 1.0 else "-beta%g" % beta_kl)
    for ext in ("png", "svg", "pdf"):
        fig.savefig(os.path.join(folder, name + "." + ext))
    plt.close(fig)
    return evaluate(enc, dec, X, beta_kl)


def check():
    """Training must raise the ELBO, and the decoder must reconstruct the data better than the mean."""
    enc, dec, X, hist = train(steps=1500)
    ok_rise = hist[-1, 1] > hist[0, 1] + 1.0
    ev = evaluate(enc, dec, X)
    ok_recon = ev["recon_mse"] < float(((X - X.mean(0)) ** 2).mean()) * 0.5
    for label, ok in [("ELBO rises during training (%.2f -> %.2f)" % (hist[0, 1], hist[-1, 1]), ok_rise),
                      ("reconstruction MSE %.3f is below half the data variance" % ev["recon_mse"], ok_recon)]:
        print("%s %s" % ("PASS" if ok else "FAIL", label))
    return ok_rise and ok_recon


FIGURES = {"bridge-vae-faithful": fig_vae}

if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--check", action="store_true")
    ap.add_argument("--beta", type=float, default=None)
    a, rest = ap.parse_known_args()
    if a.check:
        sys.exit(0 if check() else 1)
    if a.beta is not None:
        print(fig_vae(beta_kl=a.beta))
    else:
        print(fig_vae())


def export_decoder(path=None, beta_kl=1.0):
    """Write the decoder weights to JSON so the browser demo can decode any latent point."""
    import json
    enc, dec, X, _ = train(beta_kl=beta_kl)
    layers = [m for m in dec if isinstance(m, nn.Linear)]
    data = {"activation": "tanh", "layers": [{"W": l.weight.detach().numpy().tolist(),
                                             "b": l.bias.detach().numpy().tolist()} for l in layers]}
    path = path or os.path.join(common.DATA_DIR, "vae_faithful_decoder.json")
    with open(path, "w") as f:
        json.dump(data, f)
    return path
