def pothole(): df_File = pd.read_csv("Dataset-1.csv") # DataSet Selection time = df_File['Trip Time(Since journey start)(s)'] Accx = df_File['Acceleration Sensor(X axis)(g)'] Accy = df_File['Acceleration Sensor(Y axis)(g)'] Accz = df_File['Acceleration Sensor(Z axis)(g)'] Lat = df_File[' Latitude'] Longi = df_File[' Longitude'] x = np.array(time) y = np.array(Accy) indexes = peakutils.indexes(-y, thres=0.9, min_dist=10) #c=data.Accy<-0.1 #data[c] #print(indexes) #print(x[indexes], y[indexes]) plt.figure(figsize=(10, 6)) pplot(x, y, indexes) plt.title('Number of severe-potholes') plt.xlabel('Time') plt.ylabel('Acceleration in the y direction') plt.show()
def plot_fourier_peaks(self, x, y, index): plt.figure(figsize=(10, 6)) pplot(x, y, index) for idx in index: plt.annotate(f"{x[idx]:.2f}[Hz]", (x[idx], y[idx])) plt.title('Fourier Transform - peaks') plt.show()
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 plot_data(self, data, freq, spec, peak_idx): plt.subplot(1, 2, 1) plt.plot(data) plt.subplot(1, 2, 2) pplot(freq, spec, peak_idx) plt.xlim(-10, 1500) plt.show()
def showPeaks(): indexesX = peakPosition(0.5, 10)[0] x = np.array(xAxis) y = np.array(accelerationX) pyplot.figure(figsize=(10, 6)) pplot(x, y, indexesX) pyplot.title('First estimate') pylab.show()
def plot(x, y, indexes, breaths): pyplot.figure(figsize=(10,6)) pplot(x, y, indexes) pyplot.title('Find peaks') #show breaths for breath in breaths: pyplot.axvspan(breath[2], breath[3], color='y', alpha=0.5, lw=0) pyplot.show()
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 do_plot1d_peakutil(): plot.cla() y = specdata[1] plot.plot(x, y) plot.xlim([startfreq * 1e-6, stopfreq * 1e-6]) plot.title('Spectrum', fontsize=30) plot.ylabel("Amplitude(dB)", fontsize=30) plot.xlabel("Frequency(MHz)", fontsize=30) indexes = peakutils.indexes(y + 80, thres=0.3, min_dist=100) pplot(x, y, indexes) plot.show(False) plot.pause(0.001)
def do_plot1d_peakutil(): plot.cla() y=specdata[1] plot.plot(x,y) plot.xlim([startfreq*1e-6,stopfreq*1e-6]) plot.title('Spectrum',fontsize=30) plot.ylabel("Amplitude(dB)",fontsize=30) plot.xlabel("Frequency(MHz)",fontsize=30) indexes=peakutils.indexes(y+80,thres=0.3,min_dist=100) pplot(x,y,indexes) plot.show(False) plot.pause(0.001)
def getpeaks(): """ use scikit signal peak finding """ nobs = 60*2*24*7 # size of sample upload_remote_path = BaseConfig.REMOTEDATAPATH sftpkey = RSAKey.from_private_key_file(BaseConfig.SFTPKEYFILENAME) sftp = SftpClient(BaseConfig.REMOTEDATAHOST,BaseConfig.SFTPPORT,BaseConfig.SFTPLOGINUSER,BaseConfig.SFTPPASSWORD,sftpkey) sftp.dirch(BaseConfig.REMOTEDATAPATH) fl = sftp.dirlist(upload_remote_path) fl.sort() fl = [x for x in fl if (len(x.split('.')) > 1 and x.split('.')[1]=='xls')] if fl != None: lcd = loadCellDataMulti(BaseConfig.NSD,fl) datal = [] for i, df in enumerate(lcd.dfs): nd0 = df.shape[0] print('###i=',i) if nd0 > 0: print(nd0,nobs) dat = df.iloc[(nd0-nobs):nd0,] nd0 = dat.shape[0] dat0y = dat['mass'].rolling(5).median() # low pass filter helps dat0x = dat.iloc[:,0] peaks, pp = find_peaks(dat0y, height=20,width=30,distance=60) #peaks= find_peaks_cwt(dat0y,np.arange(100,960,10)) # doesn't work well with irregular waveforms I guess.. peakx = [dat0x[x] for x in peaks ] dpeaks = pd.DataFrame(peakx) dpeaks['scale'] = i if i == 0: allPeaks = dpeaks elif i != 3: allPeaks = allPeaks.append(dpeaks) # peakutils doesn't work as well with this data #peaks = peakutils.indexes(dat0y, thres=0.6, min_dist=40) #peaks = peakutils.interpolate(dat0x.index,dat0x,peaks1) #print(indexes) #print(x[indexes], y[indexes]) fig = plt.figure(figsize=(12,4)) pplot(dat0x,dat0y,peaks) fig.show() #axs[scl].title.set_text('Scale %d' % scl) #axs[scl].plot(peaks, dat0x[peaks] ,'x') #axs[scl].plot(np.zeros_like(dat0x), "--", color="gray") fig = plt.figure(figsize=(12,4)) plt.plot(allPeaks.iloc[:,0],allPeaks.scale,'x') plt.title('Times of peaks for each scale') fig.show()
def plotFFT(self, signal, fs): x, y = self.calcFFT(signal, fs) indexes = peakutils.indexes(y, thres=0.2, min_dist=100) #print(indexes) plt.plot(signal) plt.xlim(0, 1000) plt.figure(figsize=(10, 6)) pplot(x, y, indexes) plt.title('First estimate') plt.xlim(0, 2200) #plt.figure() #plt.plot(x, np.abs(y)) #plt.title('Fourier') plt.show()
def determineDynamicContrast(image): rows, cols = image.shape dynamicContrastModule = cv2.medianBlur(image, 7) # column = dynamicContrastModule[0:rows, cols-1] column = np.asarray(dynamicContrastModule, dtype=np.float) column = np.sum(column, 1) indexes = peakutils.indexes(column, thres=0.1 / max(column), min_dist=120) pyplot.plot(column) savefig('dynamicContrast.png') x = np.linspace(0, rows - 1, rows) pplot(x, column, indexes) savefig('dynamiContrastPeaks.png')
def plot(title, data): """ Plot the ECG wave using self.data :param title: The title of the waveform """ # Data to be plotted y = np.linspace(0, len(data) - 1, len(data)) pyplot.figure(figsize=(10, 6)) # Plot using pplot library pplot(y, data, [0]) pyplot.title(title)
def Saturation(tr, plot): """ return true and the component if the trace is satured find if there is a plateau formed input : tr: type trace , seimic trace to analyse output : Stat type bool, if true the signal is considerd as satured min_dist min poiunts between 2 peaks""" #selection of peaks that have asignal greater than 0.7*np.max(tr.data) indexselected = Peakindex_derivative(tr, 0.7) indexselected.sort() indexselectedbis = copy.copy(indexselected) #initialisation of the list of saturated peaks index that are greater than 0.95 than tr.data+1 Lsat = [] for i in xrange(len(indexselected) - 1): if tr.data[indexselected[i + 1]] == tr.data[indexselected[i]]: Lsat.append(True) elif tr.data[indexselected[i + 1]] >= 0.997 * tr.data[ indexselected[i]] and tr.data[indexselected[ i + 1]] <= (1 + 0.003) * tr.data[indexselected[i]] and abs( indexselected[i + 1] - indexselected[i]) < 3: Lsat.append(True) else: Lsat.append(False) indexselectedbis.remove(indexselected[i]) #Thje siganl is satured if one peak have a neigbourg with an amplitude greater than 0.98 of his amplitude a = len(np.where(Lsat)[0]) if a >= 2: Sat = True else: Sat = False #plot if plot == True: time = np.arange(0, tr.stats.npts / tr.stats.sampling_rate, tr.stats.delta) plt.figure() pplot(time, tr.data, indexselected) plt.plot(time, tr.data) #pplot(time,tr.data,Listindex60) plt.show() return Sat, Lsat
def centerdata(originalDataArray, totalIndexLength): import numpy as np import numpy import peakutils from peakutils.plot import plot as pplot from matplotlib import pyplot #funtion to find the maximum points in the array and display graph angle = np.arange(-89.55, 90, 0.45) indexes = peakutils.indexes(originalDataArray, thres=0.8, min_dist=40) print("Peak Indices/Index: ", indexes) pyplot.figure(figsize=(6, 4)) pplot(angle, originalDataArray, indexes) pyplot.title('Peaks Located') #This code will modify the indices to center at 0 avgIndex = (indexes[0] + indexes[-1]) / 2 maxIndex = int(avgIndex) if maxIndex > 200: shiftValue = maxIndex - 200 newnormalised = np.array(originalDataArray) for k in range(0, totalIndexLength - 1): if k >= (totalIndexLength - 1 - shiftValue): newnormalised[k] = 0 else: newnormalised[k] = originalDataArray[k + shiftValue] else: shiftValue = 200 - maxIndex newnormalised = np.array(originalDataArray) for k in range(0, totalIndexLength - 1): if k <= shiftValue: newnormalised[k] = 0 else: newnormalised[k] = originalDataArray[k - shiftValue] return newnormalised #The output is an array of the modified values. When graphed #it should all be centered at zero
def plot(self, title, data=None): """ Plot the ECG wave using self.data :param title: The title of the waveform """ if data == None: data = self.data # Data to be plotted y = np.linspace(0, len(data) - 1, len(data)) pyplot.figure(figsize=(10, 6)) # Plot using pplot library pplot(y, data, self.r_peaks) pyplot.title(title)
def run(path): abf = pyabf.ABF(path) print(abf) x = abf.sweepX y = abf.sweepY indexes = peakutils.indexes(y, thres=3, min_dist=250, thres_abs=True) print('indexes:', len(indexes), indexes) print('xPeak:', x[indexes]) print('yPeak:', y[indexes]) plt.figure(figsize=(10, 6)) pplot(x, y, indexes) plt.title('First estimate') # remove baseline, never works right ''' base = peakutils.baseline(y, 2) plt.figure(figsize=(10,6)) plt.plot(x, y-base) plt.title("Data with baseline removed") ''' plt.show()
def PeaktoPeak(tr, method, plot): """"Peak to Peak : calculate the Pick to pick value of a signal segment and return the time of this pick * Input : -tr type : trace - method : type int if 1 : peak to peak is the distance between the peak i and the first valley and the second ptp is the distance between the last valley and the peak i+1 if 2 : peak to peak is the distance between the peak i and the minimum valley and the second Peak to peak is the distance between the peak i+1 and the minimum valley. -plot: type boool; if true the plot of peak to peak is plotted * output : -MaxptoP:type float; maximum of the peak to peak of the "complet (ie not decompoose in 3 components) of the signal -pfirstPtoP type: float; First peak to Peak value : should be first p wave amplitude *exemple : ptpmax,ptpFirst = PeaktoPeak(tr,1,True) """ #0. Fuind peaks with a minimum distancebetween peaks in count of 20 and threshold for detcting a peak and valley of 0.001 ~~~~~~~~~~~~~~ tr_copy = tr.copy() indexesSommet = peakutils.indexes(tr_copy.data,thres = 0.001,min_dist=20) indexesVallee = peakutils.indexes(-tr_copy.data,thres = 0.001,min_dist=20) #1. Plot ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if plot==True : time = np.arange(0, tr_copy.stats.npts / tr_copy.stats.sampling_rate, tr_copy.stats.delta) plt.figure() pplot(time,tr_copy.data,indexesSommet) plt.plot(time,tr_copy.data) pplot(time,tr_copy.data, indexesVallee) plt.show() i=0 #2. Maximum peak to peak calculation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ L =[] #2.0 Initialisation of the first peak to peak which is equal to the distance betwween the first valley and the first peak. if indexesVallee[0]< indexesSommet[0]: ptp0 = tr_copy[indexesSommet[0]]+abs(tr_copy[indexesVallee[0]]) L.append(ptp0) # 3.0 measure peak to peak between 2 peaks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for i in xrange(len(indexesSommet)-1): Lvalley=[] #3.1 record all the valleys indexes that are between peaks i and i+1 to then take into account only the fist and the second peaks into account (maybe better to take the minimum! ) for valley in indexesVallee: if valley>indexesSommet[i] and valley<indexesSommet[i+1] : Lvalley.append(valley) elif valley>indexesSommet[i+1]: break #3.2 calculate pêak to peak ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if len(Lvalley)==1 : ptp1 = tr_copy[indexesSommet[i]] + abs(tr_copy[Lvalley[0]]) L.append(ptp1) ptp2 = tr_copy[indexesSommet[i+1]] + abs(tr_copy[Lvalley[0]]) L.append(ptp2) elif len(Lvalley)>1 : # peak to peak is the distance between the peak i and the first valley and the second ptp is the distance between the last valley and the peak i+1 if method == 1 : ptp1 = tr_copy[indexesSommet[i]] + abs(tr_copy[Lvalley[0]]) L.append(ptp1) ptp2 = tr_copy[indexesSommet[i+1]] + abs(tr_copy[Lvalley[len(Lvalley)-1]]) L.append(ptp2) #peak to peak is the distance between the peak i and the minimum valley and the second Peak to peak is the distance between the peak i+1 and the minimum valley. elif method == 2 : Lvaluevalley = [] if j in Lvalley : Lvaluevalley.append(abs(tr_copy[Lvalley[j]])) Valleymax= max(Lvaluevalley) ptp1 = tr_copy[indexesSommet[i]] + max(Lvaluevalley) L.append(ptp1) ptp2 = tr_copy[indexesSommet[i+1]] + max(Lvaluevalley) L.append(ptp2) #4.0 end if the signal finished with a valley the last peak to peak is the distance between the last peak and the last valley ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if indexesVallee[len(indexesVallee)-1]>indexesSommet[len(indexesSommet)-1]: ptp3 = tr_copy[indexesSommet[i+1]] + abs(tr_copy[valley]) L.append(ptp3) #5. Take the maximum of the peak to peak ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ptpmax = np.amax(L) # 6. the first vallue of the peak to peak which is suppose to be the P wave amplitude ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ptpFirst = L[0] return ptpmax,ptpFirst
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)
max_v = np.linalg.norm(head_imu[k, 1:4]) max_index = k k += 1 # print(k) change_flag_array[max_index, 1] = max_v import peakutils from peakutils.plot import plot as pplot indexs = peakutils.indexes(np.linalg.norm(head_imu[:, 1:4], axis=1),min_dist=50) plt.figure() plt.subplot(211) pplot(head_imu[:, 0], np.linalg.norm(head_imu[:, 1:4], axis=1), indexs) plt.subplot(212) plt.plot(change_flag_array[:, 0], change_flag_array[:, 1], '+',label='change') plt.legend() plt.grid() # def is_peak(data, k): # if data[k] > data[k-1] and data[k] > data[k+1]: # return True # else: # return False # # norm_value = np.linalg.norm(head_imu[:,1:4],axis=1) # for i in range(1, len(flag_list)): # pre_i = flag_list[i-1] # last_i = flag_list[i]
#print list(all_first_spike_times[:200]) min_burst_time = np.min(all_first_spike_times[all_first_spike_times > -10]) all_first_spike_times = all_first_spike_times - min_burst_time burst_times_sorted = np.sort(all_first_spike_times) bin_width = 1.0 time, burst_density = calculate_burst_density(burst_times_sorted, bin_width) indexes = peakutils.indexes(burst_density, thres=0.0, min_dist=3) indexes_negative = peakutils.indexes(-burst_density, thres=0.0, min_dist=3) print(indexes) print(time[indexes], burst_density[indexes]) plt.figure(figsize=(10, 6)) #pplot(time, burst_density, indexes) pplot(time, burst_density, indexes_negative) plt.title('First estimate') plt.figure() plt.plot(time, burst_density) plt.xlabel('Time (ms)') plt.ylabel('# of bursts / ms') #plt.title('Burst density in causal network $G_Emax = 1.5 G_L; G_Imax = 0.5 G_L$') #plt.title('pruned spread 0.0 ms') plt.xlim([0, 150]) plt.ylim([0, 160]) plt.show()
# normalize with log # and blur with gaussian y = filters.gaussian_filter(y, 30) # y = np.log10(y) pyplot.figure(figsize=(10,6)) pyplot.plot(x/INTERVAL_LENTH, y) #plotting data with peaks indexes = peakutils.indexes(y, thres=np.mean(y), min_dist=10) #possible thresholds for indexes #thres=0.4*max(y) #thres=np.mean(y) #can filter by mean, or max value percentage, #distance is set to 10 seconds pyplot.figure(figsize=(10,6)) pplot(x, y, indexes) pyplot.title('First estimate') # #removing baseline # base = peakutils.baseline(y, 2) # y2 = y-base # indexes2 = peakutils.indexes(y, thres=np.mean(y2), min_dist=10) # pyplot.figure(figsize=(10,6)) # pplot(x, y2, indexes2) # pyplot.title('Removing baseline') #calculating mean phasic peak amplitude, sum phasic peak amplitude, and phasic peak frequency EDA def get_phasic_values(time_window, peak_vals): """Given a time interval (/10 = seconds) and a csv of peakutils calculated peak y-values, returns a list containing the sum peak phasic amplitude eda, the mean peak phasic amplitude eda,
[peak_wavelengths_store, peak_wavelengths]) peak_intensities_store = np.vstack( [peak_intensities_store, peak_intensities]) num_peaks_store = np.append(num_peaks_store, num_peaks) intensity_initial_peak_store = np.append(intensity_initial_peak_store, peak_intensities[0]) initial_peak_wl_store = np.append(initial_peak_wl_store, peak_wavelengths[0]) if usr_input3 == 'y' or usr_input3 == 'Y': picno = 0 for name in camlist.keys(): if name == data_set.name[25:]: f, axarr = plt.subplots(2, 1, figsize=(15, 15)) axarr[1].plot(x_red, y_norm) pplot(x_red, y_norm, peak_indexes_orig) axarr[0].imshow(camlist[camlist.keys()[picno]]) axarr[0].set_title(data_set.name[25:], fontsize=20) axarr[0].text(683, 529, 'O', color='y', fontweight='bold') for k in range(0, len(peak_indexes_orig)): axarr[1].text(x_red[peak_indexes_orig[k]], y_norm[peak_indexes_orig[k]] + 0.005, k) axarr[1].text( max(x_red) + 50, max(y_norm) - ((k + 1) * 0.02), "Peak " + str(k) + ": " + str(x_red[peak_indexes_orig[k]]) + "nm") axarr[1].set_ylabel("Normalised intensity") axarr[1].set_xlabel("Wavelength(nm)") else: picno = picno + 1 else:
def getPeriod(): periodList = [] readList = [] reads = open(sys.argv[1], 'r') for line in reads.readlines(): if ">" not in line: readList.append(line.rstrip()) for read in readList: sequence = read count = 0 positionKmers = [] positionInSequence = 0 kSize = K_SIZE anchorSize = ANCHOR_SIZE qKToPosition = dict() headToTails = get_quasi_kmers(sequence, kSize, anchorSize) headToClusters = cluster_tails(headToTails) while positionInSequence < len(sequence) - kSize + 1: kmer = sequence[positionInSequence:positionInSequence + kSize] position = get_position_quasi_kmer(qKToPosition, kmer, headToClusters, positionInSequence, anchorSize) positionKmers.append(position) positionInSequence += 1 tfd = numpy.fft.fft(positionKmers) tfd = fftpack.fft(positionKmers) S = numpy.square(numpy.absolute(tfd)) / len(positionKmers) autocorrelation = numpy.fft.ifft(S) cb = np.array(list(autocorrelation)) peakind = signal.find_peaks_cwt(cb, np.arange(1, 1500)) #~ peakind = signal.find_peaks_cwt(cb,np.arange(1,2000)) intervals = list((peakind)) print(intervals) # #print(intervals) if len(intervals) > 1: i = 0 newIntervals = [] diffSmall = False while i < len(intervals) - 1: diff = intervals[i + 1] - intervals[i] if diff < 50: newIntervals.append( np.mean([intervals[i], intervals[i + 1]])) diffSmall = True else: if not diffSmall: newIntervals.append(intervals[i]) else: diffSmall = False i += 1 if intervals[-1] - intervals[-2] > 50: newIntervals.append(intervals[-1]) i = 0 diffForMean = [] # print(newIntervals) while i < len(newIntervals) - 1: diff = newIntervals[i + 1] - newIntervals[i] if diff > 50: diffForMean.append(diff) i += 1 if len(diffForMean) > 1: period = np.mean(diffForMean) # elif diffForMean == 1: periodList.append(period) print(period) else: periodList.append(None) else: periodList.append(None) pyplot.figure(figsize=(10, 6)) positionsSolid = [x for x in range(len(autocorrelation))] pplot(np.array(positionsSolid), np.array(autocorrelation), peakind) #~ # print(indexes) #~ #plot(positions,autocorrelation) #~ draw() #~ show() return periodList, readList
######################################################################## ''' import numpy as np import wfdb import matplotlib.pyplot as plt import peakutils from peakutils.plot import plot as pplot record = wfdb.rdrecord('100', channels=[0]) fs = record.fs data = record.p_signal[:, 0] x = input("How much data you need? (exmp=5000 samples) : ") x = int(x) indexes = peakutils.indexes(data[:x], thres=0.5, min_dist=0.7) pplot(np.arange(0, x), data[:x], indexes) beat = [] for i in range(len(indexes)): RtoR_samples = indexes[i] - indexes[i - 1] RtoR_time = RtoR_samples / fs HR = 60 / RtoR_time beat.append(HR) hr = round(np.mean(beat), 3) plt.title('Peak Detection\n mean of Heart Rate: ' + str(hr)) plt.show()
except: print ("could not parse", line) continue t=numpy.array(t) a=numpy.array(a) #getting the slope and slope's slope to identify the peaks da=numpy.gradient(a) dda=numpy.gradient(da) #pyplot.plot(t,da) #filling the area underneath the peaks pyplot.fill_between(t, a, 32, where =abs(da) > 1.3, color='green') pyplot.fill_between(t, a, 32, where =abs(dda) > 0.8, color='green') #Using peakutils to find the local maximums, we can change the threshold based on our data indexes = peakutils.indexes(a, thres=.05, min_dist=31) #print(indexes) for i in range(len(indexes)): print("You have one peak at the time=", t[indexes][i], "with the absorption of", a[indexes][i], "mAU") #pyplot.figure(figsize=(8,4)) pplot(t, a, indexes) pyplot.plot(t,a) pyplot.show()
======= index= peakutils.peak.indexes(y, thres= 0.5, min_dist=400) >>>>>>> 675ec615410e69062b0ba2be16b2ca10d58ac332 print("X index:",x[index],"Y index:", y[index]) peaks_y = peakutils.interpolate(x, y, ind=index, width= width) print("Interpolated:", peaks_y) range_left = int(index - width) range_right = int(index + width +1) xdata = np.array(x[range_left:range_right]) ydata = np.array(y[range_left:range_right]) gmodel = GaussianModel() params = gmodel.make_params(cen=x[index], amp=y[index], wid=width, sigma = x.std()) result = gmodel.fit(ydata, params, x=xdata) print(result.fit_report()) print(xdata.std()) print(x.std()) #print(f_result.best_fit) pplot(x,y, index,) pyplot.title('Gaussian Model') pyplot.ylabel('Amplitude(AUD)') pyplot.xlabel('Wavelength(nm)') plt.plot(xdata, result.best_fit, 'r-', label='best fit') plt.legend(loc='best') pyplot.show()
min_dist = 6 # meshgrid x and y coordinates x, y = np.meshgrid(x_values, y_values) # Find peak indices (vectorized) indices = indexes(values, thres=signal_threshold, min_dist=min_dist, axis=0) fig = plt.figure() ax1 = fig.add_subplot(221) ax1.set_title('>Threshold = 0.8') ax1.plot(y_values, values[:, 0]) pplot(y_values, values[:, 0], indices[0][indices[1] == 0]) ax2 = fig.add_subplot(222) ax2.set_title('Last Values only') indices = indexes(values, thres=signal_threshold, min_dist=min_dist, axis=0, last_values_only=True) ax2.plot() pplot(y_values, values[:, 0], [indices[0]]) ax3 = fig.add_subplot(212) ax3.set_title('Peak search for 3d data') ax3.contourf(x, y, values) t = y_values[indexes(values,
def main(): # Import sound as file y, fs = sf.read('./arquivos/ton2.wav') # Cacula a trasformada de Fourier do sinal X, Y = calcFFT(y, fs) ## Exibe modulo plt.figure("abs(Y[k])") #plt.stem(X[0:10000], np.abs(Y[0:10000]), linefmt='b-', markerfmt='bo', basefmt='r-') plt.plot(X, np.abs(Y)) plt.grid() plt.title('Modulo Fourier audio') ## Exibe fase plt.figure("Fase(Y[k])") plt.plot(X, np.angle(Y)) plt.grid() plt.title('Modulo Fourier audio') indexes = peakutils.indexes(Y, thres=0.86, min_dist=268) print("Frequencias principais: ", X[indexes], "Hz") pplot(X, Y, indexes) ## Exibe gráficos plt.show() ## Descobre tom com +/- 10Hz para cada valor de frequencias que formam um tom tons = [] low = X[indexes][0] high = X[indexes][1] low = int(low) high = int(high) print(low) print(high) if (687 <= low <= 707): if (1199 <= high <= 1219): tons.append("1") elif (1326 <= high <= 1346): tons.append("2") elif (1467 <= high <= 1487): tons.append("3") elif (1623 < high < 1643): tons.append("A") else: print("Erro, frequência não reconhecida") elif (760 < low < 780): if (1199 < high < 1219): tons.append("4") elif (1326 < high < 1346): tons.append("5") elif (1467 < high < 1487): tons.append("6") elif (1623 < high < 1643): tons.append("B") else: print("Erro, frequência não reconhecida") elif (842 < low < 862): if (1199 < high < 1219): tons.append("7") elif (1326 < high < 1346): tons.append("8") elif (1467 < high < 1487): tons.append("9") elif (1623 < high < 1643): tons.append("C") else: print("Erro, frequência não reconhecida") elif (931 < low < 951): if (1199 < high < 1219): tons.append("X") elif (1326 < high < 1346): tons.append("0") elif (1467 < high < 1487): tons.append("#") elif (1623 < high < 1643): tons.append("D") else: print("Erro, frequência não reconhecida") else: print("Erro, frequência não reconhecida") print(tons)
df_File = pd.read_csv("../../Datasets/Dataset-1.csv") time = df_File['Trip Time(Since journey start)(s)'] Accx = df_File['Acceleration Sensor(X axis)(g)'] Accy = df_File['Acceleration Sensor(Y axis)(g)'] Accz = df_File['Acceleration Sensor(Z axis)(g)'] Lat = df_File[' Latitude'] Longi = df_File[' Longitude'] x = np.array(time) y = np.array(Accy) indexes = peakutils.indexes(-y, thres=0.9, min_dist=10) #c=data.Accy<-0.1 #data[c] #print(indexes) #print(x[indexes], y[indexes]) plt.figure(figsize=(10, 6)) pplot(x, y, indexes) plt.title('Number of severe-potholes') plt.xlabel('Time') plt.ylabel('Acceleration in the y direction') plt.show() #lati = [13.027313,13.026308,13.025118,13.023361,13.028859,13.033193,13.036492 ] #long = [77.577713,77.580344,77.581661,77.584423,77.586206,77.588136,77.589162 ] #gmap = gmplot.GoogleMapPlotter(12.9716,77.5946, 13) #gmap.scatter(lati, long,'yellow',size = 10, marker = False) #gmap.draw("Potholes.html")
def show_peaks(x, y, peaks, mes='mes'): pplot(x, y, peaks) title(mes) show()
def execute(model, data, savepath, feature_field_name=None, x_field_name = None, group_field_name=None, label_field_name=None, threshold= 1.0, distance = 1.0, peaks = 1, valleys = 0, timex = "", xlabel="", ylabel="", end_val = None, start_val = None, bin_width = None, bin_list = None, num_bins = 50, bin_space = 3, guideline = 1, climbing_percent = 1, tick_divide = None, *args, **kwargs): """Get peaks in data Args: feature_field_name <str>: Field name for data to evaluate for peaks x_field_name <str>: X axis field group_field_name <str>: Field name for numeric field for grouping plots. If None, all data will be plotted. label_field_name <str>: Field name for string label field that goes with group field name. Leave as None if group field name is None. threshold <float>: Threshold for peaks distance <float>: Minimum distance apart peaks should be peaks <int>: 1 - Find peaks (default) 0 - do not find peaks valleys <int>: 1 - Find valleys 0 - do not find valleys (default) """ threshold=float(threshold) distance = float(distance) peaks = int(peaks) valleys = int(valleys) feature_data = np.asarray(data.get_data(feature_field_name)).ravel() x_data = np.asarray(data.get_data(x_field_name)).ravel() if (peaks == 0) and (valleys == 0): print("Nothing to evaluate! peaks and valleys input are both 0") print("Exiting.") return if not (group_field_name == None): group_indices = gttd.get_field_logo_indices(data, group_field_name) groups = list(group_indices.keys()) else: groups = list([-1]) group_indices=dict() group_indices[-1]=dict() group_indices[-1]["test_index"] = range(0, len(feature_data)) if not (label_field_name == None): labeldata = np.asarray(data.get_data(label_field_name)).ravel() else: labeldata = np.zeros(len(feature_data)) labels=list() master_array = None master_intervalarr =None for group in groups: print("Group: %i" % group) gx_data = x_data[group_indices[group]["test_index"]] gfeature_data = feature_data[group_indices[group]["test_index"]] #print(gfeature_data.shape) #print(gfeature_data) kwargs=dict() label = labeldata[group_indices[group]["test_index"][0]] labels.append(label) groupstr = "%i".zfill(3) % group if group_field_name == None: grouppath = savepath else: groupstr = groupstr + "_%s" % label grouppath = os.path.join(savepath, groupstr) if not os.path.isdir(grouppath): os.mkdir(grouppath) kwargs['savepath'] = grouppath gfeature_data = np.array(gfeature_data,'float') #None to NaN if np.nanstd(gfeature_data) < (0.01 * threshold): print("Std much smaller than threshold. Skip.") gindexes = list() else: gindexes = get_indexes(gfeature_data, threshold, distance, peaks, valleys) plt.figure() pplot(gx_data, gfeature_data, gindexes) plt.xlabel(x_field_name) plt.ylabel(feature_field_name) plt.tight_layout() plt.savefig(os.path.join(grouppath,"peaks")) plt.close() headerline = "group,xcoord,peakval" grouparr = group * np.ones(len(gindexes)) myarray = np.array([grouparr, gx_data[gindexes],gfeature_data[gindexes]]).transpose() csvname = os.path.join(grouppath,"peak_data.csv") ptools.array_to_csv(csvname, headerline, myarray) if master_array is None: master_array = np.copy(myarray) else: master_array = np.append(master_array, myarray, axis=0) x_intervals = get_x_intervals(gx_data[gindexes]) grouparr_intervals = group * np.ones(len(x_intervals)) intervalarr = np.array([grouparr_intervals, x_intervals]).transpose() if master_intervalarr is None: master_intervalarr = np.copy(intervalarr) else: master_intervalarr = np.append(master_intervalarr, intervalarr, axis=0) csvname = os.path.join(savepath,"all_peak_data.csv") ptools.array_to_csv(csvname, headerline, master_array) kwargs = dict() if xlabel == "": xlabel = x_field_name kwargs['xlabel'] = xlabel if ylabel == "": ylabel = "Count" kwargs['ylabel'] = ylabel kwargs['savepath'] = savepath kwargs['timex'] = timex kwargs['end_val'] = end_val kwargs['start_val'] = start_val kwargs['num_bins'] = num_bins kwargs['bin_width'] = bin_width kwargs['bin_list'] = bin_list kwargs['bin_space'] = bin_space kwargs['tick_divide'] = tick_divide kwargs['guideline'] = guideline kwargs['climbing_percent'] = climbing_percent x_with_peaks = master_array[:,1] plothist.simple_histogram(x_with_peaks, **kwargs) # iheader = "group,peak_interval_in_x" intervalcsvname = os.path.join(savepath,"interval_data.csv") ptools.array_to_csv(intervalcsvname, iheader, master_intervalarr) return
a, b, A, w, x_0, A1, w1, x_01 = fittedParameters y_fit = double_Lorentz(xData, a, b, A, w, x_0, A1, w1, x_01) # print("Max Y value:", max(yData)) # print("Max Y value after fitting:", max(y_fit)) <<<<<<< HEAD index = peakutils.peak.indexes(y_fit, thres=01, min_dist=400) ======= index = peakutils.peak.indexes(y_fit, thres=0.5, min_dist=400) >>>>>>> 675ec615410e69062b0ba2be16b2ca10d58ac332 # index = index[1:] print("X index:", xData[index], "Y index:", y_fit[index]) plt.plot(x, y) # plot the raw data plt.plot(xData, y_fit) # plot the equation using the fitted parameters pplot(xData, y_fit, index) pyplot.title('Double Lorentzian Model)') pyplot.ylabel('Amplitude(AUD)') pyplot.xlabel('Wavelength(nm)') # plt.show() # print(fittedParameters) return xData[index] def lorentzian_peak(x,y): def lorentzian_model(x, y): width = 200 <<<<<<< HEAD index = peakutils.peak.indexes(y, thres=01, min_dist=100)