Skip to content

Build KEEP co-occurrence from OMOP and MIMIC histories - #6

Merged
lee-leonardo merged 10 commits into
rishabshah:keepfrom
lee-leonardo:task/llee_keep_cooccurrence
Nov 21, 2026
Merged

lee-leonardo merged 10 commits into
rishabshah:keepfrom
lee-leonardo:task/llee_keep_cooccurrence

Conversation

@lee-leonardo

@lee-leonardo lee-leonardo commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Build a sparse, symmetric matrix of patient co-occurrences over the retained KEEP concept vocabulary. A concept is present for a patient after two distinct diagnoses map or roll up to it. The diagonal records patient prevalence.
  • Share the counting workflow across raw OMOP patients and mapped MIMIC histories. Add opt-in MIMIC-III and MIMIC-IV configs that preserve diagnosis identity, and expose the cohort-level builder as a Python callable.
  • Add a runnable synthetic OMOP example and document both input paths in the KEEP Tasks API.

This draft targets keep and depends on the OMOP vocabulary work in #2 and the ancestor roll-up work in #4.

Validation

  • 187 focused PyHealth tests and four doctests passed.
  • Ten synthetic KEEP ETL tests passed against a temporary copy of the updated harness, including exact OMOP/MIMIC-III and OMOP/MIMIC-IV matrix parity and a check that counting does not remap MIMIC diagnoses.
  • The OMOP example ran, the KEEP Tasks API page rendered in Sphinx, and the PyHealth PR contribution checks passed for 44ac3b6.

Clinical-data validation and runtime benchmarks are pending.

rishabshah and others added 2 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
@lee-leonardo
lee-leonardo force-pushed the task/llee_keep_cooccurrence branch from 44ac3b6 to 3ee408a Compare November 16, 2026 20:41
Maps observed concept codes to retained SNOMED ancestors within a depth
limit of the "Disease" root (KEEP, Elhussein et al., CHIL 2025) and
projects the successful paths into one PyG Data graph per sample with
edge_index, edge_type, node_ids, x, seed_mask, target_mask and an
edge-aligned closure_only mask. Two entry points build the same internal
index: a generic constructor over pyhealth.graph.KnowledgeGraph (hierarchy
relation supplied parent -> child, optional authoritative root depths and
closure pairs) and from_omop, which derives the uncut condition subtree,
closure-derived root depths and the all-domain classification universe
from the OMOPVocabulary reader. pyhealth/medcode/omop.py is unchanged.

The depth cutoff is target eligibility over the uncut subtree, so deep
sources and intermediates stay traversable; the target policy
(first_retained / all_retained) is an explicit caller choice; rollup_codes
returns one typed result per input occurrence so tasks keep their event
IDs and count converging targets once; and path_source="closure" adds
separation-one CONCEPT_ANCESTOR pairs, flagged closure_only where no
direct "Is a" row supports them. Orphan-rescue shortcuts are deferred and
stay available through the reader's hierarchy export.

Tests port the standalone behavioural reference fixtures to the generic
path (exact edges and masks, both policies, root inclusion, supplied
depths that disagree with graph distances, invalid DAGs, PyG batching and
disk round-trip) and add a synthetic Athena-discrepancy suite reproducing
the recorded 1075201/140708 signature: direct mode reports no_target
where closure mode returns the target and flags the stitched edge, with
parity checks against ConditionHierarchy.retained_ancestors and ICD
one-to-many event mapping. Adds an RST page with runnable examples,
cross-linked from the medcode OMOP section and the processors toctree.
The OMOP factory now builds a pyhealth.graph.KnowledgeGraph over the
uncut Disease subtree with explicit entity ids, so isolated concepts
(concepts whose incoming edges were filtered out) stay addressable and
even an edge-free, root-only subtree supports identity mappings;
KnowledgeGraph accordingly accepts an empty triple list when explicit
entity and relation mappings are provided. Both entry points emit
Data.node_ids as integer graph entity ids decoded through id2entity.

process() now projects node features: the generic path uses
KnowledgeGraph.node_features, and from_omop accepts node_features_by_code
keyed by string OMOP concept id, aligns the vectors to the graph, and
zero-fills entities without vectors (untrained deep sources and traversal
intermediates included). Empty samples keep the feature width so empty
and nonempty samples batch together. The cache fingerprint (schema v2)
covers the entity-id mapping and the feature contents.

Adds tests for feature projection and alignment, zero filling,
empty/nonempty batching, pickle round-tripping, identity projection on an
edge-free ontology, and the ICD-to-embedding path, and documents a
pretrained-embedding section with a runnable example. Line lengths on
the new code are held to the 88-character contributor limit.
@lee-leonardo
lee-leonardo force-pushed the task/llee_keep_cooccurrence branch from 8fa97df to 46261db Compare November 17, 2026 19:43
@lee-leonardo
lee-leonardo changed the base branch from rishab/omop to keep November 18, 2026 01:30
@rishabshah
rishabshah marked this pull request as ready for review November 18, 2026 01:34
@lee-leonardo
lee-leonardo merged commit a62800f into rishabshah:keep Nov 21, 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