Skip to content

Add MIMIC-III ICD-9 category classification task (KeyClass, Gao et al. MLHC 2022) - #1663

Merged
solarsys merged 3 commits into
sunlabuiuc:masterfrom
hekilij:keyclass-icd9-task
Nov 28, 2026
Merged

solarsys merged 3 commits into
sunlabuiuc:masterfrom
hekilij:keyclass-icd9-task

Conversation

@estherhwang0103

@estherhwang0103 estherhwang0103 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Contributors: Hekili Jordan (hekilij2), Nishanth Manjunath (nm89), Esther Hwang (inseoh2)

Contribution Type: Standalone Task (for the existing MIMIC3Dataset)

Paper: Chufan Gao, Mononito Goswami, Jieshi Chen, Artur Dubrawski.
"Classifying Unstructured Clinical Notes via Automatic Weak Supervision." MLHC 2022.
https://proceedings.mlr.press/v182/gao22a.html

Description:
Adds MIMIC3ICD9CategoryClassification, the ground-truth label generation component of the KeyClass clinical experiment: it turns MIMIC-III discharge summaries into a 19-label multilabel problem over the top-level ICD-9 diagnosis categories (following FasTag).
Each admission with a discharge note and at least one diagnosis mapped to a supported category becomes one sample (note text in, 19-dim multi-hot categories out). I
Includes icd9_to_category mapping function and a separate transformers-based ablation example exploring the effects of note length and note source.

Files to Review:

  • pyhealth/tasks/mimic3_icd9_category_classification.py - the task + icd9_to_category
  • tests/core/test_mimic3_icd9_category_classification.py - tests on synthetic data (no MIMIC)
  • examples/mimic3_icd9_category_classification_transformers.py - ablation example
  • docs/api/tasks/pyhealth.tasks.MIMIC3ICD9CategoryClassification.rst - API docs
  • docs/api/tasks.rst - toctree entry
  • pyhealth/tasks/__init__.py - exports

Testing: python -m unittest tests.core.test_mimic3_icd9_category_classification runs 21 tests on synthetic data in ~2ms; 10 module doctests pass; ruff clean.

Data note: The tests and the example's --synthetic path use synthetic MIMIC-III-shaped data, so no credentialed access is needed to review them. Real reproduction uses the full MIMIC-III v1.4.

Results (synthetic notes): --synthetic --epochs 3 --seeds 0 1 2, Colab T4 GPU, Bio_ClinicalBERT, 80 patients and 122 admissions. The notes are synthetic, so this only shows that the pipeline trains end to end; it says nothing about the real task.

configuration pr_auc_micro roc_auc_micro f1_micro
discharge_64_words 0.181 ± 0.020 0.755 ± 0.012 0.221 ± 0.015
discharge_128_words 0.222 ± 0.064 0.779 ± 0.050 0.225 ± 0.038
discharge_256_words 0.201 ± 0.035 0.796 ± 0.047 0.276 ± 0.042
discharge_physician_256_words 0.187 ± 0.037 0.780 ± 0.047 0.264 ± 0.029

Mean ± standard deviation over 3 seeds. The test split has only about 18 admissions, and the differences between settings are no larger than the spread across seeds, so no setting is better than another here. Real MIMIC-III notes have not been run.

@github-actions

Copy link
Copy Markdown

Test Coverage

Total: 87.44% (52377 / 59900 lines)

Modified file Coverage Lines
pyhealth/tasks/__init__.py 98.35% 119 / 121
pyhealth/tasks/mimic3_icd9_category_classification.py 100.00% 70 / 70

Workflow run · f9560f6

@solarsys solarsys added the status: automatic The PR will go through automatic review process. label Nov 28, 2026
@solarsys

Copy link
Copy Markdown
Collaborator

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed commit f9560f6

)


def icd9_to_category(code: Any) -> str | None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[medium] icd9_to_category duplicates the existing ICD9CHAPTER grouper

PyHealth already provides the 19-way ICD-9 chapter grouping as CrossMap.load("ICD9CM", "ICD9CHAPTER") (pyhealth/medcode/codes/icd_groupers.py:101, mapped in pyhealth/medcode/icd_mappings.py:51). icd9_to_category reimplements the same grouping with hard-coded ranges. The description does not say why the existing grouper cannot be used. Fix: build the category labels on ICD9CHAPTER, or state in the description and docstring what the existing grouper gets wrong for the paper's Table 4 categories and why the ranges here differ.

samples = dataset.set_task(MIMIC3ICD9CategoryClassification())

Codes in the ICD-9 symptoms range (780-799) belong to none of the 19
categories and give no label. The category ranges are the standard ICD-9

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[medium] Docstring says the ranges are the standard ICD-9 chapters, but the code differs

The module docstring says "The category ranges are the standard ICD-9 chapters". The table in ICD9_CATEGORY_RANGES (lines 67-85) does not match the standard layout. It splits 320-389 into "Nervous system" (320-359) and "Sense organs" (360-389), where the standard chapter is one group, and it omits the 780-799 symptoms chapter. Fix: correct the claim, and name the source of the split (for example FasTag's Table, with a link) if the split is intentional.

raise ValueError("min_words must be non-negative.")
if max_words is not None and max_words <= 0:
raise ValueError("max_words must be positive or None.")
self.note_categories = tuple(c.strip().lower() for c in note_categories)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[low] A bare string for note_categories silently matches no notes

note_categories is typed as a Sequence, so a str is accepted. note_categories="Discharge summary" becomes a tuple of single characters, and every admission is then skipped without an error. Fix: reject a str in the validation at lines 208-209, or wrap it as a one-element tuple.

)


class FakePatient:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[low] Tests never run the task through MIMIC3Dataset.set_task

All 21 tests call the task on FakePatient objects, which stand in for Patient. Nothing checks the real event attributes or the set_task path on a MIMIC3Dataset built from a small folder. Comparable tasks such as test_clinvar and test_lndb_texture do run set_task on synthetic data. Fix: add one test that writes a few synthetic CSV tables and calls dataset.set_task on the task.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI Review

suggestion: approve
decision: approve

Thank you for this contribution. It adds a MIMIC-III task that maps discharge summaries to the 19 top-level ICD-9 categories from Gao et al. (MLHC 2022). It also adds the icd9_to_category helper, API docs, an ablation example and 21 synthetic-data tests. It looks ready to merge.

The task is self-contained. It builds only on BaseTask and the existing processors, and the changes to pyhealth/tasks/__init__.py are registration only. The task is also distinct from MIMIC3ICD9Coding, because it predicts categories and not raw codes. Coverage meets the bar. The new task file is at 100%, pyhealth/tasks/__init__.py is at 98.4%, and the whole repository is at 87.4%.

Optional suggestions:

  • The module docstring calls the category ranges "the standard ICD-9 chapters". The table splits 320-389 into Nervous system and Sense organs, and it leaves out 780-799. It would be clearer to say the ranges are chapter-based and follow the paper's 19 category names, with those two differences noted.
  • A bare string passed as note_categories, such as "Discharge summary", is iterated character by character and silently matches nothing. You could wrap a str into a tuple, or raise an error.
  • You could add one test that runs set_task on a small MIMIC3Dataset built from the example's synthetic generator. The tests currently use fake patient objects only.
  • The example's results table comes from a Colab run, and the docstring already says the numbers are only a pipeline check. That is fine as it stands.

Reviewed commit f9560f6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: automatic The PR will go through automatic review process.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants