def test_interpolate_bounds(self): x = np.arange(5) y = np.array([0, 0, 1, 0, 0]) with warnings.catch_warnings(record=True) as record: for w in range(1, 10): peakutils.interpolate(x, y, [2], width=w) self.assertGreater(len(record), 0)
def peaks(spec, wls, thres=0.5, unique_solution=True, min_dist=-1, refinement=True, ignore_phony_refinements=True): import peakutils indexes = peakutils.indexes(spec, thres, min_dist=min_dist) if unique_solution: #we only want the highest amplitude peak here! indexes = [indexes[spec[indexes].argmax()]] if refinement: peaks_x = peakutils.interpolate(wls, spec, ind=indexes) if ignore_phony_refinements: for i, p in enumerate(peaks_x): if p < wls.min() or p > wls.max(): print( 'peakutils.interpolate() yielded result outside wls range, returning unrefined result' ) peaks_x[i] = wls[indexes[i]] else: peaks_x = wls[indexes] if unique_solution: return peaks_x[0] else: return peaks_x
def find_peak(y, x=None, x_name='x_num', y_name='y', thres=0.015, min_dist=1, imprv_reso=False): if x is None: x = np.array(range(len(y))) # Note: weirdly, indexes have to be reset here to get correct peak locations x = np.array(x) y = np.array(y) _index = pku.indexes(y=y, thres=thres, min_dist=min_dist) if len(_index) != 0: _peak_y = list(y[_index]) if imprv_reso is False: _peak_x = list(x[_index]) else: _peak_x = list(pku.interpolate(x, y, ind=_index)) else: # No peaks detected _peak_y = [] _peak_x = [] peak_df = pd.DataFrame() peak_df[x_name] = _peak_x peak_df[y_name] = _peak_y peak_df.sort_values([x_name], inplace=True) peak_df.reset_index(inplace=True, drop=True) return peak_df
def LocateEdges(a_n, x, modalD): """ Locate positions of discontinuities in the data, using its Chebyshev spectral expansion coefficients Parameters ---------- a_n : 1D array, shape = (N+1,) N - highest order in the Chebyshev expansion vector of Chebyshev expansion coefficients x : 1D array, 1D grid of points in real space, where the function is evaluated modalD : 2D array, shape = (N+1, N+1) Modal Differentiation Matrix Returns ---------- c_j : 1D array, Array containing x positions of the discontinuities in the data """ edges = ChebEdgeIII(a_n, x, modalD) minmod_edge = MinMod(edges) enhanced_minmod = Enhance(a_n, x, minmod_edge, 3) idx = peakutils.indexes(absolute(enhanced_minmod)) c_j = peakutils.interpolate(x, absolute(enhanced_minmod), ind=idx) return c_j
def detect_weel_times(LSsn, decimation = 8): """ Return the passby times of the train axes param: ------ LSsn: Signal LS signal decimation: int pow of 2 decimate and filter the signal return: ------ - tPeaks: np.array passby times of the train axes """ s = LSsn.decimate(decimation) y = np.array(s) t = s.times() # define the minimal possible distance in time between two axes maxSpeed = 200/3.6 axleDistance = 1.5 dt = axleDistance/maxSpeed # minimal distance in frames min_dist = int(np.round(dt * s.fs)) # use peaksUtils module for detecting maxima indexes = peakutils.indexes(y, thres = 0.05, min_dist = min_dist) tPeaks = peakutils.interpolate(t, y, ind=indexes) print('Minimal time interval between maxima is: ', dt,',which is equvalent to ', min_dist,' samples') return tPeaks
def find_peaks(filename, show_plots=False): #subtract background and find peaks num_samp_left = 200 num_samp_right = 200 data = np.genfromtxt(filename) x = data[:, 0] y = data[:, 1] x_bg = np.hstack([x[:num_samp_left], x[-num_samp_right:]]) y_bg = np.hstack([y[:num_samp_left], y[-num_samp_right:]]) model = LinearModel() params = model.guess(y_bg, x=x_bg) out = model.fit(y_bg, x=x_bg, params=params) if show_plots: plt.plot(x, y) plt.plot(x_bg, y_bg, '.') y_fit = out.model.func(x, **out.best_values) data = y - y_fit if show_plots: plt.plot(x, data) plt.show() indexes = peakutils.indexes(-data, thres=0.25, min_dist=65) print(indexes) pplot(x, data, indexes) peaks_x = peakutils.interpolate(x, data, ind=indexes) print(peaks_x)
def time_offset(time_v, signal, high_th, low_th, offset): """ shift a trace so that the steepest point is at time 0 steepest points must exist within discriminator window """ # start by finding the max of the derivative dt = np.diff(time_v)[0] # derivative of the signal, smoothed d_signal = savgol_filter(np.diff(signal), 301, 3) # ax = plt.gca() # ax2 = ax.twinx() # ax2.plot(d_signal/np.max(d_signal), color='green') # use peaks only if they are legitimate edges identified by the set-reset switch mask = pu.disc_peak_full(signal, high_th, low_th, offset) # print np.where(mask==True) # print peakutils.indexes(d_signal, .5, 3000) # find peaks in the derivative to find the steepest point idx = np.array([i for i in peakutils.indexes(d_signal, .5, 3000) if mask[i]==True]) # print idx if len(idx) == 0: return [np.nan for _ in time_v] idx_s = np.flipud(idx[d_signal[idx].argsort()])[0] try: time_p = peakutils.interpolate(time_v[:-1], d_signal, [idx_s])[0] except (RuntimeError, ValueError) as e: time_p = time_v[idx_s] n_shift = int(time_p / dt) return pa.shift(signal, - n_shift+int(len(signal)/2))
def _find_peak(self, y: np.array, thres, min_dist, imprv_reso: bool, x=None): """""" if x is None: x = np.array(range(len(y))) else: x = np.array(x) if x.shape != y.shape: raise ValueError( "The length ({}) of 'x' is not equal the length ({}) of 'y'" .format(len(x), len(y))) peak_index = pku.indexes(y=y, thres=thres, min_dist=min_dist) if len(peak_index) != 0: _peak_x_num = self.img_num[peak_index] _peak_y = y[peak_index] if imprv_reso: _peak_x = pku.interpolate(x, y, ind=peak_index) else: _peak_x = x[peak_index] else: # No peaks detected _peak_x_num = [] _peak_x = [] _peak_y = [] peak_df = pd.DataFrame() peak_df['x_num'] = _peak_x_num peak_df['x'] = _peak_x peak_df['y'] = _peak_y peak_dict = {'df': peak_df} self.peak_dict = peak_dict return peak_dict
def benchit(): tests = [("Small - Low noise", make_data(200, 1.), 100), ("Small - High noise", make_data(200, 6.), 100), ("Big - Low noise", make_data(20000, 1), 5), ("Big - High noise", make_data(20000, 6), 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.3, min_dist=y.size / 10) peakutils.interpolate(data[0], y, i) end = timer() each = (end-begin) / rep print("*{}* test took {:.3f} seconds each rep".format(name, each))
def benchit(): tests = [("Small - Low noise", make_data(200, 1.), 100), ("Small - High noise", make_data(200, 6.), 100), ("Big - Low noise", make_data(20000, 1), 5), ("Big - High noise", make_data(20000, 6), 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.3, min_dist=y.size / 10) peakutils.interpolate(data[0], y, i) end = timer() each = (end - begin) / rep print("*{}* test took {:.3f} seconds each rep".format(name, each))
def peak_find(spec_df: pd.DataFrame, freq_col="Frequency", int_col="Intensity", thres=0.015, min_dist=10): """ Wrapper for peakutils applied to pandas dataframes. First finds the peak indices, which are then used to fit Gaussians to determine the center frequency for each peak. Parameters ---------- spec_df: dataframe Pandas dataframe containing the spectrum information, with columns corresponding to frequency and intensity. freq_col: str, optional Name of the frequency column in `spec_df` int_col: str, optional Name of the intensity column in `spec_df` thres: float, optional Threshold for peak detection Returns ------- peak_df Pandas dataframe containing the peaks frequency/intensity """ peak_indices = peakutils.indexes( spec_df[int_col].to_numpy(), thres=thres, thres_abs=True, min_dist=min_dist ) frequencies = peakutils.interpolate( x=spec_df[freq_col].to_numpy(), y=spec_df[int_col].to_numpy(), ind=peak_indices, width=11 ) # Get the peaks if we were just using indexes direct_df = spec_df.iloc[peak_indices] direct_df.reset_index(inplace=True) direct_freqs = direct_df[freq_col].values # Calculate the difference in fit vs. approximate peak # frequencies differences = np.abs(direct_df[freq_col] - frequencies) intensities = spec_df.iloc[peak_indices][int_col].values peak_df = pd.DataFrame( data=list(zip(frequencies, direct_freqs, intensities)), columns=["Frequency", "Peak Frequencies", int_col] ) # Take the indexed frequencies if the fit exploded # and deviates significantly from the original prediction peak_df.update( direct_df.loc[differences >= 0.2] ) # Use 1sigma as the detection threshold; remove everything else! peak_df = peak_df.loc[ peak_df[int_col] >= thres ] peak_df.reset_index(drop=True, inplace=True) return peak_df
def EdgeDetectErr(N, pos, width, x): c2s, s2c = Vandermonde(N) modalD = ModalD(c2s) err_e = np.empty((len(pos), len(width))) err_me = np.empty((len(pos), len(width))) err_eme = np.empty((len(pos), len(width))) for idxl, loc in enumerate(pos): for idxw, w in enumerate(width): steps = np.array([loc, loc + w]) func = lambda x: 1.0 if (x >= loc and x <= (loc + w)) else 0 a_n = Decompose(func, c2s) edge = ChebEdge(a_n, x, modalD) enhanced_edge = Enhance(a_n, x, edge[:, -1], 4) idx_e = peakutils.indexes(absolute(enhanced_edge)) peaks_e = peakutils.interpolate(x, enhanced_edge, ind=idx_e) minmod_edge = MinMod(edge) idx_me = peakutils.indexes(absolute(minmod_edge)) peaks_me = peakutils.interpolate(x, minmod_edge, ind=idx_me) enhanced_minmod = Enhance(a_n, x, minmod_edge, 4) idx_eme = peakutils.indexes(absolute(enhanced_minmod)) peaks_eme = peakutils.interpolate(x, enhanced_minmod, ind=idx_eme) if peaks_e.shape != steps.shape: err_e[idxl, idxw] = np.nan else: err_e[idxl, idxw] = np.average(absolute(steps - peaks_e)) if peaks_me.shape != steps.shape: err_me[idxl, idxw] = np.nan else: err_me[idxl, idxw] = np.average(absolute(steps - peaks_me)) if peaks_eme.shape != steps.shape: err_eme[idxl, idxw] = np.nan else: err_eme[idxl, idxw] = np.average(absolute(steps - peaks_eme)) return err_e, err_me, err_eme
def plotandoFreq(sinal, fs): # sinal = sinal[:,0] freq, t = sig.calcFFT(sinal, fs) #Encontrando a posição dos picos indexes = peakutils.indexes(t, thres=3, min_dist=70) #Picos - lista peaks_x = peakutils.interpolate(freq, t, ind=indexes) pplot(freq, t, indexes) plt.title('Furier') plt.show()
def fitNpeaks(f, y, npeaks=5, thres=0.02, min_dist=5, width=300, plot_prefix=None, model=SkewedGaussianModel, offset=True): # Guess initial peak centres using peakutils indexes = peakutils.indexes(y, thres=thres, min_dist=min_dist) peaksfound = len(indexes) assert peaksfound >= npeaks, "Looking for %s or more peaks only found %s of them!" % ( npeaks, peaksfound) peak_f = peakutils.interpolate(f, y, ind=indexes) # Oder peaks by decreaing height and keep only the first npeaks peak_heights = indexes peak_order = peak_heights.argsort()[::-1] peak_heights = peak_heights[peak_order[:npeaks]] peak_f = peak_f[peak_order[:npeaks]] amplitude_scale = 1.0 peak_amplitudes = peak_heights * amplitude_scale # This is lmfit's annoying definition of a Gaussian `amplitude' print('Initial peaks guessed at ', peak_f) # Make multipeak model peaks = [] for i in range(npeaks): prefix = 'g{:d}_'.format(i + 1) peaks.append(model(prefix=prefix)) if i == 0: pars = peaks[i].make_params(x=f) else: pars.update(peaks[i].make_params()) if model == SkewedGaussianModel: pars[prefix + 'center'].set(peak_f[i], min=f.min(), max=f.max()) pars[prefix + 'sigma'].set(width, min=0.1 * width, max=10 * width) pars[prefix + 'gamma'].set(0, min=-5, max=5) pars[prefix + 'amplitude'].set(peak_amplitudes[i]) elif model == GaussianModel: pars[prefix + 'center'].set(peak_f[i], min=f.min(), max=f.max()) pars[prefix + 'sigma'].set(width, min=0.1 * width, max=10 * width) pars[prefix + 'amplitude'].set(peak_amplitudes[i]) model = peaks[0] for i in range(1, npeaks): model += peaks[i] if offset: model += ConstantModel() pars.update(ConstantModel().make_params()) pars['c'].set(0) # Fit first spectrum, creating the ModelResult object which will be used over and over fit = model.fit(y, pars, x=f) return fit
def find_grid_rotation(bimg, theta_range = (-10, 10), ntheta=None, scale=0.1): mintheta, maxtheta = min(theta_range), max(theta_range) if ntheta is None: ntheta = (maxtheta - mintheta) * 4 + 1 theta = np.linspace(mintheta, maxtheta, ntheta) sinogram = transform.radon(transform.rescale(bimg, scale=scale), theta, circle=False) sinogram_max = np.max(sinogram, axis=0) peak_indices = peakutils.indexes(sinogram_max, thres=0.999) interpolated_peaks = peakutils.interpolate(theta, sinogram_max, ind=peak_indices) return sinogram, interpolated_peaks[0]
def compute_pitches(self, display_plot_frame=-1): overall_chromagram = Chromagram() for frame, x_frame in enumerate(frame_cutter(self.x, self.ham_samples)): x = wfir(x_frame, self.fs, 12) x_hi = _highpass_filter(x, self.fs) x_hi = numpy.clip(x_hi, 0, None) # half-wave rectification x_hi = lowpass_filter(x_hi, self.fs, 1000) # paper wants it x_lo = lowpass_filter(x, self.fs, 1000) x_sacf = _sacf([x_lo, x_hi]) x_esacf, harmonic_elim_plots = _esacf(x_sacf, self.n_peaks_elim, True) peak_indices = peakutils.indexes(x_esacf, thres=self.peak_thresh, min_dist=self.peak_min_dist) peak_indices_interp = peakutils.interpolate(numpy.arange( x_esacf.shape[0]), x_esacf, ind=peak_indices) chromagram = Chromagram() for i, tau in enumerate(peak_indices_interp): pitch = self.fs / tau try: note = librosa.hz_to_note(pitch, octave=False) chromagram[note] += x_esacf[peak_indices[i]] except ValueError: continue overall_chromagram += chromagram if frame == display_plot_frame: _display_plots( self.clip_name, self.fs, self.ham_samples, frame, x, x_lo, x_hi, x_sacf, x_esacf, harmonic_elim_plots, peak_indices, peak_indices_interp, ) return overall_chromagram
def find_peaks_peakutils(uid='8537b7', x='stage_x', y='pil300KW_stats1_total', plot=True): xx = np.array(db[uid].table()[x]) yy = np.array(db[uid].table()[y]) peak_idx = peakutils.interpolate(xx, yy, width=0) if plot: plt.plot(xx, yy) plt.grid() plt.scatter(xx[peak_idx], yy[peak_idx], c='r') print(f'Peaks indices: {peak_idx}\nX coords: {xx[peak_idx]}\nY coords: {yy[peak_idx]}') return peak_idx, xx[peak_idx], yy[peak_idx]
def interpolate_tm(self, x, y, max_i, tm): """ Performs an interpolation to fine-tune a Tm value by interpolating a gaussian function around the approximate peak position. """ try: # If tm is within cutoff, perform the interpolation if (self.temp_within_cutoff(tm)): tm = round( peakutils.interpolate(x, y, width=3, ind=[max_i])[0], 2) # Remove the denatured flag self.owner.denatured_wells.remove(self) return tm # and return the Tm else: return np.NaN # otherwise, return NaN except Exception: return np.NaN # In case of error, return NaN
def analize_spectralPeaks(self, filepath, minamp=0.001, fftsize=2048, hop_length=2048, min_midi=20, max_midi=108): import librosa import peakutils checksum = util.listToCheckSum([ filepath, fftsize, hop_length, minamp, min_midi, max_midi, 'peaks' ])[:12] filehead = '%s-%s' % (os.path.splitext( os.path.split(filepath)[1])[0], checksum) outputpath = os.path.join(self.analdir, '%s-peaks.json' % (filehead)) if os.path.exists(outputpath): fh = open(outputpath, 'r') return json.load(fh) y, sr = librosa.load(filepath, sr=None, mono=True) outputdata = {'sr': sr, 'fftsize': fftsize, 'peaks': []} halfwin = int(fftsize / 2) binsize = sr / float(fftsize) lins = np.linspace(0, halfwin - 1, num=halfwin + 1) S = np.abs(librosa.stft(y, n_fft=fftsize, hop_length=hop_length)) for fidx, frame in enumerate(np.rot90(S)): indexes = peakutils.indexes(frame, thres=minamp, min_dist=10) # interpolate indexes for better frq accuracy interp_indexes = peakutils.interpolate(lins, frame, ind=indexes) timesec = fidx * (float(hop_length) / sr) framepeaks = [] for ii in interp_indexes: if ii < 0: continue midi = util.frq2Midi(ii * binsize) if midi < min_midi or midi > max_midi: continue iamp = util.ampToDb( np.interp(ii, lins, frame) * (2. / float(fftsize))) framepeaks.append([iamp, midi]) framepeaks.sort(reverse=True) outputdata['peaks'].append([timesec, framepeaks]) fh = open(outputpath, 'w') json.dump(outputdata, fh) return outputdata
def confocal_data_reader(self, file_path): if file_path not in self.labels: try: index = difflib.get_close_matches( file_path, list(self.stimulie_dataframe[0]), cutoff=0.3) self.labels[file_path] = self.stimulie_dataframe[ self.stimulie_dataframe[0] == index[0]].dropna( axis=1).values[0][1:] if len(self.labels[file_path]) < 1: self.toolbar.delete_plot() return except Exception as e: pass file_path2, ind = file_path.split('~') self.data = pandas.read_csv(file_path2, ',', header=1) del self.data["Axis [s]"] self.data = self.data.replace(to_replace=',', value='.', regex=True) self.data = self.data.ix[:, int(ind)] data_ratio = np.array(self.data) if file_path not in self.data_memory: if len(self.labels) > 0: indexes = peakutils.indexes(data_ratio, thres=0.1, min_dist=250, max_ammount=len( self.labels[file_path])) else: indexes = peakutils.indexes(data_ratio, thres=0.1, min_dist=250) try: indexes = peakutils.interpolate(self.data.index.values, data_ratio, ind=indexes) except Exception: pass indexes = [int(elem) for elem in indexes] baseline_value = self.data[0:60].median() self.data_memory[file_path] = [indexes, baseline_value] self.data = self.data.to_frame() self.data.columns = ['ratio'] self.actionSignal.setChecked(False) if self.actionNormalize.isChecked(): self.normalize_data() self.plotter(file_path)
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)
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 getFrequencies(rate, samples): # plt.figure(1) # plt.xlabel('samples') # plt.ylabel('amplitude') # plt.plot(samples) # plt.show() len_data = len(samples) channel_1 = np.zeros(2**(int(np.ceil(np.log2(len_data))))) channel_1[0:len_data] = samples fourier = np.fft.fft(channel_1) w = np.linspace(0, 44100, len(fourier)) # First half is the real component, second half is imaginary fourier_to_plot = abs(fourier[0:len(fourier) // 2]) w = w[0:len(fourier) // 2] indexesOfPeaks = peakutils.indexes(fourier_to_plot) peakFrequencies = [w[u] for u in indexesOfPeaks] xxx = peakutils.interpolate(w, fourier_to_plot, ind=indexesOfPeaks) pitchPeaks = [freqToPitch(freq) for freq in peakFrequencies] funFreqs = getFundamentalFrequencies(peakFrequencies) if os.getenv("PRINT_FREQS") is not None: print("Peaks: %s" % str(peakFrequencies)) print( "\n---------------------------------------------------------------------" ) print("========> FFs: %s" % str(funFreqs)) print( "---------------------------------------------------------------------\n" ) # print("XXX: %s" % str([freqToPitch(freq) for freq in xxx])) # print("Peak Frequencies: %s" % str(pitchPeaks)) # print() plt.figure(1) plt.xlabel('frequency') plt.ylabel('amplitude') plt.plot(w[:int(len(w) / 10)], fourier_to_plot[:int(len(fourier_to_plot) / 10)]) # plt.show() return funFreqs
def time_offset(time_v, trace, height_th): """ shift a trace so that the steepest point is at time 0 """ # start by finding the max of the derivative zero = int(len(time_v) / 4) * 0 signal = trace dt = np.diff(time_v)[0] # derivative of the signal, smoothed d_signal = savgol_filter(np.diff(signal), 301, 1) # ax = plt.gca() # ax2 = ax.twinx() # ax2.plot(np.array(time_v)[1:],d_signal*100) # use peaks only if they are legitimate edges identified by the set-reset switch [mask, clamp, edges, left_edges, right_edges] = pd.discriminator(time_v, signal, dt_left=100e-9, dt_right=0, height_th=height_th, method=2) # find peaks in the derivative to find the steepest point idx = left_edges[0] + peakutils.indexes(d_signal[(mask & clamp)[1:]], .5, 3000) # print time_v[left_edges], time_v[idx] if len(idx) == 0: return [np.nan for _ in time_v] # else: # idx=(mask&clamp)[idx]*idx idx_s = np.flipud(idx[d_signal[idx].argsort()])[0] try: time_p = peakutils.interpolate(time_v[:-1], d_signal, [idx_s])[0] except (RuntimeError, ValueError) as e: time_p = time_v[idx_s] n_shift = int(time_p / dt) return pa.shift(trace, -n_shift + zero)
def time_offset(time_v, trace): """ shift a trace so that the steepest point is at time 0 """ # start by finding the max of the derivative signal = trace dt = np.diff(time_v)[0] # derivative of the signal, soothed d_signal = savgol_filter(np.diff(signal), 301, 3) # find peaks in the derivative to find the steepest point idx = peakutils.indexes(d_signal, .5, 3000) if len(idx) == 0: return [np.nan for _ in time_v] idx_s = np.flipud(idx[d_signal[idx].argsort()])[0] try: time_p = peakutils.interpolate(time_v[:-1], d_signal, [idx_s])[0] except (RuntimeError, ValueError) as e: time_p = time_v[idx_s] n_shift = int(time_p / dt) return shift(trace, -n_shift)
pp = PeakPicking() gradient = pp.calculate_gradient(ms.xvals, ms.yvals) print(pp.gradient) found_peaks = pp.find_peaks(1) ms.plot(ax2) for peak in found_peaks: print(found_peaks[peak][0], found_peaks[peak][1]) ax2.plot(found_peaks[peak][0], found_peaks[peak][1], 'rx') ind = detect_peaks(ms.yvals, mph=None, mpd=None, show=False) print(ind) ms.plot(ax3) ax3.plot(ms.xvals[ind], ms.yvals[ind], 'go') indexes = peakutils.indexes(ms.yvals, thres=0.0, min_dist=1) peaks_x = peakutils.interpolate(ms.xvals, ms.yvals, ind=indexes) print(peaks_x) ms.plot(ax4) ax4.plot(ms.xvals[indexes], ms.yvals[indexes], 'bo') base = peakutils.baseline(ms.yvals, 2) baseline_corrected = ms.yvals - base ax4.plot(ms.xvals, baseline_corrected, 'r') indexes_baseline = peakutils.indexes(baseline_corrected, thres=0.0, min_dist=1) ax4.plot(ms.xvals[indexes_baseline], baseline_corrected[indexes_baseline], 'ro') #ms.plot(ax1) ax1.plot(ms.xvals, pp.gradient * 100, 'r') plt.show()
bl = reader.read_block() #Call the the time vector, the signal and the sampling rate time = bl.segments[0].analogsignals[0].times sampling_rate = float(bl.segments[0].analogsignals[0].sampling_rate) sig = bl.segments[0].analogsignals[0].magnitude #Remove the leak sig = sig - np.mean(sig[0:1000]) #For faster computation, let's take only the first 5 seconds of the signal limit = np.ravel(np.where(time >= 5.0))[0] sig = np.ravel(sig[0:limit]).astype('float') time = np.ravel(time[0:limit]) #Get the peaks on the filtered data threshold = np.std(sig[0:10000]) * 3 indexes = peakutils.indexes(sig, thres=threshold, min_dist=100) #Plot the peaks on the raw data plt.figure() plt.title('Spike detection on the raw trace') plt.xlabel('Time (s)') plt.plot(time, sig.ravel(), color='skyblue') for i in range(len(indexes)): plt.scatter(time[indexes[i]], sig[indexes[i]], color='gray') peaks_x = peakutils.interpolate(time, sig, ind=indexes)
c='red', label='data' if 'data' not in plt.gca().get_legend_handles_labels()[1] else '') #plt.axvline(x=26.16878048) plt.fill(x, y, zorder=10, color=color[i], alpha=0.2) plt.text(stop, peakhight / sec[i] * (dataSets[i][100, 1]) + scale * i, '[{}], {:.2f}, '.format(i, minpeak[i]) + name[i], color=color[i]) #prints the name to the line #fitiing and ploting indexes = peakutils.indexes( y, thres=minpeak[i], min_dist=len(x) / peaknumber[i]) #finding peak index position /max(y) print type(indexes), indexes, '\n', x[indexes] peaks_x = peakutils.interpolate( x, y, ind=indexes, width=window) #gausian fit better presition peaks_x = np.where( np.abs(peaks_x - x[indexes]) > 1., x[indexes], peaks_x) #check wrong fit #print name[i],peaks_x[6], y[ox_index(y[indexes],y)] #check if at given x value y value fits the data #x[Ox_index(peaks_x,x,uncertanty=[-0.02,0.02])] #scatter(x[Ox_index(peaks_x,x,uncertanty=[-0.02,0.02])],y[Ox_index(peaks_x,x,uncertanty=[-0.02,0.02])]) if (oixdes == True): # and (name[i] not in list_of_none_oxed) plt.scatter(peaks_x, y[indexes] + 0.15 * peakhight) #print name[i], peaks_x plt.xlim(start, stop)
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') plt.plot(dydx) peaks_x = peakutils.interpolate(x, y, ind=indexes) print(peaks_x) for i in range(0,len(dydx)-1): if dydx[i]>=0 and dydx[i+1]<=0: if abs((d2ydx2[i]+d2ydx2[i+1])/2)>=0.05: print 'Maximum at:',i, '-', i+1, ' Height:', (y[i]+y[i+1])/2, ' Second derivative:', ((d2ydx2[i]+d2ydx2[i+1])/2) elif dydx[i]<=0 and dydx[i+1]>=0: if abs((d2ydx2[i]+d2ydx2[i+1])/2)>=0.05: print 'Minimum at:',i, '-', i+1, ' Height:', (y[i]+y[i+1])/2, ' Second derivative:', ((d2ydx2[i]+d2ydx2[i+1])/2)
def tidal_force_pdf(fname, plot=False, nbins=20, **cosmo): ''' Computes and (optional) plots the PDF of the tidal force for each halo, averaged over a dynamical time ''' import pickle import peakutils from peakutils.peak import centroid from seren3.analysis.plots import fit_scatter from scipy.interpolate import interp1d from scipy.optimize import curve_fit def pdf(arr, bins): log_arr = np.log10(arr) idx = np.where(np.isinf(log_arr)) log_arr = np.delete(log_arr, idx) P, bin_edges = np.histogram(log_arr, bins=bins, density=False) P = np.array( [float(i)/float(len(data)) for i in P] ) bincenters = 0.5*(bin_edges[1:] + bin_edges[:-1]) dx = (bincenters.max() - bincenters.min()) / bins C = np.cumsum(P) * dx C = (C - C.min()) / C.ptp() return P,C,bincenters,dx data = pickle.load(open(fname, 'rb')) hid = np.zeros(len(data)) tidal_force_tdyn = np.zeros(len(data)) pid = np.zeros(len(data)) for i in range(len(data)): res = data[i].result hid[i] = res["hprops"]["id"] pid[i] = res["pid"] tidal_force_tdyn[i] = res["hprops"]["tidal_force_tdyn"] idx = np.where(pid == -1) hid = hid[idx] tidal_force_tdyn = tidal_force_tdyn[idx] P,C,bincenters,dx = pdf(tidal_force_tdyn, nbins) n = int(np.round(nbins/5.)) # Interpolate the PDF fn = interp1d(bincenters, P) x = np.linspace(bincenters.min(), bincenters.max(), 1000) y = fn(x) # Fit peaks to get initial estimate of guassian properties indexes = peakutils.indexes(y, thres=0.02, min_dist=250) peaks_x = peakutils.interpolate(x, y, ind=indexes) # Do the bimodal fit expected = (peaks_x[0], 0.2, 1.0, peaks_x[1], 0.2, 1.0) params,cov=curve_fit(_bimodal,x,y,expected) sigma=np.sqrt(np.diag(cov)) # Refit the peaks to improve accuracy y_2 = _bimodal(x,*params) fn = interp1d(x, y_2) indexes = peakutils.indexes(y_2, thres=0.02, min_dist=250) peaks_x = peakutils.interpolate(x, y_2, ind=indexes) if plot: import matplotlib.pylab as plt z = cosmo["z"] p = plt.plot(bincenters, P * (1. + z)**3, linewidth=1., label="z=%1.2f" % z) col = p[0].get_color() plt.plot(x, y_2 * (1. + z)**3, color=col, lw=3)#, label='model') plt.scatter(peaks_x, fn(peaks_x) * (1. + z)**3, color='r', s=250, marker='o') plt.xlabel(r"log$_{10}$ $\langle F_{\mathrm{Tidal}} \rangle_{t_{\mathrm{dyn}}}$") plt.ylabel(r"P (1 + z)$^{3}$") plt.legend() # plt.show() return P,C,bincenters,dx,hid,tidal_force_tdyn,indexes,peaks_x,params,sigma
def PLSummary(): #select the baseline ready = 0 j = 0 while j < 2: try: file_path_baseline = filedialog.askopenfilenames( title="Please select the PL baseline file") if file_path_baseline != '': ready = 1 directory = filedialog.askdirectory(title="Where saving?") if not os.path.exists(directory): os.makedirs(directory) os.chdir(directory) else: os.chdir(directory) break else: print("Please select correct PL csv files") j += 1 except: print("no file selected") j += 1 baselines = [] for i in range(len(file_path_baseline)): with open(file_path_baseline[i]) as csvfile: readCSV = csv.reader(csvfile, delimiter=',') CSVdat = [] for row in readCSV: CSVdat.append(row) energies = [] intensities = [] for j in range(2, len(CSVdat) - 1): energies.append(float(CSVdat[j][0])) intensities.append(float(CSVdat[j][1])) baselines.append([energies, intensities]) #select the csv files ready = 0 j = 0 while j < 2: try: file_path_csv = filedialog.askopenfilenames( title="Please select the PL csv file") if file_path_csv != '': ready = 1 break else: print("Please select correct PL csv files") j += 1 except: print("no file selected") j += 1 #read them all and extract data #export to txt file first column the energy, then all the intensity columns with titles named as Frame number or nothing is only 1 frame #baseline-corrected and normalized data initialpeakvalue = [] peakareas = [] if ready: for i in range(len(file_path_csv)): name = os.path.split(file_path_csv[i])[-1] txtfile = [ "Energy" + "\t" + "Intensity" + "\n", "eV" + "\t" + "-" + "\n", name + "\n", " \t \n" ] with open(file_path_csv[i]) as csvfile: readCSV = csv.reader(csvfile, delimiter=',') CSVdat = [] energies = [] intensities = [] intensitiescorr = [] for row in readCSV: CSVdat.append(row) if CSVdat[2][0] != "Frame 1": #initialpeak for j in range(2, len(CSVdat) - 1): energies.append(float(CSVdat[j][0])) intensities.append(float(CSVdat[j][1])) txtfile.append(CSVdat[j][0] + "\t" + CSVdat[j][1] + "\n") basefound = 0 for j in range(len(baselines)): if baselines[j][0][0] == energies[0]: for k in range(len(intensities)): intensitiescorr.append(intensities[k] - baselines[j][1][k]) basefound = 1 break if basefound: txtfile = [ "Energy" + "\t" + "Intensity" + "\t" + "IntensityCorr" + "\t" + "IntensityCorrNorm" + "\n", "eV" + "\t" + "-" + "\t" + "-" + "\t" + "-" + "\n", name + "\n", " \t \n" ] minimum = min(intensitiescorr) maximum = max(intensitiescorr) intensitiescorrnorm = [ (x - minimum) / (maximum - minimum) for x in intensitiescorr ] for j in range(len(energies)): txtfile.append( str(energies[j]) + "\t" + str(intensities[j]) + "\t" + str(intensitiescorr[j]) + "\t" + str(intensitiescorrnorm[j]) + "\n") x = np.array(energies) y = np.array(intensitiescorrnorm) peak_value = y.max() mean = x[y.argmax( )] # observation of the data shows that the peak is close to the center of the interval of the x-data sigma = mean - np.where(y > peak_value * np.exp(-5))[0][ 0] # when x is sigma in the gaussian model, the function evaluates to a*exp(-.5) popt, pcov = curve_fit(gaus, x, y, p0=[peak_value, mean, sigma]) initialpeakvalue.append([name, popt.max()]) plt.plot(x, y, 'b+:', label='data') plt.plot(x, gaus(x, *popt), 'ro:', label='fit') plt.legend() plt.title("max= " + str(popt.max())) plt.xlabel('Energy (eV)') plt.ylabel('PL intensity (-)') plt.savefig(name + ".png", dpi=300, transparent=False) plt.close() file = open(name + '.txt', 'w') file.writelines("%s" % item for item in txtfile) file.close() else: #if file is for ptbypt txtfile = [] txtfilecorr = [] for j in range(3, len(CSVdat)): if CSVdat[j] != []: if "Frame" not in CSVdat[j][0]: energies.append(float(CSVdat[j][0])) txtfile.append(str(energies[-1])) txtfilecorr.append(str(energies[-1])) elif "Frame" in CSVdat[j][0]: break basenumb = 99999 for j in range(len(baselines)): if baselines[j][0][0] == energies[0]: basenumb = j break if basenumb == 99999: print("no baseline found") else: intensities = [] interm = [] for j in range(3, len(CSVdat)): if CSVdat[j] != []: if "Frame" not in CSVdat[j][0]: interm.append(float(CSVdat[j][1])) elif "Frame" in CSVdat[j][0]: intensities.append(interm) interm = [] intensities.append(interm) intensitiescorr = [] intensitiescorrnorm = [] for j in range(len(intensities)): corrinterm = [] for k in range(len(intensities[j])): corrinterm.append(intensities[j][k] - baselines[basenumb][1][k]) intensitiescorr.append(corrinterm) minimum = min(corrinterm) maximum = max(corrinterm) intensitiescorrnorm.append([ (x - minimum) / (maximum - minimum) for x in corrinterm ]) #peak finding, need some data smoothing and noise filters peaksXpositions = [] for j in range(len(intensitiescorrnorm)): x = np.array(energies) y = np.array(intensitiescorrnorm[j]) indexes = peakutils.indexes(y, thres=0.999, min_dist=0.01) peaks_x = peakutils.interpolate(x, y, ind=indexes) # print(x[indexes]) peaksXpositions.append(list(peaks_x)) # plt.figure(figsize=(10,6)) # pplot(x, y, indexes) plt.close() plt.figure(figsize=(10, 6)) pplot(x, y, indexes) entete2 = " " entete1 = "eV" entete0 = "Energy" for j in range(len(intensities)): entete2 += "\tFrame " + str(j + 1) entete1 += "\t" + "-" entete0 += "\tIntensityCorrNorm" for k in range(len(intensities[j])): txtfile[k] += "\t" + str( intensitiescorrnorm[j][k]) txtfile.insert(0, entete2) txtfile.insert(0, entete1) txtfile.insert(0, entete0) for k in range(len(txtfile)): txtfile[k] += "\n" file = open(name + '_norm.txt', 'w') file.writelines("%s" % item for item in txtfile) file.close() entete2 = " " entete1 = "eV" entete0 = "Energy" for j in range(len(intensities)): entete2 += "\tFrame " + str(j + 1) entete1 += "\t" + "-" entete0 += "\tIntensityCorr" for k in range(len(intensities[j])): txtfilecorr[k] += "\t" + str( intensitiescorr[j][k]) txtfilecorr.insert(0, entete2) txtfilecorr.insert(0, entete1) txtfilecorr.insert(0, entete0) for k in range(len(txtfilecorr)): txtfilecorr[k] += "\n" file = open(name + '.txt', 'w') file.writelines("%s" % item for item in txtfilecorr) file.close() peaksareainterm = [] for k in range(len(intensitiescorrnorm)): x = np.array(energies) y = np.array(intensitiescorrnorm[k]) f = interp1d(x, y, kind='linear') xnew = lambda x: f(x) integral = integrate.quad(xnew, x.min(), x.max()) peaksareainterm.append(integral[0]) file = open(name + 'peaksarea.txt', 'w') file.writelines( "%s \t %s \n" % (str(item + 1), str(peaksareainterm[item])) for item in range(len(peaksareainterm))) file.close() peakareas.append([name, peaksareainterm]) csvfile.close() file = open('initialPeakPositions.txt', 'w') file.writelines("%s \t %s \n" % (item[0], item[1]) for item in initialpeakvalue) file.close() plt.close() for i in range(len(peakareas)): plt.plot(range(1, len(peakareas[i][1]) + 1), peakareas[i][1], label=peakareas[i][0]) #plt.legend() plt.xlabel('Frame #') plt.ylabel('Area under the curve') plt.savefig("peakareaevolution.png", dpi=300, transparent=False)
def find_peaks(a, d, thres=0.25): indexes = peakutils.indexes(a, thres=thres / max(a), min_dist=d) interpolatedIndexes = peakutils.interpolate(np.array(range(0, len(a))), a, ind=indexes) return interpolatedIndexes
def find_cluster_centroids_psmac_dmrho(self, snapnr=0, EA2="", do_plot=False, verbose=False): if verbose: print "Checking snapshot {0}".format(snapnr) if self.name and self.name != "both": # Only one cluster in simulation box expected_xpeaks = 1 expected_ypeaks = 1 thres, min_dist = 0.9, 20 # elif self.toy.parms["ImpactParam"] < 0.1: # two clusters, no impact parameter # expected_xpeaks = 2 # expected_ypeaks = 1 # thres, min_dist = 0.15, 10 else: # two clusters, yes impact parameter, or yes rotated /w EA1 expected_xpeaks = 2 expected_ypeaks = 2 thres, min_dist = 0.35, 100 """ Peakutils sometimes finds noise (i.e. 1 pixel with a slightly higher density, where slightly is no more than 0.1%). To kill of these tiny noise fluctuations the summed dark matter density is squared, then normalised to the maximum, and finally smoothed with a Savitzky-Golay filter. """ rhodm = getattr(self.psmac, "rhodm{0}best765".format(EA2), None) if rhodm is None: return xsum = p2(numpy.sum(rhodm[snapnr], axis=0)) xsum /= numpy.max(xsum) xsum = scipy.signal.savgol_filter(xsum, 15, 3) ysum = p2(numpy.sum(rhodm[snapnr], axis=1)) ysum /= numpy.max(ysum) ysum = scipy.signal.savgol_filter(ysum, 15, 3) xpeaks = peakutils.indexes(xsum, thres=thres, min_dist=min_dist) ypeaks = peakutils.indexes(ysum, thres=thres, min_dist=min_dist) if not (len(xpeaks) == expected_xpeaks and len(ypeaks) == expected_ypeaks): print "ERROR: found incorrect number of peaks in dmrho" print "xpeaks = {0}\nypeaks = {1}".format(xpeaks, ypeaks) print "Snapshot number = {0}\n".format(snapnr) if do_plot: plot.psmac_xrays_with_dmrho_peakfind(self, snapnr, xsum, ysum, xpeaks, ypeaks, numpy.nan, EA2=EA2) return xpeaks, ypeaks, numpy.nan try: # Further optimize peakfinding by interpolating xpeaks = peakutils.interpolate(range(0, self.xlen), xsum, ind=xpeaks) ypeaks = peakutils.interpolate(range(0, self.ylen), ysum, ind=ypeaks) except RuntimeError as err: if "Optimal parameters not found: Number of calls to function has reached" in str( err): print "WARNING: peakutils.interpolate broke, using integer values" else: raise if self.name and self.name != "both": # Only one cluster in simulation box center = xpeaks[0], ypeaks[0] print "Success: found 1 xpeak, and 1 ypeak!" print " {0}: (x, y) = {1}".format(self.name, center) if do_plot: plot.psmac_xrays_with_dmrho_peakfind(self, snapnr, xsum, ysum, xpeaks, ypeaks, numpy.nan, EA2=EA2) return center else: #if True: # self.toy.parms["ImpactParam"] < 0.1: # two clusters, no impact parameter # cygA = xpeaks[0], ypeaks[0] # cygNW = xpeaks[1], ypeaks[0] #else: # two clusters, yes impact parameter. cygA = xpeaks[0], ypeaks[0] cygNW = xpeaks[1], ypeaks[1] distance = numpy.sqrt( p2(cygA[0] - cygNW[0]) + p2(cygA[1] - cygNW[1])) distance *= self.pixelscale print "Success: found {0} xpeaks, and {1} ypeak!"\ .format(expected_xpeaks, expected_ypeaks) print " cygA: (x, y) = {0}".format(cygA) print " cygNW: (x, y) = {0}".format(cygNW) print " distance = {0:.2f} kpc\n".format(distance) if do_plot: plot.psmac_xrays_with_dmrho_peakfind(self, snapnr, xsum, ysum, xpeaks, ypeaks, distance, EA2=EA2) return cygA, cygNW, distance
data = concatenate((data1, data2)) else: data = data1 count = data[:, 0].size volume = data[:, 4] volume_mean = mean(volume) volume_var = var(volume) volume_std = std(volume) if count > 1: density = stats.kde.gaussian_kde(volume) x = arange(min(volume), max(volume), 1) y = density(x) indexes = peakutils.indexes(y) interpolatedIndexes = peakutils.interpolate(x, y, ind=indexes) plt.plot(x, y) plt.savefig(plotfile, bbox_inches='tight') plt.close() pat = re.compile("[-_]") parts = pat.split(file.replace(".csv", "")) sigmaA = float(parts[2]) ratio = float(parts[3]) sigmaB = sigmaA / ratio radius = float(parts[4]) cutoff = float(parts[5]) row = [ sigmaB, sigmaA, ratio, radius, cutoff, count, volume_mean, volume_var, volume_std