File size: 5,102 Bytes
7a6bc94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
{
 "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
}