Repository navigation
Add OMOP Condition Hierarchy (split from PR#1661) - #1709
rishabshah wants to merge 5 commits into
Conversation
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
Test CoverageTotal: 88.29% (57333 / 64940 lines)
|
|
Why this PR has To make it eligible: drop the |
|
/review Generated by Claude Code |
| ) | ||
| 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"]: |
There was a problem hiding this comment.
[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( |
There was a problem hiding this comment.
[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 |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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__.pyonly add exports. - The only link to another component is the lazy
pyhealth.graph.KnowledgeGraphimport into_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
.gitignoreentries 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
ICDToSNOMEDMapperconstructor does not normalize codes. A one-line note in the docstring thaticd_codemust 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
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
rishabs4, rishabs4@illinois.edu)ll37, ll37@illinois.edu)muralir2, muralir2@illinois.edu)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:OMOPVocabularyreads Athena's CONCEPT, CONCEPT_RELATIONSHIP and CONCEPT_ANCESTOR files lazily with polars.OMOPVocabularyFormatsets the separator, quoting, encoding and file names; the defaults match Athena's tab-separated export.OMOPConditionHierarchyholds 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 withrestrict()without re-reading Athena, saved and loaded as parquet, and exported as triples or apyhealth.graph.KnowledgeGraph.ICDToSNOMEDMappermaps 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.
.gitignoreexcludes 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__.pyandpyhealth/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 HEADKnowledgeGraphexport tests skip when torch-geometric is not installed.+SKIP.