Reference-Update-Benchmark / examples /minimal_reproduction.py
leoncca's picture
Add files using upload-large-folder tool
c8beaf3 verified
Raw
History Blame Contribute Delete
2.87 kB
#!/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())