SA_Label/PSG_label_2_BCG_label.py
Yorusora 7b9ec0a4ac 新增功能:
1、能够同时显示打标后和打标前的标签起始位置和终止位置
2、打标前后的绘图的信号长度都将使用之前的长度,即打标后虽然会同时显示新的标签的所在位置,但不影响查看信号时横坐标的范围
3、当打标长度小于10秒时,将会有弹窗提示
4、根据手册给出的统一备注,新增了几个快捷备注输入的按钮
5、当程序绘制已经被打标过的事件时,左边打标操作区域将会显示打标之后的参数,而不是打标前的原始标签
2025-01-11 11:14:58 +08:00

46 lines
1.8 KiB
Python

import os
import pandas as pd
import pyedflib
from pathlib import Path
def get_time_to_seconds(time_str):
h, m, s = map(int, time_str.split(":"))
return h * 3600 + m * 60 + s
base_event = ["Hypopnea", "Central apnea", "Obstructive apnea", "Mixed apnea"]
# 输入设置
sampID = 888
dir_path = r"E:\data_annotation\6SleepApnea_annotation_GUI_demo\data"
# 数据路径设置
PSG_Data_Path = Path(os.path.join(dir_path, "PSG"))
PSG_Label_Path = Path(os.path.join(dir_path, "PSG_label"))
BCG_Data_Path = Path(os.path.join(dir_path, "BCG"))
BCG_Label_Path = Path(os.path.join(dir_path, "BCG_label"))
# 读取PSG标签
df_PSG_label = pd.read_csv(PSG_Label_Path / (f"export" + str(sampID) + ".csv"), encoding="gbk")
df_PSG_label = df_PSG_label.loc[:, ~df_PSG_label.columns.str.contains('^Unnamed')]
df_PSG_label = df_PSG_label[df_PSG_label["Event type"].isin(base_event)]
df_PSG_label['Duration'] = df_PSG_label['Duration'].str.replace(r' \(.*?\)', '', regex=True)
# 读取EDF文件
edf_File = pyedflib.EdfReader(str(PSG_Data_Path / f"A{str(sampID).rjust(7, '0')}.edf"))
# 获取PSG记录开始时间
start_time = str(edf_File.getStartdatetime()).split(" ")[1]
start_time_abs = get_time_to_seconds(start_time)
# 计算起始时间秒数和终止时间秒数
df_PSG_label['Start'] = (df_PSG_label['Time'].apply(get_time_to_seconds) - start_time_abs).apply(lambda x: x + 24 * 3600 if x < 0 else x).astype(int)
df_PSG_label['End'] = df_PSG_label['Start'] + df_PSG_label['Duration'].astype(float).round(0).astype(int)
# 打印结果
print(df_PSG_label)
# 写入csv文件
df_PSG_label.to_csv(r"E:\data_annotation\6SleepApnea_annotation_GUI_demo\data\BCG_label\export" + str(sampID) + "_all.csv", index=False, encoding="gbk")
# 打印结果
print("sampID_" + str(sampID) + "写入csv成功")