Skip to content

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

Open
rishabshah wants to merge 5 commits into
sunlabuiuc:masterfrom
rishabshah:rishab/omop-hierarchy
Open

rishabshah wants to merge 5 commits into
sunlabuiuc:masterfrom
rishabshah:rishab/omop-hierarchy

Conversation

@rishabshah

@rishabshah rishabshah commented Nov 29, 2026 •

Copy link
Copy Markdown

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. .gitignore excludes local Athena and MIMIC-IV downloads. The tests build a small synthetic Athena bundle.

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

Copy link
Copy Markdown

Test Coverage

Total: 88.29% (57333 / 64940 lines)

Modified file Coverage Lines
pyhealth/medcode/__init__.py 100.00% 16 / 16
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

Workflow run · 78448c8

@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.

Reviewed commit 78448c8

)
subtree = _collect(levels.join(conditions, on="concept_id", how="inner"))
# Some bundles omit the root's self-row.
if root_concept_id not in subtree["concept_id"]:

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] Untested root self-row fallback that Athena should never need

_rooted_condition_nodes adds the root by hand when CONCEPT_ANCESTOR has no root self-row. Athena's CONCEPT_ANCESTOR includes a 0-level self row for every standard concept, so with real files this branch should be unreachable. The synthetic fixture also always has the self row, so the branch has no test. Either remove the fallback and rely on the vocabulary, or add a test that drops the self row from the fixture.

"Maps to"), ``mapped``; with a hierarchy, also ``in_hierarchy``
(at least one target under the root) and ``outside_hierarchy``.
"""
codes = frame.select(

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] coverage() counts null ICD codes as unknown codes

coverage() builds the distinct (version, code) set without dropping nulls. A null code is therefore counted in 'codes' and in 'unknown', which inflates the unknown count on tables with missing codes. Filter out rows whose code or version is null before counting, and add a test with a null code.

"""

def __init__(self, table: pl.DataFrame):
self.table = table

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] Public constructor stores codes without normalizing them

ICDToSNOMEDMapper.init takes a raw table and stores icd_code as given. map() normalizes the query, so a table that still has dotted codes (for example 'I21.4') never matches, and nothing reports it. Normalize icd_code in the constructor with the same rule icd_mapper uses, or validate that codes are dot-free and raise.

@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: 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

This branch has not been deployed

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

Labels

status: manual Pending maintainer's review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants