优化<人工纠正>的代码

This commit is contained in:
Yorusora
2025-05-20 17:31:45 +08:00
parent d01dbcb5f5
commit d2e0a61056
3 changed files with 92 additions and 68 deletions

View File

@ -84,18 +84,13 @@ class SettingWindow(QMainWindow):
Config.update({
"Path": {
"Input_Signal": str((Path(self.root_path) / ConfigParams.PUBLIC_PATH_ORGBCG_TEXT /
Path(str(self.sampID)) / Path(ConfigParams.LABEL_CHECK_INPUT_BCG_FILENAME +
str(Config["InputConfig"]["Freq"]) +
ConfigParams.ENDSWITH_TXT))),
Path(str(self.sampID)))),
"Input_Peak": str((Path(self.root_path) / ConfigParams.PUBLIC_PATH_ORGBCG_TEXT /
Path(str(self.sampID)) / Path(ConfigParams.LABEL_CHECK_INPUT_JPEAK_FILENAME +
ConfigParams.ENDSWITH_TXT))),
Path(str(self.sampID)))),
"Input_Approximately_Align": str((Path(self.root_path) / ConfigParams.PUBLIC_PATH_LABEL /
Path(str(self.sampID)) / Path(ConfigParams.LABEL_CHECK_APPROXIMATELY_ALIGNINFO_FILENAME +
ConfigParams.ENDSWITH_CSV))),
Path(str(self.sampID)))),
"Save": str((Path(self.root_path) / ConfigParams.PUBLIC_PATH_ORGBCG_TEXT /
Path(str(self.sampID)) / Path(ConfigParams.LABEL_CHECK_SAVE_JPEAK_FILENAME +
ConfigParams.ENDSWITH_TXT)))
Path(str(self.sampID))))
},
"Mode": self.mode
})
@ -103,18 +98,13 @@ class SettingWindow(QMainWindow):
Config.update({
"Path": {
"Input_Signal": str((Path(self.root_path) / ConfigParams.PUBLIC_PATH_PSG_TEXT /
Path(str(self.sampID)) / Path(ConfigParams.LABEL_CHECK_INPUT_ECG_FILENAME +
str(Config["InputConfig"]["Freq"]) +
ConfigParams.ENDSWITH_TXT))),
Path(str(self.sampID)))),
"Input_Peak": str((Path(self.root_path) / ConfigParams.PUBLIC_PATH_PSG_TEXT /
Path(str(self.sampID)) / Path(ConfigParams.LABEL_CHECK_INPUT_RPEAK_FILENAME +
ConfigParams.ENDSWITH_TXT))),
Path(str(self.sampID)))),
"Input_Approximately_Align": str((Path(self.root_path) / ConfigParams.PUBLIC_PATH_LABEL /
Path(str(self.sampID)) / Path(ConfigParams.LABEL_CHECK_APPROXIMATELY_ALIGNINFO_FILENAME +
ConfigParams.ENDSWITH_CSV))),
Path(str(self.sampID)))),
"Save": str((Path(self.root_path) / ConfigParams.PUBLIC_PATH_PSG_TEXT /
Path(str(self.sampID)) / Path(ConfigParams.LABEL_CHECK_SAVE_RPEAK_FILENAME +
ConfigParams.ENDSWITH_TXT)))
Path(str(self.sampID))))
},
"Mode": self.mode
})
@ -173,7 +163,7 @@ class SettingWindow(QMainWindow):
str((Path(self.root_path) /
ConfigParams.PUBLIC_PATH_ORGBCG_TEXT /
Path(str(self.sampID)) /
Path(ConfigParams.LABEL_CHECK_INPUT_BCG_FILENAME +
Path(ConfigParams.BCG_FILTER +
str(self.ui.spinBox_input_freq_signal.value()) +
ConfigParams.ENDSWITH_TXT))))
elif self.mode == "ECG":
@ -181,7 +171,7 @@ class SettingWindow(QMainWindow):
str((Path(self.root_path) /
ConfigParams.PUBLIC_PATH_PSG_TEXT /
Path(str(self.sampID)) /
Path(ConfigParams.LABEL_CHECK_INPUT_ECG_FILENAME +
Path(ConfigParams.ECG_FILTER +
str(self.ui.spinBox_input_freq_signal.value()) +
ConfigParams.ENDSWITH_TXT))))
else:
@ -402,7 +392,7 @@ class MainWindow_label_check(QMainWindow):
def __update_tableWidget_and_info__(self):
if self.data.original_peak is None or self.data.corrected_peak is None:
return False, Constants.LABEL_CHECK_FAILURE_REASON["Peak_Not_Exist"]
return False, Constants.FAILURE_REASON["Data_Not_Exist"]
try:
self.ui.tableWidget_peak_original.setRowCount(len(self.data.original_peak))
@ -457,7 +447,7 @@ class MainWindow_label_check(QMainWindow):
PublicFunc.text_output(self.ui, "(1/6)" + result.info, Constants.TIPS_TYPE_INFO)
# 获取存档
PublicFunc.progressbar_update(self, 2, 6, Constants.LABEL_CHECK_LOADING_ARCHIVE, 20)
PublicFunc.progressbar_update(self, 2, 6, Constants.LOADING_ARCHIVE, 20)
result = self.data.get_archive()
if not result.status:
PublicFunc.text_output(self.ui, "(2/6)" + result.info, Constants.TIPS_TYPE_ERROR)
@ -468,7 +458,7 @@ class MainWindow_label_check(QMainWindow):
PublicFunc.text_output(self.ui, "(2/6)" + result.info, Constants.TIPS_TYPE_INFO)
# 数据预处理
PublicFunc.progressbar_update(self, 3, 6, Constants.LABEL_CHECK_PROCESSING_DATA, 30)
PublicFunc.progressbar_update(self, 3, 6, Constants.PREPROCESSING_DATA, 30)
result = self.data.preprocess()
if not result.status:
PublicFunc.text_output(self.ui, "(3/6)" + result.info, Constants.TIPS_TYPE_ERROR)
@ -889,8 +879,40 @@ class Data:
self.approximately_align_pos = None
def open_file(self):
if Config["Mode"] == "BCG":
signal = ConfigParams.BCG_FILTER
peak = ConfigParams.JPEAK_REVISE
save = ConfigParams.JPEAK_REVISE_CORRECTED
elif Config["Mode"] == "ECG":
signal = ConfigParams.ECG_FILTER
peak = ConfigParams.RPEAK_FINAL
save = ConfigParams.RPEAK_FINAL_CORRECTED
else:
raise ValueError("模式不存在")
if Path(Config["Path"]["Input_Signal"]).is_file():
Config["Path"]["Input_Signal"] = str(Path(Config["Path"]["Input_Signal"]).parent)
if Path(Config["Path"]["Input_Peak"]).is_file():
Config["Path"]["Input_Peak"] = str(Path(Config["Path"]["Input_Peak"]).parent)
if Path(Config["Path"]["Input_Approximately_Align"]).is_file():
Config["Path"]["Input_Approximately_Align"] = str(Path(Config["Path"]["Input_Approximately_Align"]).parent)
if Path(Config["Path"]["Save"]).is_file():
Config["Path"]["Save"] = str(Path(Config["Path"]["Save"]).parent)
result = PublicFunc.examine_file(Config["Path"]["Input_Signal"], signal)
if result.status:
Config["Path"]["Input_Signal"] = result.data["path"]
Config["Path"]["Input_Peak"] = str(
Path(Config["Path"]["Input_Peak"]) / Path(peak + str(Config["InputConfig"]["Freq"]) + ConfigParams.ENDSWITH_TXT))
Config["InputConfig"]["Freq"] = result.data["freq"]
Config["Path"]["Input_Approximately_Align"] = str(
Path(Config["Path"]["Input_Approximately_Align"]) / Path(ConfigParams.APPROXIMATELY_ALIGN_INFO + ConfigParams.ENDSWITH_CSV))
Config["Path"]["Save"] = str(
Path(Config["Path"]["Save"]) / Path(save + str(Config["InputConfig"]["Freq"]) + ConfigParams.ENDSWITH_TXT))
else:
return result
if (not Path(Config["Path"]["Input_Signal"]).exists()) or (not Path(Config["Path"]["Input_Peak"]).exists()):
return Result().failure(info=Constants.INPUT_FAILURE + Constants.LABEL_CHECK_FAILURE_REASON["Data_Path_Not_Exist"])
return Result().failure(info=Constants.INPUT_FAILURE + Constants.FAILURE_REASON["Path_Not_Exist"])
try:
self.raw_data = read_csv(Config["Path"]["Input_Signal"],
@ -900,7 +922,8 @@ class Data:
encoding=ConfigParams.UTF8_ENCODING,
header=None).to_numpy().reshape(-1)
except Exception as e:
return Result().failure(info=Constants.INPUT_FAILURE + Constants.LABEL_CHECK_FAILURE_REASON["Read_Data_Exception"] + "\n" + format_exc())
return Result().failure(info=Constants.INPUT_FAILURE +
Constants.FAILURE_REASON["Open_Data_Exception"] + "\n" + format_exc())
try:
df = read_csv(Config["Path"]["Input_Approximately_Align"])
@ -925,16 +948,16 @@ class Data:
def get_archive(self):
if not Path(Config["Path"]["Save"]).exists():
self.corrected_peak = self.original_peak
return Result().success(info=Constants.LABEL_CHECK_ARCHIVE_NOT_EXIST)
return Result().success(info=Constants.ARCHIVE_NOT_EXIST)
else:
self.corrected_peak = read_csv(Config["Path"]["Save"],
encoding=ConfigParams.UTF8_ENCODING,
header=None).to_numpy().reshape(-1)
return Result().success(info=Constants.LABEL_CHECK_ARCHIVE_EXIST)
return Result().success(info=Constants.ARCHIVE_EXIST)
def preprocess(self):
if self.raw_data is None:
return Result().failure(info=Constants.LABEL_CHECK_PROCESS_FAILURE + Constants.LABEL_CHECK_FAILURE_REASON["Raw_Data_Not_Exist"])
return Result().failure(info=Constants.PREPROCESS_FAILURE + Constants.FAILURE_REASON["Data_Not_Exist"])
try:
if Config["Mode"] == "BCG":
@ -954,18 +977,20 @@ class Data:
self.original_peak_y = [self.processed_data[x] for x in self.original_peak]
self.corrected_peak_y = [self.processed_data[x] for x in self.corrected_peak]
except Exception as e:
return Result().failure(info=Constants.LABEL_CHECK_PROCESS_FAILURE + Constants.LABEL_CHECK_FAILURE_REASON["Filter_Exception"] + "\n" + format_exc())
return Result().failure(info=Constants.PREPROCESS_FAILURE +
Constants.FAILURE_REASON["Preprocess_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.LABEL_CHECK_PROCESS_FINISHED)
return Result().success(info=Constants.PREPROCESS_FINISHED)
def save(self, chunk):
if self.corrected_peak is None:
return Result().failure(info=Constants.SAVING_FAILURE + Constants.LABEL_CHECK_FAILURE_REASON["Peak_Not_Exist"])
return Result().failure(info=Constants.SAVING_FAILURE + Constants.FAILURE_REASON["Data_Not_Exist"])
try:
chunk.to_csv(Config["Path"]["Save"], mode='a', index=False, header=False)
except Exception as e:
return Result().failure(info=Constants.SAVING_FAILURE + Constants.LABEL_CHECK_FAILURE_REASON["Save_Exception"] + "\n" + format_exc())
return Result().failure(info=Constants.SAVING_FAILURE +
Constants.FAILURE_REASON["Save_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.SAVING_FINISHED)