Exemplo n.º 1
0
def setup_load_ecg_data():
    """Load ecg signal and sampling rate."""
    def load_signal_from_disk(filename=None, sampling_rate=2000):
        if filename is None:
            ecg = nk.ecg_simulate(duration=10,
                                  sampling_rate=sampling_rate,
                                  method="ecgsyn")
        else:
            filename = (pathlib.Path(__file__) / '../ecg_data' /
                        filename).resolve().as_posix()
            ecg = np.array(pd.read_csv(filename))[:, 1]
        return ecg, sampling_rate

    ecg, sampling_rate = load_signal_from_disk('good_4000.csv',
                                               sampling_rate=4000)
    annots_filename = (pathlib.Path(__file__) / '../ecg_data' /
                       'good_4000_annotation.csv').resolve().as_posix()
    annots = pd.read_csv(annots_filename, index_col=0, header=None).transpose()

    if SHOW_DEBUG_PLOTS:
        plt.plot(ecg)
        plt.show()

    rpeaks = nk.ecg_findpeaks(ecg,
                              sampling_rate=sampling_rate,
                              method='martinez')['ECG_R_Peaks']
    test_data = dict(ecg=ecg, sampling_rate=sampling_rate, rpeaks=rpeaks)
    test_data.update(annots)
    yield test_data
Exemplo n.º 2
0
def test_ecg_fixpeaks():

    sampling_rate = 1000
    noise = 0.15

    ecg = nk.ecg_simulate(duration=120, sampling_rate=sampling_rate,
                          noise=noise, method="simple", random_state=42)

    rpeaks = nk.ecg_findpeaks(ecg)

    # Test with iterative artifact correction.
    artifacts, rpeaks_corrected = nk.ecg_fixpeaks(rpeaks, iterative=True)

    assert np.allclose(rpeaks_corrected["ECG_R_Peaks"].sum(dtype=np.int64),
                       8624462, atol=1)

    assert all(isinstance(x, int) for x in artifacts["ectopic"])
    assert all(isinstance(x, int) for x in artifacts["missed"])
    assert all(isinstance(x, int) for x in artifacts["extra"])
    assert all(isinstance(x, int) for x in artifacts["longshort"])

    # Test with non-iterative artifact correction.
    artifacts, rpeaks_corrected = nk.ecg_fixpeaks(rpeaks, iterative=False)

    assert np.allclose(rpeaks_corrected["ECG_R_Peaks"].sum(dtype=np.int64),
                       8622997, atol=1)

    assert all(isinstance(x, int) for x in artifacts["ectopic"])
    assert all(isinstance(x, int) for x in artifacts["missed"])
    assert all(isinstance(x, int) for x in artifacts["extra"])
    assert all(isinstance(x, int) for x in artifacts["longshort"])
Exemplo n.º 3
0
def find_peaks(signal,
               fs=125,
               show_plot=True,
               peak_method="pantompkins1985",
               clean_method="neurokit"):

    peaks = nk.ecg_findpeaks(ecg_cleaned=signal,
                             sampling_rate=fs,
                             show=False,
                             method=peak_method)

    peaks_clean = nk.signal_fixpeaks(
        peaks,
        sampling_rate=fs,
        iterative=True,
        show=False,
        interval_min=int(fs * .66),  # HR 240bpm
        interval_max=int(fs * 2),  # HR 30bpm
        robust=False,
        method=clean_method)

    # peaks_clean = [i for i in peaks_clean if abs(i) >= 5]

    if show_plot:
        plt.plot(signal, color='black', zorder=0)
        plt.scatter(peaks["ECG_R_Peaks"],
                    [signal[i] for i in peaks["ECG_R_Peaks"]],
                    color='red',
                    marker='x',
                    zorder=1)
        plt.scatter(peaks_clean, [signal[i] for i in peaks_clean],
                    color='limegreen',
                    zorder=2)

    return peaks, peaks_clean
Exemplo n.º 4
0
def test_ecg_findpeaks():

    sampling_rate = 1000

    ecg = nk.ecg_simulate(duration=60, sampling_rate=sampling_rate, noise=0, method="simple", random_state=42)

    ecg_cleaned = nk.ecg_clean(ecg, sampling_rate=sampling_rate, method="neurokit")

    # Test neurokit methodwith show=True
    info_nk = nk.ecg_findpeaks(ecg_cleaned, show=True)

    assert info_nk["ECG_R_Peaks"].size == 69
    # This will identify the latest figure.
    fig = plt.gcf()
    assert len(fig.axes) == 2

    # Test pantompkins1985 method
    info_pantom = nk.ecg_findpeaks(nk.ecg_clean(ecg, method="pantompkins1985"), method="pantompkins1985")
    assert info_pantom["ECG_R_Peaks"].size == 70

    # Test hamilton2002 method
    info_hamilton = nk.ecg_findpeaks(nk.ecg_clean(ecg, method="hamilton2002"), method="hamilton2002")
    assert info_hamilton["ECG_R_Peaks"].size == 69

    # Test christov2004 method
    info_christov = nk.ecg_findpeaks(ecg_cleaned, method="christov2004")
    assert info_christov["ECG_R_Peaks"].size == 273

    # Test gamboa2008 method
    info_gamboa = nk.ecg_findpeaks(ecg_cleaned, method="gamboa2008")
    assert info_gamboa["ECG_R_Peaks"].size == 69

    # Test elgendi2010 method
    info_elgendi = nk.ecg_findpeaks(nk.ecg_clean(ecg, method="elgendi2010"), method="elgendi2010")
    assert info_elgendi["ECG_R_Peaks"].size == 70

    # Test engzeemod2012 method
    info_engzeemod = nk.ecg_findpeaks(nk.ecg_clean(ecg, method="engzeemod2012"), method="engzeemod2012")
    assert info_engzeemod["ECG_R_Peaks"].size == 70

    # Test kalidas2017 method
    info_kalidas = nk.ecg_findpeaks(nk.ecg_clean(ecg, method="kalidas2017"), method="kalidas2017")
    assert np.allclose(info_kalidas["ECG_R_Peaks"].size, 68, atol=1)

    # Test martinez2003 method
    ecg = nk.ecg_simulate(duration=60, sampling_rate=sampling_rate, noise=0, random_state=42)
    ecg_cleaned = nk.ecg_clean(ecg, sampling_rate=sampling_rate, method="neurokit")
    info_martinez = nk.ecg_findpeaks(ecg_cleaned, method="martinez2003")
    assert np.allclose(info_martinez["ECG_R_Peaks"].size, 69, atol=1)