def _logticks(self, lower, upper): #lower,upper = map(_Numeric.log10,[lower,upper]) #print 'logticks',lower,upper ticks = [] mag = _Numeric.power(10,_Numeric.floor(lower)) if upper-lower > 6: t = _Numeric.power(10,_Numeric.ceil(lower)) base = _Numeric.power(10,_Numeric.floor((upper-lower)/6)) def inc(t): return t*base-t else: t = _Numeric.ceil(_Numeric.power(10,lower)/mag)*mag def inc(t): return 10**int(_Numeric.floor(_Numeric.log10(t)+1e-16)) majortick = int(_Numeric.log10(mag)) while t <= pow(10,upper): if majortick != int(_Numeric.floor(_Numeric.log10(t)+1e-16)): majortick = int(_Numeric.floor(_Numeric.log10(t)+1e-16)) ticklabel = '1e%d'%majortick else: if upper-lower < 2: minortick = int(t/pow(10,majortick)+.5) ticklabel = '%de%d'%(minortick,majortick) else: ticklabel = '' ticks.append((_Numeric.log10(t), ticklabel)) t += inc(t) if len(ticks) == 0: ticks = [(0,'')] return ticks
def processBuffer(self, bounds): self.param1 = bounds.height/65536.0 self.param2 = bounds.height/2.0 if(self.stop==False): Fs = 48000 nfft= 65536 self.newest_buffer=self.newest_buffer[0:256] self.fftx = fft(self.newest_buffer, 256,-1) self.fftx=self.fftx[0:self.freq_range*2] self.draw_interval=bounds.width/(self.freq_range*2.) NumUniquePts = ceil((nfft+1)/2) self.buffers=abs(self.fftx)*0.02 self.y_mag_bias_multiplier=0.1 self.scaleX = "hz" self.scaleY = "" if(len(self.buffers)==0): return False # Scaling the values val = [] for i in self.buffers: temp_val_float = float(self.param1*i*self.y_mag) + self.y_mag_bias_multiplier * self.param2 if(temp_val_float >= bounds.height): temp_val_float = bounds.height-25 if(temp_val_float <= 0): temp_val_float = 25 val.append( temp_val_float ) self.peaks = val
def __init__(self, shp, ptlist, origin, selSense, **opts): """ ptlist is a list of 3d points describing a selection. origin is the center of view, and normal gives the direction of the line of light. Form a structure for telling whether arbitrary points fall inside the curve from the point of view. """ # bruce 041214 rewrote some of this method simple_shape_2d.__init__( self, shp, ptlist, origin, selSense, opts) # bounding rectangle, in integers (scaled 8 to the angstrom) ibbhi = array(map(int, ceil(8 * self.bboxhi)+2)) ibblo = array(map(int, floor(8 * self.bboxlo)-2)) bboxlo = self.bboxlo # draw the curve in these matrices and fill it # [bruce 041214 adds this comment: this might be correct but it's very # inefficient -- we should do it geometrically someday. #e] mat = zeros(ibbhi - ibblo) mat1 = zeros(ibbhi - ibblo) mat1[0,:] = 1 mat1[-1,:] = 1 mat1[:,0] = 1 mat1[:,-1] = 1 pt2d = self.pt2d pt0 = pt2d[0] for pt in pt2d[1:]: l = ceil(vlen(pt - pt0)*8) if l<0.01: continue v=(pt - pt0)/l for i in range(1 + int(l)): ij = 2 + array(map(int, floor((pt0 + v * i - bboxlo)*8))) mat[ij]=1 pt0 = pt mat1 += mat fill(mat1, array([1, 1]),1) mat1 -= mat #Which means boundary line is counted as inside the shape. # boolean raster of filled-in shape self.matrix = mat1 ## For any element inside the matrix, if it is 0, then it's inside. # where matrix[0, 0] is in x, y space self.matbase = ibblo # axes of the plane; only used for debugging self.x = self.right self.y = self.up self.z = self.normal
def lowess2(x, y, xest, f=2./3., iter=3): """Returns estimated values of y in data points xest (or None if estimation fails). Lowess smoother: Robust locally weighted regression. The lowess function fits a nonparametric regression curve to a scatterplot. The arrays x and y contain an equal number of elements; each pair (x[i], y[i]) defines a data point in the scatterplot. The function returns the estimated (smooth) values of y. The smoothing span is given by f. A larger value for f will result in a smoother curve. The number of robustifying iterations is given by iter. The function will run faster with a smaller number of iterations.""" x = Numeric.asarray(x, 'd') y = Numeric.asarray(y, 'd') xest = Numeric.asarray(xest, 'd') n = len(x) nest = len(xest) r = min(int(Numeric.ceil(f*n)),n-1) # radius: num. of points to take into LR h = [Numeric.sort(abs(x-x[i]))[r] for i in range(n)] # distance of the r-th point from x[i] w = Numeric.clip(abs(([x]-Numeric.transpose([x]))/h),0.0,1.0) w = 1-w*w*w w = w*w*w hest = [Numeric.sort(abs(x-xest[i]))[r] for i in range(nest)] # r-th min. distance from xest[i] to x west = Numeric.clip(abs(([xest]-Numeric.transpose([x]))/hest),0.0,1.0) # shape: (len(x), len(xest) west = 1-west*west*west west = west*west*west yest = Numeric.zeros(n,'d') yest2 = Numeric.zeros(nest,'d') delta = Numeric.ones(n,'d') try: for iteration in range(iter): # fit xest for i in range(nest): weights = delta * west[:,i] b = Numeric.array([sum(weights*y), sum(weights*y*x)]) A = Numeric.array([[sum(weights), sum(weights*x)], [sum(weights*x), sum(weights*x*x)]]) beta = LinearAlgebra.solve_linear_equations(A,b) yest2[i] = beta[0] + beta[1]*xest[i] # fit x (to calculate residuals and delta) for i in range(n): weights = delta * w[:,i] b = Numeric.array([sum(weights*y), sum(weights*y*x)]) A = Numeric.array([[sum(weights), sum(weights*x)], [sum(weights*x), sum(weights*x*x)]]) beta = LinearAlgebra.solve_linear_equations(A,b) yest[i] = beta[0] + beta[1]*x[i] residuals = y-yest s = MLab.median(abs(residuals)) delta = Numeric.clip(residuals/(6*s),-1,1) delta = 1-delta*delta delta = delta*delta except LinearAlgebra.LinAlgError: print "Warning: NumExtn.lowess2: LinearAlgebra.solve_linear_equations: Singular matrix" yest2 = None return yest2
def processBuffer(self, bounds): self.param1 = bounds.height / 65536.0 self.param2 = bounds.height / 2.0 if (self.stop == False): Fs = 48000 nfft = 65536 self.newest_buffer = self.newest_buffer[0:256] self.fftx = fft(self.newest_buffer, 256, -1) self.fftx = self.fftx[0:self.freq_range * 2] self.draw_interval = bounds.width / (self.freq_range * 2.) NumUniquePts = ceil((nfft + 1) / 2) self.buffers = abs(self.fftx) * 0.02 self.y_mag_bias_multiplier = 0.1 self.scaleX = "hz" self.scaleY = "" if (len(self.buffers) == 0): return False # Scaling the values val = [] for i in self.buffers: temp_val_float = float( self.param1 * i * self.y_mag) + self.y_mag_bias_multiplier * self.param2 if (temp_val_float >= bounds.height): temp_val_float = bounds.height - 25 if (temp_val_float <= 0): temp_val_float = 25 val.append(temp_val_float) self.peaks = val
def lowessW(x, y, xest, f=2./3., iter=3, dWeights=None, callback=None): """Returns estimated values of y in data points xest (or None if estimation fails). Lowess smoother: Robust locally weighted regression. The lowess function fits a nonparametric regression curve to a scatterplot. The arrays x and y contain an equal number of elements; each pair (x[i], y[i]) defines a data point in the scatterplot. The function returns the estimated (smooth) values of y. The smoothing span is given by f. A larger value for f will result in a smoother curve. The number of robustifying iterations is given by iter. The function will run faster with a smaller number of iterations. Data points may be assigned weights; if None, all weights equal 1. """ x = Numeric.asarray(x, 'd') y = Numeric.asarray(y, 'd') xest = Numeric.asarray(xest, 'd') n = len(x) if n <> len(y): raise AttributeError, "Error: lowessW(x,y,xest,f,iter,dWeights): len(x)=%i not equal to len(y)=%i" % (len(x), len(y)) nest = len(xest) # weights of data points (optional) if dWeights <> None: dWeights = Numeric.asarray(dWeights, 'd') if len(dWeights) <> n: raise AttributeError, "Error: lowessW(x,y,xest,f,iter,dWeights): len(dWeights)=%i not equal to len(x)=%i" % (len(dWeights), len(x)) ## dWeights = dWeights.reshape((n,1)) else: ## dWeights = Numeric.ones((n,1)) dWeights = Numeric.ones((n,)) r = min(int(Numeric.ceil(f*n)),n-1) # radius: num. of points to take into LR h = [Numeric.sort(abs(x-x[i]))[r] for i in range(n)] # distance of the r-th point from x[i] w = Numeric.clip(abs(([x]-Numeric.transpose([x]))/h),0.0,1.0) w = 1-w*w*w w = w*w*w hest = [Numeric.sort(abs(x-xest[i]))[r] for i in range(nest)] # r-th min. distance from xest[i] to x west = Numeric.clip(abs(([xest]-Numeric.transpose([x]))/hest),0.0,1.0) # shape: (len(x), len(xest)) west = 1-west*west*west west = west*west*west yest = Numeric.zeros(n,'d') yest2 = Numeric.zeros(nest,'d') delta = Numeric.ones(n,'d') try: for iteration in range(int(iter)): # fit xest for i in range(nest): ## print delta.shape, west[:,i].shape, dWeights.shape weights = delta * west[:,i] * dWeights b = Numeric.array([sum(weights*y), sum(weights*y*x)]) A = Numeric.array([[sum(weights), sum(weights*x)], [sum(weights*x), sum(weights*x*x)]]) beta = LinearAlgebra.solve_linear_equations(A,b) yest2[i] = beta[0] + beta[1]*xest[i] # fit x (to calculate residuals and delta) for i in range(n): weights = delta * w[:,i] * dWeights b = Numeric.array([sum(weights*y), sum(weights*y*x)]) A = Numeric.array([[sum(weights), sum(weights*x)], [sum(weights*x), sum(weights*x*x)]]) beta = LinearAlgebra.solve_linear_equations(A,b) yest[i] = beta[0] + beta[1]*x[i] residuals = y-yest s = MLab.median(abs(residuals)) delta = Numeric.clip(residuals/(6*s),-1,1) delta = 1-delta*delta delta = delta*delta if callback: callback() except LinearAlgebra.LinAlgError: print "Warning: NumExtn.lowessW: LinearAlgebra.solve_linear_equations: Singular matrix" yest2 = None return yest2