From 7b2a1b20a9bac3a7abca6d97b06570e64f78f69a Mon Sep 17 00:00:00 2001 From: marques <20172333133@m.scnu.edu.cn> Date: Thu, 21 Apr 2022 10:43:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BD=93=E5=8A=A8=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Main_Quality_Relabel.py | 4 ++- utils/Quality_Relabel.py | 59 ++++++++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/Main_Quality_Relabel.py b/Main_Quality_Relabel.py index 0191f79..448d8b0 100644 --- a/Main_Quality_Relabel.py +++ b/Main_Quality_Relabel.py @@ -24,7 +24,9 @@ PSG_Data_Path = Path("I:/CXH/Quality_Relabel/Data/PSG/") PSG_Label_Path = Path("I:/CXH/Quality_Relabel/Data/PSG_label/") BCG_Data_Path = Path("I:/CXH/Quality_Relabel/Data/BCG/") BCG_Label_Path = Path("I:/CXH/Quality_Relabel/Data/BCG_label/") -all_path = [PSG_Data_Path, PSG_Label_Path, BCG_Data_Path, BCG_Label_Path] +Artifact_Label_Path = Path("I:/CXH/Quality_Relabel/Data/Artifact_label/") +Artifact_Offset_Path = Path("I:/CXH/Quality_Relabel/Data/Artifact_label/20220421Artifact_offset_value.xlsx") +all_path = [PSG_Data_Path, PSG_Label_Path, BCG_Data_Path, BCG_Label_Path, Artifact_Label_Path, Artifact_Offset_Path] # end-----一般不用修改------ # 要遍历的事件 diff --git a/utils/Quality_Relabel.py b/utils/Quality_Relabel.py index 62de37d..d4276f8 100644 --- a/utils/Quality_Relabel.py +++ b/utils/Quality_Relabel.py @@ -63,8 +63,14 @@ class Quality_Relabel: # 3 ɫ # 4 ɫ # 5 ɫ ѪͶ½ - color_cycle = ["black", "pink", "blue", "red", "grey", "green"] - assert len(color_cycle) == len(base_event) + 1, "¼ɫһ" + # 6 ɫ 嶯 + # 7 ɫ С嶯 + # 8 ɫ + # 9 ɫ 嶯 + # 10 ɫ ЧƬ + color_cycle = ["black", "pink", "blue", "red", "silver", "green", "orange", "orange", "orange", "orange", "orange"] + + # assert len(color_cycle) == len(base_event) + 1, "¼ɫһ" def __init__(self, all_path: List, sampNo: int, frequency: int = 100, bcg_frequency: int = 1000, channel_list: List[str] = ['Effort Tho', 'Effort Abd', 'SpO2', 'Flow Patient', 'Flow Patient'], @@ -77,7 +83,13 @@ class Quality_Relabel: :param channel_list: ʾͨ :param focus_event_list: עͣ¼ """ - self.PSG_Data_Path, self.PSG_Label_Path, self.BCG_Data_Path, self.BCG_Label_Path = all_path + self.PSG_Data_Path = all_path[0] + self.PSG_Label_Path = all_path[1] + self.BCG_Data_Path = all_path[2] + self.BCG_Label_Path = all_path[3] + self.Artifact_Label_Path = all_path[4] + self.Artifact_Offset_Path = all_path[5] + self.sampNo = sampNo self.channel_list = channel_list self.focus_event_list = focus_event_list @@ -90,6 +102,7 @@ class Quality_Relabel: self.ecg_event_label = None self.bcg_event_label = None self.spo2_event_label = None + self.artifact_event_label = None # עͣ¼б self.ecg_event_label_filtered_df = None @@ -251,6 +264,23 @@ class Quality_Relabel: elif one_data["Event type"] == "Mixed apnea": self.bcg_event_label[SP:EP] = 4 + def read_artifact_label(self): + all_offset_length = pd.read_excel(self.Artifact_Offset_Path) + offset_length = all_offset_length[all_offset_length['ݱ'] == self.sampNo]['from_code'] + artifact_label_path = self.Artifact_Label_Path / f"{self.sampNo}samp" / "Artifact_a.txt" + artifact_label = pd.read_csv(artifact_label_path, header=None).to_numpy().reshape(-1, 4) + + self.artifact_event_label = np.zeros(len(self.bcg_event_label)) + + for i, artifact_type, SP, EP in artifact_label: + SP = (SP + offset_length) // (1000 // self.frequency) + EP = (SP + offset_length) // (1000 // self.frequency) + artifact_label += 5 + if EP > len(self.bcg_event_label): + continue + + self.artifact_event_label[SP:EP] = artifact_label + # assert len(self.ecg_event_label_filtered_df) == len(self.bcg_event_label_filtered_df), \ # f"PSG¼һ, PSG¼{len(self.ecg_event_label_filtered_df)}, # ¼{len(self.bcg_event_label_filtered_df)}" @@ -324,10 +354,14 @@ class Quality_Relabel: self.plt_channel(plt_=plt, SP=ecg_SP, EP=ecg_EP, channel="SpO2", event_code=[5]) plt.subplot(gs[5]) - self.plt_channel(plt_=plt, SP=bcg_SP, EP=bcg_EP, channel="orgdata", event_show_under=False) + self.plt_channel(plt_=plt, SP=bcg_SP, EP=bcg_EP, channel="orgdata", + event_code=[1, 2, 3, 4, 6, 7, 8, 9, 10], + event_show_under=False) plt.subplot(gs[6]) - self.plt_channel(plt_=plt, SP=bcg_SP, EP=bcg_EP, channel="0.7lowpass_resp", event_show_under=False, + self.plt_channel(plt_=plt, SP=bcg_SP, EP=bcg_EP, channel="0.7lowpass_resp", + event_code=[1, 2, 3, 4, 6, 7, 8, 9, 10], + event_show_under=False, ax_bottom=True, title=f" sampNo:{self.sampNo} Epoch:{one_bcg_data['Epoch']} Duration:{bcg_duration}", ) @@ -353,6 +387,7 @@ class Quality_Relabel: :param title: ʾ :return: """ + linestyle = "-" SP = 0 if SP < 0 else SP plt_.plot(np.linspace(SP, EP, (EP - SP) * self.frequency), self.signal_select[channel][SP * self.frequency:EP * self.frequency], label=channel, @@ -362,7 +397,11 @@ class Quality_Relabel: if channel == "SpO2": mask = self.spo2_event_label[SP * self.frequency:EP * self.frequency] == j elif channel == "orgdata" or channel == "0.7lowpass_resp": - mask = self.bcg_event_label[SP * self.frequency:EP * self.frequency] == j + if j < 4: + mask = self.bcg_event_label[SP * self.frequency:EP * self.frequency] == j + else: + mask = self.artifact_event_label[SP * self.frequency:EP * self.frequency] == j + linestyle = "--" else: mask = self.ecg_event_label[SP * self.frequency:EP * self.frequency] == j @@ -373,8 +412,10 @@ class Quality_Relabel: np.place(y, y == 0, np.nan) else: y = (self.signal_select[channel][SP * self.frequency:EP * self.frequency] * mask).astype('float') + np.place(y, y == 0, np.nan) - plt_.plot(np.linspace(SP, EP, (EP - SP) * self.frequency), y, color=self.color_cycle[j]) + plt_.plot(np.linspace(SP, EP, (EP - SP) * self.frequency), y, color=self.color_cycle[j], + linestyle=linestyle) plt_.legend(loc=1) if title is not None: @@ -404,6 +445,6 @@ if __name__ == '__main__': PSG_Label_Path = "../Data/PSG_label/" BCG_Data_Path = "../Data/BCG/" BCG_Label_Path = "../Data/BCG_label/" - all_path = [PSG_Data_Path, PSG_Label_Path, BCG_Data_Path, BCG_Label_Path] - prepareData = Quality_Relabel(all_path, 670) + all_paths = [PSG_Data_Path, PSG_Label_Path, BCG_Data_Path, BCG_Label_Path] + prepareData = Quality_Relabel(all_paths, 670) prepareData.show_all_event()