# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Regenerate the portable fixtures on standard output."""
import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "reference"))
import uidd as reference


def build():
    cases = [
        {"name":"state-dependent reversible drift","operation":"decompose","tol":1e-12,
         "inputs":dict(grad_v=[2.],sigma=[[10**0.5]],bands=[],div_m=[4.],div_w=[0.])},
        {"name":"two coupled skew bands with oriented row divergences","operation":"decompose","tol":1e-12,
         "inputs":dict(grad_v=[1.,-.5,.2],sigma=[[1.,.3,0.],[-.2,.8,.4],[0.,-.1,1.3]],
                       bands=[.6,-.7],div_m=[.1,.4,-.2],div_w=[.2,-.3,.5])},
        {"name":"correlated mobility and positive rate","operation":"local_epr","tol":1e-12,
         "inputs":dict(irreversible=[.6,-.8],sigma=[[1.2,-.1],[.4,.7]])},
        {"name":"normalized short-lag conditional likelihood","operation":"transition","tol":1e-12,
         "inputs":dict(start=[.5,-.3],end=[.28,-.15],dt=.12,total=[-.7,.4],sigma=[[1.,.3],[-.2,.9]])},
        {"name": "variable-diffusion loss and two weight derivatives", "operation": "diffusion_loss_gradient", "tol": 1e-10,
         "inputs": dict(pairs=[[[.55],[.39],.12], [[-.6],[-.28],.21]], weights=[1.4,.8])},
        {"name": "row divergence through two-dimensional variable fields", "operation": "variable_drift", "tol": 1e-9,
         "inputs": dict(state=[.65,-.4])},
    ]
    for case in cases:
        case["expected"] = getattr(reference, case["operation"])(**case["inputs"])
    return {"format": 1, "method": "uidd", "version": "0.1.0",
            "interface": (
                "Column vectors, row-major real matrices, absolute tolerances. decompose(grad_v,sigma,bands,div_m,div_w) returns [reversible,irreversible,total], with M=sigma*sigma^T/2, W[i,i+1]=bands[i], W[i+1,i]=-bands[i], div row-wise. "
                "rev=div_m-M*grad_v, irr=div_w-W*grad_v, total=rev+irr. local_epr(irreversible,sigma) returns irreversible^T M^-1 irreversible. "
                "transition(start,end,dt,total,sigma) returns the normalized Gaussian negative log density at end with mean start+dt*total and covariance dt*sigma*sigma^T. "
                "diffusion_loss_gradient(pairs,weights) uses the scalar state q and weights [k,s]. V=k*q^2/2, sigma=s*(1+0.2*tanh(q)), W=0. "
                "Return [mean normalized transition loss,[d/dk,d/ds]] across [start,end,lag] triples. Differentiate the mobility divergence in the drift and both the drift and covariance in the loss. "
                "variable_drift(state) returns [reversible,irreversible,total] with no supplied derivatives. For state [q,p], use V=(1.2*q^2+0.7*p^2)/2+0.25*q*p, sigma=[[1+0.2*q,0],[0.3+0.1*p,0.8+0.15*q]], and upper skew band 0.4+0.2*q-0.3*p. Evaluate the state gradient and both matrix divergences. "
                "Export all five functions from port_adapter.py."
            ), "cases": cases}


if __name__ == "__main__":
    print(json.dumps(build(), indent=2, allow_nan=False))
