#!/usr/bin/env python3
"""Builds the open benchmark's public track from Nutrition5k's own release files (CC BY 4.0), so anyone can rebuild
it from the source and check our truth file byte for byte.

    python3 build_public.py ~/marble-bench/n5k  > nutrition5k_overhead_test.csv

The set: every dish id in Nutrition5k's official `depth_test_ids.txt` (the test dishes with an overhead photo),
truth = the dish's `total_calories` from dish_metadata_cafe1.csv / cafe2.csv, cluster = the dish id. Photos are the
dataset's own `realsense_overhead/<dish>/rgb.png`, sent as they are or resized by the submitter's own app pipeline.

Not run until the registered study's exam is scored (evals/ROUND2.md): these dishes overlap its sealed half.
"""
import csv
import os
import sys


def main(root):
    ids = [ln.strip() for ln in open(os.path.join(root, "depth_test_ids.txt")) if ln.strip()]
    kcal = {}
    for f in ("dish_metadata_cafe1.csv", "dish_metadata_cafe2.csv"):
        for row in csv.reader(open(os.path.join(root, f))):
            kcal[row[0]] = float(row[1])
    w = csv.writer(sys.stdout)
    w.writerow(["id", "kcal", "cluster"])
    for i in sorted(ids):
        if i in kcal:
            w.writerow([i, round(kcal[i], 1), i])


if __name__ == "__main__":
    main(os.path.expanduser(sys.argv[1]))
