心血管疾病是一种常见病,严重影响人们的健康及日常生活。 近年来随着人们生活习惯的不断变化,心血管疾病对人们影响愈加明显,发病率呈现出逐年攀升的趋势,心血管疾病是中国城乡居民死亡的首要原因。心电图ECG已被广泛用于研究心跳活动。作为一种无创的方法,ECG的相关研究为了解心脏的功能提供了便利。对心电图进行有效分析可以为心血管疾病的诊断和防治提供丰富的信息,进而大大减缓心血管疾病对人们生活的影响。
心电信号分割是心电图解读的第一步,通过心电信号分割,有助于进一步对运动员心脏健康状况进行分析,进而减少运动员受伤、致病及运动中猝死的风险。
from typing import Union,List, Tuple
# get signal with wfdb library
# using an index
def get_signal(index:int, as_p_signal:bool=True) -> Union[wfdb.Record, np.ndarray]:
record = wfdb.rdrecord(DATA_FOLDER + "/" + str(index))
assert type(record) is wfdb.Record
if as_p_signal:
assert type(record.p_signal) is np.ndarray
return record.p_signal
return record
# get annotations given the ecg lead
def get_annotations(index:int, lead, as_sample=True) -> Union[wfdb.Annotation, np.ndarray]:
annotations = wfdb.rdann(DATA_FOLDER +"/" + str(index), extension = lead)
if as_sample:
return np.array(annotations.sample)
return annotations
# get a full EGC with 12 leads
def get_full_ecg(index:int):
signal = get_signal(index)
annotations = [
get_annotations(index, lead) for lead in LEADS
]
return signal, annotations
def get_single_lead_ecg(index, lead) -> Tuple[np.ndarray, np.ndarray]:
"""
return and ecg signal and its annotations
both as ndarray
"""
signal = get_signal(index)
assert type(signal) is np.ndarray
signal = signal[:, LEADS.index(lead)]
samples = get_annotations(index, lead)
assert type(samples) is np.ndarray
return signal, samples
def get_annotations_symbols(index, lead):
ann = get_annotations(index, lead, as_sample=False)
return ann.symbol
def paired_annotation_sample_and_symbol(index, lead):
annotations_symbols = get_annotations_symbols(index, lead)
annotations_sample = get_annotations(index, lead)
return zip(annotations_sample, annotations_symbols)
def get_single_lead_ecg_with_symbols(index, lead):
"""
return and ecg signal and its annotations
both as ndarray
"""
signal = get_signal(index)
assert type(signal) is np.ndarray
signal = signal[:, LEADS.index(lead)]
data = paired_annotation_sample_and_symbol(index, lead)
return signal, np.array(list(data))
# plot single lead ecg with annotations
def plot_single_lead_ecg(index, lead):
signal, samples