def create_db(filename, nrows): class Record(tables.IsDescription): col1 = tables.Int32Col() col2 = tables.Int32Col() col3 = tables.Float64Col() col4 = tables.Float64Col() con = open_db(filename, remove=1) table = con.createTable(con.root, 'table', Record, filters=filters, expectedrows=nrows) table.indexFilters = filters step = 1000*100 scale = 0.1 t1=time() j = 0 for i in xrange(0, nrows, step): stop = (j+1)*step if stop > nrows: stop = nrows arr_f8 = numarray.arange(i, stop, type=numarray.Float64) arr_i4 = numarray.arange(i, stop, type=numarray.Int32) if userandom: arr_f8 += random_array.normal(0, stop*scale, shape=[stop-i]) arr_i4 = numarray.array(arr_f8, type=numarray.Int32) recarr = records.fromarrays([arr_i4, arr_i4, arr_f8, arr_f8]) table.append(recarr) j += 1 table.flush() ctime = time()-t1 if verbose: print "insert time:", round(ctime, 5) print "Krows/s:", round((nrows/1000.)/ctime, 5) index_db(table) close_db(con)
def create_db(filename, nrows): class Record(tables.IsDescription): col1 = tables.Int32Col() col2 = tables.Int32Col() col3 = tables.Float64Col() col4 = tables.Float64Col() con = open_db(filename, remove=1) table = con.create_table(con.root, 'table', Record, filters=filters, expectedrows=nrows) table.indexFilters = filters step = 1000*100 scale = 0.1 t1=time() j = 0 for i in xrange(0, nrows, step): stop = (j+1)*step if stop > nrows: stop = nrows arr_f8 = numarray.arange(i, stop, type=numarray.Float64) arr_i4 = numarray.arange(i, stop, type=numarray.Int32) if userandom: arr_f8 += random_array.normal(0, stop*scale, shape=[stop-i]) arr_i4 = numarray.array(arr_f8, type=numarray.Int32) recarr = records.fromarrays([arr_i4, arr_i4, arr_f8, arr_f8]) table.append(recarr) j += 1 table.flush() ctime = time()-t1 if verbose: print "insert time:", round(ctime, 5) print "Krows/s:", round((nrows/1000.)/ctime, 5) index_db(table) close_db(con)
def testMarginalise(self): def factorial(n): if n==1:return 1 return factorial(n - 1) * n var = set('c') b = self.a.Marginalise(var) var2 = set(['c','a']) c = self.a.Marginalise(var2) d = DiscretePotential(['b','c'], [3,4], na.arange(12)) # extended test a = DiscretePotential('a b c d e f'.split(), [2,3,4,5,6,7], \ na.arange(factorial(7))) aa = a.Marginalise('c f a'.split()) assert(b.names == self.a.names - var and \ b[0,1] == na.sum(self.a[0,1]) and \ c.names == self.a.names - var2 and \ na.alltrue(c.cpt.flat == na.sum(na.sum(self.a.cpt,axis=2), axis=0)) and aa.shape == (3,5,6) and \ aa.names_list == 'b d e'.split() and \ aa[2,4,3] == na.sum(a[:,2,:,4,3,:].flat)), \ " Marginalisation doesn't work"
def plotStar(star, time='orbit', color='k', alpha=1.0): # Choices for time are # 'orbit' = plot the whole orbit # 'obs' = duration of observation # 'extend' = duration of observation plus out to 2017.5 orb = star.orbit # Determine time steps x = star.getArrayAllEpochs('x') y = star.getArrayAllEpochs('y') if (time == 'orbit'): t = na.arange(orb.t0 + 0.01, orb.t0 + orb.p + 0.11, orb.p / 200.0, type=na.Float) if (time == 'obs'): idx = (na.where(x != -1000))[0] t = na.arange(math.floor(star.years[idx[0]]), math.ceil(star.years[idx[-1]]), 0.1, type=na.Float) if (time == 'extend'): idx = (na.where(x != -1000))[0] t = na.arange(math.floor(star.years[idx[0]]), 2017.5, 0.1, type=na.Float) (r, v, a) = orb.kep2xyz(t, mass=mass, dist=dist) pp = pylab.plot(r[:, 0], r[:, 1], color=color, alpha=alpha) # To plot no line and just the data points: #pp = pylab.plot([], [],color=color) ## ## Now plot the actual data points ## # Load from points files if yrlyPts: # Just take the points from 'r' array spaced about a year apart roundT = array([int(round(t[qq])) for qq in range(len(t))]) firstT = roundT[0] tPts = [ roundT.searchsorted(firstT + zz + 1) for zz in range(roundT[-1] - firstT) ] c = pylab.scatter(r[tPts, 0], r[tPts, 1], 20.0, color, marker='o', faceted=False) else: # Get the actual data c = pylab.scatter(x, y, 20.0, color, marker='o', faceted=False) c.set_alpha(alpha) return pp
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 mask(clus_id, line_s): imagefile = c.imagefile sex_cata = c.sex_cata threshold = c.threshold thresh_area = c.thresh_area size = c.size mask_reg = c.mask_reg x = n.reshape(n.arange(size*size),(size,size)) % size x = x.astype(n.Float32) y = n.reshape(n.arange(size*size),(size,size)) / size y = y.astype(n.Float32) values = line_s.split() mask_file = 'mask_' + str(imagefile)[:6] + '_' + str(clus_id) + '.fits' xcntr_o = float(values[1]) #x center of the object ycntr_o = float(values[2]) #y center of the object xcntr = size / 2.0 + 1.0 + xcntr_o - int(xcntr_o) ycntr = size / 2.0 + 1.0 + ycntr_o - int(ycntr_o) mag = float(values[7]) #Magnitude radius = float(values[9]) #Half light radius mag_zero = c.mag_zero #magnitude zero point sky = float(values[10]) #sky pos_ang = float(values[11]) - 90.0 #position angle axis_rat = 1.0 / float(values[12]) #axis ration b/a major_axis = float(values[14]) #major axis of the object z = n.zeros((size,size)) for line_j in open(sex_cata,'r'): try: values = line_j.split() xcntr_n = float(values[1]) #x center of the neighbour ycntr_n = float(values[2]) #y center of the neighbour mag = float(values[7]) #Magnitude radius = float(values[9]) #Half light radius sky = float(values[10]) #sky pos_ang = float(values[11]) - 90.0 #position angle axis_rat = 1.0/float(values[12]) #axis ration b/a area = float(values[13]) maj_axis = float(values[14])#major axis of neighbour if(abs(xcntr_n - xcntr_o) < size/2.0 and abs(ycntr_n - ycntr_o) \ < size/2.0 and area < thresh_area): if(abs(xcntr_n - xcntr_o) >= major_axis * threshold or \ abs(ycntr_n - ycntr_o) >= major_axis * threshold): if((xcntr_o - xcntr_n) < 0): xn = xcntr + abs(xcntr_n - xcntr_o) if((ycntr_o - ycntr_n) < 0): yn = ycntr + abs(ycntr_n - ycntr_o) if((xcntr_o - xcntr_n) > 0): xn = xcntr - (xcntr_o - xcntr_n) if((ycntr_o - ycntr_n) > 0): yn = ycntr - (ycntr_o - ycntr_n) tx = x - xn + 0.5 ty = y - yn + 0.5 R = n.sqrt(tx**2.0 + ty**2.0) z[n.where(R<=mask_reg*maj_axis)] = 1 except: pass hdu = pyfits.PrimaryHDU(z.astype(n.Float32)) hdu.writeto(mask_file)
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 ap_sky(xc,yc,rr,dr): data=[] left=max(0,int(xc-rr-dr-5)) right=min(int(xc+rr+dr+5),nx) bottom=max(0,int(yc-rr-dr-5)) top=min(int(yc+rr+dr+5),ny) for x in numarray.arange(left,right,1): for y in numarray.arange(bottom,top,1): if ((x-xc)**2+(y-yc)**2)**0.5 > rr and ((x-xc)**2+(y-yc)**2)**0.5 <= rr+dr: if str(pix[y-1][x-1]) != 'nan': data.append(pix[y-1][x-1]) return xits(data,3.)
def _bench(): """time a 10**6 element median""" import time a = num.arange(10**6, shape=(1000, 1000)) arrays = [a * 2, a * 64, a * 16, a * 8] t0 = time.clock() median(arrays) print "maskless:", time.clock() - t0 a = num.arange(10**6, shape=(1000, 1000)) arrays = [a * 2, a * 64, a * 16, a * 8] t0 = time.clock() median(arrays, badmasks=num.zeros((1000, 1000), type=num.Bool)) print "masked:", time.clock() - t0
def plotold(): xmin=2.2 xmax=3.2 ymin=-2.5 ymax=-.5 psplotinit('fSsigma3Gyr.ps') ppgplot.pgbox("",0.0,0,"",0.0,0) ppgplot.pgenv(xmin,xmax,ymin,ymax,0,30) ppgplot.pglab("\gs (km/s)",'fS(10\u11\d:10\u13\d)',"") ppgplot.pgsci(1) ppgplot.pgline(sigma,frac) ppgplot.pgsls(2) ppgplot.pgsci(2) ppgplot.pgline(sigma08,frac08) ppgplot.pgsls(1) ppgplot.pgsci(1) ppgplot.pgend() xmin=2.2 xmax=3.2 ymin=11. ymax=14.2 psplotinit('maccretsigma3Gyr.ps') ppgplot.pgbox("",0.0,0,"",0.0,0) ppgplot.pgenv(xmin,xmax,ymin,ymax,0,30) ppgplot.pglab("\gs (km/s)",'M\dacc\u (M\d\(2281)\u)',"") ppgplot.pgsci(1) ppgplot.pgline(sigma,maccret) ppgplot.pgsls(2) ppgplot.pgsci(2) ppgplot.pgline(sigma08,maccret08) ppgplot.pgsls(1) ppgplot.pgsci(1) mylines=N.arange(-20.,20.,.4) mylineswidth=3 ppgplot.pgsls(4) ppgplot.pgslw(mylineswidth) x=N.arange(0.,5.,1.) lines=mylines for y0 in lines: y=3*x +y0 ppgplot.pgline(x,y) ppgplot.pgsls(1) ppgplot.pgend() os.system('cp maccretsigma.ps /Users/rfinn/SDSS/paper/.') os.system('cp fSsigma.ps /Users/rfinn/SDSS/paper/.')
def _bench(): """time a 10**6 element median""" import time a = num.arange(10**6, shape=(1000, 1000)) arrays = [a*2, a*64, a*16, a*8] t0 = time.clock() median(arrays) print "maskless:", time.clock()-t0 a = num.arange(10**6, shape=(1000, 1000)) arrays = [a*2, a*64, a*16, a*8] t0 = time.clock() median(arrays, badmasks=num.zeros((1000,1000), type=num.Bool)) print "masked:", time.clock()-t0
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 fill_arrays(start, stop): col_i = numarray.arange(start, stop, type=numarray.Int32) if userandom: col_j = random_array.uniform(0, nrows, shape=[stop-start]) else: col_j = numarray.array(col_i, type=numarray.Float64) return col_i, col_j
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
class EffectiveAreaBins(Bins): """ subclass of Bins for finer binning of effective area """ logemin = Bins.logemin logemax = Bins.logemax ebreak = 4.25 ebinfactor = 4 ebinhigh = 2 logedelta = Bins.logedelta # generate list with different anglebinfactor=4 # bins multiplier angle_bin_edges = num.arange(Bins.angle_bins*anglebinfactor+1)*Bins.cthdelta/anglebinfactor+Bins.cthmin @classmethod def set_energy_bins(cls,logemin=None,logemax=None): if logemin is None: logemin = cls.logemin if logemax is None: logemax = cls.logemax cls.energy_bin_edges = [] x = logemin factor = cls.ebinfactor while x<logemax+0.01: if x>= cls.ebreak: factor = cls.ebinhigh cls.energy_bin_edges.append(x) x += cls.logedelta/factor print 'Energy Bins ', cls.energy_bin_edges
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 testMeshgrid_DenseFromMixedArrayTypes(self): # Other combination of arrays #print 'testing Meshgrid with mixed array implementations' y = N.arange(4) z = N.arange(3) import Numeric x = Numeric.arange(10) X, Y, Z = N.meshgrid(x, y, z, sparse=False) if not N.rank(X) == 3: raise AssertionError( "Meshgrid failed with arraytype mix of Numeric and %s"\ %N.basic_NumPy) import numarray x = numarray.arange(10) X, Y, Z = N.meshgrid(x, y, z, sparse=False) if not N.rank(X) == 3: raise AssertionError( "Meshgrid failed with arraytype mix of numarray and %s"\ %N.basic_NumPy) import numpy x = numpy.arange(10) X, Y, Z = N.meshgrid(x, y, z, sparse=False) #assert N.rank(X) == 3 if not N.rank(X) == 3: raise AssertionError( "Meshgrid failed with arraytype mix of numpy and %s"\ %N.basic_NumPy)
def dLold(z, h): zstep = 10000 dz = z / (1. * zstep) c = 3. * 10**5 H0 = 100. * h t = 0 s = 0 tH = 9.78 * h zsteps = N.arange(0., z + dz, dz) #for i in range(zstep+1): for zi in zsteps: #zi=dz*(i-1) t = t + dz / E(zi) / (1 + zi) s = s + dz / E(zi) DA = c / H0 / (1 + z) * s #(kpc/arcsec) DA = DA * 1000. / 206264 DL = c / H0 * (1 + z) * s #(Mpc/h) t = t * tH #lookbackt Gyr #print "DA = ",DA #print "DL = ",DL #print "lookback t = ",t #distmod=5*log10(DL*1.d6/10) # print "Distance Modulus = ",distmod return DL
def cumulative(input): #print "max x = ",max(input) x = N.sort(input) #print "max x = ",max(x) n = len(input) y = N.arange(0., 1., (1. / float(n))) return x, y
def fill_arrays(start, stop): col_i = numarray.arange(start, stop, type=numarray.Int32) if userandom: col_j = random_array.uniform(0, nrows, shape=[stop - start]) else: col_j = numarray.array(col_i, type=numarray.Float64) return col_i, col_j
def __init__(self, name, naxis=1, crval=0, cdelt=1, ctype="none", crpix=1): if (type(name) == type(" ")): # # These values are being set by hand, # self.name = name self.crval = crval self.cdelt = cdelt self.naxis = naxis self.ctype = '%s' % ctype self.crpix = crpix else: # # Assume this is a primary header and grab the keyword data, # but check anyways # if (name.__doc__ != "FITS header class."): print "This is not a FITS header." return axis = "%s" % naxis self.name = keywordVal(name, "CTYPE" + axis, "axis %s" % naxis) self.crval = keywordVal(name, "CRVAL" + axis, 0) self.cdelt = keywordVal(name, "CDELT" + axis, 1) self.naxis = keywordVal(name, "NAXIS" + axis) self.ctype = self.name self.crpix = keywordVal(name, "CRPIX" + axis, 1) self.array = ( (numarray.arange(self.naxis) + 1 - self.crpix) * self.cdelt + self.crval)
def createFileArr(filename, ngroups, ntables, nrows): # First, create the groups # Open a file in "w"rite mode fileh = openFile(filename, mode="w", title="PyTables Stress Test") for k in range(ngroups): # Create the group group = fileh.createGroup("/", 'group%04d' % k, "Group %d" % k) fileh.close() # Now, create the arrays rowswritten = 0 arr = numarray.arange(nrows) for k in range(ngroups): fileh = openFile(filename, mode="a", rootUEP='group%04d' % k) # Get the group group = fileh.root for j in range(ntables): # Create the array group = fileh.createArray("/", 'array%04d' % j, arr, "Array %d" % j) fileh.close() return (ngroups * ntables * nrows, 4)
def __xmlEndTagFound(self, in_name): if in_name == "adcdata": # ADC_Result if self.__filetype == ResultReader.ADCDATA_TYPE: if self.try_base64: tmp_string=base64.standard_b64decode(self.adc_result_trailing_chars) self.adc_result_trailing_chars="" tmp=numarray.fromstring(tmp_string, numarray.Int16,(len(tmp_string)/2)) del tmp_string self.result.x[self.adc_result_sample_counter:]=(numarray.arange(tmp.size()/2)+self.adc_result_sample_counter)/self.result.get_sampling_rate() self.result.y[0][self.adc_result_sample_counter:]=tmp[::2] self.result.y[1][self.adc_result_sample_counter:]=tmp[1::2] self.adc_result_sample_counter+=tmp.size()/2 else: if self.adc_result_trailing_chars!="": self.__xmlCharacterDataFound(" ") return elif in_name == "result": pass # Error_Result elif self.__filetype == ResultReader.ERROR_TYPE: pass # Temp_Result elif self.__filetype == ResultReader.TEMP_TYPE: pass # Config_Result elif self.__filetype == ResultReader.CONFIG_TYPE: pass
def testMeshgrid_DenseFromMixedArrayTypes(self): # Other combination of arrays # print 'testing Meshgrid with mixed array implementations' y = N.arange(4) z = N.arange(3) import Numeric x = Numeric.arange(10) X, Y, Z = N.meshgrid(x, y, z, sparse=False) if not N.ndim(X) == 3: raise AssertionError( "Meshgrid failed with arraytype mix of Numeric and %s" % N.basic_NumPy) import numarray x = numarray.arange(10) X, Y, Z = N.meshgrid(x, y, z, sparse=False) if not N.ndim(X) == 3: raise AssertionError( "Meshgrid failed with arraytype mix of numarray and %s" % N.basic_NumPy) import numpy x = numpy.arange(10) X, Y, Z = N.meshgrid(x, y, z, sparse=False) #assert N.ndim(X) == 3 if not N.ndim(X) == 3: raise AssertionError( "Meshgrid failed with arraytype mix of numpy and %s" % N.basic_NumPy)
def main(): window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.set_name ("Test Input") window.connect("destroy", lambda w: gtk.main_quit()) vbox = gtk.VBox(False, 0) window.add(vbox) vbox.show() p = grafity.Project() w = p.new(grafity.Worksheet, 'test') w.a = numarray.arange(1000) w.b = [2,4,5, 5.5] w.c = [3,2,1,3.3] w.d = 2*w.a w.e = w.b * w.c sheet = Sheet(w) vbox.pack_start(sheet, True, True, 0) # button = gtk.Button("Quit") # vbox.pack_start(button, False, False, 0) # button.connect_object("clicked", lambda w: w.destroy(), window) # button.show() window.show() gtk.main() return 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)
def plot(self,onsets,ofunc,wplot,oplots,nplot=False): import Gnuplot, Gnuplot.funcutils import aubio.txtfile import os.path import numarray from aubio.onsetcompare import onset_roc x1,y1,y1p = [],[],[] oplot = [] if self.params.onsetmode in ('mkl','kl'): ofunc[0:10] = [0] * 10 self.lenofunc = len(ofunc) self.maxofunc = max(ofunc) # onset detection function downtime = numarray.arange(len(ofunc))*self.params.step oplot.append(Gnuplot.Data(downtime,ofunc,with='lines',title=self.params.onsetmode)) # detected onsets if not nplot: for i in onsets: x1.append(i[0]*self.params.step) y1.append(self.maxofunc) y1p.append(-self.maxofunc) #x1 = numarray.array(onsets)*self.params.step #y1 = self.maxofunc*numarray.ones(len(onsets)) if x1: oplot.append(Gnuplot.Data(x1,y1,with='impulses')) wplot.append(Gnuplot.Data(x1,y1p,with='impulses'))
def abs_fft(self, points=None, zoom=None,write = 'off'): """ Fourier transforms the timesignal; points is the number of points to transform, if more points given than data points the rest is zero padded absfft(points=4096) """ realdata = numarray.array(self.the_result.y[0]) imdata = numarray.array(self.the_result.y[1]) data = realdata + 1j*imdata fftdata = self.rearrange(numarray.fft.fft(data, points)) absfft = numarray.sqrt(fftdata.real**2 + fftdata.imag**2) # create our x axis n = fftdata.size() self.the_result.x = numarray.arange(n)*(self.sampling_rate/n)-(self.sampling_rate/2.0) self.the_result.y[0] = absfft self.the_result.y[1] = numarray.zeros(n) if write == 'on': return self else: if zoom is None: return self.the_result else: center, width = zoom return self.zoom(self.the_result, center, width)
def createFileArr(filename, ngroups, ntables, nrows): # First, create the groups # Open a file in "w"rite mode fileh = openFile(filename, mode="w", title="PyTables Stress Test") for k in range(ngroups): # Create the group group = fileh.createGroup("/", 'group%04d'% k, "Group %d" % k) fileh.close() # Now, create the arrays rowswritten = 0 arr = numarray.arange(nrows) for k in range(ngroups): fileh = openFile(filename, mode="a", rootUEP='group%04d'% k) # Get the group group = fileh.root for j in range(ntables): # Create the array group = fileh.createArray("/", 'array%04d'% j, arr, "Array %d" % j) fileh.close() return (ngroups*ntables*nrows, 4)
def __init__(self, name, naxis=1, crval=0, cdelt=1, ctype = "none", crpix = 1): if ( type(name) == type(" ") ): # # These values are being set by hand, # self.name = name self.crval = crval self.cdelt = cdelt self.naxis = naxis self.ctype = '%s' % ctype self.crpix = crpix else: # # Assume this is a primary header and grab the keyword data, # but check anyways # if (name.__doc__ != "FITS header class."): print "This is not a FITS header." return axis = "%s" % naxis self.name = keywordVal(name, "CTYPE"+axis, "axis %s" % naxis) self.crval = keywordVal(name, "CRVAL"+axis, 0) self.cdelt = keywordVal(name, "CDELT"+axis, 1) self.naxis = keywordVal(name, "NAXIS" + axis) self.ctype = self.name self.crpix = keywordVal(name, "CRPIX"+axis, 1) self.array = ((numarray.arange(self.naxis) + 1 - self.crpix)*self.cdelt + self.crval)
def points(self,npoints): """ compute arrays of npoints equally spaced intermediate points along the great circle. input parameter npoints is the number of points to compute. Returns lons, lats (lists with longitudes and latitudes of intermediate points in degrees). For example npoints=10 will return arrays lons,lats of 10 equally spaced points along the great circle. """ # can't do it if endpoints of great circle are antipodal, since # route is undefined. if self.antipodal: raise ValueError,'cannot compute intermediate points on a great circle whose endpoints are antipodal' d = self.distance delta = 1.0/(npoints-1) f = delta*N.arange(npoints) lat1 = self.lat1 lat2 = self.lat2 lon1 = self.lon1 lon2 = self.lon2 A = N.sin((1-f)*d)/math.sin(d) B = N.sin(f*d)/math.sin(d) x = A*math.cos(lat1)*math.cos(lon1)+B*math.cos(lat2)*math.cos(lon2) y = A*math.cos(lat1)*math.sin(lon1)+B*math.cos(lat2)*math.sin(lon2) z = A*math.sin(lat1) +B*math.sin(lat2) lats=N.arctan2(z,N.sqrt(x**2+y**2)) lons=N.arctan2(y,x) lons = map(math.degrees,lons.tolist()) lats = map(math.degrees,lats.tolist()) return lons,lats
def makeplotsub(c): plot(c.f24, c.fap4 * 1.67, 'bo', markersize=4) #plot(c.f24,c.fap4,'ro',markersize=4) #plot(c.f24,c.fap3,'go',markersize=2) #plot(c.f24,c.fap2,'yo',markersize=2) #plot(c.f24,c.fap1,'co',markersize=2) x = N.arange(1., max(c.f24), 100.) y = x plot(x, y, 'k-') y2 = y + N.log10(2.) plot(x, y2, 'k--') y2 = y + N.log10(.5) plot(x, y2, 'k--') y = 3. * x #plot(x,y,'k-') y = 4. * x #plot(x,y,'k-') y = 5. * x #plot(x,y,'k-') ax = gca() text(.1, .8, c.prefix, horizontalalignment='left', verticalalignment='center', transform=ax.transAxes) #xlabel('Mopex F(24)',fontsize=fsize) #ylabel('Ap Flux ',fontsize=fsize) axis([20., 4000., 20., 4000.]) ax.set_xscale('log') ax.set_yscale('log')
def plotStar(star, time='orbit', color='k', alpha=1.0): # Choices for time are # 'orbit' = plot the whole orbit # 'obs' = duration of observation # 'extend' = duration of observation plus out to 2017.5 orb = star.orbit # Determine time steps x = star.getArrayAllEpochs('x') y = star.getArrayAllEpochs('y') if (time == 'orbit'): t = na.arange(orb.t0+0.01, orb.t0+orb.p+0.11, orb.p/200.0, type=na.Float) if (time == 'obs'): idx = ( na.where(x != -1000) )[0] t = na.arange(math.floor(star.years[idx[0]]), math.ceil(star.years[idx[-1]]), 0.1, type=na.Float) if (time == 'extend'): idx = ( na.where(x != -1000) )[0] t = na.arange(math.floor(star.years[idx[0]]), 2017.5, 0.1, type=na.Float) (r, v, a) = orb.kep2xyz(t, mass=mass, dist=dist) pp = pylab.plot(r[:,0], r[:,1], color=color, alpha=alpha) # To plot no line and just the data points: #pp = pylab.plot([], [],color=color) ## ## Now plot the actual data points ## # Load from points files if yrlyPts: # Just take the points from 'r' array spaced about a year apart roundT = array([int(round(t[qq])) for qq in range(len(t))]) firstT = roundT[0] tPts = [roundT.searchsorted(firstT+zz+1) for zz in range(roundT[-1]-firstT)] c = pylab.scatter(r[tPts,0], r[tPts,1], 20.0, color, marker='o', faceted=False) else: # Get the actual data c = pylab.scatter(x, y, 20.0, color, marker='o', faceted=False) c.set_alpha(alpha) return pp
def main(): a = numarray.arange(100) r = get_readonly_p(a) print r r = get_readonly_c(a) print r
def set_angle_bins(cls, cthmin=None, cthdelta=None): """Convenience method for initializing cosTheta bin edge vector.""" if cthmin is not None: cls.cthmin = cthmin if cthdelta is not None: cls.cthdelta = cthdelta cls.angle_bins = int((1.0 - cls.cthmin) / cls.cthdelta) cls.angle_bin_edges = num.arange(cls.angle_bins + 1) * cls.cthdelta + cls.cthmin
def plot_rspgenIntegral(self, energy, inclination, phi=0, nsamp=2000): rmin = 1e-2 rmax = 30. npts = 20 rstep = num.log(rmax / rmin) / (npts - 1) radii = rmin * num.exp(rstep * num.arange(npts)) self._setPsf(energy, inclination, phi) seps = [] srcDir = SkyDir(180, 0) for i in range(nsamp): appDir = self.psf.appDir(energy, srcDir, self.scZAxis, self.scXAxis) seps.append(appDir.difference(srcDir) * 180. / num.pi) seps.sort() fraction = num.arange(nsamp, type=num.Float) / nsamp disp = plot.scatter(seps, fraction, xlog=1, xname='ROI radius', yname='enclosed Psf fraction', pointRep='Line', color='red') disp.setTitle("%s: %i MeV, %.1f deg" % (self.irfs, energy, inclination)) npred = [] resids = [] for radius in radii: npred.append( self.psf.angularIntegral(energy, inclination, phi, radius)) resids.append( num.abs( (self._interpolate(seps, fraction, radius) - npred[-1]) / npred[-1])) plot.scatter(radii, npred, pointRep='Line', oplot=1) residplot = plot.scatter(radii, resids, 'ROI radius', yname='abs(sim - npred)/npred', xlog=1, ylog=1) # Npred = Interpolator(radii, npred) ks_prob = ks2(npred, seps) plot.hline(0) residplot.setTitle("%s: %i MeV, %.1f deg\n ks prob=%.2e" % (self.irfs, energy, inclination, ks_prob[1])) return energy, inclination, ks_prob[1]
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 points(self,npoints): """ compute arrays of npoints equally spaced intermediate points along the great circle. input parameter npoints is the number of points to compute. Returns lons, lats (lists with longitudes and latitudes of intermediate points in degrees). For example npoints=10 will return arrays lons,lats of 10 equally spaced points along the great circle. """ # must ask for at least 2 points. if npoints <= 1: raise ValueError,'npoints must be greater than 1' elif npoints == 2: return [math.degrees(self.lon1),math.degrees(self.lon2)],[math.degrees(self.lat1),math.degrees(self.lat2)] # can't do it if endpoints are antipodal, since # route is undefined. if self.antipodal: raise ValueError,'cannot compute intermediate points on a great circle whose endpoints are antipodal' d = self.gcarclen delta = 1.0/(npoints-1) f = delta*N.arange(npoints) # f=0 is point 1, f=1 is point 2. incdist = self.distance/(npoints-1) lat1 = self.lat1 lat2 = self.lat2 lon1 = self.lon1 lon2 = self.lon2 # perfect sphere, use great circle formula if self.f == 0.: A = N.sin((1-f)*d)/math.sin(d) B = N.sin(f*d)/math.sin(d) x = A*math.cos(lat1)*math.cos(lon1)+B*math.cos(lat2)*math.cos(lon2) y = A*math.cos(lat1)*math.sin(lon1)+B*math.cos(lat2)*math.sin(lon2) z = A*math.sin(lat1) +B*math.sin(lat2) lats=N.arctan2(z,N.sqrt(x**2+y**2)) lons=N.arctan2(y,x) lons = map(math.degrees,lons.tolist()) lats = map(math.degrees,lats.tolist()) # use ellipsoid formulas else: latpt = self.lat1 lonpt = self.lon1 azimuth = self.azimuth12 lons = [math.degrees(lonpt)] lats = [math.degrees(latpt)] for n in range(npoints-2): latptnew,lonptnew,alpha21=vinc_pt(self.f,self.a,latpt,lonpt,azimuth,incdist) d,azimuth,a21=vinc_dist(self.f,self.a,latptnew,lonptnew,lat2,lon2) lats.append(math.degrees(latptnew)) lons.append(math.degrees(lonptnew)) latpt = latptnew; lonpt = lonptnew lons.append(math.degrees(self.lon2)) lats.append(math.degrees(self.lat2)) return lons,lats
def RandomAccessCheck(nupdate, n, table): temp = N.array([1], N.UInt64) for i in range(nupdate): temp = (temp << 1) ^ ((temp.astype(N.Int64) < 0)[0] and POLY or 0) table[temp & (n - 1)] ^= temp errors = n - (table - N.arange(n, type=N.UInt64) == 0).sum() ferr = float(errors) / float(n) * 100.0 return errors, ferr, ferr <= 0.01
def main(*args): resolution = 0.1 fwhm = 0.5 # get some (random) data, i.e. a stick-spectrum x = num.arange(-50, 50+resolution, resolution) y = num.zeros(x.shape, num.Float) for i in range(10): y[ran.randint(0, len(y)-1)] = ran.random() # create lineshape objects g = ls.GaussProfile(num.arange(-10*fwhm, 10*fwhm+resolution, resolution), 1.0) l = ls.LorentzProfile(num.arange(-10*fwhm, 10*fwhm+resolution, resolution), 1.0) v = ls.VoigtProfile(num.arange(-10*fwhm, 10*fwhm+resolution, resolution), (0.6, 0.6)) # convolute data with profile res_g = Convolve.convolve(y, g(), Convolve.SAME) res_l = Convolve.convolve(y, l(), Convolve.SAME) res_v = Convolve.convolve(y, v(), Convolve.SAME) for i in zip(x, y, res_g, res_l, res_v): print "%12.6f %12.6f %12.6f %12.6f %12.6f" % i
def plotPoly(xData, yData, coeff): m = len(coeff) x1 = min(xData) x2 = max(xData) dx = (x2 - x1) / 20.0 x = arange(x1, x2 + dx / 10.0, dx) y = zeros((len(x))) * 1.0 for i in range(m): y = y + coeff[i] * x**i xyPlot(xData, yData, 'no', x, y, 'ln')
def set_energy_bins(cls, logemin=None, logemax=None, logedelta=None): """Convenience method for initializing energy bin edge vector.""" if logemin is not None: cls.logemin = logemin if logemax is not None: cls.logemax = logemax if logedelta is not None: cls.logedelta = logedelta cls.energy_bins = int((cls.logemax - cls.logemin) / cls.logedelta) cls.energy_bin_edges = ( num.arange(cls.energy_bins + 1) * cls.logedelta + cls.logemin).tolist()
def getSineWave(freq, samplecount=320): """ Generate a sine wave of frequency 'freq'. The samples are generated at 8khz. The first 'samplecount' samples will be returned. """ from numarray import sin, arange from math import pi sine = sin(arange(HZ)/(HZ/freq) * 2.0 * pi) sine = sine[:samplecount] return sine
def set_energy_bins(cls,logemin=None,logemax=None,logedelta=None): if logemin is None: logemin = cls.logemin if logemax is None: logemax = cls.logemax if logedelta is None: logedelta = cls.logedelta cls.energy_bins = int((logemax-logemin)/logedelta) cls.energy_bin_edges = (num.arange(cls.energy_bins+1)*logedelta+logemin).tolist() print 'Energy Bins ', cls.energy_bin_edges
def create_households_for_estimation(agent_set, dbcon): estimation_set = HouseholdSet(in_base=dbcon, in_storage_type="mysql", in_place="households_for_estimation") agent_set.unload_nonderived_attributes() agent_set.load_dataset(attributes="*") estimation_set.load_dataset(agent_set.get_nonderived_attribute_names()) for attr in agent_set.get_attribute_names(): agent_set.set[attr].set_data(concatenate((estimation_set.set[attr].get_data(), agent_set.set[attr].get_data()))) agent_set.update_id_mapping() agent_set.update_size() return (agent_set, arange(estimation_set.size()))
def plotPoly(xData,yData,coeff): m = len(coeff) x1 = min(xData) x2 = max(xData) dx = (x2 - x1)/20.0 x = arange(x1,x2 + dx/10.0,dx) y = zeros((len(x)))*1.0 for i in range(m): y = y + coeff[i]*x**i xyPlot(xData,yData,'no',x,y,'ln')
def Kcorr(self):#calculate K_u(z) (Blanton et al 2003) for each cluster self.kcorr=N.zeros(len(self.z),'f') self.dL=N.zeros(len(self.z),'f') z=N.arange(0.,1.2,.1) #kc=N.array([0.,0.,.05,.05,.1,.15,.2,.225,.25,.3,.35,.35],'f')#Ku(z) kc=N.array([0.,0.,.025,.05,.07,.1,.14,.16,.2,.25,.25,.3],'f')#Kg(z) r=self.z/.01 for i in range(len(self.z)): self.kcorr[i]=kc[int(r[i])]+(r[i]-int(r[i]))*(kc[int(r[i]+1)]-kc[int(r[i])]) self.dL[i] = my.dL(self.z[i],h100)
def testMul(self): """ test multiplication """ a = Table(['a','b','c','d'],[2,3,4,5],range(2*3*4*5)) b = Table(['c','b','e'],[4,3,6],range(12*6)) c = Table(['a','b','c','d','e'],[2,3,4,5,6],range(2*3*4*5*6)) acpt = a.cpt[...,na.NewAxis] bcpt = b.cpt[...,na.NewAxis,na.NewAxis] bcpt = na.transpose(bcpt,[3,1,0,4,2]) resab = acpt*bcpt ab = a*b cc = c*c bb = b*b assert (ab == Table(['a','b','c','d','e'],[2,3,4,5,6],resab) and \ cc == Table(['a','b','c','d','e'],[2,3,4,5,6],na.arange(2*3*4*5*6)**2) and \ bb == Table(['c','b','e'],[4,3,6],na.arange(12*6)**2)), \ " Multiplication does not work"
def testEq(self): a = Table(['a','b'],[2,3],range(6),'Float32') b = Table(['a','b'],[2,3],range(6),'Float32') c = Table(['a'],[6],range(6),'Float32') d = na.arange(6,shape=(2,3)) e = Table(['b','a'],[3,2], na.transpose(a.cpt)) assert(a == b and \ not a == c and \ a == d and \ a == e and e == a), \ "__eq__ does not work"
def run(self): x = self.args[0]*self.args[0] for i in range(10000): SIZE = 10**int(random.random()*3) a = numarray.arange(1, SIZE*SIZE+1, shape=(SIZE,SIZE)) a.transpose() b = a.copy() a = a * x assert numarray.logical_and.reduce(numarray.ravel((a / x) == b)) # if (i % 100 == 0): print self.getName(), sys.stdout.flush()
def testInit(self): a = self.a b = self.b assert(a.g == 0.0 and \ na.allclose(a.h, na.zeros(3)) and \ na.allclose(a.K, na.zeros(shape=(3,3)))), \ " Error with standard initialization " assert(b.g == 2.0 and \ na.allclose(b.h, na.array([1,2,3], type='Float32')) and \ na.allclose(b.K, na.arange(9,shape=(3,3), type='Float32'))), \ " Error with standard initialization with parameter setting"
def fig6(m,w): # Metallicity distribution """ fig6(t,w) -- differential metallicity distribution """ mmin,mmax,mstep = -2.5,1.0,0.2 mmin = mmin-mstep mbins = numarray.arange(mmin,mmax,mstep) indices = numarray.searchsorted(mbins,m) weight = numarray.zeros(len(mbins))*0. for i in range(len(m)): weight[indices[i]] += w[i] pylab.bar(mbins[1:],weight[:-1],width=mstep,edgecolor=None) pylab.xlabel("[Fe/H]") pylab.ylabel("N") return mbins[1:],weight[:-1]
def MaskToList(mask): "Turn a mask into a list of rows to delete" mask = numarray.array( mask, numarray.Bool) mask = numarray.logical_not(mask) xr=numarray.arange( len(mask) )[mask] xr=numarray.array(xr) res=pybnfits.LongVector(len(xr)) for i,v in enumerate(xr): res[i]=v+1 return res
def fig7b(t,w): # Age distribution """ fig7b(t,w) -- differential age distribution """ tmin,tmax,tstep = 0.0,14.5,0.5 tmin = tmin-tstep tbins = numarray.arange(tmin,tmax,tstep) t_gyr=10.**t/1.e9 indices = numarray.searchsorted(tbins,t_gyr) weight = numarray.zeros(len(tbins))*0. for i in range(len(w)): weight[indices[i]] += w[i] pylab.bar(tbins[1:],weight[:-1],width=tstep,edgecolor=None) pylab.xlabel("Age (Gyr)") pylab.ylabel("N") return tbins[1:],weight[:-1]
def JCMTArrayToHDU(ar, pixsize_arcsecs, dz): "Convert array to table HDU" dx=numarray.zeros( ar.shape, numarray.Float64) dy=numarray.zeros( ar.shape, numarray.Float64) for j in range(ar.shape[1]): dx[:,j]= numarray.arange(-ar.shape[0]/2.0 * pixsize_arcsecs, ar.shape[0]/2.0 * pixsize_arcsecs, pixsize_arcsecs) for i in range(ar.shape[0]): dy[i,:]= numarray.arange(-ar.shape[1]/2.0 * pixsize_arcsecs, ar.shape[1]/2.0 * pixsize_arcsecs, pixsize_arcsecs) # Convert to radians dx *= math.pi / 180 / 3600 dy *= math.pi / 180 / 3600 coldefs = [ pyfits.Column ( "DX", "E", "radians", array=dx.flat ), pyfits.Column ( "DY", "E", "radians", array=dy.flat), pyfits.Column ( "fnu", "E", "Jy", array=ar.flat ), pyfits.Column ( "UFNU", "E", "Jy", array=numarray.ones(len(dx.flat))), pyfits.Column ( "TIME", "E", "d", array=numarray.arange(len(dx.flat))) ] nh = pyfits.new_table( coldefs ) nh.header.update("dz", dz ) return nh