Pyhealth clinical dl
Skill findscripter/everything-skills/09-verticals/pyhealth-clinical-dl
类书式 AI Agent 技能大典 · 精选/中文化/互见成网的 500+ 开源技能,可作为 Claude Code 插件市场一键安装。A curated, cross-referenced encyclopedia of 500+ open-source agent skills.
npx -y skills add findscripter/everything-skills --skill pyhealth-clinical-dlAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 1 stars1 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its author says it does
Copied from the file, not written here
当用 PyHealth 在 EHR/生理信号/医学影像上做临床预测(死亡率、再入院、住院时长、用药推荐、睡眠分期、ICD 编码)时使用;按数据集→任务→模型→Trainer→指标五段式搭建可训练流水线并产出模型与临床指标,含医疗编码 ICD/ATC/NDC/RxNorm 查询互映;不适用于纯表格数据的通用 PyTorch 建模;触发词:PyHealth、MIMIC、eICU、OMOP、EHR、用药推荐、ICD 编码
The file declares its own license as MIT. That is the author’s claim about this one file, and it is not the same thing as the license GitHub reports for the repository, which is listed with the other numbers below.
SKILL.md
6.6 KB, as published. Nobody here has run it
何时使用
当临床/健康 ML 任务符合 PyHealth 的五段式流水线(Dataset → Task → Model → Trainer → Metrics)时使用本条,典型场景:
- 提到 PyHealth、MIMIC-III/IV、eICU、OMOP-CDM、EHRShot、SleepEDF、SHHS、ISRUC、ChestX-ray14、TUEV/TUAB 等临床数据集。
- 要预测死亡率、再入院、住院时长(LOS)、用药推荐、睡眠分期、ICD 编码、EEG 事件、去标识化。
- 要查询或跨映射医疗编码(ICD-9-CM、ICD-10-CM、ATC、NDC、RxNorm、CCS)。
- 手上有 EHR 形态数据,想直接训练临床模型而不手写数据管道。
不该用本条的边界:
- 只是想在通用表格数据上跑原生 PyTorch → 不需要 PyHealth。
- 工作流不符合「数据集→任务→模型→Trainer」五段式,硬套通常是在和库对着干。
步骤
- 装环境:PyHealth 2.0 需 Python ≥3.12 且 <3.14,用
uv管理。 - Dataset:实例化数据集类(如
MIMIC3Dataset),得到可查询的患者注册表BaseDataset。 - Task:
base.set_task(task)把患者转成有监督样本,得到SampleDataset(模型/切分/DataLoader 都吃这个)。 - 切分 + DataLoader:
split_by_patient(默认)按患者切分防泄漏,get_dataloader构建 loader。 - Model:把
SampleDataset传给模型(Transformer/RETAIN/GAMENet/SafeDrug/MICRON/StageNet/AdaCare/CNN/RNN/MLP)。 - Trainer + Metrics:用
Trainer.train(...)训练并按monitor选最佳 checkpoint,inference后用对应指标函数评估。
指令
安装(uv):
uv init my-pyhealth-project && cd my-pyhealth-project
uv python pin 3.12
uv add pyhealth # 会一并拉入 PyTorch
uv run python train.py
# 一次性脚本:uv run --with pyhealth python script.py
# 旧 1.x 线(Python 3.9+):uv add pyhealth==1.16
选型决策:
- 任务要匹配数据集:任务类与数据集绑定。
MortalityPredictionMIMIC3不能用于 MIMIC-IV,后者用MortalityPredictionMIMIC4/InHospitalMortalityMIMIC4。 monitor要匹配任务类型:二分类用"pr_auc"/"roc_auc";多标签(用药推荐)用"pr_auc_samples"/"jaccard_samples";多分类用"accuracy"/"f1_macro"。选错会保存错误 epoch 的 checkpoint。- 写最小、地道的 PyHealth,别在原生 PyTorch 里重造训练循环——
Trainer自带 checkpoint、日志、最佳模型选择。
示例
完整流水线通常 <20 行,这是规范形态,从此出发改造:
from pyhealth.datasets import MIMIC3Dataset, split_by_patient, get_dataloader
from pyhealth.tasks import MortalityPredictionMIMIC3
from pyhealth.models import Transformer
from pyhealth.trainer import Trainer
from pyhealth.metrics.binary import binary_metrics_fn
# 1. Dataset —— 原始患者注册表(BaseDataset)
base = MIMIC3Dataset(
root="https://storage.googleapis.com/pyhealth/Synthetic_MIMIC-III/",
tables=["DIAGNOSES_ICD", "PROCEDURES_ICD", "PRESCRIPTIONS"],
# cache_dir="./cache", # 可复现:缓存解析结果,避免每次重解析
)
# 2. Task —— 把患者转成有监督样本(SampleDataset)
samples = base.set_task(MortalityPredictionMIMIC3())
# 3. 切分 + DataLoader(按患者切分防泄漏)
train_ds, val_ds, test_ds = split_by_patient(samples, [0.8, 0.1, 0.1])
train_loader = get_dataloader(train_ds, batch_size=32, shuffle=True)
val_loader = get_dataloader(val_ds, batch_size=32, shuffle=False)
test_loader = get_dataloader(test_ds, batch_size=32, shuffle=False)
# 4. Model —— 必须传 SampleDataset(samples),不是 BaseDataset(base)
model = Transformer(dataset=samples)
# 5. 训练 + 评估
trainer = Trainer(model=model)
trainer.train(train_dataloader=train_loader, val_dataloader=val_loader,
epochs=50, monitor="pr_auc")
y_true, y_prob, _ = trainer.inference(test_loader)
print(binary_metrics_fn(y_true, y_prob, metrics=["pr_auc", "roc_auc"]))
演示/学习无需凭证,可直接用合成 MIMIC-III 桶 https://storage.googleapis.com/pyhealth/Synthetic_MIMIC-III/;有私有 MIMIC 访问权时把 root 指向本地 CSV 目录。
注意事项
- 模型吃
SampleDataset,不是BaseDataset:MIMIC3Dataset(...)返回的是可查询注册表,只有.set_task(task)之后才得到模型/切分/DataLoader 所需的SampleDataset。误传base会报错或行为异常。 - 始终按患者(或就诊)切分,别按样本:随机样本级切分会让同一患者跨训练/测试,造成信息泄漏。患者级用
split_by_patient;仅当各次就诊独立时才用split_by_visit。 - 任务要匹配数据集:任务类是数据集专属的(见上文 MIMIC-III vs MIMIC-IV)。
monitor要匹配任务类型:见「指令」,选错保存错误 epoch。- MIMIC-IV 用
ehr_root=而非root=:这是数据集构造器里唯一的不一致点。 - 要可复现就设
cache_dir=到持久目录:PyHealth 会缓存解析后的数据集,不设则每次重新解析。
互见
- related:
dicom-medical-imaging—— PyHealth 也覆盖影像数据集(ChestX-ray14),影像 I/O 与预处理可参此 - related:
guided-statistical-analysis—— 临床指标解读与统计检验 - combines_with:
scientific-database-lookup—— 查询医疗编码/本体(ICD/ATC/RxNorm)背景信息时
本条采编自 K-Dense-AI/scientific-agent-skills(MIT),适配重写而非逐字翻译。