def MaxInnerProd(ser1, ser2, PSD): size = Numeric.shape(ser1)[0] pdlen = size/2 nyquistf = 0.5/15.0 # !!! hardcoded !!!! freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen) if(Numeric.shape(ser2)[0] != size): print "size of time series must be the same" sys.exit(1) if(Numeric.shape(PSD)[0] != pdlen): print "wrong size of psd: ", pdlen, Numeric.shape(PSD) sys.exit(1) fourier1 = FFT.fft(ser1) fourier2 = FFT.fft(ser2) prod1 = Numeric.zeros(pdlen+1, dtype='d') prod2 = Numeric.zeros(pdlen+1, dtype='d') prod1[0] = 0.0 prod1[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen])) + numpy.multiply(fourier1[-1:pdlen:-1],numpy.conjugate(fourier2[-1:pdlen:-1])) prod1[pdlen] = fourier1[pdlen]*fourier2[pdlen] prod2[0] = 0.0 prod2[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen]*1.j)) + numpy.multiply((fourier1[-1:pdlen:-1]),numpy.conjugate(fourier2[-1:pdlen:-1]*(-1.j))) prod2[pdlen] = fourier1[pdlen]*fourier2[pdlen] Numeric.divide(prod1[1:], PSD, prod1[1:]) Numeric.divide(prod2[1:], PSD, prod2[1:]) olap0 = 0.0 olappiby2 = 0.0 for i in xrange(pdlen): if (freqs[i] > fLow and freqs[i]<= fHigh): olap0 += prod1[i] olappiby2 += prod2[i] olap0 = 2.0*olap0/float(size) olappiby2 = 2.0*olappiby2/float(size) # olap0 = 2.0*(numpy.sum(prod1[1:]))/float(size) #it must be scaled by dt # olappiby2 = 2.0*(numpy.sum(prod2[1:]))/float(size) #it must be scaled by dt print "angle of maxim. = ", math.atan(olappiby2/olap0) return sqrt(olap0**2 + olappiby2**2)
def ring(x, y, height, thickness, gaussian_width): """ Circular ring (annulus) with Gaussian fall-off after the solid ring-shaped region. """ radius = height / 2.0 half_thickness = thickness / 2.0 distance_from_origin = sqrt(x**2 + y**2) distance_outside_outer_disk = distance_from_origin - radius - half_thickness distance_inside_inner_disk = radius - half_thickness - distance_from_origin ring = 1.0 - bitwise_xor(greater_equal(distance_inside_inner_disk, 0.0), greater_equal(distance_outside_outer_disk, 0.0)) sigmasq = gaussian_width * gaussian_width if sigmasq == 0.0: inner_falloff = x * 0.0 outer_falloff = x * 0.0 else: with float_error_ignore(): inner_falloff = exp( divide( -distance_inside_inner_disk * distance_inside_inner_disk, 2.0 * sigmasq)) outer_falloff = exp( divide( -distance_outside_outer_disk * distance_outside_outer_disk, 2.0 * sigmasq)) return maximum(inner_falloff, maximum(outer_falloff, ring))
def InnerProd(ser1, ser2, PSD): size = Numeric.shape(ser1)[0] pdlen = size/2 nyquistf = 0.5/15.0 # !!! hardcoded !!!! freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen) if(Numeric.shape(ser2)[0] != size): print "size of time series must be the same" sys.exit(1) if(Numeric.shape(PSD)[0] != pdlen): print "wrong size of psd: ", pdlen, Numeric.shape(PSD) sys.exit(1) fourier1 = FFT.fft(ser1) fourier2 = FFT.fft(ser2) prod = Numeric.zeros(pdlen+1, dtype='d') prod[0] = 0.0 prod[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen])) + numpy.multiply(fourier1[-1:pdlen:-1],numpy.conjugate(fourier2[-1:pdlen:-1])) prod[pdlen] = fourier1[pdlen]*fourier2[pdlen] Numeric.divide(prod[1:], PSD, prod[1:]) olap0 = 0.0 for i in xrange(pdlen): if (freqs[i] > fLow and freqs[i]<= fHigh): olap0 += prod[i] olap0 = 2.0*olap0/float(size) # olap0 = 2.0*(numpy.sum(prod[1:]))/float(size) #it must be scaled by dt return olap0
def InnerProd(ser1, ser2, PSD): size = Numeric.shape(ser1)[0] pdlen = size/2 nyquistf = 0.5/15.0 # !!! hardcoded !!!! freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen) if(Numeric.shape(ser2)[0] != size): print "size of time series must be the same" sys.exit(1) if(Numeric.shape(PSD)[0] != pdlen): print "wrong size of psd: ", pdlen, Numeric.shape(PSD) sys.exit(1) fourier1 = FFT.fft(ser1) fourier2 = FFT.fft(ser2) prod = Numeric.zeros(pdlen+1, dtype='d') prod[0] = 0.0 prod[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen])) + numpy.multiply(fourier1[-1:pdlen:-1],numpy.conjugate(fourier2[-1:pdlen:-1])) prod[pdlen] = fourier1[pdlen]*fourier2[pdlen] Numeric.divide(prod[1:], PSD, prod[1:]) olap0 = 0.0 for i in xrange(pdlen): if (freqs[i] > 1.e-5 and freqs[i]<0.01): olap0 += prod[i] olap0 = 2.0*olap0/float(size) # olap0 = 2.0*(numpy.sum(prod[1:]))/float(size) #it must be scaled by dt return olap0
def MaxInnerProd(ser1, ser2, PSD): size = Numeric.shape(ser1)[0] pdlen = size/2 nyquistf = 0.5/15.0 # !!! hardcoded !!!! freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen) if(Numeric.shape(ser2)[0] != size): print "size of time series must be the same" sys.exit(1) if(Numeric.shape(PSD)[0] != pdlen): print "wrong size of psd: ", pdlen, Numeric.shape(PSD) sys.exit(1) fourier1 = FFT.fft(ser1) fourier2 = FFT.fft(ser2) prod1 = Numeric.zeros(pdlen+1, dtype='d') prod2 = Numeric.zeros(pdlen+1, dtype='d') prod1[0] = 0.0 prod1[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen])) + numpy.multiply(fourier1[-1:pdlen:-1],numpy.conjugate(fourier2[-1:pdlen:-1])) prod1[pdlen] = fourier1[pdlen]*fourier2[pdlen] prod2[0] = 0.0 prod2[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen]*1.j)) + numpy.multiply((fourier1[-1:pdlen:-1]),numpy.conjugate(fourier2[-1:pdlen:-1]*(-1.j))) prod2[pdlen] = fourier1[pdlen]*fourier2[pdlen] Numeric.divide(prod1[1:], PSD, prod1[1:]) Numeric.divide(prod2[1:], PSD, prod2[1:]) olap0 = 0.0 olappiby2 = 0.0 for i in xrange(pdlen): if (freqs[i] > 1.e-5 and freqs[i]<0.01): olap0 += prod1[i] olappiby2 += prod2[i] olap0 = 2.0*olap0/float(size) olappiby2 = 2.0*olappiby2/float(size) # olap0 = 2.0*(numpy.sum(prod1[1:]))/float(size) #it must be scaled by dt # olappiby2 = 2.0*(numpy.sum(prod2[1:]))/float(size) #it must be scaled by dt print "angle of maxim. = ", math.atan(olappiby2/olap0) return sqrt(olap0**2 + olappiby2**2)
def arc_by_radian(x, y, height, radian_range, thickness, gaussian_width): """ Radial arc with Gaussian fall-off after the solid ring-shaped region with the given thickness, with shape specified by the (start,end) radian_range. """ # Create a circular ring (copied from the ring function) radius = height/2.0 half_thickness = thickness/2.0 distance_from_origin = sqrt(x**2+y**2) distance_outside_outer_disk = distance_from_origin - radius - half_thickness distance_inside_inner_disk = radius - half_thickness - distance_from_origin ring = 1.0-bitwise_xor(greater_equal(distance_inside_inner_disk,0.0),greater_equal(distance_outside_outer_disk,0.0)) sigmasq = gaussian_width*gaussian_width if sigmasq==0.0: inner_falloff = x*0.0 outer_falloff = x*0.0 else: with float_error_ignore(): inner_falloff = exp(divide(-distance_inside_inner_disk*distance_inside_inner_disk, 2.0*sigmasq)) outer_falloff = exp(divide(-distance_outside_outer_disk*distance_outside_outer_disk, 2.0*sigmasq)) output_ring = maximum(inner_falloff,maximum(outer_falloff,ring)) # Calculate radians (in 4 phases) and cut according to the set range) # RZHACKALERT: # Function float_error_ignore() cannot catch the exception when # both dividend and divisor are 0.0, and when only divisor is 0.0 # it returns 'Inf' rather than 0.0. In x, y and # distance_from_origin, only one point in distance_from_origin can # be 0.0 (circle center) and in this point x and y must be 0.0 as # well. So here is a hack to avoid the 'invalid value encountered # in divide' error by turning 0.0 to 1e-5 in distance_from_origin. distance_from_origin += where(distance_from_origin == 0.0, 1e-5, 0) with float_error_ignore(): sines = divide(y, distance_from_origin) cosines = divide(x, distance_from_origin) arcsines = arcsin(sines) phase_1 = where(logical_and(sines >= 0, cosines >= 0), 2*pi-arcsines, 0) phase_2 = where(logical_and(sines >= 0, cosines < 0), pi+arcsines, 0) phase_3 = where(logical_and(sines < 0, cosines < 0), pi+arcsines, 0) phase_4 = where(logical_and(sines < 0, cosines >= 0), -arcsines, 0) arcsines = phase_1 + phase_2 + phase_3 + phase_4 if radian_range[0] <= radian_range[1]: return where(logical_and(arcsines >= radian_range[0], arcsines <= radian_range[1]), output_ring, 0.0) else: return where(logical_or(arcsines >= radian_range[0], arcsines <= radian_range[1]), output_ring, 0.0)
def exponential(x, y, xscale, yscale): """ Two-dimensional oriented exponential decay pattern. """ if xscale == 0.0 or yscale == 0.0: return x * 0.0 with float_error_ignore(): x_w = divide(x, xscale) y_h = divide(y, yscale) return exp(-sqrt(x_w * x_w + y_h * y_h))
def exponential(x, y, xscale, yscale): """ Two-dimensional oriented exponential decay pattern. """ if xscale==0.0 or yscale==0.0: return x*0.0 with float_error_ignore(): x_w = divide(x,xscale) y_h = divide(y,yscale) return exp(-sqrt(x_w*x_w+y_h*y_h))
def gabor(x, y, xsigma, ysigma, frequency, phase): """ Gabor pattern (sine grating multiplied by a circular Gaussian). """ if xsigma==0.0 or ysigma==0.0: return x*0.0 with float_error_ignore(): x_w = divide(x,xsigma) y_h = divide(y,ysigma) p = exp(-0.5*x_w*x_w + -0.5*y_h*y_h) return p * 0.5*cos(2*pi*frequency*y + phase)
def gabor(x, y, xsigma, ysigma, frequency, phase): """ Gabor pattern (sine grating multiplied by a circular Gaussian). """ if xsigma == 0.0 or ysigma == 0.0: return x * 0.0 with float_error_ignore(): x_w = divide(x, xsigma) y_h = divide(y, ysigma) p = exp(-0.5 * x_w * x_w + -0.5 * y_h * y_h) return p * 0.5 * cos(2 * pi * frequency * y + phase)
def gaussian(x, y, xsigma, ysigma): """ Two-dimensional oriented Gaussian pattern (i.e., 2D version of a bell curve, like a normal distribution but not necessarily summing to 1.0). """ if xsigma == 0.0 or ysigma == 0.0: return x * 0.0 with float_error_ignore(): x_w = divide(x, xsigma) y_h = divide(y, ysigma) return exp(-0.5 * x_w * x_w + -0.5 * y_h * y_h)
def gaussian(x, y, xsigma, ysigma): """ Two-dimensional oriented Gaussian pattern (i.e., 2D version of a bell curve, like a normal distribution but not necessarily summing to 1.0). """ if xsigma==0.0 or ysigma==0.0: return x*0.0 with float_error_ignore(): x_w = divide(x,xsigma) y_h = divide(y,ysigma) return exp(-0.5*x_w*x_w + -0.5*y_h*y_h)
def log_gaussian(x, y, x_sigma, y_sigma, mu): """ Two-dimensional oriented Log Gaussian pattern (i.e., 2D version of a bell curve with an independent, movable peak). Much like a normal distribution, but not necessarily placing the peak above the center, and not necessarily summing to 1.0). """ if x_sigma==0.0 or y_sigma==0.0: return x * 0.0 with float_error_ignore(): x_w = divide(log(x)-mu, x_sigma*x_sigma) y_h = divide(log(y)-mu, y_sigma*y_sigma) return exp(-0.5*x_w*x_w + -0.5*y_h*y_h)
def log_gaussian(x, y, x_sigma, y_sigma, mu): """ Two-dimensional oriented Log Gaussian pattern (i.e., 2D version of a bell curve with an independent, movable peak). Much like a normal distribution, but not necessarily placing the peak above the center, and not necessarily summing to 1.0). """ if x_sigma == 0.0 or y_sigma == 0.0: return x * 0.0 with float_error_ignore(): x_w = divide(log(x) - mu, x_sigma * x_sigma) y_h = divide(log(y) - mu, y_sigma * y_sigma) return exp(-0.5 * x_w * x_w + -0.5 * y_h * y_h)
def concentricrings(x, y, white_thickness, gaussian_width, spacing): """ Concetric rings with the solid ring-shaped region, then Gaussian fall-off at the edges. """ # To have zero value in middle point this pattern calculates zero-value rings instead of # the one-value ones. But to be consistent with the rest of functions the parameters # are connected to one-value rings - like half_thickness is now recalculated for zero-value ring: half_thickness = ((spacing-white_thickness)/2.0)*greater_equal(spacing-white_thickness,0.0) distance_from_origin = sqrt(x**2+y**2) distance_from_ring_middle = fmod(distance_from_origin,spacing) distance_from_ring_middle = minimum(distance_from_ring_middle,spacing - distance_from_ring_middle) distance_from_ring = distance_from_ring_middle - half_thickness ring = 0.0 + greater_equal(distance_from_ring,0.0) sigmasq = gaussian_width*gaussian_width with float_error_ignore(): falloff = exp(divide(-distance_from_ring*distance_from_ring, 2.0*sigmasq)) return maximum(falloff,ring)
def function(self,params): """Hyperbolic function.""" aspect_ratio = params['aspect_ratio'] x = self.pattern_x/aspect_ratio y = self.pattern_y thickness = params['thickness'] gaussian_width = params['smoothing'] size = params['size'] half_thickness = thickness / 2.0 distance_from_vertex_middle = fmod(sqrt(absolute(x**2 - y**2)),size) distance_from_vertex_middle = minimum(distance_from_vertex_middle,size - distance_from_vertex_middle) distance_from_vertex = distance_from_vertex_middle - half_thickness hyperbola = 1.0 - greater_equal(distance_from_vertex,0.0) sigmasq = gaussian_width*gaussian_width with float_error_ignore(): falloff = exp(divide(-distance_from_vertex*distance_from_vertex, 2.0*sigmasq)) return maximum(falloff, hyperbola)
def function(self,params): """Concentric rings.""" aspect_ratio = params['aspect_ratio'] x = self.pattern_x/aspect_ratio y = self.pattern_y thickness = params['thickness'] gaussian_width = params['smoothing'] size = params['size'] half_thickness = thickness / 2.0 distance_from_origin = sqrt(x**2+y**2) distance_from_ring_middle = fmod(distance_from_origin,size) distance_from_ring_middle = minimum(distance_from_ring_middle,size - distance_from_ring_middle) distance_from_ring = distance_from_ring_middle - half_thickness ring = 1.0 - greater_equal(distance_from_ring,0.0) sigmasq = gaussian_width*gaussian_width with float_error_ignore(): falloff = exp(divide(-distance_from_ring*distance_from_ring, 2.0*sigmasq)) return maximum(falloff, ring)
def function(self,params): """Archemidean spiral function.""" aspect_ratio = params['aspect_ratio'] x = self.pattern_x/aspect_ratio y = self.pattern_y thickness = params['thickness'] gaussian_width = params['smoothing'] size = params['size'] half_thickness = thickness/2.0 spacing = size*2*pi distance_from_origin = sqrt(x**2+y**2) distance_from_spiral_middle = fmod(spacing + distance_from_origin - size*arctan2(y,x),spacing) distance_from_spiral_middle = minimum(distance_from_spiral_middle,spacing - distance_from_spiral_middle) distance_from_spiral = distance_from_spiral_middle - half_thickness spiral = 1.0 - greater_equal(distance_from_spiral,0.0) sigmasq = gaussian_width*gaussian_width with float_error_ignore(): falloff = exp(divide(-distance_from_spiral*distance_from_spiral, 2.0*sigmasq)) return maximum(falloff, spiral)
def hyperbola(x, y, thickness, gaussian_width, axis): """ Two conjugate hyperbolas with Gaussian fall-off which share the same asymptotes. abs(x^2/a^2 - y^2/b^2) = 1 As a = b = axis, these hyperbolas are rectangular. """ difference = absolute(x**2 - y**2) hyperbola = 1.0 - bitwise_xor(greater_equal(axis**2,difference),greater_equal(difference,(axis + thickness)**2)) distance_inside_hyperbola = sqrt(difference) - axis distance_outside_hyperbola = sqrt(difference) - axis - thickness sigmasq = gaussian_width*gaussian_width with float_error_ignore(): inner_falloff = exp(divide(-distance_inside_hyperbola*distance_inside_hyperbola, 2.0*sigmasq)) outer_falloff = exp(divide(-distance_outside_hyperbola*distance_outside_hyperbola, 2.0*sigmasq)) return maximum(hyperbola,maximum(inner_falloff,outer_falloff))
def smooth_rectangle(x, y, rec_w, rec_h, gaussian_width_x, gaussian_width_y): """ Rectangle with a solid central region, then Gaussian fall-off at the edges. """ gaussian_x_coord = abs(x)-rec_w/2.0 gaussian_y_coord = abs(y)-rec_h/2.0 box_x=less(gaussian_x_coord,0.0) box_y=less(gaussian_y_coord,0.0) sigmasq_x=gaussian_width_x*gaussian_width_x sigmasq_y=gaussian_width_y*gaussian_width_y with float_error_ignore(): falloff_x=x*0.0 if sigmasq_x==0.0 else \ exp(divide(-gaussian_x_coord*gaussian_x_coord,2*sigmasq_x)) falloff_y=y*0.0 if sigmasq_y==0.0 else \ exp(divide(-gaussian_y_coord*gaussian_y_coord,2*sigmasq_y)) return minimum(maximum(box_x,falloff_x), maximum(box_y,falloff_y))
def smooth_rectangle(x, y, rec_w, rec_h, gaussian_width_x, gaussian_width_y): """ Rectangle with a solid central region, then Gaussian fall-off at the edges. """ gaussian_x_coord = abs(x) - rec_w / 2.0 gaussian_y_coord = abs(y) - rec_h / 2.0 box_x = less(gaussian_x_coord, 0.0) box_y = less(gaussian_y_coord, 0.0) sigmasq_x = gaussian_width_x * gaussian_width_x sigmasq_y = gaussian_width_y * gaussian_width_y with float_error_ignore(): falloff_x=x*0.0 if sigmasq_x==0.0 else \ exp(divide(-gaussian_x_coord*gaussian_x_coord,2*sigmasq_x)) falloff_y=y*0.0 if sigmasq_y==0.0 else \ exp(divide(-gaussian_y_coord*gaussian_y_coord,2*sigmasq_y)) return minimum(maximum(box_x, falloff_x), maximum(box_y, falloff_y))
def ring(x, y, height, thickness, gaussian_width): """ Circular ring (annulus) with Gaussian fall-off after the solid ring-shaped region. """ radius = height/2.0 half_thickness = thickness/2.0 distance_from_origin = sqrt(x**2+y**2) distance_outside_outer_disk = distance_from_origin - radius - half_thickness distance_inside_inner_disk = radius - half_thickness - distance_from_origin ring = 1.0-bitwise_xor(greater_equal(distance_inside_inner_disk,0.0),greater_equal(distance_outside_outer_disk,0.0)) sigmasq = gaussian_width*gaussian_width if sigmasq==0.0: inner_falloff = x*0.0 outer_falloff = x*0.0 else: with float_error_ignore(): inner_falloff = exp(divide(-distance_inside_inner_disk*distance_inside_inner_disk, 2.0*sigmasq)) outer_falloff = exp(divide(-distance_outside_outer_disk*distance_outside_outer_disk, 2.0*sigmasq)) return maximum(inner_falloff,maximum(outer_falloff,ring))
def line(y, thickness, gaussian_width): """ Infinite-length line with a solid central region, then Gaussian fall-off at the edges. """ distance_from_line = abs(y) gaussian_y_coord = distance_from_line - thickness/2.0 sigmasq = gaussian_width*gaussian_width if sigmasq==0.0: falloff = y*0.0 else: with float_error_ignore(): falloff = exp(divide(-gaussian_y_coord*gaussian_y_coord,2*sigmasq)) return where(gaussian_y_coord<=0, 1.0, falloff)
def line(y, thickness, gaussian_width): """ Infinite-length line with a solid central region, then Gaussian fall-off at the edges. """ distance_from_line = abs(y) gaussian_y_coord = distance_from_line - thickness/2.0 sigmasq = gaussian_width*gaussian_width if sigmasq==0.0: falloff = x*0.0 else: with float_error_ignore(): falloff = exp(divide(-gaussian_y_coord*gaussian_y_coord,2*sigmasq)) return where(gaussian_y_coord<=0, 1.0, falloff)
def disk(x, y, height, gaussian_width): """ Circular disk with Gaussian fall-off after the solid central region. """ disk_radius = height/2.0 distance_from_origin = sqrt(x**2+y**2) distance_outside_disk = distance_from_origin - disk_radius sigmasq = gaussian_width*gaussian_width if sigmasq==0.0: falloff = x*0.0 else: with float_error_ignore(): falloff = exp(divide(-distance_outside_disk*distance_outside_disk, 2*sigmasq)) return where(distance_outside_disk<=0,1.0,falloff)
def radial(x, y, wide, gaussian_width): """ Radial grating - A sector of a circle with Gaussian fall-off. Parameter wide determines in wide of sector in radians. """ angle = absolute(arctan2(y,x)) half_wide = wide/2 radius = 1.0 - greater_equal(angle,half_wide) distance = angle - half_wide sigmasq = gaussian_width*gaussian_width with float_error_ignore(): falloff = exp(divide(-distance*distance, 2.0*sigmasq)) return maximum(radius,falloff)
def _normalize(self,a,range_): """ Normalize an array s to be in the range 0 to 1.0. For an array of identical elements, returns an array of ones if the elements are greater than zero, and zeros if the elements are less than or equal to zero. """ if range_: # i.e. not False, not None (expecting a tuple) range_min = float(range_[0]) range_max = float(range_[1]) if range_min==range_max: if range_min>0: resu = ones(a.shape) else: resu = zeros(a.shape) else: a_offset = a - range_min resu = a_offset/(range_max-range_min) return resu else: if range_ is None: if not hasattr(self,'value_range'): self.value_range=(a.min(),a.max()) else: # If normalizing multiple matrices, take the largest values self.value_range=(min(self.value_range[0],a.min()), max(self.value_range[1],a.max())) return None # (indicate that array was not scaled) else: # i.e. range_ is False a_offset = a-a.min() max_a_offset = a_offset.max() if max_a_offset>0: a = divide(a_offset,float(max_a_offset)) else: if min(a.ravel())<=0: a=zeros(a.shape,Float) else: a=ones(a.shape,Float) return a
def _normalize(self, a, range_): """ Normalize an array s to be in the range 0 to 1.0. For an array of identical elements, returns an array of ones if the elements are greater than zero, and zeros if the elements are less than or equal to zero. """ if range_: # i.e. not False, not None (expecting a tuple) range_min = float(range_[0]) range_max = float(range_[1]) if range_min == range_max: if range_min > 0: resu = ones(a.shape) else: resu = zeros(a.shape) else: a_offset = a - range_min resu = a_offset / (range_max - range_min) return resu else: if range_ is None: if not hasattr(self, 'value_range'): self.value_range = (a.min(), a.max()) else: # If normalizing multiple matrices, take the largest values self.value_range = (min(self.value_range[0], a.min()), max(self.value_range[1], a.max())) return None # (indicate that array was not scaled) else: # i.e. range_ is False a_offset = a - a.min() max_a_offset = a_offset.max() if max_a_offset > 0: a = divide(a_offset, float(max_a_offset)) else: if min(a.ravel()) <= 0: a = zeros(a.shape, Float) else: a = ones(a.shape, Float) return a
def function(self,params): """Radial function.""" aspect_ratio = params['aspect_ratio'] x = self.pattern_x/aspect_ratio y = self.pattern_y gaussian_width = params['smoothing'] angle = absolute(arctan2(y,x)) half_length = params['arc_length']/2 radius = 1.0 - greater_equal(angle,half_length) distance = angle - half_length sigmasq = gaussian_width*gaussian_width with float_error_ignore(): falloff = exp(divide(-distance*distance, 2.0*sigmasq)) return maximum(radius, falloff)
def spiral(x, y, thickness, gaussian_width, density): """ Archemidean spiral with Gaussian fall-off outside the spiral curve. """ half_thickness = thickness/2.0 spacing = density*2*pi distance_from_origin = sqrt(x**2+y**2) distance_from_spiral_middle = fmod(spacing + distance_from_origin - density*arctan2(y,x),spacing) distance_from_spiral_middle = minimum(distance_from_spiral_middle,spacing - distance_from_spiral_middle) distance_from_spiral = distance_from_spiral_middle - half_thickness spiral = 1.0 - greater_equal(distance_from_spiral,0.0) sigmasq = gaussian_width*gaussian_width with float_error_ignore(): falloff = exp(divide(-distance_from_spiral*distance_from_spiral, 2.0*sigmasq)) return maximum(falloff,spiral)
def __div__(self, other): if isVector(other): raise TypeError, "Can't divide by a vector" else: return Vector(Numeric.divide(self.array, 1. * other))
def normal(self): "Returns a normalized copy." len = Numeric.sqrt(Numeric.add.reduce(self.array * self.array)) if len == 0: raise ZeroDivisionError, "Can't normalize a zero-length vector" return Vector(Numeric.divide(self.array, len))
def __div__(self, other): if isVector(other): raise TypeError, "Can't divide by a vector" else: return Vector(Numeric.divide(self.array,1.*other))
def normal(self): "Returns a normalized copy." len = Numeric.sqrt(Numeric.add.reduce(self.array*self.array)) if len == 0: raise ZeroDivisionError, "Can't normalize a zero-length vector" return Vector(Numeric.divide(self.array, len))