index_builder
SNOMED attribute and reference index builder for pgvector.
Creates/populates two PostgreSQL tables in VOCAB_SCHEMA: snomed_attribute — attribute value concepts with embeddings. snomed_reference — 10 K sampled SNOMED disorder terms with embeddings.
This module is the canonical package-level version of sandbox/build_pg_indexes.py.
The sandbox script is kept as a standalone convenience but this module is what
the CLI (python -m ariadne.hierarchy build-index) calls.
Usage::
python -m ariadne.hierarchy build-index # build if tables empty
python -m ariadne.hierarchy build-index --check # skip if already populated
python -m ariadne.hierarchy build-index --rebuild # truncate and rebuild
python -m ariadne.hierarchy build-index --attributes-only
python -m ariadne.hierarchy build-index --reference-only
Environment variables (loaded from .env): VOCAB_CONNECTION_STRING_ADM psycopg DSN with write/DDL access VOCAB_CONNECTION_STRING SQLAlchemy URL for read-only data loading VOCAB_SCHEMA OMOP vocabulary schema name EMBEDDING_MODEL Embedding model name (via GENAI_PROVIDER routing)
build(rebuild=False, attributes_only=False, reference_only=False, check=False, reference_sample_size=REFERENCE_SAMPLE_SIZE)
Build (or rebuild) the pgvector SNOMED indexes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rebuild
|
bool
|
Truncate existing data before inserting. |
False
|
attributes_only
|
bool
|
Only build |
False
|
reference_only
|
bool
|
Only build |
False
|
check
|
bool
|
Skip any table that is already populated (takes precedence over rebuild only when the table is non-empty). |
False
|
reference_sample_size
|
int
|
Number of unique source concepts for the reference index (default 10 000). |
REFERENCE_SAMPLE_SIZE
|
Source code in src/ariadne/hierarchy/index_builder.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | |
check_populated(conn, table='snomed_attribute')
Return True if table already contains at least one row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
Connection
|
psycopg connection (read access is enough). |
required |
table
|
str
|
Table name to check ( |
'snomed_attribute'
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/ariadne/hierarchy/index_builder.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
create_tables(conn, dim=3072)
Create snomed_attribute and snomed_reference tables with HNSW indexes.
Safe to call when tables already exist (uses CREATE TABLE IF NOT EXISTS).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
Connection
|
Admin psycopg connection. |
required |
dim
|
int
|
Embedding dimension (default 3072 for text-embedding-3-large). |
3072
|
Source code in src/ariadne/hierarchy/index_builder.py
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 | |
load_attributes_from_db()
Load SNOMED attribute value rows from the OMOP vocabulary DB.
Source code in src/ariadne/hierarchy/index_builder.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
load_reference_from_db(sample_size=REFERENCE_SAMPLE_SIZE)
Load and sample SNOMED reference rows from the OMOP vocabulary DB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_size
|
int
|
Number of unique source concepts to include (default 10 000). |
REFERENCE_SAMPLE_SIZE
|
Source code in src/ariadne/hierarchy/index_builder.py
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 | |
upsert_attribute_index(conn, df, rebuild=False)
Embed attribute concept names and insert rows into snomed_attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
Connection
|
Admin psycopg connection (returned — may be replaced on retry). |
required |
df
|
DataFrame
|
DataFrame from :func: |
required |
rebuild
|
bool
|
When True, truncate the table first. |
False
|
Returns:
| Type | Description |
|---|---|
Connection
|
The (possibly reconnected) psycopg connection. |
Source code in src/ariadne/hierarchy/index_builder.py
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 | |
upsert_reference_index(conn, df, rebuild=False)
Embed unique source-concept names and insert rows into snomed_reference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
Connection
|
Admin psycopg connection (returned — may be replaced on retry). |
required |
df
|
DataFrame
|
DataFrame from :func: |
required |
rebuild
|
bool
|
When True, truncate the table first. |
False
|
Returns:
| Type | Description |
|---|---|
Connection
|
The (possibly reconnected) psycopg connection. |
Source code in src/ariadne/hierarchy/index_builder.py
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 | |