File size: 2,874 Bytes
c8beaf3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #!/usr/bin/env python3
"""Cold-start top-1 fixture reproduction using only NumPy and packaged files."""
from __future__ import annotations
import argparse
import csv
import json
from pathlib import Path
import numpy as np
def read_tsv(path: Path) -> list[dict[str, str]]:
with path.open("r", encoding="utf-8", newline="") as handle:
return list(csv.DictReader(handle, delimiter="\t"))
def deterministic_top1(scores: np.ndarray, labels: list[str]) -> int:
maximum = float(scores.max())
tied = np.flatnonzero(scores == maximum).tolist()
return min(tied, key=lambda index: labels[index])
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("kernel", type=Path)
parser.add_argument("--out", type=Path)
args = parser.parse_args()
root = args.kernel.resolve()
queries = np.load(root / "e0-query-features-fp16.npy", mmap_mode="r", allow_pickle=False)
prototypes = np.load(root / "final-species-centroids-f64.npy", mmap_mode="r", allow_pickle=False)
species_rows = read_tsv(root / "species-prototype-map.tsv")
fixtures = read_tsv(root / "minimal-reproduction-fixtures.tsv")
labels = [row["canonical_taxon_key"] for row in species_rows]
if queries.shape != (6719, 1024) or prototypes.shape != (19144, 1024):
raise ValueError("kernel array shape mismatch")
results = []
for fixture in fixtures:
query = np.asarray(queries[int(fixture["query_tensor_row"])], dtype=np.float64)
scores = query @ prototypes.T
top1_index = deterministic_top1(scores, labels)
prediction = labels[top1_index]
observed_hit = int(prediction == fixture["evaluation_label_key"])
expected_hit = int(fixture["expected_top1_hit"])
results.append(
{
"fixture_order": int(fixture["fixture_order"]),
"query_id": fixture["query_id"],
"evaluation_label_key": fixture["evaluation_label_key"],
"predicted_top1_label_key": prediction,
"expected_top1_hit": expected_hit,
"observed_top1_hit": observed_hit,
"status": "PASS" if observed_hit == expected_hit else "FAIL",
}
)
passed = sum(row["status"] == "PASS" for row in results)
summary = {
"status": "PASS" if passed == len(results) else "FAIL",
"implementation": "NumPy only; no network, pixels, PyTorch, or absolute source paths",
"fixtures": len(results),
"passed": passed,
"failed": len(results) - passed,
"results": results,
}
text = json.dumps(summary, indent=2, sort_keys=True) + "\n"
if args.out:
args.out.write_text(text, encoding="utf-8")
print(text, end="")
return 0 if summary["status"] == "PASS" else 1
if __name__ == "__main__":
raise SystemExit(main())
|