# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Generate portable NMFk component cases from fixed inputs and the reference."""

import json
import sys
from pathlib import Path

ROOT = Path(__file__).parents[1]
sys.path.insert(0, str(ROOT / "reference"))
import nmfk


def generate():
    identity = [[1, 0], [0, 1]]
    axis_bases = [[1, 0, 0], [0, 1, 0]]
    first_run = [*axis_bases, [0, 0, 1]]
    positive_v = [[4, 1, 3, 2], [2, 5, 2, 4], [1, 3, 6, 1]]
    positive_w = [[0.6, 0.3], [0.3, 0.4], [0.1, 0.3]]
    positive_h = [[1, 2, 3, 4], [4, 1, 2, 2]]
    peak = [0, 1, 2, 1, 0]
    shifted_peak = [0, 0, 1, 2, 1]
    cases = [
        ("exact identity reconstruction", "relative_reconstruction_error", [identity, identity, identity]),
        ("nonzero relative reconstruction error", "relative_reconstruction_error",
         [positive_v, positive_w, positive_h]),
        ("one multiplicative update on diagonal data", "multiplicative_step",
         [[[2, 0], [0, 1]], identity, identity]),
        ("Frobenius update on positive mixed data", "multiplicative_step",
         [positive_v, positive_w, positive_h]),
        ("equal-size clustering swaps second run", "equal_size_cluster",
         [[identity, [[0, 1], [1, 0]]]]),
        ("one-to-one assignment across three runs", "equal_size_cluster",
         [[first_run, [[1, 0, 0], [1, 0.3, 0], [0, 0, 1]], [[0, 1, 0], [0, 0, 1], [1, 0, 0]]]]),
        ("fourth run uses updated centroids", "equal_size_cluster", [[
            axis_bases,
            [[0.5, 0, 0.5], [0, 1, 0]],
            [[0.25, 0, 0.75], [0, 1, 0]],
            [[0.2, 0.2, 0.6], [0.55, 0.45, 0]],
        ]]),
        ("lexicographic assignment tie", "equal_size_cluster",
         [[[[1, 0], [1, 0]], [[1, 1], [0, 1]]]]),
        ("perfect separated clusters have unit silhouette", "silhouette_score",
         [[[[1, 0], [1, 0]], [[0, 1], [0, 1]]]]),
        ("cosine silhouette on imperfect clusters", "silhouette_score",
         [[[[1, 0.1], [0.9, 0.3]], [[0.2, 1], [0.5, 0.8]]]]),
        ("silhouette uses the nearest of three clusters", "silhouette_score", [[
            [[1, 0, 0], [0.9, 0.1, 0]],
            [[0.8, 0.2, 0], [0.7, 0.3, 0]],
            [[0, 0, 1], [0, 0.1, 0.9]],
        ]]),
        ("singleton uses zero within-cluster distance", "silhouette_score",
         [[[[1, 0]], [[0, 1], [0, 1]]]]),
        ("rank-one silhouette placeholder", "silhouette_score", [[identity]]),
        ("positive one-bin shift", "best_shift_correlation", [peak, shifted_peak, 2]),
        ("negative one-bin shift", "best_shift_correlation", [shifted_peak, peak, 2]),
        ("centered correlation on overlapping slices", "best_shift_correlation",
         [[5, 1, 0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 4, 0, 0, 5], 3]),
        ("shift tie favors smaller signed shift", "best_shift_correlation",
         [[0, 1, 0, 1, 0], [1, 0, 1, 0, 1], 2]),
        ("constant slices favor zero shift", "best_shift_correlation", [[1, 1, 1], [2, 2, 2], 1]),
    ]
    return {
        "format": 1,
        "method": "nmfk-xrd-phase-mapping",
        "version": "0.1.0",
        "interface": (
            "multiplicative_step(V, W, H) -> [W_new, H_new] updates H before W and rescales "
            "each positive-sum W column and its H row. relative_reconstruction_error(V, W, H) "
            "-> float. V is bins × patterns, W is bins × components, H is components × patterns. "
            "equal_size_cluster(run_bases) -> [clusters, assignments]. run_bases is runs × components × bins. "
            "clusters[c][r] is the basis vector from run r assigned to cluster c. assignments[r][c] "
            "is its zero-based index in run r. Break equal assignment costs lexicographically in cluster order. "
            "silhouette_score(clusters) -> float, using within-cluster distance zero for a singleton and "
            "returning 1 for a single cluster. best_shift_correlation(a, b, max_shift) -> [shift, Pearson "
            "correlation] compares a[i] and b[i+s] on their overlap. Require an integer 0 <= max_shift <= "
            "len(a) - 2 so every overlap has at least two bins. Positive shift means b is displaced "
            "to higher indices relative to a. Break correlation ties by smaller absolute shift, then "
            "smaller signed shift."
        ),
        "comparison": "Compare all numeric outputs recursively at absolute tolerance tol. Integer assignments and shifts must match exactly.",
        "cases": [
            {
                "name": name, "operation": operation, "args": args,
                "expected": getattr(nmfk, operation)(*args), "tol": 1e-12,
            }
            for name, operation, args in cases
        ],
    }


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