Compare commits

...

3 Commits

Author SHA1 Message Date
marques
446ab9810e 添加体动事件
标题添加序号
2022-04-21 10:46:41 +08:00
marques
7b2a1b20a9 添加体动事件 2022-04-21 10:43:31 +08:00
marques
f04f8a9e03 添加命令行事件选择 2022-04-21 09:53:27 +08:00
2 changed files with 70 additions and 14 deletions

View File

@ -24,12 +24,14 @@ 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-----一般不用修改------
# 要遍历的事件
# 可选一个或多个 "Hypopnea" "Central apnea" "Obstructive apnea" "Mixed apnea"
focus_event_list = ["Obstructive apnea"]
focus_event_list = ["OSA"]
# 信号显示事件前多少秒
front_add_second = 60
# 信号显示事件后多少秒
@ -43,7 +45,8 @@ start_bcg_index = 0
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--sampNo', nargs='?', type=int, default=sampNo, help='samp number')
parser.add_argument('-e', '--event', nargs='*', type=list, default='OSA', choices=['OSA', 'CSA', 'MAS', 'HPY'],
parser.add_argument('-e', '--event', nargs='*', type=str, default=focus_event_list,
choices=['OSA', 'CSA', 'MSA', 'HPY'],
help='focus_event')
parser.add_argument('-i', '--start_bcg_index', nargs='?', type=int, default=start_bcg_index, help='samp number')
parser.add_argument('-f', '--front_add_second', nargs='?', type=int, default=front_add_second,
@ -52,6 +55,16 @@ if __name__ == '__main__':
help='Add x second signal after the event')
opt = parser.parse_args()
focus_event_list = []
if 'OSA' in opt.focus_event_list:
focus_event_list.append("Obstructive apnea")
if 'CSA' in opt.focus_event_list:
focus_event_list.append("Central apnea")
if 'MSA' in opt.focus_event_list:
focus_event_list.append("Mixed apnea")
if 'HPY' in opt.focus_event_list:
focus_event_list.append("Hypopnea")
qualityRelabel = Quality_Relabel(all_path=all_path, sampNo=opt.sampNo, frequency=frequency,
bcg_frequency=bcg_frequency,
focus_event_list=focus_event_list)

View File

@ -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)}"
@ -312,7 +342,8 @@ class Quality_Relabel:
# 绘制 Flow2
plt.subplot(gs[1])
self.plt_channel(plt_=plt, SP=ecg_SP, EP=ecg_EP, channel="Flow P",
title=f"PSG sampNo:{self.sampNo} Epoch:{one_ecg_data['Epoch']} Duration:{ecg_duration}")
title=f"PSG sampNo:{self.sampNo} Index:{ecg_index}/{len(self.ecg_event_label_df)} "
f"Epoch:{one_ecg_data['Epoch']} Duration:{ecg_duration}")
plt.subplot(gs[2])
self.plt_channel(plt_=plt, SP=ecg_SP, EP=ecg_EP, channel="Effort Tho")
@ -324,12 +355,17 @@ 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}",
title=f"心晓 sampNo:{self.sampNo} Index:{bcg_index}/{len(self.bcg_event_label_df)} "
f"Epoch:{one_bcg_data['Epoch']} Duration:{bcg_duration}",
)
figManager = plt.get_current_fig_manager()
@ -353,6 +389,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 +399,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 +414,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 +447,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()