Skip to content
Closed
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
5 changes: 5 additions & 0 deletions docs/api/models/pyhealth.models.RNN.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ The separate callable RNNLayer and the complete RNN model.
:show-inheritance:

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

.. autoclass:: pyhealth.models.MultimodalRNN
:members:
:undoc-members:
:show-inheritance:
117 changes: 0 additions & 117 deletions examples/drug_recommendation/drug_recommendation_eICU_transformer.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
Drug Recommendation on eICU with Transformer

This example demonstrates how to use the modernized eICUDataset with the
DrugRecommendationEICU task class for drug recommendation using a Transformer model.

Features:
- Uses the new BaseDataset-based eICUDataset with YAML configuration
- Uses the new DrugRecommendationEICU BaseTask class
- Demonstrates the standardized PyHealth workflow
"""

import tempfile

from pyhealth.datasets import eICUDataset
from pyhealth.datasets import split_by_patient, get_dataloader
from pyhealth.models import Transformer
from pyhealth.tasks import DrugRecommendationEICU
from pyhealth.trainer import Trainer


if __name__ == "__main__":
# Use tempfile to automate cleanup
cache_dir = tempfile.TemporaryDirectory()

# STEP 1: Load dataset
# Replace with your eICU dataset path
base_dataset = eICUDataset(
root="/srv/local/data/physionet.org/files/eicu-crd/2.0",
tables=["diagnosis", "medication", "physicalexam"],
cache_dir=cache_dir.name
)
base_dataset.stats()

# STEP 2: Set task using the new DrugRecommendationEICU class
task = DrugRecommendationEICU()
sample_dataset = base_dataset.set_task(task)
sample_dataset.stats()

# STEP 3: Split and create dataloaders
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 4: Define model
model = Transformer(
dataset=sample_dataset,
feature_keys=["conditions", "procedures", "drugs_hist"],
label_key="drugs",
mode="multilabel",
)

# STEP 5: Train
trainer = Trainer(model=model)
trainer.train(
train_dataloader=train_dataloader,
val_dataloader=val_dataloader,
epochs=50,
monitor="pr_auc_samples",
)

# STEP 6: Evaluate
print(trainer.evaluate(test_dataloader))

# Cleanup
sample_dataset.close()





88 changes: 88 additions & 0 deletions examples/length_of_stay/length_of_stay_eicu_rnn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""
Length of Stay Prediction on eICU with RNN

This example demonstrates how to use the modernized eICUDataset with the
LengthOfStayPredictioneICU task class for predicting ICU length of stay
using an RNN model.

Length of stay is categorized into 10 classes:
- 0: < 1 day
- 1: 1 day
- 2: 2 days
- 3: 3 days
- 4: 4 days
- 5: 5 days
- 6: 6 days
- 7: 7 days
- 8: 1-2 weeks
- 9: > 2 weeks

Features:
- Uses the new BaseDataset-based eICUDataset with YAML configuration
- Uses the LengthOfStayPredictioneICU BaseTask class
- Demonstrates the standardized PyHealth workflow
"""

import tempfile

from pyhealth.datasets import eICUDataset
from pyhealth.datasets import split_by_patient, get_dataloader
from pyhealth.models import RNN
from pyhealth.tasks import LengthOfStayPredictioneICU
from pyhealth.trainer import Trainer


if __name__ == "__main__":
# Use tempfile to automate cleanup
cache_dir = tempfile.TemporaryDirectory()

# STEP 1: Load dataset
# Replace with your eICU dataset path
base_dataset = eICUDataset(
root="/srv/local/data/physionet.org/files/eicu-crd/2.0",
tables=["diagnosis", "medication", "physicalexam"],
cache_dir=cache_dir.name
dev=True,
)
base_dataset.stats()

# STEP 2: Set task using LengthOfStayPredictioneICU
task = LengthOfStayPredictioneICU()
sample_dataset = base_dataset.set_task(task)
sample_dataset.stats()

# STEP 3: Split and create dataloaders
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 4: Define model
model = RNN(
dataset=sample_dataset,
feature_keys=["conditions", "procedures", "drugs"],
label_key="los",
mode="multiclass",
)

# STEP 5: Train
trainer = Trainer(model=model)
trainer.train(
train_dataloader=train_dataloader,
val_dataloader=val_dataloader,
epochs=20,
monitor="accuracy",
)

# STEP 6: Evaluate
print(trainer.evaluate(test_dataloader))

# Cleanup
sample_dataset.close()





Loading
Loading