コード例 #1
0
def plot_with_detected_peaks(x, clazz):
    total = 7

    x = ecg.ecg(x, sampling_rate=300, show=False)['templates']

    m = [np.median(col) for col in x.T]

    dists = [np.sum(np.square(s-m)) for s in x]
    pmin = np.argmin(dists)

    x = x[pmin]

    p = x[:45]
    qrs = x[45:80]
    t = x[80:]

    plt.subplot(total, 1, 1)
    plt.ylabel(clazz)
    plt.plot(x)

    plt.subplot(total, 1, 2)
    plt.ylabel(clazz)
    plt.plot(p)

    plt.subplot(total, 1, 3)
    plt.ylabel("P DFT")
    ff = fft(p)[:len(p) // 2]
    plt.plot(ff, 'r')

    plt.subplot(total, 1, 4)
    plt.ylabel(clazz)
    plt.plot(qrs)

    plt.subplot(total, 1, 5)
    plt.ylabel("QRS DFT")
    ff = fft(qrs)[:len(qrs) // 2]
    plt.plot(ff, 'r')

    plt.subplot(total, 1, 6)
    plt.ylabel(clazz)
    plt.plot(t)

    plt.subplot(total, 1, 7)
    plt.ylabel("T DFT")
    ff = fft(t)[:len(t) // 2]
    plt.plot(ff, 'r')

    plt.show()
コード例 #2
0
def plot_wavelet(row, clazz):
    res = ecg(row, sampling_rate=300, show=False)
    # x = preprocessing.normalize_ecg(row)
    # x = low_pass_filtering(x)
    # x = high_pass_filtering(x)

    x = res['templates'][0]

    print(pywt.wavelist('db'))

    a, d1, d2, d3, d4 = wavedec(x, 'db4', level=4)

    print(clazz)
    print('Mean', np.mean(d1))
    print('Std', np.std(d1))
    print('Mean', np.mean(d2))
    print('Std', np.std(d2))

    total = 6
    plt.subplot(total, 1, 1)
    plt.plot(range(len(x)), x, 'g-')
    plt.ylabel("ECG:" + clazz)

    plt.subplot(total, 1, 2)
    plt.plot(range(len(a)), a, 'g-')
    plt.ylabel("Wavelet")

    plt.subplot(total, 1, 3)
    plt.plot(range(len(d1)), d1, 'g-')
    plt.ylabel("D1")

    plt.subplot(total, 1, 4)
    plt.plot(range(len(d2)), d2, 'g-')
    plt.ylabel("D2")

    plt.subplot(total, 1, 5)
    plt.plot(range(len(d3)), d3, 'g-')
    plt.ylabel("D3")

    plt.subplot(total, 1, 6)
    plt.plot(range(len(d4)), d4, 'g-')
    plt.ylabel("D4")

    plt.show()
コード例 #3
0
def get_features_dict(x):
    """
       ts : array
        Signal time axis reference (seconds).
    filtered : array
        Filtered ECG signal.
    rpeaks : array
        R-peak location indices.
    templates_ts : array
        Templates time axis reference (seconds).
    templates : array
        Extracted heartbeat templates.
    heart_rate_ts : array
        Heart rate time axis reference (seconds).
    heart_rate : array
        Instantaneous heart rate (bpm).
    :param x:
    :return:
    """
    [ts, fts, rpeaks, tts, thb, hrts,
     hr] = ecg.ecg(signal=x, sampling_rate=loader.FREQUENCY, show=False)
    """
    Returns:	

    ts (array) – Signal time axis reference (seconds).
    filtered (array) – Filtered ECG signal.
    rpeaks (array) – R-peak location indices.
    templates_ts (array) – Templates time axis reference (seconds).
    templates (array) – Extracted heartbeat templates.
    heart_rate_ts (array) – Heart rate time axis reference (seconds).
    heart_rate (array) – Instantaneous heart rate (bpm).
    """

    fx = dict()
    fx.update(heart_rate_features(hr))
    fx.update(frequency_powers(x, n_power_features=60))
    fx.update(add_suffix(frequency_powers(fts), "fil"))
    fx.update(frequency_powers_summary(fts))
    fx.update(heart_beats_features2(thb))
    fx.update(fft_features(heartbeats.median_heartbeat(thb)))
    # fx.update(heart_beats_features3(thb))
    fx.update(r_features(fts, rpeaks))

    fx['PRbyST'] = fx['PR_interval'] * fx['ST_interval']
    fx['P_form'] = fx['P_kurt'] * fx['P_skew']
    fx['T_form'] = fx['T_kurt'] * fx['T_skew']
    """
    from features.template_statistics import TemplateStatistics
    # Get features
    template_statistics = TemplateStatistics(
        ts=ts,
        signal_raw=x,
        signal_filtered=fts,
        rpeaks=rpeaks,
        templates_ts=tts,
        templates=np.array(thb).T,
        fs=loader.FREQUENCY,
        template_before=0.25,
        template_after=0.4
    )
    template_statistics.calculate_template_statistics()

    # Update feature dictionary
    fx.update(template_statistics.get_template_statistics())
    """
    for key, value in fx.items():
        if np.math.isnan(value):
            value = 0
        fx[key] = value

    return fx
コード例 #4
0
    for item in misclassified:
        print(item[2], 'is of class', item[0], 'but was classified as',
              item[1])

    print(classification_report(ytrue, ypred))

    matrix = confusion_matrix(ytrue, ypred)
    print(matrix)
    for row in matrix:
        amax = sum(row)
        if amax > 0:
            for i in range(len(row)):
                row[i] = row[i] * 100.0 / amax

    print(matrix)

    print("Plotting misclassified")
    base_dir = "../outputs/misclassified"

    mkdir(base_dir)

    misclassified = sorted(misclassified, key=lambda t: t[2])
    for item in misclassified:
        print("Saving to " + item[2])
        row = loader.load_data_from_file(item[2])
        [ts, fts, rpeaks, tts, thb, hrts,
         hr] = ecg.ecg(signal=row, sampling_rate=loader.FREQUENCY, show=False)
        __draw_to_file(
            base_dir + "/" + item[2] + "_" + item[0] + "_" + item[1] + ".png",
            tts, thb)