Insert a benchmark JSON file as the first data row in the CSV.
(benchmark_file: str, csv_file: str, platform_name: str = "BEIR Reference")
| 9 | |
| 10 | |
| 11 | def insert_benchmark(benchmark_file: str, csv_file: str, platform_name: str = "BEIR Reference"): |
| 12 | """Insert a benchmark JSON file as the first data row in the CSV.""" |
| 13 | |
| 14 | # Load benchmark data |
| 15 | with open(benchmark_file, 'r') as f: |
| 16 | bench = json.load(f) |
| 17 | |
| 18 | csv_path = Path(csv_file) |
| 19 | |
| 20 | if not csv_path.exists(): |
| 21 | print(f"Error: CSV file not found: {csv_path}") |
| 22 | return |
| 23 | |
| 24 | # Read existing CSV |
| 25 | with open(csv_path, 'r', newline='') as f: |
| 26 | reader = csv.DictReader(f) |
| 27 | fieldnames = reader.fieldnames |
| 28 | existing_rows = list(reader) |
| 29 | |
| 30 | if not fieldnames: |
| 31 | print("Error: CSV file has no headers") |
| 32 | return |
| 33 | |
| 34 | # Build benchmark row matching CSV columns |
| 35 | metrics = bench.get("metrics", {}) |
| 36 | config = bench.get("config", {}) |
| 37 | |
| 38 | benchmark_row = { |
| 39 | "timestamp": "", |
| 40 | "platform": platform_name, |
| 41 | "description": bench.get("description", ""), |
| 42 | "dataset": config.get("dataset", "scifact"), |
| 43 | "embedding_model": config.get("embedding_model", ""), |
| 44 | "version": "", |
| 45 | "hnsw_config": "", |
| 46 | "hnsw_metric": "", |
| 47 | "hnsw_max_levels": "", |
| 48 | "hnsw_ef_search": "", |
| 49 | "hnsw_ef_construction": "", |
| 50 | "ef_search_query": "", |
| 51 | "distance_threshold": "", |
| 52 | "use_new_syntax": "", |
| 53 | "num_documents": "", |
| 54 | "num_queries": "", |
| 55 | "insert_time_seconds": "", |
| 56 | "search_time_seconds": "", |
| 57 | "avg_query_time_seconds": "", |
| 58 | } |
| 59 | |
| 60 | # Add metric columns |
| 61 | for k in [1, 3, 5, 10, 100]: |
| 62 | benchmark_row[f"ndcg_{k}"] = metrics.get("ndcg", {}).get(f"NDCG@{k}", 0.0) |
| 63 | benchmark_row[f"map_{k}"] = metrics.get("map", {}).get(f"MAP@{k}", 0.0) |
| 64 | benchmark_row[f"recall_{k}"] = metrics.get("recall", {}).get(f"Recall@{k}", 0.0) |
| 65 | benchmark_row[f"precision_{k}"] = metrics.get("precision", {}).get(f"P@{k}", 0.0) |
| 66 | |
| 67 | # Only include fields that exist in the CSV |
| 68 | benchmark_row = {k: v for k, v in benchmark_row.items() if k in fieldnames} |