Exemple #1
0
    def find_peaks(self):
        width = self.peak_minimum_sample_distance
        indices = peakutils.indexes(self.filtered_data, min_dist=width)

        final_idxs = []
        fits = []
        for idx in indices:
            w = width
            if idx - width < 0:
                w = idx
            if idx + w > len(self.t):
                w = len(self.t) - idx
            ti = np.array(self.t)[idx - w:idx + w]
            datai = np.array(self.filtered_data)[idx - w:idx + w]
            try:
                params = peakutils.gaussian_fit(ti, datai, center_only=False)
                y = [peakutils.gaussian(x, *params) for x in ti]
                # ax.plot(ti, y)
                ssr = np.sum(np.power(np.subtract(y, datai), 2.0))
                sst = np.sum(np.power(np.subtract(y, datai), 2.0))
                r2 = 1 - (ssr / sst)
                fits.append(r2)
                if params[2] < self.gaussian_cutoff:
                    final_idxs.append(idx)
            except RuntimeError:
                pass
        return final_idxs, fits
Exemple #2
0
def test_est_hr():
    import os.path
    import sys
    sys.path.append(
        os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
    from est_hr import est_hr
    import peakutils
    import numpy as np

    # Simulate Data with Peaks and Noise
    # Assume 75 bpm -> 1.25/second
    delta_t = 0.01  # seconds
    signal_choice = 3  # Both ECG and PP combined
    x = np.linspace(0, 10, 10 / delta_t)
    x_ind = np.array(range(len(x)))
    centers = (1 / delta_t, 1.8 / delta_t, 2.6 / delta_t, 3.4 / delta_t,
               4.2 / delta_t, 5 / delta_t, 5.8 / delta_t, 6.6 / delta_t)

    tmp = np.zeros(len(x_ind))
    for i in range(len(centers)):
        tmp = tmp + (peakutils.gaussian(x_ind, 5, centers[i], 5))
    ECG_data = tmp + np.random.rand(x_ind.size)
    PP_data = tmp + np.random.rand(x_ind.size)

    inst_HR = est_hr(ECG_data, PP_data, delta_t, signal_choice)
    eps = 5  # Range of 5 bpm accepted for accuracy
    assert (inst_HR > (75 - eps / 2) and inst_HR < (75 + eps / 2))
def find_peaks_simplified(data, t, width=5, gaussian_cutoff=10.0):
    indices = peakutils.indexes(data, min_dist=width)

    final_idxs = []
    fits = []
    for idx in indices:
        w = width
        if idx - width < 0:
            w = idx
        if idx + w > len(t):
            w = len(t) - idx
        ti = np.array(t)[idx - w:idx + w]
        datai = np.array(data)[idx - w:idx + w]
        try:
            params = peakutils.gaussian_fit(ti, datai, center_only=False)
            y = [peakutils.gaussian(x, *params) for x in ti]
            # ax.plot(ti, y)
            ssr = np.sum(np.power(np.subtract(y, datai), 2.0))
            sst = np.sum(np.power(np.subtract(y, datai), 2.0))
            r2 = 1 - (ssr / sst)
            fits.append(r2)
            if params[2] < gaussian_cutoff:
                final_idxs.append(idx)
        except RuntimeError:
            pass
    return final_idxs, fits
Exemple #4
0
    def test_gaussian_fit(self):
        params = np.array([0.5, 6, 2])
        x = np.arange(10)
        y = peakutils.gaussian(x, *params)
        self.assertAlmostEqual(peakutils.gaussian_fit(x, y), params[1])

        res = peakutils.gaussian_fit(x, y, center_only=False)
        np.testing.assert_allclose(res, params)
Exemple #5
0
    def test_gaussian_fit(self):
        params = np.array([0.5, 6, 2])
        x = np.arange(10)
        y = peakutils.gaussian(x, *params)
        self.assertAlmostEqual(peakutils.gaussian_fit(x, y), params[1])

        res = peakutils.gaussian_fit(x, y, center_only=False)
        np.testing.assert_allclose(res, params)
Exemple #6
0
    def aux_test_peaks(self, dtype):
        '''(3 peaks + baseline + noise)'''
        x = numpy.linspace(0, 100, 1000)
        centers = (20, 40, 70)
        y = (1000 * (peakutils.gaussian(x, 1, centers[0], 3) +
                     peakutils.gaussian(x, 2, centers[1], 5) +
                     peakutils.gaussian(x, 3, centers[2], 1) +
                     numpy.random.random(x.size) * 0.2)).astype(dtype)

        filtered = scipy.signal.savgol_filter(y, 51, 3).astype(dtype)
        idx = peakutils.indexes(filtered, thres=0.3, min_dist=100)
        peaks = peakutils.interpolate(x, y, idx, width=30)
        self.assertEqual(idx.size, len(centers))
        self.assertEqual(peaks.size, len(centers))

        # interpolation should work!
        for i in range(peaks.size):
            self.assertAlmostEqual(peaks[i], centers[i], delta=0.5)
Exemple #7
0
    def test_peaks(self):
        '''(3 peaks + baseline + noise)'''
        x = numpy.linspace(0, 100, 1000)
        centers = (20, 40, 70)
        y = (peakutils.gaussian(x, 1, centers[0], 3) +
             peakutils.gaussian(x, 2, centers[1], 5) +
             peakutils.gaussian(x, 3, centers[2], 1) +
             numpy.random.random(x.size) * 0.2)

        filtered = scipy.signal.savgol_filter(y, 51, 3)
        idx = peakutils.indexes(filtered, thres=0.3, min_dist=100)
        peaks = peakutils.interpolate(x, y, idx, width=30)
        self.assertEqual(idx.size, len(centers))
        self.assertEqual(peaks.size, len(centers))

        # interpolation should work!
        for i in range(peaks.size):
            self.assertAlmostEqual(peaks[i], centers[i], delta=0.5)
def find_peaks(filename, ax, fs):
    data_dir = r"C:\Users\kevin\Desktop\Active Projects\Video Magnification Videos\\"
    t, data = np.transpose(np.load(data_dir + filename))
    ax.plot(t, data)
    width = 5

    order = 3
    cutoff = 1.0
    data = butter_lowpass_filter(data, cutoff, fs, order)

    indices = peakutils.indexes(data, min_dist=width)
    peak_ts = np.take(t, indices)
    peak_ds = np.take(data, indices)
    ax.plot(t, data)
    ax.scatter(peak_ts, peak_ds, c='r', alpha=0.5)

    final_idxs = []
    post_gaussian = []
    for idx in indices:
        w = width
        if idx - width < 0:
            w = idx
        if idx + w > len(t):
            w = len(t) - idx
        ti = t[idx - w:idx + w]
        datai = data[idx - w:idx + w]
        # ax.plot(ti, datai)
        try:
            params = peakutils.gaussian_fit(ti, datai, center_only=False)
            y = [peakutils.gaussian(x, *params) for x in ti]
            # ax.plot(ti, y)
            ssr = np.sum(np.power(np.subtract(y, datai), 2.0))
            sst = np.sum(np.power(np.subtract(y, datai), 2.0))
            r2 = 1 - (ssr / sst)
            post_gaussian.append(idx)
            if params[2] < 10.0:
                final_idxs.append(idx)
                print('idx={0}, r2={1}, params={2}'.format(idx, r2, params))
        except RuntimeError:
            pass
    peak_ts = np.take(t, post_gaussian)
    peak_ds = np.take(data, post_gaussian)
    ax.scatter(peak_ts, peak_ds, c='g', alpha=0.75)
    peak_ts = np.take(t, final_idxs)
    peak_ds = np.take(data, final_idxs)
    ax.scatter(peak_ts, peak_ds, c='b', alpha=1.0)
    for i, txt in enumerate(final_idxs):
        ax.annotate(str(txt), (peak_ts[i], peak_ds[i]))
Exemple #9
0
def gaussian(a, w, x, c):
    return pe.gaussian(x, a, c, w)
Exemple #10
0
def someData():
    centers = (30.5, 72.3)
    x = np.linspace(0, 120, 121)
    y = (peakutils.gaussian(x, 5, centers[0], 3) +
         peakutils.gaussian(x, 7, centers[1], 10) + np.random.rand(x.size))
    return [x, y]
Exemple #11
0
def gaussian(a, w, x, c):
    return pe.gaussian(x, a, c, w)
Exemple #12
0
def make_data(n, noise):
    x = np.linspace(0, 100, n)
    y = peakutils.gaussian(x, 5, 20, 10) + \
        peakutils.gaussian(x, 8, 70, 3) + \
        noise*np.random.rand(x.size) + np.polyval([3, 2], x)
    return x, y
import numpy as np
import peakutils
from peakutils.plot import plot as pplt
from matplotlib import pyplot as plt
from smooth import smooth

centers = (30.5, 72.3)
x = np.linspace(0, 120, 121)
y = (peakutils.gaussian(x, 5, centers[0], 3) +
    peakutils.gaussian(x, 7, centers[1], 10) +
    np.random.rand(x.size))
plt.figure(figsize=(10,6))
plt.plot(x, y)
plt.title("Data with noise")

y=smooth(y, 10, 'bartlett')
plt.figure(figsize=(10,6))
plt.plot(y)
plt.title("Smoothing test")

dx = x[1]-x[0]
dydx = np.gradient(y,1)
d2ydx2 = np.gradient(dydx,1)
print dydx

indexes = peakutils.indexes(y, thres=0.5, min_dist=30)
print(indexes)
print(x[indexes], y[indexes])
plt.figure(figsize=(10,6))
pplt(x, y, indexes)
plt.title('First estimate')
Exemple #14
0
def make_data(n, noise):
    x = np.linspace(0, 100, n)
    y = peakutils.gaussian(x, 5, 20, 10) + \
        peakutils.gaussian(x, 8, 70, 3) + \
        noise * np.random.rand(x.size) + np.polyval([3, 2], x)
    return x, y
Exemple #15
0
import numpy
import peakutils
from peakutils.plot import plot as pplot
from matplotlib import pyplot

centers = (30.5, 72.3)
x = numpy.linspace(0, 120, 121)
y = peakutils.gaussian(x, 5, centers[0], 3) + peakutils.gaussian(x, 7, centers[1], 10) + numpy.random.rand(x.size)
pyplot.figure(figsize=(10, 6))
pyplot.plot(x, y)
pyplot.title("Data with noise")

indexes = peakutils.indexes(y, thres=0.5, min_dist=30)
print(indexes)
print(x[indexes], y[indexes])
pyplot.figure(figsize=(10, 6))
pplot(x, y, indexes)
pyplot.title("First estimate")
pyplot.show()