longitudinal/scripts_nnu/02_plan_preprocess.py
Furen Xiao a516408c4e Add iterative pseudo-labeling scripts for MONAI and nnU-Net
- Implemented 09_run_iterative.py for MONAI to orchestrate iterative pseudo-labeling rounds, including training, predicting, and evaluation.
- Created 01_prepare_dataset.py, 02_plan_preprocess.py, 06_pseudo_label.py, 07_train.py, 08_eval.py, and 09_run_iterative.py for nnU-Net to manage dataset preparation, planning, training, evaluation, and iterative pseudo-labeling.
- Enhanced error handling and logging throughout the scripts for better traceability.
- Added functionality to handle negative pseudo-labels and TTA options during inference.
- Included visualization of Dice scores across rounds for both MONAI and nnU-Net.
2026-10-08 06:13:13 +08:00

33 lines
No EOL
1.6 KiB
Python

"""Plan + preprocess the nnU-Net dataset (always --clean), then write the
subject-level splits_final.json: val = split_val cases, train = the rest
(all base + pseudo cases), same 5-fold content so -f 0 is the one we use.
Usage: python scripts_nnu/02_nnu_plan_preprocess.py --val data/manifests/split_val.jsonl [--npp 8]
"""
import argparse
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src.common import load_jsonl
from nnu_common import run, nnu_env, plan_preprocess_cmd, dataset_case_keys, write_splits, preproc_ds
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--val", required=True, help="jsonl of internal-validation cases (subject-disjoint)")
ap.add_argument("--npp", type=int, default=8, help="preprocess processes")
ap.add_argument("--log", default=None)
args = ap.parse_args()
log = args.log or os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "logs", "nnu_plan.log")
run(plan_preprocess_cmd(args.npp), log, env=nnu_env())
val_keys = [r["key"] for r in load_jsonl(args.val)]
all_keys = dataset_case_keys()
assert all_keys, "raw dataset is empty; run 01_nnu_prepare_dataset.py first"
assert set(val_keys) <= set(all_keys), f"val keys not in dataset: {set(val_keys) - set(all_keys)}"
n_tr, n_va = write_splits(all_keys, val_keys)
print(f"[nnu:plan] splits_final.json written to {preproc_ds()}: train={n_tr} val={n_va}", flush=True)
if __name__ == "__main__":
main()