hierarchy
SNOMED CT attribute extraction pipeline (hierarchy sub-package).
Public API re-exports for convenient imports::
from ariadne.hierarchy import (
HierarchySettings,
SnomedAttributeSearcher,
SnomedReferenceSearcher,
find_attributes_two_stage,
process_gold_standard,
evaluate_results,
)
AbstractSnomedSearcher
Bases: ABC
ABC for pgvector-backed SNOMED searchers.
Enforces lifecycle management (close / context-manager) and cost
tracking — matching the patterns used by PgvectorConceptSearcher and
LlmMapper.
Subclasses must implement :meth:search.
Source code in src/ariadne/hierarchy/searchers.py
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 | |
close()
Close the database connection.
Source code in src/ariadne/hierarchy/searchers.py
116 117 118 | |
get_total_cost()
Return accumulated embedding cost (USD).
Source code in src/ariadne/hierarchy/searchers.py
112 113 114 | |
search(text, *args, **kwargs)
abstractmethod
Run a similarity search. Signature varies by subclass.
Source code in src/ariadne/hierarchy/searchers.py
106 107 108 | |
ContentFilterError
Bases: Exception
Raised when the LLM content filter blocks a response.
Source code in src/ariadne/hierarchy/pipeline.py
45 46 | |
EvaluationConfig
dataclass
Output and gold-standard paths for hierarchy evaluation.
Source code in src/ariadne/utils/settings.py
230 231 232 233 234 235 236 | |
ExtractionResult
Bases: NamedTuple
Return type for :func:~ariadne.hierarchy.pipeline.extract_components.
Source code in src/ariadne/hierarchy/types.py
29 30 31 32 33 | |
HierarchySettings
dataclass
Settings block loaded from the optional top-level hierarchy config key.
Source code in src/ariadne/utils/settings.py
247 248 249 250 251 252 253 254 255 256 | |
LlmResult
Bases: NamedTuple
Return type for :func:~ariadne.hierarchy.pipeline.call_llm.
Source code in src/ariadne/hierarchy/types.py
22 23 24 25 26 | |
ModelsConfig
dataclass
LLM / embedding model identifiers for hierarchy extraction.
Source code in src/ariadne/utils/settings.py
204 205 206 207 208 209 210 | |
PromptsConfig
dataclass
Prompt templates for hierarchy extraction and candidate selection.
Source code in src/ariadne/utils/settings.py
239 240 241 242 243 244 | |
ReferenceRetrievalResult
Bases: NamedTuple
Return type for _retrieve_reference_examples.
Source code in src/ariadne/hierarchy/types.py
57 58 59 60 61 62 | |
ReferenceSearchResult
Bases: NamedTuple
Return type for reference search and find_similar_reference_terms.
Source code in src/ariadne/hierarchy/types.py
50 51 52 53 54 | |
RetrievalConfig
dataclass
Retrieval-stage hyper-parameters for hierarchy extraction.
Source code in src/ariadne/utils/settings.py
213 214 215 216 217 218 219 | |
ScoringConfig
dataclass
Similarity score overrides used by hierarchy ranking.
Source code in src/ariadne/utils/settings.py
222 223 224 225 226 227 | |
SearchBatchResult
Bases: NamedTuple
Return type for search_batch.
Source code in src/ariadne/hierarchy/types.py
43 44 45 46 47 | |
SearchResult
Bases: NamedTuple
Return type for attribute search and _retrieve_candidates.
Source code in src/ariadne/hierarchy/types.py
36 37 38 39 40 | |
SnomedAttributeSearcher
Bases: AbstractSnomedSearcher
Source code in src/ariadne/hierarchy/searchers.py
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 | |
expand_via_hierarchy(concept_ids, attribute_category)
Expand candidate concept IDs by 1-hop SNOMED hierarchy.
For each concept_id, retrieves its immediate parents (Is a) and
children (Subsumes) from concept_relationship, then filters to
those that exist in snomed_attribute under the same
attribute_category. No embedding API call is needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
concept_ids
|
list[int]
|
Seed concept IDs to expand. |
required |
attribute_category
|
str
|
SNOMED attribute category filter. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns |
DataFrame
|
|
DataFrame
|
where similarity is set to |
Source code in src/ariadne/hierarchy/searchers.py
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 | |
search(text, category_name, top_k=None)
Embed text and return the closest concepts in the given attribute category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Free-text value to embed and search for. |
required |
category_name
|
str
|
SNOMED attribute category filter. |
required |
top_k
|
int | None
|
Maximum number of results (defaults to |
None
|
Returns:
| Type | Description |
|---|---|
SearchResult
|
SearchResult(dataframe, cost). |
Source code in src/ariadne/hierarchy/searchers.py
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 | |
search_batch(texts_and_categories, top_k=None)
Embed all texts in a single API call and run per-category queries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts_and_categories
|
list[tuple[str, str, str]]
|
List of |
required |
top_k
|
int | None
|
Number of candidates per category (defaults to |
None
|
Returns:
| Type | Description |
|---|---|
SearchBatchResult
|
SearchBatchResult(results, total_cost). |
Source code in src/ariadne/hierarchy/searchers.py
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 | |
SnomedReferenceSearcher
Bases: AbstractSnomedSearcher
Source code in src/ariadne/hierarchy/searchers.py
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 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 | |
search(text, top_k=None, embedding=None)
Embed text and return similar reference terms with their attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Free-text term to embed (ignored when embedding is supplied). |
required |
top_k
|
int | None
|
Maximum number of reference concepts to return
(defaults to |
None
|
embedding
|
ndarray | None
|
Optional precomputed embedding vector (shape |
None
|
Returns:
| Type | Description |
|---|---|
ReferenceSearchResult
|
ReferenceSearchResult(examples, cost). |
Source code in src/ariadne/hierarchy/searchers.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 372 373 374 375 376 377 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 | |
build_indexes(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 | |
classification_summary(new_is_a, removed, total_source_concepts=None)
Compute summary statistics from classification results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_is_a
|
DataFrame
|
DataFrame of new inferred Is a relationships. |
required |
removed
|
DataFrame
|
DataFrame of removed redundant relationships. |
required |
total_source_concepts
|
int | None
|
Total number of source concepts in the delta
(used to detect orphans). If |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
Dict with keys: |
dict[str, object]
|
|
dict[str, object]
|
|
Source code in src/ariadne/hierarchy/classifier.py
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |
classify_delta(delta_zip, base_snomed_zip=None, *, toolkit_jar=None, java_xms='4g', timeout=600, output_dir=None)
Run ELK classification via snomed-owl-toolkit.
This calls the snomed-owl-toolkit's -classify command which:
- Converts the base SNOMED RF2 snapshot + your delta to OWL
- Runs the ELK reasoner to infer Is a relationships
- Produces a
classification-results-*.zipwith the inferred relationship changes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delta_zip
|
str | Path
|
Path to the RF2 delta ZIP (from |
required |
base_snomed_zip
|
str | None
|
Path to the SNOMED CT International Edition RF2
snapshot ZIP. Falls back to |
None
|
toolkit_jar
|
str | None
|
Path to the snomed-owl-toolkit executable JAR.
Falls back to |
None
|
java_xms
|
str
|
JVM initial heap size (default |
'4g'
|
timeout
|
int
|
Maximum seconds to wait for classification (default 600). |
600
|
output_dir
|
str | Path | None
|
Directory where the results ZIP will be written. Defaults to the parent directory of delta_zip. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the classification results ZIP. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the classification process fails. |
FileNotFoundError
|
If the toolkit JAR or base release cannot be found. |
TimeoutExpired
|
If classification exceeds timeout. |
Source code in src/ariadne/hierarchy/classifier.py
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 | |
evaluate_results(results, gs_path, cfg=None)
Produce a combined evaluation table (full outer join of GS and predictions).
Columns
concept_id_1, concept_name_1, attribute_category,
gs_concept_id_2, gs_concept_name_2,
predicted_concept_id_2, predicted_concept_name_2,
matched, status (match / missed / extra).
Summary statistics are printed and the combined table is saved to CSV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
list[dict]
|
List of pipeline result dicts from |
required |
gs_path
|
str
|
Path to the gold-standard CSV. |
required |
cfg
|
HierarchySettings | None
|
Pipeline configuration (reads |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Combined evaluation DataFrame. |
Source code in src/ariadne/hierarchy/evaluator.py
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 | |
export_to_rf2(source, output_dir, *, date=None, module_id=_MODULE_ID_DEFAULT, stated_parent=_CLINICAL_FINDING, rel_group=1, concept_id_start=_CONCEPT_ID_START, rel_id_start=_REL_ID_START, zip_it=True)
Export Step 2 attribute predictions as an RF2 delta ZIP.
Source OMOP concept_id_1 values are remapped to sequential
synthetic IDs starting at concept_id_start to avoid collisions
with real SNOMED SCTIDs in the base release.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[str, Path, DataFrame]
|
Path to |
required |
output_dir
|
Union[str, Path]
|
Directory where the |
required |
date
|
str | None
|
Effective date string in |
None
|
module_id
|
int
|
SNOMED module concept ID (default: SNOMED CT core module). |
_MODULE_ID_DEFAULT
|
stated_parent
|
Union[int, dict[int, list[str]]]
|
Either a single SCTID |
_CLINICAL_FINDING
|
rel_group
|
int
|
Relationship group number for all predicted attributes (default: 1 — grouped). Pass 0 for ungrouped. |
1
|
concept_id_start
|
int
|
First synthetic concept ID (default: 1 000 000 001). |
_CONCEPT_ID_START
|
rel_id_start
|
int
|
Starting integer for generated relationship IDs. |
_REL_ID_START
|
zip_it
|
bool
|
When True (default), produce a |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
|
DataFrame
|
columns |
tuple[Path, DataFrame]
|
mapping original OMOP IDs to synthetic delta IDs. |
Source code in src/ariadne/hierarchy/rf2_exporter.py
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 | |
find_attributes_two_stage(medical_term, attribute_index, reference_index=None, cfg=None, verbose=True, precomputed_embedding=None)
Run the 4-step SNOMED CT attribute extraction pipeline.
Steps
- Retrieve reference examples (pgvector or in-memory).
- LLM infers applicable attributes.
- Retrieve SNOMED candidate values per attribute.
- LLM selects exact SNOMED concepts from candidates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
medical_term
|
str
|
The clinical term to decompose. |
required |
attribute_index
|
AttributeIndex
|
Attribute searcher (pgvector or legacy dict). |
required |
reference_index
|
ReferenceIndex | None
|
Reference searcher (pgvector, legacy dict, or None). |
None
|
cfg
|
HierarchySettings | None
|
Pipeline configuration. |
None
|
verbose
|
bool
|
Whether to log progress. |
True
|
precomputed_embedding
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Dict with keys |
dict
|
|
Source code in src/ariadne/hierarchy/pipeline.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 | |
parse_classification_results(results_zip)
Parse the classification results ZIP into DataFrames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results_zip
|
str | Path
|
Path to the |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
tuple[DataFrame, DataFrame, DataFrame]
|
|
Source code in src/ariadne/hierarchy/classifier.py
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 | |
pre_classification_checks(delta_zip)
Run lightweight structural checks on an RF2 delta ZIP.
Validates:
- The ZIP contains the expected Terminology files
- StatedRelationship file has correct TSV headers
- All destinationId values look like valid SCTIDs (6+ digit integers)
- All typeId values are in the known set of SNOMED attribute types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delta_zip
|
str | Path
|
Path to the RF2 delta ZIP. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of issue descriptions. Empty list means all checks passed. |
Source code in src/ariadne/hierarchy/classifier.py
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 | |
process_gold_standard(gs_path, attribute_index, reference_index=None, cfg=None, checkpoint_every=5, max_workers=1)
Run the pipeline over every unique term in a gold-standard CSV.
Supports checkpointing and optional parallel execution.
When max_workers > 1, each worker thread creates its own database connections (psycopg is not thread-safe).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gs_path
|
str
|
Path to the gold-standard CSV (must have |
required |
attribute_index
|
AttributeIndex
|
Attribute searcher. |
required |
reference_index
|
ReferenceIndex | None
|
Reference searcher (or None). |
None
|
cfg
|
HierarchySettings | None
|
Pipeline configuration. |
None
|
checkpoint_every
|
int
|
Save a checkpoint every N terms (default 5). |
5
|
max_workers
|
int
|
Number of parallel worker threads (default 1 = sequential). |
1
|
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of result dicts (one per term). |
Source code in src/ariadne/hierarchy/evaluator.py
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 | |
resolve_parent_names(is_a_df, source_names=None, id_mapping=None)
Add human-readable names to inferred Is a relationships.
Resolves destinationId (SNOMED SCTIDs) to concept names via the
vocabulary database, and sourceId (synthetic IDs) back to OMOP IDs
using the ID mapping from export_to_rf2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
is_a_df
|
DataFrame
|
DataFrame with |
required |
source_names
|
dict[int, str] | DataFrame | None
|
Mapping of source concept ID → name. Can be:
- |
None
|
id_mapping
|
DataFrame | None
|
DataFrame with |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns: |
DataFrame
|
|
Source code in src/ariadne/hierarchy/classifier.py
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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | |