{
  "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": "exact identity reconstruction",
      "operation": "relative_reconstruction_error",
      "args": [
        [
          [
            1,
            0
          ],
          [
            0,
            1
          ]
        ],
        [
          [
            1,
            0
          ],
          [
            0,
            1
          ]
        ],
        [
          [
            1,
            0
          ],
          [
            0,
            1
          ]
        ]
      ],
      "expected": 0.0,
      "tol": 1e-12
    },
    {
      "name": "nonzero relative reconstruction error",
      "operation": "relative_reconstruction_error",
      "args": [
        [
          [
            4,
            1,
            3,
            2
          ],
          [
            2,
            5,
            2,
            4
          ],
          [
            1,
            3,
            6,
            1
          ]
        ],
        [
          [
            0.6,
            0.3
          ],
          [
            0.3,
            0.4
          ],
          [
            0.1,
            0.3
          ]
        ],
        [
          [
            1,
            2,
            3,
            4
          ],
          [
            4,
            1,
            2,
            2
          ]
        ]
      ],
      "expected": 0.6837106971960563,
      "tol": 1e-12
    },
    {
      "name": "one multiplicative update on diagonal data",
      "operation": "multiplicative_step",
      "args": [
        [
          [
            2,
            0
          ],
          [
            0,
            1
          ]
        ],
        [
          [
            1,
            0
          ],
          [
            0,
            1
          ]
        ],
        [
          [
            1,
            0
          ],
          [
            0,
            1
          ]
        ]
      ],
      "expected": [
        [
          [
            1.0,
            0.0
          ],
          [
            0.0,
            1.0
          ]
        ],
        [
          [
            1.9999999999999996,
            0.0
          ],
          [
            0.0,
            0.9999999999999991
          ]
        ]
      ],
      "tol": 1e-12
    },
    {
      "name": "Frobenius update on positive mixed data",
      "operation": "multiplicative_step",
      "args": [
        [
          [
            4,
            1,
            3,
            2
          ],
          [
            2,
            5,
            2,
            4
          ],
          [
            1,
            3,
            6,
            1
          ]
        ],
        [
          [
            0.6,
            0.3
          ],
          [
            0.3,
            0.4
          ],
          [
            0.1,
            0.3
          ]
        ],
        [
          [
            1,
            2,
            3,
            4
          ],
          [
            4,
            1,
            2,
            2
          ]
        ]
      ],
      "expected": [
        [
          [
            0.40151795080772307,
            0.2095832382969689
          ],
          [
            0.38797204516734213,
            0.3611571892708379
          ],
          [
            0.21051000402493486,
            0.42925957243219315
          ]
        ],
        [
          [
            1.7733098516328103,
            3.9099766122195416,
            4.492160629847822,
            4.072892304395358
          ],
          [
            6.730176697808067,
            3.956173432798479,
            5.182113403740525,
            3.0907604943738143
          ]
        ]
      ],
      "tol": 1e-12
    },
    {
      "name": "equal-size clustering swaps second run",
      "operation": "equal_size_cluster",
      "args": [
        [
          [
            [
              1,
              0
            ],
            [
              0,
              1
            ]
          ],
          [
            [
              0,
              1
            ],
            [
              1,
              0
            ]
          ]
        ]
      ],
      "expected": [
        [
          [
            [
              1.0,
              0.0
            ],
            [
              1.0,
              0.0
            ]
          ],
          [
            [
              0.0,
              1.0
            ],
            [
              0.0,
              1.0
            ]
          ]
        ],
        [
          [
            0,
            1
          ],
          [
            1,
            0
          ]
        ]
      ],
      "tol": 1e-12
    },
    {
      "name": "one-to-one assignment across three runs",
      "operation": "equal_size_cluster",
      "args": [
        [
          [
            [
              1,
              0,
              0
            ],
            [
              0,
              1,
              0
            ],
            [
              0,
              0,
              1
            ]
          ],
          [
            [
              1,
              0,
              0
            ],
            [
              1,
              0.3,
              0
            ],
            [
              0,
              0,
              1
            ]
          ],
          [
            [
              0,
              1,
              0
            ],
            [
              0,
              0,
              1
            ],
            [
              1,
              0,
              0
            ]
          ]
        ]
      ],
      "expected": [
        [
          [
            [
              1.0,
              0.0,
              0.0
            ],
            [
              1.0,
              0.0,
              0.0
            ],
            [
              1.0,
              0.0,
              0.0
            ]
          ],
          [
            [
              0.0,
              1.0,
              0.0
            ],
            [
              1.0,
              0.3,
              0.0
            ],
            [
              0.0,
              1.0,
              0.0
            ]
          ],
          [
            [
              0.0,
              0.0,
              1.0
            ],
            [
              0.0,
              0.0,
              1.0
            ],
            [
              0.0,
              0.0,
              1.0
            ]
          ]
        ],
        [
          [
            0,
            1,
            2
          ],
          [
            0,
            1,
            2
          ],
          [
            2,
            0,
            1
          ]
        ]
      ],
      "tol": 1e-12
    },
    {
      "name": "fourth run uses updated centroids",
      "operation": "equal_size_cluster",
      "args": [
        [
          [
            [
              1,
              0,
              0
            ],
            [
              0,
              1,
              0
            ]
          ],
          [
            [
              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
            ]
          ]
        ]
      ],
      "expected": [
        [
          [
            [
              1.0,
              0.0,
              0.0
            ],
            [
              0.5,
              0.0,
              0.5
            ],
            [
              0.25,
              0.0,
              0.75
            ],
            [
              0.2,
              0.2,
              0.6
            ]
          ],
          [
            [
              0.0,
              1.0,
              0.0
            ],
            [
              0.0,
              1.0,
              0.0
            ],
            [
              0.0,
              1.0,
              0.0
            ],
            [
              0.55,
              0.45,
              0.0
            ]
          ]
        ],
        [
          [
            0,
            1
          ],
          [
            0,
            1
          ],
          [
            0,
            1
          ],
          [
            0,
            1
          ]
        ]
      ],
      "tol": 1e-12
    },
    {
      "name": "lexicographic assignment tie",
      "operation": "equal_size_cluster",
      "args": [
        [
          [
            [
              1,
              0
            ],
            [
              1,
              0
            ]
          ],
          [
            [
              1,
              1
            ],
            [
              0,
              1
            ]
          ]
        ]
      ],
      "expected": [
        [
          [
            [
              1.0,
              0.0
            ],
            [
              1.0,
              1.0
            ]
          ],
          [
            [
              1.0,
              0.0
            ],
            [
              0.0,
              1.0
            ]
          ]
        ],
        [
          [
            0,
            1
          ],
          [
            0,
            1
          ]
        ]
      ],
      "tol": 1e-12
    },
    {
      "name": "perfect separated clusters have unit silhouette",
      "operation": "silhouette_score",
      "args": [
        [
          [
            [
              1,
              0
            ],
            [
              1,
              0
            ]
          ],
          [
            [
              0,
              1
            ],
            [
              0,
              1
            ]
          ]
        ]
      ],
      "expected": 1.0,
      "tol": 1e-12
    },
    {
      "name": "cosine silhouette on imperfect clusters",
      "operation": "silhouette_score",
      "args": [
        [
          [
            [
              1,
              0.1
            ],
            [
              0.9,
              0.3
            ]
          ],
          [
            [
              0.2,
              1
            ],
            [
              0.5,
              0.8
            ]
          ]
        ]
      ],
      "expected": 0.8931304490335996,
      "tol": 1e-12
    },
    {
      "name": "silhouette uses the nearest of three clusters",
      "operation": "silhouette_score",
      "args": [
        [
          [
            [
              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
            ]
          ]
        ]
      ],
      "expected": 0.796515886817755,
      "tol": 1e-12
    },
    {
      "name": "singleton uses zero within-cluster distance",
      "operation": "silhouette_score",
      "args": [
        [
          [
            [
              1,
              0
            ]
          ],
          [
            [
              0,
              1
            ],
            [
              0,
              1
            ]
          ]
        ]
      ],
      "expected": 1.0,
      "tol": 1e-12
    },
    {
      "name": "rank-one silhouette placeholder",
      "operation": "silhouette_score",
      "args": [
        [
          [
            [
              1,
              0
            ],
            [
              0,
              1
            ]
          ]
        ]
      ],
      "expected": 1.0,
      "tol": 1e-12
    },
    {
      "name": "positive one-bin shift",
      "operation": "best_shift_correlation",
      "args": [
        [
          0,
          1,
          2,
          1,
          0
        ],
        [
          0,
          0,
          1,
          2,
          1
        ],
        2
      ],
      "expected": [
        1,
        1.0
      ],
      "tol": 1e-12
    },
    {
      "name": "negative one-bin shift",
      "operation": "best_shift_correlation",
      "args": [
        [
          0,
          0,
          1,
          2,
          1
        ],
        [
          0,
          1,
          2,
          1,
          0
        ],
        2
      ],
      "expected": [
        -1,
        1.0
      ],
      "tol": 1e-12
    },
    {
      "name": "centered correlation on overlapping slices",
      "operation": "best_shift_correlation",
      "args": [
        [
          5,
          1,
          0,
          0,
          0,
          4,
          0,
          0
        ],
        [
          0,
          0,
          0,
          0,
          4,
          0,
          0,
          5
        ],
        3
      ],
      "expected": [
        -1,
        0.9684747092264967
      ],
      "tol": 1e-12
    },
    {
      "name": "shift tie favors smaller signed shift",
      "operation": "best_shift_correlation",
      "args": [
        [
          0,
          1,
          0,
          1,
          0
        ],
        [
          1,
          0,
          1,
          0,
          1
        ],
        2
      ],
      "expected": [
        -1,
        1.0
      ],
      "tol": 1e-12
    },
    {
      "name": "constant slices favor zero shift",
      "operation": "best_shift_correlation",
      "args": [
        [
          1,
          1,
          1
        ],
        [
          2,
          2,
          2
        ],
        1
      ],
      "expected": [
        0,
        0.0
      ],
      "tol": 1e-12
    }
  ]
}
