def delete_peaks(data, remove): """ Deletes peaks in `remove` from peaks stored in `data` Parameters ---------- data : Physio_like remove : array_like Returns ------- data : Physio_like """ data = utils.check_physio(data, ensure_fs=False, copy=True) data._metadata['peaks'] = np.setdiff1d(data._metadata['peaks'], remove) data._metadata['troughs'] = utils.check_troughs(data, data.peaks) return data
def reject_peaks(data, remove): """ Marks peaks in `remove` as rejected artifacts in `data` Parameters ---------- data : Physio_like remove : array_like Returns ------- data : Physio_like """ data = utils.check_physio(data, ensure_fs=False, copy=True) data._metadata['reject'] = np.append(data._metadata['reject'], remove) data._metadata['troughs'] = utils.check_troughs(data, data.peaks) return data
def peakfind_physio(data, *, thresh=0.2, dist=None): """ Performs peak and trough detection on `data` Parameters ---------- data : Physio_like Input data in which to find peaks thresh : float [0,1], optional Relative height threshold a data point must surpass to be classified as a peak. Default: 0.2 dist : int, optional Distance in indices that peaks must be separated by in `data`. If None, this is estimated. Default: None Returns ------- peaks : :class:`peakdet.Physio` Input `data` with detected peaks and troughs """ ensure_fs = True if dist is None else False data = utils.check_physio(data, ensure_fs=ensure_fs, copy=True) # first pass peak detection to get approximate distance between peaks cdist = data.fs // 4 if dist is None else dist thresh = np.squeeze(np.diff(np.percentile(data, [5, 95]))) * thresh locs, heights = signal.find_peaks(data[:], distance=cdist, height=thresh) # second, more thorough peak detection cdist = np.diff(locs).mean() // 2 heights = np.percentile(heights['peak_heights'], 1) locs, heights = signal.find_peaks(data[:], distance=cdist, height=heights) data._metadata['peaks'] = locs # perform trough detection based on detected peaks data._metadata['troughs'] = utils.check_troughs(data, data.peaks) return data
def undo(self): """ Resets last span select peak removal """ # check if last history entry was a manual reject / delete if self.data._history[-1][0] not in ['reject_peaks', 'delete_peaks']: return # pop off last edit and delete func, peaks = self.data._history.pop() if func == 'reject_peaks': self.data._metadata['reject'] = np.setdiff1d( self.data._metadata['reject'], peaks['remove']) self.rejected.difference_update(peaks['remove']) elif func == 'delete_peaks': self.data._metadata['peaks'] = np.insert( self.data._metadata['peaks'], np.searchsorted(self.data._metadata['peaks'], peaks['remove']), peaks['remove']) self.deleted.difference_update(peaks['remove']) self.data._metadata['troughs'] = utils.check_troughs( self.data, self.data.peaks) self.plot_signals()
def test_check_troughs(): true = np.array([9, 21]) assert_array_equal(utils.check_troughs(DATA, PEAKS), true)