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 _ticks(self, lower, upper): ideal = (upper-lower)/7. log = _Numeric.log10(ideal) power = _Numeric.floor(log) fraction = log-power factor = 1. error = fraction for f, lf in self._multiples: e = _Numeric.fabs(fraction-lf) if e < error: error = e factor = f grid = factor * 10.**power if power > 4 or power < -4: format = '%+7.1e' elif power >= 0: digits = max(1, int(power)) format = '%' + `digits`+'.0f' else: digits = -int(power) format = '%'+`digits+2`+'.'+`digits`+'f' ticks = [] t = -grid*_Numeric.floor(-lower/grid) while t <= upper: ticks.append( (t, format % (t,)) ) t = t + grid return ticks
def _quantize(self, vec): """ Given a point in space, partition space into little cubes so that when the time comes, it will be quick to locate and search the cubes right around a point. """ maxradius = self._maxradius return (int(floor(vec[0] / maxradius)), int(floor(vec[1] / maxradius)), int(floor(vec[2] / maxradius)))
def _axisInterval(self, spec, lower, upper): """Returns sensible axis range for given spec""" if spec == 'none' or spec == 'min': if lower == upper: return lower-0.5, upper+0.5 else: return lower, upper elif spec == 'auto': range = upper-lower if range == 0.: return lower-0.5, upper+0.5 log = _Numeric.log10(range) power = _Numeric.floor(log) fraction = log-power if fraction <= 0.05: power = power-1 grid = 10.**power lower = lower - lower % grid mod = upper % grid if mod != 0: upper = upper - mod + grid return lower, upper elif type(spec) == type(()): lower, upper = spec if lower <= upper: return lower, upper else: return upper, lower else: raise ValueError, str(spec) + ': illegal axis specification'
def histogram(data, nbins, range=None): """ Create a histogram. Comes from Konrad Hinsen: Scientific Python @param data: data list or array @type data: [any] @param nbins: number of bins @type nbins: int @param range: data range to create histogram from (min val, max val) @type range: (float, float) OR None @return: array (2 x len(data) ) with start of bin and witdh of bin. @rtype: array """ data = Numeric.array(data, Numeric.Float) if range is None: min = Numeric.minimum.reduce(data) max = Numeric.maximum.reduce(data) else: min, max = range data = Numeric.repeat( data, Numeric.logical_and(Numeric.less_equal(data, max), Numeric.greater_equal(data, min))) bin_width = (max - min) / nbins data = Numeric.floor((data - min) / bin_width).astype(Numeric.Int) histo = Numeric.add.reduce( Numeric.equal(Numeric.arange(nbins)[:, Numeric.NewAxis], data), -1) histo[-1] = histo[-1] + Numeric.add.reduce(Numeric.equal(nbins, data)) bins = min + bin_width * (Numeric.arange(nbins) + 0.5) return Numeric.transpose(Numeric.array([bins, histo]))
def histogram(data, nbins, range=None): """ Comes from Konrad Hinsen: Scientific Python """ data = Numeric.array(data, Numeric.Float) if range is None: min = Numeric.minimum.reduce(data) max = Numeric.maximum.reduce(data) else: min, max = range data = Numeric.repeat( data, Numeric.logical_and(Numeric.less_equal(data, max), Numeric.greater_equal(data, min))) # end if bin_width = (max - min) / nbins data = Numeric.floor((data - min) / bin_width).astype(Numeric.Int) histo = Numeric.add.reduce( Numeric.equal(Numeric.arange(nbins)[:, Numeric.NewAxis], data), -1) histo[-1] = histo[-1] + Numeric.add.reduce(Numeric.equal(nbins, data)) bins = min + bin_width * (Numeric.arange(nbins) + 0.5) return Numeric.transpose(Numeric.array([bins, histo]))
def _arrayToImage(self, inArray): """ Generate a 1-channel PIL Image from an array of values from 0 to 1.0. Values larger than 1.0 are clipped, after adding them to the total clipped_pixels. Returns a one-channel (monochrome) Image. """ # PIL 'L' Images use a range of 0 to 255, so we scale the # input array to match. The pixels are scaled by 255, not # 256, so that 1.0 maps to fully white. max_pixel_value=255 inArray = (Numeric.floor(inArray * max_pixel_value)).astype(Numeric.Int) # Clip any values that are still larger than max_pixel_value to_clip = (Numeric.greater(inArray.ravel(),max_pixel_value)).sum() if (to_clip>0): # CEBALERT: no explanation of why clipped pixel count is # being accumulated. self.clipped_pixels = self.clipped_pixels + to_clip inArray.clip(0,max_pixel_value,out=inArray) self.verbose("Bitmap: clipped",to_clip,"image pixels that were out of range") r,c = inArray.shape # The size is (width,height), so we swap r and c: newImage = Image.new('L',(c,r),None) newImage.putdata(inArray.ravel()) return newImage
def histogram(data, nbins, range = None): """ Create a histogram. Comes from Konrad Hinsen: Scientific Python @param data: data list or array @type data: [any] @param nbins: number of bins @type nbins: int @param range: data range to create histogram from (min val, max val) @type range: (float, float) OR None @return: array (2 x len(data) ) with start of bin and witdh of bin. @rtype: array """ data = Numeric.array(data, Numeric.Float) if range is None: min = Numeric.minimum.reduce(data) max = Numeric.maximum.reduce(data) else: min, max = range data = Numeric.repeat(data, Numeric.logical_and(Numeric.less_equal(data, max), Numeric.greater_equal(data, min))) bin_width = (max-min)/nbins data = Numeric.floor((data - min)/bin_width).astype(Numeric.Int) histo = Numeric.add.reduce(Numeric.equal( Numeric.arange(nbins)[:,Numeric.NewAxis], data), -1) histo[-1] = histo[-1] + Numeric.add.reduce(Numeric.equal(nbins, data)) bins = min + bin_width*(Numeric.arange(nbins)+0.5) return Numeric.transpose(Numeric.array([bins, histo]))
def histogram(data, nbins, range = None): """ Comes from Konrad Hinsen: Scientific Python """ data = Numeric.array(data, Numeric.Float) if range is None: min = Numeric.minimum.reduce(data) max = Numeric.maximum.reduce(data) else: min, max = range data = Numeric.repeat(data, Numeric.logical_and(Numeric.less_equal(data, max), Numeric.greater_equal(data, min))) # end if bin_width = (max-min)/nbins data = Numeric.floor((data - min)/bin_width).astype(Numeric.Int) histo = Numeric.add.reduce(Numeric.equal( Numeric.arange(nbins)[:,Numeric.NewAxis], data), -1) histo[-1] = histo[-1] + Numeric.add.reduce(Numeric.equal(nbins, data)) bins = min + bin_width*(Numeric.arange(nbins)+0.5) return Numeric.transpose(Numeric.array([bins, histo]))
def _arrayToImage(self, inArray): """ Generate a 1-channel PIL Image from an array of values from 0 to 1.0. Values larger than 1.0 are clipped, after adding them to the total clipped_pixels. Returns a one-channel (monochrome) Image. """ # PIL 'L' Images use a range of 0 to 255, so we scale the # input array to match. The pixels are scaled by 255, not # 256, so that 1.0 maps to fully white. max_pixel_value = 255 inArray = (Numeric.floor(inArray * max_pixel_value)).astype( Numeric.Int) # Clip any values that are still larger than max_pixel_value to_clip = (Numeric.greater(inArray.ravel(), max_pixel_value)).sum() if (to_clip > 0): # CEBALERT: no explanation of why clipped pixel count is # being accumulated. self.clipped_pixels = self.clipped_pixels + to_clip inArray.clip(0, max_pixel_value, out=inArray) self.verbose("Bitmap: clipped", to_clip, "image pixels that were out of range") r, c = inArray.shape # The size is (width,height), so we swap r and c: newImage = Image.new('L', (c, r), None) newImage.putdata(inArray.ravel()) return newImage
def __call__(self,**params_to_override): p = ParamOverrides(self,params_to_override) xsize,ysize = SheetCoordinateSystem(p.bounds,p.xdensity,p.ydensity).shape xsize,ysize = int(round(xsize)),int(round(ysize)) xdisparity = int(round(xsize*p.xdisparity)) ydisparity = int(round(xsize*p.ydisparity)) dotsize = int(round(xsize*p.dotsize)) bigxsize = 2*xsize bigysize = 2*ysize ndots=int(round(p.dotdensity * (bigxsize+2*dotsize) * (bigysize+2*dotsize) / min(dotsize,xsize) / min(dotsize,ysize))) halfdot = floor(dotsize/2) # Choose random colors and locations of square dots random_seed = p.random_seed random_array.seed(random_seed*12,random_seed*99) col=where(random_array.random((ndots))>=0.5, 1.0, -1.0) random_array.seed(random_seed*122,random_seed*799) xpos=floor(random_array.random((ndots))*(bigxsize+2*dotsize)) - halfdot random_array.seed(random_seed*1243,random_seed*9349) ypos=floor(random_array.random((ndots))*(bigysize+2*dotsize)) - halfdot # Construct arrays of points specifying the boundaries of each # dot, cropping them by the big image size (0,0) to (bigxsize,bigysize) x1=xpos.astype(Int) ; x1=choose(less(x1,0),(x1,0)) y1=ypos.astype(Int) ; y1=choose(less(y1,0),(y1,0)) x2=(xpos+(dotsize-1)).astype(Int) ; x2=choose(greater(x2,bigxsize),(x2,bigxsize)) y2=(ypos+(dotsize-1)).astype(Int) ; y2=choose(greater(y2,bigysize),(y2,bigysize)) # Draw each dot in the big image, on a blank background bigimage = zeros((bigysize,bigxsize)) for i in range(ndots): bigimage[y1[i]:y2[i]+1,x1[i]:x2[i]+1] = col[i] result = p.offset + p.scale*bigimage[ (ysize/2)+ydisparity:(3*ysize/2)+ydisparity , (xsize/2)+xdisparity:(3*xsize/2)+xdisparity ] for of in p.output_fns: of(result) return result
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 _hashAtomPos(self, pos): return int(dot(V(1000000, 1000, 1), floor(pos * 1.2)))
def inc(t): return 10**int(_Numeric.floor(_Numeric.log10(t)+1e-16))