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
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
def __call__(self, x_new): """Find linearly interpolated y_new = <name>(x_new). Inputs: x_new -- New independent variables. Outputs: y_new -- Linearly interpolated values corresponding to x_new. """ # 1. Handle values in x_new that are outside of x. Throw error, # or return a list of mask array indicating the outofbounds values. # The behavior is set by the bounds_error variable. ## RHC -- was x_new = atleast_1d(x_new) x_new_1d = atleast_1d(x_new) out_of_bounds = self._check_bounds(x_new_1d) # 2. Find where in the orignal data, the values to interpolate # would be inserted. # Note: If x_new[n] = x[m], then m is returned by searchsorted. x_new_indices = searchsorted(self.x, x_new_1d) # 3. Clip x_new_indices so that they are within the range of # self.x indices and at least 1. Removes mis-interpolation # of x_new[n] = x[0] # RHC -- changed Int to Numeric_Int to avoid name clash with numarray x_new_indices = clip(x_new_indices, 1, len(self.x) - 1).astype(Numeric_Int) # 4. Calculate the slope of regions that each x_new value falls in. lo = x_new_indices - 1 hi = x_new_indices # !! take() should default to the last axis (IMHO) and remove # !! the extra argument. x_lo = take(self.x, lo, axis=self.interp_axis) x_hi = take(self.x, hi, axis=self.interp_axis) y_lo = take(self.y, lo, axis=self.interp_axis) y_hi = take(self.y, hi, axis=self.interp_axis) slope = (y_hi - y_lo) / (x_hi - x_lo) # 5. Calculate the actual value for each entry in x_new. y_new = slope * (x_new_1d - x_lo) + y_lo # 6. Fill any values that were out of bounds with NaN # !! Need to think about how to do this efficiently for # !! mutli-dimensional Cases. yshape = y_new.shape y_new = y_new.flat new_shape = list(yshape) new_shape[self.interp_axis] = 1 sec_shape = [1] * len(new_shape) sec_shape[self.interp_axis] = len(out_of_bounds) out_of_bounds.shape = sec_shape new_out = ones(new_shape) * out_of_bounds putmask(y_new, new_out.flat, self.fill_value) y_new.shape = yshape # Rotate the values of y_new back so that they correspond to the # correct x_new values. result = swapaxes(y_new, self.interp_axis, self.axis) try: len(x_new) return result except TypeError: return result[0] return result
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
def __call__(self,x_new): """Find linearly interpolated y_new = <name>(x_new). Inputs: x_new -- New independent variables. Outputs: y_new -- Linearly interpolated values corresponding to x_new. """ # 1. Handle values in x_new that are outside of x. Throw error, # or return a list of mask array indicating the outofbounds values. # The behavior is set by the bounds_error variable. ## RHC -- was x_new = atleast_1d(x_new) x_new_1d = atleast_1d(x_new) out_of_bounds = self._check_bounds(x_new_1d) # 2. Find where in the orignal data, the values to interpolate # would be inserted. # Note: If x_new[n] = x[m], then m is returned by searchsorted. x_new_indices = searchsorted(self.x,x_new_1d) # 3. Clip x_new_indices so that they are within the range of # self.x indices and at least 1. Removes mis-interpolation # of x_new[n] = x[0] # RHC -- changed Int to Numeric_Int to avoid name clash with numarray x_new_indices = clip(x_new_indices,1,len(self.x)-1).astype(Numeric_Int) # 4. Calculate the slope of regions that each x_new value falls in. lo = x_new_indices - 1; hi = x_new_indices # !! take() should default to the last axis (IMHO) and remove # !! the extra argument. x_lo = take(self.x,lo,axis=self.interp_axis) x_hi = take(self.x,hi,axis=self.interp_axis) y_lo = take(self.y,lo,axis=self.interp_axis) y_hi = take(self.y,hi,axis=self.interp_axis) slope = (y_hi-y_lo)/(x_hi-x_lo) # 5. Calculate the actual value for each entry in x_new. y_new = slope*(x_new_1d-x_lo) + y_lo # 6. Fill any values that were out of bounds with NaN # !! Need to think about how to do this efficiently for # !! mutli-dimensional Cases. yshape = y_new.shape y_new = y_new.flat new_shape = list(yshape) new_shape[self.interp_axis] = 1 sec_shape = [1]*len(new_shape) sec_shape[self.interp_axis] = len(out_of_bounds) out_of_bounds.shape = sec_shape new_out = ones(new_shape)*out_of_bounds putmask(y_new, new_out.flat, self.fill_value) y_new.shape = yshape # Rotate the values of y_new back so that they correspond to the # correct x_new values. result = swapaxes(y_new,self.interp_axis,self.axis) try: len(x_new) return result except TypeError: return result[0] return result
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
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
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)
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
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
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
def draw_hour(self): months = self.get_month_range() elem = self.element_w.getvalue() Busy.Manager.busy() self.update_idletasks() g = self.graphics['hour'] # clear display g['ax'].cla() # title if len(months) == 1: title = '%s %s %s (%d-%d)' % ((self.id_, \ self.Element.get(elem, ''), self.Month[months[0]]) + \ tuple(self.data['years'])) else: title = '%s %s %s-%s (%d-%d)' % ((self.id_, \ self.Element.get(elem, ''), self.Month[months[0]], \ self.Month[months[-1]]) + tuple(self.data['years'])) g['ax'].set_title(title) cell_text = [] col_labels = ['%02d' % x for x in range(24)] xvals = na.arange(24) + 0.2 # the bottom values for stacked bar chart yoff = na.zeros(len(col_labels), na.Float32) num_cat = len(ClimLib.FlightCats) bar = [None]*num_cat widths = [0.6]*24 # stacked bars g['ax'].xaxis.set_major_locator(multipleLocator) # sum over selected range of months and wind directions tmp = na.sum(na.sum(na.take(self.data[elem], months, 0), 0), 1) # sum over all categories total = na.sum(tmp, -1) for row in range(num_cat): yvals = 100.0*tmp[:,row]/total bar[row] = g['ax'].bar(xvals, yvals, widths, bottom=yoff, color=self.colors[num_cat-row-1]) yoff += yvals cell_row = ['%.0f' % yvals[n] for n in range(24)] cell_text.append(cell_row) cell_text.reverse() g['ax'].table(cellText=cell_text, rowLabels=self.RowLabels, rowColours=self.colors, colLabels=col_labels, loc='bottom') # legend legend_bars = [x[0] for x in bar] legend_bars.reverse() g['ax'].legend(legend_bars, self.RowLabels) # axes g['ax'].set_ylabel('Percent occurrence') g['ax'].set_xticks([]) ymax, delta = self.set_yticks(elem, 'hour') g['ax'].set_yticks(na.arange(0, ymax, delta)) g['ax'].grid(True) g['canvas'].draw() Busy.Manager.notbusy()
def test(input_array, target_array, tol): (closest_indices, accept_indices, reject_indices) = find_closest(input_array, target_array, tol) print "tol: ", tol print "input_array: ", input_array print "target_array: ", target_array print "input_array elts closest to target: ", numarray.take(input_array, closest_indices) print "their input_array indices: ", closest_indices print "indices of elts in target_array that are within tolerance of their closest match: ", accept_indices print "indices of elts in target_array that are outside tolerance of their closest match: ", reject_indices print "-" * 90
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
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
def test(input_array, target_array, tol): (closest_indices, accept_indices, reject_indices) = find_closest(input_array, target_array, tol) print "tol: ", tol print "input_array: ", input_array print "target_array: ", target_array print "input_array elts closest to target: ", numarray.take( input_array, closest_indices) print "their input_array indices: ", closest_indices print "indices of elts in target_array that are within tolerance of their closest match: ", accept_indices print "indices of elts in target_array that are outside tolerance of their closest match: ", reject_indices print "-" * 90
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
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
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 draw_month(self): hours = self.get_hour_range() elem = self.element_w.getvalue() Busy.Manager.busy() self.update_idletasks() g = self.graphics['month'] # clear display g['ax'].cla() g['ax'].xaxis.set_major_locator(multipleLocator) # title if len(hours) == 1: title = '%s %s %02dZ (%d-%d)' % ((self.id_, \ self.Element.get(elem, ''), hours[0]) + \ tuple(self.data['years'])) else: title = '%s %s %02d-%02dZ (%d-%d)' % ((self.id_, \ self.Element.get(elem, ''), hours[0], hours[-1]) + \ tuple(self.data['years'])) g['ax'].set_title(title) cell_text = [] col_labels = self.Month[:] col_labels.append('Annual') xvals = list(na.arange(len(self.Month)) + 0.2) xvals.append(xvals[-1]+1.2) # the bottom values for stacked bar chart yoff = na.array([0.0] * len(col_labels)) num_cat = len(ClimLib.FlightCats) bar = [None]*num_cat widths = [0.6]*12 + [1.2] # 0.6 for monthly, 1.2 for annual # stacked bars # sum over selected range of hours and wind directions tmp = na.sum(na.sum(na.take(self.data[elem], hours, 1), 1), 1) # sum over all categories total = na.sum(tmp, -1) total_sum = total.sum() for row in range(num_cat): yvals = 100.0*tmp[:,row]/total yvals.resize(13) yvals[12] = 100.0*tmp[:,row].sum()/total_sum bar[row] = g['ax'].bar(xvals, yvals, widths, bottom=yoff, color=self.colors[num_cat-row-1]) yoff += yvals cell_row = ['%.0f' % yvals[n] for n in range(13)] cell_text.append(cell_row) cell_text.reverse() widths = [1.0/14]*12+[1.0/7.0] g['ax'].table(cellText=cell_text, rowLabels=self.RowLabels, rowColours=self.colors, colLabels=col_labels, colWidths=widths, loc='bottom') # legend legend_bars = [x[0] for x in bar] legend_bars.reverse() g['ax'].legend(legend_bars, self.RowLabels) # axes g['ax'].set_ylabel('Percent occurrence') g['ax'].set_xticks([]) ymax, delta = self.set_yticks(elem, 'month') g['ax'].set_yticks(na.arange(0, ymax, delta)) g['ax'].grid(True) g['canvas'].draw() Busy.Manager.notbusy()
def save_stats(self, action): # save stats to a file if action == 'Close': self.stats_dialog.withdraw() self.stats_dialog.deactivate() else: if self.data is None: return element = self.stats_dialog.element.getvalue() columns = self.stats_dialog.columns.getvalue() path = self.stats_dialog.file.getvalue() fh = open(path,'a') if not columns: msg = 'Select at least one column' Busy.showerror(msg, self.interior()) return output = self.make_list(self.combine_count()) col_labels = self.RowLabels[:] col_labels.reverse() elements = {'vis':'Visibility', 'cig':'Ceiling','joint':'Joint'} hours = self.get_climate_hour_range() if 'wdir' in columns: months = self.get_month_range() num_cat = len(ClimLib.FlightCats) dir_labels = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', \ 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'VRB', 'C'] fmtw0 = '%4s ' + '(%4d-%4d)' fmtw4 = 'MONTH: %-5s ' fmtw1 = 'HOUR: %02dZ' + ' '*9 + '%10s' fmtw2 = '%-5s ' + '%-7s '*5 fmtw3 = '%-5s ' + '%-7.2f '*5 for month in months: for hour in hours: print >> fh, fmtw0 % (self.id_.strip(),self.data['years'][0],self.data['years'][1]) print >> fh, fmtw4 % (self.Month[month]) print >> fh, fmtw1 % (hour, elements[element]) print >> fh, fmtw2 % tuple(['DIR'] + col_labels[:] + ['VFR']) tmp = na.sum(na.sum(na.take(na.take(self.data[element], [month], 0), [hour], 1), 0), 0) total = na.sum(tmp, -1) wind_hours = [] wind_hours.append(dir_labels) for row in range(num_cat): yvals = tmp[:,row] yvals[na.ieeespecial.isnan(yvals)] = 0.0 wind_hours.append(yvals) wind_hours.append(total) for row in zip(*wind_hours): print >> fh, fmtw3 % row print >> fh, ' ' self.messagebar.message('userevent','Stats saved to ' + path) self.stats_dialog.withdraw() self.stats_dialog.deactivate() return fmt0 = '%4s ' + '(%4d-%4d)' fmt1 = 'HOUR: %02dZ' + ' '*9 + '%10s' fmt2 = '%-5s ' + '%-7s '*5 fmt3 = '%-5s ' + '%-7.2f '*5 print >> fh, fmt0 % (self.id_,self.data['years'][0],self.data['years'][1]) for hour in hours: print >> fh, fmt1 % (hour, elements[element]) print >> fh, fmt2 % tuple(['MONTH'] + col_labels[:] + ['VFR']) mth_ctr = 0 for row in na.sum(na.sum(na.take(self.data[element], [hour], 1), 1), 1): try: print >> fh, fmt3 % tuple([self.Month[mth_ctr]] + [x for x in row]) mth_ctr = mth_ctr + 1 except Exception, e: print self.Month[mth_ctr] print row[:] print e print >> fh, ' ' fh.close() self.messagebar.message('userevent','Stats saved to ' + path) self.stats_dialog.withdraw() self.stats_dialog.deactivate()
def draw_wdir(self): months = self.get_month_range() hours = self.get_hour_range() elem = self.element_w.getvalue() Busy.Manager.busy() self.update_idletasks() g = self.graphics['wdir'] # clear display g['ax'].cla() # title if len(months) == 1: if len(hours) == 1: title = '%s %s %02dZ %s (%d-%d)' % ((self.id_, \ self.Element.get(elem, ''), hours[0], \ self.Month[months[0]]) + tuple(self.data['years'])) else: title = '%s %s %02d-%02dZ %s (%d-%d)' % ((self.id_, \ self.Element.get(elem, ''), hours[0], hours[-1], \ self.Month[months[0]]) + tuple(self.data['years'])) else: if len(hours) == 1: title = '%s %s %02dZ %s-%s (%d-%d)' % ((self.id_, \ self.Element.get(elem, ''), hours[0], self.Month[months[0]], \ self.Month[months[-1]]) + tuple(self.data['years'])) else: title = '%s %s %02d-%02dZ %s-%s (%d-%d)' % ((self.id_, \ self.Element.get(elem, ''), hours[0], hours[-1], \ self.Month[months[0]], self.Month[months[-1]]) + \ tuple(self.data['years'])) g['ax'].set_title(title) cell_text = [] col_labels = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', \ 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'VRB', 'C'] num_dir = len(col_labels) xvals = na.arange(num_dir) + 0.2 # the bottom values for stacked bar chart yoff = na.zeros(num_dir, na.Float32) num_cat = len(ClimLib.FlightCats) bar = [None]*num_cat widths = [0.6]*num_dir # stacked bars g['ax'].xaxis.set_major_locator(multipleLocator) # sum over selected range of months, hours tmp = na.sum(na.sum(na.take(na.take(self.data[elem], months, 0), hours, 1), 0), 0) # sum over all categories total = na.sum(tmp, -1) for row in range(num_cat): yvals = 100.0*tmp[:,row]/total yvals[na.ieeespecial.isnan(yvals)] = 0.0 bar[row] = g['ax'].bar(xvals, yvals, widths, bottom=yoff, color=self.colors[num_cat-row-1]) yoff += yvals cell_row = ['%.0f' % yvals[n] for n in range(num_dir)] cell_text.append(cell_row) cell_text.reverse() cell_row = ['%.0f' % total[n] for n in range(num_dir)] cell_text.append(cell_row) row_labels = self.RowLabels[:] row_labels.append('HOURS') row_colors = self.colors[:] row_colors.append('white') g['ax'].table(cellText=cell_text, rowLabels=row_labels, rowColours=row_colors, colLabels=col_labels, loc='bottom') # legend legend_bars = [x[0] for x in bar] legend_bars.reverse() g['ax'].legend(legend_bars, self.RowLabels) # axes g['ax'].set_ylabel('Percent occurrence') g['ax'].set_xticks([]) ymax, delta = self.set_yticks(elem, 'wdir') g['ax'].set_yticks(na.arange(0, ymax, delta)) g['ax'].grid(True) g['canvas'].draw() Busy.Manager.notbusy()
#!/usr/bin/env python import numarray def complement(ind_arr, n): """ Find the complement of the set of indices in ind_arr from arange(n) """ mat = numarray.ones(n) numarray.put(mat, ind_arr, 0) out = numarray.nonzero(mat) return out[0] if __name__ == "__main__": orig_arr = numarray.arange(10) + 0.2 indices = numarray.array([1, 3, 5]) comp = complement(indices, len(orig_arr)) comp_arr = numarray.take(orig_arr, comp) print "orig_arr: ", orig_arr print "indices: ", indices print "complement indices: ", comp print "complement elements: ", comp_arr