Skip to content

Add OMOP vocabulary reader + KEEP condition hierarchy + ICD mapper - #2

Merged
rishabshah merged 2 commits into
keepfrom
rishab/omop
Nov 18, 2026
Merged

rishabshah merged 2 commits into
keepfrom
rishab/omop

Conversation

@rishabshah

@rishabshah rishabshah commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • First piece of our KEEP reproduction (Elhussein et al., CHIL 2025)
  • It reads the OMOP vocabularies (local Athena download) & builds the 2 things the rest of the pipeline (patient builder, roll-up, co-occurrence, knowledge graph adapter) needs
    -- SNOMED "Disease" hierarchy KEEP embeds
    -- mapping from MIMIC's ICD codes onto it

Changes / Additions

  • pyhealth/medcode/omop/ (new package; from pyhealth.medcode import ... works as before)
    • vocabulary.py: OMOPVocabulary reads CONCEPT, CONCEPT_RELATIONSHIP, and CONCEPT_ANCESTOR from an Athena folder (tab-separated, multi-GB; read lazily with polars). OMOPVocabularyFormat overrides the file format for non-Athena sources.
    • condition_hierarchy.py: OMOPConditionHierarchy, standard SNOMED Condition concepts within N levels of "Disease" (4274025). Default is 5 levels by shortest path, as in KEEP §A.1.1; depth_measure="max" is an option. Every "Is a" parent is kept. Concepts whose only parents are filtered out are attached to their nearest kept ancestor and flagged rescued (our own policy, not specified by KEEP; connect_orphans=False turns it off).
      • retained_ancestors() for roll-up, restrict() for depth ablations without re-reading Athena, save() / load().
      • to_triples(direction=...) and to_knowledge_graph(direction=...) export to pyhealth.graph.KnowledgeGraph with string concept ids; rescue edges get their own relation (rescued_is_a / rescued_subsumes). to_knowledge_graph() defaults to parent → child so GraphProcessor reaches a code's ancestors.
    • icd_mapper.py: ICDToSNOMEDMapper, ICD-9-CM / ICD-10-CM to standard concepts through valid "Maps to" links. Takes MIMIC's dot-free codes. map(), map_frame() (whole diagnoses table), coverage() (counts only); the last two also accept lazy polars tables.
  • pyhealth/medcode/__init__.py: exports the new classes.
  • docs/api/medcode.rst: new "OMOP Vocabularies (Athena)" section, with a runnable KnowledgeGraph / GraphProcessor example.
  • Tests (new): test_omop_vocabulary.py, test_omop_condition_hierarchy.py, test_omop_icd_mapper.py, sharing a synthetic Athena bundle in tests/core/omop_test_utils.py.
  • examples/omop_keep_condition_graph_check.py (new): builds and caches the graph, prints the depth ablation and MIMIC-IV coverage (counts only); its docstring shows the task and processor entry points.
  • .gitignore: ignores Athena files and mimiciv/.

Testing and validation

  • 43 tests in a few seconds, no real data. Covers the depth cutoff, shortest vs longest depth, multi-parent concepts, orphan rescue, deprecated rows, dotted vs dot-free codes, one-to-many mappings, comma-separated files, lazy tables, and that restrict() matches a direct build.
  • The 2 GraphProcessor tests need torch-geometric and are skipped without it. With it: from a child seed, the parent → child graph reaches the ancestors; child → parent returns only the seed. PyHealth's existing KnowledgeGraph / GraphProcessor tests still pass.
  • Real run on Athena v5.0 29-AUG-26 and MIMIC-IV 3.1, via the example script. Graph built in 26s, mapping in 15s.
Depth limit (shortest path) 3 4 5 6
Concepts 20,765 44,817 69,446 84,173
Edges 43,658 100,610 156,745 189,602
  • At depth 5, 69,446 concepts, in line with the ~68k another public KEEP reproduction reports.
  • MIMIC-IV diagnoses (6.36M rows): every code is found in Athena; 99.8% of ICD-9 and 99.0% of ICD-10 codes map; 72.5% of rows land under "Disease". The rest map to symptoms, status codes and external causes, which KEEP leaves out.

Notes

  • Athena vocabularies are licensed and are not committed; tests use synthetic data only.
  • Which ancestors a deep code rolls up to is left to the roll-up PR; this PR provides retained_ancestors() for it.

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
@rishabshah rishabshah changed the title Add OMOP vocabulary reader, KEEP condition hierarchy and ICD mapper Add OMOP vocabulary reader + KEEP condition hierarchy + ICD mapper Nov 12, 2026
Comment thread pyhealth/medcode/omop.py Resolved
Comment thread pyhealth/medcode/omop.py Resolved
Comment thread pyhealth/medcode/omop.py Resolved
Comment thread tests/core/test_omop_vocabulary.py Resolved
Comment thread pyhealth/medcode/omop.py Resolved
Comment thread docs/api/medcode.rst Resolved
Comment thread pyhealth/medcode/omop.py Resolved
Comment thread pyhealth/medcode/omop.py Resolved
- 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

@lee-leonardo lee-leonardo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

+🐯
Trejo Chefs Kiss

@rishabshah
rishabshah merged commit 2118a97 into keep Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants