全局修正

This commit is contained in:
DXC
2026-06-29 16:16:55 +08:00
parent 2788fb3fe1
commit e337f01312
7 changed files with 437 additions and 104 deletions

View File

@ -12,7 +12,7 @@ warnings.filterwarnings('ignore')
import sys
import os
from src.preprocessing.spectral_Preprocessing import Preprocessing
from src.preprocessing.spectral_Preprocessing import Preprocessing, get_preprocessing_transformer
from src.core.utils.split_methods import spxy, ks
# try:
@ -22,6 +22,7 @@ from src.core.utils.split_methods import spxy, ks
# 机器学习相关导入
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
class WaterQualityInference:
@ -423,33 +424,20 @@ class WaterQualityInference:
from src.utils.water_index import WaterQualityIndexCalculator
calc = WaterQualityIndexCalculator()
# 提取纯计算方法(排除 find_closest_wavelength 和 calculate_all_indices,
# 以及不返回 Series 的辅助方法)
algorithm_methods = []
for m in dir(calc):
if m.startswith('_'):
continue
if m in ['find_closest_wavelength', 'calculate_all_indices']:
continue
attr = getattr(calc, m)
if callable(attr):
algorithm_methods.append(m)
original_col_count = spectra.shape[1]
for algo_name in algorithm_methods:
try:
algo_func = getattr(calc, algo_name)
result = algo_func(spectra)
# 只追加返回 Series 且长度为样本数的合法结果
if isinstance(result, pd.Series) and len(result) == len(spectra):
spectra[algo_name] = result.values
else:
spectra[algo_name] = np.nan
except Exception:
spectra[algo_name] = np.nan
print(f"[特征补全] 完成!光谱列已扩充至 {spectra.shape[1]} 列"
f"(追加了 {spectra.shape[1] - original_col_count} 个 WQI 指数)")
# CSV 驱动的 WaterQualityIndexCalculator:所有公式名通过 list_available() 拿;
# 一次性 calculate_many() 批量计算。彻底摆脱 dir(calc) 反射扫描 + 单 algo_func
# 调用这种碎片化写法(Calculator 早已重构为公式驱动,不再有独立公式方法)。
formulas = calc.list_available()
if not formulas:
print("[特征补全] Calculator 未持有任何公式,跳过补全")
else:
results_df = calc.calculate_many(formulas, spectra)
# results_df 是列对齐的 WQI 计算结果(每列一个公式,行数=样本数)
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}")
@ -471,6 +459,14 @@ class WaterQualityInference:
print(f"[特征对齐] 最终输入维度: {spectra.shape}")
# ---- Pipeline 化分支:模型内置 scaler/MSC.mean_spectrum_ 等状态时,跳过手动 Preprocessing ----
if isinstance(model, Pipeline):
print(f"[Pipeline] 检测到模型是 sklearn Pipeline,"
f"其内置预处理步骤({list(model.named_steps.keys())[0]})将处理原始光谱,"
f"无需外部 Preprocessing")
return spectra.values
# ---- 兼容路径:旧 .joblib(裸模型 + preprocess_method 字符串)回退手动 Preprocessing ----
try:
# 应用预处理
spectra_processed = Preprocessing(actual_preprocess_method, spectra)
@ -479,7 +475,8 @@ class WaterQualityInference:
if isinstance(spectra_processed, pd.DataFrame):
spectra_processed = spectra_processed.values
print(f"预处理后数据形状: {spectra_processed.shape}")
print(f" [Legacy] 旧裸模型 + 手动 Preprocessing({actual_preprocess_method}) 完成,"
f"数据形状: {spectra_processed.shape}")
return spectra_processed