raise ValueError("Requires threshold to be a positive float") if ele_per_sec <= 0: raise ValueError( "Requires ele_per_sec to be a strictly positive scalar") except ValueError, err_msg: raise ValueError(err_msg) return 'initialize parameters' lag_vect = matrix['Lag'] peak_found = {} for col in sorted(matrix.keys()): if col != 'Lag': curr_corr = matrix[col] peakind = PeakDetect.peakdetect(curr_corr, lag_vect, lookahead, delta) 'Keep only the highest correlation' max = 0 max_tab = [np.nan, np.nan] for k in range(len(peakind[0])): #maxima if (max < peakind[0][k][1]): max = peakind[0][k][1] if max > threshold: if (type(peakind[0][k][1]) == np.float64): max_tab = [peakind[0][k][0], peakind[0][k][1]] else: max_tab = [peakind[0][k][0], peakind[0][k][1][0]] min = 0
if tau_max < 0 : raise ValueError("Requires tau_max to be a positive scalar") if tau_inc < 0 or tau_inc > tau_max : raise ValueError("Requires tau_inc to be a positive scalar inferior to tau_max length") if threshold <0 : raise ValueError("Requires threshold to be a positive float") if ele_per_sec <= 0 : raise ValueError("Requires ele_per_sec to be a strictly positive scalar") except ValueError, err_msg: raise ValueError(err_msg) return 'initialize parameters' lag_vect = matrix['Lag'] peak_found = {} for col in sorted(matrix.keys()): if col != 'Lag': curr_corr = matrix[col] peakind = PeakDetect.peakdetect(curr_corr, lag_vect, lookahead, delta) 'Keep only the highest correlation' max = 0 max_tab = [np.nan, np.nan] for k in range(len(peakind[0])) : #maxima if( max < peakind[0][k][1]) : max = peakind[0][k][1] if max > threshold: if(type(peakind[0][k][1]) == np.float64): max_tab = [peakind[0][k][0], peakind[0][k][1]] else: max_tab = [peakind[0][k][0], peakind[0][k][1][0]] min = 0
def compute(self): """ It computes peak picking from cross correlation matrix :returns: pd.DataFrame -- if sorted_peak is False, peaks found organized per Maximin, Minimum and Extremum :returns: pd.DataFrame -- if sorted_peak is True, peaks found organized by type of Lag and Magnitude (positive or negative) """ 'initialize parameters' lag_vect = self._corr_matrix.index peak_found = {} for col in self._corr_matrix: curr_corr = self._corr_matrix[col].values peakind = PeakDetect.peakdetect(curr_corr, lag_vect, self._lookahead, self._delta) 'Keep only the highest correlation' max = 0 max_tab = [np.nan, np.nan] for k in range(len(peakind[0])): #maxima if (max < peakind[0][k][1]): max = peakind[0][k][1] if max > self._threshold: if (type(peakind[0][k][1]) == int): max_tab = [peakind[0][k][0], peakind[0][k][1]] else: max_tab = [peakind[0][k][0], peakind[0][k][1][0]] min = 0 min_tab = [np.nan, np.nan] for l in range(len(peakind[1])): #minima if (min > peakind[1][l][1]): min = peakind[1][l][1] if min < -self._threshold: if (type(peakind[1][l][1]) == int): min_tab = [peakind[1][l][0], peakind[1][l][1]] else: min_tab = [peakind[1][l][0], peakind[1][l][1][0]] extr_tab = [np.nan, np.nan] #extremum if min_tab[1] is np.nan: extr_tab = max_tab else: if (max_tab[1] > np.abs(min_tab[1])): extr_tab = max_tab if max_tab[1] is np.nan: extr_tab = min_tab else: if (max_tab[1] <= np.abs(min_tab[1])): extr_tab = min_tab peak_found.update({col: [max_tab, min_tab, extr_tab]}) ' Save result ' peak_found.update( {'Time window': pd.Series(['maximum', 'minimum', 'extremum'])}) self._peak_found = pd.DataFrame(peak_found) self._peak_found.set_index('Time window', inplace=True) self._peak_found = self._peak_found.T if self._plot: plt.ion() self.plot() if self._sorted_peak: result = self.sort_peakPickingResult() else: result = self._peak_found return result
def PeakPicking(matrix, tau_max, tau_inc = 0, threshold = 0, lookahead = 300, delta = 0, ele_per_sec = 1, plot = False, plot_on_mat = False, sorted_peak = False): """ It computes peak picking algorithm to a cross-matrix (computed by WindowCrossCorrelation or WindowMutualInformation for example) :param matrix: cross matrix (from WindowCrossCorrelation or WindowMutualInformation for example) :type matrix: dict :param tau_max: the maximum lag (in samples) at which correlation should be computed. It is in the range [0, (length(x)+length(y)-1)/2] :type tau_max: int :param tau_inc: amount of time (in samples) elapsed between two cross-correlation :type tau_inc: int :param threshold: minimal magnitude acceptable for a peak. For maxima, compared to threshold, for minima, compared to (-threshold) :type threshold: float :param lookahead: distance to look ahead from a peak candidate to determine if it is the actual peak. Default: 200 (sample / period) / f where 4 >= f >= 1.25 might be a good value :type lookahead: int :param delta: it specifies a minimum difference between a peak and the following points, before a peak may be considered a peak. Useful to hinder the function from picking up false peaks towards to end of the signal. To work well delta should be set to delta >= RMSnoise * 5. Default: 0 :type delta: int :param ele_per_sec: number of elements in one second :type ele_per_sec: int :param plot: if True the plot of peakpicking function is returned. Default: False :type plot: bool :param plot_on_mat: if True the plot of peakpicking + correlation matrix function is returned. Default: False :type plot_on_mat: bool :param sorted_peak: if True the peaks found will be organized by type of Lag and Magnitude (positive or negative). Default: False :type plot_on_mat: bool :returns: pd.DataFrame -- if sorted_peak is False, peaks found organized per Maximin, Minimum and Extremum :returns: pd.DataFrame -- if sorted_peak is True, peaks found organized by type of Lag and Magnitude (positive or negative) """ ' Raise error if parameters are not in the correct type ' if not(isinstance(matrix, dict)) : raise TypeError("Requires corr_matrix to be a dictionary") if not(isinstance(tau_max, int)) : raise TypeError("Requires tau_max to be an integer") if not(isinstance(tau_inc, int)) : raise TypeError("Requires tau_inc to be an integer") if not(isinstance(threshold, float)) : raise TypeError("Requires threshold to be an float") if not(isinstance(lookahead, int)) : raise TypeError("Requires plot to be a integer") if not(isinstance(delta, int)) : raise TypeError("Requires delta to be an integer") if not(isinstance(ele_per_sec, int)) : raise TypeError("Requires ele_per_sec to be an integer") if not(isinstance(plot, bool)) : raise TypeError("Requires plot to be a boolean") if not(isinstance(plot_on_mat, bool)) : raise TypeError("Requires plot_on_mat to be an boolean") if not(isinstance(sorted_peak, bool)) : raise TypeError("Requires sorted_peak to be an boolean") ' Raise error if parameters do not respect input rules ' if not 'Lag' in matrix : raise ValueError("Requires dictionary to have a 'Lag' key") if tau_max < 0 : raise ValueError("Requires tau_max to be a positive scalar") if tau_inc < 0 or tau_inc > tau_max : raise ValueError("Requires tau_inc to be a positive scalar inferior to tau_max length") if threshold <0 : raise ValueError("Requires threshold to be a positive float") if ele_per_sec <= 0 : raise ValueError("Requires ele_per_sec to be a strictly positive scalar") 'initialize parameters' lag_vect = matrix['Lag'] peak_found = {} for col in sorted(matrix.keys()): if col != 'Lag': curr_corr = matrix[col] peakind = PeakDetect.peakdetect(curr_corr, lag_vect, lookahead, delta) 'Keep only the highest correlation' max = 0 max_tab = [np.nan, np.nan] for k in range(len(peakind[0])) : #maxima if( max < peakind[0][k][1]) : max = peakind[0][k][1] if max > threshold: if(type(peakind[0][k][1]) == np.float64): max_tab = [peakind[0][k][0], peakind[0][k][1]] else: max_tab = [peakind[0][k][0], peakind[0][k][1][0]] min = 0 min_tab = [np.nan, np.nan] for l in range(len(peakind[1])) : #minima if( min > peakind[1][l][1]) : min = peakind[1][l][1] if min < - threshold: if(type(peakind[1][l][1]) == np.float64): min_tab = [peakind[1][l][0], peakind[1][l][1]] else: min_tab = [peakind[1][l][0], peakind[1][l][1][0]] extr_tab = [np.nan, np.nan] #extremum if min_tab[1] is np.nan: extr_tab = max_tab else : if(max_tab[1] > np.abs(min_tab[1])) : extr_tab = max_tab if max_tab[1] is np.nan: extr_tab = min_tab else : if(max_tab[1] <= np.abs(min_tab[1])) : extr_tab = min_tab peak_found.update({col : [max_tab,min_tab, extr_tab] }) ' Save result ' peak_found.update({'Time window' : pd.Series(['maximum', 'minimum', 'extremum'])}) peak_found_dt = pd.DataFrame(peak_found) peak_found_dt.set_index('Time window', inplace = True) peak_found_dt = peak_found_dt.T if plot : plt.ion() PeakPicking_plot(peak_found_dt, matrix, tau_max, ele_per_sec, plot_on_mat) if sorted_peak : result = PeakPicking_sortResult(peak_found_dt) else : result = peak_found_dt return result
def compute(self): """ It computes peak picking from cross correlation matrix :returns: pd.DataFrame -- if sorted_peak is False, peaks found organized per Maximin, Minimum and Extremum :returns: pd.DataFrame -- if sorted_peak is True, peaks found organized by type of Lag and Magnitude (positive or negative) """ 'initialize parameters' lag_vect = self._corr_matrix.index peak_found = {} for col in self._corr_matrix : curr_corr = self._corr_matrix[col].values peakind = PeakDetect.peakdetect(curr_corr, lag_vect, self._lookahead, self._delta) 'Keep only the highest correlation' max = 0 max_tab = [np.nan, np.nan] for k in range(len(peakind[0])) : #maxima if( max < peakind[0][k][1]) : max = peakind[0][k][1] if max > self._threshold: if(type(peakind[0][k][1]) == int): max_tab = [peakind[0][k][0], peakind[0][k][1]] else: max_tab = [peakind[0][k][0], peakind[0][k][1][0]] min = 0 min_tab = [np.nan, np.nan] for l in range(len(peakind[1])) : #minima if( min > peakind[1][l][1]) : min = peakind[1][l][1] if min < - self._threshold: if(type(peakind[1][l][1]) == int): min_tab = [peakind[1][l][0], peakind[1][l][1]] else: min_tab = [peakind[1][l][0], peakind[1][l][1][0]] extr_tab = [np.nan, np.nan] #extremum if min_tab[1] is np.nan: extr_tab = max_tab else : if(max_tab[1] > np.abs(min_tab[1])) : extr_tab = max_tab if max_tab[1] is np.nan: extr_tab = min_tab else : if(max_tab[1] <= np.abs(min_tab[1])) : extr_tab = min_tab peak_found.update({col : [max_tab,min_tab, extr_tab] }) ' Save result ' peak_found.update({'Time window' : pd.Series(['maximum', 'minimum', 'extremum'])}) self._peak_found = pd.DataFrame(peak_found) self._peak_found.set_index('Time window', inplace = True) self._peak_found = self._peak_found.T if self._plot : plt.ion() self.plot() if self._sorted_peak : result = self.sort_peakPickingResult() else : result = self._peak_found return result