Enhance data parsing and validation, add extra info types files

- Improved `parse_int_list` and `parse_float_list` functions to support JSON list input.
- Introduced `validate_dataset_metadata` function to ensure dataset metadata consistency with training configuration.
- Added multiple new files for extra information types, categorizing them into assessment-only, exposure-only, and combined types.
- Removed deprecated `merge_extra_info_types` function and adjusted related logic in `train.py`.
- Updated `save_config` function to accept additional metadata for training runs.
- Refactored model and training scripts for better clarity and maintainability.
This commit is contained in:
2026-06-12 11:16:19 +08:00
parent fc8c7b7177
commit 0fa8bbbb9a
9 changed files with 818 additions and 69 deletions

110
README.md
View File

@@ -243,10 +243,108 @@ python train.py \
选择额外信息变量:
```bash
python train.py --extra_info_types 1 3 7
python train.py --extra_info_types_file extra_info_types_smoking_alcohol_bmi.txt
```
如果不传 `--extra_info_types`,默认使用全部 other-info type。
`train.py` 只接受 `--extra_info_types_file` 指定变量列表,不接受在 CLI 里直接输入 type id。文件可以每行一个 type id也可以带 `#` 注释;如果不传 `--extra_info_types_file`,默认使用全部 other-info type。
训练输出的 `train_config.json` 会记录:
- `extra_info_types_file`:训练时使用的列表文件名
- `extra_info_types`:解析后的实际 type id 列表,用于评估脚本复现变量选择
## 评估 AUC
当前提供两个 AUC 评估入口,二者都已适配新的 `DeepHealth` 模型和统一的 other-info token 输入AUC 的 DeLong 计算、病例/对照筛选和分层聚合逻辑保持原评估脚本口径。
### `evaluate_auc.py`
`evaluate_auc.py` 评估的是 **next-step / token-level 预测位置上的疾病 AUC**。
核心流程:
- 按训练配置重新构建 `HealthDataset` 和 `DeepHealth`。
- 对评估 split 中的患者做一次模型推理,缓存每个 disease-token readout hidden。
- 对疾病 token 分块投影到 `risk_head`,避免一次性保存全词表 logits。
- 对每个疾病、性别、年龄段、prediction offset 分别计算 AUC。
- 输出未池化分层结果和按疾病汇总后的结果。
典型用法:
```bash
python evaluate_auc.py \
--run_path runs/your_run_dir \
--eval_split test \
--offsets 0.1,1,5,10
```
主要输出:
- `df_auc_unpooled.csv`
- 疾病 token 在 sex、age bracket、offset 分层下的 AUC。
- `df_both.csv`
- 按疾病 token 和 offset 聚合后的 AUC。
适合回答的问题:
- “模型在历史序列中的某个预测 token 上,提前 offset 年预测未来疾病的区分能力如何?”
- “不同年龄段、性别、提前量下next-step 训练模型的疾病预测 AUC 如何?”
### `evaluate_auc_v2.py`
`evaluate_auc_v2.py` 评估的是 **landmark fixed-horizon incident disease AUC**。
它不是使用已有序列中的普通 readout 位置,而是在指定 landmark age 人工插入一个 `<NO_EVENT>` query token然后评估该 landmark 后固定 horizon 内是否发生 incident disease。
核心流程:
- 为每个患者和 landmark age 构造 landmark query 样本。
- 在 landmark age 插入 `<NO_EVENT>` token取该位置 hidden。
- 对疾病 token 分块投影到 `risk_head`。
- 按疾病、性别、landmark age、horizon 计算 incident disease AUC。
- 可选择排除 horizon 内先于目标疾病发生的死亡竞争风险。
典型用法:
```bash
python evaluate_auc_v2.py \
--run_path runs/your_run_dir \
--eval_split test \
--landmark_start 40 \
--landmark_stop 80 \
--landmark_step 5 \
--horizons 1,5,10
```
主要输出:
- `df_auc_landmark_unpooled.csv`
- 疾病 token 在 sex、landmark age、horizon 分层下的 AUC。
- `df_auc_landmark.csv`
- 按疾病 token 和 horizon 聚合后的 landmark AUC。
适合回答的问题:
- “一个人在 40/45/50/... 岁这个固定年龄点,如果此前未患某病,未来 1/5/10 年内发生该病的风险区分能力如何?”
- “模型能否作为 landmark risk prediction 模型使用?”
### 两者区别
| 项目 | `evaluate_auc.py` | `evaluate_auc_v2.py` |
| --- | --- | --- |
| 评估口径 | next-step/token-level 预测点 | landmark fixed-horizon incident risk |
| 查询位置 | 原始序列中满足 offset 条件的最新 readout token | 人工插入的 `<NO_EVENT>` landmark token |
| 时间参数 | `offsets`:预测点至少早于目标事件多少年 | `landmark_*` 和 `horizons`:固定年龄点与未来窗口 |
| 病例定义 | target table 中出现目标疾病的患者/事件 | landmark 后 horizon 内首次发生目标疾病 |
| 对照定义 | 从未出现该疾病的患者的 eligible target occurrence | landmark 时未患病,且 horizon 内未发病并有足够随访 |
| 分层 | sex + age bracket + offset | sex + landmark age + horizon |
| 输出文件 | `df_auc_unpooled.csv`, `df_both.csv` | `df_auc_landmark_unpooled.csv`, `df_auc_landmark.csv` |
| 适用问题 | 提前若干年预测未来目标事件的 token-level AUC | 固定年龄点未来固定年限 incident disease risk AUC |
简单选择:
- 想复现/延续旧的 next-token Delphi 风格 AUC用 `evaluate_auc.py`。
- 想做临床上更像 “某年龄点未来 N 年发病风险” 的 landmark AUC用 `evaluate_auc_v2.py`。
## 主要文件
@@ -278,3 +376,11 @@ python train.py --extra_info_types 1 3 7
- token readout
- same-time group readout
- last-valid readout
- `evaluate_auc.py`
- next-step/token-level 疾病 AUC 评估
- 使用 prediction offset、sex、age bracket 分层
- `evaluate_auc_v2.py`
- landmark fixed-horizon incident disease AUC 评估
- 通过插入 `<NO_EVENT>` landmark token 查询固定年龄点风险