def test_events_plot(): signal = np.cos(np.linspace(start=0, stop=20, num=1000)) events = nk.events_find(signal) data = nk.events_plot(events, signal, show=False) assert len(data["Event_Onset"]) == 1000 # Different events events1 = events["onset"] events2 = np.linspace(0, len(signal), 8) nk.events_plot([events1, events2], signal) fig = plt.gcf() for ax in fig.get_axes(): handles, labels = ax.get_legend_handles_labels() assert len(handles) == len(events1) + len(events2) + 1 assert len(labels) == len(handles) plt.close(fig) # Different conditions events = nk.events_find(signal, event_conditions=["A", "B", "A", "B"]) nk.events_plot(events, signal) fig = plt.gcf() for ax in fig.get_axes(): handles, labels = ax.get_legend_handles_labels() assert len(handles) == len(events) + 1 assert len(labels) == len(handles) plt.close(fig)
def helper_plot(attribute, ecg_characteristics, test_data): peaks = [ecg_characteristics[attribute], test_data[attribute]] print("0: computed\n1: data") ecg = test_data["ecg"] nk.events_plot(peaks, ecg) plt.title(attribute) plt.show()
def Get_R_Peaks(ecg_signal): # Extract R-peak locations from ecg signal file print( "======== Retrieving R Peaks of QRS complex ========") _, rpeaks = nk.ecg_peaks(ecg_signal, sampling_rate=3000) #Print to console for key in rpeaks: print(key, ' : ', rpeaks[key]) print() # Visualize R-peaks in ECG signal **for Jupyter Notebook plot_Rpeak_Signal = nk.events_plot(rpeaks['ECG_R_Peaks'], ecg_signal) plot_Rpeak_Signal.savefig("rpeaks_signal", dpi=300) # Visual R-peak locations for first 5 5 R-peaks **for Jupyter Notebook plot_Rpeak_Head = nk.events_plot(rpeaks['ECG_R_Peaks'][:5], ecg_signal[:20000]) plot_Rpeak_Head.savefig("rpeaks_head", dpi=300) return rpeaks
def Get_TPQS_Peaks(ecg_signal, rpeaks): # Delineate ECG signal to get TPQS peaks print( "======= Using DWT to retrieve TPQS Peaks and On/Offsets ========") dwt_sig, waves_dwt_peak = nk.ecg_delineate(ecg_signal, rpeaks, sampling_rate=3000, method="dwt", show=True, show_type="all") #Print to console for key in waves_dwt_peak: print(key, ' : ', waves_dwt_peak[key]) def_sig, waves_peak = nk.ecg_delineate(ecg_signal, rpeaks, sampling_rate=3000, show_type="peaks") # Visualize the T-peaks, P-peaks, Q-peaks and S-peaks **for JupyterNotebook plot_TPQS_Signal = nk.events_plot([ waves_peak['ECG_T_Peaks'], waves_peak['ECG_P_Peaks'], waves_peak['ECG_Q_Peaks'], waves_peak['ECG_S_Peaks'] ], ecg_signal) plot_TPQS_Signal.savefig("TPQS_signal", dpi=300) # Zooming into the first 3 R-peaks, with focus on # T_peaks, P-peaks, Q-peaks and S-peaks **for JupyterNotebook plot_TPQS_Head = nk.events_plot([ waves_peak['ECG_T_Peaks'][:3], waves_peak['ECG_P_Peaks'][:3], waves_peak['ECG_Q_Peaks'][:3], waves_peak['ECG_S_Peaks'][:3] ], ecg_signal[:12500]) plot_TPQS_Head.savefig("TPQS_head", dpi=300) # Delineate the ECG signal and visualizing all peaks of ECG complexes _, waves_peak = nk.ecg_delineate(ecg_signal, rpeaks, sampling_rate=3000, show=True, show_type='peaks') # Delineate the ECG signal and visualizing all P-peaks boundaries signal_peak, waves_peak = nk.ecg_delineate(ecg_signal, rpeaks, sampling_rate=3000, show=True, show_type='bounds_P') # Delineate the ECG signal and visualizing all T-peaks boundaries signal_peaj, waves_peak = nk.ecg_delineate(ecg_signal, rpeaks, sampling_rate=3000, show=True, show_type='bounds_T') #Print to Console for key in waves_peak: if (key == 'ECG_Q_Peaks' or key == 'ECG_S_Peaks'): print(key, ' : ', waves_peak[key]) print() return waves_dwt_peak, waves_peak
def test_events_plot(): signal = np.cos(np.linspace(start=0, stop=20, num=1000)) events = nk.events_find(signal) data = nk.events_plot(events, signal, show=False) assert len(data['Event_Onset']) == 1000