From b6fa07925a5e94049e30f104b8d8050f5d7e96cc Mon Sep 17 00:00:00 2001 From: duxin Date: Tue, 28 Jul 2026 15:31:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=96=B0=E6=A8=A1=E5=9E=8B=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=8E=A8=E7=90=86=E6=97=B6=E7=BC=BA=E5=A4=B1=20WQI=20?= =?UTF-8?q?=E7=89=B9=E5=BE=81=E5=AF=BC=E8=87=B4=20Pipeline=20=E7=BB=B4?= =?UTF-8?q?=E5=BA=A6=E4=B8=8D=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新模型路径(有 train_wavelengths)重采样后仅产出 50 列光谱, 未计算模型训练时包含的 WQI 指数特征(期望 113 列) - 旧模型路径(else 分支)有此逻辑但被隔离,新路径无法受益 - 将 WQI 特征补全逻辑提至 if/else 之后统一执行, 通过 n_features_in_ 属性自动检测特征缺口并补齐 --- src/core/prediction/inference_batch.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/core/prediction/inference_batch.py b/src/core/prediction/inference_batch.py index b392bc0..3536187 100644 --- a/src/core/prediction/inference_batch.py +++ b/src/core/prediction/inference_batch.py @@ -662,6 +662,28 @@ class WaterQualityInference: print(f"[兼容填充] 特征不足,补零 " f"{n_current} → {expected_features} 列") + # ═══════════════════════════════════════════════════════════ + # ★ 特征补全:模型训练时可能包含 WQI 指数等衍生特征, + # 推理端需自动计算补齐(适用于新旧模型两条路径) + # ═══════════════════════════════════════════════════════════ + expected_features = getattr(model, 'n_features_in_', None) + if expected_features is not None and spectra.shape[1] < expected_features: + print(f"[特征补全] 检测到特征缺口:当前 {spectra.shape[1]} 列 " + f"< 模型期望 {expected_features} 列,正在计算 WQI 指数...") + try: + from src.utils.water_index import WaterQualityIndexCalculator + calc = WaterQualityIndexCalculator() + formulas = calc.list_available() + if formulas: + results_df = calc.calculate_many(formulas, spectra, fast=True) + if isinstance(results_df, pd.DataFrame) and not results_df.empty: + original_col_count = spectra.shape[1] + spectra = pd.concat([spectra, results_df], axis=1) + print(f"[特征补全] 完成!扩充至 {spectra.shape[1]} 列 " + f"(+{spectra.shape[1] - original_col_count} WQI)") + except Exception as e: + print(f"[特征补全] 失败: {e}") + # ═══════════════════════════════════════════════════════════ # 通用清洗 # ═══════════════════════════════════════════════════════════