Repository navigation
Add OMOP Condition Hierarchy (split from PR#1661) - #1709
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: 89.32% (68104 / 76250 lines)
|
|
Why this PR has To make it eligible: drop the |
|
/review Generated by Claude Code |
There was a problem hiding this comment.
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__.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
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
Dropped the |
|
/review |
|
Thanks, with the |
|
/review |
| from tests.core.omop_test_utils import ROOT, AthenaFixture | ||
|
|
||
| try: | ||
| from pyhealth.processors import GraphProcessor |
There was a problem hiding this comment.
[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.
|
|
||
| # 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", |
There was a problem hiding this comment.
[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(): |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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 into_knowledge_graph) is named in the description. The docs and tests also useGraphProcessor, 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_triplesdoctest is skipped. You could build the small hierarchy in the docstring so it runs.
Reviewed commit 9c7cc5f
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. 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__.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.