def get_annotation(sample, ann_type, ann_fs, start, end): try: annotation = wfdb.rdann(sample, ann_type, sampfrom=start*ann_fs, sampto=end*ann_fs) except Exception as e: annotation = wfdb.Annotation(sample, ann_type, [], []) print("Error getting annotation for sample ", sample, ann_type, e) return annotation
def get_ecg_data(datfile): ## convert .dat/q1c to numpy arrays recordname=os.path.basename(datfile).split(".dat")[0] recordpath=os.path.dirname(datfile) cwd=os.getcwd() os.chdir(recordpath) ## somehow it only works if you chdir. annotator='q1c' annotation = wfdb.rdann(recordname, extension=annotator, sampfrom=0,sampto = None, pbdir=None) Lstannot=list(zip(annotation.sample,annotation.symbol,annotation.aux_note)) FirstLstannot=min( i[0] for i in Lstannot) LastLstannot=max( i[0] for i in Lstannot)-1 print("first-last annotation:", FirstLstannot,LastLstannot) record = wfdb.rdsamp(recordname, sampfrom=FirstLstannot,sampto = LastLstannot) #wfdb.showanncodes() annotation = wfdb.rdann(recordname, annotator, sampfrom=FirstLstannot,sampto = LastLstannot) ## get annotation between first and last. annotation2 = wfdb.Annotation(recordname='sel32', extension='niek', sample=(annotation.sample-FirstLstannot), symbol = annotation.symbol, aux_note=annotation.aux_note) Vctrecord=np.transpose(record.p_signals) VctAnnotationHot=np.zeros( (6,len(Vctrecord[1])), dtype=np.int) VctAnnotationHot[5]=1 ## inverse of the others #print("ecg, 2 lead of shape" , Vctrecord.shape) #print("VctAnnotationHot of shape" , VctAnnotationHot.shape) #print('plotting extracted signal with annotation') #wfdb.plotrec(record, annotation=annotation2, title='Record 100 from MIT-BIH Arrhythmia Database', timeunits = 'seconds') VctAnnotations=list(zip(annotation2.sample,annotation2.symbol)) ## zip coordinates + annotations (N),(t) etc) #print(VctAnnotations) for i in range(len(VctAnnotations)): #print(VctAnnotations[i]) # Print to display annotations of an ecg try: if VctAnnotations[i][1]=="p": if VctAnnotations[i-1][1]=="(": pstart=VctAnnotations[i-1][0] if VctAnnotations[i+1][1]==")": pend=VctAnnotations[i+1][0] if VctAnnotations[i+3][1]=="N": rpos=VctAnnotations[i+3][0] if VctAnnotations[i+2][1]=="(": qpos=VctAnnotations[i+2][0] if VctAnnotations[i+4][1]==")": spos=VctAnnotations[i+4][0] for ii in range(0,8): ## search for t (sometimes the "(" for the t is missing ) if VctAnnotations[i+ii][1]=="t": tpos=VctAnnotations[i+ii][0] if VctAnnotations[i+ii+1][1]==")": tendpos=VctAnnotations[i+ii+1][0] # #print(ppos,qpos,rpos,spos,tendpos) VctAnnotationHot[0][pstart:pend]=1 #P segment VctAnnotationHot[1][pend:qpos]=1 #part "nothing" between P and Q, previously left unnanotated, but categorical probably can't deal with that VctAnnotationHot[2][qpos:rpos]=1 #QR VctAnnotationHot[3][rpos:spos]=1 #RS VctAnnotationHot[4][spos:tendpos]=1 #ST (from end of S to end of T) VctAnnotationHot[5][pstart:tendpos]=0 #tendpos:pstart becomes 1, because it is inverted above except IndexError: pass Vctrecord=np.transpose(Vctrecord) # transpose to (timesteps,feat) VctAnnotationHot=np.transpose(VctAnnotationHot) os.chdir(cwd) return Vctrecord, VctAnnotationHot
def my_get_ecg_data(datfile): ## convert .dat/q1c to numpy arrays recordname = os.path.basename(datfile).split(".dat")[0] recordpath = os.path.dirname(datfile) cwd = os.getcwd() os.chdir(recordpath) ## somehow it only works if you chdir. annotator = annot_type annotation = wfdb.rdann(recordname, extension=annotator, sampfrom=0, sampto=None, pb_dir=None) # read annotation Lstannot = list(zip(annotation.sample, annotation.symbol, annotation.aux_note)) FirstLstannot = min(i[0] for i in Lstannot) # annot start LastLstannot = max(i[0] for i in Lstannot) - 1 # annot end print("first-last annotation:", FirstLstannot, LastLstannot) record = wfdb.rdsamp(recordname, sampfrom=FirstLstannot, sampto=LastLstannot) # read signal annotation = wfdb.rdann(recordname, annotator, sampfrom=FirstLstannot, sampto=LastLstannot) ## get annotation between first and last. # this make annotation from 0 to end annotation2 = wfdb.Annotation(record_name=recordname, extension=annot_type, sample=(annotation.sample - FirstLstannot), symbol=annotation.symbol, aux_note=annotation.aux_note) Vctrecord = np.transpose(record[0]) VctAnnotationHot = np.zeros((6, len(Vctrecord[1])), dtype=np.int) # 6 annotation type: 0: P, 1: PQ, 2: QR, 3: RS, 4: ST, 5: ISO (TP) #VctAnnotationHot[3] = 1 ## inverse of the others # print("ecg, 2 lead of shape" , Vctrecord.shape) # print("VctAnnotationHot of shape" , VctAnnotationHot.shape) # print('plotting extracted signal with annotation') # wfdb.plotrec(record, annotation=annotation2, title='Record 100 from MIT-BIH Arrhythmia Database', timeunits = 'seconds') VctAnnotations = list(zip(annotation2.sample, annotation2.symbol)) ## zip coordinates + annotations (N),(t) etc) # print(VctAnnotations) annsamp = [] anntype = [] for i in range(len(VctAnnotations)): annsamp.append(VctAnnotations[i][0]) anntype.append(VctAnnotations[i][1]) length = len(annsamp) begin = -1 end = -1 s = 'none' states = {'N': 0, 'st': 1, 't': 2, 'iso': 3, 'p': 4, 'pq': 5} annot_expand = -1 * np.ones(length) for j, samp in enumerate(annsamp): if anntype[j] == '(': begin = samp if (end > 0) & (s != 'none'): if s == 'N': VctAnnotationHot[1][end:begin] = 1 #annot_expand[end:begin] = states['st'] elif s == 't': VctAnnotationHot[3][end:begin] = 1 #annot_expand[end:begin] = states['iso'] elif s == 'p': VctAnnotationHot[5][end:begin] = 1 #annot_expand[end:begin] = states['pq'] elif anntype[j] == ')': end = samp if (begin >= 0) & (s != 'none'): VctAnnotationHot[states[s]][begin:end] = 1 #annot_expand[begin:end] = states[s] else: s = anntype[j] Vctrecord = np.transpose(Vctrecord) # transpose to (timesteps,feat) VctAnnotationHot = np.transpose(VctAnnotationHot) os.chdir(cwd) return Vctrecord, VctAnnotationHot