From 5ac46e4971082a7af6e7a7ce7c9d4ab3b39a6ee7 Mon Sep 17 00:00:00 2001 From: lehendo Date: Tue, 25 Aug 2026 17:50:45 -0500 Subject: [PATCH] Fix broken GAMENet example; label minimal_legacy_*.py as pyhealth 1.1.6-only - drug_recommendation_mimic4_gamenet.py used the dead drug_recommendation_mimic4_fn (set_task() requires a BaseTask instance, not a bare function), old MIMIC4Dataset(root=, tables=) kwargs, a duplicate import, and non-existent .stat()/.info() dataset methods. Rewrite to use DrugRecommendationMIMIC4, ehr_root=/ ehr_tables=, and .stats(); drop the hardcoded device="cuda" (crashes on CPU-only machines) in favor of Trainer's auto-detect default. Verified end-to-end against real MIMIC-IV demo data: dataset loads, task runs, GAMENet trains a real step, evaluation produces real metrics. - minimal_legacy_los.py/mortality.py/drug_rec.py are deliberately pre-2.0-API scripts paired with their 2.0-style siblings for a lines-of-code comparison, but unlike their legacy_ver/ counterparts they didn't say so. Add the same "requires pyhealth==1.1.6" disclaimer their siblings already carry; no behavior change. --- .../loc/minimal_legacy_drug_rec.py | 1 + .../benchmark_perf/loc/minimal_legacy_los.py | 1 + .../loc/minimal_legacy_mortality.py | 1 + .../drug_recommendation_mimic4_gamenet.py | 27 ++++++------------- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/examples/benchmark_perf/loc/minimal_legacy_drug_rec.py b/examples/benchmark_perf/loc/minimal_legacy_drug_rec.py index a805ab07b..a0fdeb7f0 100644 --- a/examples/benchmark_perf/loc/minimal_legacy_drug_rec.py +++ b/examples/benchmark_perf/loc/minimal_legacy_drug_rec.py @@ -1,3 +1,4 @@ +# Requires pyhealth==1.1.6 (legacy 1.x task_fn/Visit API) -- not runnable against pyhealth 2.0 from pyhealth.data import Patient,Visit; from pyhealth.datasets import MIMIC4Dataset def drug_recommendation_mimic4_fn(patient): diff --git a/examples/benchmark_perf/loc/minimal_legacy_los.py b/examples/benchmark_perf/loc/minimal_legacy_los.py index 2bc8eae8e..32bdff7df 100644 --- a/examples/benchmark_perf/loc/minimal_legacy_los.py +++ b/examples/benchmark_perf/loc/minimal_legacy_los.py @@ -1,3 +1,4 @@ +# Requires pyhealth==1.1.6 (legacy 1.x task_fn/Visit API) -- not runnable against pyhealth 2.0 from pyhealth.datasets import MIMIC4Dataset def categorize_los(d): return 0 if d<1 else (d if d<=7 else (8 if d<=14 else 9)) diff --git a/examples/benchmark_perf/loc/minimal_legacy_mortality.py b/examples/benchmark_perf/loc/minimal_legacy_mortality.py index 719959f56..c31eee1c9 100644 --- a/examples/benchmark_perf/loc/minimal_legacy_mortality.py +++ b/examples/benchmark_perf/loc/minimal_legacy_mortality.py @@ -1,3 +1,4 @@ +# Requires pyhealth==1.1.6 (legacy 1.x task_fn/Visit API) -- not runnable against pyhealth 2.0 from collections import defaultdict; from pyhealth.data import Patient; from pyhealth.datasets import MIMIC4Dataset from typing import Dict,List diff --git a/examples/drug_recommendation/drug_recommendation_mimic4_gamenet.py b/examples/drug_recommendation/drug_recommendation_mimic4_gamenet.py index bd5b33cb0..3e92ff8a3 100644 --- a/examples/drug_recommendation/drug_recommendation_mimic4_gamenet.py +++ b/examples/drug_recommendation/drug_recommendation_mimic4_gamenet.py @@ -1,12 +1,8 @@ -# import pyhealth -import pyhealth - -# import mimic4 dataset and drug recommendaton task +# import mimic4 dataset and drug recommendation task from pyhealth.datasets import MIMIC4Dataset -from pyhealth.tasks import drug_recommendation_mimic4_fn +from pyhealth.tasks import DrugRecommendationMIMIC4 # import dataloader related functions -from pyhealth.datasets.splitter import split_by_patient from pyhealth.datasets import split_by_patient, get_dataloader # import gamenet model @@ -23,16 +19,14 @@ def prepare_drug_task_data(): mimicvi = MIMIC4Dataset( - root="/srv/local/data/physionet.org/files/mimiciv/2.0/hosp", - tables=["diagnoses_icd", "procedures_icd", "prescriptions"], + ehr_root="/srv/local/data/physionet.org/files/mimiciv/2.0/hosp", + ehr_tables=["diagnoses_icd", "procedures_icd", "prescriptions"], + dev=_DEV, ) - print("stat") - mimicvi.stat() - print("info") - mimicvi.info() + mimicvi.stats() - mimic4_sample = mimicvi.set_task(drug_recommendation_mimic4_fn) + mimic4_sample = mimicvi.set_task(DrugRecommendationMIMIC4()) print(mimic4_sample[0]) return mimic4_sample @@ -50,14 +44,10 @@ def get_dataloaders(mimic4_sample): def train_gamenet(mimic4_sample, train_loader, val_loader): - # gamenet = GAMENet(mimicvi) - gamenet = GAMENet(mimic4_sample) + gamenet = GAMENet(dataset=mimic4_sample) - # print(gamenet.generate_ddi_adj()) trainer = Trainer( model=gamenet, - # metrics = ["jaccard_weighted", "pr_auc_micro", "pr_auc_macro"], - # metrics = ["jaccard", "pr_auc_micro", "pr_auc_macro"], metrics=[ "jaccard_samples", "accuracy", @@ -67,7 +57,6 @@ def train_gamenet(mimic4_sample, train_loader, val_loader): "pr_auc_samples", "f1_samples", ], - device="cuda", exp_name="drug_recommendation", )