from pathlib import Path from PySide6.QtWidgets import QMainWindow, QMessageBox, QFileDialog from matplotlib import use from yaml import dump, load, FullLoader from func.utils.PublicFunc import PublicFunc from ui.MainWindow.MainWindow_menu import Ui_Signal_Label from func.Module_approximately_align import MainWindow_approximately_align from func.Module_preprocess import MainWindow_preprocess from func.Module_detect_Jpeak import MainWindow_detect_Jpeak from func.Module_detect_Rpeak import MainWindow_detect_Rpeak from func.Module_label_check import MainWindow_label_check from func.Module_precisely_align import MainWindow_precisely_align from func.Module_cut_PSG import MainWindow_cut_PSG from func.utils.Constants import Constants, ConfigParams use("QtAgg") Config = { } class MainWindow(QMainWindow, Ui_Signal_Label): def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_Signal_Label() self.ui.setupUi(self) self.setFixedSize(720, 1080) # 设置固定大小,禁止缩放 # 消息弹窗初始化 self.msgBox = QMessageBox() self.msgBox.setWindowTitle(Constants.MAINWINDOW_MSGBOX_TITLE) self.__read_config__() self.ui.plainTextEdit_root_path.setPlainText(Config["Path"]["Root"]) self.seek_sampID(Path(Config["Path"]["Root"]) / Path(ConfigParams.PUBLIC_PATH_ORGBCG_TEXT)) self.approximately_align = None self.preprocess = None self.detect_Jpeak = None self.detect_Rpeak = None self.label_check = None self.precisely_align = None self.cut_PSG = None # 绑定槽函数 self.ui.pushButton_open.clicked.connect(self.__slot_btn_open__) self.ui.pushButton_approximately_align.clicked.connect(self.__slot_btn_approximately_align__) self.ui.pushButton_preprocess_BCG.clicked.connect(self.__slot_btn_preprocess__) self.ui.pushButton_preprocess_ECG.clicked.connect(self.__slot_btn_preprocess__) self.ui.pushButton_detect_Jpeak.clicked.connect(self.__slot_btn_detect_Jpeak__) self.ui.pushButton_detect_Rpeak.clicked.connect(self.__slot_btn_detect_Rpeak__) self.ui.pushButton_label_check_BCG.clicked.connect(self.__slot_btn_label_check__) self.ui.pushButton_label_check_ECG.clicked.connect(self.__slot_btn_label_check__) self.ui.pushButton_precisely_align.clicked.connect(self.__slot_btn_precisely_align__) self.ui.pushButton_cut_PSG.clicked.connect(self.__slot_btn_cut_PSG__) @staticmethod def __read_config__(): if not Path(ConfigParams.PUBLIC_CONFIG_FILE_PATH).exists(): Path(ConfigParams.PUBLIC_CONFIG_FILE_PATH).parent.mkdir(parents=True, exist_ok=True) with open(ConfigParams.PUBLIC_CONFIG_FILE_PATH, "w") as f: dump(ConfigParams.PUBLIC_CONFIG_NEW_CONTENT, f) with open(ConfigParams.PUBLIC_CONFIG_FILE_PATH, "r") as f: file_config = load(f.read(), Loader=FullLoader) Config.update(file_config) @staticmethod def __write_config__(): with open(Path(ConfigParams.PUBLIC_CONFIG_FILE_PATH), "w") as f: dump(Config, f) def __slot_btn_open__(self): file_dialog = QFileDialog() file_dialog.setFileMode(QFileDialog.Directory) file_dialog.setOption(QFileDialog.ShowDirsOnly, True) if file_dialog.exec_() == QFileDialog.Accepted: self.seek_sampID(Path(file_dialog.selectedFiles()[0]) / ConfigParams.PUBLIC_PATH_ORGBCG_TEXT) self.ui.plainTextEdit_root_path.setPlainText(file_dialog.selectedFiles()[0]) # 修改配置 Config["Path"]["Root"] = str(file_dialog.selectedFiles()[0]) self.__write_config__() else: PublicFunc.msgbox_output(self, Constants.OPERATION_CANCELED, Constants.MSGBOX_TYPE_INFO) def __slot_btn_approximately_align__(self): self.approximately_align = MainWindow_approximately_align() root_path = self.ui.plainTextEdit_root_path.toPlainText() sampID = self.ui.comboBox_sampID.currentText() if not self.check_root_path(): return if not self.check_sampID(): return self.approximately_align.show(root_path, int(sampID)) def __slot_btn_preprocess__(self): self.preprocess = MainWindow_preprocess() sender = self.sender() root_path = self.ui.plainTextEdit_root_path.toPlainText() sampID = self.ui.comboBox_sampID.currentText() if sender == self.ui.pushButton_preprocess_BCG: mode = "BCG" elif sender == self.ui.pushButton_preprocess_ECG: mode = "ECG" else: raise ValueError("模式不存在") if not self.check_root_path(): return if not self.check_sampID(): return self.preprocess.show(mode, root_path, int(sampID)) def __slot_btn_detect_Jpeak__(self): self.detect_Jpeak = MainWindow_detect_Jpeak() root_path = self.ui.plainTextEdit_root_path.toPlainText() sampID = self.ui.comboBox_sampID.currentText() if not self.check_root_path(): return if not self.check_sampID(): return self.detect_Jpeak.show(root_path, int(sampID)) def __slot_btn_detect_Rpeak__(self): self.detect_Rpeak = MainWindow_detect_Rpeak() root_path = self.ui.plainTextEdit_root_path.toPlainText() sampID = self.ui.comboBox_sampID.currentText() if not self.check_root_path(): return if not self.check_sampID(): return self.detect_Rpeak.show(root_path, int(sampID)) def __slot_btn_label_check__(self): self.label_check = MainWindow_label_check() sender = self.sender() root_path = self.ui.plainTextEdit_root_path.toPlainText() sampID = self.ui.comboBox_sampID.currentText() if not self.check_root_path(): return if not self.check_sampID(): return if sender == self.ui.pushButton_label_check_BCG: mode = "BCG" elif sender == self.ui.pushButton_label_check_ECG: mode = "ECG" else: raise ValueError("模式不存在") self.label_check.show(mode, root_path, int(sampID)) def __slot_btn_precisely_align__(self): self.precisely_align = MainWindow_precisely_align() root_path = self.ui.plainTextEdit_root_path.toPlainText() sampID = self.ui.comboBox_sampID.currentText() if not self.check_root_path(): return if not self.check_sampID(): return self.precisely_align.show(root_path, int(sampID)) def __slot_btn_cut_PSG__(self): self.cut_PSG = MainWindow_cut_PSG() root_path = self.ui.plainTextEdit_root_path.toPlainText() sampID = self.ui.comboBox_sampID.currentText() if not self.check_root_path(): return if not self.check_sampID(): return self.cut_PSG.show(root_path, int(sampID)) def seek_sampID(self, path): if not Path(path).exists(): PublicFunc.msgbox_output(self, Constants.MAINWINDOW_ROOT_PATH_NOT_EXIST, Constants.MSGBOX_TYPE_ERROR) return sub_folders = [item.name for item in Path(path).iterdir() if item.is_dir()] self.ui.comboBox_sampID.addItems(sub_folders) def check_root_path(self): if self.ui.plainTextEdit_root_path.toPlainText() == Constants.STRING_IS_EMPTY: PublicFunc.msgbox_output(self, Constants.MAINWINDOW_ROOT_PATH_NOT_EXIST, Constants.MSGBOX_TYPE_ERROR) return False return True def check_sampID(self): if self.ui.comboBox_sampID.currentText() == Constants.STRING_IS_EMPTY: PublicFunc.msgbox_output(self, Constants.MAINWINDOW_SAMPID_EMPTY, Constants.MSGBOX_TYPE_ERROR) return False return True