-
Notifications
You must be signed in to change notification settings - Fork 773
Add MIMIC-III circulatory failure dataset and prediction task with ablation study #1096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kywang0906
wants to merge
16
commits into
sunlabuiuc:master
Choose a base branch
from
kywang0906:bella-mimic3-cf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b40cb7f
feat: prepare data for model training
kywang0906 93640d2
feat: load data with cache
kywang0906 c14fb13
feat: show how prepared dataset can be used
kywang0906 664a1ec
feat: add baseline model for future models comparison
kywang0906 cc406e7
feat:add MIMIC-III circulatory failure dataset and prediction task
kywang0906 c8e5f36
feat:modify baseline model to fit the function-call style of PyHealth
kywang0906 5502516
feat: add CirculatoryFailure logistic regression example and register…
kywang0906 3fd6c59
feat: add chartevents configuration to MIMIC-III dataset schema
kywang0906 f86ca8a
refactor: remove baseline training script for circulatory failure pre…
kywang0906 d4030b0
fix: remove redundant code
kywang0906 a9cc815
fix: move test files to the correct path
kywang0906 6591c5b
docs: add module, class, and function docstrings
kywang0906 3ce7421
refactor: extract MAP_ITEMID constant and clean up redundant imports …
kywang0906 b46bfab
Merge branch 'sunlabuiuc:master' into bella-mimic3-cf
kywang0906 2ff44b1
Align circulatory failure pipeline with PyHealth API
kywang0906 a8cfc28
refactor: refactor circulatory failure contribution as MIMIC3 task
kywang0906 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,26 @@ | ||
| pyhealth.datasets.mimic3_cf | ||
| =========================== | ||
|
|
||
| Overview | ||
| -------- | ||
|
|
||
| MIMIC3CirculatoryFailureDataset is a MIMIC-III based dataset for early warning | ||
| prediction of circulatory failure. | ||
|
|
||
| It constructs an ICU-stay-level cohort from PATIENTS, ADMISSIONS, and ICUSTAYS, | ||
| and uses CHARTEVENTS to extract Mean Arterial Pressure (MAP) measurements. | ||
|
|
||
| Circulatory failure is defined using a proxy event: | ||
|
|
||
| - MAP < 65 mmHg | ||
|
|
||
| For each ICU stay, the dataset identifies the first occurrence of this event and | ||
| supports building task-ready patient records for downstream prediction tasks. | ||
|
|
||
| API Reference | ||
| ------------- | ||
|
|
||
| .. autoclass:: pyhealth.datasets.MIMIC3CirculatoryFailureDataset | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: |
This file contains hidden or 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
24 changes: 24 additions & 0 deletions
24
docs/api/tasks/pyhealth.tasks.circulatory_failure_prediction.rst
This file contains hidden or 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,24 @@ | ||
| pyhealth.tasks.circulatory_failure_prediction | ||
| ============================================= | ||
|
|
||
| Overview | ||
| -------- | ||
|
|
||
| CirculatoryFailurePredictionTask defines a time-series prediction task for early | ||
| detection of circulatory failure. | ||
|
|
||
| The task predicts whether a patient will experience circulatory failure within | ||
| the next 12 hours based on physiological measurements. | ||
|
|
||
| Label definition: | ||
|
|
||
| - label = 1 if circulatory failure occurs within the next 12 hours | ||
| - label = 0 otherwise | ||
|
|
||
| API Reference | ||
| ------------- | ||
|
|
||
| .. autoclass:: pyhealth.tasks.CirculatoryFailurePredictionTask | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: |
This file contains hidden or 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,154 @@ | ||
| """ | ||
| Example ablation script for MIMIC-III circulatory failure prediction. | ||
|
|
||
| This script compares different prediction windows (6h, 12h, 24h) and | ||
| feature settings using logistic regression. It is intended as an example | ||
| usage script for the standard PyHealth dataset → task → SampleDataset pipeline. | ||
|
|
||
| Usage: | ||
| python mimic3_cf_circulatory_failure_logreg.py --root /path/to/mimic-iii | ||
| """ | ||
|
|
||
| import argparse | ||
|
|
||
| import pandas as pd | ||
| from sklearn.linear_model import LogisticRegression | ||
| from sklearn.metrics import accuracy_score, recall_score, roc_auc_score | ||
| from sklearn.model_selection import train_test_split | ||
|
|
||
| from pyhealth.datasets import MIMIC3CirculatoryFailureDataset | ||
| from pyhealth.tasks import CirculatoryFailurePredictionTask | ||
|
|
||
|
|
||
| def samples_to_df(sample_dataset) -> pd.DataFrame: | ||
| """Converts a SampleDataset into a pandas DataFrame.""" | ||
| rows = [] | ||
| for i in range(len(sample_dataset)): | ||
| s = sample_dataset[i] | ||
| rows.append( | ||
| { | ||
| "patient_id": s["patient_id"], | ||
| "icustay_id": s["icustay_id"], | ||
| "gender": s.get("gender"), | ||
| "timestamp": s.get("timestamp"), | ||
| "map": to_scalar(s["map"]), | ||
| "map_diff": to_scalar(s["map_diff"]), | ||
| "label": int(to_scalar(s["label"])), | ||
| } | ||
| ) | ||
| return pd.DataFrame(rows) | ||
|
|
||
|
|
||
| def evaluate_model( | ||
| df: pd.DataFrame, | ||
| feature_cols: list[str], | ||
| balanced: bool = False, | ||
| ) -> dict: | ||
| if df.empty or df["label"].nunique() < 2: | ||
| return { | ||
| "n_samples": len(df), | ||
| "accuracy": None, | ||
| "roc_auc": None, | ||
| "recall": None, | ||
| } | ||
|
|
||
| X = df[feature_cols] | ||
| y = df["label"] | ||
|
|
||
| X_train, X_test, y_train, y_test = train_test_split( | ||
| X, | ||
| y, | ||
| test_size=0.2, | ||
| random_state=42, | ||
| stratify=y, | ||
| ) | ||
|
|
||
| model = LogisticRegression( | ||
| max_iter=1000, | ||
| class_weight="balanced" if balanced else None, | ||
| ) | ||
| model.fit(X_train, y_train) | ||
|
|
||
| preds = model.predict(X_test) | ||
| probs = model.predict_proba(X_test)[:, 1] | ||
|
|
||
| return { | ||
| "n_samples": len(df), | ||
| "accuracy": accuracy_score(y_test, preds), | ||
| "roc_auc": roc_auc_score(y_test, probs), | ||
| "recall": recall_score(y_test, preds), | ||
| } | ||
|
|
||
|
|
||
| def print_metrics(title: str, metrics: dict) -> None: | ||
| print(f"\n=== {title} ===") | ||
| print(f"n_samples: {metrics['n_samples']}") | ||
| print(f"accuracy: {metrics['accuracy']}") | ||
| print(f"roc_auc: {metrics['roc_auc']}") | ||
| print(f"recall: {metrics['recall']}") | ||
|
|
||
| def to_scalar(x): | ||
| """Converts scalar tensor-like values to Python scalars.""" | ||
| if hasattr(x, "item"): | ||
| return x.item() | ||
| return x | ||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser( | ||
| description="MIMIC-III circulatory failure prediction ablation study." | ||
| ) | ||
| parser.add_argument( | ||
| "--root", | ||
| type=str, | ||
| required=True, | ||
| help="Path to the unzipped MIMIC-III database directory.", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| dataset = MIMIC3CirculatoryFailureDataset(root=args.root) | ||
|
|
||
| # Task ablation: prediction windows | ||
| for window in [6, 12, 24]: | ||
| print(f"\n############################") | ||
| print(f"Prediction window = {window}h") | ||
| print(f"############################") | ||
|
|
||
| task = CirculatoryFailurePredictionTask(prediction_window_hours=window) | ||
| sample_dataset = dataset.set_task(task) | ||
| df = samples_to_df(sample_dataset) | ||
|
|
||
| print("\nSample preview:") | ||
| print(df.head()) | ||
|
|
||
| # Baseline setting | ||
| baseline_metrics = evaluate_model( | ||
| df=df, | ||
| feature_cols=["map"], | ||
| balanced=False, | ||
| ) | ||
| print_metrics("Baseline: LogisticRegression(map)", baseline_metrics) | ||
|
|
||
| # Advanced setting | ||
| advanced_metrics = evaluate_model( | ||
| df=df, | ||
| feature_cols=["map", "map_diff"], | ||
| balanced=True, | ||
| ) | ||
| print_metrics( | ||
| "Advanced: LogisticRegression(map + map_diff, balanced)", | ||
| advanced_metrics, | ||
| ) | ||
|
|
||
| # Subgroup fairness | ||
| for gender in ["M", "F"]: | ||
| subgroup_df = df[df["gender"] == gender].copy() | ||
| subgroup_metrics = evaluate_model( | ||
| df=subgroup_df, | ||
| feature_cols=["map", "map_diff"], | ||
| balanced=True, | ||
| ) | ||
| print_metrics(f"Advanced subgroup gender={gender}", subgroup_metrics) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or 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 hidden or 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,47 @@ | ||
| version: "1.4" | ||
| tables: | ||
| patients: | ||
| file_path: "PATIENTS.csv.gz" | ||
| patient_id: "subject_id" | ||
| timestamp: null | ||
| attributes: | ||
| - "gender" | ||
| - "dob" | ||
| - "dod" | ||
| - "expire_flag" | ||
|
|
||
| admissions: | ||
| file_path: "ADMISSIONS.csv.gz" | ||
| patient_id: "subject_id" | ||
| timestamp: "admittime" | ||
| attributes: | ||
| - "hadm_id" | ||
| - "admittime" | ||
| - "dischtime" | ||
| - "deathtime" | ||
| - "hospital_expire_flag" | ||
| - "ethnicity" | ||
|
|
||
| icustays: | ||
| file_path: "ICUSTAYS.csv.gz" | ||
| patient_id: "subject_id" | ||
| timestamp: "intime" | ||
| attributes: | ||
| - "hadm_id" | ||
| - "icustay_id" | ||
| - "intime" | ||
| - "outtime" | ||
| - "first_careunit" | ||
| - "last_careunit" | ||
|
|
||
| chartevents: | ||
| file_path: "CHARTEVENTS.csv.gz" | ||
| patient_id: "subject_id" | ||
| timestamp: "charttime" | ||
| attributes: | ||
| - "hadm_id" | ||
| - "icustay_id" | ||
| - "itemid" | ||
| - "charttime" | ||
| - "value" | ||
| - "valuenum" |
This file contains hidden or 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,91 @@ | ||
| """ | ||
| MIMIC-III Circulatory Failure Dataset for PyHealth. | ||
|
|
||
| Dataset: | ||
| MIMIC-III Clinical Database v1.4 | ||
| https://physionet.org/content/mimiciii/1.4/ | ||
|
|
||
| Inspired by: | ||
| Hoche, M., Mineeva, O., Burger, M., Blasimme, A., & Ratsch, G. (2024). | ||
| FAMEWS: A fairness auditing tool for medical early-warning systems. | ||
| Proceedings of the Fifth Conference on Health, Inference, and Learning, 248, 297–311. PMLR. | ||
| https://proceedings.mlr.press/v248/hoche24a.html | ||
|
|
||
| Description: | ||
| Configures the MIMIC-III tables required for a circulatory-failure | ||
| early-warning task. The dataset keeps data loading separate from | ||
| task logic; sample generation is handled by | ||
| ``CirculatoryFailurePredictionTask`` through the standard PyHealth | ||
| ``dataset.set_task(task)`` pipeline. | ||
|
|
||
| Authors: | ||
| Kuang-Yu Wang ([email protected]) | ||
| Ya Hsuan Yang ([email protected]) | ||
| """ | ||
|
|
||
| import logging | ||
| from pathlib import Path | ||
| from typing import List, Optional | ||
| from .base_dataset import BaseDataset | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class MIMIC3CirculatoryFailureDataset(BaseDataset): | ||
| """MIMIC-III wrapper for circulatory failure early-warning prediction. | ||
|
|
||
| This dataset configures the MIMIC-III tables required for a | ||
| FAMEWS-inspired circulatory failure early-warning task. The dataset keeps | ||
| data loading separate from task logic; sample generation is handled by | ||
| ``CirculatoryFailurePredictionTask`` through the standard PyHealth | ||
| ``dataset.set_task(task)`` pipeline. | ||
|
|
||
| Args: | ||
| root: Root directory of the MIMIC-III dataset. | ||
| tables: Additional tables to load beyond the default tables. | ||
| dataset_name: Name of the dataset instance. | ||
| config_path: Path to the dataset config YAML file. | ||
| **kwargs: Additional keyword arguments passed to BaseDataset. | ||
|
|
||
| Examples: | ||
| >>> from pyhealth.datasets import MIMIC3CirculatoryFailureDataset | ||
| >>> from pyhealth.tasks import CirculatoryFailurePredictionTask | ||
| >>> dataset = MIMIC3CirculatoryFailureDataset( | ||
| ... root="/path/to/mimic-iii", | ||
| ... ) | ||
| >>> task = CirculatoryFailurePredictionTask(prediction_window_hours=12) | ||
| >>> sample_dataset = dataset.set_task(task) | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| root: str, | ||
| tables: Optional[List[str]] = None, | ||
| dataset_name: Optional[str] = None, | ||
| config_path: Optional[str] = None, | ||
| **kwargs, | ||
| ) -> None: | ||
| """Initializes the MIMIC-III circulatory failure dataset.""" | ||
| if config_path is None: | ||
| logger.info("No config path provided, using default config") | ||
| config_path = Path(__file__).parent / "configs" / "mimic3_cf.yaml" | ||
|
|
||
| default_tables = [ | ||
| "patients", | ||
| "admissions", | ||
| "icustays", | ||
| "chartevents", | ||
| ] | ||
|
|
||
| if tables is None: | ||
| tables = default_tables | ||
| else: | ||
| tables = list(dict.fromkeys(default_tables + tables)) | ||
|
|
||
| super().__init__( | ||
| root=root, | ||
| tables=tables, | ||
| dataset_name=dataset_name or "mimic3_cf", | ||
| config_path=str(config_path), | ||
| **kwargs, | ||
| ) | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey,I don't think you need to wrap MIMIC3 again, see the existing implementation here for MIMIC3. I don't see how this is different from our MIMIC3 implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing this out. I just removed
MIMIC3CirculatoryFailureDatasetand the related dataset config/docs/tests and also updated the example script to useMIMIC3Datasetdirectly.