优化<数据粗同步>的代码

This commit is contained in:
Yorusora
2025-05-20 21:27:31 +08:00
parent db07be0ca7
commit 497e1ced27
3 changed files with 107 additions and 173 deletions

View File

@ -115,7 +115,7 @@ class SettingWindow(QMainWindow):
"Input_Abd": str((Path(self.root_path) / ConfigParams.PUBLIC_PATH_PSG_TEXT /
Path(str(self.sampID)))),
"Save": str((Path(self.root_path) / ConfigParams.PUBLIC_PATH_PSG_TEXT /
Path(str(self.sampID)) / Path(ConfigParams.APPROXIMATELY_ALIGN_SAVE_FILENAME +
Path(str(self.sampID)) / Path(ConfigParams.APPROXIMATELY_ALIGN_INFO +
ConfigParams.ENDSWITH_CSV)))
},
"orgBcgConfig": {},
@ -171,28 +171,28 @@ class SettingWindow(QMainWindow):
str((Path(self.root_path) /
ConfigParams.PUBLIC_PATH_ORGBCG_TEXT /
Path(str(self.sampID)) /
Path(ConfigParams.APPROXIMATELY_ALIGN_INPUT_ORGBCG_FILENAME +
Path(ConfigParams.ORGBCG_RAW +
str(self.ui.spinBox_input_orgBcg_freq.value()) +
ConfigParams.ENDSWITH_TXT))))
self.ui.plainTextEdit_file_path_input_Tho.setPlainText(
str((Path(self.root_path) /
ConfigParams.PUBLIC_PATH_PSG_TEXT /
Path(str(self.sampID)) /
Path(ConfigParams.APPROXIMATELY_ALIGN_INPUT_THO_FILENAME +
Path(ConfigParams.THO_RAW +
str(self.ui.spinBox_input_Tho_freq.value()) +
ConfigParams.ENDSWITH_TXT))))
self.ui.plainTextEdit_file_path_input_Abd.setPlainText(
str((Path(self.root_path) /
ConfigParams.PUBLIC_PATH_PSG_TEXT /
Path(str(self.sampID)) /
Path(ConfigParams.APPROXIMATELY_ALIGN_INPUT_ABD_FILENAME +
Path(ConfigParams.ABD_RAW +
str(self.ui.spinBox_input_Abd_freq.value()) +
ConfigParams.ENDSWITH_TXT))))
self.ui.plainTextEdit_file_path_save.setPlainText(
str((Path(self.root_path) /
ConfigParams.PUBLIC_PATH_ORGBCG_TEXT /
Path(str(self.sampID)) /
Path(ConfigParams.APPROXIMATELY_ALIGN_SAVE_FILENAME +
Path(ConfigParams.APPROXIMATELY_ALIGN_INFO +
ConfigParams.ENDSWITH_CSV))))
@ -928,87 +928,29 @@ class Data:
def open_file(self):
if Path(Config["Path"]["Input_orgBcg"]).is_file():
Config["Path"]["Input_orgBcg"] = str(Path(Config["Path"]["Input_orgBcg"]).parent)
if not Path(Config["Path"]["Input_orgBcg"]).exists():
return Result().failure(
info=Constants.INPUT_FAILURE + "orgBcg: " + Config["Path"]["Input_orgBcg"] + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Data_Path_Not_Exist"])
temp_orgBcg_path = list(
Path(Config["Path"]["Input_orgBcg"]).glob(f"{ConfigParams.APPROXIMATELY_ALIGN_INPUT_ORGBCG_FILENAME}*"))
if len(temp_orgBcg_path) == 0:
return Result().failure(
info=Constants.INPUT_FAILURE + "orgBcg: " + Config["Path"]["Input_orgBcg"] + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Data_File_Not_Exist"])
elif len(temp_orgBcg_path) > 1:
return Result().failure(
info=Constants.INPUT_FAILURE + "orgBcg: " + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Data_File_More_Than_One"])
else:
orgBcg_path = temp_orgBcg_path[0]
Config["Path"]["Input_orgBcg"] = str(orgBcg_path)
orgBcg_freq = orgBcg_path.stem.split("_")[-1]
# 验证是否为整数或是否存在
if not orgBcg_freq.isdigit():
return Result().failure(
info=Constants.INPUT_FAILURE + "orgBcg: " + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Data_Frequency_Not_In_Filename"])
Config["InputConfig"]["orgBcgFreq"] = int(orgBcg_freq)
if Path(Config["Path"]["Input_Tho"]).is_file():
Config["Path"]["Input_Tho"] = str(Path(Config["Path"]["Input_Tho"]).parent)
if not Path(Config["Path"]["Input_Tho"]).exists():
return Result().failure(
info=Constants.INPUT_FAILURE + "Tho: " + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Data_Path_Not_Exist"])
temp_Tho_path = list(
Path(Config["Path"]["Input_Tho"]).glob(f"{ConfigParams.APPROXIMATELY_ALIGN_INPUT_THO_FILENAME}*"))
if len(temp_Tho_path) == 0:
return Result().failure(
info=Constants.INPUT_FAILURE + "Tho: " + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Data_File_Not_Exist"])
elif len(temp_Tho_path) > 1:
return Result().failure(
info=Constants.INPUT_FAILURE + "Tho: " + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Data_File_More_Than_One"])
else:
tho_path = temp_Tho_path[0]
Config["Path"]["Input_Tho"] = str(tho_path)
tho_freq = tho_path.stem.split("_")[-1]
# 验证是否为整数或是否存在
if not tho_freq.isdigit():
return Result().failure(
info=Constants.INPUT_FAILURE + "Tho: " + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Data_Frequency_Not_In_Filename"])
Config["InputConfig"]["ThoFreq"] = int(tho_freq)
if Path(Config["Path"]["Input_Abd"]).is_file():
Config["Path"]["Input_Abd"] = str(Path(Config["Path"]["Input_Abd"]).parent)
if not Path(Config["Path"]["Input_Abd"]).exists():
return Result().failure(
info=Constants.INPUT_FAILURE + "Abd: " + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Data_Path_Not_Exist"])
temp_Abd_path = list(
Path(Config["Path"]["Input_Abd"]).glob(f"{ConfigParams.APPROXIMATELY_ALIGN_INPUT_ABD_FILENAME}*"))
if len(temp_Abd_path) == 0:
return Result().failure(
info=Constants.INPUT_FAILURE + "Abd: " + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Data_File_Not_Exist"])
elif len(temp_Abd_path) > 1:
return Result().failure(
info=Constants.INPUT_FAILURE + "Abd: " + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Data_File_More_Than_One"])
result = PublicFunc.examine_file(Config["Path"]["Input_orgBcg"], ConfigParams.ORGBCG_RAW)
if result.status:
Config["Path"]["Input_orgBcg"] = result.data["path"]
Config["InputConfig"]["orgBcgFreq"] = result.data["freq"]
else:
abd_path = temp_Abd_path[0]
Config["Path"]["Input_Abd"] = str(abd_path)
abd_freq = abd_path.stem.split("_")[-1]
# 验证是否为整数或是否存在
if not abd_freq.isdigit():
return Result().failure(
info=Constants.INPUT_FAILURE + "Abd: " + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Data_Frequency_Not_In_Filename"])
Config["InputConfig"]["AbdFreq"] = int(abd_freq)
return result
result = PublicFunc.examine_file(Config["Path"]["Input_Tho"], ConfigParams.THO_RAW)
if result.status:
Config["Path"]["Input_Tho"] = result.data["path"]
Config["InputConfig"]["ThoFreq"] = result.data["freq"]
else:
return result
result = PublicFunc.examine_file(Config["Path"]["Input_Abd"], ConfigParams.ABD_RAW)
if result.status:
Config["Path"]["Input_Abd"] = result.data["path"]
Config["InputConfig"]["AbdFreq"] = result.data["freq"]
else:
return result
try:
self.raw_orgBcg = read_csv(Config["Path"]["Input_orgBcg"],
@ -1021,8 +963,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.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Read_Data_Exception"] + "\n" + format_exc())
return Result().failure(info=Constants.INPUT_FAILURE + Constants.FAILURE_REASON[
"Open_Data_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.INPUT_FINISHED)
@ -1034,7 +976,7 @@ class Data:
df = DataFrame({"pos": [pos], "epoch": [epoch], "ApplyFrequency": [ApplyFrequency]})
df.to_csv(Path(Config["Path"]["Save"]), mode="w", header=True, index=False)
except Exception as e:
return Result().failure(info=Constants.SAVING_FAILURE + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
return Result().failure(info=Constants.SAVING_FAILURE + Constants.FAILURE_REASON[
"Save_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.SAVING_FINISHED)
@ -1043,8 +985,8 @@ class Data:
# 仅重采样
if self.raw_orgBcg is None or self.raw_Tho is None or self.raw_Abd is None:
return Result().failure(
info=Constants.APPROXIMATELY_ONLY_ALIGN_RESAMPLE_FAILURE + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Raw_Data_Not_Exist"])
info=Constants.APPROXIMATELY_ONLY_ALIGN_RESAMPLE_FAILURE + Constants.FAILURE_REASON[
"Data_Not_Exist"])
try:
# 按照秒数进行截断
self.raw_orgBcg = self.raw_orgBcg[:int(Config["orgBcg_seconds"] * Config["InputConfig"]["orgBcgFreq"])]
@ -1057,7 +999,7 @@ class Data:
self.processed_Abd = resample(self.raw_Abd, int(Config["PSG_seconds"] * Config["ApplyFrequency"]))
except Exception as e:
return Result().failure(
info=Constants.APPROXIMATELY_ONLY_ALIGN_RESAMPLE_FAILURE + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
info=Constants.APPROXIMATELY_ONLY_ALIGN_RESAMPLE_FAILURE + Constants.FAILURE_REASON[
"Only_Resample_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.APPROXIMATELY_ONLY_ALIGN_RESAMPLE_FINISHED)
@ -1072,8 +1014,8 @@ class Data:
if self.raw_orgBcg is None or self.raw_Tho is None or self.raw_Abd is None:
return Result().failure(
info=Constants.APPROXIMATELY_ONLY_ALIGN_RESAMPLE_FAILURE + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Raw_Data_Not_Exist"])
info=Constants.APPROXIMATELY_ONLY_ALIGN_RESAMPLE_FAILURE + Constants.FAILURE_REASON[
"Data_Not_Exist"])
try:
# 滤波
self.processed_orgBcg = butter_bandpass_filter(
@ -1093,7 +1035,7 @@ class Data:
Config["Filter"]["BandPassOrder"])
except Exception as e:
return Result().failure(
info=Constants.APPROXIMATELY_RESP_GET_FAILURE + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
info=Constants.APPROXIMATELY_RESP_GET_FAILURE + Constants.FAILURE_REASON[
"Resp_Get_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.APPROXIMATELY_RESP_GET_FINISHED)
@ -1118,7 +1060,7 @@ class Data:
except Exception as e:
return Result().failure(
info=Constants.APPROXIMATELY_PRE_ALIGN_RESAMPLE_FAILURE + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
info=Constants.APPROXIMATELY_PRE_ALIGN_RESAMPLE_FAILURE + Constants.FAILURE_REASON[
"Pre_Resample_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.APPROXIMATELY_PRE_ALIGN_RESAMPLE_FINISHED)
@ -1138,7 +1080,7 @@ class Data:
self.processed_orgBcg, ones(int(4 * temp_frequency)) / int(4 * temp_frequency), mode='same')
except Exception as e:
return Result().failure(
info=Constants.APPROXIMATELY_DELETE_BASE_FAILURE + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
info=Constants.APPROXIMATELY_DELETE_BASE_FAILURE + Constants.FAILURE_REASON[
"Delete_Base_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.APPROXIMATELY_DELETE_BASE_FINISHED)
@ -1155,7 +1097,7 @@ class Data:
self.processed_orgBcg)
except Exception as e:
return Result().failure(
info=Constants.APPROXIMATELY_STANDARDIZE_FAILURE + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
info=Constants.APPROXIMATELY_STANDARDIZE_FAILURE + Constants.FAILURE_REASON[
"Standardize_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.APPROXIMATELY_STANDARDIZE_FINISHED)
@ -1170,7 +1112,7 @@ class Data:
self.processed_downsample_orgBcg = self.processed_orgBcg[::int(temp_frequency / Config["ApplyFrequency"])]
except Exception as e:
return Result().failure(
info=Constants.APPROXIMATELY_ALIGN_RESAMPLE_FAILURE + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
info=Constants.APPROXIMATELY_ALIGN_RESAMPLE_FAILURE + Constants.FAILURE_REASON[
"Resample_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.APPROXIMATELY_ALIGN_RESAMPLE_FINISHED)
@ -1197,7 +1139,7 @@ class Data:
result = {"tho_relate": tho_relate, "tho_relate2": tho_relate2}
except Exception as e:
return Result().failure(info=Constants.APPROXIMATELY_CORRELATION_CALCULATE1_FAILURE +
Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
Constants.FAILURE_REASON[
"Calculate_Correlation1_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.APPROXIMATELY_CORRELATION_CALCULATE1_FINISHED, data=result)
@ -1222,7 +1164,7 @@ class Data:
result = {"abd_relate": abd_relate, "abd_relate2": abd_relate2}
except Exception as e:
return Result().failure(info=Constants.APPROXIMATELY_CORRELATION_CALCULATE2_FAILURE +
Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
Constants.FAILURE_REASON[
"Calculate_Correlation2_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.APPROXIMATELY_CORRELATION_CALCULATE2_FINISHED, data=result)
@ -1241,7 +1183,7 @@ class Data:
result = {"tho_max": tho_max, "tho_max2": tho_max2, "abd_max": abd_max, "abd_max2": abd_max2, "bias": bias}
except Exception as e:
return Result().failure(info=Constants.APPROXIMATELY_MAXVALUE_POS_CALCULATE_FAILURE +
Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
Constants.FAILURE_REASON[
"Calculate_Maxvalue_Pos_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.APPROXIMATELY_MAXVALUE_POS_CALCULATE_FINISHED, data=result)
@ -1257,7 +1199,7 @@ class Data:
result = {"epoch_min": epoch_min, "epoch_max": epoch_max}
except Exception as e:
return Result().failure(
info=Constants.APPROXIMATELY_EPOCH_GET_FAILURE + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
info=Constants.APPROXIMATELY_EPOCH_GET_FAILURE + Constants.FAILURE_REASON[
"Get_Epoch_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.APPROXIMATELY_EPOCH_GET_FINISHED, data=result)
@ -1307,7 +1249,7 @@ class Data:
except Exception as e:
return Result().failure(
info=Constants.APPROXIMATELY_EPOCH_GET_FAILURE + Constants.APPROXIMATELY_ALIGN_FAILURE_REASON[
"Get_Corr_By_Epoch"] + "\n" + format_exc())
info=Constants.APPROXIMATELY_EPOCH_GET_FAILURE + Constants.FAILURE_REASON[
"Get_Corr_By_Epoch_Exception"] + "\n" + format_exc())
return Result().success(info=Constants.APPROXIMATELY_EPOCH_GET_FINISHED, data=result)