image_output_4.png text_plain_output_1.png image_output_3.png image_output_2.png image_output_1.png
from scipy import stats

import csv

import matplotlib.pyplot as plt

import numpy as np

import os

import pywt

plt.rcParams['figure.figsize'] = (30, 6)
plt.rcParams['lines.linewidth'] = 1
plt.rcParams['lines.color'] = 'b'
plt.rcParams['axes.grid'] = True

def denoise(data):
    w = pywt.Wavelet('sym4')
    maxlev = pywt.dwt_max_level(len(data), w.dec_len)
    threshold = 0.04
    coeffs = pywt.wavedec(data, 'sym4', level=maxlev)
    for i in range(1, len(coeffs)):
        coeffs[i] = pywt.threshold(coeffs[i], threshold * max(coeffs[i]))
    datarec = pywt.waverec(coeffs, 'sym4')
    return datarec

path = '/kaggle/input/mitbit-arrhythmia-database/mitbih_database/'
window_size = 180
maximum_counting = 10000
classes = ['N', 'L', 'R', 'A', 'V']
n_classes = len(classes)
count_classes = [0] * n_classes
X = list()
y = list()

filenames = next(os.walk(path))[2]
records = list()
annotations = list()
filenames.sort()

for f in filenames:
    filename, file_extension = os.path.splitext(f)
    if file_extension == '.csv':
        records.append(path + filename + file_extension)
    else:
        annotations.append(path + filename + file_extension)

for r in range(0, len(records)):
    signals = []
    with open(records[r], 'rt') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
        row_index = -1
        for row in spamreader:
            if row_index >= 0:
                signals.insert(row_index, int(row[1]))
            row_index += 1
    if r is 1:
        plt.title(records[1] + ' Wave')
        plt.plot(signals[0:700])
        plt.show()
    signals = denoise(signals)
    if r is 1:
        plt.title(records[1] + ' wave after denoised')
        plt.plot(signals[0:700])
        plt.show()
    signals = stats.zscore(signals)
    if r is 1:
        plt.title(records[1] + ' wave after z-score normalization ')
        plt.plot(signals[0:700])
        plt.show()
    example_beat_printed = False
    with open(annotations[r], 'r') as fileID:
        data = fileID.readlines()
        beat = list()
        for d in range(1, len(data)):
            splitted = data[d].split(' ')
            splitted = filter(None, splitted)
            next(splitted)
            pos = int(next(splitted))
            arrhythmia_type = next(splitted)
            if arrhythmia_type in classes:
                arrhythmia_index = classes.index(arrhythmia_type)
                count_classes[arrhythmia_index] += 1
                if window_size <= pos and pos < len(signals) - window_size:
                    beat = signals[pos - window_size:pos + window_size]
                    if r is 1 and (not example_beat_printed):
                        plt.title('A Beat from ' + records[1] + ' Wave')
                        plt.plot(beat)
                        plt.show()
                        example_beat_printed = True
                    X.append(beat)
                    y.append(arrhythmia_index)
print(np.shape(X), np.shape(y))