File size: 10,779 Bytes
0a4b5f6 b2300b5 8ca18bd 662644e 0dca83b 0a4b5f6 8ca18bd 0a4b5f6 8ca18bd 0a4b5f6 b2300b5 0a4b5f6 20dc524 0a4b5f6 7c91de7 0a4b5f6 497d950 0a4b5f6 7c91de7 0a4b5f6 7c91de7 0a4b5f6 7c91de7 0a4b5f6 7c91de7 0a4b5f6 | 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | {
"cells": [
{
"cell_type": "markdown",
"id": "0",
"metadata": {},
"source": [
"# AI-Assisted Data Curation Toolkit"
]
},
{
"cell_type": "markdown",
"id": "1",
"metadata": {},
"source": [
"This notebook demonstrates the AI-Assisted Data Curation Toolkit. It is capable of suggesting harmonizations from a source data model into a target data model using AI-backed approaches, but leaving the expert curator in complete control."
]
},
{
"cell_type": "markdown",
"id": "197ad41c",
"metadata": {},
"source": [
"> NOTE: If using Google Colab, ensure you select a runtime with TPU or GPU capabilities for best performance."
]
},
{
"cell_type": "markdown",
"id": "2",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eea4e666",
"metadata": {},
"outputs": [],
"source": [
"!git clone https://huggingface.co/spaces/uc-ctds/ai_assisted_data_curation_toolkit"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8d2099c2",
"metadata": {},
"outputs": [],
"source": [
"%cd ai_assisted_data_curation_toolkit"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e9f03bd7",
"metadata": {},
"outputs": [],
"source": [
"%pip install ai_harmonization"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "870dd9bd",
"metadata": {},
"outputs": [],
"source": [
"%pip install anywidget"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"\n",
"from ai_harmonization.interactive import (\n",
" get_interactive_table_for_suggestions,\n",
" get_nodes_and_properties_df,\n",
")\n",
"from ai_harmonization.simple_data_model import (\n",
" SimpleDataModel,\n",
" get_data_model_as_node_prop_type_descriptions,\n",
")\n",
"from ai_harmonization.harmonization_approaches.similarity_inmem import (\n",
" SimilaritySearchInMemoryVectorDb,\n",
")\n",
"from ai_harmonization.harmonization_approaches.embeddings import BGEEmbeddings"
]
},
{
"cell_type": "markdown",
"id": "5",
"metadata": {},
"source": [
"Set available GPUs (skip this step if using CPUs or are okay with default behavior)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6",
"metadata": {},
"outputs": [],
"source": [
"# os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0,1,2,3\" # change as necessary"
]
},
{
"cell_type": "markdown",
"id": "7",
"metadata": {},
"source": [
"## Use a Harmonization Approach to get Suggestions"
]
},
{
"cell_type": "markdown",
"id": "8",
"metadata": {},
"source": [
"### Get Input Data\n",
"\n",
"- A `source data model` you want to harmonize from\n",
"- A `target data model` you want to harmonize to\n",
"\n",
"For this initial example, you can just using hard-coded examples.\n",
"\n",
"- The `example_synthetic_source_model.json` is a synthetically generated model for example purposes\n",
"- The `example_real_source_model.json` is a real original study before ingestion into the NHLBI BioData Catalyst ecosystem (e.g. not yet harmonized)\n",
"- The `target data model` example is the **NHLBI BioData Catalyst Gen3 Data Dictionary v4.6.5** (latest version as of 21 AUG 2025)\n",
"\n",
"You can change this to supply your own source model, so long as the format follows the example. Similarly for target model. The source model will eventually come from a connection to a previously released AI-backed tool for Schema Generation, allowing this entire flow to start from arbitrary TSVs."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9",
"metadata": {},
"outputs": [],
"source": [
"# source_file = \"./examples/example_synthetic_source_model.json\"\n",
"source_file = \"./examples/example_real_source_model.json\"\n",
"\n",
"target_file = \"./examples/example_target_model_BDC.json\"\n",
"\n",
"with open(source_file, \"r\") as f:\n",
" input_source_model = json.load(f)\n",
"\n",
"input_source_model = SimpleDataModel.get_from_unknown_json_format(\n",
" json.dumps(input_source_model)\n",
")\n",
"\n",
"with open(target_file, \"r\") as f:\n",
" input_target_model = json.load(f)\n",
"\n",
"input_target_model = SimpleDataModel.get_from_unknown_json_format(\n",
" json.dumps(input_target_model)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10",
"metadata": {},
"outputs": [],
"source": [
"print(\"Source Model\")\n",
"input_source_model.get_property_df()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11",
"metadata": {},
"outputs": [],
"source": [
"print(\"Target Model\")\n",
"input_target_model.get_property_df()"
]
},
{
"cell_type": "markdown",
"id": "12",
"metadata": {},
"source": [
"### Use a Specific Harmonization Approach to get Suggestions"
]
},
{
"cell_type": "markdown",
"id": "deb30aa8",
"metadata": {},
"source": [
"We are using a specially trained embedding model created by UChicago CTDS, which is optimized for variable-level mapping. \n",
"\n",
"You can view details of the model here: https://huggingface.co/uc-ctds/bge-large-en-v1.5-bio-mapping"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13",
"metadata": {},
"outputs": [],
"source": [
"embedding_fn = BGEEmbeddings(model_name=\"uc-ctds/bge-large-en-v1.5-bio-mapping\")\n",
"batch_size = 32\n",
"\n",
"harmonization_approach = SimilaritySearchInMemoryVectorDb(\n",
" # A unique name for this file and embedding algorithm within the limits of the length required by the in-memory vectostore\n",
" vectordb_persist_directory_name=f\"{os.path.basename(target_file)[:53]}-{embedding_fn.model_name.split(\"/\")[-1][:5]}-0\",\n",
" input_target_model=input_target_model,\n",
" embedding_function=embedding_fn,\n",
" batch_size=batch_size,\n",
")\n",
"\n",
"# By default, get all options (will eventually sort by most relevant)\n",
"max_suggestions_per_property = len(harmonization_approach.vectorstore.get()[\"ids\"])\n",
"# max_suggestions_per_property = 10\n",
"\n",
"# set threshold low to just get top properties no matter what\n",
"score_threshold = 0\n",
"\n",
"suggestions = harmonization_approach.get_harmonization_suggestions(\n",
" input_source_model=input_source_model,\n",
" input_target_model=input_target_model,\n",
" score_threshold=score_threshold,\n",
" k=max_suggestions_per_property,\n",
")"
]
},
{
"cell_type": "markdown",
"id": "14",
"metadata": {},
"source": [
"### Visualize Suggestions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15",
"metadata": {},
"outputs": [],
"source": [
"table_df = suggestions.to_simlified_dataframe()\n",
"table_df.sort_values(by=\"Similarity\", ascending=False, inplace=True)\n",
"table_df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16",
"metadata": {},
"outputs": [],
"source": [
"# Group by 'Original Node.Property' and find the index of max similarity for each group\n",
"idx = table_df.groupby(\"Original Node.Property\")[\"Similarity\"].idxmax()\n",
"\n",
"# Filter DataFrame using the indices found above\n",
"filtered_df = table_df.loc[idx]\n",
"filtered_df.drop(columns=[\"Original Description\", \"Target Description\"], inplace=True)\n",
"filtered_df.sort_values(by=\"Similarity\", ascending=False, inplace=True)\n",
"filtered_df"
]
},
{
"cell_type": "markdown",
"id": "17",
"metadata": {},
"source": [
"### Create Interactive Table for Selecting Suggestions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18",
"metadata": {},
"outputs": [],
"source": [
"table = get_interactive_table_for_suggestions(\n",
" table_df,\n",
" column_for_filtering=1,\n",
" # additional config for the interactive table\n",
" maxBytes=\"2MB\",\n",
" pageLength=50,\n",
")\n",
"table"
]
},
{
"cell_type": "markdown",
"id": "19",
"metadata": {},
"source": [
"> **Don't see the table or see an error above?** Try restarting the kernel, then try restarting jupyter lab (if that's what you're using). The installs for AnyWidgets might not be picked up yet.\n",
"\n",
"> **Colors / Theme off?** If you're using a dark theme, you might need to switch to light for the table to display properly (or vice-versa).\n",
"\n",
"> **Using VS Code Jupyter Extension?** Any Embedded links (if they exist) might not work"
]
},
{
"cell_type": "markdown",
"id": "20",
"metadata": {},
"source": [
"To use the selections above, record them below in `manual_selection_indexes` or ue multi-select in the above table and the below will automatically use those. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "21",
"metadata": {},
"outputs": [],
"source": [
"# Fill this out manually as you go, or we'll use the table selections\n",
"manual_selection_indexes = [] # [1, 8, 24, ...]\n",
"\n",
"selected_rows = manual_selection_indexes or table.selected_rows\n",
"\n",
"print(f\"Selected Suggestions: {selected_rows}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "22",
"metadata": {},
"outputs": [],
"source": [
"table_df.loc[selected_rows]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23",
"metadata": {},
"outputs": [],
"source": [
"table_df.loc[selected_rows].to_csv(\n",
" \"./selected_suggestions.tsv\",\n",
" index=False,\n",
" na_rep=\"N/A\",\n",
" sep=\"\\t\",\n",
" quotechar='\"',\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ai-harmonization (3.13.5)",
"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.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|