refactor(pipeline): 路径直接传输 — 统一 ctx 字段名/panel key/step 形参名

This commit is contained in:
DXC
2026-06-03 17:29:41 +08:00
parent 517bb28611
commit 343e316799
99 changed files with 9127 additions and 91 deletions

View File

@ -20,23 +20,28 @@ class PipelineContext:
"""流水线运行上下文(在 14 个 step 之间传递的内存字典)
字段命名约定:
- 路径字段统一 `_path` 后缀(如 water_mask_path
- 目录类字段无 `_path` 后缀(如 models_dir
- 路径字段名 = panel key 名 = step 形参名(全链路无翻译
- 训练/产物 CSV 用 `_path` 后缀(如 training_csv_path / water_mask_path
- 入参影像/CSV 沿用 panel 原名img_path / csv_path无 `_path` 后缀
- 目录类字段无 `_path` 后缀(如 models_dir / prediction_dir
- 元信息字段无后缀(如 user_config / status / log
"""
# ── 9 步主路径(按 step 输出顺序排列) ──
raw_img_path: Optional[str] = None # Step 1 入参:原始影像
# ── 11 个 step 的入参/产物(按 step 顺序排列;字段名 = panel key = step 形参 ──
img_path: Optional[str] = None # Step 1/2/3 入参:原始影像
water_mask_path: Optional[str] = None # Step 1 出 → Step 2/3/7 入
glint_mask_path: Optional[str] = None # Step 2 出 → Step 3/7 入
deglint_img_path: Optional[str] = None # Step 3 出 → Step 5/7 入
raw_csv_path: Optional[str] = None # Step 4 入:原始 CSV
csv_path: Optional[str] = None # Step 4/5/6_5/6_75:原始/训练 CSV
processed_csv_path: Optional[str] = None # Step 4 出 → Step 5 入
training_spectra_path: Optional[str] = None # Step 5 出 → Step 6
training_csv_path: Optional[str] = None # Step 5 出 → Step 5_5/6/6_5/6_75
boundary_path: Optional[str] = None # Step 5 入参:边界 SHPpanel step5 名)
indices_path: Optional[str] = None # Step 5.5 出
sampling_csv_path: Optional[str] = None # Step 7 出 → Step 8/9 入
prediction_csv_path: Optional[str] = None # Step 8 出
sampling_csv_path: Optional[str] = None # Step 7 出 → Step 8/8_5/8_75/9 入
prediction_csv_path: Optional[str] = None # Step 8 出 → Step 9 入
distribution_map_path: Optional[str] = None # Step 9 出
boundary_shp_path: Optional[str] = None # Step 9 入参:边界 SHPpanel step9 名)
formula_csv_path: Optional[str] = None # Step 8_75 入参:公式 CSV
# ── 目录类(命名不带 _path 以示区别) ──
models_dir: Optional[str] = None

View File

@ -4,10 +4,8 @@ PipelineRunner基于 StepSpec 声明式调度 14 个 step。
设计要点:
- StepSpec 声明 requiresctx 字段名列表)+ producesctx 字段名列表)
- 默认约定ctx 字段名去掉 `_path` 后缀 = step 方法形参名
ctx.water_mask_path → 形参 water_mask
ctx.raw_img_path → 形参 raw_img
- 可被 spec.parameter_map 覆盖
- 命名约定ctx 字段名 == panel key 名 == step 形参名(全链路无翻译)
- 保留 spec.parameter_map 字段骨架供极少数特例覆盖(默认空 dict
- 调度顺序:按 PIPELINE_STEPS 列表顺序requires 缺则 skip
- 软取消:在每个 step 前检查 ctx.is_cancelled()
- duck-typed pipelinerunner 只调 getattr(pipeline, method_name),不强依赖类层级
@ -48,101 +46,76 @@ class StepSpec:
PIPELINE_STEPS: List[StepSpec] = [
StepSpec(
step_id="step1", method_name="step1_generate_water_mask",
requires=["raw_img_path"], produces=["water_mask_path"],
# ctx.raw_img_path → 形参 img_path老 step1 形参名是 img_path不是 raw_img
parameter_map={"raw_img_path": "img_path"},
requires=["img_path"], produces=["water_mask_path"],
description="水域掩膜生成NDWI 或 SHP",
),
StepSpec(
step_id="step2", method_name="step2_find_glint_area",
requires=["raw_img_path", "water_mask_path"], produces=["glint_mask_path"],
# raw_img_path→img_pathwater_mask_path 不变
parameter_map={"raw_img_path": "img_path"},
requires=["img_path", "water_mask_path"], produces=["glint_mask_path"],
description="耀斑区域检测",
),
StepSpec(
step_id="step3", method_name="step3_remove_glint",
requires=["deglint_img_path"], produces=["deglint_img_path"],
# deglint_img_path→img_path老 step3 形参名是 img_path
# 注意glint_mask_path 不在 requires 中——step3 形参表无该参数,内部走 self.glint_mask_path 回退
parameter_map={"deglint_img_path": "img_path"},
requires=["img_path", "water_mask_path", "glint_mask_path"],
produces=["deglint_img_path"],
description="耀斑去除",
),
StepSpec(
step_id="step4", method_name="step4_process_csv",
requires=["raw_csv_path"], produces=["processed_csv_path"],
# raw_csv_path→csv_path老 step4 形参名是 csv_path
parameter_map={"raw_csv_path": "csv_path"},
requires=["csv_path"], produces=["processed_csv_path"],
description="CSV 异常值清洗",
),
StepSpec(
step_id="step5", method_name="step5_extract_training_spectra",
requires=["deglint_img_path", "processed_csv_path"], produces=["training_spectra_path"],
# processed_csv_path→csv_path老 step5 形参名是 csv_pathdeglint_img_path 不变
parameter_map={"processed_csv_path": "csv_path"},
requires=["deglint_img_path", "csv_path", "boundary_path", "glint_mask_path"],
produces=["training_csv_path"],
description="实测样本点光谱提取",
),
StepSpec(
step_id="step5_5", method_name="step5_5_calculate_water_quality_indices",
requires=["training_spectra_path"], produces=["indices_path"],
# 老 step5.5 形参是 training_spectra_pathctx 字段同名,无需映射
parameter_map={},
requires=["training_csv_path"], produces=["indices_path"],
description="水质光谱指数计算optional",
),
StepSpec(
step_id="step6", method_name="step6_train_models",
requires=["training_spectra_path"], produces=["models_dir"],
# training_spectra_path→training_csv_path老 step6 形参名是 training_csv_path
parameter_map={"training_spectra_path": "training_csv_path"},
requires=["training_csv_path"], produces=["models_dir"],
description="ML 建模GridSearchCV / AutoML",
),
StepSpec(
step_id="step6_5", method_name="step6_5_non_empirical_modeling",
requires=["training_spectra_path"], produces=["models_dir"],
# training_spectra_path→csv_path老 step6.5 形参名是 csv_path
parameter_map={"training_spectra_path": "csv_path"},
requires=["training_csv_path"], produces=["models_dir"],
description="非经验统计回归",
),
StepSpec(
step_id="step6_75", method_name="step6_75_custom_regression",
requires=["training_spectra_path"], produces=["models_dir"],
# training_spectra_path→csv_path老 step6.75 形参名是 csv_path
parameter_map={"training_spectra_path": "csv_path"},
requires=["training_csv_path"], produces=["models_dir"],
description="自定义回归分析",
),
StepSpec(
step_id="step7", method_name="step7_generate_sampling_points",
requires=["deglint_img_path", "water_mask_path"], produces=["sampling_csv_path"],
# 老 step7 形参是 deglint_img_path / water_mask_pathctx 字段同名
parameter_map={},
description="整景密集采样点生成 + 光谱提取",
),
StepSpec(
step_id="step8", method_name="step8_predict_water_quality",
requires=["sampling_csv_path", "models_dir"], produces=["prediction_csv_path"],
parameter_map={},
description="ML 模型预测(采样点)",
),
StepSpec(
step_id="step8_5", method_name="step8_5_predict_with_non_empirical_models",
requires=["sampling_csv_path"], produces=["prediction_dir"],
parameter_map={},
requires=["sampling_csv_path", "models_dir"], produces=["prediction_dir"],
description="非经验模型预测",
),
StepSpec(
step_id="step8_75", method_name="step8_75_predict_with_custom_regression",
requires=["sampling_csv_path"], produces=["prediction_dir"],
parameter_map={},
requires=["sampling_csv_path", "models_dir", "formula_csv_path"],
produces=["prediction_dir"],
description="自定义回归预测",
),
StepSpec(
step_id="step9", method_name="step9_generate_distribution_map",
requires=["prediction_csv_path"],
requires=["prediction_csv_path", "boundary_shp_path"],
produces=["distribution_map_path"],
# 老 step9 形参是 prediction_csv_path / boundary_shp_pathctx 字段同名
# 注意sampling_csv_path / water_mask_path 不在 requires 中——step9 形参表无该参数,
# 内部走 self.sampling_csv_path / self.water_mask_path 回退
parameter_map={},
description="克里金插值成图",
),
]
@ -157,7 +130,7 @@ class PipelineRunner:
用法:
runner = PipelineRunner(pipeline_instance)
ctx = PipelineContext(raw_img_path=..., ...)
ctx = PipelineContext(img_path=..., ...)
result_ctx = runner.run(ctx)
"""