Пример #1
0
def binitbinsfix(binsleft, binsright, x, y):  #give bins for binning
    nbin = len(binsleft)
    xbin = N.zeros(len(binsleft), 'f')
    ybin = N.zeros(len(binsleft), 'f')
    ybinerr = N.zeros(len(binsleft), 'f')
    y = N.take(y, N.argsort(x))  #sort y according to x rankings
    x = N.take(x, N.argsort(x))

    j = -1
    for i in range(len(xbin)):
        xmin = binsleft[i]
        xmax = binsright[i]
        yb = []
        for j in range(len(x)):
            if x[j] > xmin:
                yb.append(y[j])
            if x[j] > xmax:
                yb = N.array(yb, 'f')
                xbin[i] = 0.5 * (xmin + xmax)
                try:
                    ybin[i] = pylab.median(yb)
                    ybinerr[i] = pylab.std(yb) / N.sqrt(1. * len(yb))
                except ZeroDivisionError:
                    print "warning: ZeroDivision error in binitbinsfix"
                    ybin[i] = 0.
                    ybinerr[i] = 0.

                break

    return xbin, ybin, ybinerr
Пример #2
0
 def getnearest(self,j):
     self.sig5=N.zeros(len(self.ra),'f')
     self.sig10=N.zeros(len(self.ra),'f')
     self.sig5phot=N.zeros(len(self.ra),'f')
     self.sig10phot=N.zeros(len(self.ra),'f')
     self.nearest=N.zeros(len(self.ra),'f')#distance to nearest neighbor
     self.dmagnearest=N.zeros(len(self.ra),'f')#mag diff b/w spec obj and nearest
     for i in range(len(self.ra)):
         angdist=my.DA(c.z[j],h100)#kpc/arcsec
         if self.memb[i] > 0:
             dspec=N.sqrt((self.ra[i]-self.ra)**2+(self.dec[i]-self.dec)**2)#sorted array of distances in degrees
             dphot=N.sqrt((self.ra[i]-self.photra)**2+(self.dec[i]-self.photdec)**2)#sorted array of distances in degrees
             Mvsort=N.take(self.V,N.argsort(dspec))
             phMvsort=N.take(self.phMv,N.argsort(dphot))
             dspecsort=N.take(dspec,N.argsort(dspec))
             self.sig5[i]=5./(N.pi)/(dspecsort[5]*3600.*angdist/1000.)**2#convert from deg to arcsec, multiply by DA (kpc/arcsec), divide by 1000 to convert to Mpc, index 5 element b/c 0 is itself
             if (len(dspecsort) > 10):
                 self.sig10[i]=10./(N.pi)/(dspecsort[10]*3600.*angdist/1000.)**2
             else:
                 self.sig10[i]=0.
             dphotsort=N.take(dphot,N.argsort(dphot))
             self.nearest[i]=dphotsort[0]
             self.dmagnearest[i]=phMvsort[0]-Mvsort[0]
             self.sig5phot[i]=5./(N.pi)/(dphotsort[5]*3600.*angdist/1000.)**2
             self.sig10phot[i]=10./(N.pi)/(dphotsort[10]*3600.*angdist/1000.)**2
Пример #3
0
def sortwindex(
    x
):  #sort array, return sorted array and array containing indices for unsorted array
    nx = len(x)
    y = N.arange(0, nx, 1)  #index of array x
    y = N.take(y, N.argsort(x))  #sort y according to x rankings
    xs = N.take(x, N.argsort(x))
    return xs, y  #xs=sorted array, y=indices in unsorted array
Пример #4
0
def binit(x, y, n):  #bin arrays x, y into n bins, returning xbin,ybin
    nx = len(x)
    y = N.take(y, N.argsort(x))  #sort y according to x rankings
    x = N.take(x, N.argsort(x))
    xbin = N.zeros(n, 'f')
    ybin = N.zeros(n, 'f')
    for i in range(n):
        nmin = i * int(float(nx) / float(n))
        nmax = (i + 1) * int(float(nx) / float(n))
        xbin[i] = pylab.median(x[nmin:nmax])
        ybin[i] = pylab.median(y[nmin:nmax])
        #xbin[i]=N.average(x[nmin:nmax])
        #ybin[i]=N.average(y[nmin:nmax])
        #ybinerr[i]=scipy.stats.std(y[nmin:nmax])
    return xbin, ybin  #, ybinerr
Пример #5
0
def horizontalhist(x, xmin, xmax, nbin):  #give bins for binning
    #nbin=len(bins)
    xbin = N.zeros(nbin, 'f')
    ybin = N.zeros(nbin, 'f')
    ybinerr = N.zeros(nbin, 'f')
    dbin = (xmax - xmin) / (1. * nbin)
    bins = N.arange(xmin, (xmax + dbin), dbin)
    x = N.take(x, N.argsort(x))
    xmin = min(bins)
    xmax = max(bins)
    xbinnumb = ((x - xmin) * nbin / (xmax - xmin)
                )  #calculate x  bin number for each point
    #print "from within horizontal hist"
    #print "bins = ",bins
    for i in range(len(xbin) - 1):
        xmin = bins[i]
        xmax = bins[i + 1]
        xbin[i] = xmin
        yb = 0.
        for j in range(len(x)):
            if (x[j] > xmin) & (x[j] <= xmax):
                yb = yb + 1.
        ybin[i] = yb
        ybinerr[i] = N.sqrt(yb)
        #print i,len(xbin),xbin[i],xmin,xmax,ybin[i],bins[i]
    xbin[(len(xbin) - 1)] = xbin[(len(xbin) - 2)] + dbin
    #xbin=bins
    #print "from w/in horizontal hist, ybin = ",ybin
    return xbin, ybin, ybinerr
Пример #6
0
def verticalhist(y, ymin, ymax, nbin):  #give bins for binning
    #nbin=len(bins)
    xbin = N.zeros(nbin, 'f')
    ybin = N.zeros(nbin, 'f')
    ybinerr = N.zeros(nbin, 'f')
    dbin = (ymax - ymin) / (1. * nbin)
    bins = N.arange(ymin, (ymax + dbin), dbin)
    y = N.take(y, N.argsort(y))
    xmin = min(bins)
    xmax = max(bins)
    xbinnumb = ((y - xmin) * nbin / (xmax - xmin)
                )  #calculate x  bin number for each point
    for i in range(len(xbin) - 1):
        xmin = bins[i]
        xmax = bins[i + 1]
        yb = 0.
        for j in range(len(y)):
            if y[j] > xmin:
                yb = yb + 1.
            if y[j] > xmax:
                ybin[i] = yb
                ybinerr[i] = N.sqrt(yb)
                #xbin[i]=0.5*(xmin+xmax)
                xbin[i] = xmax
                break
    drawhist(ybin, xbin)
Пример #7
0
def scipyhist2(bins, y):  #give bins for binning
    nbin = len(bins)
    xbin = N.zeros(len(bins), 'f')
    ybin = N.zeros(len(bins), 'f')
    ybinerr = N.zeros(len(bins), 'f')
    y = N.take(y, N.argsort(y))
    xmin = min(bins)
    xmax = max(bins)
    xbinnumb = ((y - xmin) * nbin / (xmax - xmin)
                )  #calculate x  bin number for each point
    for i in range(len(xbin) - 1):
        xmin = bins[i]
        xmax = bins[i + 1]
        yb = 0.
        for j in range(len(y)):
            if y[j] > xmin:
                yb = yb + 1.
            if y[j] > xmax:
                ybin[i] = yb
                ybinerr[i] = N.sqrt(yb)
                xbin[i] = 0.5 * (xmin + xmax)

                break

    return xbin, ybin, ybinerr
Пример #8
0
def fig6b(m,w): # Cumulative Metallicity distribution
    """ Plot differential metallicity distribution """
    wnorm = w/sum(w)
    i = numarray.argsort(m)
    mm = m[i] 
    ww = wnorm[i]
    pylab.plot(mm,numarray.cumsum(ww))
    pylab.xlabel("[Fe/H]")
    pylab.ylabel("N < [Fe/H]")
Пример #9
0
def fig6b(m, w):  # Cumulative Metallicity distribution
    """ Plot differential metallicity distribution """
    wnorm = w / sum(w)
    i = numarray.argsort(m)
    mm = m[i]
    ww = wnorm[i]
    pylab.plot(mm, numarray.cumsum(ww))
    pylab.xlabel("[Fe/H]")
    pylab.ylabel("N < [Fe/H]")
Пример #10
0
def binitsumequal(x, y, n):  #bin arrays x, y into n bins, returning xbin,ybin
    nx = len(x)
    #x=N.array(x,'f')
    #y=N.array(y,'f')
    y = N.take(y, N.argsort(x))
    x = N.take(x, N.argsort(x))
    xbin = N.zeros(n, 'f')
    ybin = N.zeros(n, 'f')
    #ybinerr=N.zeros(n,'f')
    for i in range(n):
        nmin = i * int(float(nx) / float(n))
        nmax = (i + 1) * int(float(nx) / float(n))
        xbin[i] = N.average(x[nmin:nmax])
        ybin[i] = N.sum(y[nmin:nmax])
        #xbin[i]=N.average(x[nmin:nmax])
        #ybin[i]=N.average(y[nmin:nmax])
        #ybinerr[i]=scipy.stats.std(y[nmin:nmax])
    return xbin, ybin  #, ybinerr
Пример #11
0
 def getnearestgen(self,ra1,dec1,ra2,dec2,j):#measure distances from ra1, dec1 to members in catalog ra2, dec2
     sig5=N.zeros(len(ra1),'f')
     sig10=N.zeros(len(ra1),'f')
     for i in range(len(ra1)):
         angdist=my.DA(c.z[j],h100)#kpc/arcsec
         dspec=N.sqrt((ra1[i]-ra2)**2+(dec1[i]-dec2)**2)#sorted array of distances in degrees
         dspecsort=N.take(dspec,N.argsort(dspec))
         sig5[i]=5./(N.pi)/(dspecsort[5]*3600.*angdist/1000.)**2#convert from deg to arcsec, multiply by DA (kpc/arcsec), divide by 1000 to convert to Mpc, index 5 element b/c 0 is itself
         sig10[i]=10./(N.pi)/(dspecsort[10]*3600.*angdist/1000.)**2#convert from deg to arcsec, multiply by DA (kpc/arcsec), divide by 1000 to convert to Mpc, index 5 element b/c 0 is itself
     return sig5, sig10
Пример #12
0
def biniterr(
        x, y, n
):  #bin arrays x, y into n equally-populated bins, returning xbin,ybin
    nx = len(x)
    #x=N.array(x,'f')
    #y=N.array(y,'f')
    y = N.take(y, N.argsort(x))
    x = N.take(x, N.argsort(x))
    xbin = N.zeros(n, 'f')
    xbin = N.zeros(n, 'f')
    ybin = N.zeros(n, 'f')
    ybinerr = N.zeros(n, 'f')
    for i in range(n):
        nmin = i * int(float(nx) / float(n))
        nmax = (i + 1) * int(float(nx) / float(n))
        #xbin[i]=scipy.stats.stats.median(x[nmin:nmax])
        #ybin[i]=scipy.stats.stats.median(y[nmin:nmax])
        xbin[i] = N.average(x[nmin:nmax])
        ybin[i] = N.average(y[nmin:nmax])
        ybinerr[i] = pylab.std(y[nmin:nmax]) / N.sqrt(1. * (nmax - nmin))
    return xbin, ybin, ybinerr
Пример #13
0
def getbins(x, y, n):  #get left side of equally populated bins
    nx = len(x)
    #x=N.array(x,'f')
    #y=N.array(y,'f')
    y = N.take(y, N.argsort(x))
    x = N.take(x, N.argsort(x))
    xbinright = N.zeros(n, 'f')
    xbinleft = N.zeros(n, 'f')
    ybin = N.zeros(n, 'f')
    ybinerr = N.zeros(n, 'f')
    for i in range(n):
        nmin = i * int(float(nx) / float(n))
        nmax = (i + 1) * int(float(nx) / float(n))
        #xbin[i]=scipy.stats.stats.median(x[nmin:nmax])
        #ybin[i]=scipy.stats.stats.median(y[nmin:nmax])
        #xbin[i]=N.average(x[nmin:nmax])
        xbinleft[i] = (x[nmin])
        xbinright[i] = (x[nmax])
        #ybin[i]=N.average(y[nmin:nmax])
        #ybinerr[i]=pylab.std(y[nmin:nmax])#/N.sqrt(1.*(nmax-nmin))
    return xbinleft, xbinright
Пример #14
0
    def process(self, X, V, C):
        """Do I need to compute the branch, or will it always be in the direction of freepar = constant?"""
        BifPoint.process(self, X, V, C)

        F = DiscreteMap(C.sysfunc, period=2*C.sysfunc.period)
        FP = FixedPointMap(F)

        J_coords = FP.jac(X, C.coords)
        J_params = FP.jac(X, C.params)
        
        # Locate branch of double period map
        W, VL = linalg.eig(J_coords, left=1, right=0)
        ind = argsort([abs(eig) for eig in W])[0]
        psi = real(VL[:,ind])
        
        A = r_[c_[J_coords, J_params], [V]]
        W, VR = linalg.eig(A)
        W0 = argsort([abs(eig) for eig in W])[0]
        V1 = real(VR[:,W0])
        
        H = FP.hess(X, C.coords+C.params, C.coords+C.params)
        c11 = matrixmultiply(psi,[bilinearform(H[i,:,:], V, V) for i in range(H.shape[0])])
        c12 = matrixmultiply(psi,[bilinearform(H[i,:,:], V, V1) for i in range(H.shape[0])])
        c22 = matrixmultiply(psi,[bilinearform(H[i,:,:], V1, V1) for i in range(H.shape[0])])
        
        beta = 1
        alpha = -1*c22/(2*c12)
        V1 = alpha*V + beta*V1
        V1 /= linalg.norm(V1)
        
        J_coords = C.sysfunc.jac(X, C.coords)
        W = linalg.eig(J_coords, right=0)
        
        self.found[-1].eigs = W
        self.found[-1].branch_period = 2*C.sysfunc.period
        self.found[-1].branch = todict(C, V1)
        
        self.info(C, -1)
        
        return True
Пример #15
0
def completeness(
    bins, y, yflag
):  #y is an array of input fluxes, yflag tells if sources was recovered
    #for i in range(len(y)):
    #	print y[i],yflag[i]
    nbin = len(bins)
    xbin = N.zeros(len(bins), 'f')
    ybin = N.zeros(len(bins), 'f')
    ybin2 = N.zeros(len(bins), 'f')
    yratio = N.zeros(len(bins), 'f')
    ybinerr = N.zeros(len(bins), 'f')
    yflag = N.take(yflag, N.argsort(y))
    y = N.take(y, N.argsort(y))
    #for i in range(20):
    #print 'sorted',i,y[i],yflag[i]
    xmin = min(bins)
    xmax = max(bins)
    xbinnumb = ((y - xmin) * nbin / (xmax - xmin)
                )  #calculate x  bin number for each point
    for i in range(len(xbin) - 1):
        xmin = bins[i]
        xmax = bins[i + 1]
        yb = 0.
        yf = 0.
        for j in range(len(y)):
            if y[j] > xmin:
                yb = yb + 1.
                if yflag[j] > 0.1:
                    yf = yf + 1.
            if y[j] > xmax:
                ybin[i] = yb
                ybin2[i] = yf
                (yratio[i], ybinerr[i]) = ratioerror(1. * yf, 1. * yb)
                xbin[i] = 0.5 * (xmin + xmax)
                #print 'completeness',xbin[i],yf,yb,yratio[i],ybinerr[i]
                break

    return xbin, yratio, ybinerr
Пример #16
0
 def locate(self, P1, P2, C):
     # Initiliaze p vector to eigenvector with smallest eigenvalue
     X, V = P1
     
     J_coords = C.CorrFunc.jac(X, C.coords)
     
     W, VL = linalg.eig(J_coords, left=1, right=0)
     ind = argsort([abs(eig) for eig in W])[0]
     p = real(VL[:,ind])
     
     initpoint = zeros(2*C.dim, Float)
     initpoint[0:C.dim] = X
     initpoint[C.dim+1:] = p
     
     X = optimize.fsolve(self.__locate_newton, initpoint, C)
     self.data.psi = X[C.dim+1:]
     X = X[0:C.dim]
     
     return X, V
Пример #17
0
def getMaps(pt, pttype, output=False, screen=False, cyclic=True):
    jac0 = pt.labels['LC']['data'].jac0
    jac1 = pt.labels['LC']['data'].jac1
    flow = pt.labels[pttype]['flow']

    n = jac0.shape[0]

    # Compute jacobian times vec
    J = linalg.solve(jac1, jac0)
    if output:
        print "Jacobian J*x"
        print "------------\n"
        print J
        print "\n"

        print "Check Jacobian"
        print "--------------\n"
        print "   eigs = ", linalg.eig(J)[0]
        print "   eigs = ", pt.labels['LC']['data'].evals

        # Compute composition of flow maps

        print "Flow maps"
        print "---------\n"

    ntst = len(flow) / 2
    maps = []
    if not cyclic:
        for i in range(ntst):
            I = identity(n)
            for j in mod(arange(i, i + ntst), ntst):
                j = int(j)
                I = linalg.solve(flow[2 * j + 1],
                                 matrixmultiply(flow[2 * j], I))

            maps.append(I)

        # Check eigs of maps
        evals = []
        levecs = []
        revecs = []
        for m in maps:
            w, vl, vr = linalg.eig(m, left=1, right=1)
            evals.append(w)
            levecs.append(vl)
            revecs.append(vr)

        if output:
            for i in range(ntst):
                print evals[i]

        # Get left evecs along curve associated with 1 evalue
        evec1 = []
        good = []
        for i in range(ntst):
            ind = argsort(abs(evals[i] - 1.))[0]
            if abs(evals[i][ind] - 1) > 0.05:
                print "Bad floquet multipliers!"
            else:
                good.append(i)
            evec1.append(levecs[i][:, ind])
    else:
        # CYCLIC METHOD
        print "Similarity method!\n"
        I = identity(n)
        for i in range(ntst):
            I = linalg.solve(flow[2 * i + 1], matrixmultiply(flow[2 * i], I))

        evec1 = []
        w, vl, vr = linalg.eig(I, left=1, right=1)
        print w, vl
        ind = argsort(abs(w - 1.))[0]
        if abs(w[ind] - 1) > 0.05:
            raise "Bad floquet multipliers!"
        else:
            v = vl[:, ind]
            print v

        for i in range(ntst):
            v = matrixmultiply(transpose(flow[2 * i]),
                               linalg.solve(transpose(flow[2 * i + 1]), v))
            evec1.append(v / linalg.norm(v))

        # print "Cyclic method!\n"
        # evals = []
        # levecs = []
        # revecs = []
        # C0 = zeros((n*ntst, n*ntst), Float64)
        # C1 = zeros((n*ntst, n*ntst), Float64)
        # for i in range(ntst):
        # C0[(n*i):(n*(i+1)), int(mod(n*(i+1), n*ntst)):int(mod(n*(i+2), n*ntst))] = flow[2*i]
        # C1[(n*i):(n*(i+1)), (n*i):(n*(i+1))] = flow[2*i+1]
        #
        # w, vl, vr = linalg.eig(C0, C1, left=1, right=1)
        # print w

    # Same direction - if right eigenvector
    cycle = pt.labels[pttype]['cycle']
    coords = cycle.coordnames
    #for i in range(ntst):
    #    if matrixmultiply(evec1[i], (cycle[4*i+1]-cycle[4*i]).toarray()) < 0:
    #        evec1[i] = -1*evec1[i]

    if screen:
        x = cycle[coords[0]]
        y = cycle[coords[1]]
        pylab.plot(x, y)
        for i in good:
            a = [x[4 * i], x[4 * i] + 10 * evec1[i][0]]
            b = [y[4 * i], y[4 * i] + 10 * evec1[i][1]]
            pylab.plot(a, b, 'r', linewidth=1)
            pylab.plot([x[4 * i]], [y[4 * i]], 'gs')
            #pylab.plot([x[4*i], x[4*i] + 0.5*vhd[0]], [y[4*i], y[4*i] + 0.5*vhd[1]], 'b')
            #print evec1[i], vhd, "\n"
            #print matrixmultiply(evec1[i], vhd)

    if screen:
        Je, JE = linalg.eig(jac0, jac1)
        ind = argsort(abs(Je - 1.))[0]
        #if abs(Je[ind]-1) > 0.05:
        if 0:
            print "Jacobian: Bad floquet multipliers!"
        else:
            u, s, vh = linalg.svd(jac0 - jac1, compute_uv=1)
            evec2 = [transpose(vh)[:, -1]]
            #evec2 = [JE[:,ind]]

            for i in range(ntst):
                evec2.append(
                    linalg.solve(flow[2 * i + 1],
                                 matrixmultiply(flow[2 * i], evec2[i])))

            # Same direction
            for i in range(ntst):
                if matrixmultiply(
                        evec2[i],
                    (cycle[4 * i + 1] - cycle[4 * i]).toarray()) < 0:
                    evec2[i] = -1 * evec2[i]

            for i in range(ntst):
                a = [x[4 * i], x[4 * i] + 0.5 * evec2[i][0]]
                b = [y[4 * i], y[4 * i] + 0.5 * evec2[i][1]]
                #pylab.plot(a, b, 'r', linewidth=1)
                #pylab.plot([x[4*i]], [y[4*i]], 'gs')

    return J, maps, evec1
Пример #18
0
def fmin(func,
         x0,
         args=(),
         xtol=1e-4,
         ftol=1e-4,
         maxiter=None,
         maxfun=None,
         fulloutput=0,
         printmessg=1):
    """xopt,{fval,warnflag} = fmin(function, x0, args=(), xtol=1e-4, ftol=1e-4,
    maxiter=2000*len(x0), maxfun=2000*len(x0), fulloutput=0, printmessg=0)

    Uses a Nelder-Mead Simplex algorithm to find the minimum of function
    of one or more variables.
    """
    x0 = Num.asarray(x0)
    assert (len(x0.shape) == 1)
    N = len(x0)
    if maxiter is None:
        maxiter = N * 200
    if maxfun is None:
        maxfun = N * 200

    rho = 1
    chi = 2
    psi = 0.5
    sigma = 0.5
    one2np1 = range(1, N + 1)

    sim = Num.zeros((N + 1, N), x0.typecode())
    fsim = Num.zeros((N + 1, ), 'd')
    sim[0] = x0
    fsim[0] = apply(func, (x0, ) + args)
    nonzdelt = 0.05
    zdelt = 0.00025
    for k in range(0, N):
        y = Num.array(x0, copy=1)
        if random.randint(0, 1):
            y[k] += 10
        else:
            y[k] -= 10


##         if y[k] != 0:
##             y[k] = (1+nonzdelt)*y[k]
##         else:
##             y[k] = zdelt

        sim[k + 1] = y
        f = apply(func, (y, ) + args)
        fsim[k + 1] = f

    ind = Num.argsort(fsim)
    fsim = Num.take(fsim,
                    ind)  # sort so sim[0,:] has the lowest function value
    sim = Num.take(sim, ind, 0)

    iterations = 1
    funcalls = N + 1

    while (funcalls < maxfun and iterations < maxiter):
        ##         if (max(Num.ravel(abs(sim[1:]-sim[0]))) <= xtol \
        ##             and max(abs(fsim[0]-fsim[1:])) <= ftol):
        ##             break

        xbar = Num.add.reduce(sim[:-1], 0) / N
        xr = (1 + rho) * xbar - rho * sim[-1]
        fxr = apply(func, (xr, ) + args)
        funcalls = funcalls + 1
        doshrink = 0

        if fxr < fsim[0]:
            xe = (1 + rho * chi) * xbar - rho * chi * sim[-1]
            fxe = apply(func, (xe, ) + args)
            funcalls = funcalls + 1

            if fxe < fxr:
                sim[-1] = xe
                fsim[-1] = fxe
            else:
                sim[-1] = xr
                fsim[-1] = fxr
        else:  # fsim[0] <= fxr
            if fxr < fsim[-2]:
                sim[-1] = xr
                fsim[-1] = fxr
            else:  # fxr >= fsim[-2]
                # Perform contraction
                if fxr < fsim[-1]:
                    xc = (1 + psi * rho) * xbar - psi * rho * sim[-1]
                    fxc = apply(func, (xc, ) + args)
                    funcalls = funcalls + 1

                    if fxc <= fxr:
                        sim[-1] = xc
                        fsim[-1] = fxc
                    else:
                        doshrink = 1
                else:
                    # Perform an inside contraction
                    xcc = (1 - psi) * xbar + psi * sim[-1]
                    fxcc = apply(func, (xcc, ) + args)
                    funcalls = funcalls + 1

                    if fxcc < fsim[-1]:
                        sim[-1] = xcc
                        fsim[-1] = fxcc
                    else:
                        doshrink = 1

                if doshrink:
                    for j in one2np1:
                        sim[j] = sim[0] + sigma * (sim[j] - sim[0])
                        fsim[j] = apply(func, (sim[j], ) + args)
                    funcalls = funcalls + N

        ind = Num.argsort(fsim)
        sim = Num.take(sim, ind, 0)
        fsim = Num.take(fsim, ind)
        iterations += 1

    x = sim[0]
    fval = min(fsim)
    warnflag = 0

    if funcalls >= maxfun:
        warnflag = 1
        if printmessg:
            print "Warning: Maximum number of function evaluations has been exceeded:", funcalls
    elif iterations >= maxiter:
        warnflag = 2
        if printmessg:
            print "Warning: Maximum number of iterations has been exceeded:", iterations
    else:
        if printmessg:
            print "Optimization terminated successfully."

    print "         Current function value: %f" % fval
    print "         Iterations: %d" % iterations
    print "         Function evaluations: %d" % funcalls

    try:
        apply(func, (None, ))  #tell the function thread to stop
    except TypeError:
        print "Telling the function thread to stop"

    if fulloutput:
        return x, fval, warnflag
    else:
        return x
Пример #19
0
def matchum(file1,
            file2,
            tol=10,
            perr=4,
            aerr=1.0,
            nmax=40,
            im_masks1=[],
            im_masks2=[],
            debug=0,
            domags=0,
            xrange=None,
            yrange=None,
            sigma=4,
            aoffset=0):
    '''Take the output of two sextractor runs and match up the objects with
   each other (find out which objects in the first file match up with
   objects in the second file.  The routine considers a 'match' to be any 
   two objects that are closer than tol pixels (after applying the shift).  
   Returns a 6-tuple:  (x1,y1,x2,y2,o1,o2).  o1 and o2 are the ojbects
   numbers such that o1[i] in file 1 corresponds to o2[i] in file 2.'''
    NA = num.NewAxis

    sexdata1 = readsex(file1)
    sexdata2 = readsex(file2)

    # Use the readsex data to get arrays of the (x,y) positions
    x1 = num.asarray(sexdata1[0]['X_IMAGE'])
    y1 = num.asarray(sexdata1[0]['Y_IMAGE'])
    x2 = num.asarray(sexdata2[0]['X_IMAGE'])
    y2 = num.asarray(sexdata2[0]['Y_IMAGE'])
    m1 = num.asarray(sexdata1[0]['MAG_BEST'])
    m2 = num.asarray(sexdata2[0]['MAG_BEST'])
    o1 = num.asarray(sexdata1[0]['NUMBER'])
    o2 = num.asarray(sexdata2[0]['NUMBER'])
    f1 = num.asarray(sexdata1[0]['FLAGS'])
    f2 = num.asarray(sexdata2[0]['FLAGS'])

    # First, make a cut on the flags:
    gids = num.where(f1 < 4)
    x1 = x1[gids]
    y1 = y1[gids]
    m1 = m1[gids]
    o1 = o1[gids]
    gids = num.where(f2 < 4)
    x2 = x2[gids]
    y2 = y2[gids]
    m2 = m2[gids]
    o2 = o2[gids]

    # next, if there is a range to use:
    if xrange is not None and yrange is not None:
        cond = num.greater(x1, xrange[0])*num.less(x1,xrange[1])*\
              num.greater(y1, yrange[0])*num.less(y1,yrange[1])
        gids = num.where(cond)
        x1 = x1[gids]
        y1 = y1[gids]
        m1 = m1[gids]
        o1 = o1[gids]
        cond = num.greater(x2, xrange[0])*num.less(x2,xrange[1])*\
              num.greater(y2, yrange[0])*num.less(y2,yrange[1])
        gids = num.where(cond)
        x2 = x2[gids]
        y2 = y2[gids]
        m2 = m2[gids]
        o2 = o2[gids]

    # Use the user masks
    for m in im_masks1:
        print "applying mask (%d,%d,%d,%d)" % tuple(m)
        condx = num.less(x1, m[0]) + num.greater(x1, m[1])
        condy = num.less(y1, m[2]) + num.greater(y1, m[3])
        gids = num.where(condx + condy)
        x1 = x1[gids]
        y1 = y1[gids]
        m1 = m1[gids]
        o1 = o1[gids]

    for m in im_masks2:
        print "applying mask (%d,%d,%d,%d)" % tuple(m)
        condx = num.less(x2, m[0]) + num.greater(x2, m[1])
        condy = num.less(y2, m[2]) + num.greater(y2, m[3])
        gids = num.where(condx + condy)
        x2 = x2[gids]
        y2 = y2[gids]
        m2 = m2[gids]
        o2 = o2[gids]

    if nmax:
        if len(x1) > nmax:
            ids = num.argsort(m1)[0:nmax]
            x1 = x1[ids]
            y1 = y1[ids]
            m1 = m1[ids]
            o1 = o1[ids]
        if len(x2) > nmax:
            ids = num.argsort(m2)[0:nmax]
            x2 = x2[ids]
            y2 = y2[ids]
            m2 = m2[ids]
            o2 = o2[ids]
    if debug:
        print "objects in frame 1:"
        print o1
        print "objects in frame 2:"
        print o2
        mp = pygplot.MPlot(2, 1, device='/XWIN')
        p = pygplot.Plot()
        p.point(x1, y1)
        [p.label(x1[i], y1[i], "%d" % o1[i]) for i in range(len(x1))]
        mp.add(p)
        p = pygplot.Plot()
        p.point(x2, y2)
        [p.label(x2[i], y2[i], "%d" % o2[i]) for i in range(len(x2))]
        mp.add(p)
        mp.plot()
        mp.close()

    # Now, we make 2-D arrays of all the differences in x and y between each pair
    #  of objects.  e.g., dx1[n,m] is the delta-x between object n and m in file 1 and
    #  dy2[n,m] is the y-distance between object n and m in file 2.
    dx1 = x1[NA, :] - x1[:, NA]
    dx2 = x2[NA, :] - x2[:, NA]
    dy1 = y1[NA, :] - y1[:, NA]
    dy2 = y2[NA, :] - y2[:, NA]
    # Same, but with angles
    da1 = num.arctan2(dy1, dx1) * 180 / num.pi
    da2 = num.arctan2(dy2, dx2) * 180 / num.pi
    # Same, but with absolute distances
    ds1 = num.sqrt(num.power(dx1, 2) + num.power(dy1, 2))
    ds2 = num.sqrt(num.power(dx2, 2) + num.power(dy2, 2))

    # Here's the real magic:  this is a matrix of matrices (4-D).  Consider 4 objects:
    #  objects i and j in file 1 and objects m and n in file 2.  dx[i,j,m,n] is the
    #  difference between delta-xs for objects i,j in file 1 and m,n in file 2.  If object
    #  i corresponds to object m and object j corresponds to object n, this should be a small
    #  number, irregardless of an overall shift in coordinate systems between file 1 and 2.
    dx = dx1[::, ::, NA, NA] - dx2[NA, NA, ::, ::]
    dy = dy1[::, ::, NA, NA] - dy2[NA, NA, ::, ::]
    da = da1[::, ::, NA, NA] - da2[NA, NA, ::, ::] + aoffset
    ds = ds1[::, ::, NA, NA] - ds2[NA, NA, ::, ::]
    # pick out close pairs.
    #use = num.less(dy,perr)*num.less(dx,perr)*num.less(num.abs(da),aerr)
    use = num.less(ds, perr) * num.less(num.abs(da), aerr)
    use = use.astype(num.Int32)

    #use = num.less(num.abs(da),perr)
    suse = num.add.reduce(num.add.reduce(use, 3), 1)
    print suse[0]

    guse = num.greater(suse, suse.flat.max() / 2)
    i = [j for j in range(x1.shape[0]) if num.sum(guse[j])]
    m = [num.argmax(guse[j]) for j in range(x1.shape[0]) if num.sum(guse[j])]
    xx0, yy0, oo0, mm0 = num.take([x1, y1, o1, m1], i, 1)
    xx1, yy1, oo1, mm1 = num.take([x2, y2, o2, m2], m, 1)
    if debug:
        mp = pygplot.MPlot(2, 1, device='/XWIN')
        p = pygplot.Plot()
        p.point(xx0, yy0)
        [p.label(xx0[i], yy0[i], "%d" % oo0[i]) for i in range(len(xx0))]
        mp.add(p)
        p = pygplot.Plot()
        p.point(xx1, yy1)
        [p.label(xx1[i], yy1[i], "%d" % oo1[i]) for i in range(len(xx1))]
        mp.add(p)
        mp.plot()
        mp.close()
    xshift, xscat = stats.bwt(xx0 - xx1)
    xscat = max([1.0, xscat])
    yshift, yscat = stats.bwt(yy0 - yy1)
    yscat = max([1.0, yscat])
    mshift, mscat = stats.bwt(mm0 - mm1)
    print "xscat = ", xscat
    print "yscat = ", yscat
    print "xshift = ", xshift
    print "yshift = ", yshift
    print "mshift = ", mshift
    print "mscat = ", mscat
    keep = num.less(num.abs(xx0-xx1-xshift),sigma*xscat)*\
          num.less(num.abs(yy0-yy1-yshift),sigma*yscat)
    # This is a list of x,y,object# in each file.
    xx0, yy0, oo0, xx1, yy1, oo1 = num.compress(keep,
                                                [xx0, yy0, oo0, xx1, yy1, oo1],
                                                1)

    if debug:
        print file1, oo0
        print file2, oo1
        mp = pygplot.MPlot(2, 1, device='temp.ps/CPS')
        p1 = pygplot.Plot()
        p1.point(xx0, yy0, symbol=25, color='red')
        for i in range(len(xx0)):
            p1.label(xx0[i], yy0[i], " %d" % oo0[i], color='red')
        mp.add(p1)
        p2 = pygplot.Plot()
        p2.point(xx1, yy1, symbol=25, color='green')
        for i in range(len(xx1)):
            p2.label(xx1[i], yy1[i], " %d" % oo1[i], color='green')
        mp.add(p2)
        mp.plot()
        mp.close()

    if domags:
        return (xx0, yy0, mm0, xx1, yy1, mm1, mshift, mscat, oo0, oo1)
    else:
        return (xx0, yy0, xx1, yy1, oo0, oo1)
Пример #20
0
def getMaps(pt, pttype, output=False, screen=False, cyclic=True):
    jac0 = pt.labels['LC']['data'].jac0
    jac1 = pt.labels['LC']['data'].jac1
    flow = pt.labels[pttype]['flow']
    
    n = jac0.shape[0]

    # Compute jacobian times vec
    J = linalg.solve(jac1, jac0)
    if output:
        print "Jacobian J*x"
        print "------------\n"
        print J
        print "\n"
        
        print "Check Jacobian"
        print "--------------\n"
        print "   eigs = ", linalg.eig(J)[0]
        print "   eigs = ", pt.labels['LC']['data'].evals

    # Compute composition of flow maps
    
        print "Flow maps"
        print "---------\n"

    ntst = len(flow)/2
    maps = []
    if not cyclic:
        for i in range(ntst):
            I = identity(n)
            for j in mod(arange(i, i + ntst), ntst):
                j = int(j)
                I = linalg.solve(flow[2*j+1], matrixmultiply(flow[2*j], I))
            
            maps.append(I)
    
        # Check eigs of maps
        evals = []
        levecs = []
        revecs = []
        for m in maps:
            w, vl, vr = linalg.eig(m, left=1, right=1)
            evals.append(w)
            levecs.append(vl)
            revecs.append(vr)
            
        if output:
            for i in range(ntst):
                print evals[i]
            
        # Get left evecs along curve associated with 1 evalue
        evec1 = []
        good = []
        for i in range(ntst):
            ind = argsort(abs(evals[i]-1.))[0]
            if abs(evals[i][ind]-1) > 0.05:
                print "Bad floquet multipliers!"
            else:
                good.append(i)
            evec1.append(levecs[i][:,ind])
    else:
        # CYCLIC METHOD
        print "Similarity method!\n"
        I = identity(n)
        for i in range(ntst):
            I = linalg.solve(flow[2*i+1], matrixmultiply(flow[2*i], I))
            
        evec1 = []
        w, vl, vr = linalg.eig(I, left=1, right=1)
        print w, vl
        ind = argsort(abs(w-1.))[0]
        if abs(w[ind]-1) > 0.05:
            raise "Bad floquet multipliers!"
        else:
            v = vl[:,ind]
            print v
        
        for i in range(ntst):
            v = matrixmultiply(transpose(flow[2*i]), linalg.solve(transpose(flow[2*i+1]), v))
            evec1.append(v/linalg.norm(v))
            
        # print "Cyclic method!\n"
        # evals = []
        # levecs = []
        # revecs = []
        # C0 = zeros((n*ntst, n*ntst), Float64)
        # C1 = zeros((n*ntst, n*ntst), Float64)
        # for i in range(ntst):
            # C0[(n*i):(n*(i+1)), int(mod(n*(i+1), n*ntst)):int(mod(n*(i+2), n*ntst))] = flow[2*i]
            # C1[(n*i):(n*(i+1)), (n*i):(n*(i+1))] = flow[2*i+1]
            # 
        # w, vl, vr = linalg.eig(C0, C1, left=1, right=1)
        # print w
                
    # Same direction - if right eigenvector
    cycle = pt.labels[pttype]['cycle']
    coords = cycle.coordnames
    #for i in range(ntst):
    #    if matrixmultiply(evec1[i], (cycle[4*i+1]-cycle[4*i]).toarray()) < 0:
    #        evec1[i] = -1*evec1[i]
            
    if screen:
        x = cycle[coords[0]]
        y = cycle[coords[1]]
        pylab.plot(x,y)
        for i in good:
            a = [x[4*i], x[4*i] + 10*evec1[i][0]]
            b = [y[4*i], y[4*i] + 10*evec1[i][1]]
            pylab.plot(a, b, 'r', linewidth=1)
            pylab.plot([x[4*i]], [y[4*i]], 'gs')
            #pylab.plot([x[4*i], x[4*i] + 0.5*vhd[0]], [y[4*i], y[4*i] + 0.5*vhd[1]], 'b')
            #print evec1[i], vhd, "\n"
            #print matrixmultiply(evec1[i], vhd)
                  
    if screen:
        Je, JE = linalg.eig(jac0, jac1)
        ind = argsort(abs(Je-1.))[0]
        #if abs(Je[ind]-1) > 0.05:
        if 0:
            print "Jacobian: Bad floquet multipliers!"
        else:
            u, s, vh = linalg.svd(jac0-jac1, compute_uv=1)
            evec2 = [transpose(vh)[:,-1]]
            #evec2 = [JE[:,ind]]
             
            for i in range(ntst):
                evec2.append(linalg.solve(flow[2*i+1], matrixmultiply(flow[2*i], evec2[i])))
                 
            # Same direction
            for i in range(ntst):
                if matrixmultiply(evec2[i], (cycle[4*i+1]-cycle[4*i]).toarray()) < 0:
                    evec2[i] = -1*evec2[i]
                     
            for i in range(ntst):
                a = [x[4*i], x[4*i] + 0.5*evec2[i][0]]
                b = [y[4*i], y[4*i] + 0.5*evec2[i][1]]
                #pylab.plot(a, b, 'r', linewidth=1)
                #pylab.plot([x[4*i]], [y[4*i]], 'gs')
                                
    return J, maps, evec1