Repository navigation
Prepare KEEP clinical histories and co-occurrence counts - #1706
lee-leonardo wants to merge 5 commits into
Conversation
Test CoverageTotal: 89.41% (69558 / 77800 lines)
|
6d2ebae to
234699b
Compare
* Add OMOP vocabulary reader, KEEP condition hierarchy and ICD mapper 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 * Address review: split OMOP package, KnowledgeGraph export, format config - 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 * Split OMOP hierarchy loading into reusable vocabulary helpers Carries the vocabulary.py part of 24341db ("Reduce OMOP ancestor loading for KEEP roll-up") from #1661. The KEEP roll-up changes, their tests and the notebook from that commit stay with #1706. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JrebVyPi1jFyjkAiLbLSC1 * Document OMOP mapper and hierarchy usage examples * Modernize OMOP annotations for PyHealth's Ruff rules Takes the OMOP module annotations from 42556b0 in #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 #1706's copy line for line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JrebVyPi1jFyjkAiLbLSC1 * Drop the .gitignore change 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 --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: lee-leonardo <hi@leeleonardo.dev>
Copy the processor, factory, API docs and focused tests from task/llee_depth_ancestor_rollup (6dfa49b..33c2cb6), with the reduced ancestor queries from codex/keep-rollup-scale (24341db). Retain explicit target policy, root eligibility, direct/closure traversal, provenance and graph feature projection. Depend on the separate OMOP prerequisite queries. Exclude the KnowledgeGraph behavior change and explicitly reject root-only and closure-only edgeless bases; preserve isolated nodes on nonempty graphs. Validation: 101 focused tests passed.
Copy task, MIMIC configs, minimal reader examples and correctness tests from task/llee_keep_cooccurrence (8e21a8a, a4b7bee, 46261db). Retain the occurrence-unit options and focused API tests from task/ablations_occurrence_rule (5c0577d), excluding its research study and notebooks. Preserve complete retained axes, prevalence diagonals, diagnosis deduplication, full MIMIC-IV diagnosis identity and cache fingerprinting. Reuse the C-0018 generic counter. Validation: 40 clinical workflow tests passed, including real synthetic reader/cache paths.
Extend the existing sunlabuiuc#1666 synthetic example with a raw-result adapter and use it from the synthetic MIMIC history examples. Derive positive off-diagonal CPU NumPy triplets without mutating raw counts, retain count-one pairs and complete axes, align synthetic reference vectors by concept ID, and export every retained concept in matrix order. Skip fit_keep when no training pairs exist. Use the depth-five clinical baseline while preserving the separate ICD/two-admission preprocessing example. Validation: 254 tests passed, two CUDA-only cases skipped, and 27 subtests passed across the assembled workflow; processor/task coverage is 96%/93%; 14 public doctests passed. Both clinical examples and the original synthetic trainer example execute successfully; contribution rules pass with the CI Ruff version.
CI installs Ruff 0.17.6 through ruff~=0.15. That version reports the E402 suppression as unused (RUF100), whereas the older 0.15.22 local check required it. Remove the suppression and verify contribution rules with the actual CI version. No runtime behavior changes.
8784a00 to
e6b68bc
Compare
|
Thanks for splitting this out of #1661 and rebasing it onto the merged #1707 and #1709. I've labeled it I'll request the AI review once the build passes, so the maintainers have its notes as well. |
|
/review |
AI Reviewsuggestion: not available This PR is too large for AI review (over 300 files or 200000 diff characters). The label stays as it is: you can make the PR smaller, for example by clearing notebook outputs or splitting it, and comment |
|
I've opted to split the PR again, #1803 is focused on the processor and the task functionality specific to the ancestor roll-up |
Overview
Prepare clinical diagnosis histories and patient-level co-occurrence inputs for the existing KEEP trainer. The task preserves diagnosis identity, unions converging mapping paths within each diagnosis, and applies diagnosis/admission support thresholds after mapping and roll-up. Synthetic examples reuse
KEEP,KEEPConfig,fit_keepand embedding exports introduced in #1666.This is the clinical-history and counting portion of the original KEEP contribution #1661. The processor and OMOP factory have been extracted into #1803.
The original KEEP contribution includes work by Rishab Shah (
rishabs4), Leo Lee (ll37) and Murali Rachakonda (muralir2). Existing source attribution is preserved. The merged KEEP trainer and original synthetic training example are by Stanislav Sheludko and Kalindu Madduma Hewage.Dependency and review order
Depends on #1803: ancestor roll-up processor and OMOP factory. Review and merge that PR first.
The processor PR is based on upstream master
209dc988and contains the processor/factory implementation, their documentation and the latest regression tests. This branch remains intact at22ad43c2so its working history/counting/training pipeline is preserved. Its current diff against master therefore still includes the processor dependency.After #1803 merges, rebase this branch onto master, remove the superseded processor/factory changes, resolve documentation/export/test conflicts and rerun the history/counting/trainer checks. The remaining review will cover clinical histories, support counting, MIMIC configs and runnable synthetic examples.
The generic counter #1707 and OMOP vocabulary/hierarchy/mapper #1709 are already merged. Their implementations come from master.
The counter follow-up #1787 is separate. This contribution supplies unique patient sets to the merged counter and keeps sparse COO counts. Its callers passed an isolated compatibility check against #1787's counter at
30bcd8bb.The paper and inspected released code leave occurrence counting, roll-up boundaries, hierarchy construction, and matrix/vocabulary construction unresolved. #1802 tracks the study of preprocessing code, reference artifacts and author clarification needed to resolve these details and verify the choices used here.
Intended scope after the dependency merges
KeepConditionHistoryTask, opt-in MIMIC-III/IV reader configs and cohort counting. Preserve exact-copy deduplication, conflicting-copy rejection, the full MIMIC-IV diagnosis key(hadm_id, seq_num, icd_code, icd_version), cache versioning and effective-mapping fingerprints.examples/keep_synthetic.pywith a training adapter, reused by the clinical examples through--train. It derives positive off-diagonal CPU NumPy triplets, preserves count-one pairs and both mirrored triangles, aligns reference vectors by concept ID, and exports the complete vocabulary in matrix order. It skips training when no eligible pairs exist.The clinical examples use
all_retained, depth five, root excluded from targets and two distinct diagnoses per target. The existing ICD-10/two-admission preprocessing example retains its separate profile. Example training runs on CPU with explicit synthetic reference vectors.The count matrix remains sparse through counting and preparation of the training triplets. NumPy is used for the trainer's one-dimensional input arrays, the
V × embedding_dimreference array and embedding exports; it does not accumulate a denseV × Vcount matrix. The tiny example report does create a dense copy for printing, without changing the returned sparse tensor or the training input. The example adapter currently expects sparse COO counts; selecting #1787's optional dense layout would require a separate adapter change.Processor construction and graph behavior are covered by #1803. Research notebooks and ablation helpers remain in keep-validation; the trainer implementation is unchanged.
Validation
The results below cover the unchanged combined branch, including its processor/factory dependency. A fresh task-focused validation will follow the rebase after #1803 merges.
Source and example validation was performed at
e6b68bce, after the 2026-12-03 rebase onto the merged OMOP and co-occurrence contributions. The test-only follow-up22ad43c2reran the same selected suites; the totals below include existing dependency and trainer tests:30bcd8bb, loaded in an isolated process without changing this branch's source.--traincompletes two CPU epochs with finite loss and all 12 retained concepts exported in matrix order. The MIMIC-III prevalence-only fixture correctly skips training. The original synthetic GloVe/cosine/L2 example also executes successfully.git diff --checkpasses. The full GitHub Python test workflow is separate from the local results above.These checks establish synthetic correctness and API compatibility. They are not throughput, memory-scaling or clinical-scale performance benchmarks. Historical coverage reports in the discussion are tied to their recorded commit; they should be read with that provenance.
Supporting preprocessing studies
The accompanying research notebooks in keep-validation examine how ancestor selection and occurrence support change the histories and patient sets produced by this pipeline.
Two relevant synthetic findings are:
first_retained, which can retain several parents. Study and controls.These studies motivate explicit roll-up policies, diagnosis identity and support units. They use the earlier KEEP development revision
263f709eand establish synthetic preprocessing sensitivity; they are separate from validation of this extracted branch and do not demonstrate better embeddings, clinical predictive performance or published-result replication. The notebooks and research helpers remain in the external repository. Execution provenance and verification.