def test_csvREAD(): """" Tests ecg data extraction from .csv. """ from readCSV import readCsv [time, volts] = readCsv('testcsvreaddata.csv') [timea, voltsa] = readCsv('baddata.csv') print(timea) correctt = [0., 0.003, 0.006, 0.008, 0.011] correctv = [-0.145, -0.145, -0.145, -0.145, -0.145] correctva = [-.145, -.145, -.145, -.145, -.145, -.145] correctta = [0, 0.003, 0.006, 0.008, .011, 0.014] assert pytest.approx(time) == correctt assert pytest.approx(volts) == correctv assert pytest.approx(timea) == correctta assert pytest.approx(voltsa) == correctva
def test_timeDuration(): """" Tests extraction of time duration of ECG data. """ from readCSV import readCsv from timeDuration import timeDuration [time, volts] = readCsv('testcsvreaddata.csv') t = [0., 0.003, 0.006, 0.008, 0.011] v = [-0.145, -0.145, -0.145, -0.145, -0.145] assert pytest.approx(timeDuration(time)) == .011
def test_peakDetect(): """" Tests R wave peak detection from ECG data. """ from peakDetection import peakDetect from readCSV import readCsv from processECG import movingAverage from processECG import subtractDC [t, v] = readCsv(r'test_data1.csv') svf = subtractDC(v) invfssum = 0.0 for i in range(len(t) - 1): invfssum = invfssum + (t[i + 1] - t[i]) invfs = invfssum / (len(t) - 1) movingavgwindow = int(.085 / invfs) avgvolt = movingAverage(t, svf, movingavgwindow) peaks = peakDetect(t, avgvolt) correctbeatnum = 36 assert len(peaks) == correctbeatnum
def main(): """ Runs all attached functions This function reads and analyzes the ECG data and writes the calculated parameters to a JSON file. """ from readCSV import readCsv from readCSV import recondtiontv from processECG import subtractDC from processECG import movingAverage from peakDetection import peakDetect from timeDuration import timeDuration from extVolt import extremeVolt from hrcalc import hrCalc import matplotlib.pyplot as plt from writetoJson import writetoJson import json csvfile = r'test_data1.csv' [traw, vraw] = readCsv(csvfile) bottomtimebnd = 0 toptimebnd = 100 [t, v] = recondtiontv(bottomtimebnd, toptimebnd, traw, vraw) svf = subtractDC(v) invfssum = 0.0 for i in range(len(t) - 1): invfssum = invfssum + (t[i + 1] - t[i]) invfs = invfssum / (len(t) - 1) movingavgwindow = int(.085 / invfs) avgvolt = movingAverage(t, svf, movingavgwindow) peakindices = peakDetect(t, avgvolt) heartbeattimes = t[peakindices].tolist() [min, max] = extremeVolt(v) voltextremes = (min, max) duration = timeDuration(t) beatnum = len(peakindices) meanhr = hrCalc(peakindices, t) writetoJson(csvfile, meanhr, voltextremes, duration, beatnum, heartbeattimes) plt.plot(t, avgvolt) plt.plot(t[peakindices], avgvolt[peakindices], 'o') plt.show()
This function identifies R wave peaks using the continuous wavelet transform Args: t (list): List of ECG time floats v (list): List of processed ECG voltage floats. Returns: peakind (list): List of integer indices corresponding to peaks in input lists. """ peakind = signal.find_peaks_cwt(v, np.arange(20, 40, .1)) return peakind if __name__ == "__main__": from readCSV import readCsv from processECG import subtractDC from processECG import savgolFilter from processECG import movingAverage import matplotlib.pyplot as plt [t, v] = readCsv(r'test_data4.csv') sv = subtractDC(v) svf = savgolFilter(sv) avgvolt = movingAverage(t, svf, 30) peaks = peakDetect(t, avgvolt) plt.plot(t, avgvolt) plt.plot(t[peaks], avgvolt[peaks], 'o') plt.show()
return subvolt def movingAverage(t, volts, window): """ Reduces noise by applying a moving average window. Args: t (list): List of ECG time floats volts (list): List of ECG voltage floats window (int): Size of moving average window Returns: lengthenedavg (list): List of moving average voltage floats """ import numpy as np from numpy import convolve bleh = np.repeat(1.0, window) / window movingavg = np.convolve(volts, bleh, 'valid') lengthenedavg = np.append(volts[:len(t) - len(movingavg)], movingavg) logging.debug("Moving average was applied with a window of %f values", window) return lengthenedavg if __name__ == "__main__": from readCSV import readCsv import numpy as np [t, v] = readCsv(r'test_data10.csv') sv = subtractDC(v) avgvolt = movingAverage(t, sv, 40)