跳到正文

PyHealth 临床机器学习研究

使用 PyHealth 组织电子病历、信号和医疗编码的研究处理与模型评估,核查输入、目标、处理流程和适用限制。

开源精选医疗业务11 个文件50,135 Stars浏览次数 2K-Dense-AI/scientific-agent-skills MIT版本 a1043ef
开始使用

适用环境:Requires Python 3.12 or 3.13 and PyHealth 2.0.2 (Torch 2.7.1). Network access is needed for installation, public datasets and uncached medical-code resources. Restricted clinical datasets require authorized local access.

输入资料
获准使用的研究数据与任务定义 PyHealth 环境及研究评价要求 阅读本节完整说明
交付内容
可复查的研究流程 模型评价与数据处理记录 阅读本节完整说明
  1. 获取完整包,保留配套文件的目录结构。
  2. 按照原文准备所需工具、依赖与输入资料。
  3. 阅读对应使用步骤与输入输出说明,在自己的环境中验证结果。

使用提示摘自原文,配套文件保留来源版本;实际运行条件以各包说明为准。

正文

获取原文件

references/medcode.md

PyHealth 2.0.2 implements local lookup/mapping over downloaded, cached reference tables. They are not all bundled in the wheel. First use needs public network access to https://storage.googleapis.com/pyhealth/resource/; there is no API key, JSON request body or pagination. SDK reads retrieve CSV resources by filename and cache processed mappings. Record the resource snapshot/checksum for reproducible work; package version alone does not pin the remote tables.

Lookup within a system

from pyhealth.medcode import InnerMap
icd9 = InnerMap.load("ICD9CM")
print(icd9.lookup("428.0"))
# Congestive heart failure, unspecified
print(icd9.get_ancestors("428.0"))
# ['428', '420-429.99', '390-459.99', '001-999.99']
atc = InnerMap.load("ATC")
print(atc.lookup("M01AE51"))
# ibuprofen, combinations

These calls and results were executed with 2.0.2. Supported vocabulary classes include ICD9CM, ICD10CM, ICD9PROC, ICD10PCS, ATC, NDC, RxNorm, CCSCM and CCSPROC. A class being available does not guarantee every cross-map exists. Validate unknown codes instead of inventing a description.

Cross-map

from pyhealth.medcode import CrossMap
print(CrossMap.load("ICD9CM", "CCSCM").map("428.0"))  # ['108']
print(CrossMap.load("NDC", "RxNorm").map("50580049698"))  # ['209387']
ndc_to_atc = CrossMap.load("NDC", "ATC")
print(ndc_to_atc.map("00527051210"))  # ['A11CC01']
print(ndc_to_atc.map("00527051210", target_kwargs={"level": 3}))  # ['A11C']

These mapping examples were executed. CrossMap first tries <source>_to_<target>.csv and on an HTTP error tries the reversed filename; the CSV columns still identify the requested direction. If neither resource exists, handle the failure explicitly. A source code may yield [] or multiple targets. Keep NDCs as strings to preserve leading zeros. Never assume mappings are bijective, lossless, current regulatory coding guidance, or enough to eliminate cohort shift.

Do not promise arbitrary ICD-9/ICD-10 conversion merely because both vocabularies load. Verify the desired map resource and its version. When combining MIMIC-IV codes, preserve each row's icd_version before conversion. Report unmapped and multiply mapped fractions, normalization decisions and any discarded records. For code reduction, inspect the actual vocabulary; there is no universal fixed CCS category count across resources and editions.

Tokenizer dimensions

from pyhealth.tokenizer import Tokenizer
vocab = ["A01A", "A02A", "A02B", "A03C", "A03D", "A04A"]
tok = Tokenizer(tokens=vocab, special_tokens=["<pad>", "<unk>"])
encoded_2d = tok.batch_encode_2d([["A03C", "A03D"], ["A04A", "B035"]])
assert encoded_2d == [[5, 6], [7, 1]]
print(tok.batch_decode_2d(encoded_2d))
# [['A03C', 'A03D'], ['A04A', '<unk>']]
encoded_3d = tok.batch_encode_3d([[["A03C", "A03D"], ["A04A"]], [["B035"]]])
print(tok.batch_decode_3d(encoded_3d))
# [[['A03C', 'A03D'], ['A04A']], [['<unk>']]]

Decode a 3D encoding with batch_decode_3d, not batch_decode_2d. Padding is removed by default; unknown tokens cannot be reconstructed. The reserved indices are 0 and 1 only because the special-token order above specifies them. Fit/reuse a training vocabulary; changing token order after loading weights invalidates the embedding semantics. Normal dataset processors usually handle tokenization.

Sources: MedCode, Tokenizer, and released 2.0.2 medcode/utils.py, cross_map.py, inner_map.py, tokenizer.py.

CONTACT HAONAN

联系haonan

医疗 AI · 产品与独立开发

刘浩南

关注医疗 AI 从 demo 到真实落地,聚焦产品化、商业化与院内工作流效率提升。

12年医疗产研经验,大湾区头部医疗AI公司前产品总监,独立开发者

医疗 AI 产品化医疗 AI 商业化院内工作流效率
刘浩南的微信联系二维码
微信扫一扫,联系 haonan