Skip to content

Add OMOP Condition Hierarchy (split from PR#1661) - #1709

Merged
solarsys merged 6 commits into
sunlabuiuc:masterfrom
rishabshah:rishab/omop-hierarchy
Dec 3, 2026
Merged

solarsys merged 6 commits into
sunlabuiuc:masterfrom
rishabshah:rishab/omop-hierarchy

Conversation

@rishabshah

@rishabshah rishabshah commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Split from #1661. This PR carries only the OMOP vocabulary layer of the KEEP integration. The other two parts are in #1707 (generic co-occurrence counter) and #1706 (ancestor roll-up and KEEP clinical histories).

Contributors

Contribution Type

Medical-code utilities (pyhealth.medcode), part of a reproducibility contribution.

Paper

Elhussein et al., "KEEP: Integrating Medical Ontologies with Clinical Data for Robust Code Embeddings," CHIL 2025 (PMLR 287).

Description

KEEP's first stage embeds the SNOMED condition hierarchy from the OMOP vocabularies, cut a few levels below "Disease" (§A.1.1). This PR adds that hierarchy to pyhealth.medcode, built from a local Athena download:

  • OMOPVocabulary reads Athena's CONCEPT, CONCEPT_RELATIONSHIP and CONCEPT_ANCESTOR files lazily with polars. OMOPVocabularyFormat sets the separator, quoting, encoding and file names; the defaults match Athena's tab-separated export.
  • OMOPConditionHierarchy holds the depth-limited SNOMED condition graph (standard, valid Condition concepts under "Disease", 4274025). Depth can be measured by the shortest or longest path, and concepts left without a parent by the cut are re-attached to their nearest kept ancestor. The hierarchy can be cut shallower with restrict() without re-reading Athena, saved and loaded as parquet, and exported as triples or a pyhealth.graph.KnowledgeGraph.
  • ICDToSNOMEDMapper maps ICD-9-CM and ICD-10-CM codes to standard concepts through "Maps to" links. coverage() reports how many codes in a table map and land in the hierarchy, as counts only.

Athena is licensed and is not distributed here. Nothing is written into the repository: the tests build a small synthetic Athena bundle in a temporary directory, and the example caches its output under --cache (default ~/.cache/pyhealth/omop_keep).

Relation to #1706. #1706 currently includes its own copy of these modules (42556b0, noted there as replaceable by this PR). The modules and tests here are identical to that copy line for line, so the Python files merge without conflicts. If #1706 is rebased after this merges, that commit drops out. The only overlap is #1706's three-line roll-up pointer at the end of docs/api/medcode.rst, which belongs with #1706.

Files to Review

  • pyhealth/medcode/omop/vocabulary.py: OMOPVocabulary, OMOPVocabularyFormat.
  • pyhealth/medcode/omop/condition_hierarchy.py: OMOPConditionHierarchy.
  • pyhealth/medcode/omop/icd_mapper.py: ICDToSNOMEDMapper, normalize_icd_code.
  • pyhealth/medcode/omop/__init__.py and pyhealth/medcode/__init__.py: exports.
  • docs/api/medcode.rst: new "OMOP Vocabularies (Athena)" section.
  • examples/omop_keep_condition_graph_check.py: builds the hierarchy at several depths and, given MIMIC-IV, reports ICD coverage. It prints counts only.
  • tests/core/omop_test_utils.py: the synthetic Athena bundle.
  • tests/core/test_omop_{vocabulary,condition_hierarchy,icd_mapper}.py: the tests.

Testing

pytest tests/core/test_omop_vocabulary.py tests/core/test_omop_condition_hierarchy.py \
       tests/core/test_omop_icd_mapper.py
pytest --doctest-modules pyhealth/medcode/omop
python tools/check_pr_rules.py --base upstream/master --head HEAD
  • Unit tests: 43 passed. The slowest test takes 0.05 s. Two KnowledgeGraph export tests skip when torch-geometric is not installed.
  • Doctests: 5 passed. The two examples that need a real Athena download are marked +SKIP.
  • PR contribution rules: passed.
  • Each commit: the OMOP tests pass at every commit.
  • Example: the example script ran end to end on the synthetic bundle, including a cached rerun.

rishabshah and others added 5 commits November 12, 2026 13:49
Reads Athena OMOP vocabularies and builds the SNOMED "Disease" hierarchy
used by KEEP (depth-limited, all parents kept, orphans re-attached) plus an
ICD-9-CM/ICD-10-CM to standard concept mapper that accepts MIMIC's dot-free
codes. Adds synthetic-data tests, docs, and an example script that reports
the depth ablation and MIMIC-IV coverage as counts only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JrebVyPi1jFyjkAiLbLSC1
- Split omop.py into pyhealth/medcode/omop/ (vocabulary, condition_hierarchy,
  icd_mapper); public imports unchanged. Tests split to match, sharing
  tests/core/omop_test_utils.py.
- Rename ConditionHierarchy to OMOPConditionHierarchy.
- to_triples(direction=...) and new to_knowledge_graph(); rescue edges get
  their own relation; knowledge graph defaults to parent -> child so
  GraphProcessor reaches ancestors from a child seed.
- Add OMOPVocabularyFormat for non-Athena vocabulary files.
- map_frame() and coverage() accept polars LazyFrames.
- Docs: KnowledgeGraph/GraphProcessor example; rescue described as an
  adapter policy. Example script documents task and processor entry points.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JrebVyPi1jFyjkAiLbLSC1
Carries the vocabulary.py part of 24341db ("Reduce OMOP ancestor
loading for KEEP roll-up") from sunlabuiuc#1661. The KEEP roll-up changes,
their tests and the notebook from that commit stay with sunlabuiuc#1706.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JrebVyPi1jFyjkAiLbLSC1
Takes the OMOP module annotations from 42556b0 in sunlabuiuc#1706 (built-in
generics, `X | None`, unquoted forward references, sorted `__all__`).
PyHealth's PR contribution check rejects the typing.Dict/Optional
spellings. Behavior is unchanged, and the OMOP modules now match
sunlabuiuc#1706's copy line for line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JrebVyPi1jFyjkAiLbLSC1
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Test Coverage

Total: 89.32% (68104 / 76250 lines)

Modified file Coverage Lines
pyhealth/autoimprove/__init__.py 100.00% 18 / 18
pyhealth/autoimprove/config.py 93.47% 186 / 199
pyhealth/autoimprove/evaluation/__init__.py 100.00% 1 / 1
pyhealth/autoimprove/evaluation/explain.py 92.13% 363 / 394
pyhealth/autoimprove/evaluation/guardrails.py 91.27% 230 / 252
pyhealth/autoimprove/evaluation/holdout.py 98.25% 56 / 57
pyhealth/autoimprove/evaluation/records.py 95.95% 71 / 74
pyhealth/autoimprove/evaluation/report.py 94.86% 664 / 700
pyhealth/autoimprove/evaluation/review.py 93.33% 70 / 75
pyhealth/autoimprove/evaluation/selection.py 91.57% 228 / 249
pyhealth/autoimprove/evaluation/training.py 86.94% 366 / 421
pyhealth/autoimprove/improver.py 92.06% 116 / 126
pyhealth/autoimprove/search/__init__.py 100.00% 1 / 1
pyhealth/autoimprove/search/analysis.py 84.82% 218 / 257
pyhealth/autoimprove/search/features.py 93.79% 589 / 628
pyhealth/autoimprove/search/literature.py 93.54% 536 / 573
pyhealth/autoimprove/search/prompts.py 82.38% 187 / 227
pyhealth/autoimprove/search/proposers.py 87.50% 168 / 192
pyhealth/autoimprove/search/space.py 98.39% 244 / 248
pyhealth/autoimprove/search/turns.py 86.29% 277 / 321
pyhealth/autoimprove/state.py 96.09% 491 / 511
pyhealth/autoimprove/utils/__init__.py 100.00% 1 / 1
pyhealth/autoimprove/utils/data.py 84.71% 72 / 85
pyhealth/autoimprove/utils/llm.py 97.09% 100 / 103
pyhealth/autoimprove/utils/sandbox.py 82.24% 412 / 501
pyhealth/autoimprove/utils/store.py 90.95% 201 / 221
pyhealth/benchmarks/__init__.py 100.00% 4 / 4
pyhealth/benchmarks/robustness.py 85.71% 30 / 35
pyhealth/calib/calibration/hb.py 88.60% 101 / 114
pyhealth/calib/calibration/temperature_scale.py 100.00% 45 / 45
pyhealth/datasets/__init__.py 100.00% 95 / 95
pyhealth/datasets/chestxray14.py 84.26% 91 / 108
pyhealth/datasets/gwas_proxy_sim.py 97.70% 85 / 87
pyhealth/datasets/gwas_simulator.py 96.67% 116 / 120
pyhealth/datasets/ham10000.py 94.48% 137 / 145
pyhealth/datasets/hbn_eeg.py 94.59% 140 / 148
pyhealth/datasets/hirid.py 100.00% 32 / 32
pyhealth/datasets/physionet2012.py 96.64% 115 / 119
pyhealth/datasets/seer.py 92.19% 59 / 64
pyhealth/graph/cooccurrence.py 100.00% 37 / 37
pyhealth/medcode/__init__.py 100.00% 16 / 16
pyhealth/medcode/codes/ndc.py 72.73% 16 / 22
pyhealth/medcode/omop/__init__.py 100.00% 4 / 4
pyhealth/medcode/omop/condition_hierarchy.py 97.39% 112 / 115
pyhealth/medcode/omop/icd_mapper.py 98.31% 58 / 59
pyhealth/medcode/omop/vocabulary.py 98.10% 103 / 105
pyhealth/medcode/pretrained_embeddings/lm_emb/openai_retriever.py 100.00% 13 / 13
pyhealth/metrics/__init__.py 100.00% 21 / 21
pyhealth/metrics/fairness_selection.py 100.00% 27 / 27
pyhealth/metrics/group_fairness.py 100.00% 97 / 97
pyhealth/metrics/gwas_discovery.py 98.39% 61 / 62
pyhealth/metrics/labtop.py 98.61% 71 / 72
pyhealth/models/__init__.py 100.00% 139 / 139
pyhealth/models/apex_gp.py 95.96% 499 / 520
pyhealth/models/brain_network_transformer.py 98.20% 164 / 167
pyhealth/models/causal_gated_imputer.py 98.75% 79 / 80
pyhealth/models/cehrgpt.py 98.31% 116 / 118
pyhealth/models/deepcoxph.py 96.43% 108 / 112
pyhealth/models/dkaft.py 95.37% 371 / 389
pyhealth/models/duett.py 93.52% 101 / 108
pyhealth/models/fedweight.py 98.59% 279 / 283
pyhealth/models/generators/corgan.py 98.77% 241 / 244
pyhealth/models/generators/gpt2.py 86.01% 123 / 143
pyhealth/models/generators/halo.py 92.07% 325 / 353
pyhealth/models/generators/medgan.py 98.95% 189 / 191
pyhealth/models/generators/promptehr.py 88.89% 184 / 207
pyhealth/models/generators/synthea.py 95.96% 261 / 272
pyhealth/models/giantmed.py 96.69% 409 / 423
pyhealth/models/labram.py 98.81% 250 / 253
pyhealth/models/labtop.py 94.87% 37 / 39
pyhealth/models/llm_code_transfer.py 94.61% 614 / 649
pyhealth/models/protoecgnet.py 98.18% 377 / 384
pyhealth/models/protoecgnet_h1.py 94.53% 259 / 274
pyhealth/models/protoecgnet_h1_checkpoint.py 87.71% 157 / 179
pyhealth/models/protoecgnet_h1_data.py 88.80% 111 / 125
pyhealth/models/protoecgnet_h1_reference.py 100.00% 153 / 153
pyhealth/models/robust_shy.py 91.67% 22 / 24
pyhealth/models/salientsleepnet.py 99.47% 189 / 190
pyhealth/models/shy.py 94.43% 339 / 359
pyhealth/models/stagenet_mha.py 94.33% 266 / 282
pyhealth/models/stepwise_embedding.py 95.58% 173 / 181
pyhealth/models/surgical_aggregation.py 96.30% 156 / 162
pyhealth/models/survival_mdn.py 94.93% 281 / 296
pyhealth/models/t3a.py 98.11% 104 / 106
pyhealth/models/temporalscl.py 87.63% 248 / 283
pyhealth/models/tpc.py 99.29% 422 / 425
pyhealth/nlp/metrics.py 84.17% 218 / 259
pyhealth/processors/__init__.py 95.92% 47 / 49
pyhealth/processors/band_power_processor.py 98.80% 82 / 83
pyhealth/processors/seer_processor.py 95.40% 83 / 87
pyhealth/processors/vit_embedding_processor.py 89.60% 155 / 173
pyhealth/sampler/__init__.py 100.00% 5 / 5
pyhealth/sampler/group_sampler.py 100.00% 83 / 83
pyhealth/tasks/__init__.py 98.70% 152 / 154
pyhealth/tasks/barttender_chestxray14.py 94.00% 94 / 100
pyhealth/tasks/barttender_encoding.py 100.00% 126 / 126
pyhealth/tasks/barttender_giou.py 100.00% 60 / 60
pyhealth/tasks/cehrgpt_omop.py 97.16% 205 / 211
pyhealth/tasks/drug_recommendation.py 94.98% 265 / 279
pyhealth/tasks/fedweight_eicu.py 100.00% 91 / 91
pyhealth/tasks/fedweight_mimic3.py 96.32% 183 / 190
pyhealth/tasks/ham10000_malignancy.py 100.00% 42 / 42
pyhealth/tasks/hbn_psychopathology.py 98.67% 74 / 75
pyhealth/tasks/hbn_response_time.py 100.00% 134 / 134
pyhealth/tasks/in_icu_mortality_emdot_mimic4.py 95.82% 275 / 287
pyhealth/tasks/labtop.py 96.60% 199 / 206
pyhealth/tasks/phenotype_imputation.py 100.00% 29 / 29
pyhealth/tasks/physionet2012_stepwise_mortality.py 85.42% 41 / 48
pyhealth/tasks/ptbxl_reduced_lead_rhythm.py 100.00% 23 / 23
pyhealth/tasks/seer_survival.py 96.97% 32 / 33
pyhealth/tasks/shhs_multimodal_masking.py 90.48% 57 / 63
pyhealth/tasks/shhs_multimodal_sleep_staging.py 92.03% 127 / 138
pyhealth/tasks/sleep_staging.py 93.48% 86 / 92
pyhealth/tasks/sleep_staging_v2.py 98.84% 85 / 86
pyhealth/tasks/temporal_outcome_prediction_mimic4.py 85.81% 248 / 289
pyhealth/tasks/tpc_eicu.py 98.01% 197 / 201
pyhealth/tasks/tuh_referential_tasks.py 100.00% 37 / 37
pyhealth/tokenizer.py 79.75% 63 / 79

Workflow run · 9c7cc5f

@solarsys solarsys added the status: manual Pending maintainer's review label Nov 30, 2026
@solarsys

Copy link
Copy Markdown
Collaborator

Why this PR has status: manual: it edits .gitignore; files outside docs/ examples/ pyhealth/ test-resources/ tests/ send a PR to manual review. Everything else (the OMOP vocabulary, hierarchy and ICD mapper under medcode/omop/) is self-contained. Thanks for splitting #1661 as suggested.

To make it eligible: drop the .gitignore change, and write any generated files to a temporary or user-chosen directory. Then we'll switch the label to status: automatic and request an AI review.

Copy link
Copy Markdown
Collaborator

/review


Generated by Claude Code

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous review

Reviewed commit 78448c8

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous review

AI Review

suggestion: approve
decision: human review

Thank you for this contribution. It adds an OMOP/Athena vocabulary reader, a depth-limited SNOMED condition hierarchy (as used by KEEP), and an ICD-9/10 to standard-concept mapper to pyhealth.medcode. It looks ready to merge.

What I checked:

  • The code matches the description and docs. The new classes sit in their own package, and the changes to pyhealth/medcode/__init__.py only add exports.
  • The only link to another component is the lazy pyhealth.graph.KnowledgeGraph import in to_knowledge_graph. The description names this export, so that is fine.
  • Coverage is above the bar. Every changed file is at 97% or higher, and the whole repository is at 88.29%.
  • Neither the Athena data nor the MIMIC-IV data is committed. The .gitignore entries and the synthetic test bundle handle this well.
  • The paper and the original code are cited with links.

Optional suggestions:

  • The fallback that adds the root concept when a bundle omits its self-row (in vocabulary.py) is untested. It is probably unreachable with real Athena files, so you could either add a small test or drop it.
  • coverage() counts null ICD codes as "unknown". You could say so in the docstring, or filter nulls out.
  • The public ICDToSNOMEDMapper constructor does not normalize codes. A one-line note in the docstring that icd_code must already be dot-free and upper-case would help.
  • Orphan rescue edges are on by default and the docs say they are not part of KEEP. That is clear as written.

Reviewed commit 78448c8

Changes outside docs/, examples/, pyhealth/, test-resources/ and tests/
send a PR to manual review. The OMOP code never writes into the repo:
tests use temporary directories and the example caches under --cache
(default ~/.cache/pyhealth/omop_keep), so the ignore entries for local
Athena and MIMIC-IV downloads are not needed here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JrebVyPi1jFyjkAiLbLSC1
@rishabshah

Copy link
Copy Markdown
Contributor Author

Why this PR has status: manual: it edits .gitignore; files outside docs/ examples/ pyhealth/ test-resources/ tests/ send a PR to manual review. Everything else (the OMOP vocabulary, hierarchy and ICD mapper under medcode/omop/) is self-contained. Thanks for splitting #1661 as suggested.

To make it eligible: drop the .gitignore change, and write any generated files to a temporary or user-chosen directory. Then we'll switch the label to status: automatic and request an AI review.

Dropped the .gitignore change (6th commit). Nothing writes into the repo: the tests use temporary directories, and the example caches under --cache (default ~/.cache/pyhealth/omop_keep). Ready for status: automatic when you are.

@lee-leonardo

Copy link
Copy Markdown
Contributor

/review

@solarsys solarsys added status: automatic The PR will go through automatic review process. and removed status: manual Pending maintainer's review labels Dec 3, 2026
@solarsys

solarsys commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Thanks, with the .gitignore change gone every file is inside the AI-review folders, so I've switched the label to status: automatic. An AI review on the new head follows.

@solarsys

solarsys commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit 9c7cc5f

from tests.core.omop_test_utils import ROOT, AthenaFixture

try:
from pyhealth.processors import GraphProcessor

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[medium] Undisclosed link to GraphProcessor in tests

The tests import pyhealth.processors.GraphProcessor and check the hierarchy's edge direction through it. That is a new link from this component to another concrete component. The PR description names only KnowledgeGraph as an export target, so the link is not explained there. PyHealth keeps components self-contained so that changing one for an experiment does not silently change another. Either confirm the sharing and say in the description why the hierarchy's tests depend on GraphProcessor, or keep these tests self-contained, for example by checking the parent_to_child and child_to_parent triples directly.

Comment thread docs/api/medcode.rst

# Map a whole diagnoses table, e.g. MIMIC-IV hosp/diagnoses_icd; a
# LazyFrame stays lazy, so this can sit inside a streaming pipeline
diagnoses = pl.scan_csv("~/data/mimiciv/3.1/hosp/diagnoses_icd.csv.gz",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[low] scan_csv example may fail on a gzipped file

The example calls pl.scan_csv on diagnoses_icd.csv.gz. Polars has historically rejected lazy scans of compressed CSV and asked for read_csv instead. The example script reads the same file with pl.read_csv. Verify the snippet, or document decompressing the file first.

cache = Path(args.cache).expanduser()
graph_dir = cache / f"hierarchy_min_d{BASE_DEPTH}"
mapper_path = cache / "icd_to_snomed.parquet"
if not args.rebuild and graph_dir.is_dir() and mapper_path.is_file():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[low] Cache is not keyed by Athena release or path

load_or_build reuses any cached graph under --cache without checking which Athena folder or release built it. main prints the current release just before this, so a stale graph from another release would be used silently. Store the release string or the source path in the cache and compare on load, or name the cache folder by release.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Review

suggestion: approve
decision: approve

Thank you for this contribution. It adds an OMOP/Athena vocabulary layer to pyhealth.medcode: a lazy reader (OMOPVocabulary), the depth-limited SNOMED condition hierarchy (OMOPConditionHierarchy), and the ICD-9/10-CM to standard concept mapper (ICDToSNOMEDMapper). It looks ready to merge.

  • Correctness and scope: The code matches the description. It adds only new modules, plus registration exports in pyhealth/medcode/__init__.py. I didn't find anything unrelated to the description.
  • Component links: The link to pyhealth.graph.KnowledgeGraph (a lazy import in to_knowledge_graph) is named in the description. The docs and tests also use GraphProcessor, which is a shared processor, so it needs no extra justification.
  • Sources: The paper and Athena are cited in the docs and docstrings.
  • Tests and coverage: The 43 tests on a synthetic Athena bundle cover both depth measures, restrict() matching a direct build, orphan rescue, ICD normalisation, lazy frames and save/load. The new files are at 97–100% coverage, against the 80% bar. The whole repository is at 89.3%, against the 60% bar.

Optional suggestions:

  • The example's cache isn't keyed by Athena release. You could store the vocabulary version next to the cache and rebuild when it changes.
  • The new to_triples doctest is skipped. You could build the small hierarchy in the docstring so it runs.

Reviewed commit 9c7cc5f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: automatic The PR will go through automatic review process.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants