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)
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
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
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]))
def area_gauss_fit_pk(self, x, y): dataset = pk.gaussian_fit(x,y,center_only=False) return dataset