-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from sunlabuiuc/develop
update pyhealth live 05, add deepr model, start unittest (#67)
- Loading branch information
Showing
18 changed files
with
1,199 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
pyhealth.models.Deepr | ||
=================================== | ||
|
||
The separate callable DeeprLayer and the complete Deepr model. | ||
|
||
.. autoclass:: pyhealth.models.DeeprLayer | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
.. autoclass:: pyhealth.models.Deepr | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
from pyhealth.datasets import eICUDataset | ||
from pyhealth.datasets import split_by_patient, get_dataloader | ||
from pyhealth.models import Transformer | ||
from pyhealth.tasks import drug_recommendation_eicu_fn | ||
from pyhealth.trainer import Trainer | ||
|
||
# STEP 1: load data | ||
base_dataset = eICUDataset( | ||
root="/srv/local/data/physionet.org/files/eicu-crd/2.0", | ||
tables=["diagnosis", "medication", "physicalExam"], | ||
dev=True, | ||
) | ||
base_dataset.stat() | ||
|
||
# STEP 2: set task | ||
|
||
from pyhealth.data import Visit, Patient | ||
|
||
|
||
def drug_recommendation_eicu_fn(patient: Patient): | ||
"""Processes a single patient for the drug recommendation task. | ||
Drug recommendation aims at recommending a set of drugs given the patient health | ||
history (e.g., conditions and procedures). | ||
Args: | ||
patient: a Patient object | ||
Returns: | ||
samples: a list of samples, each sample is a dict with patient_id, visit_id, | ||
and other task-specific attributes as key | ||
Examples: | ||
>>> from pyhealth.datasets import eICUDataset | ||
>>> eicu_base = eICUDataset( | ||
... root="/srv/local/data/physionet.org/files/eicu-crd/2.0", | ||
... tables=["diagnosis", "medication"], | ||
... code_mapping={}, | ||
... dev=True | ||
... ) | ||
>>> from pyhealth.tasks import drug_recommendation_eicu_fn | ||
>>> eicu_sample = eicu_base.set_task(drug_recommendation_eicu_fn) | ||
>>> eicu_sample.samples[0] | ||
[{'visit_id': '130744', 'patient_id': '103', 'conditions': [['42', '109', '98', '663', '58', '51']], 'procedures': [['1']], 'label': [['2', '3', '4']]}] | ||
""" | ||
samples = [] | ||
for i in range(len(patient)): | ||
visit: Visit = patient[i] | ||
conditions = visit.get_code_list(table="diagnosis") | ||
procedures = visit.get_code_list(table="physicalExam") | ||
drugs = visit.get_code_list(table="medication") | ||
# exclude: visits without condition, procedure, or drug code | ||
if len(conditions) * len(procedures) * len(drugs) == 0: | ||
continue | ||
# TODO: should also exclude visit with age < 18 | ||
samples.append( | ||
{ | ||
"visit_id": visit.visit_id, | ||
"patient_id": patient.patient_id, | ||
"conditions": conditions, | ||
"procedures": procedures, | ||
"drugs": drugs, | ||
"drugs_all": drugs, | ||
} | ||
) | ||
# exclude: patients with less than 2 visit | ||
if len(samples) < 2: | ||
return [] | ||
# add history | ||
samples[0]["conditions"] = [samples[0]["conditions"]] | ||
samples[0]["procedures"] = [samples[0]["procedures"]] | ||
samples[0]["drugs_all"] = [samples[0]["drugs_all"]] | ||
|
||
for i in range(1, len(samples)): | ||
samples[i]["conditions"] = samples[i - 1]["conditions"] + [ | ||
samples[i]["conditions"] | ||
] | ||
samples[i]["procedures"] = samples[i - 1]["procedures"] + [ | ||
samples[i]["procedures"] | ||
] | ||
samples[i]["drugs_all"] = samples[i - 1]["drugs_all"] + [ | ||
samples[i]["drugs_all"] | ||
] | ||
|
||
return samples | ||
|
||
|
||
sample_dataset = base_dataset.set_task(drug_recommendation_eicu_fn) | ||
sample_dataset.stat() | ||
|
||
train_dataset, val_dataset, test_dataset = split_by_patient( | ||
sample_dataset, [0.8, 0.1, 0.1] | ||
) | ||
train_dataloader = get_dataloader(train_dataset, batch_size=32, shuffle=True) | ||
val_dataloader = get_dataloader(val_dataset, batch_size=32, shuffle=False) | ||
test_dataloader = get_dataloader(test_dataset, batch_size=32, shuffle=False) | ||
|
||
# STEP 3: define model | ||
model = Transformer( | ||
dataset=sample_dataset, | ||
feature_keys=["conditions", "procedures"], | ||
label_key="drugs", | ||
mode="multilabel", | ||
) | ||
|
||
# STEP 4: define trainer | ||
trainer = Trainer(model=model) | ||
trainer.train( | ||
train_dataloader=train_dataloader, | ||
val_dataloader=val_dataloader, | ||
epochs=50, | ||
monitor="pr_auc_samples", | ||
) | ||
|
||
# STEP 5: evaluate | ||
trainer.evaluate(test_dataloader) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.