2026-05-07 14:23:58 +08:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
"""
|
|
|
|
|
|
Step8_75 面板 - 自定义回归预测
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
2026-05-07 16:49:24 +08:00
|
|
|
|
import os
|
|
|
|
|
|
|
2026-05-07 14:23:58 +08:00
|
|
|
|
from PyQt5.QtWidgets import (
|
|
|
|
|
|
QWidget, QVBoxLayout, QGroupBox,
|
|
|
|
|
|
QPushButton, QCheckBox, QMessageBox, QFileDialog,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
from src.gui.components.custom_widgets import FileSelectWidget
|
|
|
|
|
|
from src.gui.styles import ModernStylesheet
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Step8_75Panel(QWidget):
|
|
|
|
|
|
"""步骤8.75:自定义回归预测"""
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
|
|
super().__init__(parent)
|
|
|
|
|
|
self.init_ui()
|
|
|
|
|
|
|
|
|
|
|
|
def init_ui(self):
|
|
|
|
|
|
layout = QVBoxLayout()
|
|
|
|
|
|
|
|
|
|
|
|
# 采样光谱CSV文件选择
|
|
|
|
|
|
self.sampling_csv_file = FileSelectWidget(
|
|
|
|
|
|
"采样光谱CSV:",
|
|
|
|
|
|
"CSV Files (*.csv);;All Files (*.*)"
|
|
|
|
|
|
)
|
|
|
|
|
|
layout.addWidget(self.sampling_csv_file)
|
|
|
|
|
|
|
|
|
|
|
|
# 自定义回归模型目录选择(9_Custom_Regression_Modeling)
|
|
|
|
|
|
self.regression_models_dir = FileSelectWidget(
|
|
|
|
|
|
"回归模型目录:",
|
|
|
|
|
|
"Directories;;All Files (*.*)"
|
|
|
|
|
|
)
|
|
|
|
|
|
self.regression_models_dir.label.setText("回归模型目录:")
|
|
|
|
|
|
self.regression_models_dir.browse_btn.clicked.disconnect()
|
|
|
|
|
|
self.regression_models_dir.browse_btn.clicked.connect(self.browse_regression_models_dir)
|
|
|
|
|
|
self.regression_models_dir.set_path("9_Custom_Regression_Modeling")
|
|
|
|
|
|
layout.addWidget(self.regression_models_dir)
|
|
|
|
|
|
|
|
|
|
|
|
# 公式CSV文件选择(用于查找index_formula)
|
|
|
|
|
|
self.formula_csv_file = FileSelectWidget(
|
|
|
|
|
|
"公式CSV文件:",
|
|
|
|
|
|
"CSV Files (*.csv);;All Files (*.*)"
|
|
|
|
|
|
)
|
|
|
|
|
|
self.formula_csv_file.label.setText("公式CSV文件:")
|
|
|
|
|
|
layout.addWidget(self.formula_csv_file)
|
|
|
|
|
|
|
|
|
|
|
|
# 输出目录选择
|
|
|
|
|
|
self.output_dir_widget = FileSelectWidget(
|
|
|
|
|
|
"输出目录:",
|
|
|
|
|
|
"Directories;;All Files (*.*)"
|
|
|
|
|
|
)
|
|
|
|
|
|
self.output_dir_widget.label.setText("输出目录:")
|
|
|
|
|
|
self.output_dir_widget.browse_btn.clicked.disconnect()
|
|
|
|
|
|
self.output_dir_widget.browse_btn.clicked.connect(self.browse_output_dir)
|
|
|
|
|
|
self.output_dir_widget.line_edit.setPlaceholderText("留空使用默认prediction目录")
|
|
|
|
|
|
layout.addWidget(self.output_dir_widget)
|
|
|
|
|
|
|
|
|
|
|
|
# 启用步骤
|
|
|
|
|
|
self.enable_checkbox = QCheckBox("启用此步骤")
|
|
|
|
|
|
self.enable_checkbox.setChecked(True)
|
|
|
|
|
|
layout.addWidget(self.enable_checkbox)
|
|
|
|
|
|
|
|
|
|
|
|
# 独立运行按钮
|
|
|
|
|
|
self.run_button = QPushButton("独立运行此步骤")
|
|
|
|
|
|
self.run_button.setStyleSheet(ModernStylesheet.get_button_stylesheet('success'))
|
|
|
|
|
|
self.run_button.clicked.connect(self.run_step)
|
|
|
|
|
|
layout.addWidget(self.run_button)
|
|
|
|
|
|
|
|
|
|
|
|
layout.addStretch()
|
|
|
|
|
|
self.setLayout(layout)
|
|
|
|
|
|
|
2026-05-07 16:49:24 +08:00
|
|
|
|
def update_from_config(self, work_dir=None, pipeline=None):
|
|
|
|
|
|
"""从全局配置自动填充采样光谱和自定义回归模型目录
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
work_dir: 工作目录路径
|
|
|
|
|
|
pipeline: Pipeline 实例(未使用,保留接口兼容性)
|
|
|
|
|
|
"""
|
|
|
|
|
|
if work_dir:
|
|
|
|
|
|
self.work_dir = work_dir
|
|
|
|
|
|
elif hasattr(self, 'work_dir') and self.work_dir:
|
|
|
|
|
|
pass
|
|
|
|
|
|
else:
|
|
|
|
|
|
self.work_dir = None
|
|
|
|
|
|
|
|
|
|
|
|
main_window = self.window()
|
|
|
|
|
|
|
|
|
|
|
|
# 1. 尝试从 Step7 界面读取全湖采样点 CSV 路径
|
|
|
|
|
|
if main_window and hasattr(main_window, 'step7_panel'):
|
|
|
|
|
|
step7_output_path = main_window.step7_panel.output_file.get_path()
|
|
|
|
|
|
if step7_output_path:
|
|
|
|
|
|
existing = self.sampling_csv_file.get_path()
|
|
|
|
|
|
if not existing or not existing.strip():
|
|
|
|
|
|
self.sampling_csv_file.set_path(step7_output_path)
|
|
|
|
|
|
|
|
|
|
|
|
# 2. 尝试从 Step6.75 界面读取自定义回归模型目录
|
|
|
|
|
|
if main_window and hasattr(main_window, 'step6_75_panel'):
|
|
|
|
|
|
step6_75_models_dir = main_window.step6_75_panel.output_dir.text().strip()
|
|
|
|
|
|
if step6_75_models_dir:
|
|
|
|
|
|
existing_models = self.regression_models_dir.get_path()
|
|
|
|
|
|
if not existing_models or not existing_models.strip():
|
|
|
|
|
|
self.regression_models_dir.set_path(step6_75_models_dir)
|
|
|
|
|
|
|
|
|
|
|
|
# 3. 自动填充输出目录(自定义回归预测目录)
|
|
|
|
|
|
if self.work_dir:
|
|
|
|
|
|
output_dir = os.path.join(self.work_dir, "11_12_13_predictions/Custom_Regression_Prediction")
|
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
|
|
existing_out = self.output_dir_widget.get_path()
|
|
|
|
|
|
if not existing_out or not existing_out.strip():
|
|
|
|
|
|
self.output_dir_widget.set_path(output_dir)
|
|
|
|
|
|
|
|
|
|
|
|
def _get_default_work_dir(self):
|
|
|
|
|
|
"""获取 work_dir,优先用 panel 自身缓存的,否则尝试从主窗口取"""
|
|
|
|
|
|
if hasattr(self, 'work_dir') and self.work_dir:
|
|
|
|
|
|
return str(self.work_dir)
|
|
|
|
|
|
mw = self.window()
|
|
|
|
|
|
if mw and hasattr(mw, 'work_dir') and mw.work_dir:
|
|
|
|
|
|
return str(mw.work_dir)
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
2026-05-07 14:23:58 +08:00
|
|
|
|
def browse_regression_models_dir(self):
|
|
|
|
|
|
"""浏览回归模型目录"""
|
2026-05-07 16:49:24 +08:00
|
|
|
|
default = self._get_default_work_dir()
|
|
|
|
|
|
if default:
|
|
|
|
|
|
default = os.path.join(default, "9_Custom_Regression_Modeling")
|
|
|
|
|
|
dir_path = QFileDialog.getExistingDirectory(self, "选择回归模型目录", default)
|
2026-05-07 14:23:58 +08:00
|
|
|
|
if dir_path:
|
|
|
|
|
|
self.regression_models_dir.set_path(dir_path)
|
|
|
|
|
|
|
|
|
|
|
|
def browse_output_dir(self):
|
|
|
|
|
|
"""浏览输出目录"""
|
2026-05-07 16:49:24 +08:00
|
|
|
|
default = self._get_default_work_dir()
|
|
|
|
|
|
if default:
|
|
|
|
|
|
default = os.path.join(default, "11_12_13_predictions/Custom_Regression_Prediction")
|
|
|
|
|
|
dir_path = QFileDialog.getExistingDirectory(self, "选择输出目录", default)
|
2026-05-07 14:23:58 +08:00
|
|
|
|
if dir_path:
|
|
|
|
|
|
self.output_dir_widget.set_path(dir_path)
|
|
|
|
|
|
|
|
|
|
|
|
def get_config(self):
|
|
|
|
|
|
"""获取配置"""
|
|
|
|
|
|
config = {
|
|
|
|
|
|
'enabled': self.enable_checkbox.isChecked()
|
|
|
|
|
|
}
|
|
|
|
|
|
sampling_csv_path = self.sampling_csv_file.get_path()
|
|
|
|
|
|
if sampling_csv_path:
|
|
|
|
|
|
config['sampling_csv_path'] = sampling_csv_path
|
|
|
|
|
|
regression_models_dir = self.regression_models_dir.get_path()
|
|
|
|
|
|
if regression_models_dir:
|
|
|
|
|
|
config['custom_regression_dir'] = regression_models_dir
|
|
|
|
|
|
formula_csv_path = self.formula_csv_file.get_path()
|
|
|
|
|
|
if formula_csv_path:
|
|
|
|
|
|
config['formula_csv_path'] = formula_csv_path
|
|
|
|
|
|
output_dir = self.output_dir_widget.get_path()
|
|
|
|
|
|
if output_dir:
|
|
|
|
|
|
config['output_dir'] = output_dir
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
def set_config(self, config):
|
|
|
|
|
|
"""设置配置"""
|
|
|
|
|
|
if 'sampling_csv_path' in config:
|
|
|
|
|
|
self.sampling_csv_file.set_path(config['sampling_csv_path'])
|
|
|
|
|
|
if 'custom_regression_dir' in config:
|
|
|
|
|
|
self.regression_models_dir.set_path(config['custom_regression_dir'])
|
|
|
|
|
|
if 'formula_csv_path' in config:
|
|
|
|
|
|
self.formula_csv_file.set_path(config['formula_csv_path'])
|
|
|
|
|
|
if 'output_dir' in config:
|
|
|
|
|
|
self.output_dir_widget.set_path(config['output_dir'])
|
|
|
|
|
|
if 'enabled' in config:
|
|
|
|
|
|
self.enable_checkbox.setChecked(config['enabled'])
|
|
|
|
|
|
|
|
|
|
|
|
def run_step(self):
|
|
|
|
|
|
"""独立运行步骤8.75"""
|
|
|
|
|
|
sampling_csv_path = self.sampling_csv_file.get_path()
|
|
|
|
|
|
if not sampling_csv_path:
|
|
|
|
|
|
QMessageBox.warning(self, "输入错误", "请选择采样光谱CSV文件!")
|
|
|
|
|
|
return
|
|
|
|
|
|
regression_models_dir = self.regression_models_dir.get_path()
|
|
|
|
|
|
if not regression_models_dir:
|
|
|
|
|
|
QMessageBox.warning(self, "输入错误", "请选择回归模型目录!")
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
config = self.get_config()
|
|
|
|
|
|
|
|
|
|
|
|
parent = self.parent()
|
|
|
|
|
|
while parent and not hasattr(parent, 'run_single_step'):
|
|
|
|
|
|
parent = parent.parent()
|
|
|
|
|
|
|
|
|
|
|
|
if parent and hasattr(parent, 'run_single_step'):
|
|
|
|
|
|
parent.run_single_step('step8_75', {'step8_75': config})
|
|
|
|
|
|
else:
|
|
|
|
|
|
QMessageBox.critical(self, "错误", "无法找到父级GUI对象")
|