{ "cells": [ { "cell_type": "markdown", "id": "fc834ae9-7876-4b96-85bc-b6f86fb7c5de", "metadata": {}, "source": [ "# Olfactory Intelligence Benchmark csv file" ] }, { "cell_type": "code", "execution_count": 1, "id": "34340a73-ea3b-454d-b063-0b8e2e20b3f0", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Summary\n", "-------\n", "Included files: 8\n", "Total combined rows: 1010\n", "Done\n" ] } ], "source": [ "import uuid\n", "from pathlib import Path\n", "from typing import List, Tuple\n", "import pandas as pd\n", "\n", "\n", "ROOT = Path(\".\").resolve() \n", "OUT_CSV = Path(\"OP_Benchmark.csv\") \n", "if OUT_CSV.exists():\n", " OUT_CSV.unlink()\n", "DEDUPE = False \n", "\n", "REQUIRED_COLS = [\n", " \"question_ID\",\n", " \"compound.name_1\",\n", " \"SMILES_1\",\n", " \"compound.name_2\",\n", " \"SMILES_2\",\n", " \"OPTIONS\",\n", " \"question_category\",\n", " \"prompt.1\",\n", " \"prompt.2\",\n", " \"answer\",\n", " \"other_info\",\n", "]\n", "\n", "\n", "def find_csvs_one_level(root: Path) -> List[Path]:\n", " \"\"\"\n", " Return CSV files located in immediate subfolders.\n", " Does not include CSV files in `root` itself.\n", " \"\"\"\n", " results: List[Path] = []\n", " for entry in root.iterdir():\n", " if entry.is_dir():\n", " results.extend(entry.glob(\"*.csv\"))\n", " return results\n", "\n", "def validate_columns(df: pd.DataFrame) -> Tuple[bool, List[str]]:\n", " cols = list(df.columns.astype(str))\n", " missing = [c for c in REQUIRED_COLS if c not in cols]\n", " return (len(missing) == 0), missing\n", "\n", "candidates = find_csvs_one_level(ROOT)\n", "\n", "good_frames: List[pd.DataFrame] = []\n", "included_paths: List[Path] = []\n", "excluded_missing: List[Tuple[Path, List[str]]] = []\n", "excluded_errors: List[Tuple[Path, str]] = []\n", "\n", "for csv_path in candidates:\n", " try:\n", " df = pd.read_csv(csv_path, dtype=str, keep_default_na=False, na_values=[])\n", " except Exception as e:\n", " excluded_errors.append((csv_path, f\"read error: {e}\"))\n", " print(f\"{csv_path} | read error: {e}\")\n", " continue\n", "\n", " ok, missing = validate_columns(df)\n", " if not ok:\n", " excluded_missing.append((csv_path, missing))\n", " print(f\"{csv_path} | missing columns: {', '.join(missing)}\")\n", " continue\n", "\n", " # keep only required columns, in exact order\n", " df = df[REQUIRED_COLS].copy()\n", " good_frames.append(df)\n", " included_paths.append(csv_path)\n", " \n", "\n", "if not good_frames:\n", " print(\"\\nNo valid CSVs matched the required headers. Nothing to combine.\")\n", "else:\n", " combined = pd.concat(good_frames, axis=0, ignore_index=True)\n", "\n", " if DEDUPE:\n", " before = len(combined)\n", " combined = combined.drop_duplicates()\n", " print(f\"\\nDe-duplicated rows: {before} → {len(combined)}\")\n", "\n", " # ensure exact column order\n", " combined = combined[REQUIRED_COLS]\n", "\n", " # Replace question_ID values with fresh UUID4 strings (universal identifiers)\n", " combined[\"question_ID\"] = [str(uuid.uuid4()) for _ in range(len(combined))]\n", "\n", " # write output\n", " OUT_CSV.parent.mkdir(parents=True, exist_ok=True)\n", " combined.to_csv(OUT_CSV, index=False)\n", "\n", " # summary\n", " print(\"\\nSummary\")\n", " print(\"-------\")\n", " print(f\"Included files: {len(included_paths)}\")\n", " print(f\"Total combined rows: {len(combined)}\")\n", " print(f\"Done\")\n", "\n", " if excluded_missing:\n", " print(\"\\nFiles excluded due to missing columns:\")\n", " for p, missing in excluded_missing:\n", " print(f\" - {p} (missing: {', '.join(missing)})\")\n", "\n", " if excluded_errors:\n", " print(\"\\nFiles excluded due to read errors:\")\n", " for p, msg in excluded_errors:\n", " print(f\" - {p} ({msg})\")\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }