"""Write the contract and the golden file for your own ONNX classifier, and audit a log later.

Usage:
  python make_check_files.py write MODEL.onnx ROWS.csv --features a,b,c [--from DATE] [--to DATE] [--out DIR]
  python make_check_files.py audit MODEL.onnx LOG.csv --features a,b,c [--label label] [--prob p] [--tolerance 1e-6]

ROWS.csv is any CSV with a "time" column and one column per feature. --features names the feature
columns in the order the model takes them. Columns are found by name, so their order in the CSV
does not matter. --from and --to select rows by the text of the time column (from <= time < to).

write  creates model_contract.csv and golden.csv next to the model, or in --out:
       contract  one row per input and output (kind,index,name,type,elem,dims, where -1 is a free
                 dimension), the feature names, the class labels in probability column order, and
                 the SHA-256 of the model file
       golden    time, the features as float32, and ort_label, ort_p0.. from onnxruntime
audit  runs the features of LOG.csv through onnxruntime and compares them with the label column and
       the probability columns (prefix --prob, then the class index) that an EA logged while it ran.
       It prints PASS or FAIL and exits with 0 or 1.

Example, with the article's model and its golden period:
python make_check_files.py write model.onnx features.csv --features rsi14,atr14_norm,ret10,bbw20 --from 2026.01.01 --to 2026.05.01
python make_check_files.py audit model.onnx ea_log.csv --features rsi14,atr14_norm,ret10,bbw20

The model must be a classifier exported with zipmap off: output 0 holds the labels and output 1 a
[N, C] probability tensor. Needs numpy and onnxruntime, and onnx to read the class labels.
"""
import argparse, csv, hashlib, os, sys
import numpy as np
import onnxruntime as ort

ELEM = {"tensor(float)": "float", "tensor(double)": "double", "tensor(int64)": "int64", "tensor(int32)": "int32"}


def sha256(path):
    with open(path, "rb") as f:
        return hashlib.sha256(f.read()).hexdigest()


def value_row(kind, index, v):
    """one contract row as the terminal reports the value: type, element type, dims"""
    if v.type.startswith("tensor("):
        dims = [d if isinstance(d, int) else -1 for d in v.shape]
        return [kind, index, v.name, "tensor", ELEM.get(v.type, v.type[7:-1]), "x".join(str(d) for d in dims)]
    kind_of = "sequence" if v.type.startswith("seq(") else "map" if v.type.startswith("map(") else "unknown"
    return [kind, index, v.name, kind_of, "", ""]


def class_labels(model_path, count):
    """integer class labels from the classifier node of the graph, in probability column order"""
    try:
        import onnx
        for node in onnx.load(model_path).graph.node:
            for a in node.attribute:
                if a.name == "classlabels_int64s" and len(a.ints) == count:
                    return list(a.ints), True
    except ImportError:
        pass
    return list(range(count)), False


def session(model_path):
    sess = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
    outs = sess.get_outputs()
    if len(outs) < 2 or not outs[1].type.startswith("tensor(") or len(outs[1].shape) != 2:
        sys.exit("output 1 must be a [N, C] probability tensor: export the classifier with zipmap off")
    return sess


def run(sess, x):
    """label and probabilities for float32 rows, cast to double when the model takes double"""
    inp = sess.get_inputs()[0]
    feed = x.astype(np.float64) if inp.type == "tensor(double)" else x
    label, prob = sess.run(None, {inp.name: feed})[:2]
    return label, prob


def read_rows(path, names, start=None, stop=None):
    with open(path, newline="") as f:
        rows = list(csv.DictReader(f))
    if not rows:
        sys.exit("%s has no rows" % path)
    missing = [n for n in ["time"] + names if n not in rows[0]]
    if missing:
        sys.exit("%s has no column %s" % (path, ", ".join(missing)))
    rows = [r for r in rows if (start is None or r["time"] >= start) and (stop is None or r["time"] < stop)]
    x = np.array([[float(r[n]) for n in names] for r in rows], dtype=np.float32)
    return rows, x


def write_contract(model_path, path, names):
    sess = session(model_path)
    count = sess.get_outputs()[1].shape[1]
    if not isinstance(count, int):
        sys.exit("the probability output has no fixed number of classes")
    labels, found = class_labels(model_path, count)
    if not found:
        print("class labels not found in the graph, assuming 0..%d" % (count - 1))
    with open(path, "w", newline="") as f:
        w = csv.writer(f, lineterminator="\n")
        w.writerow(["kind", "index", "name", "type", "elem", "dims"])
        for i, v in enumerate(sess.get_inputs()):
            w.writerow(value_row("input", i, v))
        for i, v in enumerate(sess.get_outputs()):
            w.writerow(value_row("output", i, v))
        w.writerow(["features", len(names), "", "", "", "|".join(names)])
        w.writerow(["classes", count, "", "", "", "|".join(str(c) for c in labels)])
        w.writerow(["sha256", 0, "", "", "", sha256(model_path)])
    return labels


def write_golden(model_path, path, times, x, names, skl=None):
    """golden rows: features, onnxruntime answers and, when given, scikit-learn's (labels, proba)"""
    label, prob = run(session(model_path), x)
    head = ["time"] + names + ["ort_label"] + ["ort_p%d" % c for c in range(prob.shape[1])]
    if skl is not None:
        head += ["skl_label"] + ["skl_p%d" % c for c in range(prob.shape[1])]
    with open(path, "w", newline="") as f:
        w = csv.writer(f, lineterminator="\n")
        w.writerow(head)
        for i in range(len(times)):
            row = [times[i]] + ["%.9g" % v for v in x[i]] + [int(label[i])] + ["%.9g" % v for v in prob[i]]
            if skl is not None:
                row += [int(skl[0][i])] + ["%.17g" % v for v in skl[1][i]]
            w.writerow(row)
    return label, prob


def audit(model_path, log_path, names, label_col, prob_col, tolerance):
    rows, x = read_rows(log_path, names)
    label, prob = run(session(model_path), x)
    labels, _ = class_labels(model_path, prob.shape[1])
    diff, flips, over = 0.0, 0, 0
    for i, r in enumerate(rows):
        d = max(abs(float(r["%s%d" % (prob_col, c)]) - float(prob[i][c])) for c in range(prob.shape[1]))
        diff = max(diff, d)
        over += d > tolerance
        flips += int(r[label_col]) != int(label[i])
    ok = len(rows) > 0 and flips == 0 and over == 0
    print("audit %s: %d rows, labels differ %d, max probability diff %.3g, rows over %.0e %d -> %s"
          % (os.path.basename(log_path), len(rows), flips, diff, tolerance, over, "PASS" if ok else "FAIL"))
    return ok


def main():
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("mode", choices=["write", "audit"])
    ap.add_argument("model")
    ap.add_argument("rows")
    ap.add_argument("--features", required=True, help="feature columns in the model's input order, comma separated")
    ap.add_argument("--from", dest="start")
    ap.add_argument("--to", dest="stop")
    ap.add_argument("--out")
    ap.add_argument("--label", default="label")
    ap.add_argument("--prob", default="p")
    ap.add_argument("--tolerance", type=float, default=1e-6)
    a = ap.parse_args()
    names = a.features.split(",")
    if a.mode == "audit":
        sys.exit(0 if audit(a.model, a.rows, names, a.label, a.prob, a.tolerance) else 1)
    out = a.out or os.path.dirname(os.path.abspath(a.model))
    os.makedirs(out, exist_ok=True)
    rows, x = read_rows(a.rows, names, a.start, a.stop)
    if not rows:
        sys.exit("no rows between --from and --to")
    labels = write_contract(a.model, os.path.join(out, "model_contract.csv"), names)
    write_golden(a.model, os.path.join(out, "golden.csv"), [r["time"] for r in rows], x, names)
    print("contract: %d features, classes %s, sha256 %s" % (len(names), labels, sha256(a.model)[:16]))
    print("golden: %d rows from %s to %s" % (len(rows), rows[0]["time"], rows[-1]["time"]))


if __name__ == "__main__":
    main()
