def interpolate(points, t): """points=(x,y), list of x's and y's. "t" ... list of x, for which we want to evaluate the dependence "y". returns a list of "y" evaluated at the grid "t". Assumes t is sorted in ascending order Assumes points x axis are sorted in ascending order """ xx, yy = points x = t t0 = numarray.compress(x <= min(xx), x) # t1 = numarray.compress( (x>min(xx)) & (x<max(xx)), x ) t1 = numarray.compress((x > min(xx)) & (x < xx[-1]), x) # t2 = numarray.compress(x>=max(xx), x) t2 = numarray.compress(x >= xx[-1], x) if (xx[1] - xx[0] == 0): slope0 = sign(yy[1] - yy[0]) * 1e20 else: slope0 = (yy[1] - yy[0]) / (xx[1] - xx[0]) slope2 = (yy[-1] - yy[-2]) / (xx[-1] - xx[-2]) indices = numarray.searchsorted(xx, t1) x0 = xx[indices - 1] x1 = xx[indices] y0 = yy[indices - 1] y1 = yy[indices] slope = (y1 - y0) / (x1 - x0) y1 = slope * (t1 - x0) + y0 y0 = slope0 * (t0 - xx[0]) + yy[0] # extrapolate y2 = slope2 * (t2 - xx[-1]) + yy[-1] # extrapolate y = numarray.concatenate((y0, y1, y2)) return y
def interpolate(points,t): """points=(x,y), list of x's and y's. "t" ... list of x, for which we want to evaluate the dependence "y". returns a list of "y" evaluated at the grid "t". Assumes t is sorted in ascending order Assumes points x axis are sorted in ascending order """ xx,yy=points x=t t0 = numarray.compress(x<=min(xx), x) # t1 = numarray.compress( (x>min(xx)) & (x<max(xx)), x ) t1 = numarray.compress( (x>min(xx)) & (x<xx[-1]), x ) # t2 = numarray.compress(x>=max(xx), x) t2 = numarray.compress(x>=xx[-1], x) if (xx[1]-xx[0] == 0): slope0 = sign(yy[1]-yy[0])*1e20 else: slope0 = (yy[1]-yy[0])/(xx[1]-xx[0]) slope2 = (yy[-1]-yy[-2])/(xx[-1]-xx[-2]) indices = numarray.searchsorted(xx,t1) x0 = xx[indices-1] x1 = xx[indices] y0 = yy[indices-1] y1 = yy[indices] slope = (y1-y0)/(x1-x0) y1 = slope*(t1-x0)+y0 y0 = slope0*(t0-xx[0])+yy[0] # extrapolate y2 = slope2*(t2-xx[-1])+yy[-1] # extrapolate y = numarray.concatenate((y0,y1,y2)) return y
def binitbins(xmin, xmax, nbin, x, y): #use equally spaced bins dx = float((xmax - xmin) / (nbin)) xbin = N.arange(xmin, (xmax), dx) + dx / 2. #print "within binitbins" #print "xbin = ",xbin #print "dx = ",dx #print "xmax = ",xmax #print "xmin = ",xmin ybin = N.zeros(len(xbin), 'd') ybinerr = N.zeros(len(xbin), 'd') xbinnumb = N.array(len(x), 'd') x1 = N.compress((x >= xmin) & (x <= xmax), x) y1 = N.compress((x >= xmin) & (x <= xmax), y) x = x1 y = y1 xbinnumb = ((x - xmin) * nbin / (xmax - xmin) ) #calculate x bin number for each point j = -1 for i in range(len(xbin)): ydata = N.compress(abs(xbinnumb - float(i)) < .5, y) try: ybin[i] = N.average(ydata) #ybin[i]=pylab.median(ydata) ybinerr[i] = pylab.std(ydata) / N.sqrt(float(len(ydata))) except ZeroDivisionError: ybin[i] = 0. ybinerr[i] = 0. return xbin, ybin, ybinerr
def plot_source_info(self,event): ra = event.xdata dec = event.ydata #print (event.key,ra,dec) if event.key == 's': #print self.constructed_pos[:,0] #print self.constructed_pos[:,1] dist = numarray.sqrt( (self.constructed_pos[:,0] - ra)**2 + (self.constructed_pos[:,1] - dec)**2) #print dist #print "min distance = %f " % min(dist) the_source_ind = numarray.compress(dist == min(dist), numarray.fromlist(range(len(self.constructed_source_list)))) #print the_source_ind #the_source_ind = numarray.compress(dist == min(dist),numarray.arange(len(self.constructed_source_list))) the_source = self.constructed_source_list[the_source_ind[0]] print the_source dist = numarray.sqrt( (the_source.current_pos[0] - self.real_pos[:,0])**2 + (the_source.current_pos[1] - self.real_pos[:,1])**2) print "min distances to nearest real source = %f arcsec" % min(dist) the_source_ind = numarray.compress(dist == min(dist), numarray.fromlist(range(len(self.real_list)))) the_source = self.real_list[the_source_ind[0]] print "That real source is at ra=%f dec=%f" % (the_source.start_pos[0],the_source.start_pos[1]) if event.key == 'r': dist = numarray.sqrt( (self.real_pos[:,0] - ra)**2 + (self.real_pos[:,1] - dec)**2) #print dist #print "min distance = %f " % min(dist) the_source_ind = numarray.compress(dist == min(dist), numarray.fromlist(range(len(self.real_list)))) #print the_source_ind #the_source_ind = numarray.compress(dist == min(dist),numarray.arange(len(self.constructed_source_list))) the_source = self.real_list[the_source_ind[0]] print the_source
def drawmeridians(self,ax,meridians,color='k',linewidth=1., \ linestyle='--',dashes=[1,1]): """ draw meridians (longitude lines). ax - current axis instance. meridians - list containing longitude values to draw (in degrees). color - color to draw meridians (default black). linewidth - line width for meridians (default 1.) linestyle - line style for meridians (default '--', i.e. dashed). dashes - dash pattern for meridians (default [1,1], i.e. 1 pixel on, 1 pixel off). """ if self.projection not in ['merc','cyl']: lats = N.arange(-80,81).astype('f') else: lats = N.arange(-90,91).astype('f') xdelta = 0.1*(self.xmax-self.xmin) ydelta = 0.1*(self.ymax-self.ymin) for merid in meridians: lons = merid*N.ones(len(lats),'f') x,y = self(lons,lats) # remove points outside domain. testx = N.logical_and(x>=self.xmin-xdelta,x<=self.xmax+xdelta) x = N.compress(testx, x) y = N.compress(testx, y) testy = N.logical_and(y>=self.ymin-ydelta,y<=self.ymax+ydelta) x = N.compress(testy, x) y = N.compress(testy, y) if len(x) > 1 and len(y) > 1: # split into separate line segments if necessary. # (not necessary for mercator or cylindrical). xd = (x[1:]-x[0:-1])**2 yd = (y[1:]-y[0:-1])**2 dist = N.sqrt(xd+yd) split = dist > 500000. if N.sum(split) and self.projection not in ['merc','cyl']: ind = (N.compress(split,MLab.squeeze(split*N.indices(xd.shape)))+1).tolist() xl = [] yl = [] iprev = 0 ind.append(len(xd)) for i in ind: xl.append(x[iprev:i]) yl.append(y[iprev:i]) iprev = i else: xl = [x] yl = [y] # draw each line segment. for x,y in zip(xl,yl): # skip if only a point. if len(x) > 1 and len(y) > 1: l = Line2D(x,y,linewidth=linewidth,linestyle=linestyle) l.set_color(color) l.set_dashes(dashes) ax.add_line(l)
def lsq_clipped(x, y, sig, int_scat=None, clip=3.0, astart=None, bstart=None, siga_in=0.0, sigb_in=0.0): olda = oldb = None if sig is None: sig = N.ones(x.shape) mwt = 0 else: sig = N.array(sig) mwt = 1 a = astart b = bstart while 1: if astart is None and bstart is None: if int_scat is None: results = nr.fit(x, y, sig) else: results = nr.fit_i(x, y, sig, int_scat) a, b, siga, sigb, chi2 = results scatter = calc_scatter(x, y, sig, int_scat, a, b) elif astart is not None and bstart is None: if int_scat is None: results = nr.fit_slope(x, y, sig, mwt, astart, siga_in) else: results = nr.fit_slope_i(x, y, sig, int_scat, astart, siga_in) b, sigb, chi2 = results siga = 0.0 scatter = calc_scatter(x, y, sig, int_scat, astart, b) elif astart is None and bstart is not None: if int_scat is None: results = nr.fit_intercept(x, y, sig, mwt, bstart, sigb_in) else: results = nr.fit_intercept_i(x, y, sig, int_scat, bstart, sigb_in) a, siga, chi2 = results sigb = 0.0 scatter = calc_scatter(x, y, sig, int_scat, a, bstart) if not (olda is None or oldb is None): if abs(a) < conv_limit: atest = conv_limit else: atest = abs(a) if abs(b) < conv_limit: btest = conv_limit else: btest = abs(b) if (abs(olda - a) < conv_limit*atest and abs(oldb - b) < conv_limit*btest): break keep = abs(y - a - b*x) < clip*scatter xnew = N.compress(keep, x) ynew = N.compress(keep, y) signew = N.compress(keep, sig) x = xnew y = ynew sig = signew olda, oldb = (a, b) n = len(x) return (a, b, siga, sigb, chi2, scatter, n)
def calcavesky(): input = open("noise.dat", 'r') aperture = [] counts = [] area = [] j = 0 for line in input: if line.find('#') > -1: #skip lines with '#' in them continue if line.find('mosaic_minus') > -1: #skip lines with '#' in them j = 0 continue j = j + 1 if (j > 3): #print j, line t = line.split() aperture.append(float(t[0])) counts.append(float(t[1])) area.append(float(t[2])) input.close() aperture = N.array(aperture, 'f') counts = N.array(counts, 'f') area = N.array(area, 'f') ap = N.zeros(npoints, 'f') aparea = N.zeros(nap, 'f') aveap = N.zeros(nap, 'f') aveaperr = N.zeros(nap, 'f') avearea = N.zeros(nap, 'f') aveareaerr = N.zeros(nap, 'f') #for i in range(len(ap)): for i in range(nap): #print i, len(ap),aperture[i],aperture[i+1] if (i < (nap - 1)): ap = N.compress( (aperture >= aperture[i]) & (aperture < aperture[i + 1]), counts) aparea = N.compress( (aperture >= aperture[i]) & (aperture < aperture[i + 1]), area) else: ap = N.compress((aperture >= aperture[i]) & (aperture < 20.), counts) aparea = N.compress((aperture >= aperture[i]) & (aperture < 20.), area) #print ap #aparea=N.compress((aperture >= aperture[i]) & (aperture < aperture[i+1]),area) aveap[i] = N.average(ap) aveaperr[i] = scipy.stats.std(ap) avearea[i] = N.average(aparea) aveareaerr[i] = scipy.stats.std(aparea) print "ave sky = %8.4f +/- %8.4f" % (N.average(ap), scipy.stats.std(ap)) print "ave area = %8.4f +/- %8.4f" % (N.average(aparea), scipy.stats.std(aparea)) return aveap, aveaperr, avearea, aveareaerr
def fig5b(t,m,p): # Metallicity vs. time """ Plot SFR vs age """ tt = numarray.compress(t>p.s0x,t) mm = numarray.compress(t>p.s0x,m) tt = numarray.compress(tt<p.s2x,tt) mm = numarray.compress(tt<p.s2x,mm) pylab.plot(tt,mm) metallicity_bezier(p) pylab.xlabel("Age (Gyr)") pylab.ylabel("Metallicity")
def fig5(t, m): # Metallicity vs. time """ Plot SFR vs age """ tt = numarray.compress(t > p.s0x, t) mm = numarray.compress(t > p.s0x, m) tt = numarray.compress(tt < p.s2x, tt) mm = numarray.compress(tt < p.s2x, mm) pylab.plot(10.**tt / 1.e9, mm) pylab.xlim(0.1, 15) pylab.xlabel("Age (Gyr)") pylab.ylabel("Metallicity")
def fig5b(t, m, p): # Metallicity vs. time """ Plot SFR vs age """ tt = numarray.compress(t > p.s0x, t) mm = numarray.compress(t > p.s0x, m) tt = numarray.compress(tt < p.s2x, tt) mm = numarray.compress(tt < p.s2x, mm) pylab.plot(tt, mm) metallicity_bezier(p) pylab.xlabel("Age (Gyr)") pylab.ylabel("Metallicity")
def fig5(t,m): # Metallicity vs. time """ Plot SFR vs age """ tt = numarray.compress(t>p.s0x,t) mm = numarray.compress(t>p.s0x,m) tt = numarray.compress(tt<p.s2x,tt) mm = numarray.compress(tt<p.s2x,mm) pylab.plot(10.**tt/1.e9,mm) pylab.xlim(0.1,15) pylab.xlabel("Age (Gyr)") pylab.ylabel("Metallicity")
def med_func(x, y, sig, b): small = 1.0e-8 aa = median(y - b*x) d = (y - aa - b*x) mad = median(N.absolute(d)) s = mad / 0.6745 d /= sig sign = N.compress(N.absolute(d) > small, d) sign = sign / N.absolute(sign) x = N.compress(N.absolute(d) > small, x) sum = N.sum(sign * x) return sum, s, aa
def contour(x, y, xmin, xmax, ymin, ymax): ncontbin = 20. dx = float(abs((xmax - xmin) / ncontbin)) dy = float(abs((ymax - ymin) / ncontbin)) xcontour = N.arange(xmin, (xmax), dx) ycontour = N.arange(ymin, (ymax), dy) A = N.zeros((int(ncontbin), int(ncontbin)), 'f') xbinnumb = N.array(len(x), 'f') ybinnumb = N.array(len(x), 'f') x1 = N.compress((x >= xmin) & (x <= xmax) & (y >= ymin) & (y <= ymax), x) y1 = N.compress((x >= xmin) & (x <= xmax) & (y >= ymin) & (y <= ymax), y) x = x1 y = y1 xbinnumb = ((x - xmin) * ncontbin / (xmax - xmin) ) #calculate x bin number for each point ybinnumb = ((y - ymin) * ncontbin / (ymax - ymin) ) #calculate x bin number for each point binID = zip(xbinnumb, ybinnumb) Amax = 0 for (i1, j1) in binID: i = int(i1) j = int(j1) if i > (ncontbin - 1): continue if j > (ncontbin - 1): continue #print i,j,A A[j, i] = A[j, i] + 1 #have to switch i,j in A[] to make contours look right #pylab.figure() #print A for i in range(int(ncontbin)): for j in range(int(ncontbin)): if A[j, i] > Amax: Amax = A[j, i] #Amax=max(max(A)) print "max of A = ", Amax V = N.array([.025, .1, .25, .5, .75, .95], 'f') V = V * Amax con = pylab.contour((xcontour + dx), (ycontour), A, V, alpha=5., colors='r', linewidth=5., hold='on')
def azhistsub(self,x,y,m1,color): m2=m1-2. y1=N.compress((x< m1) & (x > m2),y) (n,bin,patches)=pylab.hist(y1,bins=10,normed=1,fill=False,ec=color,fc='w',alpha=1.,visible=False) s=color+'-' s1=str(m1)+'> M > '+str(m2) pylab.plot(bin,n,s,label=s1)
def plotsighaall(sig, psig, o2b, file, nbin): o2b = N.array(o2b, 'f') sig = N.array(sig, 'f') psig = N.array(psig, 'f') #o2b=o2b+4. o2 = N.compress(o2b > -500., o2b) sig = N.compress(o2b > -500., sig) psig = N.compress(o2b > -500., psig) psplot = file + ".ps" psplotinit(psplot) #ppgplot.pgsch(0.7) ppgplot.pgslw(7) (sigbin, o2bin) = my.binit(sig, o2, nbin) #print 'dude', sigbin, o2bin sigbin = N.log10(sigbin) ppgplot.pgswin(-2., 2., -5., 20.) ppgplot.pgbox('blcnst', 0.0, 0.0, 'bcvnst', 0.0, 0.0) #tickmarks and labeling ppgplot.pgsch(1.0) ppgplot.pgmtxt('b', 2.5, 0.5, 0.5, "\gS\d10\u (gal/Mpc\u2\d)") #xlabel ppgplot.pgsch(1.2) ppgplot.pgmtxt('l', 2.6, 0.5, 0.5, 'EW(H\ga) (\(2078))') ppgplot.pgsls(1) #dotted ppgplot.pgslw(4) #line width ppgplot.pgpt(sigbin, o2bin, 17) ppgplot.pgpt(N.log10(sig), o2, 1) #my.errory(sigbin,o2bin,yerr) #print 'dude2', sigbin, o2bin ppgplot.pgsci(2) ppgplot.pgline(sigbin, o2bin) (sigbin, o2bin) = my.binit(psig, o2, nbin) #print 'dude', sigbin, o2bin sigbin = N.log10(sigbin) ppgplot.pgsci(1) ppgplot.pgpt(sigbin, o2bin, 21) #my.errory(sigbin,o2bin,yerr) ppgplot.pgsci(4) ppgplot.pgline(sigbin, o2bin) ppgplot.pgsci(1) ppgplot.pgend()
def plot_source_info(self, event): ra = event.xdata dec = event.ydata #print (event.key,ra,dec) if event.key == 's': #print self.constructed_pos[:,0] #print self.constructed_pos[:,1] dist = numarray.sqrt((self.constructed_pos[:, 0] - ra)**2 + (self.constructed_pos[:, 1] - dec)**2) #print dist #print "min distance = %f " % min(dist) the_source_ind = numarray.compress( dist == min(dist), numarray.fromlist(range(len(self.constructed_source_list)))) #print the_source_ind #the_source_ind = numarray.compress(dist == min(dist),numarray.arange(len(self.constructed_source_list))) the_source = self.constructed_source_list[the_source_ind[0]] print the_source dist = numarray.sqrt( (the_source.current_pos[0] - self.real_pos[:, 0])**2 + (the_source.current_pos[1] - self.real_pos[:, 1])**2) print "min distances to nearest real source = %f arcsec" % min( dist) the_source_ind = numarray.compress( dist == min(dist), numarray.fromlist(range(len(self.real_list)))) the_source = self.real_list[the_source_ind[0]] print "That real source is at ra=%f dec=%f" % ( the_source.start_pos[0], the_source.start_pos[1]) if event.key == 'r': dist = numarray.sqrt((self.real_pos[:, 0] - ra)**2 + (self.real_pos[:, 1] - dec)**2) #print dist #print "min distance = %f " % min(dist) the_source_ind = numarray.compress( dist == min(dist), numarray.fromlist(range(len(self.real_list)))) #print the_source_ind #the_source_ind = numarray.compress(dist == min(dist),numarray.arange(len(self.constructed_source_list))) the_source = self.real_list[the_source_ind[0]] print the_source
def itmean(arr,darr,thresh): arr=numarray.array(arr) darr=numarray.array(darr) print len(arr),len(darr) arr=numarray.compress(darr>0.,arr) darr=numarray.compress(darr>0.,darr) arr2=arr darr2=darr m=numarray.sum(arr/darr)/numarray.sum(1./darr) lold=0 l=len(arr) while(l!=lold): arr2=numarray.compress(abs(arr-m)<thresh,arr) darr2=numarray.compress(abs(arr-m)<thresh,darr) m=numarray.sum(arr2/darr2)/numarray.sum(1./darr2) lold=l l=len(arr2) print numarray.sum(arr/darr)/numarray.sum(1./darr), numarray.sum(arr2/darr2)/numarray.sum(1./darr2),len(arr2),len(arr) return numarray.sum(arr2/darr2)/numarray.sum(1./darr2)
def itmean(arr, darr, thresh): arr = numarray.array(arr) darr = numarray.array(darr) print len(arr), len(darr) arr = numarray.compress(darr > 0., arr) darr = numarray.compress(darr > 0., darr) arr2 = arr darr2 = darr m = numarray.sum(arr / darr) / numarray.sum(1. / darr) lold = 0 l = len(arr) while (l != lold): arr2 = numarray.compress(abs(arr - m) < thresh, arr) darr2 = numarray.compress(abs(arr - m) < thresh, darr) m = numarray.sum(arr2 / darr2) / numarray.sum(1. / darr2) lold = l l = len(arr2) print numarray.sum(arr / darr) / numarray.sum(1. / darr), numarray.sum( arr2 / darr2) / numarray.sum(1. / darr2), len(arr2), len(arr) return numarray.sum(arr2 / darr2) / numarray.sum(1. / darr2)
def azmr(self): x=N.compress((self.mpaflag > 0.1) & (self.ew > 4.) & (self.Mabs < -18.),self.Mabs) y=N.compress((self.mpaflag > 0.1) & (self.ew > 4.) & (self.Mabs < -18.),self.ar) x1=N.compress((self.mpaflag > 0.1) & (self.ew > 4.) & (self.Mabs < -20.38),self.Mabs) y1=N.compress((self.mpaflag > 0.1) & (self.ew > 4.) & (self.Mabs < -20.38),self.ar) y=2.5*N.log10(y) #pylab.plot(x,y,'k.',markersize=0.1,zorder=1) print "average Ar for Mr < -20.38 = %5.2f +/- %5.2f"%(N.average(y1),pylab.std(y1)) (xbin,ybin)=my.binit(x1,y1,20) #(xbin,ybin,ybinerr)=my.biniterr(x,y,20) for i in range(len(xbin)): print i,xbin[i],ybin[i] print "Average of binned values = ",N.average(ybin) print "average Ar for Mr < -20.38 = %5.2f +/- %5.2f"%(N.average(N.log10(y1)),pylab.std(N.log10(y1))) #pylab.axis([-26.,-12.,0.1,30.]) pylab.xlabel(r'$\rm{M_r}$',fontsize=28.) pylab.ylabel(r'$\rm{A_r}$',fontsize=28.) (xbin,ybin)=my.binit(x,y,20) #(xbin,ybin,ybinerr)=my.biniterr(x,y,20) for i in range(len(xbin)): print i,xbin[i],ybin[i] pylab.plot(xbin,ybin,'r-',lw=5) ax=pylab.gca() xmin=-24. xmax=-18. ymin=-1. ymax=3. my.contourf(x,y,xmin,xmax,ymin,ymax) pylab.axvline(x=-20.6,linewidth=3,ls='--',c='g') xl=N.arange(-23.,-20.5,.2) yl=0.76*N.ones(len(xl),'f') pylab.plot(xl,yl,'b-',lw=3) pylab.axis([-24.,-18,-1.,2.4]) #ax.set_yscale('log') #pylab.show() pylab.savefig('armr.eps') print "fraction w/MPA stellar mass and Az = ",N.sum(self.mpaflag)/(1.*len(self.mpaflag))
def azhist(self): x=N.compress((self.mpaflag > 0.1) & (self.ew > 4.),self.Mabs) y=N.compress((self.mpaflag > 0.1) & (self.ew > 4.),self.ar) y=2.5*N.log10(y) m1=-18. self.azhistsub(x,y,m1,'b') #m1=-19. #self.azhistsub(x,y,m1,'r') m1=-20. self.azhistsub(x,y,m1,'g') #m1=-21. #self.azhistsub(x,y,m1,'k') m1=-22. self.azhistsub(x,y,m1,'y') #m1=-23. #self.azhistsub(x,y,m1,'c') #pylab.show() #ax=pylab.gca() #ax.set_xscale('log') pylab.legend(loc='upper right') pylab.savefig('azhist.eps')
def itmed(arr,thresh): arr=numarray.array(arr) arr2=arr m=numarray.mlab.median(arr) lold=0 l=len(arr) while(l!=lold): arr2=numarray.compress(abs(arr-m)<thresh,arr) m=numarray.mlab.median(arr2) lold=l l=len(arr2) print numarray.mlab.median(arr), numarray.mlab.median(arr2),len(arr2),len(arr) return numarray.mlab.median(arr2)
def itmed(arr, thresh): arr = numarray.array(arr) arr2 = arr m = numarray.mlab.median(arr) lold = 0 l = len(arr) while (l != lold): arr2 = numarray.compress(abs(arr - m) < thresh, arr) m = numarray.mlab.median(arr2) lold = l l = len(arr2) print numarray.mlab.median(arr), numarray.mlab.median(arr2), len( arr2), len(arr) return numarray.mlab.median(arr2)
def biwt_func(x, y, sig, b): # Problems?!? aa = median(y - b*x) d = (y - aa - b*x) mad = median(N.absolute(d)) s = mad / 0.6745 d /= sig # biweight c = 6.0 f = d*(1-d**2/c**2)**2 sum = N.sum(N.compress(N.absolute(d) <= c, x*f)) # lorentzian #f = d/(1+0.5*d**2) #sum = N.sum(x*f) # MAD #small = 1.0e-8 #sign = N.compress(N.absolute(d) > small, d) #sign = sign / N.absolute(sign) #sum = N.sum(N.compress(N.absolute(d) > small, x)*sign) return sum, s, aa
def find_closest(input_array, target_array, tol): """ Find the set of elements in input_array that are closest to elements in target_array. Record the indices of the elements in target_array that are within tolerance, tol, of their closest match. Also record the indices of the elements in target_array that are outside tolerance, tol, of their match. For example, given an array of observations with irregular observation times along with an array of times of interest, this routine can be used to find those observations that are closest to the times of interest that are within a given time tolerance. NOTE: input_array must be sorted! The array, target_array, does not have to be sorted. Inputs: input_array: a sorted Float64 numarray target_array: a Float64 numarray tol: a tolerance Returns: closest_indices: the array of indices of elements in input_array that are closest to elements in target_array accept_indices: the indices of elements in target_array that have a match in input_array within tolerance reject_indices: the indices of elements in target_array that do not have a match in input_array within tolerance """ input_array_len = len(input_array) closest_indices = numarray.searchsorted(input_array, target_array) # determine the locations of target_array in input_array acc_rej_indices = [-1] * len(target_array) curr_tol = [tol] * len(target_array) est_tol = 0.0 for i in xrange(len(target_array)): best_off = 0 # used to adjust closest_indices[i] for best approximating element in input_array if closest_indices[i] >= input_array_len: # the value target_array[i] is >= all elements in input_array so check whether it is within tolerance of the last element closest_indices[i] = input_array_len - 1 est_tol = target_array[i] - input_array[closest_indices[i]] if est_tol < curr_tol[i]: curr_tol[i] = est_tol acc_rej_indices[i] = i elif target_array[i] == input_array[closest_indices[i]]: # target_array[i] is in input_array est_tol = 0.0 curr_tol[i] = 0.0 acc_rej_indices[i] = i elif closest_indices[i] == 0: # target_array[i] is <= all elements in input_array est_tol = input_array[0] - target_array[i] if est_tol < curr_tol[i]: curr_tol[i] = est_tol acc_rej_indices[i] = i else: # target_array[i] is between input_array[closest_indices[i]-1] and input_array[closest_indices[i]] # and closest_indices[i] must be > 0 top_tol = input_array[closest_indices[i]] - target_array[i] bot_tol = target_array[i] - input_array[closest_indices[i]-1] if bot_tol <= top_tol: est_tol = bot_tol best_off = -1 # this is the only place where best_off != 0 else: est_tol = top_tol if est_tol < curr_tol[i]: curr_tol[i] = est_tol acc_rej_indices[i] = i if est_tol <= tol: closest_indices[i] += best_off accept_indices = numarray.compress(numarray.greater(acc_rej_indices, -1), acc_rej_indices) reject_indices = numarray.compress(numarray.equal(acc_rej_indices, -1), numarray.arange(len(acc_rej_indices))) return (closest_indices, accept_indices, reject_indices)
def drawmeridians(self,ax,meridians,color='k',linewidth=1., \ linestyle='--',dashes=[1,1],labels=[0,0,0,0],\ font='rm',fontsize=12): """ draw meridians (longitude lines). ax - current axis instance. meridians - list containing longitude values to draw (in degrees). color - color to draw meridians (default black). linewidth - line width for meridians (default 1.) linestyle - line style for meridians (default '--', i.e. dashed). dashes - dash pattern for meridians (default [1,1], i.e. 1 pixel on, 1 pixel off). labels - list of 4 values (default [0,0,0,0]) that control whether meridians are labelled where they intersect the left, right, top or bottom of the plot. For example labels=[1,0,0,1] will cause meridians to be labelled where they intersect the left and bottom of the plot, but not the right and top. Labels are located with a precision of 0.1 degrees and are drawn using mathtext. font - mathtext font used for labels ('rm','tt','it' or 'cal', default 'rm'. fontsize - font size in points for labels (default 12). """ # don't draw meridians past latmax, always draw parallel at latmax. latmax = 80. # not used for cyl, merc projections. # offset for labels. yoffset = (self.urcrnry-self.llcrnry)/100./self.aspect xoffset = (self.urcrnrx-self.llcrnrx)/100. if self.projection not in ['merc','cyl']: lats = N.arange(-latmax,latmax+1).astype('f') else: lats = N.arange(-90,91).astype('f') xdelta = 0.1*(self.xmax-self.xmin) ydelta = 0.1*(self.ymax-self.ymin) for merid in meridians: lons = merid*N.ones(len(lats),'f') x,y = self(lons,lats) # remove points outside domain. testx = N.logical_and(x>=self.xmin-xdelta,x<=self.xmax+xdelta) x = N.compress(testx, x) y = N.compress(testx, y) testy = N.logical_and(y>=self.ymin-ydelta,y<=self.ymax+ydelta) x = N.compress(testy, x) y = N.compress(testy, y) if len(x) > 1 and len(y) > 1: # split into separate line segments if necessary. # (not necessary for mercator or cylindrical). xd = (x[1:]-x[0:-1])**2 yd = (y[1:]-y[0:-1])**2 dist = N.sqrt(xd+yd) split = dist > 500000. if N.sum(split) and self.projection not in ['merc','cyl']: ind = (N.compress(split,pylab.squeeze(split*N.indices(xd.shape)))+1).tolist() xl = [] yl = [] iprev = 0 ind.append(len(xd)) for i in ind: xl.append(x[iprev:i]) yl.append(y[iprev:i]) iprev = i else: xl = [x] yl = [y] # draw each line segment. for x,y in zip(xl,yl): # skip if only a point. if len(x) > 1 and len(y) > 1: l = Line2D(x,y,linewidth=linewidth,linestyle=linestyle) l.set_color(color) l.set_dashes(dashes) ax.add_line(l) # draw labels for meridians. # search along edges of map to see if parallels intersect. # if so, find x,y location of intersection and draw a label there. if self.projection == 'cyl': dx = 0.01; dy = 0.01 elif self.projection == 'merc': dx = 0.01; dy = 1000 else: dx = 1000; dy = 1000 for dolab,side in zip(labels,['l','r','t','b']): if not dolab: continue # for cyl or merc, don't draw meridians on left or right. if self.projection in ['cyl','merc'] and side in ['l','r']: continue if side in ['l','r']: nmax = int((self.ymax-self.ymin)/dy+1) if self.urcrnry < self.llcrnry: yy = self.llcrnry-dy*N.arange(nmax) else: yy = self.llcrnry+dy*N.arange(nmax) if side == 'l': lons,lats = self(self.llcrnrx*N.ones(yy.shape,'f'),yy,inverse=True) else: lons,lats = self(self.urcrnrx*N.ones(yy.shape,'f'),yy,inverse=True) lons = N.where(lons < 0, lons+360, lons) lons = [int(lon*10) for lon in lons.tolist()] lats = [int(lat*10) for lat in lats.tolist()] else: nmax = int((self.xmax-self.xmin)/dx+1) if self.urcrnrx < self.llcrnrx: xx = self.llcrnrx-dx*N.arange(nmax) else: xx = self.llcrnrx+dx*N.arange(nmax) if side == 'b': lons,lats = self(xx,self.llcrnry*N.ones(xx.shape,'f'),inverse=True) else: lons,lats = self(xx,self.urcrnry*N.ones(xx.shape,'f'),inverse=True) lons = N.where(lons < 0, lons+360, lons) lons = [int(lon*10) for lon in lons.tolist()] lats = [int(lat*10) for lat in lats.tolist()] for lon in meridians: if lon<0: lon=lon+360. # find index of meridian (there may be two, so # search from left and right). try: nl = lons.index(int(lon*10)) except: nl = -1 try: nr = len(lons)-lons[::-1].index(int(lon*10))-1 except: nr = -1 if lon>180: lonlab = r'$\%s{%g\/^{\circ}\/W}$'%(font,N.fabs(lon-360)) elif lon<180 and lon != 0: lonlab = r'$\%s{%g\/^{\circ}\/E}$'%(font,lon) else: lonlab = r'$\%s{%g\/^{\circ}}$'%(font,lon) # meridians can intersect each map edge twice. for i,n in enumerate([nl,nr]): lat = lats[n]/10. # no meridians > latmax for projections other than merc,cyl. if self.projection not in ['merc','cyl'] and lat > latmax: continue # don't bother if close to the first label. if i and abs(nr-nl) < 100: continue if n > 0: if side == 'l': pylab.text(self.llcrnrx-xoffset,yy[n],lonlab,horizontalalignment='right',verticalalignment='center',fontsize=fontsize) elif side == 'r': pylab.text(self.urcrnrx+xoffset,yy[n],lonlab,horizontalalignment='left',verticalalignment='center',fontsize=fontsize) elif side == 'b': pylab.text(xx[n],self.llcrnry-yoffset,lonlab,horizontalalignment='center',verticalalignment='top',fontsize=fontsize) else: pylab.text(xx[n],self.urcrnry+yoffset,lonlab,horizontalalignment='center',verticalalignment='bottom',fontsize=fontsize) # make sure axis ticks are turned off ax.set_xticks([]) ax.set_yticks([])
def drawmeridians(self,ax,meridians,color='k',linewidth=1., \ linestyle='--',dashes=[1,1],labels=[0,0,0,0],\ font='rm',fontsize=12): """ draw meridians (longitude lines). ax - current axis instance. meridians - list containing longitude values to draw (in degrees). color - color to draw meridians (default black). linewidth - line width for meridians (default 1.) linestyle - line style for meridians (default '--', i.e. dashed). dashes - dash pattern for meridians (default [1,1], i.e. 1 pixel on, 1 pixel off). labels - list of 4 values (default [0,0,0,0]) that control whether meridians are labelled where they intersect the left, right, top or bottom of the plot. For example labels=[1,0,0,1] will cause meridians to be labelled where they intersect the left and bottom of the plot, but not the right and top. Labels are located with a precision of 0.1 degrees and are drawn using mathtext. font - mathtext font used for labels ('rm','tt','it' or 'cal', default 'rm'. fontsize - font size in points for labels (default 12). """ # don't draw meridians past latmax, always draw parallel at latmax. latmax = 80. # not used for cyl, merc projections. # offset for labels. yoffset = (self.urcrnry - self.llcrnry) / 100. / self.aspect xoffset = (self.urcrnrx - self.llcrnrx) / 100. if self.projection not in ['merc', 'cyl']: lats = N.arange(-latmax, latmax + 1).astype('f') else: lats = N.arange(-90, 91).astype('f') xdelta = 0.1 * (self.xmax - self.xmin) ydelta = 0.1 * (self.ymax - self.ymin) for merid in meridians: lons = merid * N.ones(len(lats), 'f') x, y = self(lons, lats) # remove points outside domain. testx = N.logical_and(x >= self.xmin - xdelta, x <= self.xmax + xdelta) x = N.compress(testx, x) y = N.compress(testx, y) testy = N.logical_and(y >= self.ymin - ydelta, y <= self.ymax + ydelta) x = N.compress(testy, x) y = N.compress(testy, y) if len(x) > 1 and len(y) > 1: # split into separate line segments if necessary. # (not necessary for mercator or cylindrical). xd = (x[1:] - x[0:-1])**2 yd = (y[1:] - y[0:-1])**2 dist = N.sqrt(xd + yd) split = dist > 500000. if N.sum(split) and self.projection not in ['merc', 'cyl']: ind = (N.compress( split, pylab.squeeze(split * N.indices(xd.shape))) + 1).tolist() xl = [] yl = [] iprev = 0 ind.append(len(xd)) for i in ind: xl.append(x[iprev:i]) yl.append(y[iprev:i]) iprev = i else: xl = [x] yl = [y] # draw each line segment. for x, y in zip(xl, yl): # skip if only a point. if len(x) > 1 and len(y) > 1: l = Line2D(x, y, linewidth=linewidth, linestyle=linestyle) l.set_color(color) l.set_dashes(dashes) ax.add_line(l) # draw labels for meridians. # search along edges of map to see if parallels intersect. # if so, find x,y location of intersection and draw a label there. if self.projection == 'cyl': dx = 0.01 dy = 0.01 elif self.projection == 'merc': dx = 0.01 dy = 1000 else: dx = 1000 dy = 1000 for dolab, side in zip(labels, ['l', 'r', 't', 'b']): if not dolab: continue # for cyl or merc, don't draw meridians on left or right. if self.projection in ['cyl', 'merc'] and side in ['l', 'r']: continue if side in ['l', 'r']: nmax = int((self.ymax - self.ymin) / dy + 1) if self.urcrnry < self.llcrnry: yy = self.llcrnry - dy * N.arange(nmax) else: yy = self.llcrnry + dy * N.arange(nmax) if side == 'l': lons, lats = self(self.llcrnrx * N.ones(yy.shape, 'f'), yy, inverse=True) else: lons, lats = self(self.urcrnrx * N.ones(yy.shape, 'f'), yy, inverse=True) lons = N.where(lons < 0, lons + 360, lons) lons = [int(lon * 10) for lon in lons.tolist()] lats = [int(lat * 10) for lat in lats.tolist()] else: nmax = int((self.xmax - self.xmin) / dx + 1) if self.urcrnrx < self.llcrnrx: xx = self.llcrnrx - dx * N.arange(nmax) else: xx = self.llcrnrx + dx * N.arange(nmax) if side == 'b': lons, lats = self(xx, self.llcrnry * N.ones(xx.shape, 'f'), inverse=True) else: lons, lats = self(xx, self.urcrnry * N.ones(xx.shape, 'f'), inverse=True) lons = N.where(lons < 0, lons + 360, lons) lons = [int(lon * 10) for lon in lons.tolist()] lats = [int(lat * 10) for lat in lats.tolist()] for lon in meridians: if lon < 0: lon = lon + 360. # find index of meridian (there may be two, so # search from left and right). try: nl = lons.index(int(lon * 10)) except: nl = -1 try: nr = len(lons) - lons[::-1].index(int(lon * 10)) - 1 except: nr = -1 if lon > 180: lonlab = r'$\%s{%g\/^{\circ}\/W}$' % (font, N.fabs(lon - 360)) elif lon < 180 and lon != 0: lonlab = r'$\%s{%g\/^{\circ}\/E}$' % (font, lon) else: lonlab = r'$\%s{%g\/^{\circ}}$' % (font, lon) # meridians can intersect each map edge twice. for i, n in enumerate([nl, nr]): lat = lats[n] / 10. # no meridians > latmax for projections other than merc,cyl. if self.projection not in ['merc', 'cyl'] and lat > latmax: continue # don't bother if close to the first label. if i and abs(nr - nl) < 100: continue if n > 0: if side == 'l': pylab.text(self.llcrnrx - xoffset, yy[n], lonlab, horizontalalignment='right', verticalalignment='center', fontsize=fontsize) elif side == 'r': pylab.text(self.urcrnrx + xoffset, yy[n], lonlab, horizontalalignment='left', verticalalignment='center', fontsize=fontsize) elif side == 'b': pylab.text(xx[n], self.llcrnry - yoffset, lonlab, horizontalalignment='center', verticalalignment='top', fontsize=fontsize) else: pylab.text(xx[n], self.urcrnry + yoffset, lonlab, horizontalalignment='center', verticalalignment='bottom', fontsize=fontsize) # make sure axis ticks are turned off ax.set_xticks([]) ax.set_yticks([])
def find_closest(input_array, target_array, tol): """ Find the set of elements in input_array that are closest to elements in target_array. Record the indices of the elements in target_array that are within tolerance, tol, of their closest match. Also record the indices of the elements in target_array that are outside tolerance, tol, of their match. For example, given an array of observations with irregular observation times along with an array of times of interest, this routine can be used to find those observations that are closest to the times of interest that are within a given time tolerance. NOTE: input_array must be sorted! The array, target_array, does not have to be sorted. Inputs: input_array: a sorted Float64 numarray target_array: a Float64 numarray tol: a tolerance Returns: closest_indices: the array of indices of elements in input_array that are closest to elements in target_array accept_indices: the indices of elements in target_array that have a match in input_array within tolerance reject_indices: the indices of elements in target_array that do not have a match in input_array within tolerance """ input_array_len = len(input_array) closest_indices = numarray.searchsorted( input_array, target_array) # determine the locations of target_array in input_array acc_rej_indices = [-1] * len(target_array) curr_tol = [tol] * len(target_array) est_tol = 0.0 for i in xrange(len(target_array)): best_off = 0 # used to adjust closest_indices[i] for best approximating element in input_array if closest_indices[i] >= input_array_len: # the value target_array[i] is >= all elements in input_array so check whether it is within tolerance of the last element closest_indices[i] = input_array_len - 1 est_tol = target_array[i] - input_array[closest_indices[i]] if est_tol < curr_tol[i]: curr_tol[i] = est_tol acc_rej_indices[i] = i elif target_array[i] == input_array[closest_indices[i]]: # target_array[i] is in input_array est_tol = 0.0 curr_tol[i] = 0.0 acc_rej_indices[i] = i elif closest_indices[i] == 0: # target_array[i] is <= all elements in input_array est_tol = input_array[0] - target_array[i] if est_tol < curr_tol[i]: curr_tol[i] = est_tol acc_rej_indices[i] = i else: # target_array[i] is between input_array[closest_indices[i]-1] and input_array[closest_indices[i]] # and closest_indices[i] must be > 0 top_tol = input_array[closest_indices[i]] - target_array[i] bot_tol = target_array[i] - input_array[closest_indices[i] - 1] if bot_tol <= top_tol: est_tol = bot_tol best_off = -1 # this is the only place where best_off != 0 else: est_tol = top_tol if est_tol < curr_tol[i]: curr_tol[i] = est_tol acc_rej_indices[i] = i if est_tol <= tol: closest_indices[i] += best_off accept_indices = numarray.compress(numarray.greater(acc_rej_indices, -1), acc_rej_indices) reject_indices = numarray.compress(numarray.equal(acc_rej_indices, -1), numarray.arange(len(acc_rej_indices))) return (closest_indices, accept_indices, reject_indices)
To avoid repeated sqrt calculations the discard is based on an enclosing square. ''' global lengthRemaining, trial lengthRemaining+= [[P.shape[0]]] Pz= P - p # zero based neighbourhood while len(Pz): Pt= Pz[0] # trial value Pta= N.abs(Pt) Pz= Pz[1:] pd= math.sqrt(N.dot(Pta, Pta)) # distance of p from the trial value goodCases= N.logical_and((Pz < pd), (Pz > -pd))# use the enclosing square goodCases= N.logical_and.reduce(goodCases, 1) Pz= N.compress(goodCases, Pz) # discard points outside the square if len(Pz) == 1: Pt= Pz[0] # We have found the closest Pz= [] lengthRemaining[trial]+= [len(Pz)] z= 100 trial+= 1 return Pt + p if __name__ == '__main__': for sampleSize in range(100, 5000, 100): P= R.random(shape= (sampleSize, 2)) for i in range(20): p= R.random((1, 2)) # point a= find(P, p) ## print 'Closest neighbour:', a[0]
def cullmembers(self): self.z = N.compress(self.memb > 0, self.z) self.ra = N.compress(self.memb > 0, self.ra) self.dec = N.compress(self.memb > 0, self.dec) self.distBCG = N.compress(self.memb > 0, self.distBCG) self.distBCGR200 = N.compress(self.memb > 0, self.distBCGR200) self.dz = N.compress(self.memb > 0, self.dz) self.o2 = N.compress(self.memb > 0, self.o2) self.erro2 = N.compress(self.memb > 0, self.erro2) self.u = N.compress(self.memb > 0, self.u) self.g = N.compress(self.memb > 0, self.g) self.r = N.compress(self.memb > 0, self.r) self.i = N.compress(self.memb > 0, self.i) self.zm = N.compress(self.memb > 0, self.zm) self.sf = N.compress(self.memb > 0, self.sf) self.tot = N.compress(self.memb > 0, self.tot) self.memb = N.compress(self.memb > 0, self.memb)
def gotoit(): nbin = 10 #c=Cluster() #g=Galaxy() clusterfile = "clusters.spec.dat" print "reading in cluster file to get cluster parameters" c.creadfiles(clusterfile) print "got ", len(c.z), " clusters" c.convarray() c.Kcorr() go2 = [] #combined arrays containing all galaxies gsf = [] #combined arrays containing all galaxies gsig5 = [] gsig10 = [] gsig52r200 = [] #spec catalogs extended out to 2xR200 gsig102r200 = [] #spec catalogs extended out to 2xR200 gsig5phot = [] gsig10phot = [] sgo2 = [] #combined arrays containing all galaxies sgha = [] #combined arrays containing all galaxies sgsf = [] #combined arrays containing all galaxies sgsig5 = [] sgsig10 = [] sgsig52r200 = [] #spec catalogs extended out to 2xR200 sgsig102r200 = [] #spec catalogs extended out to 2xR200 sgsig5phot = [] sgsig10phot = [] if (mode < 1): c.getsdssphotcats() c.getsdssspeccats() gr = [] #list of median g-r colors psplotinit('summary.ps') x1 = .1 x2 = .45 x3 = .6 x4 = .95 y1 = .15 y2 = .45 y3 = .55 y4 = .85 ppgplot.pgsch(1.2) #font size ppgplot.pgslw(2) #for i in range(len(c.z)): cl = [10] (xl, xu, yl, yu) = ppgplot.pgqvp(0) print "viewport = ", xl, xu, yl, yu complall = [] for i in range(len(c.z)): #for i in cl: gname = "g" + str(i) gname = Galaxy() gspecfile = "abell" + str(c.id[i]) + ".spec.dat" gname.greadfiles(gspecfile, i) print "number of members = ", len(gname.z) if len(gname.z) < 10: print "less than 10 members", len(gname.z) continue gname.convarray() #gname.cullmembers() #gname.getmemb()#get members w/in R200 #gr.append(N.average(gname.g-gname.r)) gspec2r200file = "abell" + str(c.id[i]) + ".spec2r200.dat" gname.greadspecfiles(gspec2r200file, c.dL[i], c.kcorr[i], i) print i, c.id[i], " getnearest, first call", len(gname.ra), len( gname.sra), sum(gname.smemb) #gname.getnearest(i) (gname.sig52r200, gname.sig102r200) = gname.getnearestgen( gname.ra, gname.dec, gname.sra, gname.sdec, i ) #measure distances from ra1, dec1 to members in catalog ra2, dec2 sig52r200 = N.compress(gname.memb > 0, gname.sig52r200) gsig52r200[len(gsig5phot):] = sig52r200 sig102r200 = N.compress(gname.memb > 0, gname.sig102r200) gsig102r200[len(gsig10phot):] = sig102r200 gphotfile = "abell" + str(c.id[i]) + ".phot.dat" gname.greadphotfiles(gphotfile, c.dL[i], c.kcorr[i]) gname.getnearest(i) #print "len of local density arrays = ",len(gname.sig5),len(gname.sig5phot) #print gspecfile, c.z[i],c.kcorr[i] (ds5, ds10) = gname.gwritefiles(gspecfile, i) o2 = N.compress(gname.memb > 0, gname.o2) go2[len(go2):] = o2 sf = N.compress(gname.memb > 0, gname.sf) gsf[len(gsf):] = sf sig5 = N.compress(gname.memb > 0, gname.sig5) gsig5[len(gsig5):] = sig5 sig10 = N.compress(gname.memb > 0, gname.sig10) gsig10[len(gsig10):] = sig10 sig5phot = N.compress(gname.memb > 0, gname.sig5phot) gsig5phot[len(gsig5phot):] = sig5phot sig10phot = N.compress(gname.memb > 0, gname.sig10phot) gsig10phot[len(gsig10phot):] = sig10phot ds5 = N.array(ds5, 'f') ds10 = N.array(ds10, 'f') #print len(ds5),len(ds10) #ppgplot.pgsvp(xl,xu,yl,yu) ppgplot.pgsvp(0.1, .9, .08, .92) ppgplot.pgslw(7) label = 'Abell ' + str( c.id[i]) + ' (z=%5.2f, \gs=%3.0f km/s)' % (c.z[i], c.sigma[i]) ppgplot.pgtext(0., 1., label) ppgplot.pgslw(2) ppgplot.pgsvp(x1, x2, y1, y2) #sets viewport #ppgplot.pgbox("",0.0,0,"",0.0) ppgplot.pgswin(-1., 3., -1., 3.) #axes limits ppgplot.pgbox('bcnst', 1, 2, 'bcvnst', 1, 2) #tickmarks and labeling ppgplot.pgmtxt('b', 2.5, 0.5, 0.5, "\gS\d10\u(phot) (gal/Mpc\u2\d)") #xlabel ppgplot.pgmtxt('l', 2.6, 0.5, 0.5, "\gS\d10\u(spec) (gal/Mpc\u2\d)") x = N.arange(-5., 10., .1) y = x ppgplot.pgsls(1) #dotted ppgplot.pgslw(4) #line width ppgplot.pgline(x, y) x = N.log10(sig10phot) y = N.log10(sig10) ppgplot.pgsch(.7) ppgplot.pgpt(x, y, 17) xp = N.array([-0.5], 'f') yp = N.array([2.5], 'f') ppgplot.pgpt(xp, yp, 17) ppgplot.pgtext((xp + .1), yp, 'spec(1.2xR200) vs phot') ppgplot.pgsci(4) xp = N.array([-0.5], 'f') yp = N.array([2.2], 'f') ppgplot.pgpt(xp, yp, 21) ppgplot.pgtext((xp + .1), yp, 'spec(2xR200) vs phot') y = N.log10(sig102r200) ppgplot.pgsch(.9) ppgplot.pgpt(x, y, 21) ppgplot.pgsch(1.2) ppgplot.pgslw(2) #line width ppgplot.pgsci(1) #ppgplot.pgenv(-200.,200.,-1.,20.,0,0) #ppgplot.pgsci(2) #ppgplot.pghist(len(ds5),ds5,-200.,200.,30,1) #ppgplot.pgsci(4) #ppgplot.pghist(len(ds10),ds10,-200.,200.,30,1) #ppgplot.pgsci(1) #ppgplot.pglab("\gD\gS","Ngal",gspecfile) #ppgplot.pgpanl(1,2) g = N.compress(gname.memb > 0, gname.g) r = N.compress(gname.memb > 0, gname.r) V = N.compress(gname.memb > 0, gname.V) dmag = N.compress(gname.memb > 0, gname.dmagnearest) dnearest = N.compress(gname.memb > 0, gname.nearest) dz = N.compress(gname.memb > 0, gname.dz) #ppgplot.pgsvp(x3,x4,y1,y2) #sets viewport #ppgplot.pgenv(-.5,3.,-1.,5.,0,0) #ppgplot.pgpt((g-V),(g-r),17) #ppgplot.pgsci(1) #ppgplot.pglab("g - M\dV\u",'g-r',gspecfile) ppgplot.pgsvp(x1, x2, y3, y4) #sets viewport #ppgplot.pgbox("",0.0,0,"",0.0) ppgplot.pgswin( (c.ra[i] + 2. * c.r200deg[i] / N.cos(c.dec[i] * N.pi / 180.)), (c.ra[i] - 2 * c.r200deg[i] / N.cos(c.dec[i] * N.pi / 180.)), (c.dec[i] - 2. * c.r200deg[i]), (c.dec[i] + 2. * c.r200deg[i])) ppgplot.pgbox('bcnst', 0.0, 0.0, 'bcvnst', 0.0, 0.0) #tickmarks and labeling ppgplot.pgmtxt('b', 2.5, 0.5, 0.5, "RA") #xlabel ppgplot.pgmtxt('l', 2.6, 0.5, 0.5, "Dec") #ppgplot.pglab("RA",'Dec',gspecfile) ppgplot.pgsfs(2) ppgplot.pgcirc(c.ra[i], c.dec[i], c.r200deg[i]) ppgplot.pgsls(4) ppgplot.pgcirc(c.ra[i], c.dec[i], 1.2 * c.r200deg[i]) ppgplot.pgsls(1) #ppgplot.pgcirc(c.ra[i],c.dec[i],c.r200deg[i]/N.cos(c.dec[i]*N.pi/180.)) ppgplot.pgsci(2) ppgplot.pgpt(gname.ra, gname.dec, 17) ppgplot.pgsci(4) ppgplot.pgpt(gname.photra, gname.photdec, 21) ppgplot.pgsci(1) #calculate completeness w/in R200 dspec = N.sqrt((gname.ra - c.ra[i])**2 + (gname.dec - c.dec[i])**2) dphot = N.sqrt((gname.photra - c.ra[i])**2 + (gname.photdec - c.dec[i])**2) nphot = 1. * len(N.compress(dphot < c.r200deg[i], dphot)) nspec = 1. * len(N.compress(dspec < c.r200deg[i], dspec)) s = "Completeness for cluster Abell %s = %6.2f (nspec=%6.1f,nphot= %6.1f)" % ( str(c.id[i]), float(nspec / nphot), nspec, nphot) print s complall.append(float(nspec / nphot)) ppgplot.pgsvp(x3, x4, y3, y4) #sets viewport #ppgplot.pgsvp(x1,x2,y3,y4) #sets viewport #ppgplot.pgbox("",0.0,0,"",0.0) ppgplot.pgswin(-0.005, .05, -1., 1.) ppgplot.pgbox('bcnst', .02, 2, 'bcvnst', 1, 4) #tickmarks and labeling ppgplot.pgsch(1.0) ppgplot.pgmtxt('b', 2.5, 0.5, 0.5, "Dist to nearest phot neighbor (deg)") #xlabel ppgplot.pgsch(1.2) ppgplot.pgmtxt('l', 2.6, 0.5, 0.5, 'M\dV\u(phot) - M\dV\u(spec)') ppgplot.pgsci(2) ppgplot.pgpt(dnearest, dmag, 17) ppgplot.pgsci(1) x = N.arange(-30., 30., 1.) y = 0 * x ppgplot.pgsci(1) ppgplot.pgsls(2) ppgplot.pgline(x, y) ppgplot.pgsls(1) ppgplot.pgsci(1) dm = N.compress(dnearest < 0.01, dmag) std = '%5.3f (%5.3f)' % (pylab.mean(dm), pylab.std(dm)) #ppgplot.pgslw(7) #label='Abell '+str(c.id[i]) #ppgplot.pgtext(0.,1.,label) ppgplot.pgslw(2) label = '\gDM\dV\u(err) = ' + std ppgplot.pgsch(.9) ppgplot.pgtext(0., .8, label) #label = "z = %5.2f"%(c.z[i]) #ppgplot.pgtext(0.,.8,label) ppgplot.pgsch(1.2) #ppgplot.pgsvp(x3,x4,y3,y4) #sets viewport #ppgplot.pgenv(-.15,.15,-3.,3.,0,0) #ppgplot.pgsci(2) #ppgplot.pgpt(dz,dmag,17) #ppgplot.pgsci(1) #ppgplot.pglab("z-z\dcl\u",'\gD Mag',gspecfile) ppgplot.pgsvp(x3, x4, y1, y2) #sets viewport ppgplot.pgswin(-3., 3., -1., 1.) ppgplot.pgbox('bcnst', 1, 2, 'bcvnst', 1, 4) #tickmarks and labeling ppgplot.pgmtxt('b', 2.5, 0.5, 0.5, "\gDv/\gs") #xlabel ppgplot.pgmtxt('l', 2.6, 0.5, 0.5, 'M\dV\u(phot) - M\dV\u(spec)') ppgplot.pgsci(2) dv = dz / (1 + c.z[i]) * 3.e5 / c.sigma[i] ppgplot.pgpt(dv, dmag, 17) ppgplot.pgsci(1) x = N.arange(-30., 30., 1.) y = 0 * x ppgplot.pgsci(1) ppgplot.pgsls(2) ppgplot.pgline(x, y) ppgplot.pgsls(1) ppgplot.pgsci(1) #ppgplot.pgsvp(x1,x2,y1,y2) #sets viewport #ppgplot.pgenv(0.,3.5,-3.,3.,0,0) #ppgplot.pgsci(4) #ppgplot.pgpt((g-r),dmag,17) #ppgplot.pgsci(1) #ppgplot.pglab("g-r",'\gD Mag',gspecfile) #ppgplot.pgsvp(x1,x2,y1,y2) #sets viewport #ppgplot.pgenv(-25.,-18.,-1.,1.,0,0) #ppgplot.pgsci(4) #ppgplot.pgpt((V),dmag,17) #x=N.arange(-30.,30.,1.) #y=0*x #ppgplot.pgsci(1) #ppgplot.pgsls(2) #ppgplot.pgline(x,y) #ppgplot.pgsls(1) #ppgplot.pgsci(1) #ppgplot.pglab("M\dV\u(spec)",'M\dV\u(phot) - M\dV\u(spec)',gspecfile) #ppgplot.pgpage() #ppgplot.pgpage() #combine galaxy data ppgplot.pgpage() (sssig5, sssig10) = gname.getnearestgen(gname.sra, gname.sdec, gname.sra, gname.sdec, i) #get spec-spec local density (spsig5, spsig10) = gname.getnearestgen(gname.sra, gname.sdec, gname.photra, gname.photdec, i) #get spec-phot local density o2 = N.compress(gname.smemb > 0, gname.so2) sgo2[len(sgo2):] = o2 ha = N.compress(gname.smemb > 0, gname.sha) sgha[len(sgha):] = ha sf = N.compress(gname.smemb > 0, gname.ssf) sgsf[len(sgsf):] = sf sig5 = N.compress(gname.smemb > 0, sssig5) sgsig5[len(sgsig5):] = sig5 sig10 = N.compress(gname.smemb > 0, sssig10) sgsig10[len(sgsig10):] = sig10 sig5phot = N.compress(gname.smemb > 0, spsig5) sgsig5phot[len(sgsig5phot):] = sig5phot sig10phot = N.compress(gname.smemb > 0, spsig10) sgsig10phot[len(sgsig10phot):] = sig10phot #gr=N.array(gr,'f') #c.assigncolor(gr) #for i in range(len(c.z)): # print c.id[i],c.z[i],c.r200[i],c.r200deg[i] print "Average Completeness w/in R200 = ", N.average(N.array( complall, 'f')) print "sig o2", len(gsig10), len(gsig10phot), len(go2) print "sig o2 large", len(sgsig10), len(sgsig10phot), len(sgo2) plotsigo2all(gsig10, gsig10phot, go2, 'o2vsig10spec', nbin) #plotsigo2(gsig5phot,-1*go2,'o2vsig5phot',nbin) plotsigsff(gsig5, gsf, 'sffvsig5spec', nbin) #sf frac versus sigma plotsigsff(gsig5phot, gsf, 'sffvsig5phot', nbin) #sf frac versus sigma plotsigsffall(gsig5, gsig5phot, gsf, 'sffvsig5all', nbin) #sf frac versus sigma plotsig10sffall(gsig10, gsig10phot, gsf, 'sffvsig10all', nbin) #sf frac versus sigma #plotsighaall(gsig10,gsig10phot,gha,'havsig10spec',20) #plotsigo2all(sgsig10,sgsig10phot,sgo2,'o2vsig10spec.large',30) plotsighaall(sgsig10, sgsig10phot, sgha, 'havsig10spec.large', 10) #plotsigsffall(sgsig5,sgsig5phot,sgsf,'sffvsig5.large',nbin)#sf frac versus sigma #plotsig10sffall(sgsig10,sgsig10phot,sgsf,'sffvsig10.large',nbin)#sf frac versus sigma psplotinit('one2one.ps') ppgplot.pgenv(-1.5, 2.5, -1.5, 2.5, 0) ppgplot.pglab("\gS\d10\u(phot) (gal/Mpc\u2\d)", "\gS\d10\u(spec) (gal/Mpc\u2\d)", "") x = N.arange(-5., 10., .1) y = x ppgplot.pgsls(1) #dotted ppgplot.pgslw(4) #line width ppgplot.pgline(x, y) x = N.log10(gsig10phot) y = N.log10(gsig10) ppgplot.pgsch(.7) ppgplot.pgpt(x, y, 17) ppgplot.pgsch(1.) ppgplot.pgsci(1) ppgplot.pgend()
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)
def stellarmr(self): pylab.cla() x=N.compress((self.mpaflag > 0.1),self.Mabs) y=N.compress((self.mpaflag > 0.1),self.stellarmass) print "len x,y,Mabs = ",len(x),len(y),len(self.Mabs) #x=[] #y=[] #for i in range(len(self.Mabs)): # if self.mpaflag[i] > 0.1: # x.append(self.Mabs[i]) # y.append(self.stellarmass[i]) #print "len x,y,Mabs =",len(x),len(y),len(self.Mabs) #x=N.array(x,'d') #y=N.array(y,'d') y=N.log10(y/1.e11) xmin=-24. xmax=-18. ymin=-3. ymax=2. #my.contour(x,y,xmin,xmax,ymin,ymax) #pylab.plot(x,y,'k.',markersize=.01,zorder=1) pylab.xlabel(r'$\rm{M_r}$',fontsize=28.) pylab.ylabel(r'$\rm{log_{10}(M_* / 10^{11} \ M_\odot)}$',fontsize=28.) #(a,b)=fitpowerlaw(x,y) xtrans=-21.5 b=-13.8 m=-.63 xl=N.arange(xtrans,-18.,.05) xl=N.array(xl,'f') yl=N.zeros(len(xl),'f') yl=m*(xl)+b #xl=-1.*xl #print xl #print yl pylab.plot(xl,yl,'b-',lw=4,label='_nolegend_') m2=-.47 b2=(m-m2)*xtrans+b print "b2 = ",b2 xl=N.arange(-24.,(xtrans+.05),.05) xl=N.array(xl,'f') yl=N.zeros(len(xl),'f') yl=m2*(xl)+b2 #xl=-1.*xl #print xl #print yl pylab.plot(xl,yl,'c-',lw=4,label='_nolegend_') pylab.axvline(x=-20.6,linewidth=3,ls='--',c='g') #(xbin,ybin,ybinerr)=my.binitbinsfix(xl,(xl+0.5),x,y) #pylab.plot(xbin,ybin,'b-',linewidth=4.) pylab.hold() my.contourf(x,y,xmin,xmax,ymin,ymax) ax=pylab.gca() #ax.set_yscale('log') pylab.axis([-24.5,-17.5,-3.,1.2]) pylab.savefig('stellarmr.eps')