def __init__(self, record, ID, field): self.record = record self.ID = ID self.field = field self.fs = field['fs'] curr_date = field['comments'][2] [curr_dy, curr_mo, curr_yr] = [curr_date[-10:-8], curr_date[-7:-5], curr_date[-4:]] self.time = Time(int(curr_dy), int(curr_mo), int(curr_yr)) self.time.set_person_ID(self.ID) SEG_LEN_IN_SEC = .5 seg_len = SEG_LEN_IN_SEC / (1./self.fs) out = ecg.ecg(signal=self.record, sampling_rate=self.fs, show=False) ecg_filtered, peaks = out[1], out[2] USE_FILTERED = True if USE_FILTERED: sig_use = ecg_filtered else: sig_use = records[i] segs = mf.get_segs(sig_use, peaks, seg_len, fs=self.fs) self.segs = np.array(segs)
plt.show() quit() #plt.show() TO_PLOT_PEAKS = False if TO_PLOT_PEAKS: ts = np.linspace(0,dt*len(sig_use),len(sig_use)) peak_xs = [x*dt for x in peaks] peak_ys = [sig_use[x] for x in peaks] fig = plt.figure(figsize=(8,6)) plt.plot(ts, sig[:,1]) plt.plot(peak_xs, peak_ys, 'ro') my_funcs.config_plot('Time /s', 'Amplitude /mV') plt.show() segs = my_funcs.get_segs(ecg_filtered, peaks, seg_len, fs=fs) qual_len = int(round(seg_len/1000. * fs)) # len of a complete QRS seg qual_num = 0 seg_sum = [0] * qual_len TO_PLOT_SEGS = False if TO_PLOT_SEGS: fig = plt.figure(figsize=(8,6)) for seg in segs: segs_all.append(seg) if len(seg) == qual_len: qual_num += 1
print 'The ' + str(rec_ind_look) + '-th record is chosen.' records = [] one_filename = curr_filenames[rec_ind_look] # One rec has many pulses filename = one_filename[:-4] sig, fields = wfdb.rdsamp(filename) #, sampto=1000) #, pbdl=0) fs = fields['fs'] seg_len = SEG_LEN_IN_SEC / (1. / fs) sig = np.array(sig) sig_use = sig[:, channel_ind] out = ecg.ecg(signal=sig_use, sampling_rate=fs, show=False) ecg_filtered, peaks = out[1], out[2] segs = mf.get_segs(sig_use, peaks, seg_len, fs=fs) sigs_all = [sig[:, 0], sig[:, 1], ecg_filtered] desp_all = [ 'original signal', 'primary filtered signal', 'secondary filtered signal' ] i = 0 for curr_sig in sigs_all: fig = plt.figure(figsize=(8, 6)) plt.plot(curr_sig) mf.config_plot('time', desp_all[i]) plt.show() curr_segs = mf.get_segs(curr_sig, peaks, seg_len, fs=fs) fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(10, 8)) j = 0 for ax in axes.flat:
fs = fields['fs'] dt = 1./fs ann = wfdb.rdann(curr_ID, 'atr') print ann[0][:5] print ann[1][:5] sig_len = 600 ts = np.arange(sig_len)*1./fs curr_sig = sig[:sig_len, channel] out = ecg.ecg(signal=curr_sig, sampling_rate=fs, show=False) ecg_filtered, peaks = out[1], out[2] seg_len = mf.SEG_LEN_IN_SEC/dt segs = mf.get_segs(ecg_filtered, peaks, seg_len, fs) #segs = np.array(segs) #print segs.shape #plt.plot(segs[1]) #plt.show() # Display for looking def tim2ind(t): return int(round(t/dt)) ## TODO: define a template fitting function, including parameter preparations R_loc, R_color = peaks[0], 'blue' # color='blue' plt.scatter(R_loc*dt, 0, color=R_color) whole_range_ind = range(len(ecg_filtered)) #range(R_loc-50,R_loc+50,1) whole_range_tim = [x*dt for x in whole_range_ind]
curr_foldername = 'Person_' + curr_ID_str curr_filenames = glob.glob(curr_foldername + "/*.dat") j = 0 for one_filename in curr_filenames: # One rec has many pulses filename = one_filename[:-4] sig, fields = wfdb.rdsamp(filename)#, sampto=1000) #, pbdl=0) fs = fields['fs'] sig_use = sig[:,1] out = ecg.ecg(signal=sig_use, sampling_rate=fs, show=False) peaks = out[2] segs = my_funcs.get_segs(sig_use, peaks, seg_len, fs=fs) qual_len = int(round(seg_len/1000. * fs)) # len of a complete QRS seg qual_num = 0 seg_sum = [0] * qual_len for seg in segs: if len(seg) == qual_len: qual_num += 1 segs_all.append(seg) labels_all.append(i) seg_avg = [x/float(qual_num) for x in seg_sum] j += 1
ts = np.linspace(0, dt * len(sig_use), len(sig_use)) peak_xs = [x * dt for x in peaks] peak_ys = [sig_use[x] for x in peaks] fig = plt.figure(figsize=(8, 6)) plt.plot(ts, sig[:, 1]) plt.plot(peak_xs, peak_ys, 'ro') my_funcs.config_plot('Time /s', 'Amplitude /mV') plt.show() ## after all these things, get segs by my own function ## Three options: # sig[:,0], accuracy 87.8% # sig[:,1], accuracy 87.8% # ecg_filtered, accuracy 92.2% segs = my_funcs.get_segs(sig[:, 0], peaks, seg_len, fs=fs) qual_len = int(round(seg_len / 1000. * fs)) # len of a complete QRS seg qual_num = 0 seg_sum = [0] * qual_len TO_PLOT_SEGS = False if TO_PLOT_SEGS: fig = plt.figure(figsize=(8, 6)) for seg in segs: segs_all.append(seg) if len(seg) == qual_len: qual_num += 1