def classify(record, data_dir, clf=None):
    x = loader.load_data_from_file(record, data_dir)
    x = normalizer.normalize_ecg(x)
    x = extractor.features_for_row(x)

    if clf is None:
        clf = get_saved_model(x.shape[1:])

    # as we have one sample at a time to predict, we should resample it into 2d array to classify
    x = np.array(x).reshape(1, -1)

    return categorizer.get_original_label(clf.predict(x)[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)
Example #3
0
    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()


# Normal: A00001, A00002, A0003, A00006
plot_with_detected_peaks(loader.load_data_from_file("A07088"), "Normal")
plot_with_detected_peaks(loader.load_data_from_file("A00006"), "Normal")
plot_with_detected_peaks(loader.load_data_from_file("A08128"), "Normal")
# AF: A00004, A00009, A00015, A00027
plot_with_detected_peaks(loader.load_data_from_file("A00004"), "AF rhythm")
plot_with_detected_peaks(loader.load_data_from_file("A00009"), "AF rhythm")
# Other: A00005, A00008, A00013, A00017
plot_with_detected_peaks(loader.load_data_from_file("A00005"), "Other rhythm")
plot_with_detected_peaks(loader.load_data_from_file("A00008"), "Other rhythm")
# Noisy: A00205, A00585, A01006, A01070
plot_with_detected_peaks(loader.load_data_from_file("A00205"), "Noisy signal")
plot_with_detected_peaks(loader.load_data_from_file("A00585"), "Noisy signal")
        row,
        'k-',
        [x / 300 for x in p],
        [row[x] for x in p],
        'go',
        [x / 300 for x in q],
        [row[x] for x in q],
        'bv',
        [x / 300 for x in r],
        [row[x] for x in r],
        'r^',
        [x / 300 for x in s],
        [row[x] for x in s],
        'bv',
        [x / 300 for x in t],
        [row[x] for x in t],
        'mo',
    )
    plt.title("Positions of P, Q, R, S, T")
    plt.xlabel("Time, s")
    plt.ylabel("Normalized ECG signal")

    plt.show()


# Normal: A00001, A00002, A0003, A00006
# AF: A00004, A00009, A00015, A00027
# Other: A00005, A00008, A00013, A00017
# Noisy: A00205, A00585, A01006, A01070
plot_with_detected_peaks(loader.load_data_from_file("A00001"), "Normal")
import numpy as np

model_file = "../model.pkl"

name = input("Enter an entry name to plot: ")
name = name.strip()
if len(name) == 0:
    print("Finishing")
    exit(-1)

if not loader.check_has_example(name):
    print("File Not Found")
    exit(-1)

x = loader.load_data_from_file(name, "../validation")
x = normalizer.normalize_ecg(x)
feature_names = feature_extractor5.get_feature_names(x)
x = feature_extractor5.features_for_row(x)

# as we have one sample at a time to predict, we should resample it into 2d array to classify
x = np.array(x).reshape(1, -1)

model = joblib.load(model_file)
dtree = model.estimators_[0]


def export_tree(dtree, feature_names):
    dot_data = sklearn.tree.export_graphviz(dtree,
                                            feature_names=feature_names,
                                            out_file=None)
Example #6
0
    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()


# Normal: A00001, A00002, A0003, A00006
plot_wavelet(loader.load_data_from_file("A00001"), "Normal")
# AF: A00004, A00009, A00015, A00027
plot_wavelet(loader.load_data_from_file("A00004"), "AF rhythm")
plot_wavelet(loader.load_data_from_file("A00009"), "AF rhythm")
plot_wavelet(loader.load_data_from_file("A00015"), "AF rhythm")
plot_wavelet(loader.load_data_from_file("A00027"), "AF rhythm")
# Other: A00005, A00008, A00013, A00017
plot_wavelet(loader.load_data_from_file("A00005"), "Other rhythm")
plot_wavelet(loader.load_data_from_file("A00008"), "Other rhythm")
plot_wavelet(loader.load_data_from_file("A00013"), "Other rhythm")
plot_wavelet(loader.load_data_from_file("A00017"), "Other rhythm")
# Noisy: A00205, A00585, A01006, A01070
plot_wavelet(loader.load_data_from_file("A00205"), "Noisy signal")
plot_wavelet(loader.load_data_from_file("A00585"), "Noisy signal")
plot_wavelet(loader.load_data_from_file("A01006"), "Noisy signal")
plot_wavelet(loader.load_data_from_file("A01070"), "Noisy signal")
Example #7
0
    # templates = ecg.ecg(x, sampling_rate=300, show=False)['templates']

    fs, powers = signal.welch(x, loader.FREQUENCY)

    fs = fs[:40]
    powers = powers[:40]

    print(hrv.frequency_domain(x, fs=300))

    plt.plot(fs, powers)
    plt.xticks([2 * i for i in range(len(fs) // 2)])
    plt.grid()
    plt.show()


plot_powers(loader.load_data_from_file("A00001"))
plot_powers(loader.load_data_from_file("A07718"))
plot_powers(loader.load_data_from_file("A08523"))

plot_powers(loader.load_data_from_file("A00004"))
plot_powers(loader.load_data_from_file("A06746"))
plot_powers(loader.load_data_from_file("A07707"))

plot_powers(loader.load_data_from_file("A00013"))
plot_powers(loader.load_data_from_file("A06245"))
plot_powers(loader.load_data_from_file("A07908"))

plot_powers(loader.load_data_from_file("A01006"))
plot_powers(loader.load_data_from_file("A08402"))
plot_powers(loader.load_data_from_file("A04853"))
Example #8
0
                row[i] = row[i] * 100.0 / amax

    print(matrix)

    while (True):
        name = input("Enter an entry name to plot: ")
        name = name.strip()
        if len(name) == 0:
            print("Finishing")
            break

        if not loader.check_has_example(name):
            print("File Not Found")
            continue

        row = loader.load_data_from_file(name)

        print(info[name])
        [ts, fts, rpeaks, tts, thb, hrts,
         hr] = ecg.ecg(signal=row, sampling_rate=loader.FREQUENCY, show=True)
        b = heartbeats.median_heartbeat(thb)
        t = b[int(0.3 * loader.FREQUENCY):int(0.55 * loader.FREQUENCY)]

        a = normalizer.normalize_ecg(t)

        a[a < 0] = 0

        a = np.square(a)

        a -= np.mean(a)
import matplotlib

matplotlib.use("Qt5Agg")
import matplotlib.pyplot as plt

from loading import loader
from features.qrs_detect import *

plt.rcParams["figure.figsize"] = (14, 5)

seed = 42
random.seed(seed)
np.random.seed(seed)
print("Seed =", seed)

a_ecg = loader.load_data_from_file("A01171")
n_ecg = loader.load_data_from_file("A03823")
o_ecg = loader.load_data_from_file("A07915")
s_ecg = loader.load_data_from_file("A04946")

total = 4
plt.subplot(total, 1, 1)
plt.ylabel("A-Fib")
plt.plot(a_ecg)

plt.subplot(total, 1, 2)
plt.ylabel("Normal")
plt.plot(n_ecg)

plt.subplot(total, 1, 3)
plt.ylabel("Other")
    print('R', len(r), r)
    times = np.diff(r)
    print(np.mean(times), np.std(times))
    plt.subplot(2, 1, 1)
    plt.plot(range(len(row)), row, 'g-',
             r, [row[x] for x in r], 'r^')
    plt.ylabel(clazz + "QRS 1")

    r = qrs_detect2(row, fs=300, thres=0.4, ref_period=0.2)

    print('R', len(r), r)
    times = np.diff(r)
    print(np.mean(times), np.std(times))
    plt.subplot(2, 1, 2)
    plt.plot(range(len(row)), row, 'g-',
             r, [row[x] for x in r], 'r^')
    plt.ylabel(clazz + " QRS 2")

    plt.show()


# Normal: A00001, A00002, A0003, A00006
plot_with_detected_peaks(loader.load_data_from_file("A00001"), "Normal")
# AF: A00004, A00009, A00015, A00027
plot_with_detected_peaks(loader.load_data_from_file("A00004"), "AF")
# Other: A00005, A00008, A00013, A00017
plot_with_detected_peaks(loader.load_data_from_file("A00005"), "Other")
# Noisy: A00205, A00585, A01006, A01070
plot_with_detected_peaks(loader.load_data_from_file("A00205"), "Noisy")