fix: 修复 sampling 垃圾列 Bug — dir() 遍历方法名泄露为 CSV 列
- 旧代码用 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 个垃圾空列隐患。
This commit is contained in:
@ -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}")
|
||||
|
||||
Reference in New Issue
Block a user