def set_maximums_and_minimums(self):
        x, y, z = zip(*self.plot)
        self.x = numpy.array(x)
        self.y = numpy.array(y)

        self.base = peakutils.baseline(self.y, 2)
        self.indices = peakutils.indexes(numpy.subtract(self.y,self.base), thres=0.3, min_dist=10)

        self.polar_side_maximums = []
        for i in range(len(self.indices)):
            self.polar_side_maximums.append(self.plot[self.indices[i]])

        # invert plot to find minimums
        radius_range = self.y.max()-self.y.min()
        inverted_y_plot = []
        for i in range(len(self.y)):
            difference_from_range = radius_range - self.y[i]
            inverted_y_plot.append(difference_from_range + self.y.min())

        self.inverted_y_plot = numpy.array(inverted_y_plot)

        self.min_base = peakutils.baseline(self.inverted_y_plot, 2)
        self.min_indices = peakutils.indexes(numpy.subtract(self.inverted_y_plot,self.min_base), thres=0.3, min_dist=10)

        self.polar_side_minimums = []
        for i in range(len(self.min_indices)):
            self.polar_side_minimums.append(self.plot[self.min_indices[i]])
Exemplo n.º 2
0
    def set_maximums_and_minimums(self):
        x, y, z = zip(*self.plot)
        self.x = numpy.array(x)
        self.y = numpy.array(y)

        self.base = peakutils.baseline(self.y, 2)
        self.indices = peakutils.indexes(numpy.subtract(self.y, self.base),
                                         thres=0.3,
                                         min_dist=10)

        self.polar_side_maximums = []
        for i in range(len(self.indices)):
            self.polar_side_maximums.append(self.plot[self.indices[i]])

        # invert plot to find minimums
        radius_range = self.y.max() - self.y.min()
        inverted_y_plot = []
        for i in range(len(self.y)):
            difference_from_range = radius_range - self.y[i]
            inverted_y_plot.append(difference_from_range + self.y.min())

        self.inverted_y_plot = numpy.array(inverted_y_plot)

        self.min_base = peakutils.baseline(self.inverted_y_plot, 2)
        self.min_indices = peakutils.indexes(numpy.subtract(
            self.inverted_y_plot, self.min_base),
                                             thres=0.3,
                                             min_dist=10)

        self.polar_side_minimums = []
        for i in range(len(self.min_indices)):
            self.polar_side_minimums.append(self.plot[self.min_indices[i]])
def calculate_ideal_baseline(np_array):
    p = []
    for i in range(20):
        b = peakutils.baseline(np_array, i)
        res = np_array - b
        p += [calcProbability_conservative_np(res)]
    best = np.array(p).argmax() + 1
    baseline = peakutils.baseline(np_array, best)
    return baseline
Exemplo n.º 4
0
    def root_baseline_correction(self, square=False):
        left, right = self.split_y_into_halfs()
        left = SfgSpectrum.remove_nan_and_negative(np.sqrt(left))
        right = SfgSpectrum.remove_nan_and_negative(np.sqrt(right))

        left_base = peakutils.baseline(left, deg=1)
        right_base = peakutils.baseline(right, deg=1)
        # todo: the general shape concat(left - left_base, right - right_base) may be abstracted
        return np.concatenate([left - left_base, right - right_base])
Exemplo n.º 5
0
def find_peaks(x, thresh_from_baseline, min_dist=1):
    """ Algorithm for detecting peaks above the baseline.
    A peak should be `thresh_from_baseline` above the baseline to be detected.

    Parameters
    ----------
    x : array
        Input array
    thresh_from_baseline : float
        Threshold for detecting peaks from the baseline, in dB
    min_dist : int
        Minimum distance between peak indices - Default : 1

    Returns
    -------
    peak_indexes_sel : array
        Peak indices
    peak_amp : array
        Peak amplitudes

    """
    if not HAS_PEAKUTILS:
        raise ImportError('peakutils is not installed/available')
    x_scaled, old_range = peakutils.prepare.scale(x, (0, 1))
    x_baseline = peakutils.baseline(x_scaled)
    thresh_norm = thresh_from_baseline / np.diff(old_range)
    x_corrected = (x_scaled - x_baseline)
    # thresh_norm_scaled = thresh_norm * (x_corrected.max() - x_corrected.min())
    peak_indexes = peakutils.indexes(x_corrected, min_dist=min_dist)
    peak_indexes_sel = peak_indexes[x_corrected[peak_indexes] > thresh_norm]
    peak_amp = x[peak_indexes_sel]
    return peak_indexes_sel, peak_amp
Exemplo n.º 6
0
def background_poly(separation: Separation,
                    poly_order=3,
                    skip_start=0,
                    skip_end=0):
    """
    Calculates a polynomial fit of the electropherogram
    :param separation: separation to analyze
    :param poly_order: polynomial order to use for the background
    :param skip_start: number of points to skip at start of separation
    :param skip_end: number of points to skip at the end of the separation
    :return: rfu_adjusted background, baseline
    :rtype: tuple
    """
    # If polynomial is too large GigaCE freezes, this is a catch to prevent freezing
    assert poly_order < 10, "Poly Order to Large, please select reasonable polynomial"
    assert skip_end >= 0 and skip_start >= 0, "Skip parameters must be greater than 0"

    if skip_end == 0:
        skip_end_neg = None
    else:
        skip_end_neg = -skip_end

    base = peakutils.baseline(
        np.array(separation.rfu[skip_start:skip_end_neg]), poly_order)
    x_total = np.linspace(0,
                          len(base) + skip_start + skip_end,
                          len(base) + skip_end + skip_start)
    fit = np.polyfit(x_total[skip_start:skip_end_neg], base, poly_order)
    p = np.poly1d(fit)
    base = p(x_total)
    baseline = list(base)
    return np.subtract(separation.rfu, baseline), baseline
Exemplo n.º 7
0
def processRecording(data):
    
    z = scipy.signal.savgol_filter(data, 11, 3)
    data2 = np.asarray(z,dtype=np.float32)
    a = len(data)
    base = peakutils.baseline(data2, 2)
    
    
    
    #x = np.linspace(0,a-1, dtype ='int', num = a)  
    
    y = data2 - base
    #y=data
   
    directory = tk.filedialog.asksaveasfilename(defaultextension=".xls", filetypes=(("Excel Sheet", "*.xls"),("All Files", "*.*") ))
    if directory is None: # asksaveasfile return `None` if dialog closed with "cancel".
        return

    
    
    book = xlwt.Workbook(encoding="utf-8")
    sheet1 = book.add_sheet("Sheet 1")
    for i in range(a):
       
        sheet1.write(i,0,i)
        sheet1.write(i,1,y[i])
    
    
    
    book.save(directory)
Exemplo n.º 8
0
def get_baseline(s, var="radiance", Iunit=None):
    """Calculate and returns a baseline

    Parameters
    ----------
    s: Spectrum
        Spectrum which needs a baseline

    var: str
        on which spectral quantity to read the baseline. Default ``'radiance'``.
        See :py:data:`~radis.spectrum.utils.SPECTRAL_QUANTITIES`

    Returns
    -------
    baseline: Spectrum
        Spectrum object where intenisity is the baseline of s is computed by peakutils

    See Also
    --------

    :py:func:`~radis.spectrum.operations.sub_baseline`
    """
    import peakutils

    w1, I1 = s.get(var=var, Iunit=Iunit)
    baseline = peakutils.baseline(I1, deg=1, max_it=500)
    baselineSpectrum = Spectrum.from_array(w1,
                                           baseline,
                                           var,
                                           unit=Iunit,
                                           name=s.get_name() + "_baseline")
    return baselineSpectrum
def generate_standard_curve(file, ladder):

	#Finds peaks in ladder lane and calculates STD curve.
	#This can then be used to calculate the molecular weight of samples


	x= pandas.read_csv(file, delim_whitespace=True)
	x = x.ix[1:]
	np_array = np.array(x["Y"])
	base = peakutils.baseline(np_array,2)
	indexes = peakutils.indexes(np_array,thres=0.1, min_dist=25) #Adjust these if peak calling fails for ladder lane!


	#plt.plot(np_array)
	#plt.show()

	print indexes

	if len(indexes) == len(ladder): #If we have found the wrong number of peaks

		max_STD =  len(np_array)
		rf = indexes/len(np_array)
		ladder = np.array(ladder)
		ladder = np.log(ladder) #gets logs for log(molecular weights) vs distance linear regression.
		
		slope, intercept, r_value, pvalue, std_err = stats.linregress(rf,ladder) #Do some linear regression to get line statistics

		return [slope, intercept, r_value, max_STD]

	else:

		return "Error"
def get_peaks(file, thres_num, minimum, max_STD):

	#This function finds the peaks in data given to it.
    #Only used on sample lanes not ladder lane.
    #Variables thres_num and minimum cam be adjusted if neccecary.

	x= pandas.read_csv(file, delim_whitespace=True)

	x = x.ix[1:] #First row in bad so remove

	np_array = np.array(x["Y"])

	plt.plot(np_array)
	plt.show()

	base = peakutils.baseline(np_array,2) #remove baseline to get better peak calling

	indexes = peakutils.indexes(np_array,thres=thres_num, min_dist=minimum) # find peaks


	#print indexes

	rf = indexes/max_STD

	return rf
Exemplo n.º 11
0
def baseline(values, min_max="min", deg=7, ema_window=7, roll_window=7,
             max_it=200, tol=1e-4):
    """

    Args:
        values (iterable(int)):
        min_max (str): Whether to calculate an upper or lower baseline.
        deg (int): The degree of the polynomial fitting.
        ema_window (int): Window for the exponential moving average.
        roll_window (int): Window for the simple moving average.
        max_it (int): Number of iterations for `peakutils.baseline`.
        tol (float): Least amount of change before termination of fitting \
                     in `peakutils.baseline`.

    Returns:
        np.array: the baseline.
    """

    if min_max == "min":
        bound = np.min
    elif min_max == "max":
        bound = np.max
    else:
        raise NotImplementedError

    ema_baseline = _ema_baseline(values, bound, ema_window, roll_window)
    peakutils_baseline = peakutils.baseline(values, deg=deg,
                                            max_it=max_it,
                                            tol=tol)

    # Give double the weight to peakutil's values
    return (2/3 * np.mean([peakutils_baseline * 4/3,
                    ema_baseline * 2/3], 0)
            + 1/3 * _als_baseline(values))
Exemplo n.º 12
0
def get_baseline(s, var='radiance', wunit='nm', medium='air', Iunit=None):
    '''Calculate and returns a baseline 

    Parameters    
    ----------
    s: Spectrum
        Spectrum which needs a baseline

    Returns    
    -------
    baseline: Spectrum
        Spectrum object where intenisity is the baseline of s is computed by peakutils
        
    See Also
    --------
    
    :py:func:`~radis.spectrum.operations.sub_baseline`
    '''
    import peakutils
    w1, I1 = s.get(var=var, Iunit=Iunit, wunit=wunit, medium=medium)
    baseline = peakutils.baseline(I1, deg=1, max_it=500)
    baselineSpectrum = Spectrum.from_array(w1,
                                           baseline,
                                           var,
                                           waveunit=wunit,
                                           unit=Iunit,
                                           conditions={'medium': medium},
                                           name=s.get_name() + '_baseline')
    return baselineSpectrum
Exemplo n.º 13
0
    def __init__(self, w, u, thresh, window):
        """
        AutoPeakSelector constructor.

        Parameters
        ----------
        w : ndarray
            Array of frequency data.
        u : ndarray
            Array of the real component of the frequency response.
        thresh : float
            Threshold for minimum amplitude cutoff in peak selection.
        window : float
            Window size for local non-maximum supression.

        """
        self.thresh = thresh
        self.window = window
        f = sp.interpolate.interp1d(w, u)

        self.w = np.linspace(w.min(), w.max(),
                             int(len(w) * 100))  # arbitrary upsampling
        self.u = f(self.w)

        self.u_smoothed = sp.signal.savgol_filter(self.u, 11, 4)

        self.baseline = peakutils.baseline(self.u_smoothed, 0)[0]

        self.peaks = Peaks()
Exemplo n.º 14
0
def get_spike_times_for_cc(abfdata, sweep_num, refrac=0.015, cleanup=True):
    spike_times = []
    for channel in range(abfdata.channelCount):
        abfdata.setSweep(sweep_num, channel=channel)
        thresholds = [np.mean(peakutils.baseline(abfdata.sweepY[int(len(abfdata.sweepY)/10): int(len(abfdata.sweepY)*2/3)])) + 25, -50]
        peaks, _ = find_peaks(abfdata.sweepY, height=max(thresholds))
        spike_times = spike_times + [abfdata.sweepX[point] for point in peaks]
    spike_times = sorted(spike_times)

    if not cleanup:
        return spike_times

    for p in range(10):
        to_remove = []
        next_pass = True
        for i, t in enumerate(spike_times):
            if next_pass:
                next_pass = False
                pass
            else:
                if t - spike_times[i-1] < refrac:
                    to_remove.append(i)
                    next_pass = True
        for r in reversed(to_remove):
            spike_times.pop(r)

    return spike_times
def PPG_Peaks(data, freq, plot=False, remove_extreme=False):
	'''
	Performs the peak detection in steps. filtering (lowpass and cubic), peak detections (adaptive treshold
	and minimum distance) and lastly find the amplitudes for each peak, from the baseline removed signal.
	'''

	# filters
	_data = data
	_data = lowpass_butter_filter(_data)
	_data = extreme_removal(_data) if remove_extreme else _data
	_data = cubing_filter(_data)

	# peak detection provided by peakutils package, it uses adaptive treshold and minimum distance
	slice = 1/3
	_peaks = indexes(_data, min_dist=freq*slice)
	peaks = [softTemplate(data, i, freq) for i in _peaks]

	# peak amps from filtered data
	amps = [_data[i] for i in peaks]

	if plot:
		b_data = data-baseline(data, 2)
		plot_data([data+10, b_data], labels=['PPG', 'PPG Baselined'], normalization=True, indice=(0,len(data)))
		#plot_data([None, b_data], peaksIndexs=[None,peaks], labels=[None,'PPG Baselined'], normalization=False, indice = (0,len(data)))
		#plot_data([None, None, _data], peaksIndexs=[None, None, _peaks], labels=[None,'PPG Baselined', 'PPG Filtered'], normalization=False, indice = (0,len(data))) 
		#plot_data([data, None, _data], peaksIndexs=[peaks, None, _peaks], labels=['PPG', 'PPG Baselined','PPG Filtered'], normalization=False, indice = (0,len(data))) 

	return peaks, amps
Exemplo n.º 16
0
 def filterRAW_by_fetures(self):
     # Filter Raw file with the theoretical chirp - Match filter
     convPk = convolve(self.raw_record, TheoreticalChirp[::-1])
     convPk = convPk[2500:]  # Crop the burst chirp
     r = arange(0, len(convPk)).dot(
         Cs / (2.0 * Fs))  #Calculate radius per sample number
     decay = convPk * r  # inverse-square law
     # Locate peaks
     filtered = smooth(decay, window_len=11, window='hanning')  #LPF
     base = baseline(abs(filtered), 10)  #baseline fitting (bias)
     signal = abs(filtered) - base  #The normal data
     th = C * mean((signal / max(signal)))  # dynamic threshold
     pkslocs = indexes(signal, thres=th, min_dist=300)  # Peak detection
     features = []
     flag = 0
     # Genrate features
     for loc in pkslocs:
         if loc < 100:
             continue
         r_for_peak = r[loc]  # The raduis
         a = abs(fft(decay[loc - 60:loc + 60])).astype(
             float32)  # The FFT computation with 120 samples window
         features.append(SonarFeature(r_for_peak,
                                      a[:len(a) / 2]))  # Packing
         flag += 1
     return features
Exemplo n.º 17
0
def subtract_baseline_series(series, deg):
    """
    Subtracts baseline from plot.
    :param series: Pandas Series, to be manipulated
    :param deg: Degree of polynomial degree for the baseline correction
    :return: Pandas Series, after correction
    """
    return series - peakutils.baseline(series, deg)
Exemplo n.º 18
0
 def calc_baseline(self, order=0):
     """
     Get Baseline
     :param order: polynomial order
     :return: baseline array np.ndarray()
     """
     self.baseline = np.zeros(self.y.shape)
     self.baseline = peakutils.baseline(self.y, deg=order)
Exemplo n.º 19
0
def get_baseline(intense, time, smooth_window=7, smooth_poly=1,
                 baseline_poly=1, signal_weight=0.9, title='TIC', pola=''):
    # Savitzky–Golay滤波平滑
    # 获得底部baseline
    smooth = signal.savgol_filter(intense, smooth_window,
                                  smooth_poly, mode='nearest')
    baseline_normal = peakutils.baseline(smooth, baseline_poly)
    # 获得顶部baseline
    intense_reverse = [-i for i in intense]
    smooth_reversed = signal.savgol_filter(intense_reverse, smooth_window, smooth_poly, mode='nearest')
    baseline_reversed = peakutils.baseline(smooth_reversed, baseline_poly)
    # 获得切割线,底部基线权重较大
    midline = (baseline_normal - signal_weight * baseline_reversed) / 2
    # 绘图
    plot_detect_result(intense, time, smooth, baseline_normal,
                       baseline_reversed, midline, title, pola)
    return smooth, midline
Exemplo n.º 20
0
 def calc_baseline(y):
     """
     Calculate baseline of the well. Used for peak identification.
     """
     try:
         baseline = peakutils.baseline(y)
         return baseline
     except Exception:
         return np.NaN
 def update_baseline(self, val): # Remove the baseline from the data, plot the data without the baseline
     Mag = self.Mag
     freqSmooth = self.freqSmooth
     base = peakutils.baseline(1/Mag, polyOrder)
     plot2.set_data(freqSmooth/1e6, 20*np.log10(Mag*base))
     ax2.relim()
     ax2.autoscale_view() 
     fig.canvas.draw_idle()
     self.base = base
Exemplo n.º 22
0
    def plotit(self):
        i = self.getTic()

        base = pandas.DataFrame()
        base['retention_time'] = i['retention_time']
        base['abundance'] = peakutils.baseline(i['abundance'], deg=6)

        #i['abundance'] = i['abundance'] - base['abundance']
        mat, mit = putil.PUtil.peaksfr(i, 'abundance', 'retention_time')
        fig, ax = plt.subplots()
        ax.plot(i.get('retention_time'), i.get('abundance'))
        #mat = pandas.DataFrame(np.array(maxtab), columns=['retention_time', 'abundance'])
        #mit = pandas.DataFrame(np.array(mintab), columns=['retention_time', 'abundance'])
        ax.scatter(mat['retention_time'], mat['abundance'], color='blue')
        ax.scatter(mit['retention_time'], mit['abundance'], color='green')
        #base = pandas.DataFrame((i['retention_time'], peakutils.baseline(i['abundance'],deg=6)) , columns=['retention_time', 'abundance'])

        #print(base)
        #tot = mat.append(mit)
        areas = []
        n = 0
        cls = ['yellow', 'cyan']
        for m in mat['retention_time']:

            nearest = putil.PUtil.strad(mit, 'retention_time', m)
            strt = nearest['retention_time'].min()
            end = nearest['retention_time'].max()
            #print (m, strt, end)
            istrt = putil.PUtil.nearest(i, 'retention_time', strt).iloc[0].name
            iend = putil.PUtil.nearest(i, 'retention_time', end).iloc[0].name
            #print (istrt, iend)
            #print(i['retention_time'][istrt:iend])
            ax.fill_between(i['retention_time'][istrt:iend],
                            i['abundance'][istrt:iend],
                            color=cls[n % 2])
            #print (m, nearest)
            #print (base.index.get_loc(nearest[0].index))
            #nearest.iloc[0]['abundance'] - base.loc[nearest.iloc[0].name]['abundance']
            #nearest.iloc[1]['abundance'] - base.loc[nearest.iloc[1].name]['abundance']
            areas.append(
                scipy.integrate.trapz(i['abundance'][istrt:iend],
                                      i['retention_time'][istrt:iend]))
            n += 1
        aread = pandas.DataFrame(np.array(areas), columns=['area'])
        mat['area'] = aread['area']
        ax.plot(base['retention_time'], base['abundance'], color='red')
        mat['area'] = mat['area'] / mat['area'].max()
        ax.set(xlabel='time', ylabel='abundance', title='Ions')
        for idx, r in mat.iterrows():
            ax.annotate('%.3f' % r['area'],
                        xy=(r['retention_time'] + 0.05,
                            r['abundance'] + 10000))
        ax.grid()

        #fig.savefig("test.png")
        plt.show()
        return ax
Exemplo n.º 23
0
def find_peaks(x, thresh_from_baseline, min_dist=1):
    x_scaled, old_range = peakutils.prepare.scale(x, (0, 1))
    x_baseline = peakutils.baseline(x_scaled)
    thresh_norm = thresh_from_baseline / np.diff(old_range)
    x_corrected = (x_scaled - x_baseline)
    # thresh_norm_scaled = thresh_norm * (x_corrected.max() - x_corrected.min())
    peak_indexes = peakutils.indexes(x_corrected, min_dist=min_dist)
    peak_indexes_sel = peak_indexes[x_corrected[peak_indexes] > thresh_norm]
    peak_amp = x[peak_indexes_sel]
    return peak_indexes_sel, peak_amp
Exemplo n.º 24
0
def qrs_peak_detect(seg, qonset, soffset):
    base = peakutils.baseline(seg, 2)
    qrs_seg = seg[qonset:soffset + 1]
    pos_indexs = peakutils.peak.indexes(qrs_seg, thres=0.5,
                                        min_dist=20) + qonset
    neg_indexs = peakutils.peak.indexes(-qrs_seg, thres=0.3,
                                        min_dist=5) + qonset
    pos_indexs = [x for x in pos_indexs if seg[x] > base[x]]
    neg_indexs = [x for x in neg_indexs if seg[x] < base[x]]
    return pos_indexs, neg_indexs, base
Exemplo n.º 25
0
def background(x, n=2, type='poly', max_it=100, tol=0.001):
    """Apply baseline reduction using baseline polynomial fitting or SNIP
    algorithm. This is a wrapper around peakutils.baseline and
    silx.math.fit.snip1d.
    """
    if type not in ['poly', 'snip']:
        raise ValueError("type must be 'poly' or 'snip'")
    elif type == 'poly':
        return baseline(x, deg=n, max_it=max_it, tol=tol)
    else:
        return snip1d(x, snip_width=n)
Exemplo n.º 26
0
    def test_conditioning(self):
        data = data = load('exp')
        y = data[:, 1]
        mult = 1e-6

        while mult < 100001:
            ny = y * mult
            base = peakutils.baseline(ny, 9) / mult
            self.assertTrue(0.8 < base.max() < 1.0)
            self.assertTrue(-0.1 <= base.min() < 0.1)
            mult *= 10
Exemplo n.º 27
0
def remove_baseline(x, degree=3):
    """
    Use the baseline function from peakutils to remove base line of x. which 
    iteratively performs a polynomial fitting.
    
    deg (int (default: 3))
    – Degree of the polynomial that will estimate the data baseline
    """
    baseline_values = peakutils.baseline(x, deg=degree)
    x_removed = x - baseline_values
    return baseline_values, x_removed
Exemplo n.º 28
0
    def test_conditioning(self):
        data = data = load('exp')
        y = data[:, 1]
        mult = 1e-6

        while mult < 100001:
            ny = y * mult
            base = peakutils.baseline(ny, 9) / mult
            self.assertTrue(0.8 < base.max() < 1.0)
            self.assertTrue(-0.1 <= base.min() < 0.1)
            mult *= 10
def remove_baseline(xs,
                    ys,
                    return_xs=False,
                    degree=5):  ##Returns just ys unless otherwise specified
    ###Remove baseline with polynomial
    y2 = ys + np.polyval([0.001, -0.08, degree], xs)
    # y2 = ys
    base = peakutils.baseline(y2, 2)
    if return_xs:
        return (xs, y2 - base)
    else:
        return y2 - base
Exemplo n.º 30
0
    def correct_uv_baseline(self, plot=False):
        "Returns a baseline corrected UV trace. Requires peakutils and numpy."
        import peakutils
        import numpy as np

        time, step_no, numpy_uv = np.array(
            self.get_data('TIME_MS', 'STEP_NO', 'UV_VIS'))

        baseline = peakutils.baseline(numpy_uv, 3)
        numpy_uv_corrected = numpy_uv - baseline

        return list(zip(step_no, time, numpy_uv_corrected))
Exemplo n.º 31
0
def subtract_baseline(df, deg, key=None, model=None):
    """
    Takes DataFrame of spectrum, and correct its baseline by changing the values.
    :param df: DataFrame
    :param deg: int
    :param window: int
    :return: DataFrame
    """
    df = df.copy()
    if key == LABELS['SINGLE']:
        df[LABELS['COR']] = df[model] - peakutils.baseline(
            df[LABELS['BS']], deg)

    elif key == LABELS['MS']:
        df[LABELS['COR']] = df[model] - peakutils.baseline(
            df[LABELS['BS']], deg)
        df.dropna(inplace=True)  # TODO: why?
    else:
        for col in range(len(df.columns)):
            df.iloc[:, col] = df.iloc[:, col] - peakutils.baseline(
                df.iloc[:, col], deg)
    return df
Exemplo n.º 32
0
def baseline_search(voltage, pol_degree):

    """
    Uses PeakUtil package to find the baseline of the signal and returns it as a value.
    : params voltage : 1D array containing the input signal
    : params voltage : polynomial degree used in base line fitting
    : returns base : baseline of the signal
    """

    min = np.min(voltage)
    base = peakutils.baseline(voltage-min, pol_degree, 10000, 0.05)
    base = base+min
    return base
Exemplo n.º 33
0
    def __init__(self, w, u, n, one_click=False):
        """
        PeakSelector constructor.

        Parameters
        ----------
        w : ndarray
            Array of frequency data.
        u, v : ndarray
            Arrays of the real and imaginary components of the frequency response.
        n : int
            Number of peaks to select.
        one_click : bool, optional
            Specify whether a single click will be used to select peaks (as opposed to two)

        """
        self.u = u
        self.w = w
        self.n = n
        self.one_click = one_click

        # peak container
        self.peaks = Peaks()

        # empty list to store point information from clicks
        self.points = []

        # initialize plot
        self.fig = plt.figure(1, figsize=(10, 8), dpi=150)
        ax = plt.subplot('111')

        plt.plot(w, u, linewidth=1, color='black')

        ax.spines['top'].set_color('none')
        ax.spines['left'].set_color('none')
        ax.spines['right'].set_color('none')
        ax.set_yticklabels([])
        ax.tick_params(top='off', left='off', right='off')

        ax.set_xlabel('ppm', fontsize=16, fontweight='bold')
        ax.set_xlim((self.w.max(), self.w.min()))
        self.fig.tight_layout()

        # start event listener
        self.cid = self.fig.canvas.mpl_connect('button_press_event', self)

        self.baseline = peakutils.baseline(self.u, 0)[0]

        # display the plot
        plt.show()
Exemplo n.º 34
0
    def fit_gaus(self, y, baseline_deg=0):

        if baseline_deg != 0:
            import peakutils
            base = 1.0 * peakutils.baseline(y, baseline_deg)
            y = y - base

        y -= y.min()
        n = len(y)
        x = np.arange(n)  #pixels

        p0 = [y.max(), y.argmax(), 10]
        popt, pcov = curve_fit(gaus, x, y, p0=p0)
        return popt  #a,mean,sigma
Exemplo n.º 35
0
    def test_peaks(self):
        data = load('baseline')
        x, y = data[:, 0], data[:, 1]
        n_peaks = 2

        prepared = y - peakutils.baseline(y, 3)
        idx = peakutils.indexes(prepared, thres=0.03, min_dist=5)

        for p in range(idx.size, 1):
            self.assertGreater(idx[p], 0)
            self.assertLess(idx[p], idx.size - 1)
            self.assertGreater(idx[p], idx[p - 1])

        self.assertEqual(idx.size, n_peaks)
        assert_array_almost_equal(x[idx], numpy.array([1527.3, 1529.77]))
Exemplo n.º 36
0
def benchit():
    tests = [("Small - Low noise",  make_data(200, 1.), 100),
             ("Small - High noise", make_data(200, 3.), 100),
             ("Big - Low noise",    make_data(20000, 1), 5),
             ("Big - High noise",   make_data(20000, 2.), 5)]

    for name, data, rep in tests:
        begin = timer()

        for _ in range(rep):
            y = data[1] - peakutils.baseline(data[1])
            i = peakutils.indexes(y, thres=0.4, min_dist=y.size // 5)

        end = timer()
        each = (end - begin) / rep
        print("*{}* test took {:.3f} seconds each rep".format(name, each))
def change_vgram():
    global peak_current, noise_filter, name, files, file_index, canvas, ax
    global vgramfwd, vgrambslrm, vgrambwd, vgrambsl, bsl_poly_box, first
    global dyn, x_pts, y_pts, fwdbwd_i, bsl_i, bsl_poly, rescale
    files = glob('*.csv')
    file = files[file_index]
    
    name = file[0:-4]
    df = read_csv(file)

    peak_current = 0
    # check if data are filtered or not (column names change after filtering from the original KStat output
    if 'Potential' in df.columns:
        df.CurrentFiltered = df.CurrentFiltered * -1
        potential_col = 'Potential'
    else:
        potential_col = 'potential'
    if 'CurrentFiltered' in df.columns:
        current_col = 'CurrentFiltered'
    else:
        current_col = 'current'
        df.current = df.current *-1

    if noise_filter:
        df['CurrentIIR'] = iir_noise_filter(name, df, current_col)
        current_col = 'CurrentIIR'

    #split scan into forward and backward scans   
    fwd = df[1:df[potential_col].idxmin()+1]
    bwd = df[df[potential_col].idxmin():]

    # remove previous current & baseline plots if existing and clear points, lines etc created by user
    # not executed in first iteration
    if not first:
        try:
            vgramfwd.remove()
        except:
            pass
        try:
            vgrambwd.remove()
        except:
            pass
        try:
            vgrambsl.remove()
        except:
            pass
        try:
            vgrambslrm.remove()
        except:
            pass
        for element in dyn:
            element.remove()
        dyn = []
        x_pts = []
        y_pts = []

    # set plot parameters    
    else:
        ax.invert_xaxis()
        plt.xlabel('Potential [mV]')
        plt.ylabel('Current [nA]')

    # update plot title to current name
    plt.title(name)

    # Plot current according to forward/backward mode if not in baseline subtraction mode
    if (fwdbwd_i == 0 or fwdbwd_i == 1) and bsl_i != 2:
        vgramfwd, = ax.plot(fwd[potential_col], fwd[current_col]*1000000000, color= '#0039ca')
    if (fwdbwd_i == 0 or fwdbwd_i == 2) and bsl_i != 2:
        vgrambwd, = ax.plot(bwd[potential_col], bwd[current_col]*1000000000, color = '#6487e1')

    # baseline plotting: 
    if bsl_i != 0:
        # get polynomial degree from text box
        try:
            bsl_poly = int(bsl_poly_box.get())
        except:
            pass

        #compute baseline
        bsl_df = Series(baseline(fwd[current_col], deg=bsl_poly, max_it=100, tol=.0000001))

        # plot baseline with current or subtract it from current depending on baseline mode
        if bsl_i == 1:
            vgrambsl, = ax.plot(fwd[potential_col], bsl_df*1000000000, color= '#be6b12')
        if bsl_i == 2:
            bslrmvd = (fwd[current_col] - bsl_df).drop(0)
            vgrambslrm, = ax.plot(fwd[potential_col], bslrmvd*1000000000, color= '#0039ca')
    if rescale:      
        # rescale the axes to fit the data (normally automatic but does not work fully in all instances)
        ax.relim()
        ax.autoscale()
        rescale = False
    first = False
    canvas.draw()
Exemplo n.º 38
0
for y in y_axis:
	mean.append(float(basel))

highcut = basel + ((variance-basel)/15)
lowcut = basel - ((variance-basel)/15)

for a in range (0,len(two)):
	if two[a][1] > lowcut and two[a][1] < highcut:
		cut.append(two[a])


for m in range (0, len(cut)):
	cutx.append(cut[m][0])
	cuty.append(cut[m][1])

base = peakutils.baseline(y_axis, 2)
cutbase = peakutils.baseline(asarray(cuty), 2)

pyplot.figure(figsize=(10,6))

pyplot.plot(x_axis, y_axis, label="data")
pyplot.plot(x_axis, base, label = "baseline")
pyplot.plot(x_axis, mean, label= "mean")
pyplot.plot(cutx, cuty, label= "cut")
pyplot.plot(cutx, asarray(cutbase), label = "cut baseline")

pyplot.plot()
pyplot.legend()

pyplot.show()
Exemplo n.º 39
0
    #     low, up, area = findArea(peak,x_axis,y_axis,basel)
    #     peaknum = peaknum + 1


    cutx, cuty = cutdata(x_axis, y_axis,15)
    mean = meanarray(y_axis)

    x_axis = asarray(x_axis)
    y_axis = asarray(y_axis)
    cutx = asarray(cutx)
    cuty = asarray(cuty)
    mean = asarray(mean)
    withoutpeaksx = asarray(withoutpeaksx)
    withoutpeaksy = asarray(withoutpeaksy)

    base = peakutils.baseline(y_axis, 2)
    cutbase = peakutils.baseline(cuty, 2)

    pyplot.figure(figsize=(10,6))

    pyplot.plot(x_axis, y_axis, label="data")
    pyplot.plot(x_axis, base, label = "baseline")
    pyplot.plot(x_axis, mean, label= "mean")
    pyplot.plot(cutx, cuty, label= "cut")
    pyplot.plot(cutx, asarray(cutbase), label = "cut baseline")
    pyplot.plot(withoutpeaksx, withoutpeaksy, label = "without peaks")


    pyplot.plot()
    pyplot.legend()