Download evaluate.py from srpone/fashion200k-pooled-eval: direct link, hf CLI and curl.
- Browser
- Download file 3.18 kB
-
https://huggingface.co/datasets/srpone/fashion200k-pooled-eval/resolve/main/evaluate.py
- Command line
-
hf download hf://datasets/srpone/fashion200k-pooled-eval/evaluate.py
-
curl -L -o evaluate.py https://huggingface.co/datasets/srpone/fashion200k-pooled-eval/resolve/main/evaluate.py
3.18 kB
| """Score a Fashion200k run against the pooled qrels (and the original Marqo qrels). | |
| Run file: JSON {query_id: [ranked ids, ...]}, ids are corpus_id (int) or Marqo item_ID (str); | |
| pick with --id_type. Only the first 10 are used. | |
| Metrics (same definitions as the paper, per relevance threshold tau): | |
| S@10 1 if any top-10 item has grade >= tau | |
| MRR@10 reciprocal rank of the first top-10 item with grade >= tau | |
| nDCG@10 gain 2^g - 1 for g >= tau (else 0); ideal DCG over all pooled items of the query | |
| R@10 |top-10 ∩ pool positives| / |pool positives| (pool-positive denominator) | |
| Averaged over queries with >= 1 pooled item of grade >= tau. Unjudged items count as | |
| non-relevant (TREC convention): add your run to the pool and judge new pairs before | |
| reporting absolute numbers. | |
| """ | |
| import argparse | |
| import collections | |
| import json | |
| import math | |
| import os | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| def load_jsonl(path): | |
| with open(path) as f: | |
| return [json.loads(l) for l in f if l.strip()] | |
| def dcg(gains): | |
| return sum(g / math.log2(i + 2) for i, g in enumerate(gains)) | |
| def score(run, qrels, tau, k=10): | |
| n = s = m = nd = r = 0.0 | |
| for qid, judged in qrels.items(): | |
| pos = {t for t, g in judged.items() if g >= tau} | |
| if not pos: | |
| continue | |
| n += 1 | |
| top = run.get(qid, [])[:k] | |
| hits = [t in pos for t in top] | |
| s += any(hits) | |
| m += next((1.0 / i for i, h in enumerate(hits, 1) if h), 0.0) | |
| gains = [(2 ** judged[t] - 1) if judged.get(t, 0) >= tau else 0 for t in top] | |
| ideal = [(2 ** g - 1) for g in sorted((g for g in judged.values() if g >= tau), reverse=True)[:k]] | |
| nd += dcg(gains) / dcg(ideal) | |
| r += sum(hits) / len(pos) | |
| return {"n": int(n), "S@10": s / n, "MRR@10": m / n, "nDCG@10": nd / n, "R@10": r / n} | |
| def load_qrels(data_dir, id_key): | |
| pooled = collections.defaultdict(dict) | |
| for x in load_jsonl(os.path.join(data_dir, "qrels", "pooled.jsonl")): | |
| # 74 item_IDs occur twice in Marqo's corpus (identical rows); keep the max grade | |
| q, t = str(x["query_id"]), str(x[id_key]) | |
| pooled[q][t] = max(pooled[q].get(t, 0), int(x["grade"])) | |
| orig = collections.defaultdict(set) | |
| for x in load_jsonl(os.path.join(data_dir, "qrels", "original_marqo.jsonl")): | |
| orig[str(x["query_id"])].add(str(x[id_key])) | |
| return pooled, orig | |
| def evaluate(run_path, data_dir=HERE, id_type="corpus_id"): | |
| pooled, orig = load_qrels(data_dir, id_type) | |
| run = {str(q): [str(t) for t in ts] for q, ts in json.load(open(run_path)).items()} | |
| out = {f"thr{t}": score(run, pooled, t) for t in (3, 4, 5)} | |
| out["original_R@10"] = sum( | |
| any(t in orig[q] for t in run.get(q, [])[:10]) for q in orig) / len(orig) | |
| return out | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("run", help="JSON {query_id: [ranked ids]}") | |
| ap.add_argument("--id_type", choices=["corpus_id", "item_ID"], default="corpus_id") | |
| ap.add_argument("--data_dir", default=HERE) | |
| a = ap.parse_args() | |
| print(json.dumps(evaluate(a.run, a.data_dir, a.id_type), indent=2)) | |
| if __name__ == "__main__": | |
| main() | |