Example #1
0
 def rtz(self, pt):
     d = pt - self.p0
     z = dot(d, self.zn)
     d = d - z * self.zn
     r = vlen(d)
     theta = Numeric.arctan2(dot(d, self.v), dot(d, self.u))
     return Numeric.array((r, theta, z), 'd')
Example #2
0
 def rtz(self, pt):
     d = pt - self.p0
     z = dot(d, self.zn)
     d = d - z * self.zn
     r = vlen(d)
     theta = Numeric.arctan2(dot(d, self.v), dot(d, self.u))
     return Numeric.array((r, theta, z), "d")
Example #3
0
    def arctan2(self, other):
        den = self.value*self.value+other.value*other.value
        s = self.value/den
        o = other.value/den
	return DerivVar(Numeric.arctan2(self.value, other.value),
			_mapderiv(lambda a,b: a-b,
				  map(lambda x,f=o: f*x, self.deriv),
				  map(lambda x,f=s: f*x, other.deriv)))
Example #4
0
    def update(self):
        self.position += self.velocity

        if self.speed > 0:
            ## get heading from angle of velocity
            angle = N.arctan2(-self.velocity[1], self.velocity[0])
            hindex = round(4.0*angle/N.pi)
            self._heading = {0:'e',1:'ne',2:'n',3:'nw',4:'w',
                             -1:'se',-2:'s',-3:'sw',-4:'w'}[hindex]

        anim = self.animations[self._state][self._heading]
        self._index += self.image_speed
        self._index %= self.nframes
        self.image = anim[self._index]
        self.rect = self.image.get_rect()
        self.rect.center = self.position
    def arctan2(self, other):
	if self.order < 2:
	    s1 = self.value
	else:
	    s1 = self.toOrder(self.order-1)
	if other.order < 2:
	    o1 = other.value
	else:
	    o1 = other.toOrder(other.order-1)
        den = s1*s1+o1*o1
        s1 = s1/den
        o1 = o1/den
	return DerivVar(Numeric.arctan2(self.value, other.value),
			_mapderiv(lambda a,b: a-b,
				  map(lambda x,f=o1: x*f, self.deriv),
				  map(lambda x,f=s1: x*f, other.deriv)),
				  max(self.order, other.order))
Example #6
0
 def arctan2(self, other):
     if self.order < 2:
         s1 = self.value
     else:
         s1 = self.toOrder(self.order - 1)
     if other.order < 2:
         o1 = other.value
     else:
         o1 = other.toOrder(other.order - 1)
     den = s1 * s1 + o1 * o1
     s1 = s1 / den
     o1 = o1 / den
     return DerivVar(
         Numeric.arctan2(self.value, other.value),
         _mapderiv(lambda a, b: a - b, map(lambda x, f=o1: x * f,
                                           self.deriv),
                   map(lambda x, f=s1: x * f, other.deriv)),
         max(self.order, other.order))
Example #7
0
 def getAngle(self):
     if self.speed > 0:
         return N.arctan2(-self.velocity[1], self.velocity[0])
     else:
         return heading2angle(self._heading)
Example #8
0
def angles(imagefilename):
    im=Image.open(imagefilename)
    #im.thumbnail((20,50))
    for i in range(0):
        im=im.filter(ImageFilter.SMOOTH)

    img=Numeric.reshape(Numeric.fromstring(im.convert("L").tostring(),
                                                Numeric.UnsignedInt8),
                             (im.size[1],im.size[0]))

    img=img.astype(Numeric.Complex16)


#    img_freq=FFT.fft2d(img)
    gsize=11
    sigma=1
    gauss=iagaussian([gsize,gsize],[gsize//2,gsize//2],
                     [[sigma,0],[0,sigma]]).astype(Numeric.Complex16)
    #print Numeric.array2string(gauss,precision=3,suppress_small=1)
#    kernel_freq=FFT.fft2d(gauss)

    sys.stdout.write("convolve...")
    sys.stdout.flush()
    img=iaconv(img,gauss)
    print "done"

#    img=FFT.inverse_fft2d(img_freq)

    # we can skip borders, which have blur errors anyhow
    margin=gsize
    img=img[margin:-1-margin,margin:-1-margin]

    view(img)

    ix=deriv(img,0)
    view(ix)

    iy=deriv(img,1)
    view(iy)

    x=ix-iy*1j
    angs=Numeric.fmod(-Numeric.arctan2(x.imag,x.real)+pi,pi)
    mags=abs(Numeric.sqrt(ix*ix+iy*iy))

    view(angs)
    
    histo={} # angle:freq
    degreesperbucket=.1
    buckets=180/degreesperbucket
    totalcounted=0
    for ang,mag in zip(Numeric.ravel(angs),Numeric.ravel(mags)):
        ang=int(ang*buckets)/buckets
        histo[ang]=histo.get(ang,0)+mag
        totalcounted+=mag

    keys=histo.keys()
    keys.sort()
    f=open("angles.data",'w')
    for k in keys:
        degrees=k*360/(2*pi)
        print >>f,"%.3f %f" % (degrees,histo[k])

        # remark on counts that are more than 1% of the total
        if histo[k]>totalcounted*.005:
            print "peak at",degrees
Example #9
0
def test1(*s):
    "Test of basic array creation and arithmetic."
    x=Numeric.array([1.,1.,1.,-2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
    y=Numeric.array([5.,0.,3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
    a10 = 10.
    m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
    m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0 ,0, 1]
    xm = array(x, mask=m1)
    ym = array(y, mask=m2)
    z = Numeric.array([-.5, 0., .5, .8])
    zm = array(z, mask=[0,1,0,0])
    xf = Numeric.where(m1, 1.e+20, x)
    xm.set_fill_value(1.e+20)
    for item in [x, y, xm, ym, xf]:
        item.shape = s

    assert isMaskedArray(x) == 0
    assert isMaskedArray(xm) == 1
    assert shape(xm) == s
    assert xm.shape == s
    assert size(xm) == reduce(lambda x,y:x*y, s)
    assert xm.size() == size(xm)
    assert size(xm,0) == s[0]
    assert xm.size(0) == size(xm,0)
    assert count(xm) == len(m1) - reduce(lambda x,y:x+y, m1)
    if rank(xm) > 1:
        assert count(xm,0) == size(xm,0) - reduce(lambda x,y:x+y, xm.mask()[0])
    alltest(xm, xf)
    alltest(filled(xm, 1.e20), xf)
    alltest(x, xm)
    alltest(-x, -xm)
    alltest(x + y, xm + ym)
    alltest(x - y, xm - ym)
    alltest(x * y, xm * ym)
    alltest(x / y, xm / ym)
    alltest(a10 + y, a10 + ym)
    alltest(a10 - y, a10 - ym)
    alltest(a10 * y, a10 * ym)
    alltest(a10 / y, a10 / ym)
    alltest(x + a10, xm + a10)
    alltest(x - a10, xm - a10)
    alltest(x * a10, xm * a10)
    alltest(x / a10, xm / a10)
    a2d = array([[1,2],[0,4]], Float)
    a2dm = masked_array(a2d, [[0,0],[1,0]])
    alltest (a2d * a2d, a2d * a2dm)
    alltest (a2d + a2d, a2d + a2dm)
    alltest (a2d - a2d, a2d - a2dm)
    alltest(x**2, xm**2)
    alltest(abs(x)**2.5, abs(xm) **2.5)
    alltest(x**y, xm**ym)
    alltest(Numeric.add(x,y), add(xm, ym))
    alltest(Numeric.subtract(x,y), subtract(xm, ym))
    alltest(Numeric.multiply(x,y), multiply(xm, ym))
    alltest(Numeric.divide(x,y), divide(xm, ym))
    alltest(Numeric.cos(x), cos(xm))
    alltest(Numeric.cosh(x), cosh(xm))
    alltest(Numeric.sin(x), sin(xm))
    alltest(Numeric.sinh(x), sinh(xm))
    alltest(Numeric.tan(x), tan(xm))
    alltest(Numeric.tanh(x), tanh(xm))
    alltest(Numeric.sqrt(abs(x)), sqrt(xm))
    alltest(Numeric.log(abs(x)), log(xm))
    alltest(Numeric.log10(abs(x)), log10(xm))
    alltest(Numeric.exp(x), exp(xm))
    alltest(Numeric.arcsin(z), arcsin(zm))
    alltest(Numeric.arccos(z), arccos(zm))
    alltest(Numeric.arctan(z), arctan(zm))
    alltest(Numeric.arctan2(x, y), arctan2(xm, ym))
    alltest(Numeric.absolute(x), absolute(xm))
    alltest(Numeric.equal(x,y), equal(xm, ym))
    alltest(Numeric.not_equal(x,y), not_equal(xm, ym))
    alltest(Numeric.less(x,y), less(xm, ym))
    alltest(Numeric.greater(x,y), greater(xm, ym))
    alltest(Numeric.less_equal(x,y), less_equal(xm, ym))
    alltest(Numeric.greater_equal(x,y), greater_equal(xm, ym))
    alltest(Numeric.conjugate(x), conjugate(xm))
    alltest(Numeric.concatenate((x,y)), concatenate((xm,ym)))
    alltest(Numeric.concatenate((x,y)), concatenate((x,y)))
    alltest(Numeric.concatenate((x,y)), concatenate((xm,y)))
    alltest(Numeric.concatenate((x,y,x)), concatenate((x,ym,x)))

    ott = array([0.,1.,2.,3.], mask=[1,0,0,0])
    assert isinstance(count(ott), types.IntType)
    alltest(3, count(ott))
    alltest(1, count(1))
    alltest(0, array(1,mask=[1]))
    ott.shape = (2,2)
    assert isMaskedArray(count(ott,0))
    assert isinstance(count(ott), types.IntType)
    alltest(3, count(ott))
    assert getmask(count(ott,0)) is None
    alltest([1,2],count(ott,0))

    xr = Numeric.ravel(x) #max doesn't work if shaped
    xmr = ravel(xm)
    alltest(max(xr), maximum(xmr)) #true because of careful selection of data
    alltest(min(xr), minimum(xmr)) #true because of careful selection of data
    alltest(Numeric.add.reduce(x), add.reduce(x))
    alltest(Numeric.add.accumulate(x), add.accumulate(x))
    alltest(4, sum(array(4)))
    alltest(4, sum(array(4), axis=0))
    alltest(Numeric.sum(x), sum(x))
    alltest(Numeric.sum(filled(xm,0)), sum(xm))
    alltest(Numeric.sum(x,0), sum(x,0))
    alltest(Numeric.product(x), product(x))
    alltest(Numeric.product(x,0), product(x,0))
    alltest(Numeric.product(filled(xm,1)), product(xm))
    if len(s) > 1:
        alltest(Numeric.concatenate((x,y),1), concatenate((xm,ym),1))
        alltest(Numeric.add.reduce(x,1), add.reduce(x,1))
        alltest(Numeric.sum(x,1), sum(x,1))
        alltest(Numeric.product(x,1), product(x,1))