Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ API Reference
models/pyhealth.models.LogisticRegressionWithSummaries
models/pyhealth.models.LSTMNextStateBaseline
models/pyhealth.models.MLP
models/pyhealth.models.KeyClass
models/pyhealth.models.RASCMLP
models/pyhealth.models.CNN
models/pyhealth.models.camchex
Expand Down
98 changes: 98 additions & 0 deletions docs/api/models/pyhealth.models.KeyClass.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
pyhealth.models.KeyClass
===================================

KeyClass classifies clinical notes into diagnostic categories without any
human-labeled notes. It builds keyword labeling functions automatically from
short category descriptions, combines their votes into soft weak labels, and
trains a multilabel classifier on frozen document embeddings.

Paper: Chufan Gao, Mononito Goswami, Jieshi Chen and Artur Dubrawski.
`Classifying Unstructured Clinical Notes via Automatic Weak Supervision
<https://arxiv.org/abs/2206.12088v2>`_. MLHC 2022.

Installation
------------

Mining keyword rules needs NLTK and its English stopword list, and the optional
Snorkel label model needs Snorkel. Neither is a PyHealth dependency; install
them with::

pip install "nltk~=3.9.1" "snorkel>=0.10,<0.11"
python -m nltk.downloader stopwords

Snorkel is needed only for ``aggregate_votes(..., method="snorkel")``. Without
these packages, the functions that need them raise an ``ImportError`` that says
what to install.

MIMIC-III data
--------------

:class:`~pyhealth.tasks.mimic3_icd9_category_classification.MIMIC3ICD9CategoryClassification` builds the paper's
MIMIC-III cohort: one sample per admission with its discharge summary
(``"text"``) and its 19-category ICD-9 labels (``"categories"``). KeyClass
trains without those labels; use ``pyhealth.tasks.ICD9_CATEGORIES`` as
``category_ids`` and, to evaluate against them, pass them as 0/1 vectors in
that order under the model's ``gold_key``.

Pipeline
--------

1. Clean notes with :func:`~pyhealth.weak_supervision.clean_note`.
2. Fit a :class:`~pyhealth.weak_supervision.KeywordLabeler` on training notes and
category descriptions, then vote with
:meth:`~pyhealth.weak_supervision.KeywordLabeler.transform`.
3. Turn votes into soft weak labels with
:func:`~pyhealth.weak_supervision.aggregate_votes`, and pick confident notes with
:func:`~pyhealth.weak_supervision.confident_balanced_mask`.
4. Embed notes, for example with
:class:`~pyhealth.weak_supervision.ClinicalTextEncoder`, and train
:class:`~pyhealth.models.KeyClass` on the embeddings and soft labels.

.. code-block:: python

from pyhealth.models import KeyClass
from pyhealth.weak_supervision import (
ClinicalTextEncoder, KeywordLabeler, aggregate_votes, clean_note,
)

notes = [clean_note(text) for text in raw_notes]
encoder = ClinicalTextEncoder(
"bionlp/bluebert_pubmed_mimic_uncased_L-12_H-768_A-12"
)
labeler = KeywordLabeler(encoder, top_k=300).fit(notes, descriptions)
votes = labeler.transform(notes)
weak_labels, covered = aggregate_votes(votes, len(descriptions))
# Build a sample dataset with "embedding" and "weak_labels" tensor fields, then:
model = KeyClass(dataset, input_dim=768, category_ids=list(descriptions))
model.fit_input_standardization(embeddings)

Model
-----

.. autoclass:: pyhealth.models.KeyClass
:members:
:undoc-members:
:show-inheritance:

Weak supervision
----------------

.. autoclass:: pyhealth.weak_supervision.KeywordLabeler
:members:

.. autoclass:: pyhealth.weak_supervision.LabelingFunction

.. autoclass:: pyhealth.weak_supervision.ClinicalTextEncoder
:members:

.. autofunction:: pyhealth.weak_supervision.clean_note

.. autofunction:: pyhealth.weak_supervision.nltk_english_stopwords

.. autofunction:: pyhealth.weak_supervision.aggregate_votes

.. autofunction:: pyhealth.weak_supervision.confident_balanced_mask

.. autofunction:: pyhealth.weak_supervision.snorkel_memory_gb

.. autofunction:: pyhealth.weak_supervision.vote_diagnostics
Loading
Loading