def gen_cities_avg(climate, multi_cities, years): """ Compute the average annual temperature over multiple cities. Args: climate: instance of Climate multi_cities: the names of cities we want to average over (list of str) years: the range of years of the yearly averaged temperature (list of int) Returns: a pylab 1-d array of floats with length = len(years). Each element in this array corresponds to the average annual temperature over the given cities for a given year. """ avg_yearly_temps = pylab.array([]) for year in years: avg_multi_city_temps = pylab.array([]) day_length = 0 for month in range(1, 13): day_length += len(climate.rawdata[multi_cities[0]][year][month]) for city in multi_cities: avg_temp = sum(climate.get_yearly_temp(city, year)) / day_length avg_multi_city_temps = pylab.append(avg_multi_city_temps, avg_temp) avg_year = sum(avg_multi_city_temps) / len(avg_multi_city_temps) avg_yearly_temps = pylab.append(avg_yearly_temps, avg_year) return avg_yearly_temps
def read_file(datafile,obsv): x=[];y=[];t=[];pt=[];z=[];px=[];py=[] #l is the t coordinate, z is the obs location for o in range(obsv): x.append([]);y.append([]);t.append([]) px.append([]);py.append([]);pt.append([]) with open(datafile) as f: data = f.read() data=data.split('\n') print "data length:", (len(data)) n=0;i=0 nparts= (len(data)-8)/obsv for row in data: if row.startswith('@') or row.startswith('*') \ or row.startswith('$') or row.startswith('#')\ or len(row)==0: continue else: i+=1 w=" ".join(row.split()) s=w.split() x[n].append(float(s[2])) px[n].append(float(s[3])) y[n].append(float(s[4])) py[n].append(float(s[5])) t[n].append(float(s[6])) pt[n].append(float(s[7])) if i%nparts==0: z.append(float(s[8])) i=0;n+=1 return x,px,y,py,t,pt,z
def main(): f = open("final_position.txt","r") data = pl.genfromtxt(f,comments = "L") # need to get every other x = pl.array([]) y = pl.array([]) for i,j in enumerate(data[:-7,2]): if i%4 == 0: x = pl.append(x,data[i,4]) y = pl.append(y,j) print(x) print(y) fit = np.polyfit(x,y,2) print(fit) #fited = fit[0]+fit[1]*x + fit[2]*x**2 fited = np.poly1d(fit) print(fited) #pl.plot(pl.append(x,[.262,.264,.266]),fited(pl.append(x,[.262,.264,.266])),color="black") pl.scatter(x,y,color = "black") pl.xlabel("$A$",fontsize="30") pl.ylabel("$x$",fontsize="30") pl.savefig("fin_pts.png",transparent=True,dpi=300) os.system("open fin_pts.png")
def euler_1(top): ''' inputs: top = maximum value considered for suming outputs: sum = sum of all numbers below top that are mulitples of 3 or five ''' #define varibles mfive = [] mthree = [] mboth = [] #deine win condition win = 0 for i in range(0, top): if(i%3==0 and i%5==0): mboth = pylab.append(i, mboth) elif(i%3==0): mthree = pylab.append(i, mthree) elif(i%5==0): mfive = pylab.append(i, mfive) win = sum(mboth) + sum(mfive) + sum(mthree) print win
def main(): is_transparent = False f = open("pi_data.txt","r") # this is a little different than normal becase of the complex data for the floquet stability # multipliers. When we use the "dtype" option we get a single array of tuples so slicing is a # little more awkward has to look like data[#][#] to get a single value NOT data[#,#]. data = pl.genfromtxt(f,comments="e",dtype="complex,complex,float") eigs1 = pl.array([]) eigs2 = pl.array([]) A = pl.array([]) for i,j in enumerate(data): eigs1 = pl.append(eigs1,j[0]) eigs2 = pl.append(eigs2,j[1]) A = pl.append(A,j[2]) fig1, ax1 = pl.subplots(2,2,sharex=True) ax1[0,0].plot(A,[k.real for k in eigs1],color = "Black") ax1[1,0].plot(A,[k.imag for k in eigs1],color = "Black") ax1[0,1].plot(A,[k.real for k in eigs2],color = "Black") ax1[1,1].plot(A,[k.imag for k in eigs2],color = "Black") ax1[0,0].set_ylabel("Re[$\lambda_1$]",fontsize=25) ax1[1,0].set_ylabel("Im[$\lambda_1$]",fontsize=25) ax1[0,1].set_ylabel("Re[$\lambda_2$]",fontsize=25) ax1[1,1].set_ylabel("Im[$\lambda_2$]",fontsize=25) ax1[1,0].set_xlabel("$A$",fontsize=25) ax1[1,1].set_xlabel("$A$",fontsize=25) fig1.tight_layout() fig1.savefig("paper_A_vs_eigs.png",dpi=300,transparent=is_transparent) os.system("open paper_A_vs_eigs.png")
def gen_cities_avg(climate, multi_cities, years): """ Compute the average annual temperature over multiple cities. Args: climate: instance of Climate multi_cities: the names of cities we want to average over (list of str) years: the range of years of the yearly averaged temperature (list of int) Returns: a pylab 1-d array of floats with length = len(years). Each element in this array corresponds to the average annual temperature over the given cities for a given year. """ # TODO yearly_avg = pylab.array([]) for year in years: multi_city_avg = pylab.array([]) for city in multi_cities: avg_city_temp = climate.get_yearly_temp(city, year).mean() multi_city_avg = pylab.append(multi_city_avg, avg_city_temp) yearly_avg = pylab.append(yearly_avg, multi_city_avg.mean()) return yearly_avg
def get_first_init(x0,epsilon,N): x_new = pl.copy(x0) print('getting the first initial condition') print('fiducial initial: '+str(x0)) # multi particle array layout [nth particle v, (n-1)th particle v , ..., 0th v, nth particle x, x, ... , 0th particle x] # we will use a change of coordinates to get the location of the particle relative to x0. First # we just find some random point a distace epsilon from the origin. # need 2DN random angles angle_arr = pl.array([]) purturbs = pl.array([]) # This is just an n-sphere for i in range(2*N): angle_arr = pl.append(angle_arr,random.random()*2.0*pl.pi) cur_purt = epsilon for a,b in enumerate(angle_arr[:-1]): cur_purt *= pl.sin(b) if i == (2*N-1): cur_purt = pl.sin(angle_arr[i]) else: cur_purt = pl.cos(angle_arr[i]) purturbs = pl.append(purturbs,cur_purt) print('sqrt of sum of squars should be epsilon -> is it? --> ' +str(pl.sqrt(pl.dot(purturbs,purturbs)))) print('len(purturbs) == 2N ? ' +str(len(purturbs)==(2*N))) return x_new+purturbs
def f(self,x,t): # This one line is different than in ECclass.py N = 2 xdot = pl.array([]) # modulus the x for periodicity. x[N:2*N]= x[N:2*N]%self.d # HERE ---->> 1Dify for i in range(N): temp = 0.0 for j in range(N): if i == j: continue #repulsive x interparticle force of j on i temp += self.qq*(x[N+i]-x[N+j])/(pl.sqrt((x[N+i]-x[N+j])**2)**3) # All of the forces coming from the 'same' paricle but from other 'cells' due to the # periodic contrains can be wraped up in a sum that converges to an aswer that can # be expressed in terms of polygamma functions (se pg 92 of notebook). # Note on the sign (xi-xj or xj-xi). Changing the sign of the xi-xj term (i.e. which # particle are we considering forces on) changes the direction of the force # apropriately. temp += self.qq*(polygamma(1,(self.d+x[N+i]-x[N+j])/self.d)-polygamma(1,1.0-((x[N+i]-x[N+j])/self.d)))/(self.d**2) # periodic force on particle i temp += self.A*pl.sin(x[N+i])*pl.cos(t) temp -= self.beta*x[i] xdot = pl.append(xdot,temp) for i in range(N): xdot = pl.append(xdot,x[i]) return xdot
def f(x, t): # for now masses just = 1.0 # the 4.0 only works for 2D N = len(x) / 4 xdot = pl.array([]) for i in range(N): temp = 0.0 for j in range(N): if i == j: continue temp += -(x[2 * N + i] - x[2 * N + j]) / ( pl.sqrt((x[2 * N + i] - x[2 * N + j]) ** 2 + (x[3 * N + i] - x[3 * N + j]) ** 2) ** 3 ) xdot = pl.append(xdot, temp) for i in range(N): temp = 0.0 for j in range(N): if i == j: continue temp += -(x[3 * N + i] - x[3 * N + j]) / ( pl.sqrt((x[2 * N + i] - x[2 * N + j]) ** 2 + (x[3 * N + i] - x[3 * N + j]) ** 2) ** 3 ) xdot = pl.append(xdot, temp) for i in range(N): xdot = pl.append(xdot, x[i]) for i in range(N): xdot = pl.append(xdot, x[N + i]) print("len xdot is: " + str(len(xdot))) return xdot
def LRM_GetPlasmons(fname): """ Given a csv containing a list of wavelengths, incident beam powers, and ambient powers, sequentially opens corresponding FITS images within the same directory and integrates the power contained within a ring (the ring center is defined for the first image and held constant for subsequent images; the radii are calculated based on wavelength and substrate material). Data are reported in a plot and saved to a csv report within the same directory as fname (the original list). """ wls = pl.array([]) # wavelength in nm beampwr = pl.array([]) # measured fianium beam power in nW darkpwr = pl.array([]) # measured ambient light power in nW inttime = pl.array([]) # sxv-h9 integration time in ms pwr = pl.array([]) # integrated SPP power in nW pce = pl.array( []) # Relative Power Conversion Efficiency, pwr/(beampwr-darkpwr) # Load wavelength,power,integration time data from file with open(fname, 'rb') as df: reader = csv.reader(df) for row in reader: wls = pl.append(wls, float(row[0])) beampwr = pl.append(beampwr, float(row[1])) darkpwr = pl.append(darkpwr, float(row[2])) inttime = pl.append(inttime, float(row[3])) # Correct measured beam power for ambient light level beampwr = beampwr - darkpwr # Collect FITS files from current directory path = os.path.dirname(fname) ip = InteractivePlot(fname, beampwr, wls, inttime, radii=None) pl.show() pce = ip.pce pwr = ip.pwr # Create pce plot pcefig = pl.figure() pl.plot(wls, pce, 'r') pl.savefig(path + 'pce_plot.png') pcefig = pl.figure() pl.plot(wls, pwr, 'b') pl.savefig(path + 'pwr_plot.png') pcefig = pl.figure() pl.plot(wls, beampwr, 'g') pl.savefig(path + 'beampwr_plot.png') # Output data as a csv with open(path + 'results.csv', 'wb') as f: writer = csv.writer(f) for i, wl in enumerate(wls): writer.writerow([wl, pwr[i], pce[i]])
def poly(self,step): # step is the radian resolution cA=self.cA self.sort() st=False fin=False cdi=0 alpha=0 x0=self.cA[0].c+point(self.cA[0].r,0) xc=x0 pl=[] while (alpha<(2*math.pi)): if self.side(xc)[-1]==0: pl.append(xc) st=True break else: xx=num.cos(step)*cA[cdi].r yy=num.sin(step)*cA[cdi].r xc_t=point(xx,yy) xc=xc_t.transform(-cA[cdi].c,-alpha) alpha=alpha+step if not st: return pl # pivoting pv0=self.get_x0() pv=point(pv0.x,pv0.y) v0=vec(xc,pv) vc=vec(xc,pv) spin=0.0 pv_found=False while spin<(2*math.pi): if self.side(pv)[0]==self.n: pv_found=True break else: vc=vc/2 if vc.mag()<10*res: spin=spin+10*step vc=v0.rot(spin) pv=xc+vc # pivoting finished if not pv_found: return [xc] alpha=num.linspace(0,2*math.pi,int(2*math.pi/step)) alpha[-1]=math.pi*2 for a in alpha: ray=Ray(pv,a) try: pc=self.intersect(ray) pl.append(pc) except geoError: print 'Unknown Error in ray-ndisc intersection' raise geoError, 'Unknown' pl[-1]=pl[0] return pl
def Calibrate(datafile): """ Run within a directory containing FITS files corresponding to the back focal plane image of a blank glass slide at different wavelengths. The filenames should be formatted as (wavelength in nm).fit (eg. '570.fit'). If using the Pixis CCD the files should be saved as TIFs. Loads a spreadsheet file containing data in 4 columns (wavelength, total beam power, ambient/dark power, integration time -- no headers). Sequentially loads FITS files and integrates total counts. The power for a given pixel is calculated as Power = counts * cal_factor / int_time, and thus using this data the cal_factor for each wavelength is determined. """ wls = pl.array([]) # wavelength in nm beampwr = pl.array([]) # measured fianium beam power in nW darkpwr = pl.array([]) # measured ambient light power in nW inttime = pl.array([]) # sxv-h9 integration time in ms counts = pl.array([]) # total counts in a given FITS file # Load wavelength,power,integration time data from file with open(datafile, 'rb') as df: reader = csv.reader(df) for row in reader: wls = pl.append(wls, float(row[0])) beampwr = pl.append(beampwr, float(row[1])) darkpwr = pl.append(darkpwr, float(row[2])) inttime = pl.append(inttime, float(row[3])) # Correct measured beam power for ambient light level beampwr = beampwr - darkpwr # Collect FITS files from current directory path = os.getcwd() for f in os.listdir(path): if os.path.splitext(f)[-1] == filetype: if filetype == '.TIF': im = LoadTiff(f) elif filetype == '.fit': im = LoadFITS(f) im = CleanImage(im) counts = pl.append(counts, (pl.sum(im))) # Compute calibration factor calfactor = beampwr * inttime / counts # Output calibration data as a csv with open('calibration.csv', 'wb') as f: writer = csv.writer(f) for i, wl in enumerate(wls): writer.writerow([wl, calfactor[i]]) pl.plot(wls, calfactor) pl.show()
def readDatDirectory(key, directory): global stats #Don't read data in if it's already read if not key in DATA["mean"]: data = defaultdict(array) #Process the dat files for datfile in glob.glob(directory + "/*.dat"): fileHandle = open(datfile, 'rb') keys, dataDict = csvExtractAllCols(fileHandle) stats = union(stats, keys) for aKey in keys: if not aKey in data: data[aKey] = reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))) else: data[aKey] = append(data[aKey], reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))), axis=0) #Process the div files' for datfile in glob.glob(directory + "/*.div"): fileHandle = open(datfile, 'rb') keys, dataDict = csvExtractAllCols(fileHandle) stats = union(stats, keys) for aKey in keys: if not aKey in data: data[aKey] = reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))) else: data[aKey] = append(data[aKey], reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))), axis=0) #Iterate through the stats and calculate mean/standard deviation for aKey in stats: if aKey in data: DATA["mean"][key][aKey] = mean(data[aKey], axis=0) DATA["median"][key][aKey] = median(data[aKey], axis=0) DATA["std"][key][aKey] = std(data[aKey], axis=0) DATA["ste"][key][aKey] = std(data[aKey], axis=0) / sqrt( len(data[aKey])) DATA["min"][key][aKey] = mean(data[aKey], axis=0) - amin( data[aKey], axis=0) DATA["max"][key][aKey] = amax(data[aKey], axis=0) - mean( data[aKey], axis=0) DATA["actual"][key][aKey] = data[aKey]
def moving_average(y, window_length): """ Compute the moving average of y with specified window length. Args: y: an 1-d pylab array with length N, representing the y-coordinates of the N sample points window_length: an integer indicating the window length for computing moving average Returns: an 1-d pylab array with the same length as y storing moving average of y-coordinates of the N sample points """ # TODO moving_avg = pylab.array([]) for i in range(len(y)): na = 0 #value not available numerator = 0 for j in range(window_length): if (i - j) >= 0: numerator += y[i - j] else: na += 1 avg_temp = numerator / (window_length - na) moving_avg = pylab.append(moving_avg, avg_temp) return moving_avg
def gen_std_devs(climate, multi_cities, years): """ For each year in years, compute the standard deviation over the averaged yearly temperatures for each city in multi_cities. Args: climate: instance of Climate multi_cities: the names of cities we want to use in our std dev calculation (list of str) years: the range of years to calculate standard deviation for (list of int) Returns: a pylab 1-d array of floats with length = len(years). Each element in this array corresponds to the standard deviation of the average annual city temperatures for the given cities in a given year. """ # TODO all_std = pylab.array([]) for year in years: days = len(climate.get_yearly_temp('NEW YORK', year)) each_year_overall = pylab.array([0] * days) for city in multi_cities: each_year_city = climate.get_yearly_temp(city, year) each_year_overall = each_year_city + each_year_overall each_year_avg = each_year_overall / len(multi_cities) each_year_std = pylab.std(each_year_avg) all_std = pylab.append(all_std, each_year_std) return all_std
def remove_discontinuity(value, xgap=10, ygap=200): """ Remove discontinuity (sudden jump) in a series of values. Written by Denis, developed for LLC Fringe Counts data. value : list or numpy.array xgap : "width" of index of the list/array to adjust steps ygap : threshold value to detect discontinuity """ difflist = pl.diff(value) discont_index = pl.find(abs(difflist) > ygap) if len(discont_index) == 0: return value else: discont_index = pl.append(discont_index, len(difflist)) # find indice at discontinuities discont = {'start': [], 'end': []} qstart = discont_index[0] for i in range(len(discont_index)-1): if discont_index[i+1]-discont_index[i] > xgap: qend = discont_index[i] discont['start'].append(qstart-xgap) discont['end'].append(qend+xgap) qstart = discont_index[i+1] # add offsets at discontinuities result = pl.array(value) for i in range(len(discont['end'])): result[0:discont['start'][i]] += \ result[discont['end'][i]] - result[discont['start'][i]] #remove the median result=result-pl.median(result) return result
def moving_average(y, window_length): """ Compute the moving average of y with specified window length. Args: y: an 1-d pylab array with length N, representing the y-coordinates of the N sample points window_length: an integer indicating the window length for computing moving average Returns: an 1-d pylab array with the same length as y storing moving average of y-coordinates of the N sample points """ moving_avg_result = pylab.array([]) for i in range(len(y)): numerator = 0 if (i + 1) < window_length: for x in range(i + 1): numerator += y[x] denominator = i + 1 else: for x in range(1, window_length + 1): numerator += y[i - x + 1] denominator = window_length moving_avg_result = pylab.append(moving_avg_result, numerator / denominator) return moving_avg_result
def moving_average(y, window_length): """ Compute the moving average of y with specified window length. Args: y: an 1-d pylab array with length N, representing the y-coordinates of the N sample points window_length: an integer indicating the window length for computing moving average Returns: an 1-d pylab array with the same length as y storing moving average of y-coordinates of the N sample points """ # TODO moving_average = pylab.array([]) for i in range(len(y)): j = max(i - window_length + 1, 0) alli, k = 0, j while k <= i: alli += y[k] k += 1 avgi = alli / (i - j + 1) moving_average = pylab.append(moving_average, avgi) return moving_average
def test_TS_LSTM(path, data, sc, intervalle=[]): dataX, dataY = data print(dataX) print(dataY) lstm = load_model_TS(path) lstm.eval() train_predict = lstm(dataX) data_predict = train_predict.cpu().data.numpy() dataY_plot = dataY.cpu().data.numpy() borne_inf, borne_sup = intervalle dif = data_predict - dataY_plot anomalyX = append(where(dif < borne_inf), where(dif > borne_sup)) #print("anoma",list(anomalyX)) data_predict = sc.inverse_transform(data_predict) dataY_plot = sc.inverse_transform(dataY_plot) anomalyY = data_predict[anomalyX] #plt.axvline(x=3000, c='r', linestyle='--') plt.plot(dataY_plot) plt.plot(data_predict) plt.scatter(anomalyX, anomalyY, color="red") plt.suptitle('Time-Series Prediction') plt.show()
def linearSL(phiOld, c, nt): "Semi-Lagrangian advection of profile in phiOld using" "linear interpolation" # the number of independent points nx = len(phiOld) - 1 # add another wrap-around point for cyclic boundaries phiOld = pl.append(phiOld, [phiOld[1]]) # new time-step arrays for phi phi = pl.zeros_like(phiOld) # loop over the time steps for it in xrange(1, nt + 1): # loop over the grid-points for j in xrange(1, len(phi) - 1): # The index of the point to the left of the departure point # (wrap around if less that zero) k = pl.floor(j - c) % nx # the location of the departure point within interval k->k+1 beta = (-c) % 1 # Linear interpolation onto the departure point phi[j] = (1 - beta) * phiOld[k] + beta * phiOld[k + 1] # cyclic BCs phi[0] = phi[-2] phi[-1] = phi[1] # update arrays phiOld = phi.copy() # return phi (without the cyclic wrap-around point) return phi[0:len(phi) - 1]
def f(filename, theClass=1): fs, data = wavfile.read(filename) # load the data # b=[(ele/2**8.)*2-1 for ele in data] # this is 8-bit track, b is now normalized on [-1,1) print "Sample rates is: " print fs X = stft(data, fs, 256.0 / fs, 256.0 / fs) X = X[:, 0:(X.shape[1] / 2)] shortTimeFFT = scipy.absolute(X.T) shortTimeFFT = scipy.log10(shortTimeFFT) # Plot the magnitude spectrogram. pylab.figure() pylab.imshow(shortTimeFFT, origin='lower', aspect='auto', interpolation='nearest') pylab.xlabel('Time') pylab.ylabel('Frequency') savefig(filename + 'SFFT.png', bbox_inches='tight') features = mean(shortTimeFFT, axis=1) pylab.figure() pylab.plot(features, 'r') savefig(filename + 'AFFT.png', bbox_inches='tight') with open(filename + '.csv', 'w') as fp: a = csv.writer(fp, delimiter=',') row = pylab.transpose(features) row = pylab.append(row, theClass) a.writerow(row)
def quick(phiOld, c, nt): "advection for nt time steps with a Courant number of c using QUICK" "(quadratic upwind)" # the number of independent points nx = len(phiOld) - 1 # add two wrap-around points for cyclic boundaries phiOld = pl.append(phiOld, [phiOld[1:3]]) # new time-step arrays for phi phi = phiOld.copy() # QUICK for all the time steps for it in xrange(nt): # forward-backward time-stepping for iter in xrange(2): phiMid = (3 * phi[2:] + 6 * phi[1:-1] - phi[0:-2]) / 8. phi[2:-1] = phiOld[2:-1] - c * (phiMid[1:] - phiMid[:-1]) # cyclic BCs phi[0] = phi[nx] phi[1] = phi[nx + 1] phi[nx + 2] = phi[2] # update arrays phiOld = phi.copy() # return phiNew (without the cyclic wrap-around points) return phi[0:nx + 1]
def fitData(inputFile): masses, distances = getData(inputFile) distances = pylab.array(distances) masses = pylab.array(masses) forces = pylab.array(masses) * 9.81 pylab.plot(forces, distances, 'ko', label="Measurement Displacements") pylab.title("Measured Displacement of Spring") pylab.xlabel("|Force| (Newtons)") pylab.ylabel("Distance (Meters)") # find linear fit a, b = pylab.polyfit(forces, distances, 1) predictedDistances = a * pylab.array(forces) + b k = 1.0 / a # a is dDist/dForce, k is dForce/dDist or a inverse pylab.plot(forces, predictedDistances, label="Displacements Predicted by \nLinear Fit, k = " + str(round(k, 5))) pylab.legend(loc='best') # find cubic fit fit = pylab.polyfit(forces, distances, 3) predictedDistances = pylab.polyval(fit, forces) forces_extend = pylab.append(forces, pylab.array([15])) predicted_distances_extend = pylab.polyval(fit, forces_extend) pylab.plot(forces_extend, predicted_distances_extend, 'k:', label='cubic fit') pylab.xlim(0, 16)
def remove_discontinuity(value, xgap=10, ygap=200): """ Remove discontinuity (sudden jump) in a series of values. Written by Denis, developed for LLC Fringe Counts data. value : list or numpy.array xgap : "width" of index of the list/array to adjust steps ygap : threshold value to detect discontinuity """ difflist = pl.diff(value) discont_index = pl.find(abs(difflist) > ygap) if len(discont_index) == 0: return value else: discont_index = pl.append(discont_index, len(difflist)) # find indice at discontinuities discont = {"start": [], "end": []} qstart = discont_index[0] for i in range(len(discont_index) - 1): if discont_index[i + 1] - discont_index[i] > xgap: qend = discont_index[i] discont["start"].append(qstart - xgap) discont["end"].append(qend + xgap) qstart = discont_index[i + 1] # add offsets at discontinuities result = pl.array(value) for i in range(len(discont["end"])): result[0 : discont["start"][i]] += result[discont["end"][i]] - result[discont["start"][i]] # remove the median result = result - pl.median(result) return result
def __parseOD(self, ll): '''OD data lines parsing method''' ll = [float(x) for x in ll] # Add the current time self.time.append(ll[0]) numRep = 1 prevClone = "" prevCond = "" if self.numClones == 1: totalReps = len(self.conditionsNU) / self.numConditions for idx, od in enumerate(ll[1:]): clone = self.clonesNU[idx] source = self.sourcesNU[idx] condition = self.conditionsNU[idx] # Check which clone + replicate we are observing if self.numClones == 1: numRep = (idx % totalReps) + 1 else: numRep = numRep + 1 if (clone == prevClone and condition == prevCond) else 1 prevClone = clone prevCond = condition # Append OD reading to array self.dataHash[clone][numRep][source][condition]['od'] =\ py.append(self.dataHash[clone][numRep][source] [condition]['od'], od)
def callback(msg): global record global data global count data = append(data, msg.data) # print len(msg.data) count += 1 if count == N_CYCLES: signal_1 = data[::2] signal_2 = data[1::2] # signal_1 = [struct.unpack('B', struct.pack('b',d))[0] for d in data[::2]] # signal_2 = [struct.unpack('B', struct.pack('b',d))[0] for d in data[1::2]] print len(signal_1) print signal_1 print "number of samples(frames): " + str(len(signal_1)) print "SAMPLING RATE: " + str(RATE) + "(Hz)" print "DURATION: " + str(DURATION) + "(s)" helper.plot_from_rawdata(signal_1, signal_2, RATE) rospy.signal_shutdown("Recording finished")
def unfilledBar(xaxis, bin_content, errors = None, rel_errors = None, count_errors = False, color = 'blue', **kwargs): ''' Produce a histogram bar plot a-la-ROOT. Returns plot axes. ''' original_xaxis = xaxis if xaxis[-1] == inf: xbin = xaxis[-2] - xaxis[-3] xaxis[-1] = xaxis[-2] + xbin new_bins = array(append(bin_content[0], bin_content)) xlim([xaxis[0], xaxis[-1]]) if rel_errors != None: errors = bin_content*rel_errors elif count_errors: errors = sqrt(bin_content) if errors != None: error_xaxis = array(xaxis) error_xaxis = error_xaxis[:-1] + (error_xaxis[1:] - error_xaxis[:-1])/2. errorbar(error_xaxis, bin_content, yerr = errors, color = color, fmt='.') return plot(xaxis , new_bins, drawstyle = 'steps', color = color, **kwargs)
def callback(msg): global record global data global count data = append(data, msg.data) #print len(msg.data) count += 1 if count == N_CYCLES: signal_1 = data[::2] signal_2 = data[1::2] #signal_1 = [struct.unpack('B', struct.pack('b',d))[0] for d in data[::2]] #signal_2 = [struct.unpack('B', struct.pack('b',d))[0] for d in data[1::2]] print len(signal_1) print signal_1 print "number of samples(frames): " + str(len(signal_1)) print "SAMPLING RATE: " + str(RATE) + "(Hz)" print "DURATION: " + str(DURATION) + "(s)" helper.plot_from_rawdata(signal_1, signal_2, RATE) rospy.signal_shutdown("Recording finished")
def f(filename, theClass=1): fs, data = wavfile.read(filename) # load the data # b=[(ele/2**8.)*2-1 for ele in data] # this is 8-bit track, b is now normalized on [-1,1) print "Sample rates is: " print fs X = stft(data, fs, 256.0 / fs, 256.0 / fs) X = X[:, 0 : (X.shape[1] / 2)] shortTimeFFT = scipy.absolute(X.T) shortTimeFFT = scipy.log10(shortTimeFFT) # Plot the magnitude spectrogram. pylab.figure() pylab.imshow(shortTimeFFT, origin="lower", aspect="auto", interpolation="nearest") pylab.xlabel("Time") pylab.ylabel("Frequency") savefig(filename + "SFFT.png", bbox_inches="tight") features = mean(shortTimeFFT, axis=1) pylab.figure() pylab.plot(features, "r") savefig(filename + "AFFT.png", bbox_inches="tight") with open(filename + ".csv", "w") as fp: a = csv.writer(fp, delimiter=",") row = pylab.transpose(features) row = pylab.append(row, theClass) a.writerow(row)
def price_to_15min( dt, Input_list ): #Don´t interpolate values but repeat the same value i+(dt-1) times e.g 1,2,3,4 = 1,1,1,1,2,2,2,2,3,3,3..... sz = int(Input_list.size) #dt = 4 for 15 min Split_list = [] for i in range(sz): Split_list = pl.append(Split_list, pl.ones(dt) * Input_list[i]) return Split_list
def plothuv(xh, yh, xu, yu, xv, yv, h, h0, u, v): "plot the height and velocity for A- or C-grid variables" dx = xh[-1] - xh[-2] dy = yh[-1] - yh[-2] py.clf() py.ion() x = py.append(xh - 0.5 * dx, xh[-1] + 0.5 * dx) y = py.append(yh - 0.5 * dy, yh[-1] + 0.5 * dy) py.colorbar(py.pcolormesh(x, y, py.transpose(h + h0))) if (np.max(h0) > np.min(h0)): py.contour(xh, yh, py.transpose(h0)) if (xh == xu).all(): py.quiver(xu, yu, py.transpose(u), py.transpose(v), scale=1e3) else: py.quiver(xu, yu, py.transpose(u), py.transpose(vatu(v)), scale=1e3) py.quiver(xv, yv, py.transpose(uatv(u)), py.transpose(v), scale=1e3) py.axis([np.min(x), np.max(x), np.min(y), np.max(y)])
def xytToEcef(lat, long, height, bearing, radius): x = (radius + height) * cos(lat) * cos(long) y = (radius + height) * cos(lat) * sin(long) z = (radius + height) * sin(lat) Rtmp = rotecef(lat, long) R = coord_xfms.rotz(bearing).dot(Rtmp) rph = coord_xfms.rot2rph(R.transpose()) pose = array([]) pose = append(pose, array([x, y, z])) pose = append(pose, rph) return pose
def Brucker_inlet(self): C1_Brucker = 10 * array([ 2.26, 1.53, 0.18, 0.19, 0.2, 0.05, -0.39, -0.69, -0.93, -0.66, -0.63, -0.7, -0.23, 2.51 ]) # 12:00 C1_Brucker2 = 10 * array([ -0.33, -0.35, -0.51, 0.99, 1.27, 0.83, 0.71, 0.67, -0.15, -0.71, -0.05, -0.21, -0.43, -0.62 ]) # 6:00 t_Brucker = 1e-3 * array([ 10, 73, 136, 199, 262, 325, 388, 451, 514, 577, 640, 703, 766, 829 ]) C = C1_Brucker[5:] C2 = C1_Brucker[:5] C1_Brucker = append(C, C2) Cop = C1_Brucker2[5:] Cop2 = C1_Brucker2[:5] C1_Brucker2 = append(Cop, Cop2) class MyExpression0(Expression): def __init__(self, t_Brucker, C1_Brucker, t): self.t = t self.t_Brucker = t_Brucker self.C1_Brucker = C1_Brucker def eval(self, values, x): t = self.t t_Brucker = self.t_Brucker C1_Brucker = self.C1_Brucker while t > 0.829: t -= 0.829 tval = t_Brucker yval = C1_Brucker idx = find(t_Brucker >= t)[0] - 1 values[1] = -(yval[idx] + (yval[idx + 1] - yval[idx]) * (t - tval[idx]) / (tval[idx + 1] - tval[idx])) values[0] = 0 def value_shape(self): return (2, ) return MyExpression0(t_Brucker, C1_Brucker, 0.0)
def readDatDirectory(key, directory): global stats #Don't read data in if it's already read if not key in DATA["mean"]: data = defaultdict(array) #Process the dat files for datfile in glob.glob(directory + "/*.dat"): fileHandle = open(datfile, 'rb') keys, dataDict = csvExtractAllCols(fileHandle) stats = union(stats, keys) for aKey in keys: if not aKey in data: data[aKey] = reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))) else: data[aKey] = append(data[aKey], reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))), axis=0) #Process the div files' for datfile in glob.glob(directory + "/*.div"): fileHandle = open(datfile, 'rb') keys, dataDict = csvExtractAllCols(fileHandle) stats = union(stats, keys) for aKey in keys: if not aKey in data: data[aKey] = reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))) else: data[aKey] = append(data[aKey], reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))), axis=0) #Iterate through the stats and calculate mean/standard deviation for aKey in stats: if aKey in data: DATA["mean"][key][aKey] = mean(data[aKey], axis=0) DATA["median"][key][aKey] = median(data[aKey], axis=0) DATA["std"][key][aKey] = std(data[aKey], axis=0) DATA["ste"][key][aKey] = std(data[aKey], axis=0)/ sqrt(len(data[aKey])) DATA["min"][key][aKey] = mean(data[aKey], axis=0)-amin(data[aKey], axis=0) DATA["max"][key][aKey] = amax(data[aKey], axis=0)-mean(data[aKey], axis=0) DATA["actual"][key][aKey] = data[aKey]
def xytToEcef (lat, long, height, bearing, radius): x = (radius + height) * cos (lat) * cos (long) y = (radius + height) * cos (lat) * sin (long) z = (radius + height) * sin (lat) Rtmp = rotecef (lat, long); R = coord_xfms.rotz (bearing).dot (Rtmp) rph = coord_xfms.rot2rph (R.transpose ()); pose = array([]) pose = append (pose, array ([x, y, z])) pose = append (pose, rph) return pose
def get_poin(sol,dt): poin = pl.array([]) for i in range(len(sol)): if(((i*dt)%(2.0*pl.pi))<=dt): poin = pl.append(poin,sol[i,:]) poin = poin.reshape(-1,4) # flip order of array #poin = poin[::-1,:] return poin
def DiscreteSum(self, lower, upper): """ Perform the rectangular integration, starting and ending at the values passed to it (0-->N for rect, 1-->N for trap, etc...). """ for i in range(int(lower), int(upper)): self.I += self.f(self.a + i * self.dx) * self.dx self.plot = pl.append(self.plot, self.I) return self.I, self.plot
def DiscreteSum(self,lower,upper): """ Perform the rectangular integration, starting and ending at the values passed to it (0-->N for rect, 1-->N for trap, etc...). """ for i in range(int(lower),int(upper)): self.I += self.f(self.a+i*self.dx)*self.dx self.plot = pl.append(self.plot, self.I) return self.I, self.plot
def get_fit(which): f = open("final_position.txt","r") data = pl.genfromtxt(f,comments = "L") if which=="x": datnum = 2 if which=="vx": datnum = 0 x = pl.array([]) y = pl.array([]) for i,j in enumerate(data[:-7,datnum]): if i%2 == 0: x = pl.append(x,data[i,4]) y = pl.append(y,j) fit = pl.polyfit(x,y,2) fitted = pl.poly1d(fit) return fitted
def parser(self, argument): """ Parse data from the data source and fills the values array """ while True: buffer = self.handler.serialHandler() if buffer <> None: sample = self.unpackSample(buffer) if sample <> None: self.values = pylab.append(self.values, pylab.array([sample]), axis=0) else: break
def romb(self): """ Romberg numerical quadrature to 6 digits of agreement between subsequent diagonal elements. """ R = pl.zeros([1, 1]) # check convergence twice (to avoid flukes) Rdiff1 = 100.0 Rdiff2 = 100.0 # zeroeth order term constant = 0.50 * (self.b - self.a) R[0, 0] = constant * (self.f(self.a) + self.f(self.b)) count = 1 # higher order terms while (Rdiff2 > 0.000001) or (Rdiff1 > 0.000001): # increase size of matrix by 1 row,column R = pl.append(R, pl.zeros([1, count]), 0) R = pl.append(R, pl.zeros([count + 1, 1]), 1) # build next row, and only that row for n in range(count, count + 1): dx = (self.b - self.a) / (2.0**n) for m in range(count + 1): if m <= n: if m == 0: R[n, m] += 0.5 * R[n - 1, m] for j in range(1, int(2**(n - 1) + 1)): R[n, m] += dx * self.f(self.a + (2 * j - 1) * dx) else: term1 = R[n, m - 1] term2 = R[n - 1, m - 1] R[n, m] = ((4**m) * term1 - term2) / (4**m - 1) count += 1 Rdiff1 = abs(R[-1, -1] - R[-2, -2]) if count > 3: Rdiff2 = abs(R[-2, -2] - R[-3, -3]) return R[-1, -1]
def main(): # read in the forest as peak yield args = parseCMD() fileName = args.fileN forest = pl.loadtxt(fileName) # determine which sites were left unTreed at maximum yield sitesLeft = pl.array([]) for i in xrange(forest.size): if forest[i]!=1: sitesLeft = pl.append(sitesLeft, i) sitesLeft = sitesLeft[::-1] # determine intervals and sort them in order of size intervals = pl.array([forest.size-1-sitesLeft[0]]) for i in xrange(1,sitesLeft.size-1): sizeInt = sitesLeft[i]-sitesLeft[i+1] intervals = pl.append(intervals, sizeInt) intervals = pl.append(intervals, sitesLeft[-1]) intervals = pl.sort(intervals) # assign each interval a number nums = pl.arange(1,sitesLeft.size+1) # main plot fig1 = pl.figure(1) ax = fig1.add_subplot(111) pl.ylabel('Interval Size', fontsize=20) pl.xlabel('(Sorted) Inverval Number', fontsize=20) # loop over and plot each file we find newt = getD(fileName) ax.plot(nums,intervals,label='(D=%s)'%(newt), marker='o', linewidth=0, markerfacecolor='None', markeredgecolor='Navy') # put labels into legend ax.legend(loc='upper left',shadow=True) pl.show()
def unwrap(xs, min_value, max_value, in_place=False, jump_fraction=0.5): range_ = max_value - min_value jump_threshold = range_ * jump_fraction diffs = pl.diff(xs) octave_diffs = pl.zeros(len(xs) - 1, dtype=pl.int64) octave_diffs[diffs > jump_threshold] = -1 octave_diffs[diffs < -jump_threshold] = 1 octaves = pl.append(0, pl.cumsum(octave_diffs)) if in_place: xs += octaves * range_ else: return xs + octaves * range_
def trap(self): """ Trapezoidal numerical quadrature. Returns the value of the definite integral as well as an array for plotting of the instantaneous integral values over the integration variable. """ self.I += (self.dx / 2.0) * self.f(self.a) self.plot = pl.append(self.plot, self.I) self.I, self.plot = self.DiscreteSum(1, self.N) self.I += (self.dx / 2.0) * self.f(self.b) return self.I, self.plot
def trap(self): """ Trapezoidal numerical quadrature. Returns the value of the definite integral as well as an array for plotting of the instantaneous integral values over the integration variable. """ self.I += (self.dx/2.0)*self.f(self.a) self.plot = pl.append(self.plot,self.I) self.I,self.plot = self.DiscreteSum(1,self.N) self.I += (self.dx/2.0)*self.f(self.b) return self.I, self.plot
def romb(self): """ Romberg numerical quadrature to 6 digits of agreement between subsequent diagonal elements. """ R = pl.zeros([1,1]) # check convergence twice (to avoid flukes) Rdiff1 = 100.0 Rdiff2 = 100.0 # zeroeth order term constant = 0.50*(self.b-self.a) R[0,0] = constant*(self.f(self.a)+self.f(self.b)) count = 1 # higher order terms while (Rdiff2 > 0.000001) or (Rdiff1 > 0.000001) : # increase size of matrix by 1 row,column R = pl.append(R, pl.zeros([1,count]), 0) R = pl.append(R, pl.zeros([count+1,1]), 1) # build next row, and only that row for n in range(count,count+1): dx = (self.b-self.a)/(2.0**n) for m in range(count+1): if m<=n: if m==0: R[n,m] += 0.5*R[n-1,m] for j in range(1,int(2**(n-1)+1)): R[n,m] += dx*self.f(self.a+(2*j-1)*dx) else: term1 = R[n,m-1] term2 = R[n-1,m-1] R[n,m] = ((4**m)*term1-term2)/(4**m-1) count += 1 Rdiff1 = abs(R[-1,-1] - R[-2,-2]) if count > 3: Rdiff2 = abs(R[-2,-2] - R[-3,-3]) return R[-1,-1]
def spatio_temporal(ancl): os.mkdir('SpatioTemporalVels') print('RIGHT NOW THIS IS ONLY FOR VX!!!!!!!') p_arr = pl.arange(0,ancl.N) # How many cycles do we want to look at? how_many = 10 var_arr = pl.array([]) for i,j in enumerate(os.listdir('.')): if 'poindat.txt' not in j: continue print('working on file ' + j) poin_num = int(j[:j.find('p')]) cur_file = open(j,'r') cur_sweep_var = float(cur_file.readline().split()[-1]) cur_data=pl.genfromtxt(cur_file) cur_file.close() var_arr = pl.append(var_arr,cur_sweep_var) count = 0 grid = cur_data[-int(how_many*2.0*pl.pi/ancl.dt):,:ancl.N] # in 1D because particles never cross eachother we can order them in the images to mathch # their physical order. grid_ordered = pl.zeros(pl.shape(grid)) # can just use the initial conditions to figure out where each is init_x = cur_data[0,ancl.N:2*ancl.N] sorted_x = sorted(init_x) for a,alpha in enumerate(sorted_x): for b,beta in enumerate(init_x): if alpha == beta: grid_ordered[:,a]=grid[:,b] print('shape of grid_ordered: ' + str(pl.shape(grid_ordered))) fig = pl.figure() ax = fig.add_subplot(111) # form of errorbar(x,y,xerr=xerr_arr,yerr=yerr_arr) ax.imshow(grid_ordered,interpolation="nearest", aspect='auto') ax.set_xlabel('Particle',fontsize=30) #ax.set_aspect('equal') ax.set_ylabel(r'$ t $',fontsize=30) fig.tight_layout() fig.savefig('SpatioTemporalVels/%(number)04d.png'%{'number':poin_num}) pl.close(fig)
def get_init_arr(lines): arr = pl.array([]) count = -1 ln_splited = lines[count].split() while ("ZONE" not in ln_splited[0]): for i,j in enumerate(ln_splited): arr = pl.append(arr,float(j)) count -=1 ln_splited = lines[count].split() arr = arr.reshape(abs(count)-1,4) return arr, (abs(count)-1)
def temp_one_year(climate, city, interval, degs): years = pylab.array(interval) avg_yearly_temps = pylab.array([]) for year in years: day_length = 0 for month in range(1, 13): day_length += len(climate.rawdata[city][year][month]) avg_yearly_temps = pylab.append( avg_yearly_temps, sum(climate.get_yearly_temp(city, year)) / day_length) model = generate_models(years, avg_yearly_temps, degs) evaluate_models_on_training(years, avg_yearly_temps, model) return avg_yearly_temps
def moving_average(y, window_length): """ Compute the moving average of y with specified window length. Args: y: an 1-d pylab array with length N, representing the y-coordinates of the N sample points window_length: an integer indicating the window length for computing moving average Returns: an 1-d pylab array with the same length as y storing moving average of y-coordinates of the N sample points """ i = 1 mov_average = pylab.array([]) while i < window_length: mov_average = pylab.append(mov_average, pylab.array(sum(y[0:i]) / i)) i += 1 for j in range(i, len(y) + 1): mov_average = pylab.append(mov_average, pylab.array(sum(y[j - window_length:j]) / window_length)) return mov_average
def estimate_fwhm_ms(int_matrix, numberOfScansForFWHMCalc): """ This function is going to randomly select a variety of spectra and estimate a FWHM using a gaussian fit on some of the most intense peaks in the selected spectra. :param int_matrix: Matrix of all data points :param numberOfScansForFWHMCalc: how many scans will we use for estimate :return: estimate of full-width-half-max in number of points """ # keep the values to be averaged over fwhm_arr = pl.array([]) print "Estimating FWHM of profile MS" print "..." for i in range(numberOfScansForFWHMCalc): # randomly select a scan to investigate. spec_index = random.randint( 0, len(pl.squeeze(pl.asarray(int_matrix[0, :].todense()))) - 1) cur_spec = pl.squeeze(pl.asarray(int_matrix[:, spec_index].todense())) # could be 0 intensity points in good spectra becuase of the way the int_matrix is bui # lets work in units of scan number x = pl.arange(0, len(cur_spec)) # Only going to be looking at the highest peak in each scan. Get the location of that peak. max_y = max(cur_spec) scan_max_y = pl.where(cur_spec == max_y)[0][0] popt, pcov = curve_fit(curves.gaussian, x, cur_spec, bounds=([ max_y * 0.8, scan_max_y - 1, 0.25 / 2.35482 ], [max_y * 1.2, scan_max_y + 1, 5 / 2.35482])) tmp_x = x tmp_y = curves.gaussian(x, popt[0], popt[1], popt[2]) #pl.plot(tmp_x[scan_max_y-20:scan_max_y+20],tmp_y[scan_max_y-20:scan_max_y+20],c='r') #pl.plot(x[scan_max_y-20:scan_max_y+20],cur_spec[scan_max_y-20:scan_max_y+20],c='b',marker='o') #pl.show() # with the right fit parameter you can determine the full width at half max. # 2.35... is a known value to take this parameter and get the FWHM with. fwhm = 2.35482 * popt[2] fwhm_arr = pl.append(fwhm_arr, fwhm) avg_fwhm = fwhm_arr.mean() print "Done estimating FWHM of profile MS" print("avg_fwhm: " + str(avg_fwhm)) # round up return int(avg_fwhm + 0.5)
def read_file(datafile,name): with open(datafile) as f: data = f.read() data=data.split('\n') n=0;i=0 x=[];px=[];y=[];py=[];t=[];pt=[]; for row in data: if row.startswith('@') or row.startswith('*') \ or row.startswith('$') or row.startswith('#')\ or len(row)==0: continue else: i+=1 w=" ".join(row.split()) s=w.split() x.append(float(s[2])) px.append(float(s[3])) y.append(float(s[4])) py.append(float(s[5])) t.append(float(s[6])) pt.append(float(s[7])) do_plot(x,px,y,py,t,pt,name) return x,px,y,py,t,pt
def appendDataArrays( a1, a2, a3, a4, a5, a6, a7, timeDir ): # Read data fileName = timeDir+'/turboPerformance.dat' #a1b, a2b, a3b, a4b, a5b, a6b, a7b = pl.load(fileName,skiprows=1,usecols=(0,1,2,3,4,5,6),unpack=True) a1b, a2b, a3b, a4b, a5b, a6b, a7b = pl.mlab.load(fileName,skiprows=1,usecols=(0,1,2,3,4,5,6),unpack=True) # Append data a1 = pl.append(a1 , a1b) a2 = pl.append(a2 , a2b) a3 = pl.append(a3 , a3b) a4 = pl.append(a4 , a4b) a5 = pl.append(a5 , a5b) a6 = pl.append(a6 , a6b) a7 = pl.append(a7 , a7b) return a1, a2, a3, a4, a5, a6, a7
def callback(msg): global record global data global count data = append(data, msg.data) #print len(msg.data) count += 1 if count == N_CYCLES: #print data #print data.shape signal_1 = [] signal_2 = [] data_1 = data[::2] data_2 = data[1::2] #print "data_1=", data_1 #print "data_2=", data_2 for i in data_1: packed_value = struct.pack('B', i) signal_1.append(packed_value) for i in data_2: packed_value = struct.pack('B', i) signal_2.append(packed_value) #print hex(ord(packed_value)) value_str_1 = ''.join(signal_1) value_str_2 = ''.join(signal_2) record_1 = wave.open(FILENAME_1, 'w') record_1.setparams((1, 1, RATE, len(signal_1), 'NONE', 'not compressed')) record_1.writeframesraw(value_str_1) record_2 = wave.open(FILENAME_2, 'w') record_2.setparams((1, 1, RATE, len(signal_2), 'NONE', 'not compressed')) record_2.writeframesraw(value_str_2) print "number of samples(frames): " + str( len(signal_1)) print "SAMPLING RATE: " + str(RATE) + "(Hz)" print "DURATION: " + str(DURATION) + "(s)" record_1.close() record_2.close() print "WAV FILENAME 1: " + FILENAME_1 print "WAV FILENAME 2: " + FILENAME_2 #print "Ctrl-C to close the program" rospy.signal_shutdown("Recording finished")
def indepVarList(estimFiles, canonical, reduceVar): ''' Make list of all independent variable, in order, based on ensemble. This is set up to be temperature or chemical potential. If another variable is desired, then proper adjustments may need to be made. ''' indList = pl.array([]) for f in estimFiles: if reduceVar == 'T': if canonical: tempVar = f[13:19] else: tempVar = f[14:20] elif reduceVar == 'u': if canonical: tempVar = f[28:35] else: tempVar = f[29:36] if tempVar not in indList: indList = pl.append(indList, tempVar) return pl.sort(indList)
def testRoundActivationsUpOrDown(self): result = roundActivationsUpOrDown(linspace(-1,1,10)) target = append(zeros(5)-1, ones(5)) self.assertEqual((result-target).any(), False)
def main(): # initial purturbation size epsilon = 1.0e-7 print('epsilon is: '+str(epsilon)) # period variable should probably just be kept at the actual period (2*pl.pi) but the p # this variable is to alow us to changhe the lenght of time we wate before we colect th # distance information and renormalize. This is also nesssasary in the final calculatio # LE becae LE = 1/period * ln(rm/r0). period = 2.0*pl.pi print('period is: '+str(period)) # first throw away (to get initial conditions in atractor) throw_away_1 = 250 print('throw_away_1 is: '+str(throw_away_1)) #throw_away_1 = 200 # second throw away (to make sure puturbed trajectory is aligned) throw_away_2 = 100 print('throw_away_2 is: '+str(throw_away_2)) #throw_away_2 = 200 # number of cycle-sets to go through # try num = 55000 print('number of cycles is: '+str(num)) # works #num = 60000 #num = 16000 #num = 30000 #num = 8000 dt = .001 print('dt is: '+str(dt)) # total number of sterations to perform inorder to get cycles rioht totIter = period/dt totTime = period time = pl.arange(0.0,totTime,dt) #time array for the first run to get initial condition in strange atractor first_time = pl.arange(0.0,throw_away_1*2.0*pl.pi,dt) surf = 1.0 coef = 9.0 k = 1.0 w = 1.0 damp = .05 g = .1 # how many cells is till periodicity use x = n*pi/k (n must be even #) modNum = 2*pl.pi/k modNum = 2.0*pl.pi # some random initial conditions initx = 3.649 inity = 1.3 initvx = .1237 initvy = 0.0 # now find initial condition in stange atractor by running far in time. print this to use it for # later x0 = pl.array([initvx,initvy,initx,inity]) apx = ec.CentreLineApx(coef,k,w,damp,surf,g,dt) first_sol = odeint(apx.f,x0,first_time) #plot_first_sol(first_sol,dt) # now reset x0 first_sol[:,2]=first_sol[:,2]%(2*pl.pi) x0 = first_sol[-1,:] x_other = get_first_init(x0,epsilon) print("x0 from first_sol is:") print(x0) print("x_other (purturbed is:") print x_other # two arrays to store solution data arr_orig = pl.array([]) arr_othr = pl.array([]) full_orig = pl.array([]) full_othr = pl.array([]) # array to keep distance after driving cycle darr = pl.array([]) watch = pl.array([]) watch_le = 0.0 for i in range(num + throw_away_2): sol = odeint(apx.f,x0,time) sol_other = odeint(apx.f,x_other,time) sol[:,2]=sol[:,2]%(2*pl.pi) sol_other[:,2]=sol_other[:,2]%(2*pl.pi) #arr_orig = pl.append(arr_orig,sol[-1,:]) #arr_othr = pl.append(arr_othr,sol_other[-1,:]) if i> throw_away_2: #get the new distance darr = pl.append(darr,distance(sol[-1,:],sol_other[-1,:])) watch_le += pl.log(abs(darr[-1]/epsilon)) cur_avg = watch_le/(i-throw_away_2+1)/period watch = pl.append(watch,cur_avg) #full_orig = pl.append(full_orig,sol) #full_othr = pl.append(full_othr,sol_other) # poin = get_poin(sol,dt) # poin_other = get_poin(sol_other,dt) x0 = sol[-1,:] x_other = renormalize(x0,sol_other[-1,:],epsilon) #arr_orig = arr_orig.reshape(-1,4) #arr_othr = arr_othr.reshape(-1,4) #full_orig = full_orig.reshape(-1,4) #full_othr = full_othr.reshape(-1,4) eps_arr = pl.zeros(len(darr))+epsilon le = pl.zeros(len(darr))+pl.log(abs(darr/epsilon))/period le_avg = 0.0 for i,j in enumerate(le): le_avg += j le_avg = le_avg/len(le) print("le is") print(le_avg) fig = pl.figure() ax = fig.add_subplot(111) ax.scatter(pl.arange(len(watch)),watch,s=.1) #ax.set_xlabel("$x_1$",fontsize=25) #ax.set_ylabel("$x_2$",fontsize=25) #ax.set_xlim([0,2*pl.pi]) #ax.set_ylim([-1.3,1.3]) #fig.tight_layout() fig.savefig("convergence.png") os.system("open convergence.png")
def main(): is_transparent = False # this variable just exsits so we dont print the A value of the bifurca # once. found_bif = False dt = .001 # total number of iterations to perform totIter = 50000 totTime = totIter*dt time = pl.arange(0.0,totTime,dt) # initial conditions initx = pl.pi inity = 1.0 initvx = 0.0 initvy = 0.0 surf = 1.0 coef = .1 k = 1.0 w = 1.0 damp = .5 g = .1 # how many cells is till periodicity use x = n*pi/k (n must be even #) modNum = 2*pl.pi/k modNum = 2.0*pl.pi A = coef A_max = .30 A_step = .001 count = 0 # make arrays to keep eigen values. There willl be two eigen values so lets hve two seperate # arrays for them eigs1 = pl.array([]) eigs2 = pl.array([]) while A < A_max: # initial conditions vector # set up: [xdot,ydot,x,y] x0 = pl.array([initvx,initvy,initx,inity]) apx = surfCentreLineApx(A,k,w,damp,dt) #sol = odeint(apx.f,x0,time) #sol[:,2]=sol[:,2]%(2*pl.pi) # find a single loop of the limit cycle. Might be periodoc over more than one cycle # returns the solution of just that loop AND the periodicity of the loop # takes a threshhold number. If it cant find a solution where the begining and end of the # trajectroy lye within this threshold value than it quits and prints an error #thresh is distance in the phase place thresh = .005 #loop = find_one_full_closed(sol,thresh,dt) #loop for the 0 fixed point is just 0,0,0,0,0,0,0 loop_t = pl.arange(0.0,2.0*pl.pi,dt) w0 = pl.array([1.0,0.0,0.0,1.0]) w_of_t = odeint(apx.mw,w0,loop_t,hmax=dt,hmin=dt) #w_of_t = odeint(apx.mw,w0,loop_t) print("len w_of_t: " + str(len(w_of_t))) # make the matrix form of w_of_t matrix = w_of_t[-1,:].reshape(2,2) # print the determinante of the matrix. should be constant value (.2846) print("determinant W") print(matrix[0,0]*matrix[1,1]-matrix[1,0]*matrix[0,1]) # use linalg to get the eigen values of the W(t=q) where q is the period time of the orbit vals,vect = numpy.linalg.eig(matrix) if((abs(vals[0])>=1.0) & (not found_bif)): print("this is the bifurcation point (l1)") print(A) found_bif = True if((abs(vals[1])>=1.0) & (not found_bif)): print("this is the bifurcation point (l2)") print(A) found_bif = True eigs1 = pl.append(eigs1,vals[0]) eigs2 = pl.append(eigs2,vals[1]) count+=1 # x0 = loop[-1,:] A += A_step print(A) theta = pl.arange(0,10,.01) fig1 = pl.figure() ax1 = fig1.add_subplot(111) ax1.plot(pl.cos(theta),pl.sin(theta),color = "Black") ax1.plot([k.real for k in eigs1],[l.imag for l in eigs1],color = "Black") ax1.set_xlabel("Re[$\lambda_1$]",fontsize=25) ax1.set_ylabel("Im[$\lambda_1$]",fontsize=25) fig1.tight_layout() fig1.savefig("pi_eig1.png",dpi=300,transparent=is_transparent) os.system("open pi_eig1.png") fig2 = pl.figure() ax2 = fig2.add_subplot(111) ax2.plot(pl.cos(theta),pl.sin(theta),color = "Black") ax2.plot([k.real for k in eigs2],[l.imag for l in eigs2],color = "Black") ax2.set_xlabel("Re[$\lambda_2$]",fontsize=25) ax2.set_ylabel("Im[$\lambda_2$]",fontsize=25) fig2.tight_layout() fig2.savefig("pi_eig2.png",dpi=300,transparent=is_transparent) os.system("open pi_eig2.png") A_arr = pl.arange(coef,A_max,A_step) print("length of As and eigs") print(len(A_arr)) print(len([k.real for k in eigs1])) while len(A_arr)>len([k.real for k in eigs1]): A_arr = A_arr[:-1] while len(A_arr)<len([k.real for k in eigs1]): A_arr = pl.append(A_arr,A_arr[-1]+A_step) print(len(A_arr)) print(len([k.real for k in eigs1])) fig3, ax3 = pl.subplots(2,sharex=True) ax3[0].plot(A_arr,[k.real for k in eigs1],color = "Black") ax3[1].plot(A_arr,[k.imag for k in eigs1],color = "Black") ax3[0].set_ylabel("Re[$\lambda_1$]",fontsize=25) ax3[1].set_ylabel("Im[$\lambda_1$]",fontsize=25) ax3[1].set_xlabel("$A$",fontsize=25) fig3.tight_layout() fig3.savefig("pi_A_vs_eig1.png",dpi=300,transparent=is_transparent) os.system("open pi_A_vs_eig1.png") fig4, ax4 = pl.subplots(2,sharex=True) ax4[0].plot(A_arr,[k.real for k in eigs2],color = "Black") ax4[1].plot(A_arr,[k.imag for k in eigs2],color = "Black") ax4[0].set_ylabel("Re[$\lambda_2$]",fontsize=25) ax4[1].set_ylabel("Im[$\lambda_2$]",fontsize=25) ax4[1].set_xlabel("$A$",fontsize=25) fig4.tight_layout() fig4.savefig("pi_A_vs_eig2.png",dpi=300,transparent=is_transparent) os.system("open pi_A_vs_eig2.png") fig5, ax5 = pl.subplots(2,sharex=True) ax5[0].plot(A_arr,abs(eigs1),color = "Black") ax5[1].plot(A_arr,abs(eigs2),color = "Black") ax5[0].set_ylabel("$\lambda_1$",fontsize=25) ax5[1].set_ylabel("$\lambda_2$",fontsize=25) ax5[1].set_xlabel("$A$",fontsize=25) fig5.tight_layout() fig5.savefig("pi_A_vs_mag_eigs.png",dpi=300,transparent=is_transparent) os.system("open pi_A_vs_mag_eigs.png") eig_file = open("pi_data.txt","w") eig_file.write("eig1 eig2 A\n") for i in range(len(eigs1)): eig_file.write(str(eigs1[i])+" "+str(eigs2[i])+" "+str(A_arr[i])+"\n") eig_file.close()