From 5410bf74aed66016e142750aeffa3c22adccd745 Mon Sep 17 00:00:00 2001 From: duxin Date: Thu, 30 Jul 2026 12:43:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20sampling=20?= =?UTF-8?q?=E5=9E=83=E5=9C=BE=E5=88=97=20Bug=20=E2=80=94=20dir()=20?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E6=96=B9=E6=B3=95=E5=90=8D=E6=B3=84=E9=9C=B2?= =?UTF-8?q?=E4=B8=BA=20CSV=20=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 旧代码用 dir(calc) 遍历 WaterQualityIndexCalculator 的所有属性和方法名, 导致 DEFAULT_CSV/calculate_many/calculate_one/get_formula_info/list_available 等 5 个非公式名被错误写入 sampling_spectra.csv 列头,值为全 NaN。 - 改为 calc.list_available() 获取真实公式列表 + calc.calculate_many() 批量计算。 - 消除了下游推理端 load_sampling_data 面临的 5 个垃圾空列隐患。 --- src/utils/sampling.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/utils/sampling.py b/src/utils/sampling.py index e649d98..19d0564 100644 --- a/src/utils/sampling.py +++ b/src/utils/sampling.py @@ -386,31 +386,25 @@ def get_spectral_sampling_points_chunked(bil_file, water_mask_shp, severe_glint= try: from src.utils.water_index import WaterQualityIndexCalculator - print("\n[特征引擎挂载] 正在为采样点自动追加 45 个水质指数衍生特征...") - - # 读取基础底座(50列光谱) + # 读取基础底座 base_df = pd.read_csv(output_csvpath) - # 实例化计算器 + # 实例化计算器并获取真实公式列表 calc = WaterQualityIndexCalculator() + formula_names = calc.list_available() - # 提取有效算法 - algorithm_methods = [ - m for m in dir(calc) - if not m.startswith('_') and m not in ['find_closest_wavelength', 'calculate_all_indices'] - ] + print(f"\n[特征引擎挂载] 正在为采样点自动追加 {len(formula_names)} 个水质指数衍生特征...") - # 就地追加 45 列衍生指数 - for algo_name in algorithm_methods: - try: - algo_func = getattr(calc, algo_name) - base_df[algo_name] = algo_func(base_df) - except Exception: - base_df[algo_name] = np.nan + # ★ 使用 calculate_many 批量计算(替代 dir() 遍历方法名的 bug) + results_df = calc.calculate_many(formula_names, base_df, fast=True) + + # 仅保留计算成功的列,拼接至 base_df + for col_name in results_df.columns: + base_df[col_name] = results_df[col_name].values # 覆盖重写最终结果! base_df.to_csv(output_csvpath, index=False, encoding='utf-8-sig') - print(f"✓ 特征扩充大功告成!当前文件总维度完美适配模型: {base_df.shape}") + print(f"✓ 特征扩充大功告成!当前文件总维度: {base_df.shape}") except Exception as e: print(f"⚠ 警告:追加特征失败,保留原基础光谱。死因: {e}")